@@ -12,9 +12,11 @@ import (
12
12
"encoding/asn1"
13
13
"encoding/pem"
14
14
"errors"
15
+ "fmt"
15
16
"net"
16
17
"net/mail"
17
18
"net/url"
19
+ "strconv"
18
20
"strings"
19
21
20
22
cferr "github.com/cloudflare/cfssl/errors"
@@ -30,12 +32,13 @@ const (
30
32
31
33
// A Name contains the SubjectInfo fields.
32
34
type Name struct {
33
- C string `json:"C,omitempty" yaml:"C,omitempty"` // Country
34
- <
9E88
div class="diff-text-inner"> ST string `json:"ST,omitempty" yaml:"ST,omitempty"` // State
35
- L string `json:"L,omitempty" yaml:"L,omitempty"` // Locality
36
- O string `json:"O,omitempty" yaml:"O,omitempty"` // OrganisationName
37
- OU string `json:"OU,omitempty" yaml:"OU,omitempty"` // OrganisationalUnitName
38
- SerialNumber string `json:"SerialNumber,omitempty" yaml:"SerialNumber,omitempty"`
35
+ C string `json:"C,omitempty" yaml:"C,omitempty"` // Country
36
+ ST string `json:"ST,omitempty" yaml:"ST,omitempty"` // State
37
+ L string `json:"L,omitempty" yaml:"L,omitempty"` // Locality
38
+ O string `json:"O,omitempty" yaml:"O,omitempty"` // OrganisationName
39
+ OU string `json:"OU,omitempty" yaml:"OU,omitempty"` // OrganisationalUnitName
40
+ SerialNumber string `json:"SerialNumber,omitempty" yaml:"SerialNumber,omitempty"`
41
+ OID map [string ]string `json:"OID,omitempty", yaml:"OID,omitempty"`
39
42
}
40
43
41
44
// A KeyRequest contains the algorithm and key size for a new private key.
@@ -157,8 +160,25 @@ func appendIf(s string, a *[]string) {
157
160
}
158
161
}
159
162
163
+ // OIDFromString creates an ASN1 ObjectIdentifier from its string representation
164
+ func OIDFromString (s string ) (asn1.ObjectIdentifier , error ) {
165
+ var oid []int
166
+ parts := strings .Split (s , "." )
167
+ if len (parts ) < 1 {
168
+ return oid , fmt .Errorf ("invalid OID string: %s" , s )
169
+ }
170
+ for _ , p := range parts {
171
+ i , err := strconv .Atoi (p )
172
+ if err != nil {
173
+ return nil , fmt .Errorf ("invalid OID part %s" , p )
174
+ }
175
+ oid = append (oid , i )
176
+ }
177
+ return oid , nil
178
+ }
179
+
160
180
// Name returns the PKIX name for the request.
161
- func (cr * CertificateRequest ) Name () pkix.Name {
181
+ func (cr * CertificateRequest ) Name () ( pkix.Name , error ) {
162
182
var name pkix.Name
163
183
name .CommonName = cr .CN
164
184
@@ -168,9 +188,16 @@ func (cr *CertificateRequest) Name() pkix.Name {
168
188
appendIf (n .L , & name .Locality )
169
189
appendIf (n .O , & name .Organization )
170
190
appendIf (n .OU , & name .OrganizationalUnit )
191
+ for k , v := range n .OID {
192
+ oid , err := OIDFromString (k )
193
+ if err != nil {
194
+ return name , err
195
+ }
196
+ name .ExtraNames = append (name .ExtraNames , pkix.AttributeTypeAndValue {Type : oid , Value : v })
197
+ }
171
198
}
172
199
name .SerialNumber = cr .SerialNumber
173
- return name
200
+ return name , nil
174
201
}
175
202
176
203
// BasicConstraints CSR information RFC 5280, 4.2.1.9
@@ -367,8 +394,13 @@ func Generate(priv crypto.Signer, req *CertificateRequest) (csr []byte, err erro
367
394
return nil , cferr .New (cferr .PrivateKeyError , cferr .Unavailable )
368
395
}
369
396
397
+ subj , err := req .Name ()
398
+ if err != nil {
399
+ return nil , err
400
+ }
401
+
370
402
var tpl = x509.CertificateRequest {
371
- Subject : req . Name () ,
403
+ Subject : subj ,
372
404
SignatureAlgorithm : sigAlgo ,
373
405
}
374
406
0 commit comments