Botan: src/cert/cvc/signed_obj.cpp Source File

Botan 1.10.16

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 */
19 SecureVector<byte> EAC_Signed_Object::BER_encode() const
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 */
43 AlgorithmIdentifier EAC_Signed_Object::signature_algorithm() const
44 {
45 return sig_algo;
46 }
47
48 bool EAC_Signed_Object::check_signature(Public_Key& pub_key,
49 const MemoryRegion<byte>& sig) const
50 {
51 try
52 {
53 std::vector<std::string> sig_info =
54 split_on(OIDS::lookup(sig_algo.oid), '/');
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 */
79 void EAC_Signed_Object::do_decode()
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 }
Botan::Pipe::start_msg
void start_msg()
Definition: pipe.cpp:152
Botan::EAC_Signed_Object::do_decode
void do_decode()
Definition: signed_obj.cpp:79
Botan::EAC_Signed_Object::check_signature
bool check_signature(class Public_Key &key, const MemoryRegion< byte > &sig) const
Definition: signed_obj.cpp:48
Botan::EAC_Signed_Object::tbs_data
virtual SecureVector< byte > tbs_data() const =0
Botan::Pipe::read_all_as_string
std::string read_all_as_string(message_id=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:117
Botan::EAC_Signed_Object::sig_algo
AlgorithmIdentifier sig_algo
Definition: signed_obj.h:85
Botan::EAC_Signed_Object::encode
virtual void encode(Pipe &pipe, X509_Encoding encoding=PEM) const =0
Botan::Signature_Format
Signature_Format
Definition: pubkey.h:24
Botan::split_on
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:152
Botan::Public_Key::algo_name
virtual std::string algo_name() const =0
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::Pipe::end_msg
void end_msg()
Definition: pipe.cpp:166
Botan::Pipe::read_all
SecureVector< byte > read_all(message_id msg=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:105
Botan::EAC_Signed_Object::PEM_encode
std::string PEM_encode() const
Definition: signed_obj.cpp:31
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::AlgorithmIdentifier::oid
OID oid
Definition: alg_id.h:35
Botan::PK_Verifier::verify_message
bool verify_message(const byte msg[], size_t msg_length, const byte sig[], size_t sig_length)
Definition: pubkey.cpp:283
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::EAC_Signed_Object::PEM_label_pref
std::string PEM_label_pref
Definition: signed_obj.h:87
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::PEM
Definition: pubkey_enums.h:67
Botan::PK_Verifier
Definition: pubkey.h:211
Botan::Public_Key
Definition: pk_keys.h:21
Botan::IEEE_1363
Definition: pubkey.h:24
Botan::RAW_BER
Definition: pubkey_enums.h:67
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::DER_SEQUENCE
Definition: pubkey.h:24
Botan::EAC_Signed_Object::signature_algorithm
AlgorithmIdentifier signature_algorithm() const
Definition: signed_obj.cpp:43
Botan::SecureVector< byte >
Botan::Pipe
Definition: pipe.h:25
Botan::EAC_Signed_Object::BER_encode
SecureVector< byte > BER_encode() const
Definition: signed_obj.cpp:19
Botan::Public_Key::message_parts
virtual size_t message_parts() const
Definition: pk_keys.h:50