Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

name_constraint.h
Go to the documentation of this file.
1 /*
2 * X.509 Name Constraint
3 * (C) 2015 Kai Michaelis
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #ifndef BOTAN_NAME_CONSTRAINT_H_
9 #define BOTAN_NAME_CONSTRAINT_H_
10
11 #include <botan/asn1_obj.h>
12 #include <ostream>
13 #include <limits>
14
15 namespace Botan {
16
17 class BER_Encoder;
18 class DER_Encoder;
19 class X509_Certificate;
20
21 /**
22 * @brief X.509 GeneralName Type
23 *
24 * Handles parsing GeneralName types in their BER and canonical string
25 * encoding. Allows matching GeneralNames against each other using
26 * the rules laid out in the RFC 5280, sec. 4.2.1.10 (Name Contraints).
27 */
28 class BOTAN_PUBLIC_API(2,0) GeneralName final : public ASN1_Object
29 {
30 public:
31 enum MatchResult : int
32 {
33 All,
34 Some,
35 None,
36 NotFound,
37 UnknownType,
38 };
39
40 /**
41 * Creates an empty GeneralName.
42 */
43 GeneralName() = default;
44
45 /**
46 * Creates a new GeneralName for its string format.
47 * @param str type and name, colon-separated, e.g., "DNS:google.com"
48 */
49 GeneralName(const std::string& str);
50
51 void encode_into(DER_Encoder&) const override;
52
53 void decode_from(BER_Decoder&) override;
54
55 /**
56 * @return Type of the name. Can be DN, DNS, IP, RFC822 or URI.
57 */
58 const std::string& type() const { return m_type; }
59
60 /**
61 * @return The name as string. Format depends on type.
62 */
63 const std::string& name() const { return m_name; }
64
65 /**
66 * Checks whether a given certificate (partially) matches this name.
67 * @param cert certificate to be matched
68 * @return the match result
69 */
70 MatchResult matches(const X509_Certificate& cert) const;
71
72 private:
73 std::string m_type;
74 std::string m_name;
75
76 bool matches_dns(const std::string&) const;
77 bool matches_dn(const std::string&) const;
78 bool matches_ip(const std::string&) const;
79 };
80
81 std::ostream& operator<<(std::ostream& os, const GeneralName& gn);
82
83 /**
84 * @brief A single Name Constraint
85 *
86 * The Name Constraint extension adds a minimum and maximum path
87 * length to a GeneralName to form a constraint. The length limits
88 * are currently unused.
89 */
90 class BOTAN_PUBLIC_API(2,0) GeneralSubtree final : public ASN1_Object
91 {
92 public:
93 /**
94 * Creates an empty name constraint.
95 */
96 GeneralSubtree() : m_base(), m_minimum(0), m_maximum(std::numeric_limits<std::size_t>::max())
97 {}
98
99 /***
100 * Creates a new name constraint.
101 * @param base name
102 * @param min minimum path length
103 * @param max maximum path length
104 */
105 GeneralSubtree(const GeneralName& base, size_t min, size_t max)
106 : m_base(base), m_minimum(min), m_maximum(max)
107 {}
108
109 /**
110 * Creates a new name constraint for its string format.
111 * @param str name constraint
112 */
113 GeneralSubtree(const std::string& str);
114
115 void encode_into(DER_Encoder&) const override;
116
117 void decode_from(BER_Decoder&) override;
118
119 /**
120 * @return name
121 */
122 const GeneralName& base() const { return m_base; }
123
124 /**
125 * @return minimum path length
126 */
127 size_t minimum() const { return m_minimum; }
128
129 /**
130 * @return maximum path length
131 */
132 size_t maximum() const { return m_maximum; }
133
134 private:
135 GeneralName m_base;
136 size_t m_minimum;
137 size_t m_maximum;
138 };
139
140 std::ostream& operator<<(std::ostream& os, const GeneralSubtree& gs);
141
142 /**
143 * @brief Name Constraints
144 *
145 * Wraps the Name Constraints associated with a certificate.
146 */
147 class BOTAN_PUBLIC_API(2,0) NameConstraints final
148 {
149 public:
150 /**
151 * Creates an empty name NameConstraints.
152 */
153 NameConstraints() : m_permitted_subtrees(), m_excluded_subtrees() {}
154
155 /**
156 * Creates NameConstraints from a list of permitted and excluded subtrees.
157 * @param permitted_subtrees names for which the certificate is permitted
158 * @param excluded_subtrees names for which the certificate is not permitted
159 */
160 NameConstraints(std::vector<GeneralSubtree>&& permitted_subtrees,
161 std::vector<GeneralSubtree>&& excluded_subtrees)
162 : m_permitted_subtrees(permitted_subtrees), m_excluded_subtrees(excluded_subtrees)
163 {}
164
165 /**
166 * @return permitted names
167 */
168 const std::vector<GeneralSubtree>& permitted() const { return m_permitted_subtrees; }
169
170 /**
171 * @return excluded names
172 */
173 const std::vector<GeneralSubtree>& excluded() const { return m_excluded_subtrees; }
174
175 private:
176 std::vector<GeneralSubtree> m_permitted_subtrees;
177 std::vector<GeneralSubtree> m_excluded_subtrees;
178 };
179
180 }
181
182 #endif
Botan::ASN1_Object
Definition: asn1_obj.h:66
Botan::GeneralName::MatchResult
MatchResult
Definition: name_constraint.h:31
Botan::operator<<
int operator<<(int fd, Pipe &pipe)
Definition: fd_unix.cpp:17
m_name
std::string m_name
Definition: openssl_hash.cpp:97
Botan::GeneralName::Some
Definition: name_constraint.h:34
Botan::GeneralName::UnknownType
Definition: name_constraint.h:37
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:28
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
std
Definition: bigint.h:1135
Botan::NameConstraints::permitted
const std::vector< GeneralSubtree > & permitted() const
Definition: name_constraint.h:168
Botan::DER_Encoder
Definition: der_enc.h:23
Botan::X509_Certificate
Definition: x509cert.h:40
Botan::GeneralSubtree::maximum
size_t maximum() const
Definition: name_constraint.h:132
Botan::NameConstraints::excluded
const std::vector< GeneralSubtree > & excluded() const
Definition: name_constraint.h:173
Botan::GeneralName::NotFound
Definition: name_constraint.h:36
Botan::NameConstraints::NameConstraints
NameConstraints(std::vector< GeneralSubtree > &&permitted_subtrees, std::vector< GeneralSubtree > &&excluded_subtrees)
Definition: name_constraint.h:160
Botan::GeneralName::None
Definition: name_constraint.h:35
Botan::PEM_Code::matches
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:142
Botan::GeneralName::All
Definition: name_constraint.h:33
Botan
Definition: alg_id.cpp:13
Botan::GeneralName
X.509 GeneralName Type.
Definition: name_constraint.h:28
Botan::NameConstraints::NameConstraints
NameConstraints()
Definition: name_constraint.h:153
Botan::GeneralSubtree
A single Name Constraint.
Definition: name_constraint.h:90
Botan::GeneralName::name
const std::string & name() const
Definition: name_constraint.h:63
Botan::GeneralSubtree::base
const GeneralName & base() const
Definition: name_constraint.h:122
Botan::GeneralSubtree::minimum
size_t minimum() const
Definition: name_constraint.h:127
Botan::GeneralName::type
const std::string & type() const
Definition: name_constraint.h:58
Botan::GeneralSubtree::GeneralSubtree
GeneralSubtree(const GeneralName &base, size_t min, size_t max)
Definition: name_constraint.h:105
Botan::GeneralSubtree::GeneralSubtree
GeneralSubtree()
Definition: name_constraint.h:96
Botan::BER_Decoder
Definition: ber_dec.h:21
Botan::NameConstraints
Name Constraints.
Definition: name_constraint.h:147