Skip to content
Open
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
13 changes: 8 additions & 5 deletions ca/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha512"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
Expand Down Expand Up @@ -76,7 +76,11 @@ func makeSerial() *big.Int {
return serial
}

// Taken from https://github.com/cloudflare/cfssl/blob/b94e044bb51ec8f5a7232c71b1ed05dbe4da96ce/signer/signer.go#L221-L244
// makeSubjectKeyID generates a Subject Key Identifier according to
// RFC 7093 Section 2 method 3: the leftmost 160 bits of the SHA-512 hash of
// the subjectPublicKey BIT STRING (excluding the tag, length, and number of
// unused bits). This differs from Boulder's RFC 7093 Section 2 method 1
// (truncated SHA-256) so ACME clients cannot overfit on Let's Encrypt.
func makeSubjectKeyID(key crypto.PublicKey) ([]byte, error) {
// Marshal the public key as ASN.1
pubAsDER, err := x509.MarshalPKIXPublicKey(key)
Expand All @@ -94,9 +98,8 @@ func makeSubjectKeyID(key crypto.PublicKey) ([]byte, error) {
return nil, err
}

// Hash it according to https://tools.ietf.org/html/rfc5280#section-4.2.1.2 Method #1:
ski := sha1.Sum(pubInfo.SubjectPublicKey.Bytes)
return ski[:], nil
ski := sha512.Sum512(pubInfo.SubjectPublicKey.Bytes)
return ski[:20], nil
}

// makeKey and makeRootCert are adapted from MiniCA:
Expand Down
28 changes: 28 additions & 0 deletions ca/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/hex"
"log"
"net"
"os"
Expand All @@ -30,6 +31,33 @@
return New(logger, db, "", "ecdsa", 0, 1, map[string]Profile{"default": {}})
}

func TestMakeSubjectKeyID(t *testing.T) {
// RFC 7093 Section 3 example P-256 public key.
publicKeyBytes, err := hex.DecodeString(
"047f7f35a79794c950060b8029fc8f363a28f11159692d9d34e6ac948190434735" +
"f833b1a66652dc514337aff7f5c9c75d670c019d95a5d639b72744c64a9128bb",
)
if err != nil {
t.Fatal(err)
}
x, y := elliptic.Unmarshal(elliptic.P256(), publicKeyBytes)

Check failure on line 43 in ca/ca_test.go

View workflow job for this annotation

GitHub Actions / go-lint-checks

SA1019: elliptic.Unmarshal has been deprecated since Go 1.21: for ECDH, use the crypto/ecdh package. This function accepts an encoding equivalent to that of the NewPublicKey methods in crypto/ecdh. (staticcheck)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See lint finding: use crypto/ecdh instead of elliptic.Unmarshal

if x == nil || y == nil {
t.Fatal("failed to parse RFC 7093 public key")
}

got, err := makeSubjectKeyID(&ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y})
if err != nil {
t.Fatal(err)
}
want, err := hex.DecodeString("907e7e9d05878a273d597f2aea91bdb6056245cb")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Fatalf("unexpected subject key identifier: got %x, want %x", got, want)
}
}

func makeCertOrderWithExtensions(extensions []pkix.Extension) core.Order {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
Expand Down
Loading