Botan 3.0.0-alpha0
Crypto and TLS for C&
Public Types | Public Member Functions | List of all members
Botan::GeneralName Class Referencefinal

X.509 GeneralName Type. More...

#include <pkix_types.h>

Inheritance diagram for Botan::GeneralName:
Botan::ASN1_Object

Public Types

enum  MatchResult : int {
  All , Some , None , NotFound ,
  UnknownType
}
 

Public Member Functions

std::vector< uint8_t > BER_encode () const
 
void decode_from (BER_Decoder &) override
 
void encode_into (DER_Encoder &) const override
 
 GeneralName ()=default
 
 GeneralName (const std::string &str)
 
MatchResult matches (const X509_Certificate &cert) const
 
const std::string & name () const
 
const std::string & type () const
 

Detailed Description

X.509 GeneralName Type.

Handles parsing GeneralName types in their BER and canonical string encoding. Allows matching GeneralNames against each other using the rules laid out in the RFC 5280, sec. 4.2.1.10 (Name Contraints).

Definition at line 194 of file pkix_types.h.

Member Enumeration Documentation

◆ MatchResult

Enumerator
All 
Some 
None 
NotFound 
UnknownType 

Definition at line 197 of file pkix_types.h.

Constructor & Destructor Documentation

◆ GeneralName() [1/2]

Botan::GeneralName::GeneralName ( )
default

Creates an empty GeneralName.

◆ GeneralName() [2/2]

Botan::GeneralName::GeneralName ( const std::string &  str)

Creates a new GeneralName for its string format.

Parameters
strtype and name, colon-separated, e.g., "DNS:google.com"

Definition at line 20 of file name_constraint.cpp.

20 : GeneralName()
21 {
22 size_t p = str.find(':');
23
24 if(p != std::string::npos)
25 {
26 m_type = str.substr(0, p);
27 m_name = str.substr(p + 1, std::string::npos);
28 }
29 else
30 {
31 throw Invalid_Argument("Failed to decode Name Constraint");
32 }
33 }
GeneralName()=default

Member Function Documentation

◆ BER_encode()

std::vector< uint8_t > Botan::ASN1_Object::BER_encode ( ) const
inherited

Return the encoding of this object. This is a convenience method when just one object needs to be serialized. Use DER_Encoder for complicated encodings.

Definition at line 16 of file asn1_obj.cpp.

17 {
18 std::vector<uint8_t> output;
19 DER_Encoder der(output);
20 this->encode_into(der);
21 return output;
22 }
virtual void encode_into(DER_Encoder &to) const =0

References Botan::ASN1_Object::encode_into().

Referenced by Botan::PSSR::config_for_x509(), Botan::Certificate_Store_In_SQL::find_all_certs(), Botan::Certificate_Store_In_SQL::find_cert(), Botan::X509_Certificate::fingerprint(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::X509_Object::PEM_encode(), and Botan::Certificate_Store_In_SQL::revoke_cert().

◆ decode_from()

void Botan::GeneralName::decode_from ( BER_Decoder from)
overridevirtual

Decode whatever this object is from from

Parameters
fromthe BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 40 of file name_constraint.cpp.

41 {
42 BER_Object obj = ber.get_next_object();
43
44 if(obj.is_a(1, ASN1_Class::ContextSpecific))
45 {
46 m_type = "RFC822";
47 m_name = ASN1::to_string(obj);
48 }
49 else if(obj.is_a(2, ASN1_Class::ContextSpecific))
50 {
51 m_type = "DNS";
52 m_name = ASN1::to_string(obj);
53 }
54 else if(obj.is_a(6, ASN1_Class::ContextSpecific))
55 {
56 m_type = "URI";
57 m_name = ASN1::to_string(obj);
58 }
60 {
61 m_type = "DN";
62 X509_DN dn;
63 BER_Decoder dec(obj);
64 std::stringstream ss;
65
66 dn.decode_from(dec);
67 ss << dn;
68
69 m_name = ss.str();
70 }
71 else if(obj.is_a(7, ASN1_Class::ContextSpecific))
72 {
73 if(obj.length() == 8)
74 {
75 m_type = "IP";
76 m_name = ipv4_to_string(load_be<uint32_t>(obj.bits(), 0)) + "/" +
77 ipv4_to_string(load_be<uint32_t>(obj.bits(), 1));
78 }
79 else if(obj.length() == 32)
80 {
81 throw Decoding_Error("Unsupported IPv6 name constraint");
82 }
83 else
84 {
85 throw Decoding_Error("Invalid IP name constraint size " + std::to_string(obj.length()));
86 }
87 }
88 else
89 {
90 throw Decoding_Error("Found unknown GeneralName type");
91 }
92 }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:209
constexpr uint32_t load_be< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:190
std::string ipv4_to_string(uint32_t ip)
Definition: parsing.cpp:181

References Botan::BER_Object::bits(), Botan::Constructed, Botan::ContextSpecific, Botan::X509_DN::decode_from(), Botan::BER_Decoder::get_next_object(), Botan::ipv4_to_string(), Botan::BER_Object::is_a(), Botan::BER_Object::length(), Botan::load_be< uint32_t >(), and Botan::ASN1::to_string().

◆ encode_into()

void Botan::GeneralName::encode_into ( DER_Encoder to) const
overridevirtual

Encode whatever this object is into to

Parameters
tothe DER_Encoder that will be written to

Implements Botan::ASN1_Object.

Definition at line 35 of file name_constraint.cpp.

36 {
37 throw Not_Implemented("GeneralName encoding");
38 }

◆ matches()

GeneralName::MatchResult Botan::GeneralName::matches ( const X509_Certificate cert) const

Checks whether a given certificate (partially) matches this name.

Parameters
certcertificate to be matched
Returns
the match result

Definition at line 94 of file name_constraint.cpp.

95 {
96 std::vector<std::string> nam;
97 std::function<bool(const GeneralName*, const std::string&)> match_fn;
98
99 const X509_DN& dn = cert.subject_dn();
100 const AlternativeName& alt_name = cert.subject_alt_name();
101
102 if(type() == "DNS")
103 {
104 match_fn = std::mem_fn(&GeneralName::matches_dns);
105
106 nam = alt_name.get_attribute("DNS");
107
108 if(nam.empty())
109 {
110 nam = dn.get_attribute("CN");
111 }
112 }
113 else if(type() == "DN")
114 {
115 match_fn = std::mem_fn(&GeneralName::matches_dn);
116
117 nam.push_back(dn.to_string());
118
119 const auto alt_dn = alt_name.dn();
120 if(alt_dn.empty() == false)
121 {
122 nam.push_back(alt_dn.to_string());
123 }
124 }
125 else if(type() == "IP")
126 {
127 match_fn = std::mem_fn(&GeneralName::matches_ip);
128 nam = alt_name.get_attribute("IP");
129 }
130 else
131 {
132 return MatchResult::UnknownType;
133 }
134
135 if(nam.empty())
136 {
137 return MatchResult::NotFound;
138 }
139
140 bool some = false;
141 bool all = true;
142
143 for(const std::string& n: nam)
144 {
145 bool m = match_fn(this, n);
146
147 some |= m;
148 all &= m;
149 }
150
151 if(all)
152 {
153 return MatchResult::All;
154 }
155 else if(some)
156 {
157 return MatchResult::Some;
158 }
159 else
160 {
161 return MatchResult::None;
162 }
163 }
const std::string & type() const
Definition: pkix_types.h:224

References Botan::AlternativeName::dn(), Botan::X509_DN::get_attribute(), Botan::AlternativeName::get_attribute(), Botan::X509_Certificate::subject_alt_name(), Botan::X509_Certificate::subject_dn(), Botan::X509_DN::to_string(), and type().

◆ name()

const std::string & Botan::GeneralName::name ( ) const
inline
Returns
The name as string. Format depends on type.

Definition at line 229 of file pkix_types.h.

229{ return m_name; }

Referenced by Botan::operator<<().

◆ type()

const std::string & Botan::GeneralName::type ( ) const
inline
Returns
Type of the name. Can be DN, DNS, IP, RFC822 or URI.

Definition at line 224 of file pkix_types.h.

224{ return m_type; }

Referenced by matches(), and Botan::operator<<().


The documentation for this class was generated from the following files: