1998-12-21 10:56:39 +00:00
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
1998-12-21 10:52:47 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This package is an SSL implementation written
|
|
|
|
* by Eric Young (eay@cryptsoft.com).
|
|
|
|
* The implementation was written so as to conform with Netscapes SSL.
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
1998-12-21 10:52:47 +00:00
|
|
|
* This library is free for commercial and non-commercial use as long as
|
|
|
|
* the following conditions are aheared to. The following conditions
|
|
|
|
* apply to all code found in this distribution, be it the RC4, RSA,
|
|
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
|
|
|
* included with this distribution is covered by the same copyright terms
|
|
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
1998-12-21 10:52:47 +00:00
|
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
|
|
|
* the code are not to be removed.
|
|
|
|
* If this package is used in a product, Eric Young should be given attribution
|
|
|
|
* as the author of the parts of the library used.
|
|
|
|
* This can be in the form of a textual message at program startup or
|
|
|
|
* in documentation (online or textual) provided with the package.
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
1998-12-21 10:52:47 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* "This product includes cryptographic software written by
|
|
|
|
* Eric Young (eay@cryptsoft.com)"
|
|
|
|
* The word 'cryptographic' can be left out if the rouines from the library
|
|
|
|
* being used are not cryptographic related :-).
|
2015-01-22 03:40:55 +00:00
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
1998-12-21 10:52:47 +00:00
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
1998-12-21 10:52:47 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
1998-12-21 10:52:47 +00:00
|
|
|
* The licence and distribution terms for any publically available version or
|
|
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
|
|
|
* copied and put under another distribution licence
|
|
|
|
* [including the GNU Public Licence.]
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-03-23 11:54:51 +00:00
|
|
|
#include <ctype.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/pem.h>
|
1999-06-05 18:40:51 +00:00
|
|
|
#include <openssl/pkcs12.h>
|
2015-03-23 18:42:42 +00:00
|
|
|
#include "internal/asn1_int.h"
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_DES
|
2015-01-22 03:40:55 +00:00
|
|
|
# include <openssl/des.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
2006-06-05 11:52:46 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2015-01-22 03:40:55 +00:00
|
|
|
# include <openssl/engine.h>
|
2006-06-05 11:52:46 +00:00
|
|
|
#endif
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
#define MIN_LENGTH 4
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static int load_iv(char **fromp, unsigned char *to, int num);
|
1999-11-04 00:45:35 +00:00
|
|
|
static int check_pem(const char *nm, const char *name);
|
2006-03-23 18:02:23 +00:00
|
|
|
int pem_check_suffix(const char *pem_str, const char *suffix);
|
2001-07-27 02:22:42 +00:00
|
|
|
|
|
|
|
int PEM_def_callback(char *buf, int num, int w, void *key)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-01-14 20:57:28 +00:00
|
|
|
#ifdef OPENSSL_NO_STDIO
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* We should not ever call the default callback routine from windows.
|
|
|
|
*/
|
|
|
|
PEMerr(PEM_F_PEM_DEF_CALLBACK, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
|
|
|
return (-1);
|
1998-12-21 10:52:47 +00:00
|
|
|
#else
|
2015-01-22 03:40:55 +00:00
|
|
|
int i, j;
|
|
|
|
const char *prompt;
|
|
|
|
if (key) {
|
|
|
|
i = strlen(key);
|
|
|
|
i = (i > num) ? num : i;
|
|
|
|
memcpy(buf, key, i);
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt = EVP_get_pw_prompt();
|
|
|
|
if (prompt == NULL)
|
|
|
|
prompt = "Enter PEM pass phrase:";
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
i = EVP_read_pw_string_min(buf, MIN_LENGTH, num, prompt, w);
|
|
|
|
if (i != 0) {
|
|
|
|
PEMerr(PEM_F_PEM_DEF_CALLBACK, PEM_R_PROBLEMS_GETTING_PASSWORD);
|
|
|
|
memset(buf, 0, (unsigned int)num);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
j = strlen(buf);
|
|
|
|
if (j < MIN_LENGTH) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"phrase is too short, needs to be at least %d chars\n",
|
|
|
|
MIN_LENGTH);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (j);
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
void PEM_proc_type(char *buf, int type)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
if (type == PEM_TYPE_ENCRYPTED)
|
|
|
|
str = "ENCRYPTED";
|
|
|
|
else if (type == PEM_TYPE_MIC_CLEAR)
|
|
|
|
str = "MIC-CLEAR";
|
|
|
|
else if (type == PEM_TYPE_MIC_ONLY)
|
|
|
|
str = "MIC-ONLY";
|
|
|
|
else
|
|
|
|
str = "BAD-TYPE";
|
|
|
|
|
Rename some BUF_xxx to OPENSSL_xxx
Rename BUF_{strdup,strlcat,strlcpy,memdup,strndup,strnlen}
to OPENSSL_{strdup,strlcat,strlcpy,memdup,strndup,strnlen}
Add #define's for the old names.
Add CRYPTO_{memdup,strndup}, called by OPENSSL_{memdup,strndup} macros.
Reviewed-by: Tim Hudson <tjh@openssl.org>
2015-12-16 21:12:24 +00:00
|
|
|
OPENSSL_strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE);
|
|
|
|
OPENSSL_strlcat(buf, str, PEM_BUFSIZE);
|
|
|
|
OPENSSL_strlcat(buf, "\n", PEM_BUFSIZE);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
void PEM_dek_info(char *buf, const char *type, int len, char *str)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
static const unsigned char map[17] = "0123456789ABCDEF";
|
|
|
|
long i;
|
|
|
|
int j;
|
|
|
|
|
Rename some BUF_xxx to OPENSSL_xxx
Rename BUF_{strdup,strlcat,strlcpy,memdup,strndup,strnlen}
to OPENSSL_{strdup,strlcat,strlcpy,memdup,strndup,strnlen}
Add #define's for the old names.
Add CRYPTO_{memdup,strndup}, called by OPENSSL_{memdup,strndup} macros.
Reviewed-by: Tim Hudson <tjh@openssl.org>
2015-12-16 21:12:24 +00:00
|
|
|
OPENSSL_strlcat(buf, "DEK-Info: ", PEM_BUFSIZE);
|
|
|
|
OPENSSL_strlcat(buf, type, PEM_BUFSIZE);
|
|
|
|
OPENSSL_strlcat(buf, ",", PEM_BUFSIZE);
|
2015-01-22 03:40:55 +00:00
|
|
|
j = strlen(buf);
|
|
|
|
if (j + (len * 2) + 1 > PEM_BUFSIZE)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
buf[j + i * 2] = map[(str[i] >> 4) & 0x0f];
|
|
|
|
buf[j + i * 2 + 1] = map[(str[i]) & 0x0f];
|
|
|
|
}
|
|
|
|
buf[j + i * 2] = '\n';
|
|
|
|
buf[j + i * 2 + 1] = '\0';
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-14 20:57:28 +00:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
2005-03-31 13:57:54 +00:00
|
|
|
void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
|
2015-01-22 03:40:55 +00:00
|
|
|
pem_password_cb *cb, void *u)
|
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u);
|
|
|
|
BIO_free(b);
|
|
|
|
return (ret);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
|
|
|
|
1999-11-04 00:45:35 +00:00
|
|
|
static int check_pem(const char *nm, const char *name)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Normal matching nm and name */
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, name) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Make PEM_STRING_EVP_PKEY match any private key */
|
|
|
|
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(name, PEM_STRING_EVP_PKEY) == 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
int slen;
|
|
|
|
const EVP_PKEY_ASN1_METHOD *ameth;
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_PKCS8) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_PKCS8INF) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
slen = pem_check_suffix(nm, "PRIVATE KEY");
|
|
|
|
if (slen > 0) {
|
|
|
|
/*
|
|
|
|
* NB: ENGINE implementations wont contain a deprecated old
|
|
|
|
* private key decode function so don't look for them.
|
|
|
|
*/
|
|
|
|
ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
|
|
|
|
if (ameth && ameth->old_priv_decode)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(name, PEM_STRING_PARAMETERS) == 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
int slen;
|
|
|
|
const EVP_PKEY_ASN1_METHOD *ameth;
|
|
|
|
slen = pem_check_suffix(nm, "PARAMETERS");
|
|
|
|
if (slen > 0) {
|
|
|
|
ENGINE *e;
|
|
|
|
ameth = EVP_PKEY_asn1_find_str(&e, nm, slen);
|
|
|
|
if (ameth) {
|
|
|
|
int r;
|
|
|
|
if (ameth->param_decode)
|
|
|
|
r = 1;
|
|
|
|
else
|
|
|
|
r = 0;
|
2006-06-05 11:52:46 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2015-01-22 03:40:55 +00:00
|
|
|
if (e)
|
|
|
|
ENGINE_finish(e);
|
2006-06-05 11:52:46 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* If reading DH parameters handle X9.42 DH format too */
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_DHPARAMS) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
2006-03-28 14:35:32 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Permit older strings */
|
1999-11-04 00:45:35 +00:00
|
|
|
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_X509_OLD) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_X509) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
1999-11-04 00:45:35 +00:00
|
|
|
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_X509_REQ_OLD) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_X509_REQ) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
1999-11-04 00:45:35 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Allow normal certs to be read as trusted certs */
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_X509) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_X509_TRUSTED) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
1999-11-04 00:45:35 +00:00
|
|
|
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_X509_OLD) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_X509_TRUSTED) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
1999-11-04 00:45:35 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Some CAs use PKCS#7 with CERTIFICATE headers */
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_X509) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_PKCS7) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
2000-02-23 01:11:01 +00:00
|
|
|
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_PKCS7_SIGNED) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_PKCS7) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
2008-11-11 12:38:25 +00:00
|
|
|
|
2008-03-12 21:14:28 +00:00
|
|
|
#ifndef OPENSSL_NO_CMS
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_X509) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_CMS) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
/* Allow CMS to be read from PKCS#7 headers */
|
2015-05-06 18:56:14 +00:00
|
|
|
if (strcmp(nm, PEM_STRING_PKCS7) == 0
|
|
|
|
&& strcmp(name, PEM_STRING_CMS) == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
2008-03-12 21:14:28 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
1999-11-04 00:45:35 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
|
|
|
|
const char *name, BIO *bp, pem_password_cb *cb,
|
|
|
|
void *u)
|
|
|
|
{
|
|
|
|
EVP_CIPHER_INFO cipher;
|
|
|
|
char *nm = NULL, *header = NULL;
|
|
|
|
unsigned char *data = NULL;
|
|
|
|
long len;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!PEM_read_bio(bp, &nm, &header, &data, &len)) {
|
|
|
|
if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
|
|
|
|
ERR_add_error_data(2, "Expecting: ", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (check_pem(nm, name))
|
|
|
|
break;
|
|
|
|
OPENSSL_free(nm);
|
|
|
|
OPENSSL_free(header);
|
|
|
|
OPENSSL_free(data);
|
|
|
|
}
|
|
|
|
if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
|
|
|
|
goto err;
|
|
|
|
if (!PEM_do_header(&cipher, data, &len, cb, u))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
*pdata = data;
|
|
|
|
*plen = len;
|
|
|
|
|
|
|
|
if (pnm)
|
|
|
|
*pnm = nm;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (!ret || !pnm)
|
|
|
|
OPENSSL_free(nm);
|
|
|
|
OPENSSL_free(header);
|
|
|
|
if (!ret)
|
|
|
|
OPENSSL_free(data);
|
|
|
|
return ret;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-14 20:57:28 +00:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
2005-03-31 13:57:54 +00:00
|
|
|
int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
|
2015-01-22 03:40:55 +00:00
|
|
|
void *x, const EVP_CIPHER *enc, unsigned char *kstr,
|
|
|
|
int klen, pem_password_cb *callback, void *u)
|
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u);
|
|
|
|
BIO_free(b);
|
|
|
|
return (ret);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
|
|
|
|
2005-03-31 13:57:54 +00:00
|
|
|
int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
|
2015-01-22 03:40:55 +00:00
|
|
|
void *x, const EVP_CIPHER *enc, unsigned char *kstr,
|
|
|
|
int klen, pem_password_cb *callback, void *u)
|
|
|
|
{
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX *ctx = NULL;
|
2015-05-06 09:16:55 +00:00
|
|
|
int dsize = 0, i = 0, j = 0, ret = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char *p, *data = NULL;
|
|
|
|
const char *objstr = NULL;
|
|
|
|
char buf[PEM_BUFSIZE];
|
|
|
|
unsigned char key[EVP_MAX_KEY_LENGTH];
|
|
|
|
unsigned char iv[EVP_MAX_IV_LENGTH];
|
|
|
|
|
|
|
|
if (enc != NULL) {
|
|
|
|
objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
|
|
|
|
if (objstr == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dsize = i2d(x, NULL)) < 0) {
|
|
|
|
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_ASN1_LIB);
|
|
|
|
dsize = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* dzise + 8 bytes are needed */
|
|
|
|
/* actually it needs the cipher block size extra... */
|
2015-04-28 19:28:14 +00:00
|
|
|
data = OPENSSL_malloc((unsigned int)dsize + 20);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (data == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
p = data;
|
|
|
|
i = i2d(x, &p);
|
|
|
|
|
|
|
|
if (enc != NULL) {
|
|
|
|
if (kstr == NULL) {
|
|
|
|
if (callback == NULL)
|
|
|
|
klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
|
|
|
|
else
|
|
|
|
klen = (*callback) (buf, PEM_BUFSIZE, 1, u);
|
|
|
|
if (klen <= 0) {
|
|
|
|
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, PEM_R_READ_KEY);
|
|
|
|
goto err;
|
|
|
|
}
|
1999-06-04 21:35:58 +00:00
|
|
|
#ifdef CHARSET_EBCDIC
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Convert the pass phrase from EBCDIC */
|
|
|
|
ebcdic2ascii(buf, buf, klen);
|
1999-06-04 21:35:58 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
kstr = (unsigned char *)buf;
|
|
|
|
}
|
|
|
|
RAND_add(data, i, 0); /* put in the RSA key. */
|
2015-12-18 16:08:49 +00:00
|
|
|
OPENSSL_assert(EVP_CIPHER_iv_length(enc) <= (int)sizeof(iv));
|
|
|
|
if (RAND_bytes(iv, EVP_CIPHER_iv_length(enc)) <= 0) /* Generate a salt */
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
/*
|
|
|
|
* The 'iv' is used as the iv and as a salt. It is NOT taken from
|
|
|
|
* the BytesToKey function
|
|
|
|
*/
|
|
|
|
if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1, key, NULL))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (kstr == (unsigned char *)buf)
|
|
|
|
OPENSSL_cleanse(buf, PEM_BUFSIZE);
|
|
|
|
|
2015-12-18 16:08:49 +00:00
|
|
|
OPENSSL_assert(strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13
|
|
|
|
<= sizeof buf);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
buf[0] = '\0';
|
|
|
|
PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
|
2015-12-18 16:08:49 +00:00
|
|
|
PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc), (char *)iv);
|
2015-01-22 03:40:55 +00:00
|
|
|
/* k=strlen(buf); */
|
|
|
|
|
|
|
|
ret = 1;
|
2015-12-13 21:08:41 +00:00
|
|
|
if ((ctx = EVP_CIPHER_CTX_new()) == NULL
|
|
|
|
|| !EVP_EncryptInit_ex(ctx, enc, NULL, key, iv)
|
|
|
|
|| !EVP_EncryptUpdate(ctx, data, &j, data, i)
|
|
|
|
|| !EVP_EncryptFinal_ex(ctx, &(data[j]), &i))
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = 0;
|
|
|
|
if (ret == 0)
|
|
|
|
goto err;
|
|
|
|
i += j;
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
buf[0] = '\0';
|
|
|
|
}
|
|
|
|
i = PEM_write_bio(bp, name, buf, data, i);
|
|
|
|
if (i <= 0)
|
|
|
|
ret = 0;
|
|
|
|
err:
|
|
|
|
OPENSSL_cleanse(key, sizeof(key));
|
|
|
|
OPENSSL_cleanse(iv, sizeof(iv));
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
OPENSSL_cleanse(buf, PEM_BUFSIZE);
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(data, (unsigned int)dsize);
|
2015-01-22 03:40:55 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
|
2015-01-22 03:40:55 +00:00
|
|
|
pem_password_cb *callback, void *u)
|
|
|
|
{
|
|
|
|
int i = 0, j, o, klen;
|
|
|
|
long len;
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX *ctx;
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char key[EVP_MAX_KEY_LENGTH];
|
|
|
|
char buf[PEM_BUFSIZE];
|
|
|
|
|
|
|
|
len = *plen;
|
|
|
|
|
|
|
|
if (cipher->cipher == NULL)
|
|
|
|
return (1);
|
|
|
|
if (callback == NULL)
|
|
|
|
klen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u);
|
|
|
|
else
|
|
|
|
klen = callback(buf, PEM_BUFSIZE, 0, u);
|
|
|
|
if (klen <= 0) {
|
|
|
|
PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ);
|
|
|
|
return (0);
|
|
|
|
}
|
1999-06-04 21:35:58 +00:00
|
|
|
#ifdef CHARSET_EBCDIC
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Convert the pass phrase from EBCDIC */
|
|
|
|
ebcdic2ascii(buf, buf, klen);
|
1999-06-04 21:35:58 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]),
|
|
|
|
(unsigned char *)buf, klen, 1, key, NULL))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
j = (int)len;
|
2015-12-13 21:08:41 +00:00
|
|
|
ctx = EVP_CIPHER_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
return 0;
|
|
|
|
o = EVP_DecryptInit_ex(ctx, cipher->cipher, NULL, key, &(cipher->iv[0]));
|
2015-01-22 03:40:55 +00:00
|
|
|
if (o)
|
2015-12-13 21:08:41 +00:00
|
|
|
o = EVP_DecryptUpdate(ctx, data, &i, data, j);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (o)
|
2015-12-13 21:08:41 +00:00
|
|
|
o = EVP_DecryptFinal_ex(ctx, &(data[i]), &j);
|
|
|
|
EVP_CIPHER_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
OPENSSL_cleanse((char *)buf, sizeof(buf));
|
|
|
|
OPENSSL_cleanse((char *)key, sizeof(key));
|
|
|
|
if (o)
|
|
|
|
j += i;
|
|
|
|
else {
|
|
|
|
PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
*plen = j;
|
|
|
|
return (1);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
const EVP_CIPHER *enc = NULL;
|
2016-02-10 06:37:52 +00:00
|
|
|
char *dekinfostart, c;
|
2015-01-22 03:40:55 +00:00
|
|
|
char **header_pp = &header;
|
|
|
|
|
|
|
|
cipher->cipher = NULL;
|
|
|
|
if ((header == NULL) || (*header == '\0') || (*header == '\n'))
|
|
|
|
return (1);
|
|
|
|
if (strncmp(header, "Proc-Type: ", 11) != 0) {
|
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
header += 11;
|
|
|
|
if (*header != '4')
|
|
|
|
return (0);
|
|
|
|
header++;
|
|
|
|
if (*header != ',')
|
|
|
|
return (0);
|
|
|
|
header++;
|
|
|
|
if (strncmp(header, "ENCRYPTED", 9) != 0) {
|
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
for (; (*header != '\n') && (*header != '\0'); header++) ;
|
|
|
|
if (*header == '\0') {
|
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
header++;
|
|
|
|
if (strncmp(header, "DEK-Info: ", 10) != 0) {
|
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
header += 10;
|
|
|
|
|
2016-02-10 06:37:52 +00:00
|
|
|
dekinfostart = header;
|
2015-01-22 03:40:55 +00:00
|
|
|
for (;;) {
|
|
|
|
c = *header;
|
1999-06-04 21:35:58 +00:00
|
|
|
#ifndef CHARSET_EBCDIC
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!(((c >= 'A') && (c <= 'Z')) || (c == '-') ||
|
|
|
|
((c >= '0') && (c <= '9'))))
|
|
|
|
break;
|
1999-06-04 21:35:58 +00:00
|
|
|
#else
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!(isupper(c) || (c == '-') || isdigit(c)))
|
|
|
|
break;
|
1999-06-04 21:35:58 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
header++;
|
|
|
|
}
|
|
|
|
*header = '\0';
|
2016-02-10 06:37:52 +00:00
|
|
|
cipher->cipher = enc = EVP_get_cipherbyname(dekinfostart);
|
2015-01-22 03:40:55 +00:00
|
|
|
*header = c;
|
|
|
|
|
|
|
|
if (enc == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_UNSUPPORTED_ENCRYPTION);
|
|
|
|
return (0);
|
|
|
|
}
|
2015-12-18 16:08:49 +00:00
|
|
|
if (!load_iv(header_pp, &(cipher->iv[0]), EVP_CIPHER_iv_length(enc)))
|
2015-01-22 03:40:55 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2005-01-27 11:42:28 +00:00
|
|
|
static int load_iv(char **fromp, unsigned char *to, int num)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int v, i;
|
|
|
|
char *from;
|
|
|
|
|
|
|
|
from = *fromp;
|
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
to[i] = 0;
|
|
|
|
num *= 2;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if ((*from >= '0') && (*from <= '9'))
|
|
|
|
v = *from - '0';
|
|
|
|
else if ((*from >= 'A') && (*from <= 'F'))
|
|
|
|
v = *from - 'A' + 10;
|
|
|
|
else if ((*from >= 'a') && (*from <= 'f'))
|
|
|
|
v = *from - 'a' + 10;
|
|
|
|
else {
|
|
|
|
PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
from++;
|
|
|
|
to[i / 2] |= v << (long)((!(i & 1)) * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
*fromp = from;
|
|
|
|
return (1);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-14 20:57:28 +00:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
2013-09-10 16:58:44 +00:00
|
|
|
int PEM_write(FILE *fp, const char *name, const char *header,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *data, long len)
|
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = PEM_write_bio(b, name, header, data, len);
|
|
|
|
BIO_free(b);
|
|
|
|
return (ret);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-10 16:58:44 +00:00
|
|
|
int PEM_write_bio(BIO *bp, const char *name, const char *header,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *data, long len)
|
|
|
|
{
|
|
|
|
int nlen, n, i, j, outl;
|
|
|
|
unsigned char *buf = NULL;
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
int reason = ERR_R_BUF_LIB;
|
|
|
|
|
2015-12-11 15:10:38 +00:00
|
|
|
if (ctx == NULL) {
|
|
|
|
reason = ERR_R_MALLOC_FAILURE;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_EncodeInit(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
nlen = strlen(name);
|
|
|
|
|
|
|
|
if ((BIO_write(bp, "-----BEGIN ", 11) != 11) ||
|
|
|
|
(BIO_write(bp, name, nlen) != nlen) ||
|
|
|
|
(BIO_write(bp, "-----\n", 6) != 6))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
i = strlen(header);
|
|
|
|
if (i > 0) {
|
|
|
|
if ((BIO_write(bp, header, i) != i) || (BIO_write(bp, "\n", 1) != 1))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = OPENSSL_malloc(PEM_BUFSIZE * 8);
|
|
|
|
if (buf == NULL) {
|
|
|
|
reason = ERR_R_MALLOC_FAILURE;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = j = 0;
|
|
|
|
while (len > 0) {
|
|
|
|
n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_EncodeUpdate(ctx, buf, &outl, &(data[j]), n);
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl))
|
|
|
|
goto err;
|
|
|
|
i += outl;
|
|
|
|
len -= n;
|
|
|
|
j += n;
|
|
|
|
}
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_EncodeFinal(ctx, buf, &outl);
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
|
|
|
|
goto err;
|
|
|
|
if ((BIO_write(bp, "-----END ", 9) != 9) ||
|
|
|
|
(BIO_write(bp, name, nlen) != nlen) ||
|
|
|
|
(BIO_write(bp, "-----\n", 6) != 6))
|
|
|
|
goto err;
|
2015-12-11 15:10:38 +00:00
|
|
|
OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
|
|
|
|
EVP_ENCODE_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return (i + outl);
|
|
|
|
err:
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_ENCODE_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_PEM_WRITE_BIO, reason);
|
|
|
|
return (0);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-14 20:57:28 +00:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
1999-04-19 21:31:43 +00:00
|
|
|
int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
|
2015-01-22 03:40:55 +00:00
|
|
|
long *len)
|
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = PEM_read_bio(b, name, header, data, len);
|
|
|
|
BIO_free(b);
|
|
|
|
return (ret);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
|
2015-01-22 03:40:55 +00:00
|
|
|
long *len)
|
|
|
|
{
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
int end = 0, i, k, bl = 0, hl = 0, nohead = 0;
|
|
|
|
char buf[256];
|
|
|
|
BUF_MEM *nameB;
|
|
|
|
BUF_MEM *headerB;
|
|
|
|
BUF_MEM *dataB, *tmpB;
|
|
|
|
|
2015-12-11 15:10:38 +00:00
|
|
|
if (ctx == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
nameB = BUF_MEM_new();
|
|
|
|
headerB = BUF_MEM_new();
|
|
|
|
dataB = BUF_MEM_new();
|
|
|
|
if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) {
|
2015-12-11 15:10:38 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf[254] = '\0';
|
|
|
|
for (;;) {
|
|
|
|
i = BIO_gets(bp, buf, 254);
|
|
|
|
|
|
|
|
if (i <= 0) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((i >= 0) && (buf[i] <= ' '))
|
|
|
|
i--;
|
|
|
|
buf[++i] = '\n';
|
|
|
|
buf[++i] = '\0';
|
|
|
|
|
|
|
|
if (strncmp(buf, "-----BEGIN ", 11) == 0) {
|
|
|
|
i = strlen(&(buf[11]));
|
|
|
|
|
|
|
|
if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0)
|
|
|
|
continue;
|
|
|
|
if (!BUF_MEM_grow(nameB, i + 9)) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
memcpy(nameB->data, &(buf[11]), i - 6);
|
|
|
|
nameB->data[i - 6] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hl = 0;
|
|
|
|
if (!BUF_MEM_grow(headerB, 256)) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
headerB->data[0] = '\0';
|
|
|
|
for (;;) {
|
|
|
|
i = BIO_gets(bp, buf, 254);
|
|
|
|
if (i <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
while ((i >= 0) && (buf[i] <= ' '))
|
|
|
|
i--;
|
|
|
|
buf[++i] = '\n';
|
|
|
|
buf[++i] = '\0';
|
|
|
|
|
|
|
|
if (buf[0] == '\n')
|
|
|
|
break;
|
|
|
|
if (!BUF_MEM_grow(headerB, hl + i + 9)) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (strncmp(buf, "-----END ", 9) == 0) {
|
|
|
|
nohead = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memcpy(&(headerB->data[hl]), buf, i);
|
|
|
|
headerB->data[hl + i] = '\0';
|
|
|
|
hl += i;
|
|
|
|
}
|
|
|
|
|
|
|
|
bl = 0;
|
|
|
|
if (!BUF_MEM_grow(dataB, 1024)) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
dataB->data[0] = '\0';
|
|
|
|
if (!nohead) {
|
|
|
|
for (;;) {
|
|
|
|
i = BIO_gets(bp, buf, 254);
|
|
|
|
if (i <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
while ((i >= 0) && (buf[i] <= ' '))
|
|
|
|
i--;
|
|
|
|
buf[++i] = '\n';
|
|
|
|
buf[++i] = '\0';
|
|
|
|
|
|
|
|
if (i != 65)
|
|
|
|
end = 1;
|
|
|
|
if (strncmp(buf, "-----END ", 9) == 0)
|
|
|
|
break;
|
|
|
|
if (i > 65)
|
|
|
|
break;
|
|
|
|
if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
memcpy(&(dataB->data[bl]), buf, i);
|
|
|
|
dataB->data[bl + i] = '\0';
|
|
|
|
bl += i;
|
|
|
|
if (end) {
|
|
|
|
buf[0] = '\0';
|
|
|
|
i = BIO_gets(bp, buf, 254);
|
|
|
|
if (i <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
while ((i >= 0) && (buf[i] <= ' '))
|
|
|
|
i--;
|
|
|
|
buf[++i] = '\n';
|
|
|
|
buf[++i] = '\0';
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tmpB = headerB;
|
|
|
|
headerB = dataB;
|
|
|
|
dataB = tmpB;
|
|
|
|
bl = hl;
|
|
|
|
}
|
|
|
|
i = strlen(nameB->data);
|
|
|
|
if ((strncmp(buf, "-----END ", 9) != 0) ||
|
|
|
|
(strncmp(nameB->data, &(buf[9]), i) != 0) ||
|
|
|
|
(strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_DecodeInit(ctx);
|
|
|
|
i = EVP_DecodeUpdate(ctx,
|
2015-01-22 03:40:55 +00:00
|
|
|
(unsigned char *)dataB->data, &bl,
|
|
|
|
(unsigned char *)dataB->data, bl);
|
|
|
|
if (i < 0) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-12-11 15:10:38 +00:00
|
|
|
i = EVP_DecodeFinal(ctx, (unsigned char *)&(dataB->data[bl]), &k);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (i < 0) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
bl += k;
|
|
|
|
|
|
|
|
if (bl == 0)
|
|
|
|
goto err;
|
|
|
|
*name = nameB->data;
|
|
|
|
*header = headerB->data;
|
|
|
|
*data = (unsigned char *)dataB->data;
|
|
|
|
*len = bl;
|
|
|
|
OPENSSL_free(nameB);
|
|
|
|
OPENSSL_free(headerB);
|
|
|
|
OPENSSL_free(dataB);
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_ENCODE_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return (1);
|
|
|
|
err:
|
|
|
|
BUF_MEM_free(nameB);
|
|
|
|
BUF_MEM_free(headerB);
|
|
|
|
BUF_MEM_free(dataB);
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_ENCODE_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check pem string and return prefix length. If for example the pem_str ==
|
|
|
|
* "RSA PRIVATE KEY" and suffix = "PRIVATE KEY" the return value is 3 for the
|
|
|
|
* string "RSA".
|
2006-03-23 11:54:51 +00:00
|
|
|
*/
|
|
|
|
|
2006-03-23 18:02:23 +00:00
|
|
|
int pem_check_suffix(const char *pem_str, const char *suffix)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int pem_len = strlen(pem_str);
|
|
|
|
int suffix_len = strlen(suffix);
|
|
|
|
const char *p;
|
|
|
|
if (suffix_len + 1 >= pem_len)
|
|
|
|
return 0;
|
|
|
|
p = pem_str + pem_len - suffix_len;
|
|
|
|
if (strcmp(p, suffix))
|
|
|
|
return 0;
|
|
|
|
p--;
|
|
|
|
if (*p != ' ')
|
|
|
|
return 0;
|
|
|
|
return p - pem_str;
|
|
|
|
}
|