Skip to content

Commit

Permalink
Enhance casigner v2 1 (#237)
Browse files Browse the repository at this point in the history
* more simpliciation and compat check test

* actually fixing the test
  • Loading branch information
cviecco authored Jun 12, 2024
1 parent a40a44c commit 05a3c17
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
15 changes: 5 additions & 10 deletions lib/certgen/certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,13 @@ func derBytesCertToCertAndPem(derBytes []byte) (*x509.Certificate, string, error
// Thus we will keep the rsa behaviour for compatiblity reasons
// But for all other keys we will just return the pkix asn1 encoding
// of the public key
func getKMCompatbileKeyStableBytesForSerial(priv interface{}, commonName []byte) ([]byte, error) {
switch v := priv.(type) {
case *rsa.PrivateKey:
func getKMCompatbileKeyStableBytesForSerial(signer crypto.Signer, commonName []byte) ([]byte, error) {
swRSA, ok := signer.(*rsa.PrivateKey)
if ok {
sum := sha256.Sum256(commonName)
return v.Sign(rand.Reader, sum[:], crypto.SHA256)
case *ecdsa.PrivateKey:
return x509.MarshalPKIXPublicKey(v.Public())
case ed25519.PrivateKey:
return x509.MarshalPKIXPublicKey(v.Public())
default:
return nil, fmt.Errorf("Type not recognized %T!\n", v)
return swRSA.Sign(rand.Reader, sum[:], crypto.SHA256)
}
return x509.MarshalPKIXPublicKey(signer.Public())
}

// return both an internal representation an the pem representation of the string
Expand Down
11 changes: 10 additions & 1 deletion lib/certgen/certgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,14 @@ func TestGenx509CertGoodWithRealm(t *testing.T) {
// 6. kerberos realm info!
}

const testSignerSerialNumberCompatValue = "qQn21Wskjm7BubrPwWnFh4swblslkB/H+LxFqOSvl3I="

// GenSelfSignedCACert
func TestGenSelfSignedCACertGood(t *testing.T) {
validPemKeys := []string{testSignerPrivateKey, pkcs8ecPrivateKey, pkcs8Ed25519PrivateKey}
publcKeyPems := []string{testUserPEMPublicKey, testP224PublicKey}

for _, signerPem := range validPemKeys {
for signerIndex, signerPem := range validPemKeys {
caPriv, err := GetSignerFromPEMBytes([]byte(signerPem))
if err != nil {
t.Fatal(err)
Expand All @@ -575,6 +577,13 @@ func TestGenSelfSignedCACertGood(t *testing.T) {
t.Fatal(err)
}
t.Logf("got '%s'", pemCert)
t.Logf("certSerial='%s'", cert.Subject.SerialNumber)
if signerIndex == 0 { //
//TODO we need a better check for when using the rsa private key
if cert.Subject.SerialNumber != testSignerSerialNumberCompatValue {
t.Fatal("rsa compat serial number does not match")
}
}

derCaCert2, err := GenSelfSignedCACert("some hostname", "some organization", caPriv)
if err != nil {
Expand Down

0 comments on commit 05a3c17

Please sign in to comment.