Botan 2.19.5 Crypto and TLS for C&

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
111std::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
121std::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::Lookup_Error
Definition: exceptn.h:248
Botan::OID
Definition: asn1_obj.h:194
Botan::OID::to_string
std::string to_string() const
Definition: asn1_oid.cpp:98
name
std::string name
Definition: commoncrypto_hash.cpp:24
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
Botan::OIDS::oid2str
std::string oid2str(const OID &oid)
Definition: oids.h:84
Botan::OIDS::oid2str_or_throw
BOTAN_UNSTABLE_API std::string oid2str_or_throw(const OID &oid)
Definition: oids.cpp:121
Botan::OIDS::add_str2oid
BOTAN_UNSTABLE_API void add_str2oid(const OID &oid, const std::string &name)
Definition: oids.cpp:106
Botan::OIDS::add_oidstr
BOTAN_UNSTABLE_API void add_oidstr(const char *oidstr, const char *name)
Definition: oids.cpp:96
Botan::OIDS::add_oid2str
BOTAN_UNSTABLE_API void add_oid2str(const OID &oid, const std::string &name)
Definition: oids.cpp:101
Botan::OIDS::load_str2oid_map
std::unordered_map< std::string, OID > load_str2oid_map()
Definition: oid_maps.cpp:264
Botan::OIDS::oid2str_or_empty
BOTAN_UNSTABLE_API std::string oid2str_or_empty(const OID &oid)
Definition: oids.cpp:111
Botan::OIDS::str2oid
OID str2oid(const std::string &name)
Definition: oids.h:89
Botan::OIDS::have_oid
BOTAN_UNSTABLE_API bool have_oid(const std::string &oid)
Definition: oids.cpp:129
Botan::OIDS::add_oid
BOTAN_UNSTABLE_API void add_oid(const OID &oid, const std::string &name)
Definition: oids.cpp:91
Botan::OIDS::str2oid_or_empty
BOTAN_UNSTABLE_API OID str2oid_or_empty(const std::string &name)
Definition: oids.cpp:116
Botan::OIDS::load_oid2str_map
std::unordered_map< std::string, std::string > load_oid2str_map()
Definition: oid_maps.cpp:17
Botan
Definition: alg_id.cpp:13
Botan::mutex_type
noop_mutex mutex_type
Definition: mutex.h:52