Botan  2.13.0
Crypto and TLS for C++11
x509_dn.h
Go to the documentation of this file.
1 /*
2 * X.509 Distinguished Name
3 * (C) 1999-2010,2018 Jack Lloyd
4 * (C) 2017 Fabian Weissberg, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #ifndef BOTAN_X509_DN_H_
10 #define BOTAN_X509_DN_H_
11 
12 #include <botan/asn1_obj.h>
13 #include <botan/asn1_oid.h>
14 #include <botan/asn1_str.h>
15 #include <vector>
16 #include <map>
17 #include <iosfwd>
18 
19 namespace Botan {
20 
21 /**
22 * Distinguished Name
23 */
25  {
26  public:
27  X509_DN() = default;
28 
29  explicit X509_DN(const std::multimap<OID, std::string>& args)
30  {
31  for(auto i : args)
32  add_attribute(i.first, i.second);
33  }
34 
35  explicit X509_DN(const std::multimap<std::string, std::string>& args)
36  {
37  for(auto i : args)
38  add_attribute(i.first, i.second);
39  }
40 
41  void encode_into(class DER_Encoder&) const override;
42  void decode_from(class BER_Decoder&) override;
43 
44  bool has_field(const OID& oid) const;
45  ASN1_String get_first_attribute(const OID& oid) const;
46 
47  /*
48  * Return the BER encoded data, if any
49  */
50  const std::vector<uint8_t>& get_bits() const { return m_dn_bits; }
51 
52  bool empty() const { return m_rdn.empty(); }
53 
54  std::string to_string() const;
55 
56  const std::vector<std::pair<OID,ASN1_String>>& dn_info() const { return m_rdn; }
57 
58  std::multimap<OID, std::string> get_attributes() const;
59  std::multimap<std::string, std::string> contents() const;
60 
61  bool has_field(const std::string& attr) const;
62  std::vector<std::string> get_attribute(const std::string& attr) const;
63  std::string get_first_attribute(const std::string& attr) const;
64 
65  void add_attribute(const std::string& key, const std::string& val);
66 
67  void add_attribute(const OID& oid, const std::string& val)
68  {
69  add_attribute(oid, ASN1_String(val));
70  }
71 
72  void add_attribute(const OID& oid, const ASN1_String& val);
73 
74  static std::string deref_info_field(const std::string& key);
75 
76  /**
77  * Lookup upper bounds in characters for the length of distinguished name fields
78  * as given in RFC 5280, Appendix A.
79  *
80  * @param oid the oid of the DN to lookup
81  * @return the upper bound, or zero if no ub is known to Botan
82  */
83  static size_t lookup_ub(const OID& oid);
84 
85  private:
86  std::vector<std::pair<OID,ASN1_String>> m_rdn;
87  std::vector<uint8_t> m_dn_bits;
88  };
89 
90 bool BOTAN_PUBLIC_API(2,0) operator==(const X509_DN& dn1, const X509_DN& dn2);
91 bool BOTAN_PUBLIC_API(2,0) operator!=(const X509_DN& dn1, const X509_DN& dn2);
92 
93 /*
94 The ordering here is arbitrary and may change from release to release.
95 It is intended for allowing DNs as keys in std::map and similiar containers
96 */
97 bool BOTAN_PUBLIC_API(2,0) operator<(const X509_DN& dn1, const X509_DN& dn2);
98 
99 BOTAN_PUBLIC_API(2,0) std::ostream& operator<<(std::ostream& out, const X509_DN& dn);
100 BOTAN_PUBLIC_API(2,0) std::istream& operator>>(std::istream& in, X509_DN& dn);
101 
102 }
103 
104 #endif
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: bigint.h:1135
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
bool empty() const
Definition: x509_dn.h:52
X509_DN(const std::multimap< std::string, std::string > &args)
Definition: x509_dn.h:35
const std::vector< uint8_t > & get_bits() const
Definition: x509_dn.h:50
const std::vector< std::pair< OID, ASN1_String > > & dn_info() const
Definition: x509_dn.h:56
Definition: alg_id.cpp:13
X509_DN(const std::multimap< OID, std::string > &args)
Definition: x509_dn.h:29
void add_attribute(const OID &oid, const std::string &val)
Definition: x509_dn.h:67