2016-05-17 18:24:46 +00:00
|
|
|
/*
|
2018-03-20 13:00:17 +00:00
|
|
|
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2018-12-06 12:48:17 +00:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 18:24:46 +00:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-08-20 21:19:17 +00:00
|
|
|
#include "internal/ctype.h"
|
2016-04-24 23:48:50 +00:00
|
|
|
#include <string.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"
|
2016-03-18 18:30:20 +00:00
|
|
|
#include <openssl/des.h>
|
|
|
|
#include <openssl/engine.h>
|
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
|
|
|
|
2018-04-25 20:53:40 +00:00
|
|
|
int PEM_def_callback(char *buf, int num, int rwflag, void *userdata)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2018-04-25 11:57:39 +00:00
|
|
|
int i, min_len;
|
2015-01-22 03:40:55 +00:00
|
|
|
const char *prompt;
|
2016-05-10 13:51:28 +00:00
|
|
|
|
2018-04-25 20:53:40 +00:00
|
|
|
/* We assume that the user passes a default password as userdata */
|
|
|
|
if (userdata) {
|
|
|
|
i = strlen(userdata);
|
2015-01-22 03:40:55 +00:00
|
|
|
i = (i > num) ? num : i;
|
2018-04-25 20:53:40 +00:00
|
|
|
memcpy(buf, userdata, i);
|
2016-05-10 13:51:28 +00:00
|
|
|
return i;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prompt = EVP_get_pw_prompt();
|
|
|
|
if (prompt == NULL)
|
|
|
|
prompt = "Enter PEM pass phrase:";
|
|
|
|
|
2018-04-25 11:57:39 +00:00
|
|
|
/*
|
2018-04-25 20:53:40 +00:00
|
|
|
* rwflag == 0 means decryption
|
|
|
|
* rwflag == 1 means encryption
|
|
|
|
*
|
|
|
|
* We assume that for encryption, we want a minimum length, while for
|
|
|
|
* decryption, we cannot know any minimum length, so we assume zero.
|
2018-04-25 11:57:39 +00:00
|
|
|
*/
|
2018-04-25 20:53:40 +00:00
|
|
|
min_len = rwflag ? MIN_LENGTH : 0;
|
2016-05-16 15:29:43 +00:00
|
|
|
|
2018-04-25 20:53:40 +00:00
|
|
|
i = EVP_read_pw_string_min(buf, min_len, num, prompt, rwflag);
|
2018-04-25 11:57:39 +00:00
|
|
|
if (i != 0) {
|
|
|
|
PEMerr(PEM_F_PEM_DEF_CALLBACK, PEM_R_PROBLEMS_GETTING_PASSWORD);
|
|
|
|
memset(buf, 0, (unsigned int)num);
|
|
|
|
return -1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2018-04-25 11:57:39 +00:00
|
|
|
return strlen(buf);
|
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;
|
2017-07-07 00:17:59 +00:00
|
|
|
char *p = buf + strlen(buf);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
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";
|
|
|
|
|
2017-07-07 00:17:59 +00:00
|
|
|
BIO_snprintf(p, PEM_BUFSIZE - (size_t)(p - buf), "Proc-Type: 4,%s\n", str);
|
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
|
|
|
{
|
|
|
|
long i;
|
2017-07-07 00:17:59 +00:00
|
|
|
char *p = buf + strlen(buf);
|
|
|
|
int j = PEM_BUFSIZE - (size_t)(p - buf), n;
|
|
|
|
|
|
|
|
n = BIO_snprintf(p, j, "DEK-Info: %s,", type);
|
|
|
|
if (n > 0) {
|
|
|
|
j -= n;
|
|
|
|
p += n;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
n = BIO_snprintf(p, j, "%02X", 0xff & str[i]);
|
|
|
|
if (n <= 0)
|
|
|
|
return;
|
|
|
|
j -= n;
|
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
if (j > 1)
|
|
|
|
strcpy(p, "\n");
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
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);
|
2017-07-06 21:29:55 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u);
|
|
|
|
BIO_free(b);
|
2017-07-06 21:29:55 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
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) {
|
|
|
|
/*
|
2016-08-05 17:56:58 +00:00
|
|
|
* NB: ENGINE implementations won't contain a deprecated old
|
2015-01-22 03:40:55 +00:00
|
|
|
* 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
|
2016-02-25 17:09:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-29 12:07:25 +00:00
|
|
|
static void pem_free(void *p, unsigned int flags, size_t num)
|
2016-02-19 03:24:27 +00:00
|
|
|
{
|
|
|
|
if (flags & PEM_FLAG_SECURE)
|
2017-07-29 12:07:25 +00:00
|
|
|
OPENSSL_secure_clear_free(p, num);
|
2016-02-19 03:24:27 +00:00
|
|
|
else
|
|
|
|
OPENSSL_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *pem_malloc(int num, unsigned int flags)
|
|
|
|
{
|
|
|
|
return (flags & PEM_FLAG_SECURE) ? OPENSSL_secure_malloc(num)
|
|
|
|
: OPENSSL_malloc(num);
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:47:12 +00:00
|
|
|
static int pem_bytes_read_bio_flags(unsigned char **pdata, long *plen,
|
|
|
|
char **pnm, const char *name, BIO *bp,
|
|
|
|
pem_password_cb *cb, void *u,
|
|
|
|
unsigned int flags)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
EVP_CIPHER_INFO cipher;
|
|
|
|
char *nm = NULL, *header = NULL;
|
|
|
|
unsigned char *data = NULL;
|
2017-07-29 12:07:25 +00:00
|
|
|
long len = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2016-02-29 21:47:12 +00:00
|
|
|
do {
|
2017-07-29 12:07:25 +00:00
|
|
|
pem_free(nm, flags, 0);
|
|
|
|
pem_free(header, flags, 0);
|
|
|
|
pem_free(data, flags, len);
|
2016-02-29 21:47:12 +00:00
|
|
|
if (!PEM_read_bio_ex(bp, &nm, &header, &data, &len, flags)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
|
|
|
|
ERR_add_error_data(2, "Expecting: ", name);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-29 21:47:12 +00:00
|
|
|
} while (!check_pem(nm, name));
|
2015-01-22 03:40:55 +00:00
|
|
|
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;
|
|
|
|
|
2016-02-29 21:47:12 +00:00
|
|
|
if (pnm != NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
*pnm = nm;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
2016-02-29 21:47:12 +00:00
|
|
|
if (!ret || pnm == NULL)
|
2017-07-29 12:07:25 +00:00
|
|
|
pem_free(nm, flags, 0);
|
|
|
|
pem_free(header, flags, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!ret)
|
2017-07-29 12:07:25 +00:00
|
|
|
pem_free(data, flags, len);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-02-29 21:47:12 +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) {
|
|
|
|
return pem_bytes_read_bio_flags(pdata, plen, pnm, name, bp, cb, u,
|
|
|
|
PEM_FLAG_EAY_COMPATIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
|
|
|
|
const char *name, BIO *bp, pem_password_cb *cb,
|
|
|
|
void *u) {
|
|
|
|
return pem_bytes_read_bio_flags(pdata, plen, pnm, name, bp, cb, u,
|
|
|
|
PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE);
|
|
|
|
}
|
|
|
|
|
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);
|
2017-07-06 21:29:55 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u);
|
|
|
|
BIO_free(b);
|
2017-07-06 21:29:55 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
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));
|
2017-06-21 14:55:56 +00:00
|
|
|
if (objstr == NULL || EVP_CIPHER_iv_length(enc) == 0
|
|
|
|
|| EVP_CIPHER_iv_length(enc) > (int)sizeof(iv)
|
|
|
|
/*
|
|
|
|
* Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
|
|
|
|
* fits into buf
|
|
|
|
*/
|
|
|
|
|| (strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13)
|
|
|
|
> sizeof(buf)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-03-28 21:57:28 +00:00
|
|
|
/* dsize + 8 bytes are needed */
|
2015-01-22 03:40:55 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2015-12-18 16:08:49 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
2017-07-06 21:29:55 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2016-04-24 23:48:50 +00:00
|
|
|
int ok;
|
|
|
|
int keylen;
|
|
|
|
long len = *plen;
|
|
|
|
int ilen = (int) len; /* EVP_DecryptUpdate etc. take int lengths */
|
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];
|
|
|
|
|
2016-04-24 23:48:50 +00:00
|
|
|
#if LONG_MAX > INT_MAX
|
|
|
|
/* Check that we did not truncate the length */
|
|
|
|
if (len > INT_MAX) {
|
|
|
|
PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_HEADER_TOO_LONG);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (cipher->cipher == NULL)
|
2016-04-24 23:48:50 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
if (callback == NULL)
|
2016-04-24 23:48:50 +00:00
|
|
|
keylen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u);
|
2015-01-22 03:40:55 +00:00
|
|
|
else
|
2016-04-24 23:48:50 +00:00
|
|
|
keylen = callback(buf, PEM_BUFSIZE, 0, u);
|
2018-05-04 12:19:44 +00:00
|
|
|
if (keylen < 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ);
|
2016-04-24 23:48:50 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
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 */
|
2016-04-24 23:48:50 +00:00
|
|
|
ebcdic2ascii(buf, buf, keylen);
|
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]),
|
2016-04-24 23:48:50 +00:00
|
|
|
(unsigned char *)buf, keylen, 1, key, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-12-13 21:08:41 +00:00
|
|
|
ctx = EVP_CIPHER_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
return 0;
|
2016-04-24 23:48:50 +00:00
|
|
|
|
|
|
|
ok = EVP_DecryptInit_ex(ctx, cipher->cipher, NULL, key, &(cipher->iv[0]));
|
|
|
|
if (ok)
|
|
|
|
ok = EVP_DecryptUpdate(ctx, data, &ilen, data, ilen);
|
|
|
|
if (ok) {
|
|
|
|
/* Squirrel away the length of data decrypted so far. */
|
|
|
|
*plen = ilen;
|
|
|
|
ok = EVP_DecryptFinal_ex(ctx, &(data[ilen]), &ilen);
|
|
|
|
}
|
|
|
|
if (ok)
|
|
|
|
*plen += ilen;
|
|
|
|
else
|
|
|
|
PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT);
|
|
|
|
|
2015-12-13 21:08:41 +00:00
|
|
|
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));
|
2016-04-24 23:48:50 +00:00
|
|
|
return ok;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-04-24 23:48:50 +00:00
|
|
|
/*
|
|
|
|
* This implements a very limited PEM header parser that does not support the
|
|
|
|
* full grammar of rfc1421. In particular, folded headers are not supported,
|
|
|
|
* nor is additional whitespace.
|
|
|
|
*
|
|
|
|
* A robust implementation would make use of a library that turns the headers
|
|
|
|
* into a BIO from which one folded line is read at a time, and is then split
|
|
|
|
* into a header label and content. We would then parse the content of the
|
|
|
|
* headers we care about. This is overkill for just this limited use-case, but
|
|
|
|
* presumably we also parse rfc822-style headers for S/MIME, so a common
|
|
|
|
* abstraction might well be more generally useful.
|
|
|
|
*/
|
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
|
|
|
{
|
2016-04-24 23:48:50 +00:00
|
|
|
static const char ProcType[] = "Proc-Type:";
|
|
|
|
static const char ENCRYPTED[] = "ENCRYPTED";
|
|
|
|
static const char DEKInfo[] = "DEK-Info:";
|
2015-01-22 03:40:55 +00:00
|
|
|
const EVP_CIPHER *enc = NULL;
|
2016-04-24 23:48:50 +00:00
|
|
|
int ivlen;
|
2016-02-10 06:37:52 +00:00
|
|
|
char *dekinfostart, c;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
cipher->cipher = NULL;
|
2018-06-28 22:13:54 +00:00
|
|
|
memset(cipher->iv, 0, sizeof(cipher->iv));
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((header == NULL) || (*header == '\0') || (*header == '\n'))
|
2016-04-24 23:48:50 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (strncmp(header, ProcType, sizeof(ProcType)-1) != 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE);
|
2016-04-24 23:48:50 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-04-24 23:48:50 +00:00
|
|
|
header += sizeof(ProcType)-1;
|
|
|
|
header += strspn(header, " \t");
|
|
|
|
|
|
|
|
if (*header++ != '4' || *header++ != ',')
|
|
|
|
return 0;
|
|
|
|
header += strspn(header, " \t");
|
|
|
|
|
|
|
|
/* We expect "ENCRYPTED" followed by optional white-space + line break */
|
|
|
|
if (strncmp(header, ENCRYPTED, sizeof(ENCRYPTED)-1) != 0 ||
|
|
|
|
strspn(header+sizeof(ENCRYPTED)-1, " \t\r\n") == 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED);
|
2016-04-24 23:48:50 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-04-24 23:48:50 +00:00
|
|
|
header += sizeof(ENCRYPTED)-1;
|
|
|
|
header += strspn(header, " \t\r");
|
|
|
|
if (*header++ != '\n') {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER);
|
2016-04-24 23:48:50 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-04-24 23:48:50 +00:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* https://tools.ietf.org/html/rfc1421#section-4.6.1.3
|
|
|
|
* We expect "DEK-Info: algo[,hex-parameters]"
|
|
|
|
*/
|
|
|
|
if (strncmp(header, DEKInfo, sizeof(DEKInfo)-1) != 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO);
|
2016-04-24 23:48:50 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-04-24 23:48:50 +00:00
|
|
|
header += sizeof(DEKInfo)-1;
|
|
|
|
header += strspn(header, " \t");
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-04-24 23:48:50 +00:00
|
|
|
/*
|
|
|
|
* DEK-INFO is a comma-separated combination of algorithm name and optional
|
|
|
|
* parameters.
|
|
|
|
*/
|
2016-02-10 06:37:52 +00:00
|
|
|
dekinfostart = header;
|
2016-04-24 23:48:50 +00:00
|
|
|
header += strcspn(header, " \t,");
|
|
|
|
c = *header;
|
2015-01-22 03:40:55 +00:00
|
|
|
*header = '\0';
|
2016-02-10 06:37:52 +00:00
|
|
|
cipher->cipher = enc = EVP_get_cipherbyname(dekinfostart);
|
2016-04-24 23:48:50 +00:00
|
|
|
*header = c;
|
|
|
|
header += strspn(header, " \t");
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (enc == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_UNSUPPORTED_ENCRYPTION);
|
2016-04-24 23:48:50 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-04-24 23:48:50 +00:00
|
|
|
ivlen = EVP_CIPHER_iv_length(enc);
|
|
|
|
if (ivlen > 0 && *header++ != ',') {
|
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_MISSING_DEK_IV);
|
|
|
|
return 0;
|
|
|
|
} else if (ivlen == 0 && *header == ',') {
|
|
|
|
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_UNEXPECTED_DEK_IV);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-17 21:13:49 +00:00
|
|
|
if (!load_iv(&header, cipher->iv, EVP_CIPHER_iv_length(enc)))
|
2016-04-24 23:48:50 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-04-24 23:48:50 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
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++) {
|
2016-05-12 19:52:58 +00:00
|
|
|
v = OPENSSL_hexchar2int(*from);
|
|
|
|
if (v < 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS);
|
2017-07-06 21:29:55 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
from++;
|
|
|
|
to[i / 2] |= v << (long)((!(i & 1)) * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
*fromp = from;
|
2017-07-06 21:29:55 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
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);
|
2017-07-06 21:29:55 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = PEM_write_bio(b, name, header, data, len);
|
|
|
|
BIO_free(b);
|
2017-07-06 21:29:55 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
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;
|
2018-03-30 16:40:08 +00:00
|
|
|
int retval = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
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);
|
2016-04-25 12:56:44 +00:00
|
|
|
if (!EVP_EncodeUpdate(ctx, buf, &outl, &(data[j]), n))
|
|
|
|
goto err;
|
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;
|
2018-03-30 16:40:08 +00:00
|
|
|
retval = i + outl;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
2018-03-30 16:40:08 +00:00
|
|
|
if (retval == 0)
|
|
|
|
PEMerr(PEM_F_PEM_WRITE_BIO, reason);
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_ENCODE_CTX_free(ctx);
|
2018-03-30 17:13:06 +00:00
|
|
|
OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
|
2018-03-30 16:40:08 +00:00
|
|
|
return retval;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
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);
|
2017-07-06 21:29:55 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
|
|
ret = PEM_read_bio(b, name, header, data, len);
|
|
|
|
BIO_free(b);
|
2017-07-06 21:29:55 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
/* Some helpers for PEM_read_bio_ex(). */
|
|
|
|
static int sanitize_line(char *linebuf, int len, unsigned int flags)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-02-19 03:24:27 +00:00
|
|
|
int i;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
if (flags & PEM_FLAG_EAY_COMPATIBLE) {
|
|
|
|
/* Strip trailing whitespace */
|
|
|
|
while ((len >= 0) && (linebuf[len] <= ' '))
|
|
|
|
len--;
|
|
|
|
/* Go back to whitespace before applying uniform line ending. */
|
|
|
|
len++;
|
|
|
|
} else if (flags & PEM_FLAG_ONLY_B64) {
|
|
|
|
for (i = 0; i < len; ++i) {
|
2017-08-20 21:19:17 +00:00
|
|
|
if (!ossl_isbase64(linebuf[i]) || linebuf[i] == '\n'
|
|
|
|
|| linebuf[i] == '\r')
|
2016-02-19 03:24:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
len = i;
|
|
|
|
} else {
|
|
|
|
/* EVP_DecodeBlock strips leading and trailing whitespace, so just strip
|
|
|
|
* control characters in-place and let everything through. */
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (linebuf[i] == '\n' || linebuf[i] == '\r')
|
|
|
|
break;
|
2017-08-20 21:19:17 +00:00
|
|
|
if (ossl_iscntrl(linebuf[i]))
|
2016-02-19 03:24:27 +00:00
|
|
|
linebuf[i] = ' ';
|
|
|
|
}
|
|
|
|
len = i;
|
2015-12-11 15:10:38 +00:00
|
|
|
}
|
2016-02-19 03:24:27 +00:00
|
|
|
/* The caller allocated LINESIZE+1, so this is safe. */
|
|
|
|
linebuf[len++] = '\n';
|
|
|
|
linebuf[len] = '\0';
|
|
|
|
return len;
|
|
|
|
}
|
2015-12-11 15:10:38 +00:00
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
#define LINESIZE 255
|
|
|
|
/* Note trailing spaces for begin and end. */
|
|
|
|
static const char beginstr[] = "-----BEGIN ";
|
|
|
|
static const char endstr[] = "-----END ";
|
|
|
|
static const char tailstr[] = "-----\n";
|
2018-01-30 18:55:44 +00:00
|
|
|
#define BEGINLEN ((int)(sizeof(beginstr) - 1))
|
|
|
|
#define ENDLEN ((int)(sizeof(endstr) - 1))
|
|
|
|
#define TAILLEN ((int)(sizeof(tailstr) - 1))
|
2016-02-19 03:24:27 +00:00
|
|
|
static int get_name(BIO *bp, char **name, unsigned int flags)
|
|
|
|
{
|
|
|
|
char *linebuf;
|
|
|
|
int ret = 0;
|
2018-01-30 18:55:44 +00:00
|
|
|
int len;
|
2016-02-19 03:24:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to hold trailing NUL (accounted for by BIO_gets() and the newline
|
|
|
|
* that will be added by sanitize_line() (the extra '1').
|
|
|
|
*/
|
|
|
|
linebuf = pem_malloc(LINESIZE + 1, flags);
|
|
|
|
if (linebuf == NULL) {
|
|
|
|
PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
do {
|
|
|
|
len = BIO_gets(bp, linebuf, LINESIZE);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
if (len <= 0) {
|
|
|
|
PEMerr(PEM_F_GET_NAME, PEM_R_NO_START_LINE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
/* Strip trailing garbage and standardize ending. */
|
|
|
|
len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64);
|
|
|
|
|
|
|
|
/* Allow leading empty or non-matching lines. */
|
|
|
|
} while (strncmp(linebuf, beginstr, BEGINLEN) != 0
|
|
|
|
|| len < TAILLEN
|
|
|
|
|| strncmp(linebuf + len - TAILLEN, tailstr, TAILLEN) != 0);
|
|
|
|
linebuf[len - TAILLEN] = '\0';
|
|
|
|
len = len - BEGINLEN - TAILLEN + 1;
|
|
|
|
*name = pem_malloc(len, flags);
|
|
|
|
if (*name == NULL) {
|
|
|
|
PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
memcpy(*name, linebuf + BEGINLEN, len);
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
2017-07-29 12:07:25 +00:00
|
|
|
pem_free(linebuf, flags, LINESIZE + 1);
|
2016-02-19 03:24:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep track of how much of a header we've seen. */
|
|
|
|
enum header_status {
|
|
|
|
MAYBE_HEADER,
|
|
|
|
IN_HEADER,
|
|
|
|
POST_HEADER
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the optional PEM header, with details on the type of content and
|
|
|
|
* any encryption used on the contents, and the bulk of the data from the bio.
|
|
|
|
* The end of the header is marked by a blank line; if the end-of-input marker
|
|
|
|
* is reached prior to a blank line, there is no header.
|
|
|
|
*
|
|
|
|
* The header and data arguments are BIO** since we may have to swap them
|
|
|
|
* if there is no header, for efficiency.
|
|
|
|
*
|
|
|
|
* We need the name of the PEM-encoded type to verify the end string.
|
|
|
|
*/
|
|
|
|
static int get_header_and_data(BIO *bp, BIO **header, BIO **data, char *name,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
BIO *tmp = *header;
|
|
|
|
char *linebuf, *p;
|
|
|
|
int len, line, ret = 0, end = 0;
|
|
|
|
/* 0 if not seen (yet), 1 if reading header, 2 if finished header */
|
|
|
|
enum header_status got_header = MAYBE_HEADER;
|
|
|
|
unsigned int flags_mask;
|
|
|
|
size_t namelen;
|
|
|
|
|
|
|
|
/* Need to hold trailing NUL (accounted for by BIO_gets() and the newline
|
|
|
|
* that will be added by sanitize_line() (the extra '1'). */
|
|
|
|
linebuf = pem_malloc(LINESIZE + 1, flags);
|
|
|
|
if (linebuf == NULL) {
|
|
|
|
PEMerr(PEM_F_GET_HEADER_AND_DATA, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
for (line = 0; ; line++) {
|
|
|
|
flags_mask = ~0u;
|
|
|
|
len = BIO_gets(bp, linebuf, LINESIZE);
|
|
|
|
if (len <= 0) {
|
|
|
|
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_SHORT_HEADER);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
if (got_header == MAYBE_HEADER) {
|
|
|
|
if (memchr(linebuf, ':', len) != NULL)
|
|
|
|
got_header = IN_HEADER;
|
|
|
|
}
|
|
|
|
if (!strncmp(linebuf, endstr, ENDLEN) || got_header == IN_HEADER)
|
|
|
|
flags_mask &= ~PEM_FLAG_ONLY_B64;
|
|
|
|
len = sanitize_line(linebuf, len, flags & flags_mask);
|
|
|
|
|
|
|
|
/* Check for end of header. */
|
|
|
|
if (linebuf[0] == '\n') {
|
|
|
|
if (got_header == POST_HEADER) {
|
|
|
|
/* Another blank line is an error. */
|
|
|
|
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-02-19 03:24:27 +00:00
|
|
|
got_header = POST_HEADER;
|
|
|
|
tmp = *data;
|
|
|
|
continue;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
/* Check for end of stream (which means there is no header). */
|
|
|
|
if (strncmp(linebuf, endstr, ENDLEN) == 0) {
|
|
|
|
p = linebuf + ENDLEN;
|
|
|
|
namelen = strlen(name);
|
|
|
|
if (strncmp(p, name, namelen) != 0 ||
|
|
|
|
strncmp(p + namelen, tailstr, TAILLEN) != 0) {
|
|
|
|
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (got_header == MAYBE_HEADER) {
|
|
|
|
*header = *data;
|
|
|
|
*data = tmp;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
break;
|
2016-02-19 03:24:27 +00:00
|
|
|
} else if (end) {
|
|
|
|
/* Malformed input; short line not at end of data. */
|
|
|
|
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-02-19 03:24:27 +00:00
|
|
|
/*
|
|
|
|
* Else, a line of text -- could be header or data; we don't
|
|
|
|
* know yet. Just pass it through.
|
|
|
|
*/
|
2017-07-09 19:24:18 +00:00
|
|
|
if (BIO_puts(tmp, linebuf) < 0)
|
|
|
|
goto err;
|
2016-02-19 03:24:27 +00:00
|
|
|
/*
|
|
|
|
* Only encrypted files need the line length check applied.
|
|
|
|
*/
|
|
|
|
if (got_header == POST_HEADER) {
|
|
|
|
/* 65 includes the trailing newline */
|
|
|
|
if (len > 65)
|
|
|
|
goto err;
|
|
|
|
if (len < 65)
|
|
|
|
end = 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
ret = 1;
|
|
|
|
err:
|
2017-07-29 12:07:25 +00:00
|
|
|
pem_free(linebuf, flags, LINESIZE + 1);
|
2016-02-19 03:24:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
/**
|
|
|
|
* Read in PEM-formatted data from the given BIO.
|
|
|
|
*
|
|
|
|
* By nature of the PEM format, all content must be printable ASCII (except
|
2018-08-08 08:04:18 +00:00
|
|
|
* for line endings). Other characters are malformed input and will be rejected.
|
2016-02-19 03:24:27 +00:00
|
|
|
*/
|
|
|
|
int PEM_read_bio_ex(BIO *bp, char **name_out, char **header,
|
|
|
|
unsigned char **data, long *len_out, unsigned int flags)
|
|
|
|
{
|
|
|
|
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
|
|
|
|
const BIO_METHOD *bmeth;
|
|
|
|
BIO *headerB = NULL, *dataB = NULL;
|
|
|
|
char *name = NULL;
|
|
|
|
int len, taillen, headerlen, ret = 0;
|
|
|
|
BUF_MEM * buf_mem;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
if (ctx == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
*len_out = 0;
|
|
|
|
*name_out = *header = NULL;
|
|
|
|
*data = NULL;
|
|
|
|
if ((flags & PEM_FLAG_EAY_COMPATIBLE) && (flags & PEM_FLAG_ONLY_B64)) {
|
|
|
|
/* These two are mutually incompatible; bail out. */
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_PASSED_INVALID_ARGUMENT);
|
|
|
|
goto end;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-02-19 03:24:27 +00:00
|
|
|
bmeth = (flags & PEM_FLAG_SECURE) ? BIO_s_secmem() : BIO_s_mem();
|
|
|
|
|
|
|
|
headerB = BIO_new(bmeth);
|
|
|
|
dataB = BIO_new(bmeth);
|
|
|
|
if (headerB == NULL || dataB == NULL) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto end;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:24:27 +00:00
|
|
|
if (!get_name(bp, &name, flags))
|
|
|
|
goto end;
|
|
|
|
if (!get_header_and_data(bp, &headerB, &dataB, name, flags))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
EVP_DecodeInit(ctx);
|
|
|
|
BIO_get_mem_ptr(dataB, &buf_mem);
|
|
|
|
len = buf_mem->length;
|
|
|
|
if (EVP_DecodeUpdate(ctx, (unsigned char*)buf_mem->data, &len,
|
|
|
|
(unsigned char*)buf_mem->data, len) < 0
|
|
|
|
|| EVP_DecodeFinal(ctx, (unsigned char*)&(buf_mem->data[len]),
|
|
|
|
&taillen) < 0) {
|
|
|
|
PEMerr(PEM_F_PEM_READ_BIO_EX, PEM_R_BAD_BASE64_DECODE);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
len += taillen;
|
|
|
|
buf_mem->length = len;
|
|
|
|
|
|
|
|
/* There was no data in the PEM file; avoid malloc(0). */
|
|
|
|
if (len == 0)
|
|
|
|
goto end;
|
|
|
|
headerlen = BIO_get_mem_data(headerB, NULL);
|
|
|
|
*header = pem_malloc(headerlen + 1, flags);
|
|
|
|
*data = pem_malloc(len, flags);
|
|
|
|
if (*header == NULL || *data == NULL) {
|
2017-07-29 12:07:25 +00:00
|
|
|
pem_free(*header, flags, 0);
|
|
|
|
pem_free(*data, flags, 0);
|
2016-02-19 03:24:27 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
BIO_read(headerB, *header, headerlen);
|
|
|
|
(*header)[headerlen] = '\0';
|
|
|
|
BIO_read(dataB, *data, len);
|
|
|
|
*len_out = len;
|
|
|
|
*name_out = name;
|
|
|
|
name = NULL;
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
end:
|
2015-12-11 15:10:38 +00:00
|
|
|
EVP_ENCODE_CTX_free(ctx);
|
2017-07-29 12:07:25 +00:00
|
|
|
pem_free(name, flags, 0);
|
2016-02-19 03:24:27 +00:00
|
|
|
BIO_free(headerB);
|
|
|
|
BIO_free(dataB);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
|
|
|
|
long *len)
|
|
|
|
{
|
|
|
|
return PEM_read_bio_ex(bp, name, header, data, len, PEM_FLAG_EAY_COMPATIBLE);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|