diff options
Diffstat (limited to 'src/if_algo.cpp')
-rw-r--r-- | src/if_algo.cpp | 148 |
1 files changed, 79 insertions, 69 deletions
diff --git a/src/if_algo.cpp b/src/if_algo.cpp index 3668580b3..97d0d0510 100644 --- a/src/if_algo.cpp +++ b/src/if_algo.cpp @@ -15,12 +15,12 @@ namespace Botan { *************************************************/ X509_Encoder* IF_Scheme_PublicKey::x509_encoder() const { - class IF_Algo_Encoder : public X509_Encoder + class IF_Scheme_Encoder : public X509_Encoder { public: AlgorithmIdentifier alg_id() const { - return AlgorithmIdentifier(oid, + return AlgorithmIdentifier(key->get_oid(), AlgorithmIdentifier::USE_NULL_PARAM); } @@ -28,24 +28,18 @@ X509_Encoder* IF_Scheme_PublicKey::x509_encoder() const { return DER_Encoder() .start_cons(SEQUENCE) - .encode(n) - .encode(e) + .encode(key->n) + .encode(key->e) .end_cons() .get_contents(); } - IF_Algo_Encoder(const OID& oid, const BigInt& n, const BigInt& e) - { - this->oid = oid; - this->n = n; - this->e = e; - } + IF_Scheme_Encoder(const IF_Scheme_PublicKey* k) : key(k) {} private: - OID oid; - BigInt n, e; + const IF_Scheme_PublicKey* key; }; - return new IF_Algo_Encoder(get_oid(), n, e); + return new IF_Scheme_Encoder(this); } /************************************************* @@ -53,7 +47,7 @@ X509_Encoder* IF_Scheme_PublicKey::x509_encoder() const *************************************************/ X509_Decoder* IF_Scheme_PublicKey::x509_decoder() { - class IF_Algo_Decoder : public X509_Decoder + class IF_Scheme_Decoder : public X509_Decoder { public: void alg_id(const AlgorithmIdentifier&) {} @@ -70,77 +64,93 @@ X509_Decoder* IF_Scheme_PublicKey::x509_decoder() key->X509_load_hook(); } - IF_Algo_Decoder(IF_Scheme_PublicKey* k) : key(k) {} + IF_Scheme_Decoder(IF_Scheme_PublicKey* k) : key(k) {} private: IF_Scheme_PublicKey* key; }; - return new IF_Algo_Decoder(this); + return new IF_Scheme_Decoder(this); } /************************************************* -* Return the X.509 parameters encoding * +* Return the PKCS #8 public key encoder * *************************************************/ -MemoryVector<byte> IF_Scheme_PrivateKey::DER_encode_params() const +PKCS8_Encoder* IF_Scheme_PrivateKey::pkcs8_encoder() const { - return DER_Encoder().encode_null().get_contents(); - } + class IF_Scheme_Encoder : public PKCS8_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + return AlgorithmIdentifier(key->get_oid(), + AlgorithmIdentifier::USE_NULL_PARAM); + } -/************************************************* -* Decode X.509 algorithm parameters * -*************************************************/ -void IF_Scheme_PrivateKey::BER_decode_params(DataSource& source) - { - byte dummy = 0; - while(!source.end_of_data()) - source.read_byte(dummy); - } + MemoryVector<byte> key_bits() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode((u32bit)0) + .encode(key->n) + .encode(key->e) + .encode(key->d) + .encode(key->p) + .encode(key->q) + .encode(key->d1) + .encode(key->d2) + .encode(key->c) + .end_cons() + .get_contents(); + } -/************************************************* -* Return the PKCS #1 private key encoding * -*************************************************/ -SecureVector<byte> IF_Scheme_PrivateKey::DER_encode_priv() const - { - return DER_Encoder() - .start_cons(SEQUENCE) - .encode((u32bit)0) - .encode(n) - .encode(e) - .encode(d) - .encode(p) - .encode(q) - .encode(d1) - .encode(d2) - .encode(c) - .end_cons() - .get_contents(); + IF_Scheme_Encoder(const IF_Scheme_PrivateKey* k) : key(k) {} + private: + const IF_Scheme_PrivateKey* key; + }; + + return new IF_Scheme_Encoder(this); } /************************************************* -* Decode a PKCS #1 private key encoding * +* Return the PKCS #8 public key decoder * *************************************************/ -void IF_Scheme_PrivateKey::BER_decode_priv(DataSource& source) +PKCS8_Decoder* IF_Scheme_PrivateKey::pkcs8_decoder() { - u32bit version; - - BER_Decoder(source) - .start_cons(SEQUENCE) - .decode(version) - .decode(n) - .decode(e) - .decode(d) - .decode(p) - .decode(q) - .decode(d1) - .decode(d2) - .decode(c) - .end_cons(); - - if(version != 0) - throw Decoding_Error(algo_name() + ": Unknown PKCS #1 key version"); - - PKCS8_load_hook(); - check_loaded_private(); + class IF_Scheme_Decoder : public PKCS8_Decoder + { + public: + void alg_id(const AlgorithmIdentifier&) {} + + void key_bits(const MemoryRegion<byte>& bits) + { + u32bit version; + + BER_Decoder(bits) + .start_cons(SEQUENCE) + .decode(version) + .decode(key->n) + .decode(key->e) + .decode(key->d) + .decode(key->p) + .decode(key->q) + .decode(key->d1) + .decode(key->d2) + .decode(key->c) + .end_cons(); + + if(version != 0) + throw Decoding_Error(key->algo_name() + + ": Unknown PKCS #1 key version"); + + key->PKCS8_load_hook(); + } + + IF_Scheme_Decoder(IF_Scheme_PrivateKey* k) : key(k) {} + private: + IF_Scheme_PrivateKey* key; + }; + + return new IF_Scheme_Decoder(this); } /************************************************* |