aboutsummaryrefslogtreecommitdiff
path: root/rpkid/rpki/oids.py
blob: 1acc8035baa7476d6420a6a915eb21378ad36fdd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# $Id$
# 
# Copyright (C) 2013--2014  Dragon Research Labs ("DRL")
# Portions copyright (C) 2009--2012  Internet Systems Consortium ("ISC")
# Portions copyright (C) 2007--2008  American Registry for Internet Numbers ("ARIN")
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notices and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND DRL, ISC, AND ARIN DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL DRL,
# ISC, OR ARIN BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

"""
OID database.
"""

def defoid(name, *numbers):
  """
  Define a new OID, including adding it to a few dictionaries and
  making an entry for it in the rpki.oids module symbol table, so
  other code can refer to it as an ordinary symbol.
  """

  assert all(isinstance(n, (int, long)) for n in numbers)

  dotted = ".".join(str(n) for n in numbers)
  name_ = name.replace("-", "_")

  assert name_ not in globals()

  global oid2name
  oid2name[numbers] = name

  globals()[name_] = dotted

  global dotted2name
  dotted2name[dotted] = name

  global dotted2name_
  dotted2name_[dotted] = name_

  global name2dotted
  name2dotted[name] = dotted
  name2dotted[name_] = dotted


## @var oid2name
# Mapping table of OIDs to conventional string names.

oid2name = {
  (1, 2, 840, 10045, 4, 3, 2)           : "ecdsa-with-SHA256",
  (1, 2, 840, 113549, 1, 1, 11)         : "sha256WithRSAEncryption",
  (1, 2, 840, 113549, 1, 1, 12)         : "sha384WithRSAEncryption",
  (1, 2, 840, 113549, 1, 1, 13)         : "sha512WithRSAEncryption",
  (1, 2, 840, 113549, 1, 7,  1)         : "id-data",
  (1, 2, 840, 113549, 1, 9, 16)         : "id-smime",
  (1, 2, 840, 113549, 1, 9, 16, 1)      : "id-ct",
  (1, 2, 840, 113549, 1, 9, 16, 1, 24)  : "id-ct-routeOriginAttestation",
  (1, 2, 840, 113549, 1, 9, 16, 1, 26)  : "id-ct-rpkiManifest",
  (1, 2, 840, 113549, 1, 9, 16, 1, 28)  : "id-ct-xml",
  (1, 2, 840, 113549, 1, 9, 16, 1, 35)  : "id-ct-rpkiGhostbusters",
  (1, 3, 6, 1, 5, 5, 7, 1, 1)           : "authorityInfoAccess",
  (1, 3, 6, 1, 5, 5, 7, 1, 11)          : "subjectInfoAccess",
  (1, 3, 6, 1, 5, 5, 7, 1, 7)           : "sbgp-ipAddrBlock",
  (1, 3, 6, 1, 5, 5, 7, 1, 8)           : "sbgp-autonomousSysNum",
  (1, 3, 6, 1, 5, 5, 7, 14, 2)          : "id-cp-ipAddr-asNumber",
  (1, 3, 6, 1, 5, 5, 7, 3, 666)         : "id-kp-bgpsec-router",        # {id-kp, 666} -- Real value not known yet
  (1, 3, 6, 1, 5, 5, 7, 48, 10)         : "id-ad-rpkiManifest",
  (1, 3, 6, 1, 5, 5, 7, 48, 11)         : "id-ad-signedObject",
  (1, 3, 6, 1, 5, 5, 7, 48, 2)          : "id-ad-caIssuers",
  (1, 3, 6, 1, 5, 5, 7, 48, 5)          : "id-ad-caRepository",
  (1, 3, 6, 1, 5, 5, 7, 48, 9)          : "id-ad-signedObjectRepository",
  (2, 16, 840, 1, 101, 3, 4, 2, 1)      : "id-sha256",
  (2, 5, 29, 14)                        : "subjectKeyIdentifier",
  (2, 5, 29, 15)                        : "keyUsage",
  (2, 5, 29, 19)                        : "basicConstraints",
  (2, 5, 29, 20)                        : "cRLNumber",
  (2, 5, 29, 31)                        : "cRLDistributionPoints",
  (2, 5, 29, 32)                        : "certificatePolicies",
  (2, 5, 29, 35)                        : "authorityKeyIdentifier",
  (2, 5, 29, 37)                        : "extendedKeyUsage",
  (2, 5, 4, 10)                         : "organizationName",
  (2, 5, 4, 11)                         : "organizationalUnitName",
  (2, 5, 4, 3)                          : "commonName",
  (2, 5, 4, 5)                          : "serialNumber",
  (2, 5, 4, 6)                          : "countryName",
  (2, 5, 4, 7)                          : "localityName",
  (2, 5, 4, 8)                          : "stateOrProvinceName",
  (2, 5, 4, 9)                          : "streetAddress",
}

## @var name2oid
# Mapping table of string names to OIDs

name2oid = dict((v, k) for k, v in oid2name.items())

def safe_name2oid(name):
  """
  Map name to OID, also parsing numeric (dotted decimal) format.
  """

  try:
    return name2oid[name]    
  except KeyError:
    fields = name.split(".")
    if all(field.isdigit() for field in fields):
      return tuple(int(field) for field in fields)
    raise

def safe_oid2name(oid):
  """
  Map OID to name.  If we have no mapping, generate numeric (dotted
  decimal) format.
  """

  try:
    return oid2name[oid]
  except KeyError:
    return oid2dotted(oid)

def oid2dotted(oid):
  """
  Convert OID to numeric (dotted decimal) format.
  """

  return ".".join(str(field) for field in oid)

def dotted2oid(dotted):
  """
  Convert dotted decimal format to OID tuple.
  """
  
  fields = dotted.split(".")
  if all(field.isdigit() for field in fields):
    return tuple(int(field) for field in fields)
  raise ValueError("%r is not a dotted decimal OID" % dotted)

def safe_name2dotted(name):
  """
  Convert name to dotted decimal format.
  """

  return oid2dotted(safe_name2oid(name))

def safe_dotted2name(dotted):
  """
  Convert dotted decimal to name if we know one,
  otherwise just return dotted.
  """

  try:
    return oid2name[dotted2oid(dotted)]
  except KeyError:
    return dotted