Botan  1.11.15
src/lib/pubkey/x509_key.cpp
Go to the documentation of this file.
00001 /*
00002 * X.509 Public Key
00003 * (C) 1999-2010 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/x509_key.h>
00009 #include <botan/der_enc.h>
00010 #include <botan/ber_dec.h>
00011 #include <botan/pem.h>
00012 #include <botan/alg_id.h>
00013 #include <botan/internal/pk_algs.h>
00014 
00015 namespace Botan {
00016 
00017 namespace X509 {
00018 
00019 std::vector<byte> BER_encode(const Public_Key& key)
00020    {
00021    return DER_Encoder()
00022          .start_cons(SEQUENCE)
00023             .encode(key.algorithm_identifier())
00024             .encode(key.x509_subject_public_key(), BIT_STRING)
00025          .end_cons()
00026       .get_contents_unlocked();
00027    }
00028 
00029 /*
00030 * PEM encode a X.509 public key
00031 */
00032 std::string PEM_encode(const Public_Key& key)
00033    {
00034    return PEM_Code::encode(X509::BER_encode(key),
00035                            "PUBLIC KEY");
00036    }
00037 
00038 /*
00039 * Extract a public key and return it
00040 */
00041 Public_Key* load_key(DataSource& source)
00042    {
00043    try {
00044       AlgorithmIdentifier alg_id;
00045       secure_vector<byte> key_bits;
00046 
00047       if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
00048          {
00049          BER_Decoder(source)
00050             .start_cons(SEQUENCE)
00051             .decode(alg_id)
00052             .decode(key_bits, BIT_STRING)
00053             .verify_end()
00054          .end_cons();
00055          }
00056       else
00057          {
00058          DataSource_Memory ber(
00059             PEM_Code::decode_check_label(source, "PUBLIC KEY")
00060             );
00061 
00062          BER_Decoder(ber)
00063             .start_cons(SEQUENCE)
00064             .decode(alg_id)
00065             .decode(key_bits, BIT_STRING)
00066             .verify_end()
00067          .end_cons();
00068          }
00069 
00070       if(key_bits.empty())
00071          throw Decoding_Error("X.509 public key decoding failed");
00072 
00073       return make_public_key(alg_id, key_bits);
00074       }
00075    catch(Decoding_Error& e)
00076       {
00077       throw Decoding_Error("X.509 public key decoding failed: " + std::string(e.what()));
00078       }
00079    }
00080 
00081 /*
00082 * Extract a public key and return it
00083 */
00084 Public_Key* load_key(const std::string& fsname)
00085    {
00086    DataSource_Stream source(fsname, true);
00087    return X509::load_key(source);
00088    }
00089 
00090 /*
00091 * Extract a public key and return it
00092 */
00093 Public_Key* load_key(const std::vector<byte>& mem)
00094    {
00095    DataSource_Memory source(mem);
00096    return X509::load_key(source);
00097    }
00098 
00099 /*
00100 * Make a copy of this public key
00101 */
00102 Public_Key* copy_key(const Public_Key& key)
00103    {
00104    DataSource_Memory source(PEM_encode(key));
00105    return X509::load_key(source);
00106    }
00107 
00108 }
00109 
00110 }