13

我正在尝试在 Go 中实现Chef API 客户端,但一直在尝试创建正确的请求标头 RSA 签名。根据文件

规范标头使用发送请求的客户端机器使用的私钥进行签名,并且也使用 Base64 进行编码。

以下 ruby​​ 调用OpenSSL::PKey::RSA.private_encrypt()可以在mixlib-authentication gem 代码中找到,它使用OpenSSL 绑定private_encrypt()方法调用RSA_private_encrypt openssl 函数

不幸的是,我在 Go 的标准库中找不到匹配的函数;crypto/rsa看起来很接近,但它只实现了传统的加密方法:使用公钥加密,使用私钥进行哈希签名。OpenSSLRSA_private_encrypt则相反:它使用私钥加密(小)消息(类似于从消息哈希创建签名)。

这个“签名”也可以用这个命令来实现:

openssl rsautl -sign -inkey path/to/private/key.pem \
    -in file/to/encrypt -out encrypted/output

是否有任何本机 Go 库可以实现与 OpenSSL 相同的结果RSA_private_encrypt,或者唯一的方法是使用 Cgo 从 OpenSSL 库中调用此函数?也许我错过了一些东西。我的想法是在没有任何非 go 依赖项的情况下实现客户端。

我是 Go 新手,所以我不确定我是否可以深入研究crypto/rsa模块源代码。


找到了类似的问题,但是使用的答案SignPKCS1v15显然是错误的(这个函数加密了消息的哈希,而不是消息本身)。

4

4 回答 4

5

golang社区的大力帮助下,找到了解决方案:

Alex在http://play.golang.org/p/jrqN2KnUEM上发布的原始代码(参见邮件列表)。

我添加了rfc2313第 8 节中指定的输入块大小检查:http ://play.golang.org/p/dGTl9siO8E

这是代码:

package main

import (
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "errors"
    "fmt"
    "io/ioutil"
    "math/big"
    "os/exec"
)

var (
    ErrInputSize  = errors.New("input size too large")
    ErrEncryption = errors.New("encryption error")
)

func PrivateEncrypt(priv *rsa.PrivateKey, data []byte) (enc []byte, err error) {

    k := (priv.N.BitLen() + 7) / 8
    tLen := len(data)
    // rfc2313, section 8:
    // The length of the data D shall not be more than k-11 octets
    if tLen > k-11 {
        err = ErrInputSize
        return
    }
    em := make([]byte, k)
    em[1] = 1
    for i := 2; i < k-tLen-1; i++ {
        em[i] = 0xff
    }
    copy(em[k-tLen:k], data)
    c := new(big.Int).SetBytes(em)
    if c.Cmp(priv.N) > 0 {
        err = ErrEncryption
        return
    }
    var m *big.Int
    var ir *big.Int
    if priv.Precomputed.Dp == nil {
        m = new(big.Int).Exp(c, priv.D, priv.N)
    } else {
        // We have the precalculated values needed for the CRT.
        m = new(big.Int).Exp(c, priv.Precomputed.Dp, priv.Primes[0])
        m2 := new(big.Int).Exp(c, priv.Precomputed.Dq, priv.Primes[1])
        m.Sub(m, m2)
        if m.Sign() < 0 {
            m.Add(m, priv.Primes[0])
        }
        m.Mul(m, priv.Precomputed.Qinv)
        m.Mod(m, priv.Primes[0])
        m.Mul(m, priv.Primes[1])
        m.Add(m, m2)

        for i, values := range priv.Precomputed.CRTValues {
            prime := priv.Primes[2+i]
            m2.Exp(c, values.Exp, prime)
            m2.Sub(m2, m)
            m2.Mul(m2, values.Coeff)
            m2.Mod(m2, prime)
            if m2.Sign() < 0 {
                m2.Add(m2, prime)
            }
            m2.Mul(m2, values.R)
            m.Add(m, m2)
        }
    }

    if ir != nil {
        // Unblind.
        m.Mul(m, ir)
        m.Mod(m, priv.N)
    }
    enc = m.Bytes()
    return
}

func main() {
    // o is output from openssl
    o, _ := exec.Command("openssl", "rsautl", "-sign", "-inkey", "t.key", "-in", "in.txt").Output()

    // t.key is private keyfile
    // in.txt is what to encode
    kt, _ := ioutil.ReadFile("t.key")
    e, _ := ioutil.ReadFile("in.txt")
    block, _ := pem.Decode(kt)
    privkey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
    encData, _ := PrivateEncrypt(privkey, e)
    fmt.Println(encData)
    fmt.Println(o)
    fmt.Println(string(o) == string(encData))
}

更新:我们可以期待在 Go 1.3 中对这种类型的登录提供原生支持,请参阅相应的提交

于 2013-10-27T17:24:34.680 回答
3

从 go 开始1.3,您可以轻松地使用SignPKCS1v15

rsa.SignPKCS1v15(nil, priv, crypto.Hash(0), signedData) 

参考:https ://groups.google.com/forum/#!topic/Golang-Nuts/Vocj33WNhJQ

于 2015-04-20T04:09:04.173 回答
1

我在这个问题上停留了一段时间。

最终,我用这里的代码解决了这个问题: https ://github.com/bitmartexchange/bitmart-go-api/blob/master/bm_client.go

// Sign secret with rsa with PKCS 1.5 as the padding algorithm
// The result should be exactly same as "openssl rsautl -sign -inkey "YOUR_RSA_PRIVATE_KEY" -in "YOUR_PLAIN_TEXT""
signer, err := rsa.SignPKCS1v15(rand.Reader, rsaPrivateKey.(*rsa.PrivateKey), crypto.Hash(0), []byte(message))
于 2018-06-25T05:43:20.567 回答
0

欢迎来到 openssl 的乐趣……这是一个非常糟糕的命名函数。如果你在 ruby​​ 代码中四处寻找,它会调用这个 openssl 函数

http://www.openssl.org/docs/crypto/RSA_private_encrypt.html

阅读文档,这实际上是用私钥对缓冲区进行签名,而不是对其进行加密。

描述

这些函数在低级别处理 RSA 签名。

RSA_private_encrypt() 使用私钥 rsa 对 from(通常是带有算法标识符的消息摘要)处的 flen 字节进行签名,并将签名存储到 to。to 必须指向内存的 RSA_size(rsa) 字节。

于 2013-10-20T18:14:42.573 回答