1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package pkix contains shared, low level structures used for ASN.1 parsing
6 // and serialization of X.509 certificates, CRL and OCSP.
15 // AlgorithmIdentifier represents the ASN.1 structure of the same name. See RFC
16 // 5280, section 4.1.1.2.
17 type AlgorithmIdentifier struct {
18 Algorithm asn1.ObjectIdentifier
19 Parameters asn1.RawValue `asn1:"optional"`
22 type RDNSequence []RelativeDistinguishedNameSET
24 type RelativeDistinguishedNameSET []AttributeTypeAndValue
26 type AttributeTypeAndValue struct {
27 Type asn1.ObjectIdentifier
31 // Extension represents the ASN.1 structure of the same name. See RFC
33 type Extension struct {
34 Id asn1.ObjectIdentifier
35 Critical bool `asn1:"optional"`
39 // Name represents an X.509 distinguished name. This only includes the common
40 // elements of a DN. Additional elements in the name are ignored.
42 Country, Organization, OrganizationalUnit []string
43 Locality, Province []string
44 StreetAddress, PostalCode []string
45 SerialNumber, CommonName string
47 Names []AttributeTypeAndValue
50 func (n *Name) FillFromRDNSequence(rdns *RDNSequence) {
51 for _, rdn := range *rdns {
56 n.Names = append(n.Names, atv)
57 value, ok := atv.Value.(string)
63 if len(t) == 4 && t[0] == 2 && t[1] == 5 && t[2] == 4 {
68 n.SerialNumber = value
70 n.Country = append(n.Country, value)
72 n.Locality = append(n.Locality, value)
74 n.Province = append(n.Province, value)
76 n.StreetAddress = append(n.StreetAddress, value)
78 n.Organization = append(n.Organization, value)
80 n.OrganizationalUnit = append(n.OrganizationalUnit, value)
82 n.PostalCode = append(n.PostalCode, value)
89 oidCountry = []int{2, 5, 4, 6}
90 oidOrganization = []int{2, 5, 4, 10}
91 oidOrganizationalUnit = []int{2, 5, 4, 11}
92 oidCommonName = []int{2, 5, 4, 3}
93 oidSerialNumber = []int{2, 5, 4, 5}
94 oidLocality = []int{2, 5, 4, 7}
95 oidProvince = []int{2, 5, 4, 8}
96 oidStreetAddress = []int{2, 5, 4, 9}
97 oidPostalCode = []int{2, 5, 4, 17}
100 // appendRDNs appends a relativeDistinguishedNameSET to the given RDNSequence
101 // and returns the new value. The relativeDistinguishedNameSET contains an
102 // attributeTypeAndValue for each of the given values. See RFC 5280, A.1, and
103 // search for AttributeTypeAndValue.
104 func appendRDNs(in RDNSequence, values []string, oid asn1.ObjectIdentifier) RDNSequence {
105 if len(values) == 0 {
109 s := make([]AttributeTypeAndValue, len(values))
110 for i, value := range values {
118 func (n Name) ToRDNSequence() (ret RDNSequence) {
119 ret = appendRDNs(ret, n.Country, oidCountry)
120 ret = appendRDNs(ret, n.Organization, oidOrganization)
121 ret = appendRDNs(ret, n.OrganizationalUnit, oidOrganizationalUnit)
122 ret = appendRDNs(ret, n.Locality, oidLocality)
123 ret = appendRDNs(ret, n.Province, oidProvince)
124 ret = appendRDNs(ret, n.StreetAddress, oidStreetAddress)
125 ret = appendRDNs(ret, n.PostalCode, oidPostalCode)
126 if len(n.CommonName) > 0 {
127 ret = appendRDNs(ret, []string{n.CommonName}, oidCommonName)
129 if len(n.SerialNumber) > 0 {
130 ret = appendRDNs(ret, []string{n.SerialNumber}, oidSerialNumber)
136 // CertificateList represents the ASN.1 structure of the same name. See RFC
137 // 5280, section 5.1. Use Certificate.CheckCRLSignature to verify the
139 type CertificateList struct {
140 TBSCertList TBSCertificateList
141 SignatureAlgorithm AlgorithmIdentifier
142 SignatureValue asn1.BitString
145 // HasExpired returns true iff currentTimeSeconds is past the expiry time of
147 func (certList *CertificateList) HasExpired(currentTimeSeconds int64) bool {
148 return certList.TBSCertList.NextUpdate.Seconds() <= currentTimeSeconds
151 // TBSCertificateList represents the ASN.1 structure of the same name. See RFC
152 // 5280, section 5.1.
153 type TBSCertificateList struct {
155 Version int `asn1:"optional,default:2"`
156 Signature AlgorithmIdentifier
158 ThisUpdate *time.Time
159 NextUpdate *time.Time
160 RevokedCertificates []RevokedCertificate `asn1:"optional"`
161 Extensions []Extension `asn1:"tag:0,optional,explicit"`
164 // RevokedCertificate represents the ASN.1 structure of the same name. See RFC
165 // 5280, section 5.1.
166 type RevokedCertificate struct {
167 SerialNumber *big.Int
168 RevocationTime *time.Time
169 Extensions []Extension `asn1:"optional"`