rpki.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2018 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. "bytes"
  7. "encoding/binary"
  8. "errors"
  9. "fmt"
  10. "github.com/google/certificate-transparency-go/asn1"
  11. )
  12. // IPAddressPrefix describes an IP address prefix as an ASN.1 bit string,
  13. // where the BitLength field holds the prefix length.
  14. type IPAddressPrefix asn1.BitString
  15. // IPAddressRange describes an (inclusive) IP address range.
  16. type IPAddressRange struct {
  17. Min IPAddressPrefix
  18. Max IPAddressPrefix
  19. }
  20. // Most relevant values for AFI from:
  21. // http://www.iana.org/assignments/address-family-numbers.
  22. const (
  23. IPv4AddressFamilyIndicator = uint16(1)
  24. IPv6AddressFamilyIndicator = uint16(2)
  25. )
  26. // IPAddressFamilyBlocks describes a set of ranges of IP addresses.
  27. type IPAddressFamilyBlocks struct {
  28. // AFI holds an address family indicator from
  29. // http://www.iana.org/assignments/address-family-numbers.
  30. AFI uint16
  31. // SAFI holds a subsequent address family indicator from
  32. // http://www.iana.org/assignments/safi-namespace.
  33. SAFI byte
  34. // InheritFromIssuer indicates that the set of addresses should
  35. // be taken from the issuer's certificate.
  36. InheritFromIssuer bool
  37. // AddressPrefixes holds prefixes if InheritFromIssuer is false.
  38. AddressPrefixes []IPAddressPrefix
  39. // AddressRanges holds ranges if InheritFromIssuer is false.
  40. AddressRanges []IPAddressRange
  41. }
  42. // Internal types for asn1 unmarshalling.
  43. type ipAddressFamily struct {
  44. AddressFamily []byte // 2-byte AFI plus optional 1 byte SAFI
  45. Choice asn1.RawValue
  46. }
  47. // Internally, use raw asn1.BitString rather than the IPAddressPrefix
  48. // type alias (so that asn1.Unmarshal() decodes properly).
  49. type ipAddressRange struct {
  50. Min asn1.BitString
  51. Max asn1.BitString
  52. }
  53. func parseRPKIAddrBlocks(data []byte, nfe *NonFatalErrors) []*IPAddressFamilyBlocks {
  54. // RFC 3779 2.2.3
  55. // IPAddrBlocks ::= SEQUENCE OF IPAddressFamily
  56. //
  57. // IPAddressFamily ::= SEQUENCE { -- AFI & optional SAFI --
  58. // addressFamily OCTET STRING (SIZE (2..3)),
  59. // ipAddressChoice IPAddressChoice }
  60. //
  61. // IPAddressChoice ::= CHOICE {
  62. // inherit NULL, -- inherit from issuer --
  63. // addressesOrRanges SEQUENCE OF IPAddressOrRange }
  64. //
  65. // IPAddressOrRange ::= CHOICE {
  66. // addressPrefix IPAddress,
  67. // addressRange IPAddressRange }
  68. //
  69. // IPAddressRange ::= SEQUENCE {
  70. // min IPAddress,
  71. // max IPAddress }
  72. //
  73. // IPAddress ::= BIT STRING
  74. var addrBlocks []ipAddressFamily
  75. if rest, err := asn1.Unmarshal(data, &addrBlocks); err != nil {
  76. nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal ipAddrBlocks extension: %v", err))
  77. return nil
  78. } else if len(rest) != 0 {
  79. nfe.AddError(errors.New("trailing data after ipAddrBlocks extension"))
  80. return nil
  81. }
  82. var results []*IPAddressFamilyBlocks
  83. for i, block := range addrBlocks {
  84. var fam IPAddressFamilyBlocks
  85. if l := len(block.AddressFamily); l < 2 || l > 3 {
  86. nfe.AddError(fmt.Errorf("invalid address family length (%d) for ipAddrBlock.addressFamily", l))
  87. continue
  88. }
  89. fam.AFI = binary.BigEndian.Uint16(block.AddressFamily[0:2])
  90. if len(block.AddressFamily) > 2 {
  91. fam.SAFI = block.AddressFamily[2]
  92. }
  93. // IPAddressChoice is an ASN.1 CHOICE where the chosen alternative is indicated by (implicit)
  94. // tagging of the alternatives -- here, either NULL or SEQUENCE OF.
  95. if bytes.Equal(block.Choice.FullBytes, asn1.NullBytes) {
  96. fam.InheritFromIssuer = true
  97. results = append(results, &fam)
  98. continue
  99. }
  100. var addrRanges []asn1.RawValue
  101. if _, err := asn1.Unmarshal(block.Choice.FullBytes, &addrRanges); err != nil {
  102. nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal ipAddrBlocks[%d].ipAddressChoice.addressesOrRanges: %v", i, err))
  103. continue
  104. }
  105. for j, ar := range addrRanges {
  106. // Each IPAddressOrRange is a CHOICE where the alternatives have distinct (implicit)
  107. // tags -- here, either BIT STRING or SEQUENCE.
  108. switch ar.Tag {
  109. case asn1.TagBitString:
  110. // BIT STRING for single prefix IPAddress
  111. var val asn1.BitString
  112. if _, err := asn1.Unmarshal(ar.FullBytes, &val); err != nil {
  113. nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal ipAddrBlocks[%d].ipAddressChoice.addressesOrRanges[%d].addressPrefix: %v", i, j, err))
  114. continue
  115. }
  116. fam.AddressPrefixes = append(fam.AddressPrefixes, IPAddressPrefix(val))
  117. case asn1.TagSequence:
  118. var val ipAddressRange
  119. if _, err := asn1.Unmarshal(ar.FullBytes, &val); err != nil {
  120. nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal ipAddrBlocks[%d].ipAddressChoice.addressesOrRanges[%d].addressRange: %v", i, j, err))
  121. continue
  122. }
  123. fam.AddressRanges = append(fam.AddressRanges, IPAddressRange{Min: IPAddressPrefix(val.Min), Max: IPAddressPrefix(val.Max)})
  124. default:
  125. nfe.AddError(fmt.Errorf("unexpected ASN.1 type in ipAddrBlocks[%d].ipAddressChoice.addressesOrRanges[%d]: %+v", i, j, ar))
  126. }
  127. }
  128. results = append(results, &fam)
  129. }
  130. return results
  131. }
  132. // ASIDRange describes an inclusive range of AS Identifiers (AS numbers or routing
  133. // domain identifiers).
  134. type ASIDRange struct {
  135. Min int
  136. Max int
  137. }
  138. // ASIdentifiers describes a collection of AS Identifiers (AS numbers or routing
  139. // domain identifiers).
  140. type ASIdentifiers struct {
  141. // InheritFromIssuer indicates that the set of AS identifiers should
  142. // be taken from the issuer's certificate.
  143. InheritFromIssuer bool
  144. // ASIDs holds AS identifiers if InheritFromIssuer is false.
  145. ASIDs []int
  146. // ASIDs holds AS identifier ranges (inclusive) if InheritFromIssuer is false.
  147. ASIDRanges []ASIDRange
  148. }
  149. type asIdentifiers struct {
  150. ASNum asn1.RawValue `asn1:"optional,tag:0"`
  151. RDI asn1.RawValue `asn1:"optional,tag:1"`
  152. }
  153. func parseASIDChoice(val asn1.RawValue, nfe *NonFatalErrors) *ASIdentifiers {
  154. // RFC 3779 2.3.2
  155. // ASIdentifierChoice ::= CHOICE {
  156. // inherit NULL, -- inherit from issuer --
  157. // asIdsOrRanges SEQUENCE OF ASIdOrRange }
  158. // ASIdOrRange ::= CHOICE {
  159. // id ASId,
  160. // range ASRange }
  161. // ASRange ::= SEQUENCE {
  162. // min ASId,
  163. // max ASId }
  164. // ASId ::= INTEGER
  165. if len(val.FullBytes) == 0 { // OPTIONAL
  166. return nil
  167. }
  168. // ASIdentifierChoice is an ASN.1 CHOICE where the chosen alternative is indicated by (implicit)
  169. // tagging of the alternatives -- here, either NULL or SEQUENCE OF.
  170. if bytes.Equal(val.Bytes, asn1.NullBytes) {
  171. return &ASIdentifiers{InheritFromIssuer: true}
  172. }
  173. var ids []asn1.RawValue
  174. if rest, err := asn1.Unmarshal(val.Bytes, &ids); err != nil {
  175. nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal ASIdentifiers.asIdsOrRanges: %v", err))
  176. return nil
  177. } else if len(rest) != 0 {
  178. nfe.AddError(errors.New("trailing data after ASIdentifiers.asIdsOrRanges"))
  179. return nil
  180. }
  181. var asID ASIdentifiers
  182. for i, id := range ids {
  183. // Each ASIdOrRange is a CHOICE where the alternatives have distinct (implicit)
  184. // tags -- here, either INTEGER or SEQUENCE.
  185. switch id.Tag {
  186. case asn1.TagInteger:
  187. var val int
  188. if _, err := asn1.Unmarshal(id.FullBytes, &val); err != nil {
  189. nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal ASIdentifiers.asIdsOrRanges[%d].id: %v", i, err))
  190. continue
  191. }
  192. asID.ASIDs = append(asID.ASIDs, val)
  193. case asn1.TagSequence:
  194. var val ASIDRange
  195. if _, err := asn1.Unmarshal(id.FullBytes, &val); err != nil {
  196. nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal ASIdentifiers.asIdsOrRanges[%d].range: %v", i, err))
  197. continue
  198. }
  199. asID.ASIDRanges = append(asID.ASIDRanges, val)
  200. default:
  201. nfe.AddError(fmt.Errorf("unexpected value in ASIdentifiers.asIdsOrRanges[%d]: %+v", i, id))
  202. }
  203. }
  204. return &asID
  205. }
  206. func parseRPKIASIdentifiers(data []byte, nfe *NonFatalErrors) (*ASIdentifiers, *ASIdentifiers) {
  207. // RFC 3779 2.3.2
  208. // ASIdentifiers ::= SEQUENCE {
  209. // asnum [0] EXPLICIT ASIdentifierChoice OPTIONAL,
  210. // rdi [1] EXPLICIT ASIdentifierChoice OPTIONAL}
  211. var asIDs asIdentifiers
  212. if rest, err := asn1.Unmarshal(data, &asIDs); err != nil {
  213. nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal ASIdentifiers extension: %v", err))
  214. return nil, nil
  215. } else if len(rest) != 0 {
  216. nfe.AddError(errors.New("trailing data after ASIdentifiers extension"))
  217. return nil, nil
  218. }
  219. return parseASIDChoice(asIDs.ASNum, nfe), parseASIDChoice(asIDs.RDI, nfe)
  220. }