Skip to content
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: 15 additions & 7 deletions types/certificate_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package types
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"time"

"crypto/sha1"

"github.com/cloudfoundry/bosh-utils/errors"
)

Expand Down Expand Up @@ -60,10 +59,15 @@ func (cfg CertificateGenerator) Generate(parameters interface{}) (interface{}, e
return cfg.generateCertificate(params)
}

func (cfg CertificateGenerator) bigIntHash(n *big.Int) []byte {
h := sha1.New()
h.Write(n.Bytes())
return h.Sum(nil)
// computeSubjectKeyId derives the SubjectKeyIdentifier per RFC 7093 Method 4:
// SHA-256 of the full DER-encoded SubjectPublicKeyInfo structure.
func computeSubjectKeyId(pub *rsa.PublicKey) ([]byte, error) {
pubDER, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return nil, err
}
hash := sha256.Sum256(pubDER)
return hash[:], nil
}

func (cfg CertificateGenerator) generateCertificate(cParams certParams) (CertResponse, error) {
Expand Down Expand Up @@ -94,7 +98,11 @@ func (cfg CertificateGenerator) generateCertificate(cParams certParams) (CertRes
}
}

certTemplate.SubjectKeyId = cfg.bigIntHash(privateKey.N)
subjectKeyId, err := computeSubjectKeyId(&privateKey.PublicKey)
if err != nil {
return certResponse, errors.WrapError(err, "Computing SubjectKeyId")
}
certTemplate.SubjectKeyId = subjectKeyId

if cParams.IsCA {
certTemplate.KeyUsage = x509.KeyUsageCertSign | x509.KeyUsageCRLSign
Expand Down