diff options
-rw-r--r-- | src/pubkey/pk_keys.cpp | 14 | ||||
-rw-r--r-- | src/pubkey/pk_keys.h | 2 | ||||
-rw-r--r-- | src/pubkey/pkcs8.cpp | 24 |
3 files changed, 19 insertions, 21 deletions
diff --git a/src/pubkey/pk_keys.cpp b/src/pubkey/pk_keys.cpp index b93158558..c7ca9673e 100644 --- a/src/pubkey/pk_keys.cpp +++ b/src/pubkey/pk_keys.cpp @@ -6,6 +6,7 @@ */ #include <botan/pk_keys.h> +#include <botan/der_enc.h> #include <botan/oids.h> namespace Botan { @@ -24,6 +25,19 @@ OID Public_Key::get_oid() const } } +SecureVector<byte> Private_Key::PKCS8_BER_encode() const + { + const u32bit PKCS8_VERSION = 0; + + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(PKCS8_VERSION) + .encode(this->pkcs8_algorithm_identifier()) + .encode(this->pkcs8_private_key(), OCTET_STRING) + .end_cons() + .get_contents(); + } + /* * Run checks on a loaded public key */ diff --git a/src/pubkey/pk_keys.h b/src/pubkey/pk_keys.h index da73db0ee..fab8e99ae 100644 --- a/src/pubkey/pk_keys.h +++ b/src/pubkey/pk_keys.h @@ -94,6 +94,8 @@ class BOTAN_DLL Private_Key : public virtual Public_Key virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const { return algorithm_identifier(); } + SecureVector<byte> PKCS8_BER_encode() const; + protected: void load_check(RandomNumberGenerator&) const; void gen_check(RandomNumberGenerator&) const; diff --git a/src/pubkey/pkcs8.cpp b/src/pubkey/pkcs8.cpp index 35ff7f206..00993223f 100644 --- a/src/pubkey/pkcs8.cpp +++ b/src/pubkey/pkcs8.cpp @@ -133,16 +133,7 @@ SecureVector<byte> PKCS8_decode(DataSource& source, const User_Interface& ui, */ void encode(const Private_Key& key, Pipe& pipe, X509_Encoding encoding) { - const u32bit PKCS8_VERSION = 0; - - SecureVector<byte> contents = - DER_Encoder() - .start_cons(SEQUENCE) - .encode(PKCS8_VERSION) - .encode(key.pkcs8_algorithm_identifier()) - .encode(key.pkcs8_private_key(), OCTET_STRING) - .end_cons() - .get_contents(); + SecureVector<byte> contents = key.PKCS8_BER_encode(); if(encoding == PEM) pipe.write(PEM_Code::encode(contents, "PRIVATE KEY")); @@ -161,11 +152,6 @@ void encrypt_key(const Private_Key& key, { const std::string DEFAULT_PBE = "PBE-PKCS5v20(SHA-1,AES-128/CBC)"; - Pipe raw_key; - raw_key.start_msg(); - encode(key, raw_key, RAW_BER); - raw_key.end_msg(); - std::auto_ptr<PBE> pbe(get_pbe(((pbe_algo != "") ? pbe_algo : DEFAULT_PBE))); pbe->new_params(rng); @@ -174,7 +160,7 @@ void encrypt_key(const Private_Key& key, AlgorithmIdentifier pbe_algid(pbe->get_oid(), pbe->encode_params()); Pipe key_encrytor(pbe.release()); - key_encrytor.process_msg(raw_key); + key_encrytor.process_msg(key.PKCS8_BER_encode()); SecureVector<byte> enc_key = DER_Encoder() @@ -195,11 +181,7 @@ void encrypt_key(const Private_Key& key, */ std::string PEM_encode(const Private_Key& key) { - Pipe pem; - pem.start_msg(); - encode(key, pem, PEM); - pem.end_msg(); - return pem.read_all_as_string(); + return PEM_Code::encode(key.PKCS8_BER_encode(), "PRIVATE KEY"); } /* |