aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/pubkey/dl_algo/dl_algo.cpp7
-rw-r--r--src/pubkey/dl_algo/dl_algo.h2
-rw-r--r--src/pubkey/ecc_key/ecc_key.cpp19
-rw-r--r--src/pubkey/ecc_key/ecc_key.h2
-rw-r--r--src/pubkey/if_algo/if_algo.cpp31
-rw-r--r--src/pubkey/if_algo/if_algo.h2
-rw-r--r--src/pubkey/pk_codecs/pkcs8.cpp8
-rw-r--r--src/pubkey/pk_keys.h5
8 files changed, 49 insertions, 27 deletions
diff --git a/src/pubkey/dl_algo/dl_algo.cpp b/src/pubkey/dl_algo/dl_algo.cpp
index b8f96bcf1..bab535c69 100644
--- a/src/pubkey/dl_algo/dl_algo.cpp
+++ b/src/pubkey/dl_algo/dl_algo.cpp
@@ -51,6 +51,11 @@ X509_Decoder* DL_Scheme_PublicKey::x509_decoder()
return new DL_Scheme_Decoder(this);
}
+MemoryVector<byte> DL_Scheme_PrivateKey::pkcs8_private_key() const
+ {
+ return DER_Encoder().encode(x).get_contents();
+ }
+
/*
* Return the PKCS #8 private key encoder
*/
@@ -66,7 +71,7 @@ PKCS8_Encoder* DL_Scheme_PrivateKey::pkcs8_encoder() const
MemoryVector<byte> key_bits() const
{
- return DER_Encoder().encode(key->x).get_contents();
+ return key->pkcs8_private_key();
}
DL_Scheme_Encoder(const DL_Scheme_PrivateKey* k) : key(k) {}
diff --git a/src/pubkey/dl_algo/dl_algo.h b/src/pubkey/dl_algo/dl_algo.h
index 63875d27b..e06630011 100644
--- a/src/pubkey/dl_algo/dl_algo.h
+++ b/src/pubkey/dl_algo/dl_algo.h
@@ -96,6 +96,8 @@ class BOTAN_DLL DL_Scheme_PrivateKey : public virtual DL_Scheme_PublicKey,
*/
PKCS8_Encoder* pkcs8_encoder() const;
+ MemoryVector<byte> pkcs8_private_key() const;
+
/**
* Get an PKCS#8 decoder for this key.
* @param rng the rng to use
diff --git a/src/pubkey/ecc_key/ecc_key.cpp b/src/pubkey/ecc_key/ecc_key.cpp
index f91f394dc..b09ddfdb7 100644
--- a/src/pubkey/ecc_key/ecc_key.cpp
+++ b/src/pubkey/ecc_key/ecc_key.cpp
@@ -137,6 +137,17 @@ EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng,
}
}
+MemoryVector<byte> EC_PrivateKey::pkcs8_private_key() const
+ {
+ return DER_Encoder()
+ .start_cons(SEQUENCE)
+ .encode(BigInt(1))
+ .encode(BigInt::encode_1363(private_key, private_key.bytes()),
+ OCTET_STRING)
+ .end_cons()
+ .get_contents();
+ }
+
/**
* Return the PKCS #8 public key encoder
**/
@@ -152,13 +163,7 @@ PKCS8_Encoder* EC_PrivateKey::pkcs8_encoder() const
MemoryVector<byte> key_bits() const
{
- return DER_Encoder()
- .start_cons(SEQUENCE)
- .encode(BigInt(1))
- .encode(BigInt::encode_1363(key->private_key, key->private_key.bytes()),
- OCTET_STRING)
- .end_cons()
- .get_contents();
+ return key->pkcs8_private_key();
}
EC_Key_Encoder(const EC_PrivateKey* k): key(k) {}
diff --git a/src/pubkey/ecc_key/ecc_key.h b/src/pubkey/ecc_key/ecc_key.h
index 226427768..8fcb7454f 100644
--- a/src/pubkey/ecc_key/ecc_key.h
+++ b/src/pubkey/ecc_key/ecc_key.h
@@ -110,6 +110,8 @@ class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey,
virtual ~EC_PrivateKey() {}
+ MemoryVector<byte> pkcs8_private_key() const;
+
/**
* Get an PKCS#8 encoder that can be used to encoded this key.
* @result an PKCS#8 encoder for this key
diff --git a/src/pubkey/if_algo/if_algo.cpp b/src/pubkey/if_algo/if_algo.cpp
index 2a7b19d3b..1a1ade28e 100644
--- a/src/pubkey/if_algo/if_algo.cpp
+++ b/src/pubkey/if_algo/if_algo.cpp
@@ -58,6 +58,23 @@ X509_Decoder* IF_Scheme_PublicKey::x509_decoder()
return new IF_Scheme_Decoder(this);
}
+MemoryVector<byte> IF_Scheme_PrivateKey::pkcs8_private_key() const
+ {
+ return DER_Encoder()
+ .start_cons(SEQUENCE)
+ .encode(static_cast<u32bit>(0))
+ .encode(n)
+ .encode(e)
+ .encode(d)
+ .encode(p)
+ .encode(q)
+ .encode(d1)
+ .encode(d2)
+ .encode(c)
+ .end_cons()
+ .get_contents();
+ }
+
/*
* Return the PKCS #8 public key encoder
*/
@@ -73,19 +90,7 @@ PKCS8_Encoder* IF_Scheme_PrivateKey::pkcs8_encoder() const
MemoryVector<byte> key_bits() const
{
- return DER_Encoder()
- .start_cons(SEQUENCE)
- .encode(static_cast<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 key->pkcs8_private_key();
}
IF_Scheme_Encoder(const IF_Scheme_PrivateKey* k) : key(k) {}
diff --git a/src/pubkey/if_algo/if_algo.h b/src/pubkey/if_algo/if_algo.h
index 17598dd52..254afc083 100644
--- a/src/pubkey/if_algo/if_algo.h
+++ b/src/pubkey/if_algo/if_algo.h
@@ -76,6 +76,8 @@ class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey,
*/
const BigInt& get_d() const { return d; }
+ MemoryVector<byte> pkcs8_private_key() const;
+
PKCS8_Encoder* pkcs8_encoder() const;
PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&);
protected:
diff --git a/src/pubkey/pk_codecs/pkcs8.cpp b/src/pubkey/pk_codecs/pkcs8.cpp
index d0954df39..099d52ffa 100644
--- a/src/pubkey/pk_codecs/pkcs8.cpp
+++ b/src/pubkey/pk_codecs/pkcs8.cpp
@@ -138,18 +138,14 @@ SecureVector<byte> PKCS8_decode(DataSource& source, const User_Interface& ui,
*/
void encode(const Private_Key& key, Pipe& pipe, X509_Encoding encoding)
{
- std::auto_ptr<PKCS8_Encoder> encoder(key.pkcs8_encoder());
- if(!encoder.get())
- throw Encoding_Error("PKCS8::encode: Key does not support encoding");
-
const u32bit PKCS8_VERSION = 0;
SecureVector<byte> contents =
DER_Encoder()
.start_cons(SEQUENCE)
.encode(PKCS8_VERSION)
- .encode(encoder->alg_id())
- .encode(encoder->key_bits(), OCTET_STRING)
+ .encode(key.algorithm_identifier())
+ .encode(key.pkcs8_private_key(), OCTET_STRING)
.end_cons()
.get_contents();
diff --git a/src/pubkey/pk_keys.h b/src/pubkey/pk_keys.h
index d1a841b9a..63302012c 100644
--- a/src/pubkey/pk_keys.h
+++ b/src/pubkey/pk_keys.h
@@ -90,6 +90,11 @@ class BOTAN_DLL Private_Key : public virtual Public_Key
{
public:
/**
+ * @return PKCS #8 private key encoding for this key object
+ */
+ virtual MemoryVector<byte> pkcs8_private_key() const = 0;
+
+ /**
* Get a PKCS#8 encoder that can be used to encode this key in
* PKCS#8 format.
* @return an PKCS#8 encoder for this key