Skip to content

Commit

Permalink
MAGIC - fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Emman committed Jun 28, 2021
1 parent 9a011f1 commit 0469562
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
26 changes: 24 additions & 2 deletions smx509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,22 @@ func signaturePublicKeyAlgoMismatchError(expectedPubKeyAlgo x509.PublicKeyAlgori
return fmt.Errorf("x509: signature algorithm specifies an %s public key, but have public key of type %T", expectedPubKeyAlgo.String(), pubKey)
}

func verifyECDSAASN1(pub *ecdsa.PublicKey, hash, sig []byte) bool {
var (
r, s = &big.Int{}, &big.Int{}
inner cryptobyte.String
)
input := cryptobyte.String(sig)
if !input.ReadASN1(&inner, cryptobyte_asn1.SEQUENCE) ||
!input.Empty() ||
!inner.ReadASN1Integer(r) ||
!inner.ReadASN1Integer(s) ||
!inner.Empty() {
return false
}
return ecdsa.Verify(pub, hash, r, s)
}

// checkSignature verifies that signature is a valid signature over signed from
// a crypto.PublicKey.
func checkSignature(algo x509.SignatureAlgorithm, signed, signature []byte, publicKey crypto.PublicKey) (err error) {
Expand Down Expand Up @@ -634,9 +650,14 @@ func checkSignature(algo x509.SignatureAlgorithm, signed, signature []byte, publ
if pubKeyAlgo != x509.ECDSA {
return signaturePublicKeyAlgoMismatchError(pubKeyAlgo, pub)
}
if (!isSM2 && !ecdsa.VerifyASN1(pub, signed, signature)) || !sm2.VerifyASN1WithSM2(pub, nil, signed, signature) {
if isSM2 {
if !sm2.VerifyASN1WithSM2(pub, nil, signed, signature) {
return errors.New("x509: ECDSA verification failure")
}
} else if !verifyECDSAASN1(pub, signed, signature) {
return errors.New("x509: ECDSA verification failure")
}
return
case ed25519.PublicKey:
if pubKeyAlgo != x509.Ed25519 {
return signaturePublicKeyAlgoMismatchError(pubKeyAlgo, pub)
Expand Down Expand Up @@ -2336,7 +2357,8 @@ func parseCertificateRequest(in *certificateRequest) (*CertificateRequest, error
RawSubjectPublicKeyInfo: in.TBSCSR.PublicKey.Raw,
RawSubject: in.TBSCSR.Subject.FullBytes,

Signature: in.SignatureValue.RightAlign(),
Signature: in.SignatureValue.RightAlign(),
SignatureAlgorithm: getSignatureAlgorithmFromAI(in.SignatureAlgorithm),

PublicKeyAlgorithm: getPublicKeyAlgorithmFromOID(in.TBSCSR.PublicKey.Algorithm.Algorithm),

Expand Down
3 changes: 1 addition & 2 deletions smx509/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ func parseAndCheckCsr(csrPem []byte) error {
if err != nil {
return err
}
fmt.Printf("%v\n", csr)
return csr.CheckSignature()
}

Expand Down Expand Up @@ -217,7 +216,7 @@ func TestCreateCertificateRequest(t *testing.T) {
block := &pem.Block{Bytes: csrblock, Type: "CERTIFICATE REQUEST"}
pemContent := string(pem.EncodeToMemory(block))
fmt.Printf("%s\n", pemContent)
err = parseAndCheckCsr(csrblock)
err = parseAndCheckCsr([]byte(pemContent))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 0469562

Please sign in to comment.