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

Enhance casigner v2 1 #237

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
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
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
Loading