~ubuntu-branches/ubuntu/oneiric/botan1.8/oneiric-updates

« back to all changes in this revision

Viewing changes to src/pubkey/pk_codecs/pkcs8.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2010-08-04 16:59:58 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100804165958-xdmg1izi7xk0t4b5
Tags: 1.8.9-1
* New upstream version
* Add git-buildpackage config

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
* PKCS #8
3
 
* (C) 1999-2008 Jack Lloyd
 
3
* (C) 1999-2010 Jack Lloyd
4
4
*
5
5
* Distributed under the terms of the Botan license
6
6
*/
134
134
}
135
135
 
136
136
/*
137
 
* DER or PEM encode a PKCS #8 private key
 
137
* BER encode a PKCS #8 private key
138
138
*/
139
 
void encode(const Private_Key& key, Pipe& pipe, X509_Encoding encoding)
 
139
SecureVector<byte> BER_encode(const Private_Key& key)
140
140
   {
141
141
   std::auto_ptr<PKCS8_Encoder> encoder(key.pkcs8_encoder());
142
142
   if(!encoder.get())
144
144
 
145
145
   const u32bit PKCS8_VERSION = 0;
146
146
 
147
 
   SecureVector<byte> contents =
148
 
      DER_Encoder()
 
147
   return DER_Encoder()
149
148
         .start_cons(SEQUENCE)
150
149
            .encode(PKCS8_VERSION)
151
150
            .encode(encoder->alg_id())
152
151
            .encode(encoder->key_bits(), OCTET_STRING)
153
152
         .end_cons()
154
153
      .get_contents();
155
 
 
156
 
   if(encoding == PEM)
157
 
      pipe.write(PEM_Code::encode(contents, "PRIVATE KEY"));
158
 
   else
159
 
      pipe.write(contents);
160
 
   }
161
 
 
162
 
/*
163
 
* Encode and encrypt a PKCS #8 private key
164
 
*/
165
 
void encrypt_key(const Private_Key& key,
166
 
                 Pipe& pipe,
167
 
                 RandomNumberGenerator& rng,
168
 
                 const std::string& pass, const std::string& pbe_algo,
169
 
                 X509_Encoding encoding)
 
154
   }
 
155
 
 
156
/*
 
157
* PEM encode a PKCS #8 private key
 
158
*/
 
159
std::string PEM_encode(const Private_Key& key)
 
160
   {
 
161
   return PEM_Code::encode(PKCS8::BER_encode(key),
 
162
                           "PRIVATE KEY");
 
163
   }
 
164
 
 
165
/*
 
166
* Encrypt a PKCS #8 private key and return as BER
 
167
*/
 
168
SecureVector<byte> BER_encode(const Private_Key& key,
 
169
                              RandomNumberGenerator& rng,
 
170
                              const std::string& pass,
 
171
                              const std::string& pbe_algo)
170
172
   {
171
173
   const std::string DEFAULT_PBE = "PBE-PKCS5v20(SHA-1,TripleDES/CBC)";
172
174
 
173
 
   Pipe raw_key;
174
 
   raw_key.start_msg();
175
 
   encode(key, raw_key, RAW_BER);
176
 
   raw_key.end_msg();
177
 
 
178
175
   std::auto_ptr<PBE> pbe(get_pbe(((pbe_algo != "") ? pbe_algo : DEFAULT_PBE)));
179
176
 
180
177
   pbe->new_params(rng);
183
180
   AlgorithmIdentifier pbe_algid(pbe->get_oid(), pbe->encode_params());
184
181
 
185
182
   Pipe key_encrytor(pbe.release());
186
 
   key_encrytor.process_msg(raw_key);
 
183
   key_encrytor.process_msg(PKCS8::BER_encode(key));
187
184
 
188
 
   SecureVector<byte> enc_key =
189
 
      DER_Encoder()
 
185
   return DER_Encoder()
190
186
         .start_cons(SEQUENCE)
191
187
            .encode(pbe_algid)
192
188
            .encode(key_encrytor.read_all(), OCTET_STRING)
193
189
         .end_cons()
194
190
      .get_contents();
195
 
 
196
 
   if(encoding == PEM)
197
 
      pipe.write(PEM_Code::encode(enc_key, "ENCRYPTED PRIVATE KEY"));
198
 
   else
199
 
      pipe.write(enc_key);
200
 
   }
201
 
 
202
 
/*
203
 
* PEM encode a PKCS #8 private key
204
 
*/
205
 
std::string PEM_encode(const Private_Key& key)
206
 
   {
207
 
   Pipe pem;
208
 
   pem.start_msg();
209
 
   encode(key, pem, PEM);
210
 
   pem.end_msg();
211
 
   return pem.read_all_as_string();
212
191
   }
213
192
 
214
193
/*
222
201
   if(pass == "")
223
202
      return PEM_encode(key);
224
203
 
225
 
   Pipe pem;
226
 
   pem.start_msg();
227
 
   encrypt_key(key, pem, rng, pass, pbe_algo, PEM);
228
 
   pem.end_msg();
229
 
   return pem.read_all_as_string();
 
204
   return PEM_Code::encode(PKCS8::BER_encode(key, rng, pass, pbe_algo),
 
205
                           "ENCRYPTED PRIVATE KEY");
 
206
   }
 
207
 
 
208
/*
 
209
* DER or PEM encode a PKCS #8 private key
 
210
*/
 
211
void encode(const Private_Key& key, Pipe& pipe, X509_Encoding encoding)
 
212
   {
 
213
   if(encoding == PEM)
 
214
      pipe.write(PKCS8::PEM_encode(key));
 
215
   else
 
216
      pipe.write(PKCS8::BER_encode(key));
 
217
   }
 
218
 
 
219
/*
 
220
* Encode and encrypt a PKCS #8 private key
 
221
*/
 
222
void encrypt_key(const Private_Key& key,
 
223
                 Pipe& pipe,
 
224
                 RandomNumberGenerator& rng,
 
225
                 const std::string& pass,
 
226
                 const std::string& pbe_algo,
 
227
                 X509_Encoding encoding)
 
228
   {
 
229
   if(encoding == PEM)
 
230
      pipe.write(PKCS8::PEM_encode(key, rng, pass, pbe_algo));
 
231
   else
 
232
      pipe.write(PKCS8::BER_encode(key, rng, pass, pbe_algo));
230
233
   }
231
234
 
232
235
/*