Skip to content

Commit

Permalink
[X509] simplify ParsePKIXPublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyang2 committed Feb 9, 2022
1 parent bb11c68 commit 40159e6
Showing 1 changed file with 4 additions and 28 deletions.
32 changes: 4 additions & 28 deletions smx509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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,
algo := getPublicKeyAlgorithmFromOID(pki.Algorithm.Algorithm)
if algo == UnknownPublicKeyAlgorithm {
return nil, errors.New("x509: unknown public key algorithm")
}
return pub, nil
return parsePublicKey(algo, &pki)
}

func marshalPublicKey(pub interface{}) (publicKeyBytes []byte, publicKeyAlgorithm pkix.AlgorithmIdentifier, err error) {
Expand Down

0 comments on commit 40159e6

Please sign in to comment.