From 40159e654241d31fb2a11ca3620ae3e4cc988297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E8=83=96?= Date: Wed, 9 Feb 2022 09:33:19 +0800 Subject: [PATCH] [X509] simplify ParsePKIXPublicKey --- smx509/x509.go | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/smx509/x509.go b/smx509/x509.go index 7853707..982e218 100644 --- a/smx509/x509.go +++ b/smx509/x509.go @@ -52,35 +52,11 @@ func ParsePKIXPublicKey(derBytes []byte) (interface{}, error) { } else if len(rest) != 0 { return nil, errors.New("x509: trailing data after ASN.1 of public-key") } - - if !pki.Algorithm.Algorithm.Equal(oidPublicKeyECDSA) { - return x509.ParsePKIXPublicKey(derBytes) + algo := getPublicKeyAlgorithmFromOID(pki.Algorithm.Algorithm) + if algo == UnknownPublicKeyAlgorithm { + return nil, errors.New("x509: unknown public key algorithm") } - keyData := &pki - asn1Data := keyData.PublicKey.RightAlign() - paramsData := keyData.Algorithm.Parameters.FullBytes - namedCurveOID := new(asn1.ObjectIdentifier) - rest, err := asn1.Unmarshal(paramsData, namedCurveOID) - if err != nil { - return nil, errors.New("x509: failed to parse ECDSA parameters as named curve") - } - if len(rest) != 0 { - return nil, errors.New("x509: trailing data after ECDSA parameters") - } - if !namedCurveOID.Equal(oidNamedCurveP256SM2) { - return x509.ParsePKIXPublicKey(derBytes) - } - namedCurve := sm2.P256() - x, y := elliptic.Unmarshal(namedCurve, asn1Data) - if x == nil { - return nil, errors.New("x509: failed to unmarshal elliptic curve point") - } - pub := &ecdsa.PublicKey{ - Curve: namedCurve, - X: x, - Y: y, - } - return pub, nil + return parsePublicKey(algo, &pki) } func marshalPublicKey(pub interface{}) (publicKeyBytes []byte, publicKeyAlgorithm pkix.AlgorithmIdentifier, err error) {