首頁  >  文章  >  後端開發  >  tls.X509KeyPair 彈出「無法在憑證輸入中找到任何 PEM 資料」錯誤

tls.X509KeyPair 彈出「無法在憑證輸入中找到任何 PEM 資料」錯誤

WBOY
WBOY轉載
2024-02-05 23:00:081281瀏覽

tls.X509KeyPair 弹出“无法在证书输入中找到任何 PEM 数据”错误

問題內容

我正在嘗試透過 golang x509 套件為 https 伺服器製作 tls 憑證

我收到了這個錯誤

tls: failed to find any pem data in certificate input

經過一番研究,我像這樣創建了我的憑證

func generatecert()  {
    ca := &x509.certificate{
        serialnumber: big.newint(2023),
        subject: pkix.name{
            organization:       []string{"company"},
            organizationalunit: []string{"lol"},
            country:            []string{"us"},
            province:           []string{""},
            locality:           []string{"ny"},
            streetaddress:      []string{"no street"},
            postalcode:         []string{"77777"},
        },
        notbefore:             time.now(),
        notafter:              time.now().adddate(10, 0, 0),
        subjectkeyid:          []byte{1, 2, 3, 4, 5},
        basicconstraintsvalid: true,
        isca:                  true,
        extkeyusage:           []x509.extkeyusage{x509.extkeyusageclientauth, x509.extkeyusageserverauth},
        keyusage:              x509.keyusagedigitalsignature | x509.keyusagecertsign,
    }

    certpubl, certpriv, err := ed25519.generatekey(rand.reader)
    if err != nil {
        log.println("key generate failed", err)
        return
    }

    certcert, err := x509.createcertificate(rand.reader, ca, ca, certpubl, certpriv)
    if err != nil {
        log.println("create cert failed", err)
        return
    }

    out := &bytes.buffer{}

    //encoding cert
    certtestpem := &pem.block{type: "certificate", bytes: certcert}
    pem.encode(out, certtestpem)
    publiccert := out.bytes()
    certderblock, publiccert := pem.decode(publiccert)

    //check decoded cert
    print(certderblock.type, "\n")
    if publiccert != nil {
        print("publiccert nil\n")
    }

    //encoding private key
    out.reset()
    privatepem, _ := x509.marshalpkcs8privatekey(certpriv)
    pem.encode(out, &pem.block{type: "private key", bytes: privatepem})
    privitkey := out.bytes()

    //check keypair
    _, err = tls.x509keypair(publiccert, privitkey)
    if err != nil {
        print(err.error())
    }
}

它顯示如下錯誤

CERTIFICATE
publicCert nil
tls: failed to find any PEM data in certificate input

我嘗試在 pem.encodetomemory 之後解碼

pem.type 是正確的,但變數「publiccert」為零,我嘗試新增 \n 憑證的開頭,它什麼也沒做,但憑證本身不為零,有人可以幫助我嗎 p>

我該如何做才能讓 tls 正常工作? ? ?


正確答案


這段程式碼有幾個問題

publiccert := out.bytes()

現階段檢查publiccert的內容顯示了期望值。但以下語句將簡單地覆寫 publiccert

certderblock, publiccert := pem.decode(publiccert)

透過檢查此語句後面的 publiccert 可以看到這一點。 如文件所述 publiccert 現在將在實際憑證之後顯示資料。

應該是這樣

certderblock, _ := pem.decode(publiccert)

檢查此修正語句後的 publiccert 內容再次顯示預期值。

out.reset()
privatepem, _ := x509.marshalpkcs8privatekey(certpriv)
pem.encode(out, &pem.block{type: "private key", bytes: privatepem})
privitkey := out.bytes()

這會將預期值取得到 privitkey 中。但是,它將更改 publiccert,因為它只是 out 的一部分,並且 out 已更改操作。因此,out 現在將在開頭包含 privitkey,而不再是憑證的開頭 - 這反映在 publiccert 的值中。

另請參閱bytes.buffer.bytes 的文檔

切片僅在下一次緩衝區修改之前有效(即,僅在下次呼叫 read、write、reset 或 truncate 等方法之前有效)

因此,不只是重置現有緩衝區

out.reset()

最好為 privitkey 建立一個新緩衝區,並為 publiccert 保留現有緩衝區

out = &bytes.Buffer{}

以上是tls.X509KeyPair 彈出「無法在憑證輸入中找到任何 PEM 資料」錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除