Botan  2.4.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
Botan::X509_Object Class Referenceabstract

#include <x509_obj.h>

Inheritance diagram for Botan::X509_Object:
Botan::ASN1_Object Botan::PKCS10_Request Botan::X509_Certificate Botan::X509_CRL

Public Member Functions

virtual std::vector< std::string > alternate_PEM_labels () const
 
std::vector< uint8_t > BER_encode () const
 
bool check_signature (const Public_Key &key) const
 
bool check_signature (const Public_Key *key) const
 
void decode_from (class BER_Decoder &from) override
 
void encode_into (class DER_Encoder &to) const override
 
std::string hash_used_for_signature () const
 
X509_Objectoperator= (const X509_Object &)=default
 
std::string PEM_encode () const
 
virtual std::string PEM_label () const =0
 
const std::vector< uint8_t > & signature () const
 
const AlgorithmIdentifiersignature_algorithm () const
 
const std::vector< uint8_t > & signed_body () const
 
std::vector< uint8_t > tbs_data () const
 
Certificate_Status_Code verify_signature (const Public_Key &key) const
 
 X509_Object (const X509_Object &)=default
 
virtual ~X509_Object ()=default
 

Static Public Member Functions

static std::vector< uint8_t > make_signed (class PK_Signer *signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &tbs)
 

Protected Member Functions

void load_data (DataSource &src)
 
 X509_Object ()=default
 

Detailed Description

This class represents abstract X.509 signed objects as in the X.500 SIGNED macro

Definition at line 25 of file x509_obj.h.

Constructor & Destructor Documentation

◆ X509_Object() [1/2]

Botan::X509_Object::X509_Object ( const X509_Object )
default

◆ ~X509_Object()

virtual Botan::X509_Object::~X509_Object ( )
virtualdefault

◆ X509_Object() [2/2]

Botan::X509_Object::X509_Object ( )
protecteddefault

Member Function Documentation

◆ alternate_PEM_labels()

virtual std::vector<std::string> Botan::X509_Object::alternate_PEM_labels ( ) const
inlinevirtual

Definition at line 118 of file x509_obj.h.

119  { return std::vector<std::string>(); }

◆ BER_encode()

std::vector< uint8_t > Botan::X509_Object::BER_encode ( ) const
Returns
BER encoding of this

Definition at line 122 of file x509_obj.cpp.

References Botan::DER_Encoder::get_contents_unlocked().

123  {
124  DER_Encoder der;
125  encode_into(der);
126  return der.get_contents_unlocked();
127  }
void encode_into(class DER_Encoder &to) const override
Definition: x509_obj.cpp:92

◆ check_signature() [1/2]

bool Botan::X509_Object::check_signature ( const Public_Key key) const

Check the signature on this data

Parameters
keythe public key purportedly used to sign this data
Returns
true if the signature is valid, otherwise false

Definition at line 186 of file x509_obj.cpp.

References Botan::VERIFIED.

187  {
188  const Certificate_Status_Code code = verify_signature(pub_key);
189  return (code == Certificate_Status_Code::VERIFIED);
190  }
Certificate_Status_Code verify_signature(const Public_Key &key) const
Definition: x509_obj.cpp:192
Certificate_Status_Code
Definition: cert_status.h:18

◆ check_signature() [2/2]

bool Botan::X509_Object::check_signature ( const Public_Key key) const

Check the signature on this data

Parameters
keythe public key purportedly used to sign this data the object will be deleted after use (this should have been a std::unique_ptr<Public_Key>)
Returns
true if the signature is valid, otherwise false

Definition at line 178 of file x509_obj.cpp.

179  {
180  if(!pub_key)
181  throw Exception("No key provided for " + PEM_label() + " signature check");
182  std::unique_ptr<const Public_Key> key(pub_key);
183  return check_signature(*key);
184  }
virtual std::string PEM_label() const =0
bool check_signature(const Public_Key &key) const
Definition: x509_obj.cpp:186

◆ decode_from()

void Botan::X509_Object::decode_from ( class BER_Decoder from)
overridevirtual

Decode a BER encoded X509_Object See ASN1_Object::decode_from()

Implements Botan::ASN1_Object.

Definition at line 106 of file x509_obj.cpp.

References Botan::BIT_STRING, Botan::BER_Decoder::decode(), Botan::BER_Decoder::end_cons(), Botan::BER_Decoder::raw_bytes(), Botan::SEQUENCE, and Botan::BER_Decoder::start_cons().

107  {
108  from.start_cons(SEQUENCE)
109  .start_cons(SEQUENCE)
110  .raw_bytes(m_tbs_bits)
111  .end_cons()
112  .decode(m_sig_algo)
113  .decode(m_sig, BIT_STRING)
114  .end_cons();
115 
116  force_decode();
117  }

◆ encode_into()

void Botan::X509_Object::encode_into ( class DER_Encoder to) const
overridevirtual

DER encode an X509_Object See ASN1_Object::encode_into()

Implements Botan::ASN1_Object.

Definition at line 92 of file x509_obj.cpp.

References Botan::BIT_STRING, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::raw_bytes(), Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

Referenced by Botan::Certificate_Store_In_SQL::insert_cert().

93  {
94  to.start_cons(SEQUENCE)
95  .start_cons(SEQUENCE)
96  .raw_bytes(signed_body())
97  .end_cons()
98  .encode(signature_algorithm())
99  .encode(signature(), BIT_STRING)
100  .end_cons();
101  }
const AlgorithmIdentifier & signature_algorithm() const
Definition: x509_obj.h:47
const std::vector< uint8_t > & signature() const
Definition: x509_obj.h:37
const std::vector< uint8_t > & signed_body() const
Definition: x509_obj.h:42

◆ hash_used_for_signature()

std::string Botan::X509_Object::hash_used_for_signature ( ) const
Returns
hash algorithm that was used to generate signature

Definition at line 148 of file x509_obj.cpp.

References Botan::OID::as_string(), hash_algo, Botan::OIDS::lookup(), Botan::parse_algorithm_name(), and Botan::split_on().

149  {
150  const OID& oid = m_sig_algo.get_oid();
151  std::vector<std::string> sig_info = split_on(OIDS::lookup(oid), '/');
152 
153  if(sig_info.size() != 2)
154  throw Internal_Error("Invalid name format found for " +
155  oid.as_string());
156 
157  if(sig_info[1] == "EMSA4")
158  {
159  return OIDS::lookup(decode_pss_params(signature_algorithm().get_parameters()).hash_algo.get_oid());
160  }
161  else
162  {
163  std::vector<std::string> pad_and_hash =
164  parse_algorithm_name(sig_info[1]);
165 
166  if(pad_and_hash.size() != 2)
167  {
168  throw Internal_Error("Invalid name format " + sig_info[1]);
169  }
170 
171  return pad_and_hash[1];
172  }
173  }
const AlgorithmIdentifier & signature_algorithm() const
Definition: x509_obj.h:47
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:142
std::vector< std::string > parse_algorithm_name(const std::string &namex)
Definition: parsing.cpp:89
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22
const OID & get_oid() const
Definition: alg_id.h:37
std::string lookup(const OID &oid)
Definition: oids.cpp:18

◆ load_data()

void Botan::X509_Object::load_data ( DataSource src)
protected

Decodes from src as either DER or PEM data, then calls force_decode()

Definition at line 52 of file x509_obj.cpp.

References Botan::PEM_Code::decode(), Botan::PEM_Code::matches(), Botan::ASN1::maybe_BER(), and Botan::Exception::what().

53  {
54  try {
55  if(ASN1::maybe_BER(in) && !PEM_Code::matches(in))
56  {
57  BER_Decoder dec(in);
58  decode_from(dec);
59  }
60  else
61  {
62  std::string got_label;
63  DataSource_Memory ber(PEM_Code::decode(in, got_label));
64 
65  if(got_label != PEM_label())
66  {
67  bool is_alternate = false;
68  for(std::string alt_label : alternate_PEM_labels())
69  {
70  if(got_label == alt_label)
71  {
72  is_alternate = true;
73  break;
74  }
75  }
76 
77  if(!is_alternate)
78  throw Decoding_Error("Unexpected PEM label for " + PEM_label() + " of " + got_label);
79  }
80 
81  BER_Decoder dec(ber);
82  decode_from(dec);
83  }
84  }
85  catch(Decoding_Error& e)
86  {
87  throw Decoding_Error(PEM_label() + " decoding failed: " + e.what());
88  }
89  }
virtual std::vector< std::string > alternate_PEM_labels() const
Definition: x509_obj.h:118
virtual std::string PEM_label() const =0
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:116
void decode_from(class BER_Decoder &from) override
Definition: x509_obj.cpp:106
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:142
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition: pem.cpp:68

◆ make_signed()

std::vector< uint8_t > Botan::X509_Object::make_signed ( class PK_Signer signer,
RandomNumberGenerator rng,
const AlgorithmIdentifier alg_id,
const secure_vector< uint8_t > &  tbs 
)
static

Create a signed X509 object.

Parameters
signerthe signer used to sign the object
rngthe random number generator to use
alg_idthe algorithm identifier of the signature scheme
tbsthe tbs bits to be signed
Returns
signed X509 object

Definition at line 271 of file x509_obj.cpp.

References Botan::BIT_STRING, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::get_contents_unlocked(), Botan::DER_Encoder::raw_bytes(), Botan::SEQUENCE, Botan::PK_Signer::sign_message(), and Botan::DER_Encoder::start_cons().

Referenced by Botan::X509::create_cert_req(), Botan::X509_CA::make_cert(), and Botan::X509_CA::update_crl().

275  {
276  const std::vector<uint8_t> signature = signer->sign_message(tbs_bits, rng);
277 
278  return DER_Encoder()
279  .start_cons(SEQUENCE)
280  .raw_bytes(tbs_bits)
281  .encode(algo)
282  .encode(signature, BIT_STRING)
283  .end_cons()
284  .get_contents_unlocked();
285  }
const std::vector< uint8_t > & signature() const
Definition: x509_obj.h:37

◆ operator=()

X509_Object& Botan::X509_Object::operator= ( const X509_Object )
default

◆ PEM_encode()

std::string Botan::X509_Object::PEM_encode ( ) const
Returns
PEM encoding of this

Definition at line 132 of file x509_obj.cpp.

References Botan::PKCS8::BER_encode(), and Botan::PEM_Code::encode().

133  {
135  }
virtual std::string PEM_label() const =0
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
std::vector< uint8_t > BER_encode() const
Definition: x509_obj.cpp:122

◆ PEM_label()

virtual std::string Botan::X509_Object::PEM_label ( ) const
pure virtual

◆ signature()

const std::vector<uint8_t>& Botan::X509_Object::signature ( ) const
inline
Returns
signature on tbs_data()

Definition at line 37 of file x509_obj.h.

Referenced by Botan::X509_Certificate::operator<(), and Botan::X509_Certificate::operator==().

37 { return m_sig; }

◆ signature_algorithm()

const AlgorithmIdentifier& Botan::X509_Object::signature_algorithm ( ) const
inline
Returns
signature algorithm that was used to generate signature

Definition at line 47 of file x509_obj.h.

References Botan::PKCS8::BER_encode(), and Botan::PKCS8::PEM_encode().

Referenced by Botan::X509_CRL::is_revoked(), Botan::X509_Certificate::operator==(), Botan::X509_CA::X509_CA(), and Botan::X509_Certificate::X509_Certificate().

47 { return m_sig_algo; }

◆ signed_body()

const std::vector<uint8_t>& Botan::X509_Object::signed_body ( ) const
inline

◆ tbs_data()

std::vector< uint8_t > Botan::X509_Object::tbs_data ( ) const

The underlying data that is to be or was signed

Returns
data that is or was signed

Definition at line 140 of file x509_obj.cpp.

References Botan::ASN1::put_in_sequence().

141  {
142  return ASN1::put_in_sequence(m_tbs_bits);
143  }
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition: asn1_obj.cpp:96

◆ verify_signature()

Certificate_Status_Code Botan::X509_Object::verify_signature ( const Public_Key key) const

Check the signature on this data

Parameters
keythe public key purportedly used to sign this data
Returns
status of the signature - OK if verified or otherwise an indicator of the problem preventing verification.

Definition at line 192 of file x509_obj.cpp.

References Botan::Public_Key::algo_name(), Botan::DER_SEQUENCE, hash_algo, Botan::IEEE_1363, Botan::OIDS::lookup(), Botan::Public_Key::message_parts(), Botan::SIGNATURE_ALGO_BAD_PARAMS, Botan::SIGNATURE_ALGO_UNKNOWN, Botan::SIGNATURE_ERROR, Botan::split_on(), Botan::ASN1::to_string(), Botan::UNTRUSTED_HASH, Botan::VERIFIED, and Botan::PK_Verifier::verify_message().

Referenced by Botan::X509_Certificate::X509_Certificate().

193  {
194  const std::vector<std::string> sig_info =
195  split_on(OIDS::lookup(m_sig_algo.get_oid()), '/');
196 
197  if(sig_info.size() != 2 || sig_info[0] != pub_key.algo_name())
199 
200  std::string padding = sig_info[1];
201  const Signature_Format format =
202  (pub_key.message_parts() >= 2) ? DER_SEQUENCE : IEEE_1363;
203 
204  if(padding == "EMSA4")
205  {
206  // "MUST contain RSASSA-PSS-params"
207  if(signature_algorithm().parameters.empty())
208  {
210  }
211 
212  Pss_params pss_parameter = decode_pss_params(signature_algorithm().parameters);
213 
214  // hash_algo must be SHA1, SHA2-224, SHA2-256, SHA2-384 or SHA2-512
215  const std::string hash_algo = OIDS::lookup(pss_parameter.hash_algo.oid);
216  if(hash_algo != "SHA-160" &&
217  hash_algo != "SHA-224" &&
218  hash_algo != "SHA-256" &&
219  hash_algo != "SHA-384" &&
220  hash_algo != "SHA-512")
221  {
223  }
224 
225  const std::string mgf_algo = OIDS::lookup(pss_parameter.mask_gen_algo.oid);
226  if(mgf_algo != "MGF1")
227  {
229  }
230 
231  // For MGF1, it is strongly RECOMMENDED that the underlying hash function be the same as the one identified by hashAlgorithm
232  // Must be SHA1, SHA2-224, SHA2-256, SHA2-384 or SHA2-512
233  if(pss_parameter.mask_gen_hash.oid != pss_parameter.hash_algo.oid)
234  {
236  }
237 
238  if(pss_parameter.trailer_field != 1)
239  {
241  }
242 
243  // salt_len is actually not used for verification. Length is inferred from the signature
244  padding += "(" + hash_algo + "," + mgf_algo + "," + std::to_string(pss_parameter.salt_len) + ")";
245  }
246 
247  try
248  {
249  PK_Verifier verifier(pub_key, padding, format);
250  const bool valid = verifier.verify_message(tbs_data(), signature());
251 
252  if(valid)
254  else
256  }
257  catch(Algorithm_Not_Found&)
258  {
260  }
261  catch(...)
262  {
263  // This shouldn't happen, fallback to generic signature error
265  }
266  }
const AlgorithmIdentifier & signature_algorithm() const
Definition: x509_obj.h:47
std::vector< uint8_t > parameters
Definition: alg_id.h:45
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:142
Signature_Format
Definition: pubkey.h:27
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
const std::vector< uint8_t > & signature() const
Definition: x509_obj.h:37
std::vector< uint8_t > tbs_data() const
Definition: x509_obj.cpp:140
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22
const OID & get_oid() const
Definition: alg_id.h:37
std::string lookup(const OID &oid)
Definition: oids.cpp:18

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