summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/pem
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
committercvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
commitdecf84ba5550c1656a7fdb51b5b81969590c3f03 (patch)
tree44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libcrypto/pem
parent7a8f138352aa4eb7b65ac4b1a5fe7630fbee1427 (diff)
downloadopenbsd-libressl-v2.1.5.tar.gz
openbsd-libressl-v2.1.5.tar.bz2
openbsd-libressl-v2.1.5.zip
This commit was manufactured by cvs2git to create branch 'OPENBSD_5_7'.libressl-v2.1.5
Diffstat (limited to 'src/lib/libcrypto/pem')
-rw-r--r--src/lib/libcrypto/pem/message16
-rw-r--r--src/lib/libcrypto/pem/pem.h616
-rw-r--r--src/lib/libcrypto/pem/pem2.h71
-rw-r--r--src/lib/libcrypto/pem/pem_all.c313
-rw-r--r--src/lib/libcrypto/pem/pem_err.c161
-rw-r--r--src/lib/libcrypto/pem/pem_info.c406
-rw-r--r--src/lib/libcrypto/pem/pem_lib.c871
-rw-r--r--src/lib/libcrypto/pem/pem_oth.c87
-rw-r--r--src/lib/libcrypto/pem/pem_pk8.c255
-rw-r--r--src/lib/libcrypto/pem/pem_pkey.c253
-rw-r--r--src/lib/libcrypto/pem/pem_seal.c190
-rw-r--r--src/lib/libcrypto/pem/pem_sign.c105
-rw-r--r--src/lib/libcrypto/pem/pem_x509.c67
-rw-r--r--src/lib/libcrypto/pem/pem_xaux.c69
-rw-r--r--src/lib/libcrypto/pem/pkcs7.lis22
-rw-r--r--src/lib/libcrypto/pem/pvkfmt.c933
16 files changed, 0 insertions, 4435 deletions
diff --git a/src/lib/libcrypto/pem/message b/src/lib/libcrypto/pem/message
deleted file mode 100644
index e8bf9d7592..0000000000
--- a/src/lib/libcrypto/pem/message
+++ /dev/null
@@ -1,16 +0,0 @@
1-----BEGIN PRIVACY-ENHANCED MESSAGE-----
2Proc-Type: 4,ENCRYPTED
3Proc-Type: 4,MIC-ONLY
4Proc-Type: 4,MIC-CLEAR
5Content-Domain: RFC822
6DEK-Info: DES-CBC,0123456789abcdef
7Originator-Certificate
8 xxxx
9Issuer-Certificate
10 xxxx
11MIC-Info: RSA-MD5,RSA,
12 xxxx
13
14
15-----END PRIVACY-ENHANCED MESSAGE-----
16
diff --git a/src/lib/libcrypto/pem/pem.h b/src/lib/libcrypto/pem/pem.h
deleted file mode 100644
index 3a0d8b3576..0000000000
--- a/src/lib/libcrypto/pem/pem.h
+++ /dev/null
@@ -1,616 +0,0 @@
1/* $OpenBSD: pem.h,v 1.16 2014/06/12 15:49:30 deraadt Exp $ */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_PEM_H
60#define HEADER_PEM_H
61
62#include <openssl/opensslconf.h>
63
64#ifndef OPENSSL_NO_BIO
65#include <openssl/bio.h>
66#endif
67#ifndef OPENSSL_NO_STACK
68#include <openssl/stack.h>
69#endif
70#include <openssl/evp.h>
71#include <openssl/x509.h>
72#include <openssl/pem2.h>
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78#define PEM_BUFSIZE 1024
79
80#define PEM_OBJ_UNDEF 0
81#define PEM_OBJ_X509 1
82#define PEM_OBJ_X509_REQ 2
83#define PEM_OBJ_CRL 3
84#define PEM_OBJ_SSL_SESSION 4
85#define PEM_OBJ_PRIV_KEY 10
86#define PEM_OBJ_PRIV_RSA 11
87#define PEM_OBJ_PRIV_DSA 12
88#define PEM_OBJ_PRIV_DH 13
89#define PEM_OBJ_PUB_RSA 14
90#define PEM_OBJ_PUB_DSA 15
91#define PEM_OBJ_PUB_DH 16
92#define PEM_OBJ_DHPARAMS 17
93#define PEM_OBJ_DSAPARAMS 18
94#define PEM_OBJ_PRIV_RSA_PUBLIC 19
95#define PEM_OBJ_PRIV_ECDSA 20
96#define PEM_OBJ_PUB_ECDSA 21
97#define PEM_OBJ_ECPARAMETERS 22
98
99#define PEM_ERROR 30
100#define PEM_DEK_DES_CBC 40
101#define PEM_DEK_IDEA_CBC 45
102#define PEM_DEK_DES_EDE 50
103#define PEM_DEK_DES_ECB 60
104#define PEM_DEK_RSA 70
105#define PEM_DEK_RSA_MD2 80
106#define PEM_DEK_RSA_MD5 90
107
108#define PEM_MD_MD2 NID_md2
109#define PEM_MD_MD5 NID_md5
110#define PEM_MD_SHA NID_sha
111#define PEM_MD_MD2_RSA NID_md2WithRSAEncryption
112#define PEM_MD_MD5_RSA NID_md5WithRSAEncryption
113#define PEM_MD_SHA_RSA NID_sha1WithRSAEncryption
114
115#define PEM_STRING_X509_OLD "X509 CERTIFICATE"
116#define PEM_STRING_X509 "CERTIFICATE"
117#define PEM_STRING_X509_PAIR "CERTIFICATE PAIR"
118#define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
119#define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
120#define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
121#define PEM_STRING_X509_CRL "X509 CRL"
122#define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY"
123#define PEM_STRING_PUBLIC "PUBLIC KEY"
124#define PEM_STRING_RSA "RSA PRIVATE KEY"
125#define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
126#define PEM_STRING_DSA "DSA PRIVATE KEY"
127#define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
128#define PEM_STRING_PKCS7 "PKCS7"
129#define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
130#define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
131#define PEM_STRING_PKCS8INF "PRIVATE KEY"
132#define PEM_STRING_DHPARAMS "DH PARAMETERS"
133#define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
134#define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
135#define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
136#define PEM_STRING_ECPARAMETERS "EC PARAMETERS"
137#define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
138#define PEM_STRING_PARAMETERS "PARAMETERS"
139#define PEM_STRING_CMS "CMS"
140
141 /* Note that this structure is initialised by PEM_SealInit and cleaned up
142 by PEM_SealFinal (at least for now) */
143typedef struct PEM_Encode_Seal_st {
144 EVP_ENCODE_CTX encode;
145 EVP_MD_CTX md;
146 EVP_CIPHER_CTX cipher;
147} PEM_ENCODE_SEAL_CTX;
148
149/* enc_type is one off */
150#define PEM_TYPE_ENCRYPTED 10
151#define PEM_TYPE_MIC_ONLY 20
152#define PEM_TYPE_MIC_CLEAR 30
153#define PEM_TYPE_CLEAR 40
154
155typedef struct pem_recip_st {
156 char *name;
157 X509_NAME *dn;
158
159 int cipher;
160 int key_enc;
161 /* char iv[8]; unused and wrong size */
162} PEM_USER;
163
164typedef struct pem_ctx_st {
165 int type; /* what type of object */
166
167 struct {
168 int version;
169 int mode;
170 } proc_type;
171
172 char *domain;
173
174 struct {
175 int cipher;
176 /* unused, and wrong size
177 unsigned char iv[8]; */
178 } DEK_info;
179
180 PEM_USER *originator;
181
182 int num_recipient;
183 PEM_USER **recipient;
184
185 /* XXX(ben): don#t think this is used!
186 STACK *x509_chain; / * certificate chain */
187 EVP_MD *md; /* signature type */
188
189 int md_enc; /* is the md encrypted or not? */
190 int md_len; /* length of md_data */
191 char *md_data; /* message digest, could be pkey encrypted */
192
193 EVP_CIPHER *dec; /* date encryption cipher */
194 int key_len; /* key length */
195 unsigned char *key; /* key */
196 /* unused, and wrong size
197 unsigned char iv[8]; */
198
199 int data_enc; /* is the data encrypted */
200 int data_len;
201 unsigned char *data;
202} PEM_CTX;
203
204/* These macros make the PEM_read/PEM_write functions easier to maintain and
205 * write. Now they are all implemented with either:
206 * IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...)
207 */
208
209
210#define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
211type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\
212{ \
213return PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str,fp,(void **)x,cb,u); \
214}
215
216#define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
217int PEM_write_##name(FILE *fp, type *x) \
218{ \
219return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,NULL,NULL,0,NULL,NULL); \
220}
221
222#define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
223int PEM_write_##name(FILE *fp, const type *x) \
224{ \
225return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,(void *)x,NULL,NULL,0,NULL,NULL); \
226}
227
228#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
229int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
230 unsigned char *kstr, int klen, pem_password_cb *cb, \
231 void *u) \
232 { \
233 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
234 }
235
236#define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
237int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
238 unsigned char *kstr, int klen, pem_password_cb *cb, \
239 void *u) \
240 { \
241 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
242 }
243
244
245#define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
246type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\
247{ \
248return PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str,bp,(void **)x,cb,u); \
249}
250
251#define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
252int PEM_write_bio_##name(BIO *bp, type *x) \
253{ \
254return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,NULL,NULL,0,NULL,NULL); \
255}
256
257#define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
258int PEM_write_bio_##name(BIO *bp, const type *x) \
259{ \
260return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,NULL,NULL,0,NULL,NULL); \
261}
262
263#define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
264int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
265 unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
266 { \
267 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,enc,kstr,klen,cb,u); \
268 }
269
270#define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
271int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
272 unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
273 { \
274 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,enc,kstr,klen,cb,u); \
275 }
276
277#define IMPLEMENT_PEM_write(name, type, str, asn1) \
278 IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
279 IMPLEMENT_PEM_write_fp(name, type, str, asn1)
280
281#define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
282 IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
283 IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
284
285#define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
286 IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
287 IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
288
289#define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
290 IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
291 IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
292
293#define IMPLEMENT_PEM_read(name, type, str, asn1) \
294 IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
295 IMPLEMENT_PEM_read_fp(name, type, str, asn1)
296
297#define IMPLEMENT_PEM_rw(name, type, str, asn1) \
298 IMPLEMENT_PEM_read(name, type, str, asn1) \
299 IMPLEMENT_PEM_write(name, type, str, asn1)
300
301#define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
302 IMPLEMENT_PEM_read(name, type, str, asn1) \
303 IMPLEMENT_PEM_write_const(name, type, str, asn1)
304
305#define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
306 IMPLEMENT_PEM_read(name, type, str, asn1) \
307 IMPLEMENT_PEM_write_cb(name, type, str, asn1)
308
309/* These are the same except they are for the declarations */
310
311
312#define DECLARE_PEM_read_fp(name, type) \
313 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
314
315#define DECLARE_PEM_write_fp(name, type) \
316 int PEM_write_##name(FILE *fp, type *x);
317
318#define DECLARE_PEM_write_fp_const(name, type) \
319 int PEM_write_##name(FILE *fp, const type *x);
320
321#define DECLARE_PEM_write_cb_fp(name, type) \
322 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
323 unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
324
325
326#ifndef OPENSSL_NO_BIO
327#define DECLARE_PEM_read_bio(name, type) \
328 type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u);
329
330#define DECLARE_PEM_write_bio(name, type) \
331 int PEM_write_bio_##name(BIO *bp, type *x);
332
333#define DECLARE_PEM_write_bio_const(name, type) \
334 int PEM_write_bio_##name(BIO *bp, const type *x);
335
336#define DECLARE_PEM_write_cb_bio(name, type) \
337 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
338 unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
339
340#else
341
342#define DECLARE_PEM_read_bio(name, type) /**/
343#define DECLARE_PEM_write_bio(name, type) /**/
344#define DECLARE_PEM_write_bio_const(name, type) /**/
345#define DECLARE_PEM_write_cb_bio(name, type) /**/
346
347#endif
348
349#define DECLARE_PEM_write(name, type) \
350 DECLARE_PEM_write_bio(name, type) \
351 DECLARE_PEM_write_fp(name, type)
352
353#define DECLARE_PEM_write_const(name, type) \
354 DECLARE_PEM_write_bio_const(name, type) \
355 DECLARE_PEM_write_fp_const(name, type)
356
357#define DECLARE_PEM_write_cb(name, type) \
358 DECLARE_PEM_write_cb_bio(name, type) \
359 DECLARE_PEM_write_cb_fp(name, type)
360
361#define DECLARE_PEM_read(name, type) \
362 DECLARE_PEM_read_bio(name, type) \
363 DECLARE_PEM_read_fp(name, type)
364
365#define DECLARE_PEM_rw(name, type) \
366 DECLARE_PEM_read(name, type) \
367 DECLARE_PEM_write(name, type)
368
369#define DECLARE_PEM_rw_const(name, type) \
370 DECLARE_PEM_read(name, type) \
371 DECLARE_PEM_write_const(name, type)
372
373#define DECLARE_PEM_rw_cb(name, type) \
374 DECLARE_PEM_read(name, type) \
375 DECLARE_PEM_write_cb(name, type)
376
377typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata);
378
379int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
380int PEM_do_header (EVP_CIPHER_INFO *cipher, unsigned char *data, long *len,
381 pem_password_cb *callback, void *u);
382
383#ifndef OPENSSL_NO_BIO
384int PEM_read_bio(BIO *bp, char **name, char **header,
385 unsigned char **data, long *len);
386int PEM_write_bio(BIO *bp, const char *name, char *hdr, unsigned char *data,
387 long len);
388int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
389 const char *name, BIO *bp, pem_password_cb *cb, void *u);
390void * PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp,
391 void **x, pem_password_cb *cb, void *u);
392int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x,
393 const EVP_CIPHER *enc, unsigned char *kstr, int klen,
394 pem_password_cb *cb, void *u);
395
396STACK_OF(X509_INFO) * PEM_X509_INFO_read_bio(BIO *bp,
397 STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u);
398int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
399 unsigned char *kstr, int klen, pem_password_cb *cd, void *u);
400#endif
401
402int PEM_read(FILE *fp, char **name, char **header,
403 unsigned char **data, long *len);
404int PEM_write(FILE *fp, char *name, char *hdr, unsigned char *data,
405 long len);
406void * PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
407 pem_password_cb *cb, void *u);
408int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
409 void *x, const EVP_CIPHER *enc, unsigned char *kstr,
410 int klen, pem_password_cb *callback, void *u);
411STACK_OF(X509_INFO) * PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
412 pem_password_cb *cb, void *u);
413
414int PEM_SealInit(PEM_ENCODE_SEAL_CTX *ctx, EVP_CIPHER *type,
415 EVP_MD *md_type, unsigned char **ek, int *ekl,
416 unsigned char *iv, EVP_PKEY **pubk, int npubk);
417void PEM_SealUpdate(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *out, int *outl,
418 unsigned char *in, int inl);
419int PEM_SealFinal(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *sig, int *sigl,
420 unsigned char *out, int *outl, EVP_PKEY *priv);
421
422void PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
423void PEM_SignUpdate(EVP_MD_CTX *ctx, unsigned char *d, unsigned int cnt);
424int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
425 unsigned int *siglen, EVP_PKEY *pkey);
426
427int PEM_def_callback(char *buf, int num, int w, void *key);
428void PEM_proc_type(char *buf, int type);
429void PEM_dek_info(char *buf, const char *type, int len, char *str);
430
431
432DECLARE_PEM_rw(X509, X509)
433
434DECLARE_PEM_rw(X509_AUX, X509)
435
436DECLARE_PEM_rw(X509_CERT_PAIR, X509_CERT_PAIR)
437
438DECLARE_PEM_rw(X509_REQ, X509_REQ)
439DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
440
441DECLARE_PEM_rw(X509_CRL, X509_CRL)
442
443DECLARE_PEM_rw(PKCS7, PKCS7)
444
445DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
446
447DECLARE_PEM_rw(PKCS8, X509_SIG)
448
449DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
450
451#ifndef OPENSSL_NO_RSA
452
453DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
454
455DECLARE_PEM_rw_const(RSAPublicKey, RSA)
456DECLARE_PEM_rw(RSA_PUBKEY, RSA)
457
458#endif
459
460#ifndef OPENSSL_NO_DSA
461
462DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
463
464DECLARE_PEM_rw(DSA_PUBKEY, DSA)
465
466DECLARE_PEM_rw_const(DSAparams, DSA)
467
468#endif
469
470#ifndef OPENSSL_NO_EC
471DECLARE_PEM_rw_const(ECPKParameters, EC_GROUP)
472DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
473DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
474#endif
475
476#ifndef OPENSSL_NO_DH
477
478DECLARE_PEM_rw_const(DHparams, DH)
479
480#endif
481
482DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
483
484DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
485
486int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
487 char *kstr, int klen,
488 pem_password_cb *cb, void *u);
489int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *, const EVP_CIPHER *,
490 char *, int, pem_password_cb *, void *);
491int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
492 char *kstr, int klen,
493 pem_password_cb *cb, void *u);
494int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
495 char *kstr, int klen,
496 pem_password_cb *cb, void *u);
497EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
498 void *u);
499
500int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
501 char *kstr, int klen,
502 pem_password_cb *cb, void *u);
503int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
504 char *kstr, int klen,
505 pem_password_cb *cb, void *u);
506int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
507 char *kstr, int klen,
508 pem_password_cb *cb, void *u);
509
510EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
511 void *u);
512
513int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
514 char *kstr, int klen, pem_password_cb *cd, void *u);
515
516EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
517int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x);
518
519
520EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length);
521EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length);
522EVP_PKEY *b2i_PrivateKey_bio(BIO *in);
523EVP_PKEY *b2i_PublicKey_bio(BIO *in);
524int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk);
525int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk);
526#ifndef OPENSSL_NO_RC4
527EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u);
528int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel, pem_password_cb *cb,
529 void *u);
530#endif
531
532
533/* BEGIN ERROR CODES */
534/* The following lines are auto generated by the script mkerr.pl. Any changes
535 * made after this point may be overwritten when the script is next run.
536 */
537void ERR_load_PEM_strings(void);
538
539/* Error codes for the PEM functions. */
540
541/* Function codes. */
542#define PEM_F_B2I_DSS 127
543#define PEM_F_B2I_PVK_BIO 128
544#define PEM_F_B2I_RSA 129
545#define PEM_F_CHECK_BITLEN_DSA 130
546#define PEM_F_CHECK_BITLEN_RSA 131
547#define PEM_F_D2I_PKCS8PRIVATEKEY_BIO 120
548#define PEM_F_D2I_PKCS8PRIVATEKEY_FP 121
549#define PEM_F_DO_B2I 132
550#define PEM_F_DO_B2I_BIO 133
551#define PEM_F_DO_BLOB_HEADER 134
552#define PEM_F_DO_PK8PKEY 126
553#define PEM_F_DO_PK8PKEY_FP 125
554#define PEM_F_DO_PVK_BODY 135
555#define PEM_F_DO_PVK_HEADER 136
556#define PEM_F_I2B_PVK 137
557#define PEM_F_I2B_PVK_BIO 138
558#define PEM_F_LOAD_IV 101
559#define PEM_F_PEM_ASN1_READ 102
560#define PEM_F_PEM_ASN1_READ_BIO 103
561#define PEM_F_PEM_ASN1_WRITE 104
562#define PEM_F_PEM_ASN1_WRITE_BIO 105
563#define PEM_F_PEM_DEF_CALLBACK 100
564#define PEM_F_PEM_DO_HEADER 106
565#define PEM_F_PEM_F_PEM_WRITE_PKCS8PRIVATEKEY 118
566#define PEM_F_PEM_GET_EVP_CIPHER_INFO 107
567#define PEM_F_PEM_PK8PKEY 119
568#define PEM_F_PEM_READ 108
569#define PEM_F_PEM_READ_BIO 109
570#define PEM_F_PEM_READ_BIO_PARAMETERS 140
571#define PEM_F_PEM_READ_BIO_PRIVATEKEY 123
572#define PEM_F_PEM_READ_PRIVATEKEY 124
573#define PEM_F_PEM_SEALFINAL 110
574#define PEM_F_PEM_SEALINIT 111
575#define PEM_F_PEM_SIGNFINAL 112
576#define PEM_F_PEM_WRITE 113
577#define PEM_F_PEM_WRITE_BIO 114
578#define PEM_F_PEM_WRITE_PRIVATEKEY 139
579#define PEM_F_PEM_X509_INFO_READ 115
580#define PEM_F_PEM_X509_INFO_READ_BIO 116
581#define PEM_F_PEM_X509_INFO_WRITE_BIO 117
582
583/* Reason codes. */
584#define PEM_R_BAD_BASE64_DECODE 100
585#define PEM_R_BAD_DECRYPT 101
586#define PEM_R_BAD_END_LINE 102
587#define PEM_R_BAD_IV_CHARS 103
588#define PEM_R_BAD_MAGIC_NUMBER 116
589#define PEM_R_BAD_PASSWORD_READ 104
590#define PEM_R_BAD_VERSION_NUMBER 117
591#define PEM_R_BIO_WRITE_FAILURE 118
592#define PEM_R_CIPHER_IS_NULL 127
593#define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 115
594#define PEM_R_EXPECTING_PRIVATE_KEY_BLOB 119
595#define PEM_R_EXPECTING_PUBLIC_KEY_BLOB 120
596#define PEM_R_INCONSISTENT_HEADER 121
597#define PEM_R_KEYBLOB_HEADER_PARSE_ERROR 122
598#define PEM_R_KEYBLOB_TOO_SHORT 123
599#define PEM_R_NOT_DEK_INFO 105
600#define PEM_R_NOT_ENCRYPTED 106
601#define PEM_R_NOT_PROC_TYPE 107
602#define PEM_R_NO_START_LINE 108
603#define PEM_R_PROBLEMS_GETTING_PASSWORD 109
604#define PEM_R_PUBLIC_KEY_NO_RSA 110
605#define PEM_R_PVK_DATA_TOO_SHORT 124
606#define PEM_R_PVK_TOO_SHORT 125
607#define PEM_R_READ_KEY 111
608#define PEM_R_SHORT_HEADER 112
609#define PEM_R_UNSUPPORTED_CIPHER 113
610#define PEM_R_UNSUPPORTED_ENCRYPTION 114
611#define PEM_R_UNSUPPORTED_KEY_COMPONENTS 126
612
613#ifdef __cplusplus
614}
615#endif
616#endif
diff --git a/src/lib/libcrypto/pem/pem2.h b/src/lib/libcrypto/pem/pem2.h
deleted file mode 100644
index 19525b4a45..0000000000
--- a/src/lib/libcrypto/pem/pem2.h
+++ /dev/null
@@ -1,71 +0,0 @@
1/* $OpenBSD: pem2.h,v 1.5 2014/06/12 15:49:30 deraadt Exp $ */
2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/*
57 * This header only exists to break a circular dependency between pem and err
58 * Ben 30 Jan 1999.
59 */
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
65#ifndef HEADER_PEM_H
66void ERR_load_PEM_strings(void);
67#endif
68
69#ifdef __cplusplus
70}
71#endif
diff --git a/src/lib/libcrypto/pem/pem_all.c b/src/lib/libcrypto/pem/pem_all.c
deleted file mode 100644
index 7df917b01c..0000000000
--- a/src/lib/libcrypto/pem/pem_all.c
+++ /dev/null
@@ -1,313 +0,0 @@
1/* $OpenBSD: pem_all.c,v 1.15 2014/07/11 08:44:49 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112#include <stdio.h>
113
114#include <openssl/opensslconf.h>
115
116#include <openssl/bio.h>
117#include <openssl/evp.h>
118#include <openssl/pem.h>
119#include <openssl/pkcs7.h>
120#include <openssl/x509.h>
121
122#ifndef OPENSSL_NO_DH
123#include <openssl/dh.h>
124#endif
125#ifndef OPENSSL_NO_DSA
126#include <openssl/dsa.h>
127#endif
128#ifndef OPENSSL_NO_RSA
129#include <openssl/rsa.h>
130#endif
131
132#ifndef OPENSSL_NO_RSA
133static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
134#endif
135#ifndef OPENSSL_NO_DSA
136static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
137#endif
138
139#ifndef OPENSSL_NO_EC
140static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
141#endif
142
143IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
144
145IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
146
147IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
148
149IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
150
151IMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
152 PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE)
153
154
155#ifndef OPENSSL_NO_RSA
156
157/* We treat RSA or DSA private keys as a special case.
158 *
159 * For private keys we read in an EVP_PKEY structure with
160 * PEM_read_bio_PrivateKey() and extract the relevant private
161 * key: this means can handle "traditional" and PKCS#8 formats
162 * transparently.
163 */
164
165static RSA *
166pkey_get_rsa(EVP_PKEY *key, RSA **rsa)
167{
168 RSA *rtmp;
169
170 if (!key)
171 return NULL;
172 rtmp = EVP_PKEY_get1_RSA(key);
173 EVP_PKEY_free(key);
174 if (!rtmp)
175 return NULL;
176 if (rsa) {
177 RSA_free(*rsa);
178 *rsa = rtmp;
179 }
180 return rtmp;
181}
182
183RSA *
184PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb, void *u)
185{
186 EVP_PKEY *pktmp;
187
188 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
189 return pkey_get_rsa(pktmp, rsa);
190}
191
192
193RSA *
194PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
195{
196 EVP_PKEY *pktmp;
197
198 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
199 return pkey_get_rsa(pktmp, rsa);
200}
201
202
203IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
204
205IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
206IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
207
208#endif
209
210#ifndef OPENSSL_NO_DSA
211
212static DSA *
213pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
214{
215 DSA *dtmp;
216
217 if (!key)
218 return NULL;
219 dtmp = EVP_PKEY_get1_DSA(key);
220 EVP_PKEY_free(key);
221 if (!dtmp)
222 return NULL;
223 if (dsa) {
224 DSA_free(*dsa);
225 *dsa = dtmp;
226 }
227 return dtmp;
228}
229
230DSA *
231PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb, void *u)
232{
233 EVP_PKEY *pktmp;
234
235 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
236 return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
237}
238
239IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
240
241IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
242
243
244DSA *
245PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
246{
247 EVP_PKEY *pktmp;
248
249 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
250 return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
251}
252
253
254IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
255
256#endif
257
258
259#ifndef OPENSSL_NO_EC
260static EC_KEY *
261pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
262{
263 EC_KEY *dtmp;
264
265 if (!key)
266 return NULL;
267 dtmp = EVP_PKEY_get1_EC_KEY(key);
268 EVP_PKEY_free(key);
269 if (!dtmp)
270 return NULL;
271 if (eckey) {
272 EC_KEY_free(*eckey);
273 *eckey = dtmp;
274 }
275 return dtmp;
276}
277
278EC_KEY *
279PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb, void *u)
280{
281 EVP_PKEY *pktmp;
282 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
283 return pkey_get_eckey(pktmp, key); /* will free pktmp */
284}
285
286IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
287 ECPKParameters)
288
289IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
290 ECPrivateKey)
291
292IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
293
294
295EC_KEY *
296PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb, void *u)
297{
298 EVP_PKEY *pktmp;
299
300 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
301 return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
302}
303
304
305#endif
306
307#ifndef OPENSSL_NO_DH
308
309IMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
310
311#endif
312
313IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
diff --git a/src/lib/libcrypto/pem/pem_err.c b/src/lib/libcrypto/pem/pem_err.c
deleted file mode 100644
index c3d0fa3576..0000000000
--- a/src/lib/libcrypto/pem/pem_err.c
+++ /dev/null
@@ -1,161 +0,0 @@
1/* $OpenBSD: pem_err.c,v 1.11 2014/07/10 22:45:57 jsing Exp $ */
2/* ====================================================================
3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/* NOTE: this file was auto generated by the mkerr.pl script: any changes
57 * made to it will be overwritten when the script next updates this file,
58 * only reason strings will be preserved.
59 */
60
61#include <stdio.h>
62
63#include <openssl/opensslconf.h>
64
65#include <openssl/err.h>
66#include <openssl/pem.h>
67
68/* BEGIN ERROR CODES */
69#ifndef OPENSSL_NO_ERR
70
71#define ERR_FUNC(func) ERR_PACK(ERR_LIB_PEM,func,0)
72#define ERR_REASON(reason) ERR_PACK(ERR_LIB_PEM,0,reason)
73
74static ERR_STRING_DATA PEM_str_functs[] = {
75 {ERR_FUNC(PEM_F_B2I_DSS), "B2I_DSS"},
76 {ERR_FUNC(PEM_F_B2I_PVK_BIO), "b2i_PVK_bio"},
77 {ERR_FUNC(PEM_F_B2I_RSA), "B2I_RSA"},
78 {ERR_FUNC(PEM_F_CHECK_BITLEN_DSA), "CHECK_BITLEN_DSA"},
79 {ERR_FUNC(PEM_F_CHECK_BITLEN_RSA), "CHECK_BITLEN_RSA"},
80 {ERR_FUNC(PEM_F_D2I_PKCS8PRIVATEKEY_BIO), "d2i_PKCS8PrivateKey_bio"},
81 {ERR_FUNC(PEM_F_D2I_PKCS8PRIVATEKEY_FP), "d2i_PKCS8PrivateKey_fp"},
82 {ERR_FUNC(PEM_F_DO_B2I), "DO_B2I"},
83 {ERR_FUNC(PEM_F_DO_B2I_BIO), "DO_B2I_BIO"},
84 {ERR_FUNC(PEM_F_DO_BLOB_HEADER), "DO_BLOB_HEADER"},
85 {ERR_FUNC(PEM_F_DO_PK8PKEY), "DO_PK8PKEY"},
86 {ERR_FUNC(PEM_F_DO_PK8PKEY_FP), "DO_PK8PKEY_FP"},
87 {ERR_FUNC(PEM_F_DO_PVK_BODY), "DO_PVK_BODY"},
88 {ERR_FUNC(PEM_F_DO_PVK_HEADER), "DO_PVK_HEADER"},
89 {ERR_FUNC(PEM_F_I2B_PVK), "I2B_PVK"},
90 {ERR_FUNC(PEM_F_I2B_PVK_BIO), "i2b_PVK_bio"},
91 {ERR_FUNC(PEM_F_LOAD_IV), "LOAD_IV"},
92 {ERR_FUNC(PEM_F_PEM_ASN1_READ), "PEM_ASN1_read"},
93 {ERR_FUNC(PEM_F_PEM_ASN1_READ_BIO), "PEM_ASN1_read_bio"},
94 {ERR_FUNC(PEM_F_PEM_ASN1_WRITE), "PEM_ASN1_write"},
95 {ERR_FUNC(PEM_F_PEM_ASN1_WRITE_BIO), "PEM_ASN1_write_bio"},
96 {ERR_FUNC(PEM_F_PEM_DEF_CALLBACK), "PEM_def_callback"},
97 {ERR_FUNC(PEM_F_PEM_DO_HEADER), "PEM_do_header"},
98 {ERR_FUNC(PEM_F_PEM_F_PEM_WRITE_PKCS8PRIVATEKEY), "PEM_F_PEM_WRITE_PKCS8PRIVATEKEY"},
99 {ERR_FUNC(PEM_F_PEM_GET_EVP_CIPHER_INFO), "PEM_get_EVP_CIPHER_INFO"},
100 {ERR_FUNC(PEM_F_PEM_PK8PKEY), "PEM_PK8PKEY"},
101 {ERR_FUNC(PEM_F_PEM_READ), "PEM_read"},
102 {ERR_FUNC(PEM_F_PEM_READ_BIO), "PEM_read_bio"},
103 {ERR_FUNC(PEM_F_PEM_READ_BIO_PARAMETERS), "PEM_read_bio_Parameters"},
104 {ERR_FUNC(PEM_F_PEM_READ_BIO_PRIVATEKEY), "PEM_READ_BIO_PRIVATEKEY"},
105 {ERR_FUNC(PEM_F_PEM_READ_PRIVATEKEY), "PEM_READ_PRIVATEKEY"},
106 {ERR_FUNC(PEM_F_PEM_SEALFINAL), "PEM_SealFinal"},
107 {ERR_FUNC(PEM_F_PEM_SEALINIT), "PEM_SealInit"},
108 {ERR_FUNC(PEM_F_PEM_SIGNFINAL), "PEM_SignFinal"},
109 {ERR_FUNC(PEM_F_PEM_WRITE), "PEM_write"},
110 {ERR_FUNC(PEM_F_PEM_WRITE_BIO), "PEM_write_bio"},
111 {ERR_FUNC(PEM_F_PEM_WRITE_PRIVATEKEY), "PEM_WRITE_PRIVATEKEY"},
112 {ERR_FUNC(PEM_F_PEM_X509_INFO_READ), "PEM_X509_INFO_read"},
113 {ERR_FUNC(PEM_F_PEM_X509_INFO_READ_BIO), "PEM_X509_INFO_read_bio"},
114 {ERR_FUNC(PEM_F_PEM_X509_INFO_WRITE_BIO), "PEM_X509_INFO_write_bio"},
115 {0, NULL}
116};
117
118static ERR_STRING_DATA PEM_str_reasons[] = {
119 {ERR_REASON(PEM_R_BAD_BASE64_DECODE) , "bad base64 decode"},
120 {ERR_REASON(PEM_R_BAD_DECRYPT) , "bad decrypt"},
121 {ERR_REASON(PEM_R_BAD_END_LINE) , "bad end line"},
122 {ERR_REASON(PEM_R_BAD_IV_CHARS) , "bad iv chars"},
123 {ERR_REASON(PEM_R_BAD_MAGIC_NUMBER) , "bad magic number"},
124 {ERR_REASON(PEM_R_BAD_PASSWORD_READ) , "bad password read"},
125 {ERR_REASON(PEM_R_BAD_VERSION_NUMBER) , "bad version number"},
126 {ERR_REASON(PEM_R_BIO_WRITE_FAILURE) , "bio write failure"},
127 {ERR_REASON(PEM_R_CIPHER_IS_NULL) , "cipher is null"},
128 {ERR_REASON(PEM_R_ERROR_CONVERTING_PRIVATE_KEY), "error converting private key"},
129 {ERR_REASON(PEM_R_EXPECTING_PRIVATE_KEY_BLOB), "expecting private key blob"},
130 {ERR_REASON(PEM_R_EXPECTING_PUBLIC_KEY_BLOB), "expecting public key blob"},
131 {ERR_REASON(PEM_R_INCONSISTENT_HEADER) , "inconsistent header"},
132 {ERR_REASON(PEM_R_KEYBLOB_HEADER_PARSE_ERROR), "keyblob header parse error"},
133 {ERR_REASON(PEM_R_KEYBLOB_TOO_SHORT) , "keyblob too short"},
134 {ERR_REASON(PEM_R_NOT_DEK_INFO) , "not dek info"},
135 {ERR_REASON(PEM_R_NOT_ENCRYPTED) , "not encrypted"},
136 {ERR_REASON(PEM_R_NOT_PROC_TYPE) , "not proc type"},
137 {ERR_REASON(PEM_R_NO_START_LINE) , "no start line"},
138 {ERR_REASON(PEM_R_PROBLEMS_GETTING_PASSWORD), "problems getting password"},
139 {ERR_REASON(PEM_R_PUBLIC_KEY_NO_RSA) , "public key no rsa"},
140 {ERR_REASON(PEM_R_PVK_DATA_TOO_SHORT) , "pvk data too short"},
141 {ERR_REASON(PEM_R_PVK_TOO_SHORT) , "pvk too short"},
142 {ERR_REASON(PEM_R_READ_KEY) , "read key"},
143 {ERR_REASON(PEM_R_SHORT_HEADER) , "short header"},
144 {ERR_REASON(PEM_R_UNSUPPORTED_CIPHER) , "unsupported cipher"},
145 {ERR_REASON(PEM_R_UNSUPPORTED_ENCRYPTION), "unsupported encryption"},
146 {ERR_REASON(PEM_R_UNSUPPORTED_KEY_COMPONENTS), "unsupported key components"},
147 {0, NULL}
148};
149
150#endif
151
152void
153ERR_load_PEM_strings(void)
154{
155#ifndef OPENSSL_NO_ERR
156 if (ERR_func_error_string(PEM_str_functs[0].error) == NULL) {
157 ERR_load_strings(0, PEM_str_functs);
158 ERR_load_strings(0, PEM_str_reasons);
159 }
160#endif
161}
diff --git a/src/lib/libcrypto/pem/pem_info.c b/src/lib/libcrypto/pem/pem_info.c
deleted file mode 100644
index 6fe72ce742..0000000000
--- a/src/lib/libcrypto/pem/pem_info.c
+++ /dev/null
@@ -1,406 +0,0 @@
1/* $OpenBSD: pem_info.c,v 1.20 2015/02/10 09:52:35 miod Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/opensslconf.h>
63
64#include <openssl/buffer.h>
65#include <openssl/err.h>
66#include <openssl/evp.h>
67#include <openssl/objects.h>
68#include <openssl/pem.h>
69#include <openssl/x509.h>
70
71#ifndef OPENSSL_NO_DSA
72#include <openssl/dsa.h>
73#endif
74#ifndef OPENSSL_NO_RSA
75#include <openssl/rsa.h>
76#endif
77
78STACK_OF(X509_INFO) *
79PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb,
80 void *u)
81{
82 BIO *b;
83 STACK_OF(X509_INFO) *ret;
84
85 if ((b = BIO_new(BIO_s_file())) == NULL) {
86 PEMerr(PEM_F_PEM_X509_INFO_READ, ERR_R_BUF_LIB);
87 return (0);
88 }
89 BIO_set_fp(b, fp, BIO_NOCLOSE);
90 ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
91 BIO_free(b);
92 return (ret);
93}
94
95STACK_OF(X509_INFO) *
96PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb,
97 void *u)
98{
99 X509_INFO *xi = NULL;
100 char *name = NULL, *header = NULL;
101 void *pp;
102 unsigned char *data = NULL;
103 const unsigned char *p;
104 long len, error = 0;
105 int ok = 0;
106 STACK_OF(X509_INFO) *ret = NULL;
107 unsigned int i, raw, ptype;
108 d2i_of_void *d2i = 0;
109
110 if (sk == NULL) {
111 if ((ret = sk_X509_INFO_new_null()) == NULL) {
112 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,
113 ERR_R_MALLOC_FAILURE);
114 return 0;
115 }
116 } else
117 ret = sk;
118
119 if ((xi = X509_INFO_new()) == NULL)
120 goto err;
121 for (;;) {
122 raw = 0;
123 ptype = 0;
124 i = PEM_read_bio(bp, &name, &header, &data, &len);
125 if (i == 0) {
126 error = ERR_GET_REASON(ERR_peek_last_error());
127 if (error == PEM_R_NO_START_LINE) {
128 ERR_clear_error();
129 break;
130 }
131 goto err;
132 }
133start:
134 if ((strcmp(name, PEM_STRING_X509) == 0) ||
135 (strcmp(name, PEM_STRING_X509_OLD) == 0)) {
136 d2i = (D2I_OF(void))d2i_X509;
137 if (xi->x509 != NULL) {
138 if (!sk_X509_INFO_push(ret, xi))
139 goto err;
140 if ((xi = X509_INFO_new()) == NULL)
141 goto err;
142 goto start;
143 }
144 pp = &(xi->x509);
145 } else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) {
146 d2i = (D2I_OF(void))d2i_X509_AUX;
147 if (xi->x509 != NULL) {
148 if (!sk_X509_INFO_push(ret, xi))
149 goto err;
150 if ((xi = X509_INFO_new()) == NULL)
151 goto err;
152 goto start;
153 }
154 pp = &(xi->x509);
155 } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
156 d2i = (D2I_OF(void))d2i_X509_CRL;
157 if (xi->crl != NULL) {
158 if (!sk_X509_INFO_push(ret, xi))
159 goto err;
160 if ((xi = X509_INFO_new()) == NULL)
161 goto err;
162 goto start;
163 }
164 pp = &(xi->crl);
165 } else
166#ifndef OPENSSL_NO_RSA
167 if (strcmp(name, PEM_STRING_RSA) == 0) {
168 d2i = (D2I_OF(void))d2i_RSAPrivateKey;
169 if (xi->x_pkey != NULL) {
170 if (!sk_X509_INFO_push(ret, xi))
171 goto err;
172 if ((xi = X509_INFO_new()) == NULL)
173 goto err;
174 goto start;
175 }
176
177 xi->enc_data = NULL;
178 xi->enc_len = 0;
179
180 xi->x_pkey = X509_PKEY_new();
181 if (xi->x_pkey == NULL)
182 goto err;
183 ptype = EVP_PKEY_RSA;
184 pp = &xi->x_pkey->dec_pkey;
185 if (strlen(header) > 10) /* assume encrypted */
186 raw = 1;
187 } else
188#endif
189#ifndef OPENSSL_NO_DSA
190 if (strcmp(name, PEM_STRING_DSA) == 0) {
191 d2i = (D2I_OF(void))d2i_DSAPrivateKey;
192 if (xi->x_pkey != NULL) {
193 if (!sk_X509_INFO_push(ret, xi))
194 goto err;
195 if ((xi = X509_INFO_new()) == NULL)
196 goto err;
197 goto start;
198 }
199
200 xi->enc_data = NULL;
201 xi->enc_len = 0;
202
203 xi->x_pkey = X509_PKEY_new();
204 if (xi->x_pkey == NULL)
205 goto err;
206 ptype = EVP_PKEY_DSA;
207 pp = &xi->x_pkey->dec_pkey;
208 if (strlen(header) > 10) /* assume encrypted */
209 raw = 1;
210 } else
211#endif
212#ifndef OPENSSL_NO_EC
213 if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
214 d2i = (D2I_OF(void))d2i_ECPrivateKey;
215 if (xi->x_pkey != NULL) {
216 if (!sk_X509_INFO_push(ret, xi))
217 goto err;
218 if ((xi = X509_INFO_new()) == NULL)
219 goto err;
220 goto start;
221 }
222
223 xi->enc_data = NULL;
224 xi->enc_len = 0;
225
226 xi->x_pkey = X509_PKEY_new();
227 if (xi->x_pkey == NULL)
228 goto err;
229 ptype = EVP_PKEY_EC;
230 pp = &xi->x_pkey->dec_pkey;
231 if (strlen(header) > 10) /* assume encrypted */
232 raw = 1;
233 } else
234#endif
235 {
236 d2i = NULL;
237 pp = NULL;
238 }
239
240 if (d2i != NULL) {
241 if (!raw) {
242 EVP_CIPHER_INFO cipher;
243
244 if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
245 goto err;
246 if (!PEM_do_header(&cipher, data, &len, cb, u))
247 goto err;
248 p = data;
249 if (ptype) {
250 if (!d2i_PrivateKey(ptype, pp, &p,
251 len)) {
252 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,
253 ERR_R_ASN1_LIB);
254 goto err;
255 }
256 } else if (d2i(pp, &p, len) == NULL) {
257 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,
258 ERR_R_ASN1_LIB);
259 goto err;
260 }
261 } else { /* encrypted RSA data */
262 if (!PEM_get_EVP_CIPHER_INFO(header,
263 &xi->enc_cipher))
264 goto err;
265 xi->enc_data = (char *)data;
266 xi->enc_len = (int)len;
267 data = NULL;
268 }
269 } else {
270 /* unknown */
271 }
272 free(name);
273 free(header);
274 free(data);
275 name = NULL;
276 header = NULL;
277 data = NULL;
278 }
279
280 /* if the last one hasn't been pushed yet and there is anything
281 * in it then add it to the stack ...
282 */
283 if ((xi->x509 != NULL) || (xi->crl != NULL) ||
284 (xi->x_pkey != NULL) || (xi->enc_data != NULL)) {
285 if (!sk_X509_INFO_push(ret, xi))
286 goto err;
287 xi = NULL;
288 }
289 ok = 1;
290
291err:
292 if (xi != NULL)
293 X509_INFO_free(xi);
294 if (!ok) {
295 for (i = 0; ((int)i) < sk_X509_INFO_num(ret); i++) {
296 xi = sk_X509_INFO_value(ret, i);
297 X509_INFO_free(xi);
298 }
299 if (ret != sk)
300 sk_X509_INFO_free(ret);
301 ret = NULL;
302 }
303
304 free(name);
305 free(header);
306 free(data);
307 return (ret);
308}
309
310
311/* A TJH addition */
312int
313PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
314 unsigned char *kstr, int klen, pem_password_cb *cb, void *u)
315{
316 EVP_CIPHER_CTX ctx;
317 int i, ret = 0;
318 unsigned char *data = NULL;
319 const char *objstr = NULL;
320 char buf[PEM_BUFSIZE];
321 unsigned char *iv = NULL;
322
323 if (enc != NULL) {
324 objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
325 if (objstr == NULL) {
326 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,
327 PEM_R_UNSUPPORTED_CIPHER);
328 goto err;
329 }
330 }
331
332 /* now for the fun part ... if we have a private key then
333 * we have to be able to handle a not-yet-decrypted key
334 * being written out correctly ... if it is decrypted or
335 * it is non-encrypted then we use the base code
336 */
337 if (xi->x_pkey != NULL) {
338 if ((xi->enc_data != NULL) && (xi->enc_len > 0) ) {
339 if (enc == NULL) {
340 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,
341 PEM_R_CIPHER_IS_NULL);
342 goto err;
343 }
344
345 /* copy from weirdo names into more normal things */
346 iv = xi->enc_cipher.iv;
347 data = (unsigned char *)xi->enc_data;
348 i = xi->enc_len;
349
350 /* we take the encryption data from the
351 * internal stuff rather than what the
352 * user has passed us ... as we have to
353 * match exactly for some strange reason
354 */
355 objstr = OBJ_nid2sn(
356 EVP_CIPHER_nid(xi->enc_cipher.cipher));
357 if (objstr == NULL) {
358 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,
359 PEM_R_UNSUPPORTED_CIPHER);
360 goto err;
361 }
362
363 /* create the right magic header stuff */
364 if (strlen(objstr) + 23 + 2 * enc->iv_len + 13 >
365 sizeof buf) {
366 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,
367 ASN1_R_BUFFER_TOO_SMALL);
368 goto err;
369 }
370 buf[0] = '\0';
371 PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
372 PEM_dek_info(buf, objstr, enc->iv_len, (char *)iv);
373
374 /* use the normal code to write things out */
375 i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
376 if (i <= 0)
377 goto err;
378 } else {
379 /* Add DSA/DH */
380#ifndef OPENSSL_NO_RSA
381 /* normal optionally encrypted stuff */
382 if (PEM_write_bio_RSAPrivateKey(bp,
383 xi->x_pkey->dec_pkey->pkey.rsa,
384 enc, kstr, klen, cb, u) <= 0)
385 goto err;
386#endif
387 }
388 }
389
390 /* if we have a certificate then write it out now */
391 if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
392 goto err;
393
394 /* we are ignoring anything else that is loaded into the X509_INFO
395 * structure for the moment ... as I don't need it so I'm not
396 * coding it here and Eric can do it when this makes it into the
397 * base library --tjh
398 */
399
400 ret = 1;
401
402err:
403 OPENSSL_cleanse((char *)&ctx, sizeof(ctx));
404 OPENSSL_cleanse(buf, PEM_BUFSIZE);
405 return (ret);
406}
diff --git a/src/lib/libcrypto/pem/pem_lib.c b/src/lib/libcrypto/pem/pem_lib.c
deleted file mode 100644
index 48768a4467..0000000000
--- a/src/lib/libcrypto/pem/pem_lib.c
+++ /dev/null
@@ -1,871 +0,0 @@
1/* $OpenBSD: pem_lib.c,v 1.39 2015/02/11 04:05:14 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <ctype.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63
64#include <openssl/opensslconf.h>
65
66#include <openssl/buffer.h>
67#include <openssl/err.h>
68#include <openssl/evp.h>
69#include <openssl/objects.h>
70#include <openssl/pem.h>
71#include <openssl/pkcs12.h>
72#include <openssl/x509.h>
73
74#ifndef OPENSSL_NO_DES
75#include <openssl/des.h>
76#endif
77#ifndef OPENSSL_NO_ENGINE
78#include <openssl/engine.h>
79#endif
80
81#include "asn1_locl.h"
82
83#define MIN_LENGTH 4
84
85static int load_iv(char **fromp, unsigned char *to, int num);
86static int check_pem(const char *nm, const char *name);
87int pem_check_suffix(const char *pem_str, const char *suffix);
88
89/* XXX LSSL ABI XXX return value and `num' ought to be size_t */
90int
91PEM_def_callback(char *buf, int num, int w, void *key)
92{
93 size_t l;
94 int i;
95 const char *prompt;
96
97 if (key) {
98 l = strlen(key);
99 if (num < 0)
100 return -1;
101 if (l > (size_t)num)
102 l = (size_t)num;
103 memcpy(buf, key, l);
104 return (int)l;
105 }
106
107 prompt = EVP_get_pw_prompt();
108 if (prompt == NULL)
109 prompt = "Enter PEM pass phrase:";
110
111 for (;;) {
112 i = EVP_read_pw_string_min(buf, MIN_LENGTH, num, prompt, w);
113 if (i != 0) {
114 PEMerr(PEM_F_PEM_DEF_CALLBACK,
115 PEM_R_PROBLEMS_GETTING_PASSWORD);
116 memset(buf, 0, num);
117 return (-1);
118 }
119 l = strlen(buf);
120 if (l < MIN_LENGTH) {
121 fprintf(stderr, "phrase is too short, "
122 "needs to be at least %zu chars\n",
123 (size_t)MIN_LENGTH);
124 } else
125 break;
126 }
127 return (int)l;
128}
129
130void
131PEM_proc_type(char *buf, int type)
132{
133 const char *str;
134
135 if (type == PEM_TYPE_ENCRYPTED)
136 str = "ENCRYPTED";
137 else if (type == PEM_TYPE_MIC_CLEAR)
138 str = "MIC-CLEAR";
139 else if (type == PEM_TYPE_MIC_ONLY)
140 str = "MIC-ONLY";
141 else
142 str = "BAD-TYPE";
143
144 strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE);
145 strlcat(buf, str, PEM_BUFSIZE);
146 strlcat(buf, "\n", PEM_BUFSIZE);
147}
148
149void
150PEM_dek_info(char *buf, const char *type, int len, char *str)
151{
152 static const unsigned char map[17] = "0123456789ABCDEF";
153 long i;
154 int j;
155
156 strlcat(buf, "DEK-Info: ", PEM_BUFSIZE);
157 strlcat(buf, type, PEM_BUFSIZE);
158 strlcat(buf, ",", PEM_BUFSIZE);
159 j = strlen(buf);
160 if (j + (len * 2) + 1 > PEM_BUFSIZE)
161 return;
162 for (i = 0; i < len; i++) {
163 buf[j + i * 2] = map[(str[i] >> 4) & 0x0f];
164 buf[j + i * 2 + 1] = map[(str[i]) & 0x0f];
165 }
166 buf[j + i * 2] = '\n';
167 buf[j + i * 2 + 1] = '\0';
168}
169
170void *
171PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
172 pem_password_cb *cb, void *u)
173{
174 BIO *b;
175 void *ret;
176
177 if ((b = BIO_new(BIO_s_file())) == NULL) {
178 PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB);
179 return (0);
180 }
181 BIO_set_fp(b, fp, BIO_NOCLOSE);
182 ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u);
183 BIO_free(b);
184 return (ret);
185}
186
187static int
188check_pem(const char *nm, const char *name)
189{
190 /* Normal matching nm and name */
191 if (!strcmp(nm, name))
192 return 1;
193
194 /* Make PEM_STRING_EVP_PKEY match any private key */
195
196 if (!strcmp(name, PEM_STRING_EVP_PKEY)) {
197 int slen;
198 const EVP_PKEY_ASN1_METHOD *ameth;
199 if (!strcmp(nm, PEM_STRING_PKCS8))
200 return 1;
201 if (!strcmp(nm, PEM_STRING_PKCS8INF))
202 return 1;
203 slen = pem_check_suffix(nm, "PRIVATE KEY");
204 if (slen > 0) {
205 /* NB: ENGINE implementations wont contain
206 * a deprecated old private key decode function
207 * so don't look for them.
208 */
209 ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
210 if (ameth && ameth->old_priv_decode)
211 return 1;
212 }
213 return 0;
214 }
215
216 if (!strcmp(name, PEM_STRING_PARAMETERS)) {
217 int slen;
218 const EVP_PKEY_ASN1_METHOD *ameth;
219 slen = pem_check_suffix(nm, "PARAMETERS");
220 if (slen > 0) {
221 ENGINE *e;
222 ameth = EVP_PKEY_asn1_find_str(&e, nm, slen);
223 if (ameth) {
224 int r;
225 if (ameth->param_decode)
226 r = 1;
227 else
228 r = 0;
229#ifndef OPENSSL_NO_ENGINE
230 if (e)
231 ENGINE_finish(e);
232#endif
233 return r;
234 }
235 }
236 return 0;
237 }
238
239 /* Permit older strings */
240
241 if (!strcmp(nm, PEM_STRING_X509_OLD) &&
242 !strcmp(name, PEM_STRING_X509))
243 return 1;
244
245 if (!strcmp(nm, PEM_STRING_X509_REQ_OLD) &&
246 !strcmp(name, PEM_STRING_X509_REQ))
247 return 1;
248
249 /* Allow normal certs to be read as trusted certs */
250 if (!strcmp(nm, PEM_STRING_X509) &&
251 !strcmp(name, PEM_STRING_X509_TRUSTED))
252 return 1;
253
254 if (!strcmp(nm, PEM_STRING_X509_OLD) &&
255 !strcmp(name, PEM_STRING_X509_TRUSTED))
256 return 1;
257
258 /* Some CAs use PKCS#7 with CERTIFICATE headers */
259 if (!strcmp(nm, PEM_STRING_X509) &&
260 !strcmp(name, PEM_STRING_PKCS7))
261 return 1;
262
263 if (!strcmp(nm, PEM_STRING_PKCS7_SIGNED) &&
264 !strcmp(name, PEM_STRING_PKCS7))
265 return 1;
266
267#ifndef OPENSSL_NO_CMS
268 if (!strcmp(nm, PEM_STRING_X509) &&
269 !strcmp(name, PEM_STRING_CMS))
270 return 1;
271 /* Allow CMS to be read from PKCS#7 headers */
272 if (!strcmp(nm, PEM_STRING_PKCS7) &&
273 !strcmp(name, PEM_STRING_CMS))
274 return 1;
275#endif
276
277 return 0;
278}
279
280int
281PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
282 const char *name, BIO *bp, pem_password_cb *cb, void *u)
283{
284 EVP_CIPHER_INFO cipher;
285 char *nm = NULL, *header = NULL;
286 unsigned char *data = NULL;
287 long len;
288 int ret = 0;
289
290 for (;;) {
291 if (!PEM_read_bio(bp, &nm, &header, &data, &len)) {
292 if (ERR_GET_REASON(ERR_peek_error()) ==
293 PEM_R_NO_START_LINE)
294 ERR_asprintf_error_data("Expecting: %s", name);
295 return 0;
296 }
297 if (check_pem(nm, name))
298 break;
299 free(nm);
300 free(header);
301 free(data);
302 }
303 if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
304 goto err;
305 if (!PEM_do_header(&cipher, data, &len, cb, u))
306 goto err;
307
308 *pdata = data;
309 *plen = len;
310
311 if (pnm)
312 *pnm = nm;
313
314 ret = 1;
315
316err:
317 if (!ret || !pnm)
318 free(nm);
319 free(header);
320 if (!ret)
321 free(data);
322 return ret;
323}
324
325int
326PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x,
327 const EVP_CIPHER *enc, unsigned char *kstr, int klen,
328 pem_password_cb *callback, void *u)
329{
330 BIO *b;
331 int ret;
332
333 if ((b = BIO_new(BIO_s_file())) == NULL) {
334 PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB);
335 return (0);
336 }
337 BIO_set_fp(b, fp, BIO_NOCLOSE);
338 ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u);
339 BIO_free(b);
340 return (ret);
341}
342
343int
344PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x,
345 const EVP_CIPHER *enc, unsigned char *kstr, int klen,
346 pem_password_cb *callback, void *u)
347{
348 EVP_CIPHER_CTX ctx;
349 int dsize = 0, i, j, ret = 0;
350 unsigned char *p, *data = NULL;
351 const char *objstr = NULL;
352 char buf[PEM_BUFSIZE];
353 unsigned char key[EVP_MAX_KEY_LENGTH];
354 unsigned char iv[EVP_MAX_IV_LENGTH];
355
356 if (enc != NULL) {
357 objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
358 if (objstr == NULL) {
359 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,
360 PEM_R_UNSUPPORTED_CIPHER);
361 goto err;
362 }
363 }
364
365 if ((dsize = i2d(x, NULL)) < 0) {
366 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_ASN1_LIB);
367 dsize = 0;
368 goto err;
369 }
370 /* dzise + 8 bytes are needed */
371 /* actually it needs the cipher block size extra... */
372 data = malloc((unsigned int)dsize + 20);
373 if (data == NULL) {
374 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_MALLOC_FAILURE);
375 goto err;
376 }
377 p = data;
378 i = i2d(x, &p);
379
380 if (enc != NULL) {
381 if (kstr == NULL) {
382 if (callback == NULL)
383 klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
384 else
385 klen = (*callback)(buf, PEM_BUFSIZE, 1, u);
386 if (klen <= 0) {
387 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,
388 PEM_R_READ_KEY);
389 goto err;
390 }
391 kstr = (unsigned char *)buf;
392 }
393 if ((size_t)enc->iv_len > sizeof(iv)) {
394 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, EVP_R_IV_TOO_LARGE);
395 goto err;
396 }
397 arc4random_buf(iv, enc->iv_len); /* Generate a salt */
398 /* The 'iv' is used as the iv and as a salt. It is
399 * NOT taken from the BytesToKey function */
400 if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1,
401 key, NULL))
402 goto err;
403
404 if (kstr == (unsigned char *)buf)
405 OPENSSL_cleanse(buf, PEM_BUFSIZE);
406
407 if (strlen(objstr) + 23 + 2 * enc->iv_len + 13 > sizeof buf) {
408 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,
409 ASN1_R_BUFFER_TOO_SMALL);
410 goto err;
411 }
412
413 buf[0] = '\0';
414 PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
415 PEM_dek_info(buf, objstr, enc->iv_len, (char *)iv);
416 /* k=strlen(buf); */
417
418 EVP_CIPHER_CTX_init(&ctx);
419 ret = 1;
420 if (!EVP_EncryptInit_ex(&ctx, enc, NULL, key, iv) ||
421 !EVP_EncryptUpdate(&ctx, data, &j, data, i) ||
422 !EVP_EncryptFinal_ex(&ctx, &(data[j]), &i))
423 ret = 0;
424 EVP_CIPHER_CTX_cleanup(&ctx);
425 if (ret == 0)
426 goto err;
427 i += j;
428 } else {
429 ret = 1;
430 buf[0] = '\0';
431 }
432 i = PEM_write_bio(bp, name, buf, data, i);
433 if (i <= 0)
434 ret = 0;
435err:
436 OPENSSL_cleanse(key, sizeof(key));
437 OPENSSL_cleanse(iv, sizeof(iv));
438 OPENSSL_cleanse((char *)&ctx, sizeof(ctx));
439 OPENSSL_cleanse(buf, PEM_BUFSIZE);
440 if (data != NULL) {
441 OPENSSL_cleanse(data, (unsigned int)dsize);
442 free(data);
443 }
444 return (ret);
445}
446
447int
448PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
449 pem_password_cb *callback, void *u)
450{
451 int i, j, o, klen;
452 long len;
453 EVP_CIPHER_CTX ctx;
454 unsigned char key[EVP_MAX_KEY_LENGTH];
455 char buf[PEM_BUFSIZE];
456
457 len = *plen;
458
459 if (cipher->cipher == NULL)
460 return (1);
461 if (callback == NULL)
462 klen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u);
463 else
464 klen = callback(buf, PEM_BUFSIZE, 0, u);
465 if (klen <= 0) {
466 PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ);
467 return (0);
468 }
469 if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]),
470 (unsigned char *)buf, klen, 1, key, NULL))
471 return 0;
472
473 j = (int)len;
474 EVP_CIPHER_CTX_init(&ctx);
475 o = EVP_DecryptInit_ex(&ctx, cipher->cipher, NULL, key,
476 &(cipher->iv[0]));
477 if (o)
478 o = EVP_DecryptUpdate(&ctx, data, &i, data, j);
479 if (o)
480 o = EVP_DecryptFinal_ex(&ctx, &(data[i]), &j);
481 EVP_CIPHER_CTX_cleanup(&ctx);
482 OPENSSL_cleanse((char *)buf, sizeof(buf));
483 OPENSSL_cleanse((char *)key, sizeof(key));
484 if (!o) {
485 PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT);
486 return (0);
487 }
488 *plen = j + i;
489 return (1);
490}
491
492int
493PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
494{
495 const EVP_CIPHER *enc = NULL;
496 char *p, c;
497 char **header_pp = &header;
498
499 cipher->cipher = NULL;
500 if ((header == NULL) || (*header == '\0') || (*header == '\n'))
501 return (1);
502 if (strncmp(header, "Proc-Type: ", 11) != 0) {
503 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE);
504 return (0);
505 }
506 header += 11;
507 if (*header != '4')
508 return (0);
509 header++;
510 if (*header != ',')
511 return (0);
512 header++;
513 if (strncmp(header, "ENCRYPTED", 9) != 0) {
514 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED);
515 return (0);
516 }
517 for (; (*header != '\n') && (*header != '\0'); header++)
518 ;
519 if (*header == '\0') {
520 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER);
521 return (0);
522 }
523 header++;
524 if (strncmp(header, "DEK-Info: ", 10) != 0) {
525 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO);
526 return (0);
527 }
528 header += 10;
529
530 p = header;
531 for (;;) {
532 c= *header;
533 if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') ||
534 ((c >= '0') && (c <= '9'))))
535 break;
536 header++;
537 }
538 *header = '\0';
539 cipher->cipher = enc = EVP_get_cipherbyname(p);
540 *header = c;
541 header++;
542
543 if (enc == NULL) {
544 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,
545 PEM_R_UNSUPPORTED_ENCRYPTION);
546 return (0);
547 }
548 if (!load_iv(header_pp, &(cipher->iv[0]), enc->iv_len))
549 return (0);
550
551 return (1);
552}
553
554static int
555load_iv(char **fromp, unsigned char *to, int num)
556{
557 int v, i;
558 char *from;
559
560 from= *fromp;
561 for (i = 0; i < num; i++)
562 to[i] = 0;
563 num *= 2;
564 for (i = 0; i < num; i++) {
565 if ((*from >= '0') && (*from <= '9'))
566 v = *from - '0';
567 else if ((*from >= 'A') && (*from <= 'F'))
568 v = *from - 'A' + 10;
569 else if ((*from >= 'a') && (*from <= 'f'))
570 v = *from - 'a' + 10;
571 else {
572 PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS);
573 return (0);
574 }
575 from++;
576 to[i / 2] |= v << (long)((!(i & 1)) * 4);
577 }
578
579 *fromp = from;
580 return (1);
581}
582
583int
584PEM_write(FILE *fp, char *name, char *header, unsigned char *data, long len)
585{
586 BIO *b;
587 int ret;
588
589 if ((b = BIO_new(BIO_s_file())) == NULL) {
590 PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB);
591 return (0);
592 }
593 BIO_set_fp(b, fp, BIO_NOCLOSE);
594 ret = PEM_write_bio(b, name, header, data, len);
595 BIO_free(b);
596 return (ret);
597}
598
599int
600PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data,
601 long len)
602{
603 int nlen, n, i, j, outl;
604 unsigned char *buf = NULL;
605 EVP_ENCODE_CTX ctx;
606 int reason = ERR_R_BUF_LIB;
607
608 EVP_EncodeInit(&ctx);
609 nlen = strlen(name);
610
611 if ((BIO_write(bp, "-----BEGIN ", 11) != 11) ||
612 (BIO_write(bp, name, nlen) != nlen) ||
613 (BIO_write(bp, "-----\n", 6) != 6))
614 goto err;
615
616 i = strlen(header);
617 if (i > 0) {
618 if ((BIO_write(bp, header, i) != i) ||
619 (BIO_write(bp, "\n", 1) != 1))
620 goto err;
621 }
622
623 buf = reallocarray(NULL, PEM_BUFSIZE, 8);
624 if (buf == NULL) {
625 reason = ERR_R_MALLOC_FAILURE;
626 goto err;
627 }
628
629 i = j = 0;
630 while (len > 0) {
631 n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
632 EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n);
633 if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl))
634 goto err;
635 i += outl;
636 len -= n;
637 j += n;
638 }
639 EVP_EncodeFinal(&ctx, buf, &outl);
640 if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
641 goto err;
642 OPENSSL_cleanse(buf, PEM_BUFSIZE * 8);
643 free(buf);
644 buf = NULL;
645 if ((BIO_write(bp, "-----END ", 9) != 9) ||
646 (BIO_write(bp, name, nlen) != nlen) ||
647 (BIO_write(bp, "-----\n", 6) != 6))
648 goto err;
649 return (i + outl);
650
651err:
652 if (buf) {
653 OPENSSL_cleanse(buf, PEM_BUFSIZE * 8);
654 free(buf);
655 }
656 PEMerr(PEM_F_PEM_WRITE_BIO, reason);
657 return (0);
658}
659
660int
661PEM_read(FILE *fp, char **name, char **header, unsigned char **data, long *len)
662{
663 BIO *b;
664 int ret;
665
666 if ((b = BIO_new(BIO_s_file())) == NULL) {
667 PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB);
668 return (0);
669 }
670 BIO_set_fp(b, fp, BIO_NOCLOSE);
671 ret = PEM_read_bio(b, name, header, data, len);
672 BIO_free(b);
673 return (ret);
674}
675
676int
677PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
678 long *len)
679{
680 EVP_ENCODE_CTX ctx;
681 int end = 0, i, k, bl = 0, hl = 0, nohead = 0;
682 char buf[256];
683 BUF_MEM *nameB;
684 BUF_MEM *headerB;
685 BUF_MEM *dataB, *tmpB;
686
687 nameB = BUF_MEM_new();
688 headerB = BUF_MEM_new();
689 dataB = BUF_MEM_new();
690 if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) {
691 BUF_MEM_free(nameB);
692 BUF_MEM_free(headerB);
693 BUF_MEM_free(dataB);
694 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
695 return (0);
696 }
697
698 buf[254] = '\0';
699 for (;;) {
700 i = BIO_gets(bp, buf, 254);
701
702 if (i <= 0) {
703 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE);
704 goto err;
705 }
706
707 while ((i >= 0) && (buf[i] <= ' '))
708 i--;
709 buf[++i] = '\n';
710 buf[++i] = '\0';
711
712 if (strncmp(buf, "-----BEGIN ", 11) == 0) {
713 i = strlen(&(buf[11]));
714
715 if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0)
716 continue;
717 if (!BUF_MEM_grow(nameB, i + 9)) {
718 PEMerr(PEM_F_PEM_READ_BIO,
719 ERR_R_MALLOC_FAILURE);
720 goto err;
721 }
722 memcpy(nameB->data, &(buf[11]), i - 6);
723 nameB->data[i - 6] = '\0';
724 break;
725 }
726 }
727 hl = 0;
728 if (!BUF_MEM_grow(headerB, 256)) {
729 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
730 goto err;
731 }
732 headerB->data[0] = '\0';
733 for (;;) {
734 i = BIO_gets(bp, buf, 254);
735 if (i <= 0)
736 break;
737
738 while ((i >= 0) && (buf[i] <= ' '))
739 i--;
740 buf[++i] = '\n';
741 buf[++i] = '\0';
742
743 if (buf[0] == '\n')
744 break;
745 if (!BUF_MEM_grow(headerB, hl + i + 9)) {
746 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
747 goto err;
748 }
749 if (strncmp(buf, "-----END ", 9) == 0) {
750 nohead = 1;
751 break;
752 }
753 memcpy(&(headerB->data[hl]), buf, i);
754 headerB->data[hl + i] = '\0';
755 hl += i;
756 }
757
758 bl = 0;
759 if (!BUF_MEM_grow(dataB, 1024)) {
760 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
761 goto err;
762 }
763 dataB->data[0] = '\0';
764 if (!nohead) {
765 for (;;) {
766 i = BIO_gets(bp, buf, 254);
767 if (i <= 0)
768 break;
769
770 while ((i >= 0) && (buf[i] <= ' '))
771 i--;
772 buf[++i] = '\n';
773 buf[++i] = '\0';
774
775 if (i != 65)
776 end = 1;
777 if (strncmp(buf, "-----END ", 9) == 0)
778 break;
779 if (i > 65)
780 break;
781 if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) {
782 PEMerr(PEM_F_PEM_READ_BIO,
783 ERR_R_MALLOC_FAILURE);
784 goto err;
785 }
786 memcpy(&(dataB->data[bl]), buf, i);
787 dataB->data[bl + i] = '\0';
788 bl += i;
789 if (end) {
790 buf[0] = '\0';
791 i = BIO_gets(bp, buf, 254);
792 if (i <= 0)
793 break;
794
795 while ((i >= 0) && (buf[i] <= ' '))
796 i--;
797 buf[++i] = '\n';
798 buf[++i] = '\0';
799
800 break;
801 }
802 }
803 } else {
804 tmpB = headerB;
805 headerB = dataB;
806 dataB = tmpB;
807 bl = hl;
808 }
809 i = strlen(nameB->data);
810 if ((strncmp(buf, "-----END ", 9) != 0) ||
811 (strncmp(nameB->data, &(buf[9]), i) != 0) ||
812 (strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) {
813 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE);
814 goto err;
815 }
816
817 EVP_DecodeInit(&ctx);
818 i = EVP_DecodeUpdate(&ctx,
819 (unsigned char *)dataB->data, &bl,
820 (unsigned char *)dataB->data, bl);
821 if (i < 0) {
822 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
823 goto err;
824 }
825 i = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k);
826 if (i < 0) {
827 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
828 goto err;
829 }
830 bl += k;
831
832 if (bl == 0)
833 goto err;
834 *name = nameB->data;
835 *header = headerB->data;
836 *data = (unsigned char *)dataB->data;
837 *len = bl;
838 free(nameB);
839 free(headerB);
840 free(dataB);
841 return (1);
842
843err:
844 BUF_MEM_free(nameB);
845 BUF_MEM_free(headerB);
846 BUF_MEM_free(dataB);
847 return (0);
848}
849
850/* Check pem string and return prefix length.
851 * If for example the pem_str == "RSA PRIVATE KEY" and suffix = "PRIVATE KEY"
852 * the return value is 3 for the string "RSA".
853 */
854
855int
856pem_check_suffix(const char *pem_str, const char *suffix)
857{
858 int pem_len = strlen(pem_str);
859 int suffix_len = strlen(suffix);
860 const char *p;
861
862 if (suffix_len + 1 >= pem_len)
863 return 0;
864 p = pem_str + pem_len - suffix_len;
865 if (strcmp(p, suffix))
866 return 0;
867 p--;
868 if (*p != ' ')
869 return 0;
870 return p - pem_str;
871}
diff --git a/src/lib/libcrypto/pem/pem_oth.c b/src/lib/libcrypto/pem/pem_oth.c
deleted file mode 100644
index ccd2b893d5..0000000000
--- a/src/lib/libcrypto/pem/pem_oth.c
+++ /dev/null
@@ -1,87 +0,0 @@
1/* $OpenBSD: pem_oth.c,v 1.7 2014/10/18 17:20:40 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/buffer.h>
62#include <openssl/err.h>
63#include <openssl/evp.h>
64#include <openssl/objects.h>
65#include <openssl/pem.h>
66#include <openssl/x509.h>
67
68/* Handle 'other' PEMs: not private keys */
69
70void *
71PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp, void **x,
72 pem_password_cb *cb, void *u)
73{
74 const unsigned char *p = NULL;
75 unsigned char *data = NULL;
76 long len;
77 char *ret = NULL;
78
79 if (!PEM_bytes_read_bio(&data, &len, NULL, name, bp, cb, u))
80 return NULL;
81 p = data;
82 ret = d2i(x, &p, len);
83 if (ret == NULL)
84 PEMerr(PEM_F_PEM_ASN1_READ_BIO, ERR_R_ASN1_LIB);
85 free(data);
86 return (ret);
87}
diff --git a/src/lib/libcrypto/pem/pem_pk8.c b/src/lib/libcrypto/pem/pem_pk8.c
deleted file mode 100644
index 5b0fcc236b..0000000000
--- a/src/lib/libcrypto/pem/pem_pk8.c
+++ /dev/null
@@ -1,255 +0,0 @@
1/* $OpenBSD: pem_pk8.c,v 1.9 2014/10/18 17:20:40 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/buffer.h>
62#include <openssl/err.h>
63#include <openssl/evp.h>
64#include <openssl/objects.h>
65#include <openssl/pem.h>
66#include <openssl/pkcs12.h>
67#include <openssl/x509.h>
68
69static int do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder, int nid,
70 const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u);
71static int do_pk8pkey_fp(FILE *bp, EVP_PKEY *x, int isder, int nid,
72 const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u);
73
74/* These functions write a private key in PKCS#8 format: it is a "drop in"
75 * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
76 * is NULL then it uses the unencrypted private key form. The 'nid' versions
77 * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
78 */
79
80int
81PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid, char *kstr,
82 int klen, pem_password_cb *cb, void *u)
83{
84 return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
85}
86
87int
88PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
89 char *kstr, int klen, pem_password_cb *cb, void *u)
90{
91 return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
92}
93
94int
95i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
96 char *kstr, int klen, pem_password_cb *cb, void *u)
97{
98 return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
99}
100
101int
102i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
103 char *kstr, int klen, pem_password_cb *cb, void *u)
104{
105 return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
106}
107
108static int
109do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder, int nid, const EVP_CIPHER *enc,
110 char *kstr, int klen, pem_password_cb *cb, void *u)
111{
112 X509_SIG *p8;
113 PKCS8_PRIV_KEY_INFO *p8inf;
114 char buf[PEM_BUFSIZE];
115 int ret;
116
117 if (!(p8inf = EVP_PKEY2PKCS8(x))) {
118 PEMerr(PEM_F_DO_PK8PKEY,
119 PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
120 return 0;
121 }
122 if (enc || (nid != -1)) {
123 if (!kstr) {
124 if (!cb)
125 klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
126 else
127 klen = cb(buf, PEM_BUFSIZE, 1, u);
128 if (klen <= 0) {
129 PEMerr(PEM_F_DO_PK8PKEY, PEM_R_READ_KEY);
130 PKCS8_PRIV_KEY_INFO_free(p8inf);
131 return 0;
132 }
133
134 kstr = buf;
135 }
136 p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
137 if (kstr == buf)
138 OPENSSL_cleanse(buf, klen);
139 PKCS8_PRIV_KEY_INFO_free(p8inf);
140 if (isder)
141 ret = i2d_PKCS8_bio(bp, p8);
142 else
143 ret = PEM_write_bio_PKCS8(bp, p8);
144 X509_SIG_free(p8);
145 return ret;
146 } else {
147 if (isder)
148 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
149 else
150 ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
151 PKCS8_PRIV_KEY_INFO_free(p8inf);
152 return ret;
153 }
154}
155
156EVP_PKEY *
157d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)
158{
159 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
160 X509_SIG *p8 = NULL;
161 int klen;
162 EVP_PKEY *ret;
163 char psbuf[PEM_BUFSIZE];
164
165 p8 = d2i_PKCS8_bio(bp, NULL);
166 if (!p8)
167 return NULL;
168 if (cb)
169 klen = cb(psbuf, PEM_BUFSIZE, 0, u);
170 else
171 klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
172 if (klen <= 0) {
173 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ);
174 X509_SIG_free(p8);
175 return NULL;
176 }
177 p8inf = PKCS8_decrypt(p8, psbuf, klen);
178 X509_SIG_free(p8);
179 if (!p8inf)
180 return NULL;
181 ret = EVP_PKCS82PKEY(p8inf);
182 PKCS8_PRIV_KEY_INFO_free(p8inf);
183 if (!ret)
184 return NULL;
185 if (x) {
186 EVP_PKEY_free(*x);
187 *x = ret;
188 }
189 return ret;
190}
191
192
193int
194i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
195 char *kstr, int klen, pem_password_cb *cb, void *u)
196{
197 return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
198}
199
200int
201i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid, char *kstr,
202 int klen, pem_password_cb *cb, void *u)
203{
204 return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
205}
206
207int
208PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid, char *kstr,
209 int klen, pem_password_cb *cb, void *u)
210{
211 return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
212}
213
214int
215PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
216 char *kstr, int klen, pem_password_cb *cb, void *u)
217{
218 return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
219}
220
221static int
222do_pk8pkey_fp(FILE *fp, EVP_PKEY *x, int isder, int nid, const EVP_CIPHER *enc,
223 char *kstr, int klen, pem_password_cb *cb, void *u)
224{
225 BIO *bp;
226 int ret;
227
228 if (!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) {
229 PEMerr(PEM_F_DO_PK8PKEY_FP, ERR_R_BUF_LIB);
230 return (0);
231 }
232 ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
233 BIO_free(bp);
234 return ret;
235}
236
237EVP_PKEY *
238d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
239{
240 BIO *bp;
241 EVP_PKEY *ret;
242
243 if (!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) {
244 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP, ERR_R_BUF_LIB);
245 return NULL;
246 }
247 ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
248 BIO_free(bp);
249 return ret;
250}
251
252
253IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
254IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,
255 PKCS8_PRIV_KEY_INFO)
diff --git a/src/lib/libcrypto/pem/pem_pkey.c b/src/lib/libcrypto/pem/pem_pkey.c
deleted file mode 100644
index e9c0a8b1c9..0000000000
--- a/src/lib/libcrypto/pem/pem_pkey.c
+++ /dev/null
@@ -1,253 +0,0 @@
1/* $OpenBSD: pem_pkey.c,v 1.20 2015/02/11 03:19:37 doug Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/opensslconf.h>
63
64#include <openssl/buffer.h>
65#include <openssl/err.h>
66#include <openssl/evp.h>
67#include <openssl/objects.h>
68#include <openssl/pem.h>
69#include <openssl/pkcs12.h>
70#include <openssl/x509.h>
71
72#ifndef OPENSSL_NO_ENGINE
73#include <openssl/engine.h>
74#endif
75
76#include "asn1_locl.h"
77
78int pem_check_suffix(const char *pem_str, const char *suffix);
79
80EVP_PKEY *
81PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)
82{
83 char *nm = NULL;
84 const unsigned char *p = NULL;
85 unsigned char *data = NULL;
86 long len;
87 int slen;
88 EVP_PKEY *ret = NULL;
89
90 if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY,
91 bp, cb, u))
92 return NULL;
93 p = data;
94
95 if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
96 PKCS8_PRIV_KEY_INFO *p8inf;
97 p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
98 if (!p8inf)
99 goto p8err;
100 ret = EVP_PKCS82PKEY(p8inf);
101 if (x) {
102 EVP_PKEY_free(*x);
103 *x = ret;
104 }
105 PKCS8_PRIV_KEY_INFO_free(p8inf);
106 } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
107 PKCS8_PRIV_KEY_INFO *p8inf;
108 X509_SIG *p8;
109 int klen;
110 char psbuf[PEM_BUFSIZE];
111 p8 = d2i_X509_SIG(NULL, &p, len);
112 if (!p8)
113 goto p8err;
114 if (cb)
115 klen = cb(psbuf, PEM_BUFSIZE, 0, u);
116 else
117 klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
118 if (klen <= 0) {
119 PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY,
120 PEM_R_BAD_PASSWORD_READ);
121 X509_SIG_free(p8);
122 goto err;
123 }
124 p8inf = PKCS8_decrypt(p8, psbuf, klen);
125 X509_SIG_free(p8);
126 if (!p8inf)
127 goto p8err;
128 ret = EVP_PKCS82PKEY(p8inf);
129 if (x) {
130 EVP_PKEY_free(*x);
131 *x = ret;
132 }
133 PKCS8_PRIV_KEY_INFO_free(p8inf);
134 } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) {
135 const EVP_PKEY_ASN1_METHOD *ameth;
136 ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
137 if (!ameth || !ameth->old_priv_decode)
138 goto p8err;
139 ret = d2i_PrivateKey(ameth->pkey_id, x, &p, len);
140 }
141
142p8err:
143 if (ret == NULL)
144 PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB);
145err:
146 free(nm);
147 OPENSSL_cleanse(data, len);
148 free(data);
149 return (ret);
150}
151
152int
153PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
154 unsigned char *kstr, int klen, pem_password_cb *cb, void *u)
155{
156 char pem_str[80];
157
158 if (!x->ameth || x->ameth->priv_encode)
159 return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
160 (char *)kstr, klen, cb, u);
161
162 (void) snprintf(pem_str, sizeof(pem_str), "%s PRIVATE KEY",
163 x->ameth->pem_str);
164 return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
165 pem_str, bp, x, enc, kstr, klen, cb, u);
166}
167
168EVP_PKEY *
169PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
170{
171 char *nm = NULL;
172 const unsigned char *p = NULL;
173 unsigned char *data = NULL;
174 long len;
175 int slen;
176 EVP_PKEY *ret = NULL;
177
178 if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS,
179 bp, 0, NULL))
180 return NULL;
181 p = data;
182
183 if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0) {
184 ret = EVP_PKEY_new();
185 if (!ret)
186 goto err;
187 if (!EVP_PKEY_set_type_str(ret, nm, slen) ||
188 !ret->ameth->param_decode ||
189 !ret->ameth->param_decode(ret, &p, len)) {
190 EVP_PKEY_free(ret);
191 ret = NULL;
192 goto err;
193 }
194 if (x) {
195 EVP_PKEY_free(*x);
196 *x = ret;
197 }
198 }
199
200err:
201 if (ret == NULL)
202 PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS, ERR_R_ASN1_LIB);
203 free(nm);
204 free(data);
205 return (ret);
206}
207
208int
209PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
210{
211 char pem_str[80];
212
213 if (!x->ameth || !x->ameth->param_encode)
214 return 0;
215
216 (void) snprintf(pem_str, sizeof(pem_str), "%s PARAMETERS",
217 x->ameth->pem_str);
218 return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
219 pem_str, bp, x, NULL, NULL, 0, 0, NULL);
220}
221
222EVP_PKEY *
223PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
224{
225 BIO *b;
226 EVP_PKEY *ret;
227
228 if ((b = BIO_new(BIO_s_file())) == NULL) {
229 PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
230 return (0);
231 }
232 BIO_set_fp(b, fp, BIO_NOCLOSE);
233 ret = PEM_read_bio_PrivateKey(b, x, cb, u);
234 BIO_free(b);
235 return (ret);
236}
237
238int
239PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
240 unsigned char *kstr, int klen, pem_password_cb *cb, void *u)
241{
242 BIO *b;
243 int ret;
244
245 if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
246 PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
247 return 0;
248 }
249 ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
250 BIO_free(b);
251 return ret;
252}
253
diff --git a/src/lib/libcrypto/pem/pem_seal.c b/src/lib/libcrypto/pem/pem_seal.c
deleted file mode 100644
index 08837bd7f7..0000000000
--- a/src/lib/libcrypto/pem/pem_seal.c
+++ /dev/null
@@ -1,190 +0,0 @@
1/* $OpenBSD: pem_seal.c,v 1.21 2014/10/18 17:20:40 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/opensslconf.h> /* for OPENSSL_NO_RSA */
63
64#ifndef OPENSSL_NO_RSA
65
66#include <openssl/err.h>
67#include <openssl/evp.h>
68#include <openssl/objects.h>
69#include <openssl/pem.h>
70#include <openssl/rsa.h>
71#include <openssl/x509.h>
72
73int
74PEM_SealInit(PEM_ENCODE_SEAL_CTX *ctx, EVP_CIPHER *type, EVP_MD *md_type,
75 unsigned char **ek, int *ekl, unsigned char *iv, EVP_PKEY **pubk, int npubk)
76{
77 unsigned char key[EVP_MAX_KEY_LENGTH];
78 int ret = -1;
79 int i, j, max = 0;
80 char *s = NULL;
81
82 for (i = 0; i < npubk; i++) {
83 if (pubk[i]->type != EVP_PKEY_RSA) {
84 PEMerr(PEM_F_PEM_SEALINIT, PEM_R_PUBLIC_KEY_NO_RSA);
85 goto err;
86 }
87 j = RSA_size(pubk[i]->pkey.rsa);
88 if (j > max)
89 max = j;
90 }
91 s = reallocarray(NULL, max, 2);
92 if (s == NULL) {
93 PEMerr(PEM_F_PEM_SEALINIT, ERR_R_MALLOC_FAILURE);
94 goto err;
95 }
96
97 EVP_EncodeInit(&ctx->encode);
98
99 EVP_MD_CTX_init(&ctx->md);
100 if (!EVP_SignInit(&ctx->md, md_type))
101 goto err;
102
103 EVP_CIPHER_CTX_init(&ctx->cipher);
104 ret = EVP_SealInit(&ctx->cipher, type, ek, ekl, iv, pubk, npubk);
105 if (ret <= 0)
106 goto err;
107
108 /* base64 encode the keys */
109 for (i = 0; i < npubk; i++) {
110 j = EVP_EncodeBlock((unsigned char *)s, ek[i],
111 RSA_size(pubk[i]->pkey.rsa));
112 ekl[i] = j;
113 memcpy(ek[i], s, j + 1);
114 }
115
116 ret = npubk;
117
118err:
119 free(s);
120 OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
121 return (ret);
122}
123
124void
125PEM_SealUpdate(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *out, int *outl,
126 unsigned char *in, int inl)
127{
128 unsigned char buffer[1600];
129 int i, j;
130
131 *outl = 0;
132 EVP_SignUpdate(&ctx->md, in, inl);
133 for (;;) {
134 if (inl <= 0)
135 break;
136 if (inl > 1200)
137 i = 1200;
138 else
139 i = inl;
140 EVP_EncryptUpdate(&ctx->cipher, buffer, &j, in, i);
141 EVP_EncodeUpdate(&ctx->encode, out, &j, buffer, j);
142 *outl += j;
143 out += j;
144 in += i;
145 inl -= i;
146 }
147}
148
149int
150PEM_SealFinal(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *sig, int *sigl,
151 unsigned char *out, int *outl, EVP_PKEY *priv)
152{
153 unsigned char *s = NULL;
154 int ret = 0, j;
155 unsigned int i;
156
157 if (priv->type != EVP_PKEY_RSA) {
158 PEMerr(PEM_F_PEM_SEALFINAL, PEM_R_PUBLIC_KEY_NO_RSA);
159 goto err;
160 }
161 i = RSA_size(priv->pkey.rsa);
162 if (i < 100)
163 i = 100;
164 s = reallocarray(NULL, i, 2);
165 if (s == NULL) {
166 PEMerr(PEM_F_PEM_SEALFINAL, ERR_R_MALLOC_FAILURE);
167 goto err;
168 }
169
170 if (!EVP_EncryptFinal_ex(&ctx->cipher, s, (int *)&i))
171 goto err;
172 EVP_EncodeUpdate(&ctx->encode, out, &j, s, i);
173 *outl = j;
174 out += j;
175 EVP_EncodeFinal(&ctx->encode, out, &j);
176 *outl += j;
177
178 if (!EVP_SignFinal(&ctx->md, s, &i, priv))
179 goto err;
180 *sigl = EVP_EncodeBlock(sig, s, i);
181
182 ret = 1;
183
184err:
185 EVP_MD_CTX_cleanup(&ctx->md);
186 EVP_CIPHER_CTX_cleanup(&ctx->cipher);
187 free(s);
188 return (ret);
189}
190#endif
diff --git a/src/lib/libcrypto/pem/pem_sign.c b/src/lib/libcrypto/pem/pem_sign.c
deleted file mode 100644
index aab8c4d6b8..0000000000
--- a/src/lib/libcrypto/pem/pem_sign.c
+++ /dev/null
@@ -1,105 +0,0 @@
1/* $OpenBSD: pem_sign.c,v 1.12 2014/10/18 17:20:40 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61#include <openssl/err.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#include <openssl/pem.h>
65#include <openssl/x509.h>
66
67void
68PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type)
69{
70 EVP_DigestInit_ex(ctx, type, NULL);
71}
72
73void
74PEM_SignUpdate(EVP_MD_CTX *ctx, unsigned char *data,
75 unsigned int count)
76{
77 EVP_DigestUpdate(ctx, data, count);
78}
79
80int
81PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, unsigned int *siglen,
82 EVP_PKEY *pkey)
83{
84 unsigned char *m;
85 int i, ret = 0;
86 unsigned int m_len;
87
88 m = malloc(EVP_PKEY_size(pkey) + 2);
89 if (m == NULL) {
90 PEMerr(PEM_F_PEM_SIGNFINAL, ERR_R_MALLOC_FAILURE);
91 goto err;
92 }
93
94 if (EVP_SignFinal(ctx, m, &m_len, pkey) <= 0)
95 goto err;
96
97 i = EVP_EncodeBlock(sigret, m, m_len);
98 *siglen = i;
99 ret = 1;
100
101err:
102 /* ctx has been zeroed by EVP_SignFinal() */
103 free(m);
104 return (ret);
105}
diff --git a/src/lib/libcrypto/pem/pem_x509.c b/src/lib/libcrypto/pem/pem_x509.c
deleted file mode 100644
index e4b7f1c2c6..0000000000
--- a/src/lib/libcrypto/pem/pem_x509.c
+++ /dev/null
@@ -1,67 +0,0 @@
1/* $OpenBSD: pem_x509.c,v 1.6 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001.
4 */
5/* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60
61#include <openssl/bio.h>
62#include <openssl/evp.h>
63#include <openssl/pem.h>
64#include <openssl/pkcs7.h>
65#include <openssl/x509.h>
66
67IMPLEMENT_PEM_rw(X509, X509, PEM_STRING_X509, X509)
diff --git a/src/lib/libcrypto/pem/pem_xaux.c b/src/lib/libcrypto/pem/pem_xaux.c
deleted file mode 100644
index eb76840532..0000000000
--- a/src/lib/libcrypto/pem/pem_xaux.c
+++ /dev/null
@@ -1,69 +0,0 @@
1/* $OpenBSD: pem_xaux.c,v 1.7 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001.
4 */
5/* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60
61#include <openssl/bio.h>
62#include <openssl/evp.h>
63#include <openssl/pem.h>
64#include <openssl/pkcs7.h>
65#include <openssl/x509.h>
66
67IMPLEMENT_PEM_rw(X509_AUX, X509, PEM_STRING_X509_TRUSTED, X509_AUX)
68IMPLEMENT_PEM_rw(X509_CERT_PAIR, X509_CERT_PAIR, PEM_STRING_X509_PAIR,
69 X509_CERT_PAIR)
diff --git a/src/lib/libcrypto/pem/pkcs7.lis b/src/lib/libcrypto/pem/pkcs7.lis
deleted file mode 100644
index be90c5d87f..0000000000
--- a/src/lib/libcrypto/pem/pkcs7.lis
+++ /dev/null
@@ -1,22 +0,0 @@
121 0:d=0 hl=2 l= 0 cons: univ: SEQUENCE
2 00 2:d=0 hl=2 l= 9 prim: univ: OBJECT_IDENTIFIER :pkcs-7-signedData
3 21 13:d=0 hl=2 l= 0 cons: cont: 00 # explicit tag
4 21 15:d=0 hl=2 l= 0 cons: univ: SEQUENCE
5 00 17:d=0 hl=2 l= 1 prim: univ: INTEGER # version
6 20 20:d=0 hl=2 l= 0 cons: univ: SET
7 21 22:d=0 hl=2 l= 0 cons: univ: SEQUENCE
8 00 24:d=0 hl=2 l= 9 prim: univ: OBJECT_IDENTIFIER :pkcs-7-data
9 00 35:d=0 hl=2 l= 0 prim: univ: EOC
10 21 37:d=0 hl=2 l= 0 cons: cont: 00 # cert tag
11 20 39:d=0 hl=4 l=545 cons: univ: SEQUENCE
12 20 588:d=0 hl=4 l=524 cons: univ: SEQUENCE
13 00 1116:d=0 hl=2 l= 0 prim: univ: EOC
14 21 1118:d=0 hl=2 l= 0 cons: cont: 01 # crl tag
15 20 1120:d=0 hl=4 l=653 cons: univ: SEQUENCE
16 20 1777:d=0 hl=4 l=285 cons: univ: SEQUENCE
17 00 2066:d=0 hl=2 l= 0 prim: univ: EOC
18 21 2068:d=0 hl=2 l= 0 cons: univ: SET # signers
19 00 2070:d=0 hl=2 l= 0 prim: univ: EOC
20 00 2072:d=0 hl=2 l= 0 prim: univ: EOC
21 00 2074:d=0 hl=2 l= 0 prim: univ: EOC
2200 2076:d=0 hl=2 l= 0 prim: univ: EOC
diff --git a/src/lib/libcrypto/pem/pvkfmt.c b/src/lib/libcrypto/pem/pvkfmt.c
deleted file mode 100644
index 2009c9db80..0000000000
--- a/src/lib/libcrypto/pem/pvkfmt.c
+++ /dev/null
@@ -1,933 +0,0 @@
1/* $OpenBSD: pvkfmt.c,v 1.12 2014/10/22 13:02:04 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2005.
4 */
5/* ====================================================================
6 * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59/* Support for PVK format keys and related structures (such a PUBLICKEYBLOB
60 * and PRIVATEKEYBLOB).
61 */
62
63#include <stdlib.h>
64#include <string.h>
65
66#include <openssl/opensslconf.h>
67
68#include <openssl/bn.h>
69#include <openssl/err.h>
70#include <openssl/pem.h>
71
72#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
73#include <openssl/dsa.h>
74#include <openssl/rsa.h>
75
76/* Utility function: read a DWORD (4 byte unsigned integer) in little endian
77 * format
78 */
79
80static unsigned int
81read_ledword(const unsigned char **in)
82{
83 const unsigned char *p = *in;
84 unsigned int ret;
85
86 ret = *p++;
87 ret |= (*p++ << 8);
88 ret |= (*p++ << 16);
89 ret |= (*p++ << 24);
90 *in = p;
91 return ret;
92}
93
94/* Read a BIGNUM in little endian format. The docs say that this should take up
95 * bitlen/8 bytes.
96 */
97
98static int
99read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
100{
101 const unsigned char *p;
102 unsigned char *tmpbuf, *q;
103 unsigned int i;
104
105 p = *in + nbyte - 1;
106 tmpbuf = malloc(nbyte);
107 if (!tmpbuf)
108 return 0;
109 q = tmpbuf;
110 for (i = 0; i < nbyte; i++)
111 *q++ = *p--;
112 *r = BN_bin2bn(tmpbuf, nbyte, NULL);
113 free(tmpbuf);
114 if (*r) {
115 *in += nbyte;
116 return 1;
117 } else
118 return 0;
119}
120
121
122/* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
123
124#define MS_PUBLICKEYBLOB 0x6
125#define MS_PRIVATEKEYBLOB 0x7
126#define MS_RSA1MAGIC 0x31415352L
127#define MS_RSA2MAGIC 0x32415352L
128#define MS_DSS1MAGIC 0x31535344L
129#define MS_DSS2MAGIC 0x32535344L
130
131#define MS_KEYALG_RSA_KEYX 0xa400
132#define MS_KEYALG_DSS_SIGN 0x2200
133
134#define MS_KEYTYPE_KEYX 0x1
135#define MS_KEYTYPE_SIGN 0x2
136
137/* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
138#define MS_PVKMAGIC 0xb0b5f11eL
139/* Salt length for PVK files */
140#define PVK_SALTLEN 0x10
141
142static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
143 unsigned int bitlen, int ispub);
144static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
145 unsigned int bitlen, int ispub);
146
147static int
148do_blob_header(const unsigned char **in, unsigned int length,
149 unsigned int *pmagic, unsigned int *pbitlen, int *pisdss, int *pispub)
150{
151 const unsigned char *p = *in;
152
153 if (length < 16)
154 return 0;
155 /* bType */
156 if (*p == MS_PUBLICKEYBLOB) {
157 if (*pispub == 0) {
158 PEMerr(PEM_F_DO_BLOB_HEADER,
159 PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
160 return 0;
161 }
162 *pispub = 1;
163 } else if (*p == MS_PRIVATEKEYBLOB) {
164 if (*pispub == 1) {
165 PEMerr(PEM_F_DO_BLOB_HEADER,
166 PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
167 return 0;
168 }
169 *pispub = 0;
170 } else
171 return 0;
172 p++;
173 /* Version */
174 if (*p++ != 0x2) {
175 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER);
176 return 0;
177 }
178 /* Ignore reserved, aiKeyAlg */
179 p += 6;
180 *pmagic = read_ledword(&p);
181 *pbitlen = read_ledword(&p);
182 *pisdss = 0;
183 switch (*pmagic) {
184
185 case MS_DSS1MAGIC:
186 *pisdss = 1;
187 case MS_RSA1MAGIC:
188 if (*pispub == 0) {
189 PEMerr(PEM_F_DO_BLOB_HEADER,
190 PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
191 return 0;
192 }
193 break;
194
195 case MS_DSS2MAGIC:
196 *pisdss = 1;
197 case MS_RSA2MAGIC:
198 if (*pispub == 1) {
199 PEMerr(PEM_F_DO_BLOB_HEADER,
200 PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
201 return 0;
202 }
203 break;
204
205 default:
206 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
207 return -1;
208 }
209 *in = p;
210 return 1;
211}
212
213static unsigned int
214blob_length(unsigned bitlen, int isdss, int ispub)
215{
216 unsigned int nbyte, hnbyte;
217
218 nbyte = (bitlen + 7) >> 3;
219 hnbyte = (bitlen + 15) >> 4;
220 if (isdss) {
221
222 /* Expected length: 20 for q + 3 components bitlen each + 24
223 * for seed structure.
224 */
225 if (ispub)
226 return 44 + 3 * nbyte;
227 /* Expected length: 20 for q, priv, 2 bitlen components + 24
228 * for seed structure.
229 */
230 else
231 return 64 + 2 * nbyte;
232 } else {
233 /* Expected length: 4 for 'e' + 'n' */
234 if (ispub)
235 return 4 + nbyte;
236 else
237 /* Expected length: 4 for 'e' and 7 other components.
238 * 2 components are bitlen size, 5 are bitlen/2
239 */
240 return 4 + 2*nbyte + 5*hnbyte;
241 }
242
243}
244
245static EVP_PKEY *
246do_b2i(const unsigned char **in, unsigned int length, int ispub)
247{
248 const unsigned char *p = *in;
249 unsigned int bitlen, magic;
250 int isdss;
251
252 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
253 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
254 return NULL;
255 }
256 length -= 16;
257 if (length < blob_length(bitlen, isdss, ispub)) {
258 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
259 return NULL;
260 }
261 if (isdss)
262 return b2i_dss(&p, length, bitlen, ispub);
263 else
264 return b2i_rsa(&p, length, bitlen, ispub);
265}
266
267static EVP_PKEY *
268do_b2i_bio(BIO *in, int ispub)
269{
270 const unsigned char *p;
271 unsigned char hdr_buf[16], *buf = NULL;
272 unsigned int bitlen, magic, length;
273 int isdss;
274 EVP_PKEY *ret = NULL;
275
276 if (BIO_read(in, hdr_buf, 16) != 16) {
277 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
278 return NULL;
279 }
280 p = hdr_buf;
281 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
282 return NULL;
283
284 length = blob_length(bitlen, isdss, ispub);
285 buf = malloc(length);
286 if (!buf) {
287 PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
288 goto err;
289 }
290 p = buf;
291 if (BIO_read(in, buf, length) != (int)length) {
292 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
293 goto err;
294 }
295
296 if (isdss)
297 ret = b2i_dss(&p, length, bitlen, ispub);
298 else
299 ret = b2i_rsa(&p, length, bitlen, ispub);
300
301err:
302 free(buf);
303 return ret;
304}
305
306static EVP_PKEY *
307b2i_dss(const unsigned char **in, unsigned int length, unsigned int bitlen,
308 int ispub)
309{
310 const unsigned char *p = *in;
311 EVP_PKEY *ret = NULL;
312 DSA *dsa = NULL;
313 BN_CTX *ctx = NULL;
314 unsigned int nbyte;
315
316 nbyte = (bitlen + 7) >> 3;
317
318 dsa = DSA_new();
319 ret = EVP_PKEY_new();
320 if (!dsa || !ret)
321 goto memerr;
322 if (!read_lebn(&p, nbyte, &dsa->p))
323 goto memerr;
324 if (!read_lebn(&p, 20, &dsa->q))
325 goto memerr;
326 if (!read_lebn(&p, nbyte, &dsa->g))
327 goto memerr;
328 if (ispub) {
329 if (!read_lebn(&p, nbyte, &dsa->pub_key))
330 goto memerr;
331 } else {
332 if (!read_lebn(&p, 20, &dsa->priv_key))
333 goto memerr;
334 /* Calculate public key */
335 if (!(dsa->pub_key = BN_new()))
336 goto memerr;
337 if (!(ctx = BN_CTX_new()))
338 goto memerr;
339 if (!BN_mod_exp(dsa->pub_key, dsa->g,
340 dsa->priv_key, dsa->p, ctx))
341 goto memerr;
342 BN_CTX_free(ctx);
343 }
344
345 EVP_PKEY_set1_DSA(ret, dsa);
346 DSA_free(dsa);
347 *in = p;
348 return ret;
349
350memerr:
351 PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
352 DSA_free(dsa);
353 EVP_PKEY_free(ret);
354 BN_CTX_free(ctx);
355 return NULL;
356}
357
358static EVP_PKEY *
359b2i_rsa(const unsigned char **in, unsigned int length, unsigned int bitlen,
360 int ispub)
361{
362 const unsigned char *p = *in;
363 EVP_PKEY *ret = NULL;
364 RSA *rsa = NULL;
365 unsigned int nbyte, hnbyte;
366
367 nbyte = (bitlen + 7) >> 3;
368 hnbyte = (bitlen + 15) >> 4;
369 rsa = RSA_new();
370 ret = EVP_PKEY_new();
371 if (!rsa || !ret)
372 goto memerr;
373 rsa->e = BN_new();
374 if (!rsa->e)
375 goto memerr;
376 if (!BN_set_word(rsa->e, read_ledword(&p)))
377 goto memerr;
378 if (!read_lebn(&p, nbyte, &rsa->n))
379 goto memerr;
380 if (!ispub) {
381 if (!read_lebn(&p, hnbyte, &rsa->p))
382 goto memerr;
383 if (!read_lebn(&p, hnbyte, &rsa->q))
384 goto memerr;
385 if (!read_lebn(&p, hnbyte, &rsa->dmp1))
386 goto memerr;
387 if (!read_lebn(&p, hnbyte, &rsa->dmq1))
388 goto memerr;
389 if (!read_lebn(&p, hnbyte, &rsa->iqmp))
390 goto memerr;
391 if (!read_lebn(&p, nbyte, &rsa->d))
392 goto memerr;
393 }
394
395 EVP_PKEY_set1_RSA(ret, rsa);
396 RSA_free(rsa);
397 *in = p;
398 return ret;
399
400memerr:
401 PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
402 RSA_free(rsa);
403 EVP_PKEY_free(ret);
404 return NULL;
405}
406
407EVP_PKEY *
408b2i_PrivateKey(const unsigned char **in, long length)
409{
410 return do_b2i(in, length, 0);
411}
412
413EVP_PKEY *
414b2i_PublicKey(const unsigned char **in, long length)
415{
416 return do_b2i(in, length, 1);
417}
418
419EVP_PKEY *
420b2i_PrivateKey_bio(BIO *in)
421{
422 return do_b2i_bio(in, 0);
423}
424
425EVP_PKEY *
426b2i_PublicKey_bio(BIO *in)
427{
428 return do_b2i_bio(in, 1);
429}
430
431static void
432write_ledword(unsigned char **out, unsigned int dw)
433{
434 unsigned char *p = *out;
435
436 *p++ = dw & 0xff;
437 *p++ = (dw >> 8) & 0xff;
438 *p++ = (dw >> 16) & 0xff;
439 *p++ = (dw >> 24) & 0xff;
440 *out = p;
441}
442
443static void
444write_lebn(unsigned char **out, const BIGNUM *bn, int len)
445{
446 int nb, i;
447 unsigned char *p = *out, *q, c;
448
449 nb = BN_num_bytes(bn);
450 BN_bn2bin(bn, p);
451 q = p + nb - 1;
452 /* In place byte order reversal */
453 for (i = 0; i < nb / 2; i++) {
454 c = *p;
455 *p++ = *q;
456 *q-- = c;
457 }
458 *out += nb;
459 /* Pad with zeroes if we have to */
460 if (len > 0) {
461 len -= nb;
462 if (len > 0) {
463 memset(*out, 0, len);
464 *out += len;
465 }
466 }
467}
468
469
470static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
471static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
472
473static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
474static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
475
476static int
477do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
478{
479 unsigned char *p;
480 unsigned int bitlen, magic = 0, keyalg;
481 int outlen, noinc = 0;
482
483 if (pk->type == EVP_PKEY_DSA) {
484 bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic);
485 keyalg = MS_KEYALG_DSS_SIGN;
486 } else if (pk->type == EVP_PKEY_RSA) {
487 bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic);
488 keyalg = MS_KEYALG_RSA_KEYX;
489 } else
490 return -1;
491 if (bitlen == 0)
492 return -1;
493 outlen = 16 + blob_length(bitlen,
494 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
495 if (out == NULL)
496 return outlen;
497 if (*out)
498 p = *out;
499 else {
500 p = malloc(outlen);
501 if (!p)
502 return -1;
503 *out = p;
504 noinc = 1;
505 }
506 if (ispub)
507 *p++ = MS_PUBLICKEYBLOB;
508 else
509 *p++ = MS_PRIVATEKEYBLOB;
510 *p++ = 0x2;
511 *p++ = 0;
512 *p++ = 0;
513 write_ledword(&p, keyalg);
514 write_ledword(&p, magic);
515 write_ledword(&p, bitlen);
516 if (keyalg == MS_KEYALG_DSS_SIGN)
517 write_dsa(&p, pk->pkey.dsa, ispub);
518 else
519 write_rsa(&p, pk->pkey.rsa, ispub);
520 if (!noinc)
521 *out += outlen;
522 return outlen;
523}
524
525static int
526do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
527{
528 unsigned char *tmp = NULL;
529 int outlen, wrlen;
530
531 outlen = do_i2b(&tmp, pk, ispub);
532 if (outlen < 0)
533 return -1;
534 wrlen = BIO_write(out, tmp, outlen);
535 free(tmp);
536 if (wrlen == outlen)
537 return outlen;
538 return -1;
539}
540
541static int
542check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
543{
544 int bitlen;
545
546 bitlen = BN_num_bits(dsa->p);
547 if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160) ||
548 (BN_num_bits(dsa->g) > bitlen))
549 goto badkey;
550 if (ispub) {
551 if (BN_num_bits(dsa->pub_key) > bitlen)
552 goto badkey;
553 *pmagic = MS_DSS1MAGIC;
554 } else {
555 if (BN_num_bits(dsa->priv_key) > 160)
556 goto badkey;
557 *pmagic = MS_DSS2MAGIC;
558 }
559
560 return bitlen;
561
562badkey:
563 PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
564 return 0;
565}
566
567static int
568check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
569{
570 int nbyte, hnbyte, bitlen;
571
572 if (BN_num_bits(rsa->e) > 32)
573 goto badkey;
574 bitlen = BN_num_bits(rsa->n);
575 nbyte = BN_num_bytes(rsa->n);
576 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
577 if (ispub) {
578 *pmagic = MS_RSA1MAGIC;
579 return bitlen;
580 } else {
581 *pmagic = MS_RSA2MAGIC;
582 /* For private key each component must fit within nbyte or
583 * hnbyte.
584 */
585 if (BN_num_bytes(rsa->d) > nbyte)
586 goto badkey;
587 if ((BN_num_bytes(rsa->iqmp) > hnbyte) ||
588 (BN_num_bytes(rsa->p) > hnbyte) ||
589 (BN_num_bytes(rsa->q) > hnbyte) ||
590 (BN_num_bytes(rsa->dmp1) > hnbyte) ||
591 (BN_num_bytes(rsa->dmq1) > hnbyte))
592 goto badkey;
593 }
594 return bitlen;
595
596badkey:
597 PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
598 return 0;
599}
600
601static void
602write_rsa(unsigned char **out, RSA *rsa, int ispub)
603{
604 int nbyte, hnbyte;
605
606 nbyte = BN_num_bytes(rsa->n);
607 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
608 write_lebn(out, rsa->e, 4);
609 write_lebn(out, rsa->n, -1);
610 if (ispub)
611 return;
612 write_lebn(out, rsa->p, hnbyte);
613 write_lebn(out, rsa->q, hnbyte);
614 write_lebn(out, rsa->dmp1, hnbyte);
615 write_lebn(out, rsa->dmq1, hnbyte);
616 write_lebn(out, rsa->iqmp, hnbyte);
617 write_lebn(out, rsa->d, nbyte);
618}
619
620static void
621write_dsa(unsigned char **out, DSA *dsa, int ispub)
622{
623 int nbyte;
624
625 nbyte = BN_num_bytes(dsa->p);
626 write_lebn(out, dsa->p, nbyte);
627 write_lebn(out, dsa->q, 20);
628 write_lebn(out, dsa->g, nbyte);
629 if (ispub)
630 write_lebn(out, dsa->pub_key, nbyte);
631 else
632 write_lebn(out, dsa->priv_key, 20);
633 /* Set "invalid" for seed structure values */
634 memset(*out, 0xff, 24);
635 *out += 24;
636 return;
637}
638
639int
640i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
641{
642 return do_i2b_bio(out, pk, 0);
643}
644
645int
646i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
647{
648 return do_i2b_bio(out, pk, 1);
649}
650
651#ifndef OPENSSL_NO_RC4
652
653static int
654do_PVK_header(const unsigned char **in, unsigned int length, int skip_magic,
655 unsigned int *psaltlen, unsigned int *pkeylen)
656{
657 const unsigned char *p = *in;
658 unsigned int pvk_magic, is_encrypted;
659
660 if (skip_magic) {
661 if (length < 20) {
662 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
663 return 0;
664 }
665 length -= 20;
666 } else {
667 if (length < 24) {
668 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
669 return 0;
670 }
671 length -= 24;
672 pvk_magic = read_ledword(&p);
673 if (pvk_magic != MS_PVKMAGIC) {
674 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
675 return 0;
676 }
677 }
678 /* Skip reserved */
679 p += 4;
680 /*keytype = */read_ledword(&p);
681 is_encrypted = read_ledword(&p);
682 *psaltlen = read_ledword(&p);
683 *pkeylen = read_ledword(&p);
684
685 if (is_encrypted && !*psaltlen) {
686 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
687 return 0;
688 }
689
690 *in = p;
691 return 1;
692}
693
694static int
695derive_pvk_key(unsigned char *key, const unsigned char *salt,
696 unsigned int saltlen, const unsigned char *pass, int passlen)
697{
698 EVP_MD_CTX mctx;
699 int rv = 1;
700
701 EVP_MD_CTX_init(&mctx);
702 if (!EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL) ||
703 !EVP_DigestUpdate(&mctx, salt, saltlen) ||
704 !EVP_DigestUpdate(&mctx, pass, passlen) ||
705 !EVP_DigestFinal_ex(&mctx, key, NULL))
706 rv = 0;
707
708 EVP_MD_CTX_cleanup(&mctx);
709 return rv;
710}
711
712static EVP_PKEY *
713do_PVK_body(const unsigned char **in, unsigned int saltlen,
714 unsigned int keylen, pem_password_cb *cb, void *u)
715{
716 EVP_PKEY *ret = NULL;
717 const unsigned char *p = *in;
718 unsigned int magic;
719 unsigned char *enctmp = NULL, *q;
720 EVP_CIPHER_CTX cctx;
721
722 EVP_CIPHER_CTX_init(&cctx);
723 if (saltlen) {
724 char psbuf[PEM_BUFSIZE];
725 unsigned char keybuf[20];
726 int enctmplen, inlen;
727
728 if (cb)
729 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
730 else
731 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
732 if (inlen <= 0) {
733 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
734 return NULL;
735 }
736 enctmp = malloc(keylen + 8);
737 if (!enctmp) {
738 PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
739 return NULL;
740 }
741 if (!derive_pvk_key(keybuf, p, saltlen, (unsigned char *)psbuf,
742 inlen)) {
743 free(enctmp);
744 return NULL;
745 }
746 p += saltlen;
747 /* Copy BLOBHEADER across, decrypt rest */
748 memcpy(enctmp, p, 8);
749 p += 8;
750 if (keylen < 8) {
751 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
752 free(enctmp);
753 return NULL;
754 }
755 inlen = keylen - 8;
756 q = enctmp + 8;
757 if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
758 goto err;
759 if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
760 goto err;
761 if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
762 goto err;
763 magic = read_ledword((const unsigned char **)&q);
764 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
765 q = enctmp + 8;
766 memset(keybuf + 5, 0, 11);
767 if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf,
768 NULL))
769 goto err;
770 OPENSSL_cleanse(keybuf, 20);
771 if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
772 goto err;
773 if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen,
774 &enctmplen))
775 goto err;
776 magic = read_ledword((const unsigned char **)&q);
777 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
778 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
779 goto err;
780 }
781 } else
782 OPENSSL_cleanse(keybuf, 20);
783 p = enctmp;
784 }
785
786 ret = b2i_PrivateKey(&p, keylen);
787
788err:
789 EVP_CIPHER_CTX_cleanup(&cctx);
790 if (enctmp && saltlen)
791 free(enctmp);
792 return ret;
793}
794
795
796EVP_PKEY *
797b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
798{
799 unsigned char pvk_hdr[24], *buf = NULL;
800 const unsigned char *p;
801 int buflen;
802 EVP_PKEY *ret = NULL;
803 unsigned int saltlen, keylen;
804
805 if (BIO_read(in, pvk_hdr, 24) != 24) {
806 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
807 return NULL;
808 }
809 p = pvk_hdr;
810
811 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
812 return 0;
813 buflen = (int) keylen + saltlen;
814 buf = malloc(buflen);
815 if (!buf) {
816 PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
817 return 0;
818 }
819 p = buf;
820 if (BIO_read(in, buf, buflen) != buflen) {
821 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
822 goto err;
823 }
824 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
825
826err:
827 if (buf) {
828 OPENSSL_cleanse(buf, buflen);
829 free(buf);
830 }
831 return ret;
832}
833
834static int
835i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel, pem_password_cb *cb,
836 void *u)
837{
838 int outlen = 24, pklen;
839 unsigned char *p, *salt = NULL;
840 EVP_CIPHER_CTX cctx;
841
842 EVP_CIPHER_CTX_init(&cctx);
843 if (enclevel)
844 outlen += PVK_SALTLEN;
845 pklen = do_i2b(NULL, pk, 0);
846 if (pklen < 0)
847 return -1;
848 outlen += pklen;
849 if (!out)
850 return outlen;
851 if (*out)
852 p = *out;
853 else {
854 p = malloc(outlen);
855 if (!p) {
856 PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
857 return -1;
858 }
859 *out = p;
860 }
861
862 write_ledword(&p, MS_PVKMAGIC);
863 write_ledword(&p, 0);
864 if (pk->type == EVP_PKEY_DSA)
865 write_ledword(&p, MS_KEYTYPE_SIGN);
866 else
867 write_ledword(&p, MS_KEYTYPE_KEYX);
868 write_ledword(&p, enclevel ? 1 : 0);
869 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
870 write_ledword(&p, pklen);
871 if (enclevel) {
872 arc4random_buf(p, PVK_SALTLEN);
873 salt = p;
874 p += PVK_SALTLEN;
875 }
876 do_i2b(&p, pk, 0);
877 if (enclevel == 0)
878 return outlen;
879 else {
880 char psbuf[PEM_BUFSIZE];
881 unsigned char keybuf[20];
882 int enctmplen, inlen;
883 if (cb)
884 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
885 else
886 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
887 if (inlen <= 0) {
888 PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
889 goto error;
890 }
891 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
892 (unsigned char *)psbuf, inlen))
893 goto error;
894 if (enclevel == 1)
895 memset(keybuf + 5, 0, 11);
896 p = salt + PVK_SALTLEN + 8;
897 if (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
898 goto error;
899 OPENSSL_cleanse(keybuf, 20);
900 if (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8))
901 goto error;
902 if (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen))
903 goto error;
904 }
905 EVP_CIPHER_CTX_cleanup(&cctx);
906 return outlen;
907
908error:
909 EVP_CIPHER_CTX_cleanup(&cctx);
910 return -1;
911}
912
913int
914i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel, pem_password_cb *cb, void *u)
915{
916 unsigned char *tmp = NULL;
917 int outlen, wrlen;
918
919 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
920 if (outlen < 0)
921 return -1;
922 wrlen = BIO_write(out, tmp, outlen);
923 free(tmp);
924 if (wrlen == outlen) {
925 PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);
926 return outlen;
927 }
928 return -1;
929}
930
931#endif
932
933#endif