Skip to content

Commit 5cf4d0f

Browse files
authored
Merge pull request #15 from ellgreen/serial-cn-support
Add ability to pass Serial Number and Common Name
2 parents 0fbb387 + 129213a commit 5cf4d0f

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

kpconfig.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testcerts
22

33
import (
44
"errors"
5+
"math/big"
56
"net"
67
)
78

@@ -22,6 +23,12 @@ type KeyPairConfig struct {
2223
// IPAddresses is a list of IP addresses to include in the certificate
2324
// as Subject Alternative Names.
2425
IPAddresses []string
26+
27+
// SerialNumber is the serial number to use for the certificate.
28+
SerialNumber *big.Int
29+
30+
// CommonName is the Common Name to use for the certificate.
31+
CommonName string
2532
}
2633

2734
// Validate validates the KeyPairConfig ensuring that it is not empty and that

testcerts.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,21 @@ func (ca *CertificateAuthority) NewKeyPairFromConfig(config KeyPairConfig) (*Key
158158
return nil, err
159159
}
160160

161+
// If a serial number is provided, use it, otherwise use 42
162+
serialNumber := config.SerialNumber
163+
if serialNumber == nil {
164+
serialNumber = big.NewInt(42)
165+
}
166+
161167
// Create a Certificate
162168
kp := &KeyPair{cert: &x509.Certificate{
163169
Subject: pkix.Name{
164170
Organization: []string{"Never Use this Certificate in Production Inc."},
171+
CommonName: config.CommonName,
165172
},
166173
DNSNames: config.Domains,
167174
IPAddresses: ips,
168-
SerialNumber: big.NewInt(42),
175+
SerialNumber: serialNumber,
169176
NotBefore: time.Now().Add(-1 * time.Hour),
170177
NotAfter: time.Now().Add(2 * time.Hour),
171178
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},

testcerts_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/tls"
55
"crypto/x509"
66
"fmt"
7+
"math/big"
78
"net/http"
89
"os"
910
"path/filepath"
@@ -273,6 +274,22 @@ func TestKeyPairConfig(t *testing.T) {
273274
},
274275
err: ErrInvalidIP,
275276
},
277+
{
278+
name: "Happy Path - Serial Number provided",
279+
cfg: KeyPairConfig{
280+
Domains: []string{"example.com"},
281+
SerialNumber: big.NewInt(123),
282+
},
283+
err: nil,
284+
},
285+
{
286+
name: "Happy Path - Common Name provided",
287+
cfg: KeyPairConfig{
288+
Domains: []string{"example.com"},
289+
CommonName: "Example Common Name",
290+
},
291+
err: nil,
292+
},
276293
}
277294

278295
for _, c := range tc {
@@ -290,6 +307,34 @@ func TestKeyPairConfig(t *testing.T) {
290307
}
291308
})
292309
}
310+
311+
t.Run("Serial Number is correct in Key Pair", func(t *testing.T) {
312+
certs, err := NewCA().NewKeyPairFromConfig(KeyPairConfig{
313+
Domains: []string{"example.com"},
314+
SerialNumber: big.NewInt(123),
315+
})
316+
if err != nil {
317+
t.Fatalf("KeyPair Generation Failed expected nil got %v", err)
318+
}
319+
320+
if certs.cert.SerialNumber.Cmp(big.NewInt(123)) != 0 {
321+
t.Fatalf("Unexpected Serial Number expected 123 got %v", certs.cert.SerialNumber)
322+
}
323+
})
324+
325+
t.Run("Common Name is correct in Key Pair", func(t *testing.T) {
326+
certs, err := NewCA().NewKeyPairFromConfig(KeyPairConfig{
327+
Domains: []string{"example.com"},
328+
CommonName: "Example Common Name",
329+
})
330+
if err != nil {
331+
t.Fatalf("KeyPair Generation Failed expected nil got %v", err)
332+
}
333+
334+
if certs.cert.Subject.CommonName != "Example Common Name" {
335+
t.Fatalf("Unexpected Common Name expected 'Example Common Name' got %v", certs.cert.Subject.CommonName)
336+
}
337+
})
293338
}
294339

295340
type FullFlowTestCase struct {
@@ -327,6 +372,17 @@ func TestFullFlow(t *testing.T) {
327372
},
328373
kpErr: nil,
329374
},
375+
{
376+
name: "Localhost IP, Domain, Serial Number, and Common Name",
377+
listenAddr: "0.0.0.0",
378+
kpCfg: KeyPairConfig{
379+
IPAddresses: []string{"127.0.0.1", "::1"},
380+
Domains: []string{"localhost"},
381+
SerialNumber: big.NewInt(123),
382+
CommonName: "Example Common Name",
383+
},
384+
kpErr: nil,
385+
},
330386
}
331387

332388
for _, c := range tc {

0 commit comments

Comments
 (0)