names.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2009 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 x509
  5. import (
  6. "fmt"
  7. "net"
  8. "github.com/google/certificate-transparency-go/asn1"
  9. "github.com/google/certificate-transparency-go/x509/pkix"
  10. )
  11. const (
  12. // GeneralName tag values from RFC 5280, 4.2.1.6
  13. tagOtherName = 0
  14. tagRFC822Name = 1
  15. tagDNSName = 2
  16. tagX400Address = 3
  17. tagDirectoryName = 4
  18. tagEDIPartyName = 5
  19. tagURI = 6
  20. tagIPAddress = 7
  21. tagRegisteredID = 8
  22. )
  23. // OtherName describes a name related to a certificate which is not in one
  24. // of the standard name formats. RFC 5280, 4.2.1.6:
  25. //
  26. // OtherName ::= SEQUENCE {
  27. // type-id OBJECT IDENTIFIER,
  28. // value [0] EXPLICIT ANY DEFINED BY type-id }
  29. type OtherName struct {
  30. TypeID asn1.ObjectIdentifier
  31. Value asn1.RawValue
  32. }
  33. // GeneralNames holds a collection of names related to a certificate.
  34. type GeneralNames struct {
  35. DNSNames []string
  36. EmailAddresses []string
  37. DirectoryNames []pkix.Name
  38. URIs []string
  39. IPNets []net.IPNet
  40. RegisteredIDs []asn1.ObjectIdentifier
  41. OtherNames []OtherName
  42. }
  43. // Len returns the total number of names in a GeneralNames object.
  44. func (gn GeneralNames) Len() int {
  45. return (len(gn.DNSNames) + len(gn.EmailAddresses) + len(gn.DirectoryNames) +
  46. len(gn.URIs) + len(gn.IPNets) + len(gn.RegisteredIDs) + len(gn.OtherNames))
  47. }
  48. // Empty indicates whether a GeneralNames object is empty.
  49. func (gn GeneralNames) Empty() bool {
  50. return gn.Len() == 0
  51. }
  52. func parseGeneralNames(value []byte, gname *GeneralNames) error {
  53. // RFC 5280, 4.2.1.6
  54. // GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
  55. //
  56. // GeneralName ::= CHOICE {
  57. // otherName [0] OtherName,
  58. // rfc822Name [1] IA5String,
  59. // dNSName [2] IA5String,
  60. // x400Address [3] ORAddress,
  61. // directoryName [4] Name,
  62. // ediPartyName [5] EDIPartyName,
  63. // uniformResourceIdentifier [6] IA5String,
  64. // iPAddress [7] OCTET STRING,
  65. // registeredID [8] OBJECT IDENTIFIER }
  66. var seq asn1.RawValue
  67. var rest []byte
  68. if rest, err := asn1.Unmarshal(value, &seq); err != nil {
  69. return fmt.Errorf("x509: failed to parse GeneralNames: %v", err)
  70. } else if len(rest) != 0 {
  71. return fmt.Errorf("x509: trailing data after GeneralNames")
  72. }
  73. if !seq.IsCompound || seq.Tag != asn1.TagSequence || seq.Class != asn1.ClassUniversal {
  74. return fmt.Errorf("x509: failed to parse GeneralNames sequence, tag %+v", seq)
  75. }
  76. rest = seq.Bytes
  77. for len(rest) > 0 {
  78. var err error
  79. rest, err = parseGeneralName(rest, gname, false)
  80. if err != nil {
  81. return fmt.Errorf("x509: failed to parse GeneralName: %v", err)
  82. }
  83. }
  84. return nil
  85. }
  86. func parseGeneralName(data []byte, gname *GeneralNames, withMask bool) ([]byte, error) {
  87. var v asn1.RawValue
  88. var rest []byte
  89. var err error
  90. rest, err = asn1.Unmarshal(data, &v)
  91. if err != nil {
  92. return nil, fmt.Errorf("x509: failed to unmarshal GeneralNames: %v", err)
  93. }
  94. switch v.Tag {
  95. case tagOtherName:
  96. if !v.IsCompound {
  97. return nil, fmt.Errorf("x509: failed to unmarshal GeneralNames.otherName: not compound")
  98. }
  99. var other OtherName
  100. v.FullBytes = append([]byte{}, v.FullBytes...)
  101. v.FullBytes[0] = asn1.TagSequence | 0x20
  102. _, err = asn1.Unmarshal(v.FullBytes, &other)
  103. if err != nil {
  104. return nil, fmt.Errorf("x509: failed to unmarshal GeneralNames.otherName: %v", err)
  105. }
  106. gname.OtherNames = append(gname.OtherNames, other)
  107. case tagRFC822Name:
  108. gname.EmailAddresses = append(gname.EmailAddresses, string(v.Bytes))
  109. case tagDNSName:
  110. dns := string(v.Bytes)
  111. gname.DNSNames = append(gname.DNSNames, dns)
  112. case tagDirectoryName:
  113. var rdnSeq pkix.RDNSequence
  114. if _, err := asn1.Unmarshal(v.Bytes, &rdnSeq); err != nil {
  115. return nil, fmt.Errorf("x509: failed to unmarshal GeneralNames.directoryName: %v", err)
  116. }
  117. var dirName pkix.Name
  118. dirName.FillFromRDNSequence(&rdnSeq)
  119. gname.DirectoryNames = append(gname.DirectoryNames, dirName)
  120. case tagURI:
  121. gname.URIs = append(gname.URIs, string(v.Bytes))
  122. case tagIPAddress:
  123. vlen := len(v.Bytes)
  124. if withMask {
  125. switch vlen {
  126. case (2 * net.IPv4len), (2 * net.IPv6len):
  127. ipNet := net.IPNet{IP: v.Bytes[0 : vlen/2], Mask: v.Bytes[vlen/2:]}
  128. gname.IPNets = append(gname.IPNets, ipNet)
  129. default:
  130. return nil, fmt.Errorf("x509: invalid IP/mask length %d in GeneralNames.iPAddress", vlen)
  131. }
  132. } else {
  133. switch vlen {
  134. case net.IPv4len, net.IPv6len:
  135. ipNet := net.IPNet{IP: v.Bytes}
  136. gname.IPNets = append(gname.IPNets, ipNet)
  137. default:
  138. return nil, fmt.Errorf("x509: invalid IP length %d in GeneralNames.iPAddress", vlen)
  139. }
  140. }
  141. case tagRegisteredID:
  142. var oid asn1.ObjectIdentifier
  143. v.FullBytes = append([]byte{}, v.FullBytes...)
  144. v.FullBytes[0] = asn1.TagOID
  145. _, err = asn1.Unmarshal(v.FullBytes, &oid)
  146. if err != nil {
  147. return nil, fmt.Errorf("x509: failed to unmarshal GeneralNames.registeredID: %v", err)
  148. }
  149. gname.RegisteredIDs = append(gname.RegisteredIDs, oid)
  150. default:
  151. return nil, fmt.Errorf("x509: failed to unmarshal GeneralName: unknown tag %d", v.Tag)
  152. }
  153. return rest, nil
  154. }