Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Golang openpgp 库中 GPG 密钥签名错误分析与解决方案_创想鸟

Golang openpgp 库中 GPG 密钥签名错误分析与解决方案

Golang openpgp 库中 GPG 密钥签名错误分析与解决方案

本文深入探讨了使用 go 语言 `go.crypto/openpgp` 库进行 gpg 公钥签名时可能遇到的“签名无效”问题。核心原因在于该库早期版本中 `signidentity` 方法的实现缺陷,它错误地使用了子密钥签名算法而非用户id签名算法。文章将分析此问题,并强调使用最新 `golang.org/x/crypto/openpgp` 库的重要性,以确保加密操作的正确性和安全性。

引言:GPG 密钥签名在 Go 中的挑战

GPG(GNU Privacy Guard)签名是确保数据完整性、来源真实性和不可否认性的重要手段。在 Go 语言中,openpgp 库提供了实现 GPG 加密和签名的能力。然而,开发者在使用该库进行公钥签名时,可能会遇到生成的签名在外部 GPG 工具(如 gpg –check-sigs)验证时显示为“bad Signature”的问题。这通常不是因为代码逻辑上的明显错误,而是由于库底层实现的特定缺陷。

问题复现:早期 openpgp 库的签名逻辑

为了理解这个问题,我们首先来看一个典型的 Go 语言中用于签名公钥用户 ID 的代码结构。以下示例展示了如何加载公钥和私钥实体,然后使用私钥对公钥的用户 ID 进行签名:

package mainimport (    "bytes"    "fmt"    "io"    "golang.org/x/crypto/openpgp"    "golang.org/x/crypto/openpgp/armor"    "golang.org/x/crypto/openpgp/packet")// LoadEntityFromArmoredKeyRing 从 ASCII Armored 字符串加载 openpgp.Entity// password 参数仅在加载私钥且私钥加密时需要func LoadEntityFromArmoredKeyRing(armoredKey string, password []byte) (*openpgp.Entity, error) {    keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewReader([]byte(armoredKey)))    if err != nil {        return nil, fmt.Errorf("读取 Armored 密钥环失败: %w", err)    }    if len(keyring) == 0 {        return nil, fmt.Errorf("未找到密钥实体")    }    entity := keyring[0] // 假设密钥环中只有一个实体    // 如果有私钥且需要解密    if entity.PrivateKey != nil && entity.PrivateKey.Encrypted {        if password == nil {            return nil, fmt.Errorf("私钥已加密,但未提供密码")        }        if err := entity.PrivateKey.Decrypt(password); err != nil {            return nil, fmt.Errorf("解密私钥失败: %w", err)        }    }    return entity, nil}// SignPublicKeyWithPrivateKey 使用私钥签名公钥的用户ID// 此函数演示了签名的核心逻辑,并强调了可能出现问题的 SignIdentity 调用func SignPublicKeyWithPrivateKey(    armoredPublicKey string,    armoredPrivateKey string,    privateKeyPassword string,) (string, error) {    // 1. 加载公钥实体    pubEntity, err := LoadEntityFromArmoredKeyRing(armoredPublicKey, nil)    if err != nil {        return "", fmt.Errorf("加载公钥实体失败: %w", err)    }    // 2. 加载私钥实体并解密    priEntity, err := LoadEntityFromArmoredKeyRing(armoredPrivateKey, []byte(privateKeyPassword))    if err != nil {        return "", fmt.Errorf("加载私钥实体失败: %w", err)    }    // 3. 获取公钥的用户ID    var userIdName string    for _, identity := range pubEntity.Identities {        userIdName = identity.Name        break // 通常取第一个用户ID进行签名    }    if userIdName == "" {        return "", fmt.Errorf("公钥实体中未找到用户ID")    }    fmt.Printf("正在使用私钥 '%s' 签名公钥 '%s' 的用户ID '%s'n", priEntity.PrimaryKey.KeyIdString(), pubEntity.PrimaryKey.KeyIdString(), userIdName)    // 4. 执行签名操作    // 核心问题曾发生在此处:早期版本的 openpgp 库在此方法中存在实现缺陷    err = pubEntity.SignIdentity(userIdName, priEntity, nil)    if err != nil {        return "", fmt.Errorf("签名用户ID失败: %w", err)    }    fmt.Println("用户ID签名成功。")    // 5. 将签名的公钥实体序列化为 ASCII Armored 字符串    buf := new(bytes.Buffer)    writer, err := armor.Encode(buf, openpgp.PublicKeyType, nil)    if err != nil {        return "", fmt.Errorf("创建 Armored 编码器失败: %w", err)    }    if err := pubEntity.Serialize(writer); err != nil {        return "", fmt.Errorf("序列化签名的公钥实体失败: %w", err)    }    if err := writer.Close(); err != nil

以上就是Golang openpgp 库中 GPG 密钥签名错误分析与解决方案的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1425720.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Golang如何配置GOPATH和GOROOT_Golang 开发环境变量设置实践
上一篇 2025年12月16日 19:51:11
如何使用Golang搭建Linux开发环境_Golang Linux开发环境实践
下一篇 2025年12月16日 19:51:18

相关推荐

发表回复

登录后才能评论
关注微信