Botan  1.11.15
src/lib/cert/cvc/signed_obj.cpp
Go to the documentation of this file.
00001 /*
00002 * EAC SIGNED Object
00003 * (C) 1999-2010 Jack Lloyd
00004 *     2007 FlexSecure GmbH
00005 *
00006 * Botan is released under the Simplified BSD License (see license.txt)
00007 */
00008 
00009 #include <botan/signed_obj.h>
00010 #include <botan/pubkey.h>
00011 #include <botan/oids.h>
00012 
00013 namespace Botan {
00014 
00015 /*
00016 * Return a BER encoded X.509 object
00017 */
00018 std::vector<byte> EAC_Signed_Object::BER_encode() const
00019    {
00020    Pipe ber;
00021    ber.start_msg();
00022    encode(ber, RAW_BER);
00023    ber.end_msg();
00024    return unlock(ber.read_all());
00025    }
00026 
00027 /*
00028 * Return a PEM encoded X.509 object
00029 */
00030 std::string EAC_Signed_Object::PEM_encode() const
00031    {
00032    Pipe pem;
00033    pem.start_msg();
00034    encode(pem, PEM);
00035    pem.end_msg();
00036    return pem.read_all_as_string();
00037    }
00038 
00039 /*
00040 * Return the algorithm used to sign this object
00041 */
00042 AlgorithmIdentifier EAC_Signed_Object::signature_algorithm() const
00043    {
00044    return sig_algo;
00045    }
00046 
00047 bool EAC_Signed_Object::check_signature(Public_Key& pub_key,
00048                                         const std::vector<byte>& sig) const
00049    {
00050    try
00051       {
00052       std::vector<std::string> sig_info =
00053          split_on(OIDS::lookup(sig_algo.oid), '/');
00054 
00055       if(sig_info.size() != 2 || sig_info[0] != pub_key.algo_name())
00056          {
00057          return false;
00058          }
00059 
00060       std::string padding = sig_info[1];
00061       Signature_Format format =
00062          (pub_key.message_parts() >= 2) ? DER_SEQUENCE : IEEE_1363;
00063 
00064       std::vector<byte> to_sign = tbs_data();
00065 
00066       PK_Verifier verifier(pub_key, padding, format);
00067       return verifier.verify_message(to_sign, sig);
00068       }
00069    catch(...)
00070       {
00071       return false;
00072       }
00073    }
00074 
00075 /*
00076 * Try to decode the actual information
00077 */
00078 void EAC_Signed_Object::do_decode()
00079    {
00080    try {
00081       force_decode();
00082    }
00083    catch(Decoding_Error& e)
00084       {
00085       const std::string what = e.what();
00086       throw Decoding_Error(PEM_label_pref + " decoding failed (" + what + ")");
00087       }
00088    catch(Invalid_Argument& e)
00089       {
00090       const std::string what = e.what();
00091       throw Decoding_Error(PEM_label_pref + " decoding failed (" + what + ")");
00092       }
00093    }
00094 
00095 }