Skip to content

Commit d06157b

Browse files
committed
fix unit tests
1 parent 4993dfa commit d06157b

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

crypto/tls/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"container/list"
1010
"context"
11-
"crypto/rand"
1211
"errors"
1312
"fmt"
1413
"io"
@@ -24,6 +23,7 @@ import (
2423
"github.com/runZeroInc/excrypto/crypto/ecdsa"
2524
"github.com/runZeroInc/excrypto/crypto/ed25519"
2625
"github.com/runZeroInc/excrypto/crypto/elliptic"
26+
"github.com/runZeroInc/excrypto/crypto/internal/rand"
2727
"github.com/runZeroInc/excrypto/crypto/rsa"
2828
"github.com/runZeroInc/excrypto/crypto/sha512"
2929
"github.com/runZeroInc/excrypto/crypto/tls/internal/fips140tls"

crypto/x509/fingerprint.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/hex"
1010
"encoding/json"
1111

12+
"github.com/runZeroInc/excrypto/crypto/internal/fips140only"
1213
"github.com/runZeroInc/excrypto/crypto/md5"
1314
"github.com/runZeroInc/excrypto/crypto/sha1"
1415
"github.com/runZeroInc/excrypto/crypto/sha256"
@@ -20,13 +21,21 @@ import (
2021
type CertificateFingerprint []byte
2122

2223
// MD5Fingerprint creates a fingerprint of data using the MD5 hash algorithm.
24+
// Returns nil when FIPS 140-only mode is enforced, since MD5 is not allowed.
2325
func MD5Fingerprint(data []byte) CertificateFingerprint {
26+
if fips140only.Enforced() {
27+
return nil
28+
}
2429
sum := md5.Sum(data)
2530
return sum[:]
2631
}
2732

2833
// SHA1Fingerprint creates a fingerprint of data using the SHA1 hash algorithm.
34+
// Returns nil when FIPS 140-only mode is enforced, since SHA-1 is not allowed.
2935
func SHA1Fingerprint(data []byte) CertificateFingerprint {
36+
if fips140only.Enforced() {
37+
return nil
38+
}
3039
sum := sha1.Sum(data)
3140
return sum[:]
3241
}

crypto/x509/parser.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/runZeroInc/excrypto/crypto/ecdsa"
2222
"github.com/runZeroInc/excrypto/crypto/ed25519"
2323
"github.com/runZeroInc/excrypto/crypto/elliptic"
24+
"github.com/runZeroInc/excrypto/crypto/internal/fips140only"
2425
"github.com/runZeroInc/excrypto/crypto/rsa"
2526
"github.com/runZeroInc/excrypto/crypto/sha256"
2627
"github.com/runZeroInc/excrypto/crypto/x509/pkix"
@@ -31,6 +32,16 @@ import (
3132
cryptobyte_asn1 "github.com/runZeroInc/excrypto/x/crypto/cryptobyte/asn1"
3233
)
3334

35+
// signatureUsesNonFIPSHash reports whether the given signature algorithm
36+
// uses a hash (MD5, SHA-1) that panics in FIPS 140-only mode.
37+
func signatureUsesNonFIPSHash(algo SignatureAlgorithm) bool {
38+
switch algo {
39+
case MD2WithRSA, MD5WithRSA, SHA1WithRSA, DSAWithSHA1, ECDSAWithSHA1:
40+
return true
41+
}
42+
return false
43+
}
44+
3445
// isPrintable reports whether the given b is in the ASN.1 PrintableString set.
3546
// This is a simplified version of encoding/asn1.isPrintable.
3647
func isPrintable(b byte) bool {
@@ -1288,8 +1299,12 @@ func parseCertificate(in *certificate) (*Certificate, error) {
12881299
// zcrypto: Check if self-signed
12891300
if bytes.Equal(cert.RawSubject, cert.RawIssuer) {
12901301
// Possibly self-signed, check the signature against itself.
1291-
if err := cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature); err == nil {
1292-
cert.SelfSigned = true
1302+
// Skip in FIPS 140-only mode when the signature algorithm uses
1303+
// a hash that panics (MD5, SHA-1). The self-signed flag is informational.
1304+
if !fips140only.Enforced() || !signatureUsesNonFIPSHash(cert.SignatureAlgorithm) {
1305+
if err := cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature); err == nil {
1306+
cert.SelfSigned = true
1307+
}
12931308
}
12941309
}
12951310

0 commit comments

Comments
 (0)