Botan  2.12.1
Crypto and TLS for C++11
oids.cpp
Go to the documentation of this file.
1 /*
2 * OID Registry
3 * (C) 1999-2008,2013 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/oids.h>
9 #include <botan/mutex.h>
10 
11 namespace Botan {
12 
13 namespace {
14 
15 class OID_Map final
16  {
17  public:
18  void add_oid(const OID& oid, const std::string& str)
19  {
20  add_str2oid(oid, str);
21  add_oid2str(oid, str);
22  }
23 
24  void add_str2oid(const OID& oid, const std::string& str)
25  {
26  lock_guard_type<mutex_type> lock(m_mutex);
27  auto i = m_str2oid.find(str);
28  if(i == m_str2oid.end())
29  m_str2oid.insert(std::make_pair(str, oid));
30  }
31 
32  void add_oid2str(const OID& oid, const std::string& str)
33  {
34  const std::string oid_str = oid.to_string();
35  lock_guard_type<mutex_type> lock(m_mutex);
36  auto i = m_oid2str.find(oid_str);
37  if(i == m_oid2str.end())
38  m_oid2str.insert(std::make_pair(oid_str, str));
39  }
40 
41  std::string oid2str(const OID& oid)
42  {
43  const std::string oid_str = oid.to_string();
44 
45  lock_guard_type<mutex_type> lock(m_mutex);
46 
47  auto i = m_oid2str.find(oid_str);
48  if(i != m_oid2str.end())
49  return i->second;
50 
51  return "";
52  }
53 
54  OID str2oid(const std::string& str)
55  {
56  lock_guard_type<mutex_type> lock(m_mutex);
57  auto i = m_str2oid.find(str);
58  if(i != m_str2oid.end())
59  return i->second;
60 
61  return OID();
62  }
63 
64  bool have_oid(const std::string& str)
65  {
66  lock_guard_type<mutex_type> lock(m_mutex);
67  return m_str2oid.find(str) != m_str2oid.end();
68  }
69 
70  static OID_Map& global_registry()
71  {
72  static OID_Map g_map;
73  return g_map;
74  }
75 
76  private:
77 
78  OID_Map()
79  {
80  m_str2oid = OIDS::load_str2oid_map();
81  m_oid2str = OIDS::load_oid2str_map();
82  }
83 
84  mutex_type m_mutex;
85  std::unordered_map<std::string, OID> m_str2oid;
86  std::unordered_map<std::string, std::string> m_oid2str;
87  };
88 
89 }
90 
91 void OIDS::add_oid(const OID& oid, const std::string& name)
92  {
93  OID_Map::global_registry().add_oid(oid, name);
94  }
95 
96 void OIDS::add_oidstr(const char* oidstr, const char* name)
97  {
98  add_oid(OID(oidstr), name);
99  }
100 
101 void OIDS::add_oid2str(const OID& oid, const std::string& name)
102  {
103  OID_Map::global_registry().add_oid2str(oid, name);
104  }
105 
106 void OIDS::add_str2oid(const OID& oid, const std::string& name)
107  {
108  OID_Map::global_registry().add_str2oid(oid, name);
109  }
110 
111 std::string OIDS::oid2str_or_empty(const OID& oid)
112  {
113  return OID_Map::global_registry().oid2str(oid);
114  }
115 
116 OID OIDS::str2oid_or_empty(const std::string& name)
117  {
118  return OID_Map::global_registry().str2oid(name);
119  }
120 
121 std::string OIDS::oid2str_or_throw(const OID& oid)
122  {
123  const std::string s = OIDS::oid2str_or_empty(oid);
124  if(s.empty())
125  throw Lookup_Error("No name associated with OID " + oid.to_string());
126  return s;
127  }
128 
129 bool OIDS::have_oid(const std::string& name)
130  {
131  return OID_Map::global_registry().have_oid(name);
132  }
133 
134 }
BOTAN_UNSTABLE_API std::string oid2str_or_throw(const OID &oid)
Definition: oids.cpp:121
std::unordered_map< std::string, std::string > load_oid2str_map()
Definition: oid_maps.cpp:17
noop_mutex mutex_type
Definition: mutex.h:51
BOTAN_UNSTABLE_API std::string oid2str_or_empty(const OID &oid)
Definition: oids.cpp:111
int(* final)(unsigned char *, CTX *)
std::unordered_map< std::string, OID > load_str2oid_map()
Definition: oid_maps.cpp:263
BOTAN_UNSTABLE_API OID str2oid_or_empty(const std::string &name)
Definition: oids.cpp:116
std::string to_string() const
Definition: asn1_oid.cpp:98
OID str2oid(const std::string &name)
Definition: oids.h:89
std::string name
BOTAN_UNSTABLE_API void add_str2oid(const OID &oid, const std::string &name)
Definition: oids.cpp:106
Definition: alg_id.cpp:13
BOTAN_UNSTABLE_API bool have_oid(const std::string &oid)
Definition: oids.cpp:129
BOTAN_UNSTABLE_API void add_oidstr(const char *oidstr, const char *name)
Definition: oids.cpp:96
BOTAN_UNSTABLE_API void add_oid(const OID &oid, const std::string &name)
Definition: oids.cpp:91
std::string oid2str(const OID &oid)
Definition: oids.h:84
BOTAN_UNSTABLE_API void add_oid2str(const OID &oid, const std::string &name)
Definition: oids.cpp:101