@@ -3,6 +3,7 @@ package x509util
33import (
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.
1314type 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 {
4360type Subject Name
4461
4562func 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.
8694type Issuer Name
8795
8896func 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