Skip to content

Commit 10762eb

Browse files
authored
Merge pull request #41 from smallstep/feat/extraNames
Add ExtraNames
2 parents 7d0f0dd + 06fddec commit 10762eb

File tree

6 files changed

+190
-38
lines changed

6 files changed

+190
-38
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-20.04
1313
strategy:
1414
matrix:
15-
go: [ '1.16', '1.17' ]
15+
go: [ '1.17', '1.18' ]
1616
steps:
1717
-
1818
name: Checkout
@@ -26,7 +26,7 @@ jobs:
2626
name: golangci-lint
2727
uses: golangci/golangci-lint-action@v2
2828
with:
29-
version: 'v1.44.0'
29+
version: 'v1.45.2'
3030
args: --timeout=30m
3131
-
3232
name: Test, Build

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-20.04
1515
strategy:
1616
matrix:
17-
go: [ '1.16', '1.17' ]
17+
go: [ '1.17', '1.18' ]
1818
steps:
1919
-
2020
name: Checkout
@@ -28,7 +28,7 @@ jobs:
2828
name: golangci-lint
2929
uses: golangci/golangci-lint-action@v2
3030
with:
31-
version: 'v1.44.0'
31+
version: 'v1.45.2'
3232
args: --timeout=30m
3333
-
3434
name: Test, Build
@@ -37,7 +37,7 @@ jobs:
3737
-
3838
name: Codecov
3939
uses: codecov/[email protected]
40-
if: matrix.go == '1.17'
40+
if: matrix.go == '1.18'
4141
with:
4242
file: ./coverage.out
4343
name: codecov-umbrella

minica/minica.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
// CA is the implementation of a simple X.509 and SSH CA.
1515
type CA struct {
1616
Root *x509.Certificate
17+
RootSigner crypto.Signer
1718
Intermediate *x509.Certificate
1819
Signer crypto.Signer
1920
SSHHostSigner ssh.Signer
@@ -92,6 +93,7 @@ func New(opts ...Option) (*CA, error) {
9293

9394
return &CA{
9495
Root: root,
96+
RootSigner: rootSigner,
9597
Intermediate: intermediate,
9698
Signer: intSigner,
9799
SSHHostSigner: sshHostSigner,

x509util/certpool_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func TestReadCertPool(t *testing.T) {
3030
return
3131
}
3232
if got != nil {
33+
// nolint:staticcheck // there's no other way to compare two
34+
// certpools, https://github.com/golang/go/issues/46057 might
35+
// fix this.
3336
subjects := got.Subjects()
3437
if !reflect.DeepEqual(subjects, tt.wantSubjects) {
3538
t.Errorf("x509.CertPool.Subjects() got = %v, want %v", subjects, tt.wantSubjects)

x509util/name.go

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package x509util
33
import (
44
"crypto/x509"
55
"crypto/x509/pkix"
6+
"encoding/asn1"
67
"encoding/json"
78

89
"github.com/pkg/errors"
@@ -11,15 +12,31 @@ import (
1112
// Name is the JSON representation of X.501 type Name, used in the X.509 subject
1213
// and issuer fields.
1314
type Name struct {
14-
Country MultiString `json:"country,omitempty"`
15-
Organization MultiString `json:"organization,omitempty"`
16-
OrganizationalUnit MultiString `json:"organizationalUnit,omitempty"`
17-
Locality MultiString `json:"locality,omitempty"`
18-
Province MultiString `json:"province,omitempty"`
19-
StreetAddress MultiString `json:"streetAddress,omitempty"`
20-
PostalCode MultiString `json:"postalCode,omitempty"`
21-
SerialNumber string `json:"serialNumber,omitempty"`
22-
CommonName string `json:"commonName,omitempty"`
15+
Country MultiString `json:"country,omitempty"`
16+
Organization MultiString `json:"organization,omitempty"`
17+
OrganizationalUnit MultiString `json:"organizationalUnit,omitempty"`
18+
Locality MultiString `json:"locality,omitempty"`
19+
Province MultiString `json:"province,omitempty"`
20+
StreetAddress MultiString `json:"streetAddress,omitempty"`
21+
PostalCode MultiString `json:"postalCode,omitempty"`
22+
SerialNumber string `json:"serialNumber,omitempty"`
23+
CommonName string `json:"commonName,omitempty"`
24+
ExtraNames []DistinguishedName `json:"extraNames,omitempty"`
25+
}
26+
27+
func newName(n pkix.Name) Name {
28+
return Name{
29+
Country: n.Country,
30+
Organization: n.Organization,
31+
OrganizationalUnit: n.OrganizationalUnit,
32+
Locality: n.Locality,
33+
Province: n.Province,
34+
StreetAddress: n.StreetAddress,
35+
PostalCode: n.PostalCode,
36+
SerialNumber: n.SerialNumber,
37+
CommonName: n.CommonName,
38+
ExtraNames: newDistinguisedNames(n.ExtraNames),
39+
}
2340
}
2441

2542
// UnmarshalJSON implements the json.Unmarshal interface and unmarshals a JSON
@@ -43,17 +60,7 @@ func (n *Name) UnmarshalJSON(data []byte) error {
4360
type Subject Name
4461

4562
func newSubject(n pkix.Name) Subject {
46-
return Subject{
47-
Country: n.Country,
48-
Organization: n.Organization,
49-
OrganizationalUnit: n.OrganizationalUnit,
50-
Locality: n.Locality,
51-
Province: n.Province,
52-
StreetAddress: n.StreetAddress,
53-
PostalCode: n.PostalCode,
54-
SerialNumber: n.SerialNumber,
55-
CommonName: n.CommonName,
56-
}
63+
return Subject(newName(n))
5764
}
5865

5966
// UnmarshalJSON implements the json.Unmarshal interface and unmarshals a JSON
@@ -79,24 +86,15 @@ func (s Subject) Set(c *x509.Certificate) {
7986
PostalCode: s.PostalCode,
8087
SerialNumber: s.SerialNumber,
8188
CommonName: s.CommonName,
89+
ExtraNames: fromDistinguisedNames(s.ExtraNames),
8290
}
8391
}
8492

8593
// Issuer is the JSON representation of the X.509 issuer field.
8694
type Issuer Name
8795

8896
func newIssuer(n pkix.Name) Issuer {
89-
return Issuer{
90-
Country: n.Country,
91-
Organization: n.Organization,
92-
OrganizationalUnit: n.OrganizationalUnit,
93-
Locality: n.Locality,
94-
Province: n.Province,
95-
StreetAddress: n.StreetAddress,
96-
PostalCode: n.PostalCode,
97-
SerialNumber: n.SerialNumber,
98-
CommonName: n.CommonName,
99-
}
97+
return Issuer(newName(n))
10098
}
10199

102100
// UnmarshalJSON implements the json.Unmarshal interface and unmarshals a JSON
@@ -122,5 +120,35 @@ func (i Issuer) Set(c *x509.Certificate) {
122120
PostalCode: i.PostalCode,
123121
SerialNumber: i.SerialNumber,
124122
CommonName: i.CommonName,
123+
ExtraNames: fromDistinguisedNames(i.ExtraNames),
124+
}
125+
}
126+
127+
// DistinguishedName mirrors the ASN.1 structure AttributeTypeAndValue in RFC
128+
// 5280, Section 4.1.2.4.
129+
type DistinguishedName struct {
130+
Type ObjectIdentifier `json:"type"`
131+
Value interface{} `json:"value"`
132+
}
133+
134+
func newDistinguisedNames(atvs []pkix.AttributeTypeAndValue) []DistinguishedName {
135+
var extraNames []DistinguishedName
136+
for _, atv := range atvs {
137+
extraNames = append(extraNames, DistinguishedName{
138+
Type: ObjectIdentifier(atv.Type),
139+
Value: atv.Value,
140+
})
141+
}
142+
return extraNames
143+
}
144+
145+
func fromDistinguisedNames(dns []DistinguishedName) []pkix.AttributeTypeAndValue {
146+
var atvs []pkix.AttributeTypeAndValue
147+
for _, dn := range dns {
148+
atvs = append(atvs, pkix.AttributeTypeAndValue{
149+
Type: asn1.ObjectIdentifier(dn.Type),
150+
Value: dn.Value,
151+
})
125152
}
153+
return atvs
126154
}

0 commit comments

Comments
 (0)