Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / google / certificate-transparency / go / x509 / pkix / pkix.go
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.
4
5 // Package pkix contains shared, low level structures used for ASN.1 parsing
6 // and serialization of X.509 certificates, CRL and OCSP.
7 package pkix
8
9 import (
10         // START CT CHANGES
11         "github.com/google/certificate-transparency/go/asn1"
12         // END CT CHANGES
13         "math/big"
14         "time"
15 )
16
17 // AlgorithmIdentifier represents the ASN.1 structure of the same name. See RFC
18 // 5280, section 4.1.1.2.
19 type AlgorithmIdentifier struct {
20         Algorithm  asn1.ObjectIdentifier
21         Parameters asn1.RawValue `asn1:"optional"`
22 }
23
24 type RDNSequence []RelativeDistinguishedNameSET
25
26 type RelativeDistinguishedNameSET []AttributeTypeAndValue
27
28 // AttributeTypeAndValue mirrors the ASN.1 structure of the same name in
29 // http://tools.ietf.org/html/rfc5280#section-4.1.2.4
30 type AttributeTypeAndValue struct {
31         Type  asn1.ObjectIdentifier
32         Value interface{}
33 }
34
35 // Extension represents the ASN.1 structure of the same name. See RFC
36 // 5280, section 4.2.
37 type Extension struct {
38         Id       asn1.ObjectIdentifier
39         Critical bool `asn1:"optional"`
40         Value    []byte
41 }
42
43 // Name represents an X.509 distinguished name. This only includes the common
44 // elements of a DN.  Additional elements in the name are ignored.
45 type Name struct {
46         Country, Organization, OrganizationalUnit []string
47         Locality, Province                        []string
48         StreetAddress, PostalCode                 []string
49         SerialNumber, CommonName                  string
50
51         Names []AttributeTypeAndValue
52 }
53
54 func (n *Name) FillFromRDNSequence(rdns *RDNSequence) {
55         for _, rdn := range *rdns {
56                 if len(rdn) == 0 {
57                         continue
58                 }
59                 atv := rdn[0]
60                 n.Names = append(n.Names, atv)
61                 value, ok := atv.Value.(string)
62                 if !ok {
63                         continue
64                 }
65
66                 t := atv.Type
67                 if len(t) == 4 && t[0] == 2 && t[1] == 5 && t[2] == 4 {
68                         switch t[3] {
69                         case 3:
70                                 n.CommonName = value
71                         case 5:
72                                 n.SerialNumber = value
73                         case 6:
74                                 n.Country = append(n.Country, value)
75                         case 7:
76                                 n.Locality = append(n.Locality, value)
77                         case 8:
78                                 n.Province = append(n.Province, value)
79                         case 9:
80                                 n.StreetAddress = append(n.StreetAddress, value)
81                         case 10:
82                                 n.Organization = append(n.Organization, value)
83                         case 11:
84                                 n.OrganizationalUnit = append(n.OrganizationalUnit, value)
85                         case 17:
86                                 n.PostalCode = append(n.PostalCode, value)
87                         }
88                 }
89         }
90 }
91
92 var (
93         oidCountry            = []int{2, 5, 4, 6}
94         oidOrganization       = []int{2, 5, 4, 10}
95         oidOrganizationalUnit = []int{2, 5, 4, 11}
96         oidCommonName         = []int{2, 5, 4, 3}
97         oidSerialNumber       = []int{2, 5, 4, 5}
98         oidLocality           = []int{2, 5, 4, 7}
99         oidProvince           = []int{2, 5, 4, 8}
100         oidStreetAddress      = []int{2, 5, 4, 9}
101         oidPostalCode         = []int{2, 5, 4, 17}
102 )
103
104 // appendRDNs appends a relativeDistinguishedNameSET to the given RDNSequence
105 // and returns the new value. The relativeDistinguishedNameSET contains an
106 // attributeTypeAndValue for each of the given values. See RFC 5280, A.1, and
107 // search for AttributeTypeAndValue.
108 func appendRDNs(in RDNSequence, values []string, oid asn1.ObjectIdentifier) RDNSequence {
109         if len(values) == 0 {
110                 return in
111         }
112
113         s := make([]AttributeTypeAndValue, len(values))
114         for i, value := range values {
115                 s[i].Type = oid
116                 s[i].Value = value
117         }
118
119         return append(in, s)
120 }
121
122 func (n Name) ToRDNSequence() (ret RDNSequence) {
123         ret = appendRDNs(ret, n.Country, oidCountry)
124         ret = appendRDNs(ret, n.Organization, oidOrganization)
125         ret = appendRDNs(ret, n.OrganizationalUnit, oidOrganizationalUnit)
126         ret = appendRDNs(ret, n.Locality, oidLocality)
127         ret = appendRDNs(ret, n.Province, oidProvince)
128         ret = appendRDNs(ret, n.StreetAddress, oidStreetAddress)
129         ret = appendRDNs(ret, n.PostalCode, oidPostalCode)
130         if len(n.CommonName) > 0 {
131                 ret = appendRDNs(ret, []string{n.CommonName}, oidCommonName)
132         }
133         if len(n.SerialNumber) > 0 {
134                 ret = appendRDNs(ret, []string{n.SerialNumber}, oidSerialNumber)
135         }
136
137         return ret
138 }
139
140 // CertificateList represents the ASN.1 structure of the same name. See RFC
141 // 5280, section 5.1. Use Certificate.CheckCRLSignature to verify the
142 // signature.
143 type CertificateList struct {
144         TBSCertList        TBSCertificateList
145         SignatureAlgorithm AlgorithmIdentifier
146         SignatureValue     asn1.BitString
147 }
148
149 // HasExpired reports whether now is past the expiry time of certList.
150 func (certList *CertificateList) HasExpired(now time.Time) bool {
151         return now.After(certList.TBSCertList.NextUpdate)
152 }
153
154 // TBSCertificateList represents the ASN.1 structure of the same name. See RFC
155 // 5280, section 5.1.
156 type TBSCertificateList struct {
157         Raw                 asn1.RawContent
158         Version             int `asn1:"optional,default:2"`
159         Signature           AlgorithmIdentifier
160         Issuer              RDNSequence
161         ThisUpdate          time.Time
162         NextUpdate          time.Time
163         RevokedCertificates []RevokedCertificate `asn1:"optional"`
164         Extensions          []Extension          `asn1:"tag:0,optional,explicit"`
165 }
166
167 // RevokedCertificate represents the ASN.1 structure of the same name. See RFC
168 // 5280, section 5.1.
169 type RevokedCertificate struct {
170         SerialNumber   *big.Int
171         RevocationTime time.Time
172         Extensions     []Extension `asn1:"optional"`
173 }