summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/pem
diff options
context:
space:
mode:
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.c872
-rw-r--r--src/lib/libcrypto/pem/pem_oth.c87
-rw-r--r--src/lib/libcrypto/pem/pem_pk8.c256
-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.c939
16 files changed, 0 insertions, 4443 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 191e3b5b10..0000000000
--- a/src/lib/libcrypto/pem/pem_info.c
+++ /dev/null
@@ -1,406 +0,0 @@
1/* $OpenBSD: pem_info.c,v 1.21 2015/09/10 15:56:25 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>
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 explicit_bzero((char *)&ctx, sizeof(ctx));
404 explicit_bzero(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 852b0eaf86..0000000000
--- a/src/lib/libcrypto/pem/pem_lib.c
+++ /dev/null
@@ -1,872 +0,0 @@
1/* $OpenBSD: pem_lib.c,v 1.42 2015/09/10 15:56:25 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 <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 (num < 0)
98 return -1;
99
100 if (key) {
101 l = strlen(key);
102 if (l > (size_t)num)
103 l = (size_t)num;
104 memcpy(buf, key, l);
105 return (int)l;
106 }
107
108 prompt = EVP_get_pw_prompt();
109 if (prompt == NULL)
110 prompt = "Enter PEM pass phrase:";
111
112 for (;;) {
113 i = EVP_read_pw_string_min(buf, MIN_LENGTH, num, prompt, w);
114 if (i != 0) {
115 PEMerr(PEM_F_PEM_DEF_CALLBACK,
116 PEM_R_PROBLEMS_GETTING_PASSWORD);
117 memset(buf, 0, num);
118 return (-1);
119 }
120 l = strlen(buf);
121 if (l < MIN_LENGTH) {
122 fprintf(stderr, "phrase is too short, "
123 "needs to be at least %zu chars\n",
124 (size_t)MIN_LENGTH);
125 } else
126 break;
127 }
128 return (int)l;
129}
130
131void
132PEM_proc_type(char *buf, int type)
133{
134 const char *str;
135
136 if (type == PEM_TYPE_ENCRYPTED)
137 str = "ENCRYPTED";
138 else if (type == PEM_TYPE_MIC_CLEAR)
139 str = "MIC-CLEAR";
140 else if (type == PEM_TYPE_MIC_ONLY)
141 str = "MIC-ONLY";
142 else
143 str = "BAD-TYPE";
144
145 strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE);
146 strlcat(buf, str, PEM_BUFSIZE);
147 strlcat(buf, "\n", PEM_BUFSIZE);
148}
149
150void
151PEM_dek_info(char *buf, const char *type, int len, char *str)
152{
153 static const unsigned char map[17] = "0123456789ABCDEF";
154 long i;
155 int j;
156
157 strlcat(buf, "DEK-Info: ", PEM_BUFSIZE);
158 strlcat(buf, type, PEM_BUFSIZE);
159 strlcat(buf, ",", PEM_BUFSIZE);
160 j = strlen(buf);
161 if (j + (len * 2) + 1 > PEM_BUFSIZE)
162 return;
163 for (i = 0; i < len; i++) {
164 buf[j + i * 2] = map[(str[i] >> 4) & 0x0f];
165 buf[j + i * 2 + 1] = map[(str[i]) & 0x0f];
166 }
167 buf[j + i * 2] = '\n';
168 buf[j + i * 2 + 1] = '\0';
169}
170
171void *
172PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
173 pem_password_cb *cb, void *u)
174{
175 BIO *b;
176 void *ret;
177
178 if ((b = BIO_new(BIO_s_file())) == NULL) {
179 PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB);
180 return (0);
181 }
182 BIO_set_fp(b, fp, BIO_NOCLOSE);
183 ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u);
184 BIO_free(b);
185 return (ret);
186}
187
188static int
189check_pem(const char *nm, const char *name)
190{
191 /* Normal matching nm and name */
192 if (!strcmp(nm, name))
193 return 1;
194
195 /* Make PEM_STRING_EVP_PKEY match any private key */
196
197 if (!strcmp(name, PEM_STRING_EVP_PKEY)) {
198 int slen;
199 const EVP_PKEY_ASN1_METHOD *ameth;
200 if (!strcmp(nm, PEM_STRING_PKCS8))
201 return 1;
202 if (!strcmp(nm, PEM_STRING_PKCS8INF))
203 return 1;
204 slen = pem_check_suffix(nm, "PRIVATE KEY");
205 if (slen > 0) {
206 /* NB: ENGINE implementations wont contain
207 * a deprecated old private key decode function
208 * so don't look for them.
209 */
210 ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
211 if (ameth && ameth->old_priv_decode)
212 return 1;
213 }
214 return 0;
215 }
216
217 if (!strcmp(name, PEM_STRING_PARAMETERS)) {
218 int slen;
219 const EVP_PKEY_ASN1_METHOD *ameth;
220 slen = pem_check_suffix(nm, "PARAMETERS");
221 if (slen > 0) {
222 ENGINE *e;
223 ameth = EVP_PKEY_asn1_find_str(&e, nm, slen);
224 if (ameth) {
225 int r;
226 if (ameth->param_decode)
227 r = 1;
228 else
229 r = 0;
230#ifndef OPENSSL_NO_ENGINE
231 if (e)
232 ENGINE_finish(e);
233#endif
234 return r;
235 }
236 }
237 return 0;
238 }
239
240 /* Permit older strings */
241
242 if (!strcmp(nm, PEM_STRING_X509_OLD) &&
243 !strcmp(name, PEM_STRING_X509))
244 return 1;
245
246 if (!strcmp(nm, PEM_STRING_X509_REQ_OLD) &&
247 !strcmp(name, PEM_STRING_X509_REQ))
248 return 1;
249
250 /* Allow normal certs to be read as trusted certs */
251 if (!strcmp(nm, PEM_STRING_X509) &&
252 !strcmp(name, PEM_STRING_X509_TRUSTED))
253 return 1;
254
255 if (!strcmp(nm, PEM_STRING_X509_OLD) &&
256 !strcmp(name, PEM_STRING_X509_TRUSTED))
257 return 1;
258
259 /* Some CAs use PKCS#7 with CERTIFICATE headers */
260 if (!strcmp(nm, PEM_STRING_X509) &&
261 !strcmp(name, PEM_STRING_PKCS7))
262 return 1;
263
264 if (!strcmp(nm, PEM_STRING_PKCS7_SIGNED) &&
265 !strcmp(name, PEM_STRING_PKCS7))
266 return 1;
267
268#ifndef OPENSSL_NO_CMS
269 if (!strcmp(nm, PEM_STRING_X509) &&
270 !strcmp(name, PEM_STRING_CMS))
271 return 1;
272 /* Allow CMS to be read from PKCS#7 headers */
273 if (!strcmp(nm, PEM_STRING_PKCS7) &&
274 !strcmp(name, PEM_STRING_CMS))
275 return 1;
276#endif
277
278 return 0;
279}
280
281int
282PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
283 const char *name, BIO *bp, pem_password_cb *cb, void *u)
284{
285 EVP_CIPHER_INFO cipher;
286 char *nm = NULL, *header = NULL;
287 unsigned char *data = NULL;
288 long len;
289 int ret = 0;
290
291 for (;;) {
292 if (!PEM_read_bio(bp, &nm, &header, &data, &len)) {
293 if (ERR_GET_REASON(ERR_peek_error()) ==
294 PEM_R_NO_START_LINE)
295 ERR_asprintf_error_data("Expecting: %s", name);
296 return 0;
297 }
298 if (check_pem(nm, name))
299 break;
300 free(nm);
301 free(header);
302 free(data);
303 }
304 if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
305 goto err;
306 if (!PEM_do_header(&cipher, data, &len, cb, u))
307 goto err;
308
309 *pdata = data;
310 *plen = len;
311
312 if (pnm)
313 *pnm = nm;
314
315 ret = 1;
316
317err:
318 if (!ret || !pnm)
319 free(nm);
320 free(header);
321 if (!ret)
322 free(data);
323 return ret;
324}
325
326int
327PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x,
328 const EVP_CIPHER *enc, unsigned char *kstr, int klen,
329 pem_password_cb *callback, void *u)
330{
331 BIO *b;
332 int ret;
333
334 if ((b = BIO_new(BIO_s_file())) == NULL) {
335 PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB);
336 return (0);
337 }
338 BIO_set_fp(b, fp, BIO_NOCLOSE);
339 ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u);
340 BIO_free(b);
341 return (ret);
342}
343
344int
345PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x,
346 const EVP_CIPHER *enc, unsigned char *kstr, int klen,
347 pem_password_cb *callback, void *u)
348{
349 EVP_CIPHER_CTX ctx;
350 int dsize = 0, i, j, ret = 0;
351 unsigned char *p, *data = NULL;
352 const char *objstr = NULL;
353 char buf[PEM_BUFSIZE];
354 unsigned char key[EVP_MAX_KEY_LENGTH];
355 unsigned char iv[EVP_MAX_IV_LENGTH];
356
357 if (enc != NULL) {
358 objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
359 if (objstr == NULL) {
360 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,
361 PEM_R_UNSUPPORTED_CIPHER);
362 goto err;
363 }
364 }
365
366 if ((dsize = i2d(x, NULL)) < 0) {
367 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_ASN1_LIB);
368 dsize = 0;
369 goto err;
370 }
371 /* dzise + 8 bytes are needed */
372 /* actually it needs the cipher block size extra... */
373 data = malloc(dsize + 20);
374 if (data == NULL) {
375 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_MALLOC_FAILURE);
376 goto err;
377 }
378 p = data;
379 i = i2d(x, &p);
380
381 if (enc != NULL) {
382 if (kstr == NULL) {
383 if (callback == NULL)
384 klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
385 else
386 klen = (*callback)(buf, PEM_BUFSIZE, 1, u);
387 if (klen <= 0) {
388 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,
389 PEM_R_READ_KEY);
390 goto err;
391 }
392 kstr = (unsigned char *)buf;
393 }
394 if ((size_t)enc->iv_len > sizeof(iv)) {
395 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, EVP_R_IV_TOO_LARGE);
396 goto err;
397 }
398 arc4random_buf(iv, enc->iv_len); /* Generate a salt */
399 /* The 'iv' is used as the iv and as a salt. It is
400 * NOT taken from the BytesToKey function */
401 if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1,
402 key, NULL))
403 goto err;
404
405 if (kstr == (unsigned char *)buf)
406 explicit_bzero(buf, PEM_BUFSIZE);
407
408 if (strlen(objstr) + 23 + 2 * enc->iv_len + 13 > sizeof buf) {
409 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,
410 ASN1_R_BUFFER_TOO_SMALL);
411 goto err;
412 }
413
414 buf[0] = '\0';
415 PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
416 PEM_dek_info(buf, objstr, enc->iv_len, (char *)iv);
417 /* k=strlen(buf); */
418
419 EVP_CIPHER_CTX_init(&ctx);
420 ret = 1;
421 if (!EVP_EncryptInit_ex(&ctx, enc, NULL, key, iv) ||
422 !EVP_EncryptUpdate(&ctx, data, &j, data, i) ||
423 !EVP_EncryptFinal_ex(&ctx, &(data[j]), &i))
424 ret = 0;
425 EVP_CIPHER_CTX_cleanup(&ctx);
426 if (ret == 0)
427 goto err;
428 i += j;
429 } else {
430 ret = 1;
431 buf[0] = '\0';
432 }
433 i = PEM_write_bio(bp, name, buf, data, i);
434 if (i <= 0)
435 ret = 0;
436err:
437 explicit_bzero(key, sizeof(key));
438 explicit_bzero(iv, sizeof(iv));
439 explicit_bzero((char *)&ctx, sizeof(ctx));
440 explicit_bzero(buf, PEM_BUFSIZE);
441 if (data != NULL) {
442 explicit_bzero(data, (unsigned int)dsize);
443 free(data);
444 }
445 return (ret);
446}
447
448int
449PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
450 pem_password_cb *callback, void *u)
451{
452 int i, j, o, klen;
453 long len;
454 EVP_CIPHER_CTX ctx;
455 unsigned char key[EVP_MAX_KEY_LENGTH];
456 char buf[PEM_BUFSIZE];
457
458 len = *plen;
459
460 if (cipher->cipher == NULL)
461 return (1);
462 if (callback == NULL)
463 klen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u);
464 else
465 klen = callback(buf, PEM_BUFSIZE, 0, u);
466 if (klen <= 0) {
467 PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ);
468 return (0);
469 }
470 if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]),
471 (unsigned char *)buf, klen, 1, key, NULL))
472 return 0;
473
474 j = (int)len;
475 EVP_CIPHER_CTX_init(&ctx);
476 o = EVP_DecryptInit_ex(&ctx, cipher->cipher, NULL, key,
477 &(cipher->iv[0]));
478 if (o)
479 o = EVP_DecryptUpdate(&ctx, data, &i, data, j);
480 if (o)
481 o = EVP_DecryptFinal_ex(&ctx, &(data[i]), &j);
482 EVP_CIPHER_CTX_cleanup(&ctx);
483 explicit_bzero((char *)buf, sizeof(buf));
484 explicit_bzero((char *)key, sizeof(key));
485 if (!o) {
486 PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT);
487 return (0);
488 }
489 *plen = j + i;
490 return (1);
491}
492
493int
494PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
495{
496 const EVP_CIPHER *enc = NULL;
497 char *p, c;
498 char **header_pp = &header;
499
500 cipher->cipher = NULL;
501 if ((header == NULL) || (*header == '\0') || (*header == '\n'))
502 return (1);
503 if (strncmp(header, "Proc-Type: ", 11) != 0) {
504 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE);
505 return (0);
506 }
507 header += 11;
508 if (*header != '4')
509 return (0);
510 header++;
511 if (*header != ',')
512 return (0);
513 header++;
514 if (strncmp(header, "ENCRYPTED", 9) != 0) {
515 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED);
516 return (0);
517 }
518 for (; (*header != '\n') && (*header != '\0'); header++)
519 ;
520 if (*header == '\0') {
521 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER);
522 return (0);
523 }
524 header++;
525 if (strncmp(header, "DEK-Info: ", 10) != 0) {
526 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO);
527 return (0);
528 }
529 header += 10;
530
531 p = header;
532 for (;;) {
533 c= *header;
534 if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') ||
535 ((c >= '0') && (c <= '9'))))
536 break;
537 header++;
538 }
539 *header = '\0';
540 cipher->cipher = enc = EVP_get_cipherbyname(p);
541 *header = c;
542 header++;
543
544 if (enc == NULL) {
545 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,
546 PEM_R_UNSUPPORTED_ENCRYPTION);
547 return (0);
548 }
549 if (!load_iv(header_pp, &(cipher->iv[0]), enc->iv_len))
550 return (0);
551
552 return (1);
553}
554
555static int
556load_iv(char **fromp, unsigned char *to, int num)
557{
558 int v, i;
559 char *from;
560
561 from= *fromp;
562 for (i = 0; i < num; i++)
563 to[i] = 0;
564 num *= 2;
565 for (i = 0; i < num; i++) {
566 if ((*from >= '0') && (*from <= '9'))
567 v = *from - '0';
568 else if ((*from >= 'A') && (*from <= 'F'))
569 v = *from - 'A' + 10;
570 else if ((*from >= 'a') && (*from <= 'f'))
571 v = *from - 'a' + 10;
572 else {
573 PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS);
574 return (0);
575 }
576 from++;
577 to[i / 2] |= v << (long)((!(i & 1)) * 4);
578 }
579
580 *fromp = from;
581 return (1);
582}
583
584int
585PEM_write(FILE *fp, char *name, char *header, unsigned char *data, long len)
586{
587 BIO *b;
588 int ret;
589
590 if ((b = BIO_new(BIO_s_file())) == NULL) {
591 PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB);
592 return (0);
593 }
594 BIO_set_fp(b, fp, BIO_NOCLOSE);
595 ret = PEM_write_bio(b, name, header, data, len);
596 BIO_free(b);
597 return (ret);
598}
599
600int
601PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data,
602 long len)
603{
604 int nlen, n, i, j, outl;
605 unsigned char *buf = NULL;
606 EVP_ENCODE_CTX ctx;
607 int reason = ERR_R_BUF_LIB;
608
609 EVP_EncodeInit(&ctx);
610 nlen = strlen(name);
611
612 if ((BIO_write(bp, "-----BEGIN ", 11) != 11) ||
613 (BIO_write(bp, name, nlen) != nlen) ||
614 (BIO_write(bp, "-----\n", 6) != 6))
615 goto err;
616
617 i = strlen(header);
618 if (i > 0) {
619 if ((BIO_write(bp, header, i) != i) ||
620 (BIO_write(bp, "\n", 1) != 1))
621 goto err;
622 }
623
624 buf = reallocarray(NULL, PEM_BUFSIZE, 8);
625 if (buf == NULL) {
626 reason = ERR_R_MALLOC_FAILURE;
627 goto err;
628 }
629
630 i = j = 0;
631 while (len > 0) {
632 n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
633 EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n);
634 if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl))
635 goto err;
636 i += outl;
637 len -= n;
638 j += n;
639 }
640 EVP_EncodeFinal(&ctx, buf, &outl);
641 if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
642 goto err;
643 explicit_bzero(buf, PEM_BUFSIZE * 8);
644 free(buf);
645 buf = NULL;
646 if ((BIO_write(bp, "-----END ", 9) != 9) ||
647 (BIO_write(bp, name, nlen) != nlen) ||
648 (BIO_write(bp, "-----\n", 6) != 6))
649 goto err;
650 return (i + outl);
651
652err:
653 if (buf) {
654 explicit_bzero(buf, PEM_BUFSIZE * 8);
655 free(buf);
656 }
657 PEMerr(PEM_F_PEM_WRITE_BIO, reason);
658 return (0);
659}
660
661int
662PEM_read(FILE *fp, char **name, char **header, unsigned char **data, long *len)
663{
664 BIO *b;
665 int ret;
666
667 if ((b = BIO_new(BIO_s_file())) == NULL) {
668 PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB);
669 return (0);
670 }
671 BIO_set_fp(b, fp, BIO_NOCLOSE);
672 ret = PEM_read_bio(b, name, header, data, len);
673 BIO_free(b);
674 return (ret);
675}
676
677int
678PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
679 long *len)
680{
681 EVP_ENCODE_CTX ctx;
682 int end = 0, i, k, bl = 0, hl = 0, nohead = 0;
683 char buf[256];
684 BUF_MEM *nameB;
685 BUF_MEM *headerB;
686 BUF_MEM *dataB, *tmpB;
687
688 nameB = BUF_MEM_new();
689 headerB = BUF_MEM_new();
690 dataB = BUF_MEM_new();
691 if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) {
692 BUF_MEM_free(nameB);
693 BUF_MEM_free(headerB);
694 BUF_MEM_free(dataB);
695 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
696 return (0);
697 }
698
699 buf[254] = '\0';
700 for (;;) {
701 i = BIO_gets(bp, buf, 254);
702
703 if (i <= 0) {
704 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE);
705 goto err;
706 }
707
708 while ((i >= 0) && (buf[i] <= ' '))
709 i--;
710 buf[++i] = '\n';
711 buf[++i] = '\0';
712
713 if (strncmp(buf, "-----BEGIN ", 11) == 0) {
714 i = strlen(&(buf[11]));
715
716 if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0)
717 continue;
718 if (!BUF_MEM_grow(nameB, i + 9)) {
719 PEMerr(PEM_F_PEM_READ_BIO,
720 ERR_R_MALLOC_FAILURE);
721 goto err;
722 }
723 memcpy(nameB->data, &(buf[11]), i - 6);
724 nameB->data[i - 6] = '\0';
725 break;
726 }
727 }
728 hl = 0;
729 if (!BUF_MEM_grow(headerB, 256)) {
730 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
731 goto err;
732 }
733 headerB->data[0] = '\0';
734 for (;;) {
735 i = BIO_gets(bp, buf, 254);
736 if (i <= 0)
737 break;
738
739 while ((i >= 0) && (buf[i] <= ' '))
740 i--;
741 buf[++i] = '\n';
742 buf[++i] = '\0';
743
744 if (buf[0] == '\n')
745 break;
746 if (!BUF_MEM_grow(headerB, hl + i + 9)) {
747 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
748 goto err;
749 }
750 if (strncmp(buf, "-----END ", 9) == 0) {
751 nohead = 1;
752 break;
753 }
754 memcpy(&(headerB->data[hl]), buf, i);
755 headerB->data[hl + i] = '\0';
756 hl += i;
757 }
758
759 bl = 0;
760 if (!BUF_MEM_grow(dataB, 1024)) {
761 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
762 goto err;
763 }
764 dataB->data[0] = '\0';
765 if (!nohead) {
766 for (;;) {
767 i = BIO_gets(bp, buf, 254);
768 if (i <= 0)
769 break;
770
771 while ((i >= 0) && (buf[i] <= ' '))
772 i--;
773 buf[++i] = '\n';
774 buf[++i] = '\0';
775
776 if (i != 65)
777 end = 1;
778 if (strncmp(buf, "-----END ", 9) == 0)
779 break;
780 if (i > 65)
781 break;
782 if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) {
783 PEMerr(PEM_F_PEM_READ_BIO,
784 ERR_R_MALLOC_FAILURE);
785 goto err;
786 }
787 memcpy(&(dataB->data[bl]), buf, i);
788 dataB->data[bl + i] = '\0';
789 bl += i;
790 if (end) {
791 buf[0] = '\0';
792 i = BIO_gets(bp, buf, 254);
793 if (i <= 0)
794 break;
795
796 while ((i >= 0) && (buf[i] <= ' '))
797 i--;
798 buf[++i] = '\n';
799 buf[++i] = '\0';
800
801 break;
802 }
803 }
804 } else {
805 tmpB = headerB;
806 headerB = dataB;
807 dataB = tmpB;
808 bl = hl;
809 }
810 i = strlen(nameB->data);
811 if ((strncmp(buf, "-----END ", 9) != 0) ||
812 (strncmp(nameB->data, &(buf[9]), i) != 0) ||
813 (strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) {
814 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE);
815 goto err;
816 }
817
818 EVP_DecodeInit(&ctx);
819 i = EVP_DecodeUpdate(&ctx,
820 (unsigned char *)dataB->data, &bl,
821 (unsigned char *)dataB->data, bl);
822 if (i < 0) {
823 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
824 goto err;
825 }
826 i = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k);
827 if (i < 0) {
828 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
829 goto err;
830 }
831 bl += k;
832
833 if (bl == 0)
834 goto err;
835 *name = nameB->data;
836 *header = headerB->data;
837 *data = (unsigned char *)dataB->data;
838 *len = bl;
839 free(nameB);
840 free(headerB);
841 free(dataB);
842 return (1);
843
844err:
845 BUF_MEM_free(nameB);
846 BUF_MEM_free(headerB);
847 BUF_MEM_free(dataB);
848 return (0);
849}
850
851/* Check pem string and return prefix length.
852 * If for example the pem_str == "RSA PRIVATE KEY" and suffix = "PRIVATE KEY"
853 * the return value is 3 for the string "RSA".
854 */
855
856int
857pem_check_suffix(const char *pem_str, const char *suffix)
858{
859 int pem_len = strlen(pem_str);
860 int suffix_len = strlen(suffix);
861 const char *p;
862
863 if (suffix_len + 1 >= pem_len)
864 return 0;
865 p = pem_str + pem_len - suffix_len;
866 if (strcmp(p, suffix))
867 return 0;
868 p--;
869 if (*p != ' ')
870 return 0;
871 return p - pem_str;
872}
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 d02dec1546..0000000000
--- a/src/lib/libcrypto/pem/pem_pk8.c
+++ /dev/null
@@ -1,256 +0,0 @@
1/* $OpenBSD: pem_pk8.c,v 1.10 2015/09/10 15:56:25 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/buffer.h>
63#include <openssl/err.h>
64#include <openssl/evp.h>
65#include <openssl/objects.h>
66#include <openssl/pem.h>
67#include <openssl/pkcs12.h>
68#include <openssl/x509.h>
69
70static int do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder, int nid,
71 const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u);
72static int do_pk8pkey_fp(FILE *bp, EVP_PKEY *x, int isder, int nid,
73 const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u);
74
75/* These functions write a private key in PKCS#8 format: it is a "drop in"
76 * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
77 * is NULL then it uses the unencrypted private key form. The 'nid' versions
78 * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
79 */
80
81int
82PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid, char *kstr,
83 int klen, pem_password_cb *cb, void *u)
84{
85 return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
86}
87
88int
89PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
90 char *kstr, int klen, pem_password_cb *cb, void *u)
91{
92 return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
93}
94
95int
96i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
97 char *kstr, int klen, pem_password_cb *cb, void *u)
98{
99 return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
100}
101
102int
103i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
104 char *kstr, int klen, pem_password_cb *cb, void *u)
105{
106 return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
107}
108
109static int
110do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder, int nid, const EVP_CIPHER *enc,
111 char *kstr, int klen, pem_password_cb *cb, void *u)
112{
113 X509_SIG *p8;
114 PKCS8_PRIV_KEY_INFO *p8inf;
115 char buf[PEM_BUFSIZE];
116 int ret;
117
118 if (!(p8inf = EVP_PKEY2PKCS8(x))) {
119 PEMerr(PEM_F_DO_PK8PKEY,
120 PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
121 return 0;
122 }
123 if (enc || (nid != -1)) {
124 if (!kstr) {
125 if (!cb)
126 klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
127 else
128 klen = cb(buf, PEM_BUFSIZE, 1, u);
129 if (klen <= 0) {
130 PEMerr(PEM_F_DO_PK8PKEY, PEM_R_READ_KEY);
131 PKCS8_PRIV_KEY_INFO_free(p8inf);
132 return 0;
133 }
134
135 kstr = buf;
136 }
137 p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
138 if (kstr == buf)
139 explicit_bzero(buf, klen);
140 PKCS8_PRIV_KEY_INFO_free(p8inf);
141 if (isder)
142 ret = i2d_PKCS8_bio(bp, p8);
143 else
144 ret = PEM_write_bio_PKCS8(bp, p8);
145 X509_SIG_free(p8);
146 return ret;
147 } else {
148 if (isder)
149 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
150 else
151 ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
152 PKCS8_PRIV_KEY_INFO_free(p8inf);
153 return ret;
154 }
155}
156
157EVP_PKEY *
158d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)
159{
160 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
161 X509_SIG *p8 = NULL;
162 int klen;
163 EVP_PKEY *ret;
164 char psbuf[PEM_BUFSIZE];
165
166 p8 = d2i_PKCS8_bio(bp, NULL);
167 if (!p8)
168 return NULL;
169 if (cb)
170 klen = cb(psbuf, PEM_BUFSIZE, 0, u);
171 else
172 klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
173 if (klen <= 0) {
174 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ);
175 X509_SIG_free(p8);
176 return NULL;
177 }
178 p8inf = PKCS8_decrypt(p8, psbuf, klen);
179 X509_SIG_free(p8);
180 if (!p8inf)
181 return NULL;
182 ret = EVP_PKCS82PKEY(p8inf);
183 PKCS8_PRIV_KEY_INFO_free(p8inf);
184 if (!ret)
185 return NULL;
186 if (x) {
187 EVP_PKEY_free(*x);
188 *x = ret;
189 }
190 return ret;
191}
192
193
194int
195i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
196 char *kstr, int klen, pem_password_cb *cb, void *u)
197{
198 return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
199}
200
201int
202i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid, char *kstr,
203 int klen, pem_password_cb *cb, void *u)
204{
205 return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
206}
207
208int
209PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid, char *kstr,
210 int klen, pem_password_cb *cb, void *u)
211{
212 return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
213}
214
215int
216PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
217 char *kstr, int klen, pem_password_cb *cb, void *u)
218{
219 return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
220}
221
222static int
223do_pk8pkey_fp(FILE *fp, EVP_PKEY *x, int isder, int nid, const EVP_CIPHER *enc,
224 char *kstr, int klen, pem_password_cb *cb, void *u)
225{
226 BIO *bp;
227 int ret;
228
229 if (!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) {
230 PEMerr(PEM_F_DO_PK8PKEY_FP, ERR_R_BUF_LIB);
231 return (0);
232 }
233 ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
234 BIO_free(bp);
235 return ret;
236}
237
238EVP_PKEY *
239d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
240{
241 BIO *bp;
242 EVP_PKEY *ret;
243
244 if (!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) {
245 PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP, ERR_R_BUF_LIB);
246 return NULL;
247 }
248 ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
249 BIO_free(bp);
250 return ret;
251}
252
253
254IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
255IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,
256 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 afb476f818..0000000000
--- a/src/lib/libcrypto/pem/pem_pkey.c
+++ /dev/null
@@ -1,253 +0,0 @@
1/* $OpenBSD: pem_pkey.c,v 1.21 2015/09/10 15:56:25 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>
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 explicit_bzero(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 96687eb77f..0000000000
--- a/src/lib/libcrypto/pem/pem_seal.c
+++ /dev/null
@@ -1,190 +0,0 @@
1/* $OpenBSD: pem_seal.c,v 1.22 2015/09/10 15:56:25 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 explicit_bzero(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 7a9045396c..0000000000
--- a/src/lib/libcrypto/pem/pvkfmt.c
+++ /dev/null
@@ -1,939 +0,0 @@
1/* $OpenBSD: pvkfmt.c,v 1.16 2016/03/02 14:28:14 beck 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 if (*pbitlen > 65536) {
183 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_INCONSISTENT_HEADER);
184 return 0;
185 }
186 *pisdss = 0;
187 switch (*pmagic) {
188
189 case MS_DSS1MAGIC:
190 *pisdss = 1;
191 case MS_RSA1MAGIC:
192 if (*pispub == 0) {
193 PEMerr(PEM_F_DO_BLOB_HEADER,
194 PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
195 return 0;
196 }
197 break;
198
199 case MS_DSS2MAGIC:
200 *pisdss = 1;
201 case MS_RSA2MAGIC:
202 if (*pispub == 1) {
203 PEMerr(PEM_F_DO_BLOB_HEADER,
204 PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
205 return 0;
206 }
207 break;
208
209 default:
210 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
211 return -1;
212 }
213 *in = p;
214 return 1;
215}
216
217static unsigned int
218blob_length(unsigned bitlen, int isdss, int ispub)
219{
220 unsigned int nbyte, hnbyte;
221
222 nbyte = (bitlen + 7) >> 3;
223 hnbyte = (bitlen + 15) >> 4;
224 if (isdss) {
225
226 /* Expected length: 20 for q + 3 components bitlen each + 24
227 * for seed structure.
228 */
229 if (ispub)
230 return 44 + 3 * nbyte;
231 /* Expected length: 20 for q, priv, 2 bitlen components + 24
232 * for seed structure.
233 */
234 else
235 return 64 + 2 * nbyte;
236 } else {
237 /* Expected length: 4 for 'e' + 'n' */
238 if (ispub)
239 return 4 + nbyte;
240 else
241 /* Expected length: 4 for 'e' and 7 other components.
242 * 2 components are bitlen size, 5 are bitlen/2
243 */
244 return 4 + 2*nbyte + 5*hnbyte;
245 }
246
247}
248
249static EVP_PKEY *
250do_b2i(const unsigned char **in, unsigned int length, int ispub)
251{
252 const unsigned char *p = *in;
253 unsigned int bitlen, magic;
254 int isdss;
255
256 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
257 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
258 return NULL;
259 }
260 length -= 16;
261 if (length < blob_length(bitlen, isdss, ispub)) {
262 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
263 return NULL;
264 }
265 if (isdss)
266 return b2i_dss(&p, length, bitlen, ispub);
267 else
268 return b2i_rsa(&p, length, bitlen, ispub);
269}
270
271static EVP_PKEY *
272do_b2i_bio(BIO *in, int ispub)
273{
274 const unsigned char *p;
275 unsigned char hdr_buf[16], *buf = NULL;
276 unsigned int bitlen, magic, length;
277 int isdss;
278 EVP_PKEY *ret = NULL;
279
280 if (BIO_read(in, hdr_buf, 16) != 16) {
281 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
282 return NULL;
283 }
284 p = hdr_buf;
285 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
286 return NULL;
287
288 length = blob_length(bitlen, isdss, ispub);
289 buf = malloc(length);
290 if (!buf) {
291 PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
292 goto err;
293 }
294 p = buf;
295 if (BIO_read(in, buf, length) != (int)length) {
296 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
297 goto err;
298 }
299
300 if (isdss)
301 ret = b2i_dss(&p, length, bitlen, ispub);
302 else
303 ret = b2i_rsa(&p, length, bitlen, ispub);
304
305err:
306 free(buf);
307 return ret;
308}
309
310static EVP_PKEY *
311b2i_dss(const unsigned char **in, unsigned int length, unsigned int bitlen,
312 int ispub)
313{
314 const unsigned char *p = *in;
315 EVP_PKEY *ret = NULL;
316 DSA *dsa = NULL;
317 BN_CTX *ctx = NULL;
318 unsigned int nbyte;
319
320 nbyte = (bitlen + 7) >> 3;
321
322 dsa = DSA_new();
323 ret = EVP_PKEY_new();
324 if (!dsa || !ret)
325 goto memerr;
326 if (!read_lebn(&p, nbyte, &dsa->p))
327 goto memerr;
328 if (!read_lebn(&p, 20, &dsa->q))
329 goto memerr;
330 if (!read_lebn(&p, nbyte, &dsa->g))
331 goto memerr;
332 if (ispub) {
333 if (!read_lebn(&p, nbyte, &dsa->pub_key))
334 goto memerr;
335 } else {
336 if (!read_lebn(&p, 20, &dsa->priv_key))
337 goto memerr;
338 /* Calculate public key */
339 if (!(dsa->pub_key = BN_new()))
340 goto memerr;
341 if (!(ctx = BN_CTX_new()))
342 goto memerr;
343 if (!BN_mod_exp(dsa->pub_key, dsa->g,
344 dsa->priv_key, dsa->p, ctx))
345 goto memerr;
346 BN_CTX_free(ctx);
347 }
348
349 EVP_PKEY_set1_DSA(ret, dsa);
350 DSA_free(dsa);
351 *in = p;
352 return ret;
353
354memerr:
355 PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
356 DSA_free(dsa);
357 EVP_PKEY_free(ret);
358 BN_CTX_free(ctx);
359 return NULL;
360}
361
362static EVP_PKEY *
363b2i_rsa(const unsigned char **in, unsigned int length, unsigned int bitlen,
364 int ispub)
365{
366 const unsigned char *p = *in;
367 EVP_PKEY *ret = NULL;
368 RSA *rsa = NULL;
369 unsigned int nbyte, hnbyte;
370
371 nbyte = (bitlen + 7) >> 3;
372 hnbyte = (bitlen + 15) >> 4;
373 rsa = RSA_new();
374 ret = EVP_PKEY_new();
375 if (!rsa || !ret)
376 goto memerr;
377 rsa->e = BN_new();
378 if (!rsa->e)
379 goto memerr;
380 if (!BN_set_word(rsa->e, read_ledword(&p)))
381 goto memerr;
382 if (!read_lebn(&p, nbyte, &rsa->n))
383 goto memerr;
384 if (!ispub) {
385 if (!read_lebn(&p, hnbyte, &rsa->p))
386 goto memerr;
387 if (!read_lebn(&p, hnbyte, &rsa->q))
388 goto memerr;
389 if (!read_lebn(&p, hnbyte, &rsa->dmp1))
390 goto memerr;
391 if (!read_lebn(&p, hnbyte, &rsa->dmq1))
392 goto memerr;
393 if (!read_lebn(&p, hnbyte, &rsa->iqmp))
394 goto memerr;
395 if (!read_lebn(&p, nbyte, &rsa->d))
396 goto memerr;
397 }
398
399 EVP_PKEY_set1_RSA(ret, rsa);
400 RSA_free(rsa);
401 *in = p;
402 return ret;
403
404memerr:
405 PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
406 RSA_free(rsa);
407 EVP_PKEY_free(ret);
408 return NULL;
409}
410
411EVP_PKEY *
412b2i_PrivateKey(const unsigned char **in, long length)
413{
414 return do_b2i(in, length, 0);
415}
416
417EVP_PKEY *
418b2i_PublicKey(const unsigned char **in, long length)
419{
420 return do_b2i(in, length, 1);
421}
422
423EVP_PKEY *
424b2i_PrivateKey_bio(BIO *in)
425{
426 return do_b2i_bio(in, 0);
427}
428
429EVP_PKEY *
430b2i_PublicKey_bio(BIO *in)
431{
432 return do_b2i_bio(in, 1);
433}
434
435static void
436write_ledword(unsigned char **out, unsigned int dw)
437{
438 unsigned char *p = *out;
439
440 *p++ = dw & 0xff;
441 *p++ = (dw >> 8) & 0xff;
442 *p++ = (dw >> 16) & 0xff;
443 *p++ = (dw >> 24) & 0xff;
444 *out = p;
445}
446
447static void
448write_lebn(unsigned char **out, const BIGNUM *bn, int len)
449{
450 int nb, i;
451 unsigned char *p = *out, *q, c;
452
453 nb = BN_num_bytes(bn);
454 BN_bn2bin(bn, p);
455 q = p + nb - 1;
456 /* In place byte order reversal */
457 for (i = 0; i < nb / 2; i++) {
458 c = *p;
459 *p++ = *q;
460 *q-- = c;
461 }
462 *out += nb;
463 /* Pad with zeroes if we have to */
464 if (len > 0) {
465 len -= nb;
466 if (len > 0) {
467 memset(*out, 0, len);
468 *out += len;
469 }
470 }
471}
472
473
474static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
475static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
476
477static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
478static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
479
480static int
481do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
482{
483 unsigned char *p;
484 unsigned int bitlen, magic = 0, keyalg;
485 int outlen, noinc = 0;
486
487 if (pk->type == EVP_PKEY_DSA) {
488 bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic);
489 keyalg = MS_KEYALG_DSS_SIGN;
490 } else if (pk->type == EVP_PKEY_RSA) {
491 bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic);
492 keyalg = MS_KEYALG_RSA_KEYX;
493 } else
494 return -1;
495 if (bitlen == 0)
496 return -1;
497 outlen = 16 + blob_length(bitlen,
498 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
499 if (out == NULL)
500 return outlen;
501 if (*out)
502 p = *out;
503 else {
504 p = malloc(outlen);
505 if (!p)
506 return -1;
507 *out = p;
508 noinc = 1;
509 }
510 if (ispub)
511 *p++ = MS_PUBLICKEYBLOB;
512 else
513 *p++ = MS_PRIVATEKEYBLOB;
514 *p++ = 0x2;
515 *p++ = 0;
516 *p++ = 0;
517 write_ledword(&p, keyalg);
518 write_ledword(&p, magic);
519 write_ledword(&p, bitlen);
520 if (keyalg == MS_KEYALG_DSS_SIGN)
521 write_dsa(&p, pk->pkey.dsa, ispub);
522 else
523 write_rsa(&p, pk->pkey.rsa, ispub);
524 if (!noinc)
525 *out += outlen;
526 return outlen;
527}
528
529static int
530do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
531{
532 unsigned char *tmp = NULL;
533 int outlen, wrlen;
534
535 outlen = do_i2b(&tmp, pk, ispub);
536 if (outlen < 0)
537 return -1;
538 wrlen = BIO_write(out, tmp, outlen);
539 free(tmp);
540 if (wrlen == outlen)
541 return outlen;
542 return -1;
543}
544
545static int
546check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
547{
548 int bitlen;
549
550 bitlen = BN_num_bits(dsa->p);
551 if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160) ||
552 (BN_num_bits(dsa->g) > bitlen))
553 goto badkey;
554 if (ispub) {
555 if (BN_num_bits(dsa->pub_key) > bitlen)
556 goto badkey;
557 *pmagic = MS_DSS1MAGIC;
558 } else {
559 if (BN_num_bits(dsa->priv_key) > 160)
560 goto badkey;
561 *pmagic = MS_DSS2MAGIC;
562 }
563
564 return bitlen;
565
566badkey:
567 PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
568 return 0;
569}
570
571static int
572check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
573{
574 int nbyte, hnbyte, bitlen;
575
576 if (BN_num_bits(rsa->e) > 32)
577 goto badkey;
578 bitlen = BN_num_bits(rsa->n);
579 nbyte = BN_num_bytes(rsa->n);
580 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
581 if (ispub) {
582 *pmagic = MS_RSA1MAGIC;
583 return bitlen;
584 } else {
585 *pmagic = MS_RSA2MAGIC;
586 /* For private key each component must fit within nbyte or
587 * hnbyte.
588 */
589 if (BN_num_bytes(rsa->d) > nbyte)
590 goto badkey;
591 if ((BN_num_bytes(rsa->iqmp) > hnbyte) ||
592 (BN_num_bytes(rsa->p) > hnbyte) ||
593 (BN_num_bytes(rsa->q) > hnbyte) ||
594 (BN_num_bytes(rsa->dmp1) > hnbyte) ||
595 (BN_num_bytes(rsa->dmq1) > hnbyte))
596 goto badkey;
597 }
598 return bitlen;
599
600badkey:
601 PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
602 return 0;
603}
604
605static void
606write_rsa(unsigned char **out, RSA *rsa, int ispub)
607{
608 int nbyte, hnbyte;
609
610 nbyte = BN_num_bytes(rsa->n);
611 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
612 write_lebn(out, rsa->e, 4);
613 write_lebn(out, rsa->n, -1);
614 if (ispub)
615 return;
616 write_lebn(out, rsa->p, hnbyte);
617 write_lebn(out, rsa->q, hnbyte);
618 write_lebn(out, rsa->dmp1, hnbyte);
619 write_lebn(out, rsa->dmq1, hnbyte);
620 write_lebn(out, rsa->iqmp, hnbyte);
621 write_lebn(out, rsa->d, nbyte);
622}
623
624static void
625write_dsa(unsigned char **out, DSA *dsa, int ispub)
626{
627 int nbyte;
628
629 nbyte = BN_num_bytes(dsa->p);
630 write_lebn(out, dsa->p, nbyte);
631 write_lebn(out, dsa->q, 20);
632 write_lebn(out, dsa->g, nbyte);
633 if (ispub)
634 write_lebn(out, dsa->pub_key, nbyte);
635 else
636 write_lebn(out, dsa->priv_key, 20);
637 /* Set "invalid" for seed structure values */
638 memset(*out, 0xff, 24);
639 *out += 24;
640 return;
641}
642
643int
644i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
645{
646 return do_i2b_bio(out, pk, 0);
647}
648
649int
650i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
651{
652 return do_i2b_bio(out, pk, 1);
653}
654
655#ifndef OPENSSL_NO_RC4
656
657static int
658do_PVK_header(const unsigned char **in, unsigned int length, int skip_magic,
659 unsigned int *psaltlen, unsigned int *pkeylen)
660{
661 const unsigned char *p = *in;
662 unsigned int pvk_magic, is_encrypted;
663
664 if (skip_magic) {
665 if (length < 20) {
666 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
667 return 0;
668 }
669 length -= 20;
670 } else {
671 if (length < 24) {
672 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
673 return 0;
674 }
675 length -= 24;
676 pvk_magic = read_ledword(&p);
677 if (pvk_magic != MS_PVKMAGIC) {
678 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
679 return 0;
680 }
681 }
682 /* Skip reserved */
683 p += 4;
684 /*keytype = */read_ledword(&p);
685 is_encrypted = read_ledword(&p);
686 *psaltlen = read_ledword(&p);
687 *pkeylen = read_ledword(&p);
688 if (*psaltlen > 65536 || *pkeylen > 65536) {
689 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
690 return 0;
691 }
692
693 if (is_encrypted && !*psaltlen) {
694 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
695 return 0;
696 }
697
698 *in = p;
699 return 1;
700}
701
702static int
703derive_pvk_key(unsigned char *key, const unsigned char *salt,
704 unsigned int saltlen, const unsigned char *pass, int passlen)
705{
706 EVP_MD_CTX mctx;
707 int rv = 1;
708
709 EVP_MD_CTX_init(&mctx);
710 if (!EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL) ||
711 !EVP_DigestUpdate(&mctx, salt, saltlen) ||
712 !EVP_DigestUpdate(&mctx, pass, passlen) ||
713 !EVP_DigestFinal_ex(&mctx, key, NULL))
714 rv = 0;
715
716 EVP_MD_CTX_cleanup(&mctx);
717 return rv;
718}
719
720static EVP_PKEY *
721do_PVK_body(const unsigned char **in, unsigned int saltlen,
722 unsigned int keylen, pem_password_cb *cb, void *u)
723{
724 EVP_PKEY *ret = NULL;
725 const unsigned char *p = *in;
726 unsigned int magic;
727 unsigned char *enctmp = NULL, *q;
728 EVP_CIPHER_CTX cctx;
729
730 EVP_CIPHER_CTX_init(&cctx);
731 if (saltlen) {
732 char psbuf[PEM_BUFSIZE];
733 unsigned char keybuf[20];
734 int enctmplen, inlen;
735
736 if (cb)
737 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
738 else
739 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
740 if (inlen <= 0) {
741 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
742 goto err;
743 }
744 enctmp = malloc(keylen + 8);
745 if (!enctmp) {
746 PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
747 goto err;
748 }
749 if (!derive_pvk_key(keybuf, p, saltlen, (unsigned char *)psbuf,
750 inlen)) {
751 goto err;
752 }
753 p += saltlen;
754 /* Copy BLOBHEADER across, decrypt rest */
755 memcpy(enctmp, p, 8);
756 p += 8;
757 if (keylen < 8) {
758 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
759 goto err;
760 }
761 inlen = keylen - 8;
762 q = enctmp + 8;
763 if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
764 goto err;
765 if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
766 goto err;
767 if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
768 goto err;
769 magic = read_ledword((const unsigned char **)&q);
770 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
771 q = enctmp + 8;
772 memset(keybuf + 5, 0, 11);
773 if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf,
774 NULL))
775 goto err;
776 explicit_bzero(keybuf, 20);
777 if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
778 goto err;
779 if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen,
780 &enctmplen))
781 goto err;
782 magic = read_ledword((const unsigned char **)&q);
783 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
784 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
785 goto err;
786 }
787 } else
788 explicit_bzero(keybuf, 20);
789 p = enctmp;
790 }
791
792 ret = b2i_PrivateKey(&p, keylen);
793
794err:
795 EVP_CIPHER_CTX_cleanup(&cctx);
796 if (enctmp && saltlen)
797 free(enctmp);
798 return ret;
799}
800
801
802EVP_PKEY *
803b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
804{
805 unsigned char pvk_hdr[24], *buf = NULL;
806 const unsigned char *p;
807 size_t buflen;
808 EVP_PKEY *ret = NULL;
809 unsigned int saltlen, keylen;
810
811 if (BIO_read(in, pvk_hdr, 24) != 24) {
812 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
813 return NULL;
814 }
815 p = pvk_hdr;
816
817 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
818 return 0;
819 buflen = keylen + saltlen;
820 buf = malloc(buflen);
821 if (!buf) {
822 PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
823 return 0;
824 }
825 p = buf;
826 if (BIO_read(in, buf, buflen) != buflen) {
827 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
828 goto err;
829 }
830 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
831
832err:
833 if (buf) {
834 explicit_bzero(buf, buflen);
835 free(buf);
836 }
837 return ret;
838}
839
840static int
841i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel, pem_password_cb *cb,
842 void *u)
843{
844 int outlen = 24, pklen;
845 unsigned char *p, *salt = NULL;
846 EVP_CIPHER_CTX cctx;
847
848 EVP_CIPHER_CTX_init(&cctx);
849 if (enclevel)
850 outlen += PVK_SALTLEN;
851 pklen = do_i2b(NULL, pk, 0);
852 if (pklen < 0)
853 return -1;
854 outlen += pklen;
855 if (!out)
856 return outlen;
857 if (*out)
858 p = *out;
859 else {
860 p = malloc(outlen);
861 if (!p) {
862 PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
863 return -1;
864 }
865 *out = p;
866 }
867
868 write_ledword(&p, MS_PVKMAGIC);
869 write_ledword(&p, 0);
870 if (pk->type == EVP_PKEY_DSA)
871 write_ledword(&p, MS_KEYTYPE_SIGN);
872 else
873 write_ledword(&p, MS_KEYTYPE_KEYX);
874 write_ledword(&p, enclevel ? 1 : 0);
875 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
876 write_ledword(&p, pklen);
877 if (enclevel) {
878 arc4random_buf(p, PVK_SALTLEN);
879 salt = p;
880 p += PVK_SALTLEN;
881 }
882 do_i2b(&p, pk, 0);
883 if (enclevel == 0)
884 return outlen;
885 else {
886 char psbuf[PEM_BUFSIZE];
887 unsigned char keybuf[20];
888 int enctmplen, inlen;
889 if (cb)
890 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
891 else
892 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
893 if (inlen <= 0) {
894 PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
895 goto error;
896 }
897 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
898 (unsigned char *)psbuf, inlen))
899 goto error;
900 if (enclevel == 1)
901 memset(keybuf + 5, 0, 11);
902 p = salt + PVK_SALTLEN + 8;
903 if (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
904 goto error;
905 explicit_bzero(keybuf, 20);
906 if (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8))
907 goto error;
908 if (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen))
909 goto error;
910 }
911 EVP_CIPHER_CTX_cleanup(&cctx);
912 return outlen;
913
914error:
915 EVP_CIPHER_CTX_cleanup(&cctx);
916 return -1;
917}
918
919int
920i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel, pem_password_cb *cb, void *u)
921{
922 unsigned char *tmp = NULL;
923 int outlen, wrlen;
924
925 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
926 if (outlen < 0)
927 return -1;
928 wrlen = BIO_write(out, tmp, outlen);
929 free(tmp);
930 if (wrlen == outlen) {
931 PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);
932 return outlen;
933 }
934 return -1;
935}
936
937#endif
938
939#endif