duanchun1909 2018-02-21 09:12
浏览 61

未定义:调用* rsa.PublicKey时x509.MarshalPKCS1PublicKey

I'm having trouble accessing the PublicKey field of *rsa.PrivateKey

Whenever I call x509.MarshalPKCS1PublicKey(keyBytes.PublicKey) in the code below, I get:

[tony@localhost app]$ go run gencsr.go
# command-line-arguments
./gencsr.go:37:90: undefined: x509.MarshalPKCS1PublicKey

As you can see, I've included the x509 package and I'm able to access keyBytes.PublicKey and see that it's of type PublicKey.

Sources:
crypto/x509
crypto/rsa

package main

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/x509"
    "crypto/x509/pkix"
    "encoding/asn1"
    "encoding/pem"
    "fmt"
)

var oidEmailAddress = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}

func main() {

    email := ""
    subj := pkix.Name{
        CommonName:         "example.com",
        Country:            []string{"US"},
        Province:           []string{"New York"},
        Locality:           []string{"Albany"},
        Organization:       []string{"My Company Ltd"},
        OrganizationalUnit: []string{"IT"},
    }
    bits := 4096

    key, csr := genCSR(subj, email, bits)
    fmt.Printf(key)
    fmt.Printf(csr)

}

func genCSR(subj pkix.Name, email string, bits int) (pemKey, csr string) {

    keyBytes, _ := rsa.GenerateKey(rand.Reader, bits)
    pemPubKey := string(pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: x509.MarshalPKCS1PublicKey(keyBytes.PublicKey)}))
    pemKey = string(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(keyBytes)}))
    fmt.Printf(pemPubKey)

    rawSubj := subj.ToRDNSequence()
    if len(email) > 0 {
        rawSubj = append(rawSubj, []pkix.AttributeTypeAndValue{
            {Type: oidEmailAddress, Value: email},
        })
    }

    asn1Subj, _ := asn1.Marshal(rawSubj)
    template := x509.CertificateRequest{
        RawSubject:         asn1Subj,
        SignatureAlgorithm: x509.SHA256WithRSA,
    }

    csrBytes, _ := x509.CreateCertificateRequest(rand.Reader, &template, keyBytes)
    csr = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes}))

    return pemKey, csr
}
  • 写回答

1条回答 默认 最新

  • dongyupen6269 2018-02-21 09:17
    关注

    x509.MarshalPKCS1PublicKey is new in Go 1.10. You are probably running an older version.

    If you can't upgrade: the function is very simple and you can just copy it into your own package:

    // pkcs1PublicKey reflects the ASN.1 structure of a PKCS#1 public key.
    type pkcs1PublicKey struct {
        N *big.Int
        E int
    }
    
    // MarshalPKCS1PublicKey converts an RSA public key to PKCS#1, ASN.1 DER form.
    func MarshalPKCS1PublicKey(key *rsa.PublicKey) []byte {
        derBytes, _ := asn1.Marshal(pkcs1PublicKey{
                N: key.N,
                E: key.E,
        })
        return derBytes
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题