diff --git a/x509/x509.go b/x509/x509.go index ea92800a..cf314a61 100644 --- a/x509/x509.go +++ b/x509/x509.go @@ -614,8 +614,28 @@ var ( oidPublicKeyRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} oidPublicKeyDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1} oidPublicKeyECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1} + oidPublicKeySM2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301} ) +func getPublicKeyAlgorithmFromAlgorithm(alg pkix.AlgorithmIdentifier) PublicKeyAlgorithm { + oid := alg.Algorithm + switch { + case oid.Equal(oidPublicKeyRSA): + return RSA + case oid.Equal(oidPublicKeyDSA): + return DSA + case oid.Equal(oidPublicKeyECDSA): + parameters := alg.Parameters + var sm2Oid = asn1.ObjectIdentifier{} + _, err := asn1.Unmarshal(parameters.FullBytes, &sm2Oid) + if err == nil && sm2Oid.Equal(oidPublicKeySM2) { + return SM2 + } + return ECDSA + } + return UnknownPublicKeyAlgorithm +} + func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) PublicKeyAlgorithm { switch { case oid.Equal(oidPublicKeyRSA): @@ -1260,7 +1280,7 @@ func parseCertificate(in *certificate) (*Certificate, error) { getSignatureAlgorithmFromAI(in.TBSCertificate.SignatureAlgorithm) out.PublicKeyAlgorithm = - getPublicKeyAlgorithmFromOID(in.TBSCertificate.PublicKey.Algorithm.Algorithm) + getPublicKeyAlgorithmFromAlgorithm(in.TBSCertificate.PublicKey.Algorithm) var err error out.PublicKey, err = parsePublicKey(out.PublicKeyAlgorithm, &in.TBSCertificate.PublicKey) if err != nil {