| 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/hex" |
| 11 |
"fmt" |
| 12 |
"math/big" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/google/certificate-transparency-go/asn1" |
| 16 |
) |
| 17 |
|
| 18 |
// AlgorithmIdentifier represents the ASN.1 structure of the same name. See RFC |
| 19 |
// 5280, section 4.1.1.2. |
| 20 |
type AlgorithmIdentifier struct { |
| 21 |
Raw asn1.RawContent |
| 22 |
Algorithm asn1.ObjectIdentifier |
| 23 |
Parameters asn1.RawValue `asn1:"optional"` |
| 24 |
} |
| 25 |
|
| 26 |
type RDNSequence []RelativeDistinguishedNameSET |
| 27 |
|
| 28 |
var attributeTypeNames = map[string]string{ |
| 29 |
"2.5.4.6": "C", |
| 30 |
"2.5.4.10": "O", |
| 31 |
"2.5.4.11": "OU", |
| 32 |
"2.5.4.3": "CN", |
| 33 |
"2.5.4.5": "SERIALNUMBER", |
| 34 |
"2.5.4.7": "L", |
| 35 |
"2.5.4.8": "ST", |
| 36 |
"2.5.4.9": "STREET", |
| 37 |
"2.5.4.17": "POSTALCODE", |
| 38 |
} |
| 39 |
|
| 40 |
// String returns a string representation of the sequence r, |
| 41 |
// roughly following the RFC 2253 Distinguished Names syntax. |
| 42 |
func (r RDNSequence) String() string { |
| 43 |
s := "" |
| 44 |
for i := 0; i < len(r); i++ { |
| 45 |
rdn := r[len(r)-1-i] |
| 46 |
if i > 0 { |
| 47 |
s += "," |
| 48 |
} |
| 49 |
for j, tv := range rdn { |
| 50 |
if j > 0 { |
| 51 |
s += "+" |
| 52 |
} |
| 53 |
|
| 54 |
oidString := tv.Type.String() |
| 55 |
typeName, ok := attributeTypeNames[oidString] |
| 56 |
if !ok { |
| 57 |
derBytes, err := asn1.Marshal(tv.Value) |
| 58 |
if err == nil { |
| 59 |
s += oidString + "=#" + hex.EncodeToString(derBytes) |
| 60 |
continue // No value escaping necessary. |
| 61 |
} |
| 62 |
|
| 63 |
typeName = oidString |
| 64 |
} |
| 65 |
|
| 66 |
valueString := fmt.Sprint(tv.Value) |
| 67 |
escaped := make([]rune, 0, len(valueString)) |
| 68 |
|
| 69 |
for k, c := range valueString { |
| 70 |
escape := false |
| 71 |
|
| 72 |
switch c { |
| 73 |
case ',', '+', '"', '\\', '<', '>', ';': |
| 74 |
escape = true |
| 75 |
|
| 76 |
case ' ': |
| 77 |
escape = k == 0 || k == len(valueString)-1 |
| 78 |
|
| 79 |
case '#': |
| 80 |
escape = k == 0 |
| 81 |
} |
| 82 |
|
| 83 |
if escape { |
| 84 |
escaped = append(escaped, '\\', c) |
| 85 |
} else { |
| 86 |
escaped = append(escaped, c) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
s += typeName + "=" + string(escaped) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return s |
| 95 |
} |
| 96 |
|
| 97 |
type RelativeDistinguishedNameSET []AttributeTypeAndValue |
| 98 |
|
| 99 |
// AttributeTypeAndValue mirrors the ASN.1 structure of the same name in |
| 100 |
// RFC 5280, Section 4.1.2.4. |
| 101 |
type AttributeTypeAndValue struct { |
| 102 |
Type asn1.ObjectIdentifier |
| 103 |
Value interface{} |
| 104 |
} |
| 105 |
|
| 106 |
// AttributeTypeAndValueSET represents a set of ASN.1 sequences of |
| 107 |
// AttributeTypeAndValue sequences from RFC 2986 (PKCS #10). |
| 108 |
type AttributeTypeAndValueSET struct { |
| 109 |
Type asn1.ObjectIdentifier |
| 110 |
Value [][]AttributeTypeAndValue `asn1:"set"` |
| 111 |
} |
| 112 |
|
| 113 |
// Extension represents the ASN.1 structure of the same name. See RFC |
| 114 |
// 5280, section 4.2. |
| 115 |
type Extension struct { |
| 116 |
Id asn1.ObjectIdentifier |
| 117 |
Critical bool `asn1:"optional"` |
| 118 |
Value []byte |
| 119 |
} |
| 120 |
|
| 121 |
// Name represents an X.509 distinguished name. This only includes the common |
| 122 |
// elements of a DN. When parsing, all elements are stored in Names and |
| 123 |
// non-standard elements can be extracted from there. When marshaling, elements |
| 124 |
// in ExtraNames are appended and override other values with the same OID. |
| 125 |
type Name struct { |
| 126 |
Country, Organization, OrganizationalUnit []string |
| 127 |
Locality, Province []string |
| 128 |
StreetAddress, PostalCode []string |
| 129 |
SerialNumber, CommonName string |
| 130 |
|
| 131 |
Names []AttributeTypeAndValue |
| 132 |
ExtraNames []AttributeTypeAndValue |
| 133 |
} |
| 134 |
|
| 135 |
func (n *Name) FillFromRDNSequence(rdns *RDNSequence) { |
| 136 |
for _, rdn := range *rdns { |
| 137 |
if len(rdn) == 0 { |
| 138 |
continue |
| 139 |
} |
| 140 |
|
| 141 |
for _, atv := range rdn { |
| 142 |
n.Names = append(n.Names, atv) |
| 143 |
value, ok := atv.Value.(string) |
| 144 |
if !ok { |
| 145 |
continue |
| 146 |
} |
| 147 |
|
| 148 |
t := atv.Type |
| 149 |
if len(t) == 4 && t[0] == OIDAttribute[0] && t[1] == OIDAttribute[1] && t[2] == OIDAttribute[2] { |
| 150 |
switch t[3] { |
| 151 |
case OIDCommonName[3]: |
| 152 |
n.CommonName = value |
| 153 |
case OIDSerialNumber[3]: |
| 154 |
n.SerialNumber = value |
| 155 |
case OIDCountry[3]: |
| 156 |
n.Country = append(n.Country, value) |
| 157 |
case OIDLocality[3]: |
| 158 |
n.Locality = append(n.Locality, value) |
| 159 |
case OIDProvince[3]: |
| 160 |
n.Province = append(n.Province, value) |
| 161 |
case OIDStreetAddress[3]: |
| 162 |
n.StreetAddress = append(n.StreetAddress, value) |
| 163 |
case OIDOrganization[3]: |
| 164 |
n.Organization = append(n.Organization, value) |
| 165 |
case OIDOrganizationalUnit[3]: |
| 166 |
n.OrganizationalUnit = append(n.OrganizationalUnit, value) |
| 167 |
case OIDPostalCode[3]: |
| 168 |
n.PostalCode = append(n.PostalCode, value) |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
var ( |
| 176 |
OIDAttribute = asn1.ObjectIdentifier{2, 5, 4} |
| 177 |
OIDCountry = asn1.ObjectIdentifier{2, 5, 4, 6} |
| 178 |
OIDOrganization = asn1.ObjectIdentifier{2, 5, 4, 10} |
| 179 |
OIDOrganizationalUnit = asn1.ObjectIdentifier{2, 5, 4, 11} |
| 180 |
OIDCommonName = asn1.ObjectIdentifier{2, 5, 4, 3} |
| 181 |
OIDSerialNumber = asn1.ObjectIdentifier{2, 5, 4, 5} |
| 182 |
OIDLocality = asn1.ObjectIdentifier{2, 5, 4, 7} |
| 183 |
OIDProvince = asn1.ObjectIdentifier{2, 5, 4, 8} |
| 184 |
OIDStreetAddress = asn1.ObjectIdentifier{2, 5, 4, 9} |
| 185 |
OIDPostalCode = asn1.ObjectIdentifier{2, 5, 4, 17} |
| 186 |
|
| 187 |
OIDPseudonym = asn1.ObjectIdentifier{2, 5, 4, 65} |
| 188 |
OIDTitle = asn1.ObjectIdentifier{2, 5, 4, 12} |
| 189 |
OIDDnQualifier = asn1.ObjectIdentifier{2, 5, 4, 46} |
| 190 |
OIDName = asn1.ObjectIdentifier{2, 5, 4, 41} |
| 191 |
OIDSurname = asn1.ObjectIdentifier{2, 5, 4, 4} |
| 192 |
OIDGivenName = asn1.ObjectIdentifier{2, 5, 4, 42} |
| 193 |
OIDInitials = asn1.ObjectIdentifier{2, 5, 4, 43} |
| 194 |
OIDGenerationQualifier = asn1.ObjectIdentifier{2, 5, 4, 44} |
| 195 |
) |
| 196 |
|
| 197 |
// appendRDNs appends a relativeDistinguishedNameSET to the given RDNSequence |
| 198 |
// and returns the new value. The relativeDistinguishedNameSET contains an |
| 199 |
// attributeTypeAndValue for each of the given values. See RFC 5280, A.1, and |
| 200 |
// search for AttributeTypeAndValue. |
| 201 |
func (n Name) appendRDNs(in RDNSequence, values []string, oid asn1.ObjectIdentifier) RDNSequence { |
| 202 |
if len(values) == 0 || oidInAttributeTypeAndValue(oid, n.ExtraNames) { |
| 203 |
return in |
| 204 |
} |
| 205 |
|
| 206 |
s := make([]AttributeTypeAndValue, len(values)) |
| 207 |
for i, value := range values { |
| 208 |
s[i].Type = oid |
| 209 |
s[i].Value = value |
| 210 |
} |
| 211 |
|
| 212 |
return append(in, s) |
| 213 |
} |
| 214 |
|
| 215 |
func (n Name) ToRDNSequence() (ret RDNSequence) { |
| 216 |
ret = n.appendRDNs(ret, n.Country, OIDCountry) |
| 217 |
ret = n.appendRDNs(ret, n.Province, OIDProvince) |
| 218 |
ret = n.appendRDNs(ret, n.Locality, OIDLocality) |
| 219 |
ret = n.appendRDNs(ret, n.StreetAddress, OIDStreetAddress) |
| 220 |
ret = n.appendRDNs(ret, n.PostalCode, OIDPostalCode) |
| 221 |
ret = n.appendRDNs(ret, n.Organization, OIDOrganization) |
| 222 |
ret = n.appendRDNs(ret, n.OrganizationalUnit, OIDOrganizationalUnit) |
| 223 |
if len(n.CommonName) > 0 { |
| 224 |
ret = n.appendRDNs(ret, []string{n.CommonName}, OIDCommonName) |
| 225 |
} |
| 226 |
if len(n.SerialNumber) > 0 { |
| 227 |
ret = n.appendRDNs(ret, []string{n.SerialNumber}, OIDSerialNumber) |
| 228 |
} |
| 229 |
for _, atv := range n.ExtraNames { |
| 230 |
ret = append(ret, []AttributeTypeAndValue{atv}) |
| 231 |
} |
| 232 |
|
| 233 |
return ret |
| 234 |
} |
| 235 |
|
| 236 |
// String returns the string form of n, roughly following |
| 237 |
// the RFC 2253 Distinguished Names syntax. |
| 238 |
func (n Name) String() string { |
| 239 |
return n.ToRDNSequence().String() |
| 240 |
} |
| 241 |
|
| 242 |
// oidInAttributeTypeAndValue reports whether a type with the given OID exists |
| 243 |
// in atv. |
| 244 |
func oidInAttributeTypeAndValue(oid asn1.ObjectIdentifier, atv []AttributeTypeAndValue) bool { |
| 245 |
for _, a := range atv { |
| 246 |
if a.Type.Equal(oid) { |
| 247 |
return true |
| 248 |
} |
| 249 |
} |
| 250 |
return false |
| 251 |
} |
| 252 |
|
| 253 |
// CertificateList represents the ASN.1 structure of the same name. See RFC |
| 254 |
// 5280, section 5.1. Use Certificate.CheckCRLSignature to verify the |
| 255 |
// signature. |
| 256 |
type CertificateList struct { |
| 257 |
TBSCertList TBSCertificateList |
| 258 |
SignatureAlgorithm AlgorithmIdentifier |
| 259 |
SignatureValue asn1.BitString |
| 260 |
} |
| 261 |
|
| 262 |
// HasExpired reports whether certList should have been updated by now. |
| 263 |
func (certList *CertificateList) HasExpired(now time.Time) bool { |
| 264 |
return !now.Before(certList.TBSCertList.NextUpdate) |
| 265 |
} |
| 266 |
|
| 267 |
// TBSCertificateList represents the ASN.1 structure TBSCertList. See RFC |
| 268 |
// 5280, section 5.1. |
| 269 |
type TBSCertificateList struct { |
| 270 |
Raw asn1.RawContent |
| 271 |
Version int `asn1:"optional,default:0"` |
| 272 |
Signature AlgorithmIdentifier |
| 273 |
Issuer RDNSequence |
| 274 |
ThisUpdate time.Time |
| 275 |
NextUpdate time.Time `asn1:"optional"` |
| 276 |
RevokedCertificates []RevokedCertificate `asn1:"optional"` |
| 277 |
Extensions []Extension `asn1:"tag:0,optional,explicit"` |
| 278 |
} |
| 279 |
|
| 280 |
// RevokedCertificate represents the unnamed ASN.1 structure that makes up the |
| 281 |
// revokedCertificates member of the TBSCertList structure. See RFC |
| 282 |
// 5280, section 5.1. |
| 283 |
type RevokedCertificate struct { |
| 284 |
SerialNumber *big.Int |
| 285 |
RevocationTime time.Time |
| 286 |
Extensions []Extension `asn1:"optional"` |
| 287 |
} |