diff --git a/feature/acme/cert.go b/feature/acme/cert.go index 9985efc9f..2effdbc51 100644 --- a/feature/acme/cert.go +++ b/feature/acme/cert.go @@ -542,7 +542,11 @@ func (e *extension) issueACMECert(ctx context.Context, b *ipnlocal.LocalBackend, if ch == nil { return nil, errors.New("tls-alpn-01 challenge not offered") } - cert, err := ac.TLSALPN01ChallengeCert(ch.Token, az.Identifier.Value) + challengeKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) + if err != nil { + return nil, fmt.Errorf("generating TLS-ALPN-01 challenge key: %w", err) + } + cert, err := ac.TLSALPN01ChallengeCert(ch.Token, az.Identifier.Value, xacme.WithKey(challengeKey)) if err != nil { return nil, fmt.Errorf("TLSALPN01ChallengeCert: %w", err) } @@ -595,7 +599,7 @@ func (e *extension) issueACMECert(ctx context.Context, b *ipnlocal.LocalBackend, } args.traceACME(order) - certPrivKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + certPrivKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) if err != nil { return nil, err } diff --git a/feature/acme/cert_test.go b/feature/acme/cert_test.go index 90661ab2c..47d3c1e20 100644 --- a/feature/acme/cert_test.go +++ b/feature/acme/cert_test.go @@ -1384,6 +1384,13 @@ func TestAcmeKey_ConcurrentFirstCall(t *testing.T) { if err != nil { t.Fatalf("parsing persisted key: %v", err) } + ecKey, ok := want.(*ecdsa.PrivateKey) + if !ok { + t.Fatalf("persisted key type = %T, want *ecdsa.PrivateKey", want) + } + if ecKey.Curve != elliptic.P384() { + t.Errorf("persisted account key curve = %s, want P-384", ecKey.Curve.Params().Name) + } wantPub, err := x509.MarshalPKIXPublicKey(want.Public()) if err != nil { t.Fatalf("marshaling persisted pubkey: %v", err) @@ -1398,3 +1405,34 @@ func TestAcmeKey_ConcurrentFirstCall(t *testing.T) { } } } + +// TestAcmeKey_PreservesExistingP256 verifies that an existing persisted +// P-256 ACME account key continues to be loaded without error or replacement. +func TestAcmeKey_PreservesExistingP256(t *testing.T) { + e := &extension{} + cs := certStateStore{StateStore: new(mem.Store)} + + p256Key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatalf("GenerateKey: %v", err) + } + var pemBuf bytes.Buffer + if err := encodeECDSAKey(&pemBuf, p256Key); err != nil { + t.Fatalf("encodeECDSAKey: %v", err) + } + if err := cs.WriteACMEKey(pemBuf.Bytes()); err != nil { + t.Fatalf("WriteACMEKey: %v", err) + } + + got, err := e.acmeKey(cs) + if err != nil { + t.Fatalf("acmeKey: %v", err) + } + gotEC, ok := got.(*ecdsa.PrivateKey) + if !ok { + t.Fatalf("acmeKey returned %T, want *ecdsa.PrivateKey", got) + } + if !gotEC.Equal(p256Key) { + t.Errorf("acmeKey returned a different key than the persisted P-256 key") + } +} diff --git a/feature/acme/certstore.go b/feature/acme/certstore.go index 0c77fbaf0..b2db19832 100644 --- a/feature/acme/certstore.go +++ b/feature/acme/certstore.go @@ -380,7 +380,7 @@ func (e *extension) acmeKey(cs certStore) (crypto.Signer, error) { return nil, err } - privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + privKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) if err != nil { return nil, err } diff --git a/feature/acme/issue_test.go b/feature/acme/issue_test.go index 101b7bb97..b6dfb5989 100644 --- a/feature/acme/issue_test.go +++ b/feature/acme/issue_test.go @@ -9,6 +9,7 @@ import ( "crypto/ecdsa" "crypto/elliptic" "crypto/rand" + "crypto/tls" "crypto/x509" "encoding/base64" "encoding/json" @@ -33,7 +34,7 @@ import ( func newTestACMEClient(t *testing.T, s *acmetest.Server) *xacme.Client { t.Helper() - key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) if err != nil { t.Fatal(err) } @@ -118,6 +119,29 @@ func TestIssueACMECertValidAuthorization(t *testing.T) { if _, err := cs.Read(domain, time.Now()); err != nil { t.Errorf("stored certificate does not read back: %v", err) } + + tlsCert, err := tls.X509KeyPair(pair.CertPEM, pair.KeyPEM) + if err != nil { + t.Fatalf("X509KeyPair: %v", err) + } + ecPriv, ok := tlsCert.PrivateKey.(*ecdsa.PrivateKey) + if !ok { + t.Fatalf("private key type = %T, want *ecdsa.PrivateKey", tlsCert.PrivateKey) + } + if ecPriv.Curve != elliptic.P384() { + t.Errorf("private key curve = %s, want P-384", ecPriv.Curve.Params().Name) + } + leaf, err := x509.ParseCertificate(tlsCert.Certificate[0]) + if err != nil { + t.Fatalf("ParseCertificate: %v", err) + } + ecPub, ok := leaf.PublicKey.(*ecdsa.PublicKey) + if !ok { + t.Fatalf("leaf public key type = %T, want *ecdsa.PublicKey", leaf.PublicKey) + } + if ecPub.Curve != elliptic.P384() { + t.Errorf("leaf public key curve = %s, want P-384", ecPub.Curve.Params().Name) + } } // TestIssueACMECertRetriesWithoutReplaces checks that a renewal order the