Botan  2.4.0
Crypto and TLS for C++11
Public Types | Public Member Functions | List of all members
Botan::GeneralName Class Referencefinal

X.509 GeneralName Type. More...

#include <name_constraint.h>

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

Public Types

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

Public Member Functions

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 28 of file name_constraint.h.

Member Enumeration Documentation

◆ MatchResult

Enumerator
All 
Some 
None 
NotFound 
UnknownType 

Definition at line 31 of file name_constraint.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 21 of file name_constraint.cpp.

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

Member Function Documentation

◆ 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 41 of file name_constraint.cpp.

References Botan::BER_Object::class_tag, Botan::CONSTRUCTED, Botan::CONTEXT_SPECIFIC, Botan::BER_Decoder::get_next_object(), and Botan::BER_Object::type_tag.

42  {
43  BER_Object obj = ber.get_next_object();
44  if((obj.class_tag != CONTEXT_SPECIFIC) &&
45  (obj.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED)))
46  throw Decoding_Error("Invalid class tag while decoding GeneralName");
47 
48  const ASN1_Tag tag = obj.type_tag;
49 
50  if(tag == 1 || tag == 2 || tag == 6)
51  {
52  m_name = ASN1::to_string(obj);
53 
54  if(tag == 1)
55  {
56  m_type = "RFC822";
57  }
58  else if(tag == 2)
59  {
60  m_type = "DNS";
61  }
62  else if(tag == 6)
63  {
64  m_type = "URI";
65  }
66  }
67  else if(tag == 4)
68  {
69  X509_DN dn;
70  BER_Decoder dec(obj.value);
71  std::stringstream ss;
72 
73  dn.decode_from(dec);
74  ss << dn;
75 
76  m_name = ss.str();
77  m_type = "DN";
78  }
79  else if(tag == 7)
80  {
81  if(obj.value.size() == 8)
82  {
83  const std::vector<uint8_t> ip(obj.value.begin(), obj.value.begin() + 4);
84  const std::vector<uint8_t> net(obj.value.begin() + 4, obj.value.end());
85  m_type = "IP";
86  m_name = ipv4_to_string(load_be<uint32_t>(ip.data(), 0)) + "/" + ipv4_to_string(load_be<uint32_t>(net.data(), 0));
87  }
88  else if(obj.value.size() == 32)
89  {
90  throw Decoding_Error("Unsupported IPv6 name constraint");
91  }
92  else
93  {
94  throw Decoding_Error("Invalid IP name constraint size " +
95  std::to_string(obj.value.size()));
96  }
97  }
98  else
99  {
100  throw Decoding_Error("Found unknown GeneralName type");
101  }
102  }
uint32_t load_be< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:177
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
ASN1_Tag
Definition: asn1_obj.h:22
std::string ipv4_to_string(uint32_t ip)
Definition: parsing.cpp:292

◆ 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 36 of file name_constraint.cpp.

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

◆ 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 104 of file name_constraint.cpp.

References Botan::AlternativeName::get_attribute(), Botan::X509_DN::get_attribute(), Botan::X509_DN::get_attributes(), name(), Botan::split_on(), Botan::string_to_ipv4(), Botan::X509_Certificate::subject_alt_name(), Botan::X509_Certificate::subject_dn(), and type().

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

◆ name()

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

Definition at line 63 of file name_constraint.h.

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

63 { return m_name; }

◆ 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 58 of file name_constraint.h.

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

58 { return m_type; }

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