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

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 (class BER_Decoder &) override
 
void encode_into (class 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 25 of file name_constraint.h.

Member Enumeration Documentation

Enumerator
All 
Some 
None 
NotFound 
UnknownType 

Definition at line 28 of file name_constraint.h.

Constructor & Destructor Documentation

Botan::GeneralName::GeneralName ( )
default

Creates an empty GeneralName.

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

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

Member Function Documentation

void Botan::GeneralName::decode_from ( class 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 39 of file name_constraint.cpp.

References Botan::BER_Object::class_tag, Botan::CONSTRUCTED, Botan::CONTEXT_SPECIFIC, Botan::X509_DN::decode_from(), Botan::BER_Decoder::get_next_object(), Botan::ipv4_to_string(), Botan::LATIN1_CHARSET, Botan::load_be< uint32_t >(), Botan::LOCAL_CHARSET, Botan::ASN1::to_string(), Botan::Charset::transcode(), Botan::BER_Object::type_tag, and Botan::BER_Object::value.

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

35  {
36  throw Not_Implemented("GeneralName encoding");
37  }
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 103 of file name_constraint.cpp.

References Botan::X509_Certificate::subject_dn(), Botan::X509_Certificate::subject_info(), and type().

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

Definition at line 60 of file name_constraint.h.

References m_name.

Referenced by Botan::operator<<().

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

Definition at line 55 of file name_constraint.h.

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

55 { return m_type; }

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