OSDN Git Service

b35274c9ae16b6340da0f06fccafea651ccef226
[pf3gnuchains/gcc-fork.git] / libgo / go / crypto / 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         "encoding/asn1"
11         "math/big"
12         "time"
13 )
14
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"`
20 }
21
22 type RDNSequence []RelativeDistinguishedNameSET
23
24 type RelativeDistinguishedNameSET []AttributeTypeAndValue
25
26 type AttributeTypeAndValue struct {
27         Type  asn1.ObjectIdentifier
28         Value interface{}
29 }
30
31 // Extension represents the ASN.1 structure of the same name. See RFC
32 // 5280, section 4.2.
33 type Extension struct {
34         Id       asn1.ObjectIdentifier
35         Critical bool `asn1:"optional"`
36         Value    []byte
37 }
38
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.
41 type Name struct {
42         Country, Organization, OrganizationalUnit []string
43         Locality, Province                        []string
44         StreetAddress, PostalCode                 []string
45         SerialNumber, CommonName                  string
46
47         Names []AttributeTypeAndValue
48 }
49
50 func (n *Name) FillFromRDNSequence(rdns *RDNSequence) {
51         for _, rdn := range *rdns {
52                 if len(rdn) == 0 {
53                         continue
54                 }
55                 atv := rdn[0]
56                 n.Names = append(n.Names, atv)
57                 value, ok := atv.Value.(string)
58                 if !ok {
59                         continue
60                 }
61
62                 t := atv.Type
63                 if len(t) == 4 && t[0] == 2 && t[1] == 5 && t[2] == 4 {
64                         switch t[3] {
65                         case 3:
66                                 n.CommonName = value
67                         case 5:
68                                 n.SerialNumber = value
69                         case 6:
70                                 n.Country = append(n.Country, value)
71                         case 7:
72                                 n.Locality = append(n.Locality, value)
73                         case 8:
74                                 n.Province = append(n.Province, value)
75                         case 9:
76                                 n.StreetAddress = append(n.StreetAddress, value)
77                         case 10:
78                                 n.Organization = append(n.Organization, value)
79                         case 11:
80                                 n.OrganizationalUnit = append(n.OrganizationalUnit, value)
81                         case 17:
82                                 n.PostalCode = append(n.PostalCode, value)
83                         }
84                 }
85         }
86 }
87
88 var (
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}
98 )
99
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 {
106                 return in
107         }
108
109         s := make([]AttributeTypeAndValue, len(values))
110         for i, value := range values {
111                 s[i].Type = oid
112                 s[i].Value = value
113         }
114
115         return append(in, s)
116 }
117
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)
128         }
129         if len(n.SerialNumber) > 0 {
130                 ret = appendRDNs(ret, []string{n.SerialNumber}, oidSerialNumber)
131         }
132
133         return ret
134 }
135
136 // CertificateList represents the ASN.1 structure of the same name. See RFC
137 // 5280, section 5.1. Use Certificate.CheckCRLSignature to verify the
138 // signature.
139 type CertificateList struct {
140         TBSCertList        TBSCertificateList
141         SignatureAlgorithm AlgorithmIdentifier
142         SignatureValue     asn1.BitString
143 }
144
145 // HasExpired returns true iff currentTimeSeconds is past the expiry time of
146 // certList.
147 func (certList *CertificateList) HasExpired(currentTimeSeconds int64) bool {
148         return certList.TBSCertList.NextUpdate.Seconds() <= currentTimeSeconds
149 }
150
151 // TBSCertificateList represents the ASN.1 structure of the same name. See RFC
152 // 5280, section 5.1.
153 type TBSCertificateList struct {
154         Raw                 asn1.RawContent
155         Version             int `asn1:"optional,default:2"`
156         Signature           AlgorithmIdentifier
157         Issuer              RDNSequence
158         ThisUpdate          *time.Time
159         NextUpdate          *time.Time
160         RevokedCertificates []RevokedCertificate `asn1:"optional"`
161         Extensions          []Extension          `asn1:"tag:0,optional,explicit"`
162 }
163
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"`
170 }