Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udpate x509.go , support SM2 Algorithm on parse Certificate PublicKey… #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion x509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 {
Expand Down