names.go 5.2 KB

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