Botan  1.10.9
signed_obj.cpp
Go to the documentation of this file.
1 /*
2 * EAC SIGNED Object
3 * (C) 1999-2010 Jack Lloyd
4 * 2007 FlexSecure GmbH
5 *
6 * Distributed under the terms of the Botan license
7 */
8 
9 #include <botan/signed_obj.h>
10 #include <botan/pubkey.h>
11 #include <botan/oids.h>
12 #include <memory>
13 
14 namespace Botan {
15 
16 /*
17 * Return a BER encoded X.509 object
18 */
20  {
21  Pipe ber;
22  ber.start_msg();
23  encode(ber, RAW_BER);
24  ber.end_msg();
25  return ber.read_all();
26  }
27 
28 /*
29 * Return a PEM encoded X.509 object
30 */
31 std::string EAC_Signed_Object::PEM_encode() const
32  {
33  Pipe pem;
34  pem.start_msg();
35  encode(pem, PEM);
36  pem.end_msg();
37  return pem.read_all_as_string();
38  }
39 
40 /*
41 * Return the algorithm used to sign this object
42 */
44  {
45  return sig_algo;
46  }
47 
49  const MemoryRegion<byte>& sig) const
50  {
51  try
52  {
53  std::vector<std::string> sig_info =
55 
56  if(sig_info.size() != 2 || sig_info[0] != pub_key.algo_name())
57  {
58  return false;
59  }
60 
61  std::string padding = sig_info[1];
62  Signature_Format format =
63  (pub_key.message_parts() >= 2) ? DER_SEQUENCE : IEEE_1363;
64 
65  SecureVector<byte> to_sign = tbs_data();
66 
67  PK_Verifier verifier(pub_key, padding, format);
68  return verifier.verify_message(to_sign, sig);
69  }
70  catch(...)
71  {
72  return false;
73  }
74  }
75 
76 /*
77 * Try to decode the actual information
78 */
80  {
81  try {
82  force_decode();
83  }
84  catch(Decoding_Error& e)
85  {
86  const std::string what = e.what();
87  throw Decoding_Error(PEM_label_pref + " decoding failed (" + what + ")");
88  }
89  catch(Invalid_Argument& e)
90  {
91  const std::string what = e.what();
92  throw Decoding_Error(PEM_label_pref + " decoding failed (" + what + ")");
93  }
94  }
95 
96 }
void start_msg()
Definition: pipe.cpp:152
virtual SecureVector< byte > tbs_data() const =0
std::string read_all_as_string(message_id=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:117
AlgorithmIdentifier sig_algo
Definition: signed_obj.h:85
virtual void encode(Pipe &pipe, X509_Encoding encoding=PEM) const =0
Signature_Format
Definition: pubkey.h:24
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:152
std::string PEM_encode() const
Definition: signed_obj.cpp:31
virtual std::string algo_name() const =0
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
SecureVector< byte > BER_encode() const
Definition: signed_obj.cpp:19
void end_msg()
Definition: pipe.cpp:166
SecureVector< byte > read_all(message_id msg=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:105
std::string lookup(const OID &oid)
Definition: oids.cpp:31
virtual size_t message_parts() const
Definition: pk_keys.h:50
bool verify_message(const byte msg[], size_t msg_length, const byte sig[], size_t sig_length)
Definition: pubkey.cpp:283
std::string PEM_label_pref
Definition: signed_obj.h:87
bool check_signature(class Public_Key &key, const MemoryRegion< byte > &sig) const
Definition: signed_obj.cpp:48
AlgorithmIdentifier signature_algorithm() const
Definition: signed_obj.cpp:43