mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 12:16:20 +08:00
Merge pull request #26 from easyops-cn/polish
[X509] simplify ParsePKIXPublicKey
This commit is contained in:
commit
949540a65b
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user