dongxin0031 2019-03-20 05:13
浏览 251
已采纳

从JSON Golang读取PGP密钥时出现EOF错误

I'm developing an API using Golang, and I have a JSON file keys.json as follows:

{
  "publicKeys": {
    "Flex": "<valid pgp public key>",
    "Flex2": "<valid pgp public key>"
  },
  "privateKey": "<valid pgp private key>"
}

To unmarshal this, I have the following model

type PGPKeys struct {
    PublicKeys map[string]string `json:"publicKeys"`
    PrivateKey string            `json:"privateKey"`
}

and I unmarshal the code using

keysJSONFile, err := os.Open(keysPath)
    if keysJSONFile != nil {
        defer keysJSONFile.Close()
    }
    if err != nil {
        return nil, err
    }

    keysJSONBytes, err := ioutil.ReadAll(keysJSONFile)
    if err != nil {
        return nil, err
    }

    var pgpKeys PGPKeys
    err = json.Unmarshal(keysJSONBytes, &pgpKeys)
    if err != nil {
        return nil, err
    }

Later, when I use openpgp to get the public key packet, I am met with EOF error which armor.Decode returns when it's unable to find any blocks -- but I'm not sure why it's happening

func GetPublicKeyPacket(publicKey []byte) (*packet.PublicKey, error) {
    publicKeyReader := bytes.NewReader(publicKey)
    block, err := armor.Decode(publicKeyReader)
    if err != nil {
        return nil, err
    }

    if block.Type != openpgp.PublicKeyType {
        return nil, errors.New("Invalid public key data")
    }

    packetReader := packet.NewReader(block.Body)
    pkt, err := packetReader.Next()
    if err != nil {
        return nil, err
    }

    key, ok := pkt.(*packet.PublicKey)
    if !ok {
        return nil, err
    }
    return key, nil
}

NOTE: When I call the function, I do type conversion using something like

publicKeyPacket, err := pgp.GetPublicKeyPacket([]byte(h.PGPKeys.PublicKeys[h.Config.PGPIdentifier]))

Finally, I have tried moving the keys into individual TXT files and that works but for some reason having them in JSON does not

  • 写回答

1条回答 默认 最新

  • dragon7713 2019-03-20 05:59
    关注

    I found a solution to this while randomly trying stuff, and I am as surprised as you (future answer reader) are. If someone can provide an explanation, I'd be thankful.

    I was storing the keys in JSON by replacing all newlines with " " so that I can store it in a single line. You know how PGP keys have an empty line right after -----BEGIN PGP PUBLIC KEY BLOCK-----? Well, in my file, I had something like

    "publicKeys": {
        "Flex": "-----BEGIN PGP PUBLIC KEY BLOCK-----
    Qfdsf...."
    }
    

    Adding that extra empty line, i.e. changing it to

    "publicKeys": {
        "Flex": "-----BEGIN PGP PUBLIC KEY BLOCK-----
    
    Qfdsf...."
    }
    

    EDIT: As @Adrian mentioned in the comments, this was because it's an invalid PGP key if the line isn't there anymore according to the RFC spec. This is how it should be.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?