@@ -22,6 +22,7 @@ import (
2222
2323 "github.com/stretchr/testify/assert"
2424 "github.com/stretchr/testify/require"
25+ "go.step.sm/crypto/pemutil"
2526)
2627
2728func createCertificateRequest (t * testing.T , commonName string , sans []string ) (* x509.CertificateRequest , crypto.Signer ) {
@@ -49,6 +50,21 @@ func createCertificateRequest(t *testing.T, commonName string, sans []string) (*
4950 return cr , priv
5051}
5152
53+ func readCertificateRequest (t * testing.T , filename , keyFilename string ) (* x509.CertificateRequest , crypto.Signer ) {
54+ t .Helper ()
55+
56+ cr , err := pemutil .ReadCertificateRequest (filename )
57+ require .NoError (t , err )
58+
59+ key , err := pemutil .Read (keyFilename )
60+ require .NoError (t , err )
61+
62+ signer , ok := key .(crypto.Signer )
63+ require .True (t , ok )
64+
65+ return cr , signer
66+ }
67+
5268func createIssuerCertificate (t * testing.T , commonName string ) (* x509.Certificate , crypto.Signer ) {
5369 t .Helper ()
5470 now := time .Now ()
@@ -135,6 +151,8 @@ func TestNewCertificate(t *testing.T) {
135151 return ipNet
136152 }
137153
154+ rawSubjectCR , rawSubjectKey := readCertificateRequest (t , "testdata/rawSubject.csr" , "testdata/rawSubject.key" )
155+
138156 type args struct {
139157 cr * x509.CertificateRequest
140158 opts []Option
@@ -283,6 +301,38 @@ func TestNewCertificate(t *testing.T) {
283301 PublicKey : priv .Public (),
284302 PublicKeyAlgorithm : x509 .Ed25519 ,
285303 }, false },
304+ {"okRawSubject" , args {rawSubjectCR , []Option {WithTemplateFile ("./testdata/rawSubject.tpl" , TemplateData {
305+ SANsKey : []SubjectAlternativeName {
306+ {Type : "dns" , Value : "foo.com" },
307+ },
308+ CertificateRequestKey : NewCertificateRequestFromX509 (rawSubjectCR ),
309+ })}}, & Certificate {
310+ Subject : Subject {},
311+ RawSubject : []byte {
312+ 0x30 , 0x68 , 0x31 , 0x0b , 0x30 , 0x09 , 0x06 , 0x03 ,
313+ 0x55 , 0x04 , 0x06 , 0x13 , 0x02 , 0x55 , 0x53 , 0x31 ,
314+ 0x13 , 0x30 , 0x11 , 0x06 , 0x03 , 0x55 , 0x04 , 0x08 ,
315+ 0x0c , 0x0a , 0x43 , 0x61 , 0x6c , 0x69 , 0x66 , 0x6f ,
316+ 0x72 , 0x6e , 0x69 , 0x61 , 0x31 , 0x16 , 0x30 , 0x14 ,
317+ 0x06 , 0x03 , 0x55 , 0x04 , 0x07 , 0x0c , 0x0d , 0x53 ,
318+ 0x61 , 0x6e , 0x20 , 0x46 , 0x72 , 0x61 , 0x6e , 0x63 ,
319+ 0x69 , 0x73 , 0x63 , 0x6f , 0x31 , 0x1d , 0x30 , 0x1b ,
320+ 0x06 , 0x03 , 0x55 , 0x04 , 0x0a , 0x0c , 0x14 , 0x53 ,
321+ 0x6d , 0x61 , 0x6c , 0x6c , 0x73 , 0x74 , 0x65 , 0x70 ,
322+ 0x20 , 0x4c , 0x61 , 0x62 , 0x73 , 0x2c , 0x20 , 0x49 ,
323+ 0x6e , 0x63 , 0x2e , 0x31 , 0x0d , 0x30 , 0x0b , 0x06 ,
324+ 0x03 , 0x55 , 0x04 , 0x03 , 0x0c , 0x04 , 0x54 , 0x65 ,
325+ 0x73 , 0x74 ,
326+ },
327+ SANs : []SubjectAlternativeName {{Type : DNSType , Value : "foo.com" }},
328+ KeyUsage : KeyUsage (x509 .KeyUsageDigitalSignature ),
329+ ExtKeyUsage : ExtKeyUsage ([]x509.ExtKeyUsage {
330+ x509 .ExtKeyUsageServerAuth ,
331+ x509 .ExtKeyUsageClientAuth ,
332+ }),
333+ PublicKey : rawSubjectKey .Public (),
334+ PublicKeyAlgorithm : x509 .ECDSA ,
335+ }, false },
286336 {"badSignature" , args {crBadSignateure , nil }, nil , true },
287337 {"failTemplate" , args {cr , []Option {WithTemplate (`{{ fail "fatal error }}` , CreateTemplateData ("commonName" , []string {"foo.com" }))}}, nil , true },
288338 {"missingTemplate" , args {cr , []Option {WithTemplateFile ("./testdata/missing.tpl" , CreateTemplateData ("commonName" , []string {"foo.com" }))}}, nil , true },
0 commit comments