duandan9680 2019-05-05 20:41
浏览 185
已采纳

使用x509.ParseCertificateRequest解组失败,但是openssl可以吗?

When i try to load the CSR (from a PEM line) golang does not parse the ASN.1 data inside the CSR correctly. It spits out that the sequence is truncated.

Openssl on the other hand is fine with the CSR and prints out the correct CN.

Here is the generation code (in c#):

public string generateCSR(string cn) 
    {
        try
        {
            AsymmetricCipherKeyPair pair = this.keyHolder.keypair;

            var subject = new X509Name("CN=" + cn);
            var pkcs10CertificationRequest = new Pkcs10CertificationRequest
                (PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id, subject, pair.Public, null, pair.Private);

            string csr = Convert.ToBase64String(pkcs10CertificationRequest.GetDerEncoded());
            Debug.Log(csr);
            return csr;
        } catch(Exception e) 
        {
            Debug.LogError(e);
        }

        return null;
    }

That gets send directly via http to the golang service (without any url encoding etc.)

Golang server side:

// Try to parse CSR
bytes := make([]byte, base64.StdEncoding.DecodedLen(len(csrData)))
n, err := base64.StdEncoding.Decode(bytes, csrData)
if err != nil {
    fmt.Printf("Error whilst parsing PEM: %v
", err)
    resp.WriteHeader(http.StatusBadRequest)
    return
}

_, err = x509.ParseCertificateRequest(bytes[:n])
if err != nil {
    fmt.Printf("Error whilst parsing ASN.1/REQ CSR: %v
", err)
    resp.WriteHeader(http.StatusBadRequest)
    return
}

Test CSR (in PEM):

MIIEWTCCAkECAQAwFjEUMBIGA1UEAwwLdGVzdEFjY291bnQwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCHu2/2GXBGlEuIWAjhBHXRnT17Pod9aPbTnLwfVSD+1xjchsu2rB83LtSWlfRXoIs4geUTvr2io0iP/kU+IzVZwL/CMzPWoWwq4VH5nQ5QMqCCMEF2g90BmIlLgaopNuQPfAEO64d+3dSFnha3QLbmv2xXe3WwYJ+RTvf+Kij0jgeB3MGzEmlVDiVWlaH4TO5LzY5/CbQgtfDHmhp4LDGe+9vbSkX/m4TXMEg4//21oe7YKO4spM1z9pIUsk8vBb0jDwRFnYRhR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qIO0mgZm5sEpisBZMgCJ9AmK36u7wjqZZC6jnuxYoWg==

Is there any option i miss in the ASN parser in go? Why is openssl fine with that input?

  • 写回答

1条回答 默认 最新

  • doushen2154 2019-05-06 16:05
    关注

    I found the answer to this problem.

    Bouncycastle does not add a0:00 to the attributes when no attribute instance is given (null). This leads to incomplete encoded data.

    "If you just see: Attributes: then the SET OF is missing and the encoding is technically invalid (but it is tolerated)." - https://www.openssl.org/docs/man1.0.2/man1/openssl-req.html

    The solution is to provide a empty DerSet to the CSR which leads to the a0:00 generation indicating there are no attributes present.

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

报告相同问题?

悬赏问题

  • ¥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里的文字?