|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <stdio.h>
|
|
Packit Service |
084de1 |
#include "crypto/ctype.h"
|
|
Packit Service |
084de1 |
#include <string.h>
|
|
Packit Service |
084de1 |
#include "internal/cryptlib.h"
|
|
Packit Service |
084de1 |
#include <openssl/buffer.h>
|
|
Packit Service |
084de1 |
#include <openssl/objects.h>
|
|
Packit Service |
084de1 |
#include <openssl/evp.h>
|
|
Packit Service |
084de1 |
#include <openssl/rand.h>
|
|
Packit Service |
084de1 |
#include <openssl/x509.h>
|
|
Packit Service |
084de1 |
#include <openssl/pem.h>
|
|
Packit Service |
084de1 |
#include <openssl/pkcs12.h>
|
|
Packit Service |
084de1 |
#include "crypto/asn1.h"
|
|
Packit Service |
084de1 |
#include <openssl/des.h>
|
|
Packit Service |
084de1 |
#include <openssl/engine.h>
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#define MIN_LENGTH 4
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int load_iv(char **fromp, unsigned char *to, int num);
|
|
Packit Service |
084de1 |
static int check_pem(const char *nm, const char *name);
|
|
Packit Service |
084de1 |
int pem_check_suffix(const char *pem_str, const char *suffix);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int PEM_def_callback(char *buf, int num, int rwflag, void *userdata)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int i, min_len;
|
|
Packit Service |
084de1 |
const char *prompt;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* We assume that the user passes a default password as userdata */
|
|
Packit Service |
084de1 |
if (userdata) {
|
|
Packit Service |
084de1 |
i = strlen(userdata);
|
|
Packit Service |
084de1 |
i = (i > num) ? num : i;
|
|
Packit Service |
084de1 |
memcpy(buf, userdata, i);
|
|
Packit Service |
084de1 |
return i;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
prompt = EVP_get_pw_prompt();
|
|
Packit Service |
084de1 |
if (prompt == NULL)
|
|
Packit Service |
084de1 |
prompt = "Enter PEM pass phrase:";
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* rwflag == 0 means decryption
|
|
Packit Service |
084de1 |
* rwflag == 1 means encryption
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* We assume that for encryption, we want a minimum length, while for
|
|
Packit Service |
084de1 |
* decryption, we cannot know any minimum length, so we assume zero.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
min_len = rwflag ? MIN_LENGTH : 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
i = EVP_read_pw_string_min(buf, min_len, num, prompt, rwflag);
|
|
Packit Service |
084de1 |
if (i != 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_DEF_CALLBACK, PEM_R_PROBLEMS_GETTING_PASSWORD);
|
|
Packit Service |
084de1 |
memset(buf, 0, (unsigned int)num);
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return strlen(buf);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void PEM_proc_type(char *buf, int type)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
const char *str;
|
|
Packit Service |
084de1 |
char *p = buf + strlen(buf);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (type == PEM_TYPE_ENCRYPTED)
|
|
Packit Service |
084de1 |
str = "ENCRYPTED";
|
|
Packit Service |
084de1 |
else if (type == PEM_TYPE_MIC_CLEAR)
|
|
Packit Service |
084de1 |
str = "MIC-CLEAR";
|
|
Packit Service |
084de1 |
else if (type == PEM_TYPE_MIC_ONLY)
|
|
Packit Service |
084de1 |
str = "MIC-ONLY";
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
str = "BAD-TYPE";
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BIO_snprintf(p, PEM_BUFSIZE - (size_t)(p - buf), "Proc-Type: 4,%s\n", str);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void PEM_dek_info(char *buf, const char *type, int len, char *str)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
long i;
|
|
Packit Service |
084de1 |
char *p = buf + strlen(buf);
|
|
Packit Service |
084de1 |
int j = PEM_BUFSIZE - (size_t)(p - buf), n;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
n = BIO_snprintf(p, j, "DEK-Info: %s,", type);
|
|
Packit Service |
084de1 |
if (n > 0) {
|
|
Packit Service |
084de1 |
j -= n;
|
|
Packit Service |
084de1 |
p += n;
|
|
Packit Service |
084de1 |
for (i = 0; i < len; i++) {
|
|
Packit Service |
084de1 |
n = BIO_snprintf(p, j, "%02X", 0xff & str[i]);
|
|
Packit Service |
084de1 |
if (n <= 0)
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
j -= n;
|
|
Packit Service |
084de1 |
p += n;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (j > 1)
|
|
Packit Service |
084de1 |
strcpy(p, "\n");
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_STDIO
|
|
Packit Service |
084de1 |
void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
|
|
Packit Service |
084de1 |
pem_password_cb *cb, void *u)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *b;
|
|
Packit Service |
084de1 |
void *ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
Packit Service |
084de1 |
ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u);
|
|
Packit Service |
084de1 |
BIO_free(b);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int check_pem(const char *nm, const char *name)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
/* Normal matching nm and name */
|
|
Packit Service |
084de1 |
if (strcmp(nm, name) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Make PEM_STRING_EVP_PKEY match any private key */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (strcmp(name, PEM_STRING_EVP_PKEY) == 0) {
|
|
Packit Service |
084de1 |
int slen;
|
|
Packit Service |
084de1 |
const EVP_PKEY_ASN1_METHOD *ameth;
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_PKCS8) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_PKCS8INF) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
slen = pem_check_suffix(nm, "PRIVATE KEY");
|
|
Packit Service |
084de1 |
if (slen > 0) {
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* NB: ENGINE implementations won't contain a deprecated old
|
|
Packit Service |
084de1 |
* private key decode function so don't look for them.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
|
|
Packit Service |
084de1 |
if (ameth && ameth->old_priv_decode)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (strcmp(name, PEM_STRING_PARAMETERS) == 0) {
|
|
Packit Service |
084de1 |
int slen;
|
|
Packit Service |
084de1 |
const EVP_PKEY_ASN1_METHOD *ameth;
|
|
Packit Service |
084de1 |
slen = pem_check_suffix(nm, "PARAMETERS");
|
|
Packit Service |
084de1 |
if (slen > 0) {
|
|
Packit Service |
084de1 |
ENGINE *e;
|
|
Packit Service |
084de1 |
ameth = EVP_PKEY_asn1_find_str(&e, nm, slen);
|
|
Packit Service |
084de1 |
if (ameth) {
|
|
Packit Service |
084de1 |
int r;
|
|
Packit Service |
084de1 |
if (ameth->param_decode)
|
|
Packit Service |
084de1 |
r = 1;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
r = 0;
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_ENGINE
|
|
Packit Service |
084de1 |
ENGINE_finish(e);
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
return r;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
/* If reading DH parameters handle X9.42 DH format too */
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_DHPARAMS) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Permit older strings */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_X509_OLD) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_X509) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_X509_REQ_OLD) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_X509_REQ) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Allow normal certs to be read as trusted certs */
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_X509) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_X509_TRUSTED) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_X509_OLD) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_X509_TRUSTED) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Some CAs use PKCS#7 with CERTIFICATE headers */
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_X509) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_PKCS7) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_PKCS7_SIGNED) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_PKCS7) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_CMS
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_X509) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_CMS) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
/* Allow CMS to be read from PKCS#7 headers */
|
|
Packit Service |
084de1 |
if (strcmp(nm, PEM_STRING_PKCS7) == 0
|
|
Packit Service |
084de1 |
&& strcmp(name, PEM_STRING_CMS) == 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static void pem_free(void *p, unsigned int flags, size_t num)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
if (flags & PEM_FLAG_SECURE)
|
|
Packit Service |
084de1 |
OPENSSL_secure_clear_free(p, num);
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
OPENSSL_free(p);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static void *pem_malloc(int num, unsigned int flags)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return (flags & PEM_FLAG_SECURE) ? OPENSSL_secure_malloc(num)
|
|
Packit Service |
084de1 |
: OPENSSL_malloc(num);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int pem_bytes_read_bio_flags(unsigned char **pdata, long *plen,
|
|
Packit Service |
084de1 |
char **pnm, const char *name, BIO *bp,
|
|
Packit Service |
084de1 |
pem_password_cb *cb, void *u,
|
|
Packit Service |
084de1 |
unsigned int flags)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
EVP_CIPHER_INFO cipher;
|
|
Packit Service |
084de1 |
char *nm = NULL, *header = NULL;
|
|
Packit Service |
084de1 |
unsigned char *data = NULL;
|
|
Packit Service |
084de1 |
long len = 0;
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
do {
|
|
Packit Service |
084de1 |
pem_free(nm, flags, 0);
|
|
Packit Service |
084de1 |
pem_free(header, flags, 0);
|
|
Packit Service |
084de1 |
pem_free(data, flags, len);
|
|
Packit Service |
084de1 |
if (!PEM_read_bio_ex(bp, &nm, &header, &data, &len, flags)) {
|
|
Packit Service |
084de1 |
if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
|
|
Packit Service |
084de1 |
ERR_add_error_data(2, "Expecting: ", name);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
} while (!check_pem(nm, name));
|
|
Packit Service |
084de1 |
if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!PEM_do_header(&cipher, data, &len, cb, u))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
*pdata = data;
|
|
Packit Service |
084de1 |
*plen = len;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (pnm != NULL)
|
|
Packit Service |
084de1 |
*pnm = nm;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
if (!ret || pnm == NULL)
|
|
Packit Service |
084de1 |
pem_free(nm, flags, 0);
|
|
Packit Service |
084de1 |
pem_free(header, flags, 0);
|
|
Packit Service |
084de1 |
if (!ret)
|
|
Packit Service |
084de1 |
pem_free(data, flags, len);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
|
|
Packit Service |
084de1 |
const char *name, BIO *bp, pem_password_cb *cb,
|
|
Packit Service |
084de1 |
void *u) {
|
|
Packit Service |
084de1 |
return pem_bytes_read_bio_flags(pdata, plen, pnm, name, bp, cb, u,
|
|
Packit Service |
084de1 |
PEM_FLAG_EAY_COMPATIBLE);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
|
|
Packit Service |
084de1 |
const char *name, BIO *bp, pem_password_cb *cb,
|
|
Packit Service |
084de1 |
void *u) {
|
|
Packit Service |
084de1 |
return pem_bytes_read_bio_flags(pdata, plen, pnm, name, bp, cb, u,
|
|
Packit Service |
084de1 |
PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_STDIO
|
|
Packit Service |
084de1 |
int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
|
|
Packit Service |
084de1 |
void *x, const EVP_CIPHER *enc, unsigned char *kstr,
|
|
Packit Service |
084de1 |
int klen, pem_password_cb *callback, void *u)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *b;
|
|
Packit Service |
084de1 |
int ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
Packit Service |
084de1 |
ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u);
|
|
Packit Service |
084de1 |
BIO_free(b);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
|
|
Packit Service |
084de1 |
void *x, const EVP_CIPHER *enc, unsigned char *kstr,
|
|
Packit Service |
084de1 |
int klen, pem_password_cb *callback, void *u)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
EVP_CIPHER_CTX *ctx = NULL;
|
|
Packit Service |
084de1 |
int dsize = 0, i = 0, j = 0, ret = 0;
|
|
Packit Service |
084de1 |
unsigned char *p, *data = NULL;
|
|
Packit Service |
084de1 |
const char *objstr = NULL;
|
|
Packit Service |
084de1 |
char buf[PEM_BUFSIZE];
|
|
Packit Service |
084de1 |
unsigned char key[EVP_MAX_KEY_LENGTH];
|
|
Packit Service |
084de1 |
unsigned char iv[EVP_MAX_IV_LENGTH];
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (enc != NULL) {
|
|
Packit Service |
084de1 |
objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
|
|
Packit Service |
084de1 |
if (objstr == NULL || EVP_CIPHER_iv_length(enc) == 0
|
|
Packit Service |
084de1 |
|| EVP_CIPHER_iv_length(enc) > (int)sizeof(iv)
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
|
|
Packit Service |
084de1 |
* fits into buf
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|| (strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13)
|
|
Packit Service |
084de1 |
> sizeof(buf)) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((dsize = i2d(x, NULL)) < 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_ASN1_LIB);
|
|
Packit Service |
084de1 |
dsize = 0;
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
/* dsize + 8 bytes are needed */
|
|
Packit Service |
084de1 |
/* actually it needs the cipher block size extra... */
|
|
Packit Service |
084de1 |
data = OPENSSL_malloc((unsigned int)dsize + 20);
|
|
Packit Service |
084de1 |
if (data == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
p = data;
|
|
Packit Service |
084de1 |
i = i2d(x, &p);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (enc != NULL) {
|
|
Packit Service |
084de1 |
if (kstr == NULL) {
|
|
Packit Service |
084de1 |
if (callback == NULL)
|
|
Packit Service |
084de1 |
klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
klen = (*callback) (buf, PEM_BUFSIZE, 1, u);
|
|
Packit Service |
084de1 |
if (klen <= 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, PEM_R_READ_KEY);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#ifdef CHARSET_EBCDIC
|
|
Packit Service |
084de1 |
/* Convert the pass phrase from EBCDIC */
|
|
Packit Service |
084de1 |
ebcdic2ascii(buf, buf, klen);
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
kstr = (unsigned char *)buf;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (RAND_bytes(iv, EVP_CIPHER_iv_length(enc)) <= 0) /* Generate a salt */
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* The 'iv' is used as the iv and as a salt. It is NOT taken from
|
|
Packit Service |
084de1 |
* the BytesToKey function
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1, key, NULL))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (kstr == (unsigned char *)buf)
|
|
Packit Service |
084de1 |
OPENSSL_cleanse(buf, PEM_BUFSIZE);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
buf[0] = '\0';
|
|
Packit Service |
084de1 |
PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
|
|
Packit Service |
084de1 |
PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc), (char *)iv);
|
|
Packit Service |
084de1 |
/* k=strlen(buf); */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
if ((ctx = EVP_CIPHER_CTX_new()) == NULL
|
|
Packit Service |
084de1 |
|| !EVP_EncryptInit_ex(ctx, enc, NULL, key, iv)
|
|
Packit Service |
084de1 |
|| !EVP_EncryptUpdate(ctx, data, &j, data, i)
|
|
Packit Service |
084de1 |
|| !EVP_EncryptFinal_ex(ctx, &(data[j]), &i))
|
|
Packit Service |
084de1 |
ret = 0;
|
|
Packit Service |
084de1 |
if (ret == 0)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
i += j;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
buf[0] = '\0';
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
i = PEM_write_bio(bp, name, buf, data, i);
|
|
Packit Service |
084de1 |
if (i <= 0)
|
|
Packit Service |
084de1 |
ret = 0;
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
OPENSSL_cleanse(key, sizeof(key));
|
|
Packit Service |
084de1 |
OPENSSL_cleanse(iv, sizeof(iv));
|
|
Packit Service |
084de1 |
EVP_CIPHER_CTX_free(ctx);
|
|
Packit Service |
084de1 |
OPENSSL_cleanse(buf, PEM_BUFSIZE);
|
|
Packit Service |
084de1 |
OPENSSL_clear_free(data, (unsigned int)dsize);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
|
|
Packit Service |
084de1 |
pem_password_cb *callback, void *u)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int ok;
|
|
Packit Service |
084de1 |
int keylen;
|
|
Packit Service |
084de1 |
long len = *plen;
|
|
Packit Service |
084de1 |
int ilen = (int) len; /* EVP_DecryptUpdate etc. take int lengths */
|
|
Packit Service |
084de1 |
EVP_CIPHER_CTX *ctx;
|
|
Packit Service |
084de1 |
unsigned char key[EVP_MAX_KEY_LENGTH];
|
|
Packit Service |
084de1 |
char buf[PEM_BUFSIZE];
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#if LONG_MAX > INT_MAX
|
|
Packit Service |
084de1 |
/* Check that we did not truncate the length */
|
|
Packit Service |
084de1 |
if (len > INT_MAX) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_HEADER_TOO_LONG);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (cipher->cipher == NULL)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
if (callback == NULL)
|
|
Packit Service |
084de1 |
keylen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u);
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
keylen = callback(buf, PEM_BUFSIZE, 0, u);
|
|
Packit Service |
084de1 |
if (keylen < 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#ifdef CHARSET_EBCDIC
|
|
Packit Service |
084de1 |
/* Convert the pass phrase from EBCDIC */
|
|
Packit Service |
084de1 |
ebcdic2ascii(buf, buf, keylen);
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]),
|
|
Packit Service |
084de1 |
(unsigned char *)buf, keylen, 1, key, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ctx = EVP_CIPHER_CTX_new();
|
|
Packit Service |
084de1 |
if (ctx == NULL)
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ok = EVP_DecryptInit_ex(ctx, cipher->cipher, NULL, key, &(cipher->iv[0]));
|
|
Packit Service |
084de1 |
if (ok)
|
|
Packit Service |
084de1 |
ok = EVP_DecryptUpdate(ctx, data, &ilen, data, ilen);
|
|
Packit Service |
084de1 |
if (ok) {
|
|
Packit Service |
084de1 |
/* Squirrel away the length of data decrypted so far. */
|
|
Packit Service |
084de1 |
*plen = ilen;
|
|
Packit Service |
084de1 |
ok = EVP_DecryptFinal_ex(ctx, &(data[ilen]), &ilen);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (ok)
|
|
Packit Service |
084de1 |
*plen += ilen;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
EVP_CIPHER_CTX_free(ctx);
|
|
Packit Service |
084de1 |
OPENSSL_cleanse((char *)buf, sizeof(buf));
|
|
Packit Service |
084de1 |
OPENSSL_cleanse((char *)key, sizeof(key));
|
|
Packit Service |
084de1 |
return ok;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* This implements a very limited PEM header parser that does not support the
|
|
Packit Service |
084de1 |
* full grammar of rfc1421. In particular, folded headers are not supported,
|
|
Packit Service |
084de1 |
* nor is additional whitespace.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* A robust implementation would make use of a library that turns the headers
|
|
Packit Service |
084de1 |
* into a BIO from which one folded line is read at a time, and is then split
|
|
Packit Service |
084de1 |
* into a header label and content. We would then parse the content of the
|
|
Packit Service |
084de1 |
* headers we care about. This is overkill for just this limited use-case, but
|
|
Packit Service |
084de1 |
* presumably we also parse rfc822-style headers for S/MIME, so a common
|
|
Packit Service |
084de1 |
* abstraction might well be more generally useful.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
static const char ProcType[] = "Proc-Type:";
|
|
Packit Service |
084de1 |
static const char ENCRYPTED[] = "ENCRYPTED";
|
|
Packit Service |
084de1 |
static const char DEKInfo[] = "DEK-Info:";
|
|
Packit Service |
084de1 |
const EVP_CIPHER *enc = NULL;
|
|
Packit Service |
084de1 |
int ivlen;
|
|
Packit Service |
084de1 |
char *dekinfostart, c;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
cipher->cipher = NULL;
|
|
Packit Service |
084de1 |
memset(cipher->iv, 0, sizeof(cipher->iv));
|
|
Packit Service |
084de1 |
if ((header == NULL) || (*header == '\0') || (*header == '\n'))
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (strncmp(header, ProcType, sizeof(ProcType)-1) != 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
header += sizeof(ProcType)-1;
|
|
Packit Service |
084de1 |
header += strspn(header, " \t");
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (*header++ != '4' || *header++ != ',')
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
header += strspn(header, " \t");
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* We expect "ENCRYPTED" followed by optional white-space + line break */
|
|
Packit Service |
084de1 |
if (strncmp(header, ENCRYPTED, sizeof(ENCRYPTED)-1) != 0 ||
|
|
Packit Service |
084de1 |
strspn(header+sizeof(ENCRYPTED)-1, " \t\r\n") == 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
header += sizeof(ENCRYPTED)-1;
|
|
Packit Service |
084de1 |
header += strspn(header, " \t\r");
|
|
Packit Service |
084de1 |
if (*header++ != '\n') {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*-
|
|
Packit Service |
084de1 |
* https://tools.ietf.org/html/rfc1421#section-4.6.1.3
|
|
Packit Service |
084de1 |
* We expect "DEK-Info: algo[,hex-parameters]"
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (strncmp(header, DEKInfo, sizeof(DEKInfo)-1) != 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
header += sizeof(DEKInfo)-1;
|
|
Packit Service |
084de1 |
header += strspn(header, " \t");
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* DEK-INFO is a comma-separated combination of algorithm name and optional
|
|
Packit Service |
084de1 |
* parameters.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
dekinfostart = header;
|
|
Packit Service |
084de1 |
header += strcspn(header, " \t,");
|
|
Packit Service |
084de1 |
c = *header;
|
|
Packit Service |
084de1 |
*header = '\0';
|
|
Packit Service |
084de1 |
cipher->cipher = enc = EVP_get_cipherbyname(dekinfostart);
|
|
Packit Service |
084de1 |
*header = c;
|
|
Packit Service |
084de1 |
header += strspn(header, " \t");
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (enc == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_UNSUPPORTED_ENCRYPTION);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
ivlen = EVP_CIPHER_iv_length(enc);
|
|
Packit Service |
084de1 |
if (ivlen > 0 && *header++ != ',') {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_MISSING_DEK_IV);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
} else if (ivlen == 0 && *header == ',') {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_UNEXPECTED_DEK_IV);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!load_iv(&header, cipher->iv, EVP_CIPHER_iv_length(enc)))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int load_iv(char **fromp, unsigned char *to, int num)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int v, i;
|
|
Packit Service |
084de1 |
char *from;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
from = *fromp;
|
|
Packit Service |
084de1 |
for (i = 0; i < num; i++)
|
|
Packit Service |
084de1 |
to[i] = 0;
|
|
Packit Service |
084de1 |
num *= 2;
|
|
Packit Service |
084de1 |
for (i = 0; i < num; i++) {
|
|
Packit Service |
084de1 |
v = OPENSSL_hexchar2int(*from);
|
|
Packit Service |
084de1 |
if (v < 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
from++;
|
|
Packit Service |
084de1 |
to[i / 2] |= v << (long)((!(i & 1)) * 4);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
*fromp = from;
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_STDIO
|
|
Packit Service |
084de1 |
int PEM_write(FILE *fp, const char *name, const char *header,
|
|
Packit Service |
084de1 |
const unsigned char *data, long len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *b;
|
|
Packit Service |
084de1 |
int ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
Packit Service |
084de1 |
ret = PEM_write_bio(b, name, header, data, len);
|
|
Packit Service |
084de1 |
BIO_free(b);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int PEM_write_bio(BIO *bp, const char *name, const char *header,
|
|
Packit Service |
084de1 |
const unsigned char *data, long len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int nlen, n, i, j, outl;
|
|
Packit Service |
084de1 |
unsigned char *buf = NULL;
|
|
Packit Service |
084de1 |
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
|
|
Packit Service |
084de1 |
int reason = ERR_R_BUF_LIB;
|
|
Packit Service |
084de1 |
int retval = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (ctx == NULL) {
|
|
Packit Service |
084de1 |
reason = ERR_R_MALLOC_FAILURE;
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
EVP_EncodeInit(ctx);
|
|
Packit Service |
084de1 |
nlen = strlen(name);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((BIO_write(bp, "-----BEGIN ", 11) != 11) ||
|
|
Packit Service |
084de1 |
(BIO_write(bp, name, nlen) != nlen) ||
|
|
Packit Service |
084de1 |
(BIO_write(bp, "-----\n", 6) != 6))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
i = strlen(header);
|
|
Packit Service |
084de1 |
if (i > 0) {
|
|
Packit Service |
084de1 |
if ((BIO_write(bp, header, i) != i) || (BIO_write(bp, "\n", 1) != 1))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
buf = OPENSSL_malloc(PEM_BUFSIZE * 8);
|
|
Packit Service |
084de1 |
if (buf == NULL) {
|
|
Packit Service |
084de1 |
reason = ERR_R_MALLOC_FAILURE;
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
i = j = 0;
|
|
Packit Service |
084de1 |
while (len > 0) {
|
|
Packit Service |
084de1 |
n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
|
|
Packit Service |
084de1 |
if (!EVP_EncodeUpdate(ctx, buf, &outl, &(data[j]), n))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
i += outl;
|
|
Packit Service |
084de1 |
len -= n;
|
|
Packit Service |
084de1 |
j += n;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
EVP_EncodeFinal(ctx, buf, &outl);
|
|
Packit Service |
084de1 |
if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if ((BIO_write(bp, "-----END ", 9) != 9) ||
|
|
Packit Service |
084de1 |
(BIO_write(bp, name, nlen) != nlen) ||
|
|
Packit Service |
084de1 |
(BIO_write(bp, "-----\n", 6) != 6))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
retval = i + outl;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
if (retval == 0)
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_WRITE_BIO, reason);
|
|
Packit Service |
084de1 |
EVP_ENCODE_CTX_free(ctx);
|
|
Packit Service |
084de1 |
OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
|
|
Packit Service |
084de1 |
return retval;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef OPENSSL_NO_STDIO
|
|
Packit Service |
084de1 |
int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
|
|
Packit Service |
084de1 |
long *len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *b;
|
|
Packit Service |
084de1 |
int ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
|
Packit Service |
084de1 |
ret = PEM_read_bio(b, name, header, data, len);
|
|
Packit Service |
084de1 |
BIO_free(b);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Some helpers for PEM_read_bio_ex(). */
|
|
Packit Service |
084de1 |
static int sanitize_line(char *linebuf, int len, unsigned int flags)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int i;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (flags & PEM_FLAG_EAY_COMPATIBLE) {
|
|
Packit Service |
084de1 |
/* Strip trailing whitespace */
|
|
Packit Service |
084de1 |
while ((len >= 0) && (linebuf[len] <= ' '))
|
|
Packit Service |
084de1 |
len--;
|
|
Packit Service |
084de1 |
/* Go back to whitespace before applying uniform line ending. */
|
|
Packit Service |
084de1 |
len++;
|
|
Packit Service |
084de1 |
} else if (flags & PEM_FLAG_ONLY_B64) {
|
|
Packit Service |
084de1 |
for (i = 0; i < len; ++i) {
|
|
Packit Service |
084de1 |
if (!ossl_isbase64(linebuf[i]) || linebuf[i] == '\n'
|
|
Packit Service |
084de1 |
|| linebuf[i] == '\r')
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
len = i;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
/* EVP_DecodeBlock strips leading and trailing whitespace, so just strip
|
|
Packit Service |
084de1 |
* control characters in-place and let everything through. */
|
|
Packit Service |
084de1 |
for (i = 0; i < len; ++i) {
|
|
Packit Service |
084de1 |
if (linebuf[i] == '\n' || linebuf[i] == '\r')
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
if (ossl_iscntrl(linebuf[i]))
|
|
Packit Service |
084de1 |
linebuf[i] = ' ';
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
len = i;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
/* The caller allocated LINESIZE+1, so this is safe. */
|
|
Packit Service |
084de1 |
linebuf[len++] = '\n';
|
|
Packit Service |
084de1 |
linebuf[len] = '\0';
|
|
Packit Service |
084de1 |
return len;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#define LINESIZE 255
|
|
Packit Service |
084de1 |
/* Note trailing spaces for begin and end. */
|
|
Packit Service |
084de1 |
static const char beginstr[] = "-----BEGIN ";
|
|
Packit Service |
084de1 |
static const char endstr[] = "-----END ";
|
|
Packit Service |
084de1 |
static const char tailstr[] = "-----\n";
|
|
Packit Service |
084de1 |
#define BEGINLEN ((int)(sizeof(beginstr) - 1))
|
|
Packit Service |
084de1 |
#define ENDLEN ((int)(sizeof(endstr) - 1))
|
|
Packit Service |
084de1 |
#define TAILLEN ((int)(sizeof(tailstr) - 1))
|
|
Packit Service |
084de1 |
static int get_name(BIO *bp, char **name, unsigned int flags)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
char *linebuf;
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
int len;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Need to hold trailing NUL (accounted for by BIO_gets() and the newline
|
|
Packit Service |
084de1 |
* that will be added by sanitize_line() (the extra '1').
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
linebuf = pem_malloc(LINESIZE + 1, flags);
|
|
Packit Service |
084de1 |
if (linebuf == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
do {
|
|
Packit Service |
084de1 |
len = BIO_gets(bp, linebuf, LINESIZE);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (len <= 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_GET_NAME, PEM_R_NO_START_LINE);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Strip trailing garbage and standardize ending. */
|
|
Packit Service |
084de1 |
len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Allow leading empty or non-matching lines. */
|
|
Packit Service |
084de1 |
} while (strncmp(linebuf, beginstr, BEGINLEN) != 0
|
|
Packit Service |
084de1 |
|| len < TAILLEN
|
|
Packit Service |
084de1 |
|| strncmp(linebuf + len - TAILLEN, tailstr, TAILLEN) != 0);
|
|
Packit Service |
084de1 |
linebuf[len - TAILLEN] = '\0';
|
|
Packit Service |
084de1 |
len = len - BEGINLEN - TAILLEN + 1;
|
|
Packit Service |
084de1 |
*name = pem_malloc(len, flags);
|
|
Packit Service |
084de1 |
if (*name == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
memcpy(*name, linebuf + BEGINLEN, len);
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
pem_free(linebuf, flags, LINESIZE + 1);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Keep track of how much of a header we've seen. */
|
|
Packit Service |
084de1 |
enum header_status {
|
|
Packit Service |
084de1 |
MAYBE_HEADER,
|
|
Packit Service |
084de1 |
IN_HEADER,
|
|
Packit Service |
084de1 |
POST_HEADER
|
|
Packit Service |
084de1 |
};
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/**
|
|
Packit Service |
084de1 |
* Extract the optional PEM header, with details on the type of content and
|
|
Packit Service |
084de1 |
* any encryption used on the contents, and the bulk of the data from the bio.
|
|
Packit Service |
084de1 |
* The end of the header is marked by a blank line; if the end-of-input marker
|
|
Packit Service |
084de1 |
* is reached prior to a blank line, there is no header.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* The header and data arguments are BIO** since we may have to swap them
|
|
Packit Service |
084de1 |
* if there is no header, for efficiency.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* We need the name of the PEM-encoded type to verify the end string.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
static int get_header_and_data(BIO *bp, BIO **header, BIO **data, char *name,
|
|
Packit Service |
084de1 |
unsigned int flags)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *tmp = *header;
|
|
Packit Service |
084de1 |
char *linebuf, *p;
|
|
Packit Service |
084de1 |
int len, line, ret = 0, end = 0;
|
|
Packit Service |
084de1 |
/* 0 if not seen (yet), 1 if reading header, 2 if finished header */
|
|
Packit Service |
084de1 |
enum header_status got_header = MAYBE_HEADER;
|
|
Packit Service |
084de1 |
unsigned int flags_mask;
|
|
Packit Service |
084de1 |
size_t namelen;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Need to hold trailing NUL (accounted for by BIO_gets() and the newline
|
|
Packit Service |
084de1 |
* that will be added by sanitize_line() (the extra '1'). */
|
|
Packit Service |
084de1 |
linebuf = pem_malloc(LINESIZE + 1, flags);
|
|
Packit Service |
084de1 |
if (linebuf == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_GET_HEADER_AND_DATA, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
for (line = 0; ; line++) {
|
|
Packit Service |
084de1 |
flags_mask = ~0u;
|
|
Packit Service |
084de1 |
len = BIO_gets(bp, linebuf, LINESIZE);
|
|
Packit Service |
084de1 |
if (len <= 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_SHORT_HEADER);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (got_header == MAYBE_HEADER) {
|
|
Packit Service |
084de1 |
if (memchr(linebuf, ':', len) != NULL)
|
|
Packit Service |
084de1 |
got_header = IN_HEADER;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (!strncmp(linebuf, endstr, ENDLEN) || got_header == IN_HEADER)
|
|
Packit Service |
084de1 |
flags_mask &= ~PEM_FLAG_ONLY_B64;
|
|
Packit Service |
084de1 |
len = sanitize_line(linebuf, len, flags & flags_mask);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Check for end of header. */
|
|
Packit Service |
084de1 |
if (linebuf[0] == '\n') {
|
|
Packit Service |
084de1 |
if (got_header == POST_HEADER) {
|
|
Packit Service |
084de1 |
/* Another blank line is an error. */
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
got_header = POST_HEADER;
|
|
Packit Service |
084de1 |
tmp = *data;
|
|
Packit Service |
084de1 |
continue;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Check for end of stream (which means there is no header). */
|
|
Packit Service |
084de1 |
if (strncmp(linebuf, endstr, ENDLEN) == 0) {
|
|
Packit Service |
084de1 |
p = linebuf + ENDLEN;
|
|
Packit Service |
084de1 |
namelen = strlen(name);
|
|
Packit Service |
084de1 |
if (strncmp(p, name, namelen) != 0 ||
|
|
Packit Service |
084de1 |
strncmp(p + namelen, tailstr, TAILLEN) != 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (got_header == MAYBE_HEADER) {
|
|
Packit Service |
084de1 |
*header = *data;
|
|
Packit Service |
084de1 |
*data = tmp;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
} else if (end) {
|
|
Packit Service |
084de1 |
/* Malformed input; short line not at end of data. */
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Else, a line of text -- could be header or data; we don't
|
|
Packit Service |
084de1 |
* know yet. Just pass it through.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (BIO_puts(tmp, linebuf) < 0)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Only encrypted files need the line length check applied.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (got_header == POST_HEADER) {
|
|
Packit Service |
084de1 |
/* 65 includes the trailing newline */
|
|
Packit Service |
084de1 |
if (len > 65)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (len < 65)
|
|
Packit Service |
084de1 |
end = 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
pem_free(linebuf, flags, LINESIZE + 1);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/**
|
|
Packit Service |
084de1 |
* Read in PEM-formatted data from the given BIO.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* By nature of the PEM format, all content must be printable ASCII (except
|
|
Packit Service |
084de1 |
* for line endings). Other characters are malformed input and will be rejected.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
int PEM_read_bio_ex(BIO *bp, char **name_out, char **header,
|
|
Packit Service |
084de1 |
unsigned char **data, long *len_out, unsigned int flags)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
|
|
Packit Service |
084de1 |
const BIO_METHOD *bmeth;
|
|
Packit Service |
084de1 |
BIO *headerB = NULL, *dataB = NULL;
|
|
Packit Service |
084de1 |
char *name = NULL;
|
|
Packit Service |
084de1 |
int len, taillen, headerlen, ret = 0;
|
|
Packit Service |
084de1 |
BUF_MEM * buf_mem;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (ctx == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
*len_out = 0;
|
|
Packit Service |
084de1 |
*name_out = *header = NULL;
|
|
Packit Service |
084de1 |
*data = NULL;
|
|
Packit Service |
084de1 |
if ((flags & PEM_FLAG_EAY_COMPATIBLE) && (flags & PEM_FLAG_ONLY_B64)) {
|
|
Packit Service |
084de1 |
/* These two are mutually incompatible; bail out. */
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_PASSED_INVALID_ARGUMENT);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
bmeth = (flags & PEM_FLAG_SECURE) ? BIO_s_secmem() : BIO_s_mem();
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
headerB = BIO_new(bmeth);
|
|
Packit Service |
084de1 |
dataB = BIO_new(bmeth);
|
|
Packit Service |
084de1 |
if (headerB == NULL || dataB == NULL) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!get_name(bp, &name, flags))
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
if (!get_header_and_data(bp, &headerB, &dataB, name, flags))
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
EVP_DecodeInit(ctx);
|
|
Packit Service |
084de1 |
BIO_get_mem_ptr(dataB, &buf_mem);
|
|
Packit Service |
084de1 |
len = buf_mem->length;
|
|
Packit Service |
084de1 |
if (EVP_DecodeUpdate(ctx, (unsigned char*)buf_mem->data, &len,
|
|
Packit Service |
084de1 |
(unsigned char*)buf_mem->data, len) < 0
|
|
Packit Service |
084de1 |
|| EVP_DecodeFinal(ctx, (unsigned char*)&(buf_mem->data[len]),
|
|
Packit Service |
084de1 |
&taillen) < 0) {
|
|
Packit Service |
084de1 |
PEMerr(PEM_F_PEM_READ_BIO_EX, PEM_R_BAD_BASE64_DECODE);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
len += taillen;
|
|
Packit Service |
084de1 |
buf_mem->length = len;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* There was no data in the PEM file; avoid malloc(0). */
|
|
Packit Service |
084de1 |
if (len == 0)
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
headerlen = BIO_get_mem_data(headerB, NULL);
|
|
Packit Service |
084de1 |
*header = pem_malloc(headerlen + 1, flags);
|
|
Packit Service |
084de1 |
*data = pem_malloc(len, flags);
|
|
Packit Service |
084de1 |
if (*header == NULL || *data == NULL) {
|
|
Packit Service |
084de1 |
pem_free(*header, flags, 0);
|
|
Packit Service |
084de1 |
pem_free(*data, flags, 0);
|
|
Packit Service |
084de1 |
goto end;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
BIO_read(headerB, *header, headerlen);
|
|
Packit Service |
084de1 |
(*header)[headerlen] = '\0';
|
|
Packit Service |
084de1 |
BIO_read(dataB, *data, len);
|
|
Packit Service |
084de1 |
*len_out = len;
|
|
Packit Service |
084de1 |
*name_out = name;
|
|
Packit Service |
084de1 |
name = NULL;
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
end:
|
|
Packit Service |
084de1 |
EVP_ENCODE_CTX_free(ctx);
|
|
Packit Service |
084de1 |
pem_free(name, flags, 0);
|
|
Packit Service |
084de1 |
BIO_free(headerB);
|
|
Packit Service |
084de1 |
BIO_free(dataB);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
|
|
Packit Service |
084de1 |
long *len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return PEM_read_bio_ex(bp, name, header, data, len, PEM_FLAG_EAY_COMPATIBLE);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Check pem string and return prefix length. If for example the pem_str ==
|
|
Packit Service |
084de1 |
* "RSA PRIVATE KEY" and suffix = "PRIVATE KEY" the return value is 3 for the
|
|
Packit Service |
084de1 |
* string "RSA".
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int pem_check_suffix(const char *pem_str, const char *suffix)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int pem_len = strlen(pem_str);
|
|
Packit Service |
084de1 |
int suffix_len = strlen(suffix);
|
|
Packit Service |
084de1 |
const char *p;
|
|
Packit Service |
084de1 |
if (suffix_len + 1 >= pem_len)
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
p = pem_str + pem_len - suffix_len;
|
|
Packit Service |
084de1 |
if (strcmp(p, suffix))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
p--;
|
|
Packit Service |
084de1 |
if (*p != ' ')
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
return p - pem_str;
|
|
Packit Service |
084de1 |
}
|