137
* DER or PEM encode a PKCS #8 private key
137
* BER encode a PKCS #8 private key
139
void encode(const Private_Key& key, Pipe& pipe, X509_Encoding encoding)
139
SecureVector<byte> BER_encode(const Private_Key& key)
141
141
std::auto_ptr<PKCS8_Encoder> encoder(key.pkcs8_encoder());
142
142
if(!encoder.get())
145
145
const u32bit PKCS8_VERSION = 0;
147
SecureVector<byte> contents =
149
148
.start_cons(SEQUENCE)
150
149
.encode(PKCS8_VERSION)
151
150
.encode(encoder->alg_id())
152
151
.encode(encoder->key_bits(), OCTET_STRING)
157
pipe.write(PEM_Code::encode(contents, "PRIVATE KEY"));
159
pipe.write(contents);
163
* Encode and encrypt a PKCS #8 private key
165
void encrypt_key(const Private_Key& key,
167
RandomNumberGenerator& rng,
168
const std::string& pass, const std::string& pbe_algo,
169
X509_Encoding encoding)
157
* PEM encode a PKCS #8 private key
159
std::string PEM_encode(const Private_Key& key)
161
return PEM_Code::encode(PKCS8::BER_encode(key),
166
* Encrypt a PKCS #8 private key and return as BER
168
SecureVector<byte> BER_encode(const Private_Key& key,
169
RandomNumberGenerator& rng,
170
const std::string& pass,
171
const std::string& pbe_algo)
171
173
const std::string DEFAULT_PBE = "PBE-PKCS5v20(SHA-1,TripleDES/CBC)";
175
encode(key, raw_key, RAW_BER);
178
175
std::auto_ptr<PBE> pbe(get_pbe(((pbe_algo != "") ? pbe_algo : DEFAULT_PBE)));
180
177
pbe->new_params(rng);
183
180
AlgorithmIdentifier pbe_algid(pbe->get_oid(), pbe->encode_params());
185
182
Pipe key_encrytor(pbe.release());
186
key_encrytor.process_msg(raw_key);
183
key_encrytor.process_msg(PKCS8::BER_encode(key));
188
SecureVector<byte> enc_key =
190
186
.start_cons(SEQUENCE)
191
187
.encode(pbe_algid)
192
188
.encode(key_encrytor.read_all(), OCTET_STRING)
197
pipe.write(PEM_Code::encode(enc_key, "ENCRYPTED PRIVATE KEY"));
203
* PEM encode a PKCS #8 private key
205
std::string PEM_encode(const Private_Key& key)
209
encode(key, pem, PEM);
211
return pem.read_all_as_string();
223
202
return PEM_encode(key);
227
encrypt_key(key, pem, rng, pass, pbe_algo, PEM);
229
return pem.read_all_as_string();
204
return PEM_Code::encode(PKCS8::BER_encode(key, rng, pass, pbe_algo),
205
"ENCRYPTED PRIVATE KEY");
209
* DER or PEM encode a PKCS #8 private key
211
void encode(const Private_Key& key, Pipe& pipe, X509_Encoding encoding)
214
pipe.write(PKCS8::PEM_encode(key));
216
pipe.write(PKCS8::BER_encode(key));
220
* Encode and encrypt a PKCS #8 private key
222
void encrypt_key(const Private_Key& key,
224
RandomNumberGenerator& rng,
225
const std::string& pass,
226
const std::string& pbe_algo,
227
X509_Encoding encoding)
230
pipe.write(PKCS8::PEM_encode(key, rng, pass, pbe_algo));
232
pipe.write(PKCS8::BER_encode(key, rng, pass, pbe_algo));