Skip to content

Commit

Permalink
Support for extension in csr
Browse files Browse the repository at this point in the history
  • Loading branch information
orb li authored and cbroglie committed Nov 1, 2018
1 parent 0019845 commit dd9e5cc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
27 changes: 15 additions & 12 deletions csr/csr.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ type CAConfig struct {
// certificate request functionality.
type CertificateRequest struct {
CN string
Names []Name `json:"names" yaml:"names"`
Hosts []string `json:"hosts" yaml:"hosts"`
KeyRequest KeyRequest `json:"key,omitempty" yaml:"key,omitempty"`
CA *CAConfig `json:"ca,omitempty" yaml:"ca,omitempty"`
SerialNumber string `json:"serialnumber,omitempty" yaml:"serialnumber,omitempty"`
Names []Name `json:"names" yaml:"names"`
Hosts []string `json:"hosts" yaml:"hosts"`
KeyRequest KeyRequest `json:"key,omitempty" yaml:"key,omitempty"`
CA *CAConfig `json:"ca,omitempty" yaml:"ca,omitempty"`
SerialNumber string `json:"serialnumber,omitempty" yaml:"serialnumber,omitempty"`
Extensions []pkix.Extension `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}

// New returns a new, empty CertificateRequest with a
Expand Down Expand Up @@ -392,6 +393,10 @@ func Generate(priv crypto.Signer, req *CertificateRequest) (csr []byte, err erro
}
}

if req.Extensions != nil {
tpl.ExtraExtensions = append(tpl.ExtraExtensions, req.Extensions...)
}

csr, err = x509.CreateCertificateRequest(rand.Reader, &tpl, priv)
if err != nil {
log.Errorf("failed to generate a CSR: %v", err)
Expand Down Expand Up @@ -420,13 +425,11 @@ func appendCAInfoToCSR(reqConf *CAConfig, csr *x509.CertificateRequest) error {
return err
}

csr.ExtraExtensions = []pkix.Extension{
{
Id: asn1.ObjectIdentifier{2, 5, 29, 19},
Value: val,
Critical: true,
},
}
csr.ExtraExtensions = append(csr.ExtraExtensions, pkix.Extension{
Id: asn1.ObjectIdentifier{2, 5, 29, 19},
Value: val,
Critical: true,
})

return nil
}
35 changes: 34 additions & 1 deletion csr/csr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/elliptic"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"io/ioutil"
Expand Down Expand Up @@ -110,12 +111,44 @@ func TestParseRequest(t *testing.T) {
},
Hosts: []string{"cloudflare.com", "www.cloudflare.com", "192.168.0.1", "[email protected]"},
KeyRequest: NewBasicKeyRequest(),
Extensions: []pkix.Extension{
pkix.Extension{
Id: asn1.ObjectIdentifier{1, 2, 3, 4, 5},
Value: []byte("AgEB"),
},
},
}

_, _, err := ParseRequest(cr)
csrBytes, _, err := ParseRequest(cr)
if err != nil {
t.Fatalf("%v", err)
}

block, _ := pem.Decode(csrBytes)
if block == nil {
t.Fatalf("%v", err)
}

if block.Type != "CERTIFICATE REQUEST" {
t.Fatalf("Incorrect block type: %s", block.Type)
}

csr, err := x509.ParseCertificateRequest(block.Bytes)
if err != nil {
t.Fatalf("%v", err)
}

found := false
for _, ext := range csr.Extensions {
if ext.Id.Equal(asn1.ObjectIdentifier{1, 2, 3, 4, 5}) {
found = true
break
}
}

if !found {
t.Fatalf("CSR did not include Custom Extension")
}
}

// TestParseRequestCA ensures that a valid CA certificate request does not
Expand Down

0 comments on commit dd9e5cc

Please sign in to comment.