pkix.go 5.1 KB

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