OpenSSL 1.1.1-pre2
This commit is contained in:
+96
-86
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Generated by util/mkerr.pl DO NOT EDIT
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -8,95 +8,106 @@
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/pemerr.h>
|
||||
|
||||
/* BEGIN ERROR CODES */
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
# define ERR_FUNC(func) ERR_PACK(ERR_LIB_PEM,func,0)
|
||||
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_PEM,0,reason)
|
||||
|
||||
static ERR_STRING_DATA PEM_str_functs[] = {
|
||||
{ERR_FUNC(PEM_F_B2I_DSS), "b2i_dss"},
|
||||
{ERR_FUNC(PEM_F_B2I_PVK_BIO), "b2i_PVK_bio"},
|
||||
{ERR_FUNC(PEM_F_B2I_RSA), "b2i_rsa"},
|
||||
{ERR_FUNC(PEM_F_CHECK_BITLEN_DSA), "check_bitlen_dsa"},
|
||||
{ERR_FUNC(PEM_F_CHECK_BITLEN_RSA), "check_bitlen_rsa"},
|
||||
{ERR_FUNC(PEM_F_D2I_PKCS8PRIVATEKEY_BIO), "d2i_PKCS8PrivateKey_bio"},
|
||||
{ERR_FUNC(PEM_F_D2I_PKCS8PRIVATEKEY_FP), "d2i_PKCS8PrivateKey_fp"},
|
||||
{ERR_FUNC(PEM_F_DO_B2I), "do_b2i"},
|
||||
{ERR_FUNC(PEM_F_DO_B2I_BIO), "do_b2i_bio"},
|
||||
{ERR_FUNC(PEM_F_DO_BLOB_HEADER), "do_blob_header"},
|
||||
{ERR_FUNC(PEM_F_DO_PK8PKEY), "do_pk8pkey"},
|
||||
{ERR_FUNC(PEM_F_DO_PK8PKEY_FP), "do_pk8pkey_fp"},
|
||||
{ERR_FUNC(PEM_F_DO_PVK_BODY), "do_PVK_body"},
|
||||
{ERR_FUNC(PEM_F_DO_PVK_HEADER), "do_PVK_header"},
|
||||
{ERR_FUNC(PEM_F_I2B_PVK), "i2b_PVK"},
|
||||
{ERR_FUNC(PEM_F_I2B_PVK_BIO), "i2b_PVK_bio"},
|
||||
{ERR_FUNC(PEM_F_LOAD_IV), "load_iv"},
|
||||
{ERR_FUNC(PEM_F_PEM_ASN1_READ), "PEM_ASN1_read"},
|
||||
{ERR_FUNC(PEM_F_PEM_ASN1_READ_BIO), "PEM_ASN1_read_bio"},
|
||||
{ERR_FUNC(PEM_F_PEM_ASN1_WRITE), "PEM_ASN1_write"},
|
||||
{ERR_FUNC(PEM_F_PEM_ASN1_WRITE_BIO), "PEM_ASN1_write_bio"},
|
||||
{ERR_FUNC(PEM_F_PEM_DEF_CALLBACK), "PEM_def_callback"},
|
||||
{ERR_FUNC(PEM_F_PEM_DO_HEADER), "PEM_do_header"},
|
||||
{ERR_FUNC(PEM_F_PEM_GET_EVP_CIPHER_INFO), "PEM_get_EVP_CIPHER_INFO"},
|
||||
{ERR_FUNC(PEM_F_PEM_READ), "PEM_read"},
|
||||
{ERR_FUNC(PEM_F_PEM_READ_BIO), "PEM_read_bio"},
|
||||
{ERR_FUNC(PEM_F_PEM_READ_BIO_DHPARAMS), "PEM_read_bio_DHparams"},
|
||||
{ERR_FUNC(PEM_F_PEM_READ_BIO_PARAMETERS), "PEM_read_bio_Parameters"},
|
||||
{ERR_FUNC(PEM_F_PEM_READ_BIO_PRIVATEKEY), "PEM_read_bio_PrivateKey"},
|
||||
{ERR_FUNC(PEM_F_PEM_READ_DHPARAMS), "PEM_read_DHparams"},
|
||||
{ERR_FUNC(PEM_F_PEM_READ_PRIVATEKEY), "PEM_read_PrivateKey"},
|
||||
{ERR_FUNC(PEM_F_PEM_SIGNFINAL), "PEM_SignFinal"},
|
||||
{ERR_FUNC(PEM_F_PEM_WRITE), "PEM_write"},
|
||||
{ERR_FUNC(PEM_F_PEM_WRITE_BIO), "PEM_write_bio"},
|
||||
{ERR_FUNC(PEM_F_PEM_WRITE_PRIVATEKEY), "PEM_write_PrivateKey"},
|
||||
{ERR_FUNC(PEM_F_PEM_X509_INFO_READ), "PEM_X509_INFO_read"},
|
||||
{ERR_FUNC(PEM_F_PEM_X509_INFO_READ_BIO), "PEM_X509_INFO_read_bio"},
|
||||
{ERR_FUNC(PEM_F_PEM_X509_INFO_WRITE_BIO), "PEM_X509_INFO_write_bio"},
|
||||
static const ERR_STRING_DATA PEM_str_functs[] = {
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_B2I_DSS, 0), "b2i_dss"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_B2I_PVK_BIO, 0), "b2i_PVK_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_B2I_RSA, 0), "b2i_rsa"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_CHECK_BITLEN_DSA, 0), "check_bitlen_dsa"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_CHECK_BITLEN_RSA, 0), "check_bitlen_rsa"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_D2I_PKCS8PRIVATEKEY_BIO, 0),
|
||||
"d2i_PKCS8PrivateKey_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_D2I_PKCS8PRIVATEKEY_FP, 0),
|
||||
"d2i_PKCS8PrivateKey_fp"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_DO_B2I, 0), "do_b2i"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_DO_B2I_BIO, 0), "do_b2i_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_DO_BLOB_HEADER, 0), "do_blob_header"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_DO_PK8PKEY, 0), "do_pk8pkey"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_DO_PK8PKEY_FP, 0), "do_pk8pkey_fp"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_DO_PVK_BODY, 0), "do_PVK_body"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_DO_PVK_HEADER, 0), "do_PVK_header"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_GET_HEADER_AND_DATA, 0),
|
||||
"get_header_and_data"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_GET_NAME, 0), "get_name"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_I2B_PVK, 0), "i2b_PVK"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_I2B_PVK_BIO, 0), "i2b_PVK_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_LOAD_IV, 0), "load_iv"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_ASN1_READ, 0), "PEM_ASN1_read"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_ASN1_READ_BIO, 0), "PEM_ASN1_read_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_ASN1_WRITE, 0), "PEM_ASN1_write"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_ASN1_WRITE_BIO, 0), "PEM_ASN1_write_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_DEF_CALLBACK, 0), "PEM_def_callback"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_DO_HEADER, 0), "PEM_do_header"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_GET_EVP_CIPHER_INFO, 0),
|
||||
"PEM_get_EVP_CIPHER_INFO"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ, 0), "PEM_read"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO, 0), "PEM_read_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO_DHPARAMS, 0),
|
||||
"PEM_read_bio_DHparams"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO_EX, 0), "PEM_read_bio_ex"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO_PARAMETERS, 0),
|
||||
"PEM_read_bio_Parameters"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO_PRIVATEKEY, 0),
|
||||
"PEM_read_bio_PrivateKey"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_DHPARAMS, 0), "PEM_read_DHparams"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_PRIVATEKEY, 0),
|
||||
"PEM_read_PrivateKey"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_SIGNFINAL, 0), "PEM_SignFinal"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_WRITE, 0), "PEM_write"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_WRITE_BIO, 0), "PEM_write_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_WRITE_PRIVATEKEY, 0),
|
||||
"PEM_write_PrivateKey"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_X509_INFO_READ, 0), "PEM_X509_INFO_read"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_X509_INFO_READ_BIO, 0),
|
||||
"PEM_X509_INFO_read_bio"},
|
||||
{ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_X509_INFO_WRITE_BIO, 0),
|
||||
"PEM_X509_INFO_write_bio"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
static ERR_STRING_DATA PEM_str_reasons[] = {
|
||||
{ERR_REASON(PEM_R_BAD_BASE64_DECODE), "bad base64 decode"},
|
||||
{ERR_REASON(PEM_R_BAD_DECRYPT), "bad decrypt"},
|
||||
{ERR_REASON(PEM_R_BAD_END_LINE), "bad end line"},
|
||||
{ERR_REASON(PEM_R_BAD_IV_CHARS), "bad iv chars"},
|
||||
{ERR_REASON(PEM_R_BAD_MAGIC_NUMBER), "bad magic number"},
|
||||
{ERR_REASON(PEM_R_BAD_PASSWORD_READ), "bad password read"},
|
||||
{ERR_REASON(PEM_R_BAD_VERSION_NUMBER), "bad version number"},
|
||||
{ERR_REASON(PEM_R_BIO_WRITE_FAILURE), "bio write failure"},
|
||||
{ERR_REASON(PEM_R_CIPHER_IS_NULL), "cipher is null"},
|
||||
{ERR_REASON(PEM_R_ERROR_CONVERTING_PRIVATE_KEY),
|
||||
"error converting private key"},
|
||||
{ERR_REASON(PEM_R_EXPECTING_PRIVATE_KEY_BLOB),
|
||||
"expecting private key blob"},
|
||||
{ERR_REASON(PEM_R_EXPECTING_PUBLIC_KEY_BLOB),
|
||||
"expecting public key blob"},
|
||||
{ERR_REASON(PEM_R_HEADER_TOO_LONG), "header too long"},
|
||||
{ERR_REASON(PEM_R_INCONSISTENT_HEADER), "inconsistent header"},
|
||||
{ERR_REASON(PEM_R_KEYBLOB_HEADER_PARSE_ERROR),
|
||||
"keyblob header parse error"},
|
||||
{ERR_REASON(PEM_R_KEYBLOB_TOO_SHORT), "keyblob too short"},
|
||||
{ERR_REASON(PEM_R_MISSING_DEK_IV), "missing dek iv"},
|
||||
{ERR_REASON(PEM_R_NOT_DEK_INFO), "not dek info"},
|
||||
{ERR_REASON(PEM_R_NOT_ENCRYPTED), "not encrypted"},
|
||||
{ERR_REASON(PEM_R_NOT_PROC_TYPE), "not proc type"},
|
||||
{ERR_REASON(PEM_R_NO_START_LINE), "no start line"},
|
||||
{ERR_REASON(PEM_R_PROBLEMS_GETTING_PASSWORD),
|
||||
"problems getting password"},
|
||||
{ERR_REASON(PEM_R_PVK_DATA_TOO_SHORT), "pvk data too short"},
|
||||
{ERR_REASON(PEM_R_PVK_TOO_SHORT), "pvk too short"},
|
||||
{ERR_REASON(PEM_R_READ_KEY), "read key"},
|
||||
{ERR_REASON(PEM_R_SHORT_HEADER), "short header"},
|
||||
{ERR_REASON(PEM_R_UNEXPECTED_DEK_IV), "unexpected dek iv"},
|
||||
{ERR_REASON(PEM_R_UNSUPPORTED_CIPHER), "unsupported cipher"},
|
||||
{ERR_REASON(PEM_R_UNSUPPORTED_ENCRYPTION), "unsupported encryption"},
|
||||
{ERR_REASON(PEM_R_UNSUPPORTED_KEY_COMPONENTS),
|
||||
"unsupported key components"},
|
||||
static const ERR_STRING_DATA PEM_str_reasons[] = {
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_BASE64_DECODE), "bad base64 decode"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_DECRYPT), "bad decrypt"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_END_LINE), "bad end line"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_IV_CHARS), "bad iv chars"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_MAGIC_NUMBER), "bad magic number"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_PASSWORD_READ), "bad password read"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_VERSION_NUMBER), "bad version number"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BIO_WRITE_FAILURE), "bio write failure"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_CIPHER_IS_NULL), "cipher is null"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_ERROR_CONVERTING_PRIVATE_KEY),
|
||||
"error converting private key"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_EXPECTING_PRIVATE_KEY_BLOB),
|
||||
"expecting private key blob"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_EXPECTING_PUBLIC_KEY_BLOB),
|
||||
"expecting public key blob"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_HEADER_TOO_LONG), "header too long"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_INCONSISTENT_HEADER),
|
||||
"inconsistent header"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_KEYBLOB_HEADER_PARSE_ERROR),
|
||||
"keyblob header parse error"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_KEYBLOB_TOO_SHORT), "keyblob too short"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_MISSING_DEK_IV), "missing dek iv"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_NOT_DEK_INFO), "not dek info"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_NOT_ENCRYPTED), "not encrypted"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_NOT_PROC_TYPE), "not proc type"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_NO_START_LINE), "no start line"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_PROBLEMS_GETTING_PASSWORD),
|
||||
"problems getting password"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_PVK_DATA_TOO_SHORT), "pvk data too short"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_PVK_TOO_SHORT), "pvk too short"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_READ_KEY), "read key"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_SHORT_HEADER), "short header"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_UNEXPECTED_DEK_IV), "unexpected dek iv"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_UNSUPPORTED_CIPHER), "unsupported cipher"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_UNSUPPORTED_ENCRYPTION),
|
||||
"unsupported encryption"},
|
||||
{ERR_PACK(ERR_LIB_PEM, 0, PEM_R_UNSUPPORTED_KEY_COMPONENTS),
|
||||
"unsupported key components"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
@@ -105,10 +116,9 @@ static ERR_STRING_DATA PEM_str_reasons[] = {
|
||||
int ERR_load_PEM_strings(void)
|
||||
{
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
if (ERR_func_error_string(PEM_str_functs[0].error) == NULL) {
|
||||
ERR_load_strings(0, PEM_str_functs);
|
||||
ERR_load_strings(0, PEM_str_reasons);
|
||||
ERR_load_strings_const(PEM_str_functs);
|
||||
ERR_load_strings_const(PEM_str_reasons);
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
|
||||
+12
-9
@@ -26,12 +26,12 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
|
||||
|
||||
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
||||
PEMerr(PEM_F_PEM_X509_INFO_READ, ERR_R_BUF_LIB);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
||||
ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
|
||||
BIO_free(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -240,7 +240,7 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
|
||||
OPENSSL_free(name);
|
||||
OPENSSL_free(header);
|
||||
OPENSSL_free(data);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* A TJH addition */
|
||||
@@ -256,7 +256,13 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
|
||||
|
||||
if (enc != NULL) {
|
||||
objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
|
||||
if (objstr == NULL) {
|
||||
if (objstr == NULL
|
||||
/*
|
||||
* 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)) {
|
||||
PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER);
|
||||
goto err;
|
||||
}
|
||||
@@ -291,10 +297,7 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* create the right magic header stuff */
|
||||
OPENSSL_assert(strlen(objstr) + 23
|
||||
+ 2 * EVP_CIPHER_iv_length(enc) + 13 <=
|
||||
sizeof(buf));
|
||||
/* Create the right magic header stuff */
|
||||
buf[0] = '\0';
|
||||
PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
|
||||
PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc),
|
||||
@@ -330,5 +333,5 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
|
||||
|
||||
err:
|
||||
OPENSSL_cleanse(buf, PEM_BUFSIZE);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
+366
-231
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -8,7 +8,7 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include "internal/ctype.h"
|
||||
#include <string.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include <openssl/buffer.h>
|
||||
@@ -30,12 +30,8 @@ int pem_check_suffix(const char *pem_str, const char *suffix);
|
||||
|
||||
int PEM_def_callback(char *buf, int num, int w, void *key)
|
||||
{
|
||||
#if defined(OPENSSL_NO_STDIO) || defined(OPENSSL_NO_UI)
|
||||
int i;
|
||||
#else
|
||||
int i, j;
|
||||
const char *prompt;
|
||||
#endif
|
||||
|
||||
if (key) {
|
||||
i = strlen(key);
|
||||
@@ -44,10 +40,6 @@ int PEM_def_callback(char *buf, int num, int w, void *key)
|
||||
return i;
|
||||
}
|
||||
|
||||
#if defined(OPENSSL_NO_STDIO) || defined(OPENSSL_NO_UI)
|
||||
PEMerr(PEM_F_PEM_DEF_CALLBACK, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return -1;
|
||||
#else
|
||||
prompt = EVP_get_pw_prompt();
|
||||
if (prompt == NULL)
|
||||
prompt = "Enter PEM pass phrase:";
|
||||
@@ -74,12 +66,12 @@ int PEM_def_callback(char *buf, int num, int w, void *key)
|
||||
break;
|
||||
}
|
||||
return j;
|
||||
#endif
|
||||
}
|
||||
|
||||
void PEM_proc_type(char *buf, int type)
|
||||
{
|
||||
const char *str;
|
||||
char *p = buf + strlen(buf);
|
||||
|
||||
if (type == PEM_TYPE_ENCRYPTED)
|
||||
str = "ENCRYPTED";
|
||||
@@ -90,29 +82,29 @@ void PEM_proc_type(char *buf, int type)
|
||||
else
|
||||
str = "BAD-TYPE";
|
||||
|
||||
OPENSSL_strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE);
|
||||
OPENSSL_strlcat(buf, str, PEM_BUFSIZE);
|
||||
OPENSSL_strlcat(buf, "\n", PEM_BUFSIZE);
|
||||
BIO_snprintf(p, PEM_BUFSIZE - (size_t)(p - buf), "Proc-Type: 4,%s\n", str);
|
||||
}
|
||||
|
||||
void PEM_dek_info(char *buf, const char *type, int len, char *str)
|
||||
{
|
||||
static const unsigned char map[17] = "0123456789ABCDEF";
|
||||
long i;
|
||||
int j;
|
||||
char *p = buf + strlen(buf);
|
||||
int j = PEM_BUFSIZE - (size_t)(p - buf), n;
|
||||
|
||||
OPENSSL_strlcat(buf, "DEK-Info: ", PEM_BUFSIZE);
|
||||
OPENSSL_strlcat(buf, type, PEM_BUFSIZE);
|
||||
OPENSSL_strlcat(buf, ",", PEM_BUFSIZE);
|
||||
j = strlen(buf);
|
||||
if (j + (len * 2) + 1 > PEM_BUFSIZE)
|
||||
return;
|
||||
for (i = 0; i < len; i++) {
|
||||
buf[j + i * 2] = map[(str[i] >> 4) & 0x0f];
|
||||
buf[j + i * 2 + 1] = map[(str[i]) & 0x0f];
|
||||
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");
|
||||
}
|
||||
buf[j + i * 2] = '\n';
|
||||
buf[j + i * 2 + 1] = '\0';
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_STDIO
|
||||
@@ -124,12 +116,12 @@ void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
|
||||
|
||||
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
||||
PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
||||
ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u);
|
||||
BIO_free(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -228,28 +220,41 @@ static int check_pem(const char *nm, const char *name)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
|
||||
const char *name, BIO *bp, pem_password_cb *cb,
|
||||
void *u)
|
||||
static void pem_free(void *p, unsigned int flags, size_t num)
|
||||
{
|
||||
if (flags & PEM_FLAG_SECURE)
|
||||
OPENSSL_secure_clear_free(p, num);
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
EVP_CIPHER_INFO cipher;
|
||||
char *nm = NULL, *header = NULL;
|
||||
unsigned char *data = NULL;
|
||||
long len;
|
||||
long len = 0;
|
||||
int ret = 0;
|
||||
|
||||
for (;;) {
|
||||
if (!PEM_read_bio(bp, &nm, &header, &data, &len)) {
|
||||
do {
|
||||
pem_free(nm, flags, 0);
|
||||
pem_free(header, flags, 0);
|
||||
pem_free(data, flags, len);
|
||||
if (!PEM_read_bio_ex(bp, &nm, &header, &data, &len, flags)) {
|
||||
if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
|
||||
ERR_add_error_data(2, "Expecting: ", name);
|
||||
return 0;
|
||||
}
|
||||
if (check_pem(nm, name))
|
||||
break;
|
||||
OPENSSL_free(nm);
|
||||
OPENSSL_free(header);
|
||||
OPENSSL_free(data);
|
||||
}
|
||||
} while (!check_pem(nm, name));
|
||||
if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
|
||||
goto err;
|
||||
if (!PEM_do_header(&cipher, data, &len, cb, u))
|
||||
@@ -258,20 +263,34 @@ int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
|
||||
*pdata = data;
|
||||
*plen = len;
|
||||
|
||||
if (pnm)
|
||||
if (pnm != NULL)
|
||||
*pnm = nm;
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
if (!ret || !pnm)
|
||||
OPENSSL_free(nm);
|
||||
OPENSSL_free(header);
|
||||
if (!ret || pnm == NULL)
|
||||
pem_free(nm, flags, 0);
|
||||
pem_free(header, flags, 0);
|
||||
if (!ret)
|
||||
OPENSSL_free(data);
|
||||
pem_free(data, flags, len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_STDIO
|
||||
int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
|
||||
void *x, const EVP_CIPHER *enc, unsigned char *kstr,
|
||||
@@ -282,12 +301,12 @@ int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
|
||||
|
||||
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
||||
PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
||||
ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u);
|
||||
BIO_free(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -305,7 +324,14 @@ int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
|
||||
|
||||
if (enc != NULL) {
|
||||
objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
|
||||
if (objstr == NULL || EVP_CIPHER_iv_length(enc) == 0) {
|
||||
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)) {
|
||||
PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER);
|
||||
goto err;
|
||||
}
|
||||
@@ -342,8 +368,6 @@ int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
|
||||
#endif
|
||||
kstr = (unsigned char *)buf;
|
||||
}
|
||||
RAND_add(data, i, 0); /* put in the RSA key. */
|
||||
OPENSSL_assert(EVP_CIPHER_iv_length(enc) <= (int)sizeof(iv));
|
||||
if (RAND_bytes(iv, EVP_CIPHER_iv_length(enc)) <= 0) /* Generate a salt */
|
||||
goto err;
|
||||
/*
|
||||
@@ -356,9 +380,6 @@ int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
|
||||
if (kstr == (unsigned char *)buf)
|
||||
OPENSSL_cleanse(buf, PEM_BUFSIZE);
|
||||
|
||||
OPENSSL_assert(strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13
|
||||
<= sizeof(buf));
|
||||
|
||||
buf[0] = '\0';
|
||||
PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
|
||||
PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc), (char *)iv);
|
||||
@@ -386,7 +407,7 @@ int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
OPENSSL_cleanse(buf, PEM_BUFSIZE);
|
||||
OPENSSL_clear_free(data, (unsigned int)dsize);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
|
||||
@@ -554,14 +575,14 @@ static int load_iv(char **fromp, unsigned char *to, int num)
|
||||
v = OPENSSL_hexchar2int(*from);
|
||||
if (v < 0) {
|
||||
PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
from++;
|
||||
to[i / 2] |= v << (long)((!(i & 1)) * 4);
|
||||
}
|
||||
|
||||
*fromp = from;
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_STDIO
|
||||
@@ -573,12 +594,12 @@ int PEM_write(FILE *fp, const char *name, const char *header,
|
||||
|
||||
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
||||
PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
||||
ret = PEM_write_bio(b, name, header, data, len);
|
||||
BIO_free(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -635,12 +656,12 @@ int PEM_write_bio(BIO *bp, const char *name, const char *header,
|
||||
goto err;
|
||||
OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
|
||||
EVP_ENCODE_CTX_free(ctx);
|
||||
return (i + outl);
|
||||
return i + outl;
|
||||
err:
|
||||
OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
|
||||
EVP_ENCODE_CTX_free(ctx);
|
||||
PEMerr(PEM_F_PEM_WRITE_BIO, reason);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_STDIO
|
||||
@@ -652,186 +673,300 @@ int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
|
||||
|
||||
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
||||
PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
||||
ret = PEM_read_bio(b, name, header, data, len);
|
||||
BIO_free(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Some helpers for PEM_read_bio_ex(). */
|
||||
static int sanitize_line(char *linebuf, int len, unsigned int flags)
|
||||
{
|
||||
int i;
|
||||
|
||||
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) {
|
||||
if (!ossl_isbase64(linebuf[i]) || linebuf[i] == '\n'
|
||||
|| linebuf[i] == '\r')
|
||||
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;
|
||||
if (ossl_iscntrl(linebuf[i]))
|
||||
linebuf[i] = ' ';
|
||||
}
|
||||
len = i;
|
||||
}
|
||||
/* The caller allocated LINESIZE+1, so this is safe. */
|
||||
linebuf[len++] = '\n';
|
||||
linebuf[len] = '\0';
|
||||
return len;
|
||||
}
|
||||
|
||||
#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";
|
||||
#define BEGINLEN (sizeof(beginstr) - 1)
|
||||
#define ENDLEN (sizeof(endstr) - 1)
|
||||
#define TAILLEN (sizeof(tailstr) - 1)
|
||||
static int get_name(BIO *bp, char **name, unsigned int flags)
|
||||
{
|
||||
char *linebuf;
|
||||
int ret = 0;
|
||||
size_t len;
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
do {
|
||||
len = BIO_gets(bp, linebuf, LINESIZE);
|
||||
|
||||
if (len <= 0) {
|
||||
PEMerr(PEM_F_GET_NAME, PEM_R_NO_START_LINE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* 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:
|
||||
pem_free(linebuf, flags, LINESIZE + 1);
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
goto err;
|
||||
}
|
||||
got_header = POST_HEADER;
|
||||
tmp = *data;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
break;
|
||||
} else if (end) {
|
||||
/* Malformed input; short line not at end of data. */
|
||||
PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE);
|
||||
goto err;
|
||||
}
|
||||
/*
|
||||
* Else, a line of text -- could be header or data; we don't
|
||||
* know yet. Just pass it through.
|
||||
*/
|
||||
if (BIO_puts(tmp, linebuf) < 0)
|
||||
goto err;
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
err:
|
||||
pem_free(linebuf, flags, LINESIZE + 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read in PEM-formatted data from the given BIO.
|
||||
*
|
||||
* By nature of the PEM format, all content must be printable ASCII (except
|
||||
* for line endings). Other characters, or lines that are longer than 80
|
||||
* characters, are malformed input and will be rejected.
|
||||
*/
|
||||
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;
|
||||
|
||||
if (ctx == NULL) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
pem_free(*header, flags, 0);
|
||||
pem_free(*data, flags, 0);
|
||||
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:
|
||||
EVP_ENCODE_CTX_free(ctx);
|
||||
pem_free(name, flags, 0);
|
||||
BIO_free(headerB);
|
||||
BIO_free(dataB);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
|
||||
long *len)
|
||||
{
|
||||
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
|
||||
int end = 0, i, k, bl = 0, hl = 0, nohead = 0;
|
||||
char buf[256];
|
||||
BUF_MEM *nameB;
|
||||
BUF_MEM *headerB;
|
||||
BUF_MEM *dataB, *tmpB;
|
||||
|
||||
if (ctx == NULL) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
||||
return (0);
|
||||
}
|
||||
|
||||
nameB = BUF_MEM_new();
|
||||
headerB = BUF_MEM_new();
|
||||
dataB = BUF_MEM_new();
|
||||
if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
buf[254] = '\0';
|
||||
for (;;) {
|
||||
i = BIO_gets(bp, buf, 254);
|
||||
|
||||
if (i <= 0) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
while ((i >= 0) && (buf[i] <= ' '))
|
||||
i--;
|
||||
buf[++i] = '\n';
|
||||
buf[++i] = '\0';
|
||||
|
||||
if (strncmp(buf, "-----BEGIN ", 11) == 0) {
|
||||
i = strlen(&(buf[11]));
|
||||
|
||||
if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0)
|
||||
continue;
|
||||
if (!BUF_MEM_grow(nameB, i + 9)) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
memcpy(nameB->data, &(buf[11]), i - 6);
|
||||
nameB->data[i - 6] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
hl = 0;
|
||||
if (!BUF_MEM_grow(headerB, 256)) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
headerB->data[0] = '\0';
|
||||
for (;;) {
|
||||
i = BIO_gets(bp, buf, 254);
|
||||
if (i <= 0)
|
||||
break;
|
||||
|
||||
while ((i >= 0) && (buf[i] <= ' '))
|
||||
i--;
|
||||
buf[++i] = '\n';
|
||||
buf[++i] = '\0';
|
||||
|
||||
if (buf[0] == '\n')
|
||||
break;
|
||||
if (!BUF_MEM_grow(headerB, hl + i + 9)) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
if (strncmp(buf, "-----END ", 9) == 0) {
|
||||
nohead = 1;
|
||||
break;
|
||||
}
|
||||
memcpy(&(headerB->data[hl]), buf, i);
|
||||
headerB->data[hl + i] = '\0';
|
||||
hl += i;
|
||||
}
|
||||
|
||||
bl = 0;
|
||||
if (!BUF_MEM_grow(dataB, 1024)) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
dataB->data[0] = '\0';
|
||||
if (!nohead) {
|
||||
for (;;) {
|
||||
i = BIO_gets(bp, buf, 254);
|
||||
if (i <= 0)
|
||||
break;
|
||||
|
||||
while ((i >= 0) && (buf[i] <= ' '))
|
||||
i--;
|
||||
buf[++i] = '\n';
|
||||
buf[++i] = '\0';
|
||||
|
||||
if (i != 65)
|
||||
end = 1;
|
||||
if (strncmp(buf, "-----END ", 9) == 0)
|
||||
break;
|
||||
if (i > 65)
|
||||
break;
|
||||
if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
memcpy(&(dataB->data[bl]), buf, i);
|
||||
dataB->data[bl + i] = '\0';
|
||||
bl += i;
|
||||
if (end) {
|
||||
buf[0] = '\0';
|
||||
i = BIO_gets(bp, buf, 254);
|
||||
if (i <= 0)
|
||||
break;
|
||||
|
||||
while ((i >= 0) && (buf[i] <= ' '))
|
||||
i--;
|
||||
buf[++i] = '\n';
|
||||
buf[++i] = '\0';
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tmpB = headerB;
|
||||
headerB = dataB;
|
||||
dataB = tmpB;
|
||||
bl = hl;
|
||||
}
|
||||
i = strlen(nameB->data);
|
||||
if ((strncmp(buf, "-----END ", 9) != 0) ||
|
||||
(strncmp(nameB->data, &(buf[9]), i) != 0) ||
|
||||
(strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
EVP_DecodeInit(ctx);
|
||||
i = EVP_DecodeUpdate(ctx,
|
||||
(unsigned char *)dataB->data, &bl,
|
||||
(unsigned char *)dataB->data, bl);
|
||||
if (i < 0) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
|
||||
goto err;
|
||||
}
|
||||
i = EVP_DecodeFinal(ctx, (unsigned char *)&(dataB->data[bl]), &k);
|
||||
if (i < 0) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
|
||||
goto err;
|
||||
}
|
||||
bl += k;
|
||||
|
||||
if (bl == 0)
|
||||
goto err;
|
||||
*name = nameB->data;
|
||||
*header = headerB->data;
|
||||
*data = (unsigned char *)dataB->data;
|
||||
*len = bl;
|
||||
OPENSSL_free(nameB);
|
||||
OPENSSL_free(headerB);
|
||||
OPENSSL_free(dataB);
|
||||
EVP_ENCODE_CTX_free(ctx);
|
||||
return (1);
|
||||
err:
|
||||
BUF_MEM_free(nameB);
|
||||
BUF_MEM_free(headerB);
|
||||
BUF_MEM_free(dataB);
|
||||
EVP_ENCODE_CTX_free(ctx);
|
||||
return (0);
|
||||
return PEM_read_bio_ex(bp, name, header, data, len, PEM_FLAG_EAY_COMPATIBLE);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -32,5 +32,5 @@ void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp, void **x,
|
||||
if (ret == NULL)
|
||||
PEMerr(PEM_F_PEM_ASN1_READ_BIO, ERR_R_ASN1_LIB);
|
||||
OPENSSL_free(data);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
@@ -183,7 +183,7 @@ static int do_pk8pkey_fp(FILE *fp, EVP_PKEY *x, int isder, int nid,
|
||||
|
||||
if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
|
||||
PEMerr(PEM_F_DO_PK8PKEY_FP, ERR_R_BUF_LIB);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
|
||||
BIO_free(bp);
|
||||
|
||||
+10
-9
@@ -32,7 +32,8 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
|
||||
int slen;
|
||||
EVP_PKEY *ret = NULL;
|
||||
|
||||
if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
|
||||
if (!PEM_bytes_read_bio_secmem(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp,
|
||||
cb, u))
|
||||
return NULL;
|
||||
p = data;
|
||||
|
||||
@@ -86,9 +87,9 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
|
||||
if (ret == NULL)
|
||||
PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB);
|
||||
err:
|
||||
OPENSSL_free(nm);
|
||||
OPENSSL_clear_free(data, len);
|
||||
return (ret);
|
||||
OPENSSL_secure_free(nm);
|
||||
OPENSSL_secure_clear_free(data, len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
@@ -147,7 +148,7 @@ EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
|
||||
PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS, ERR_R_ASN1_LIB);
|
||||
OPENSSL_free(nm);
|
||||
OPENSSL_free(data);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
|
||||
@@ -170,12 +171,12 @@ EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
|
||||
|
||||
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
||||
PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
||||
ret = PEM_read_bio_PrivateKey(b, x, cb, u);
|
||||
BIO_free(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
@@ -232,12 +233,12 @@ DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
|
||||
|
||||
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
||||
PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
||||
ret = PEM_read_bio_DHparams(b, x, cb, u);
|
||||
BIO_free(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
# endif
|
||||
|
||||
|
||||
@@ -46,5 +46,5 @@ int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
|
||||
err:
|
||||
/* ctx has been zeroed by EVP_SignFinal() */
|
||||
OPENSSL_free(m);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
+6
-16
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -285,17 +285,14 @@ static EVP_PKEY *b2i_dss(const unsigned char **in,
|
||||
goto memerr;
|
||||
|
||||
BN_CTX_free(ctx);
|
||||
ctx = NULL;
|
||||
}
|
||||
if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
|
||||
goto memerr;
|
||||
pbn = qbn = gbn = NULL;
|
||||
if (!DSA_set0_key(dsa, pub_key, priv_key))
|
||||
goto memerr;
|
||||
pub_key = priv_key = NULL;
|
||||
|
||||
if (!EVP_PKEY_set1_DSA(ret, dsa))
|
||||
goto memerr;
|
||||
EVP_PKEY_set1_DSA(ret, dsa);
|
||||
DSA_free(dsa);
|
||||
*in = p;
|
||||
return ret;
|
||||
@@ -348,19 +345,12 @@ static EVP_PKEY *b2i_rsa(const unsigned char **in,
|
||||
goto memerr;
|
||||
if (!read_lebn(&pin, nbyte, &d))
|
||||
goto memerr;
|
||||
if (!RSA_set0_factors(rsa, p, q))
|
||||
goto memerr;
|
||||
p = q = NULL;
|
||||
if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
|
||||
goto memerr;
|
||||
dmp1 = dmq1 = iqmp = NULL;
|
||||
RSA_set0_factors(rsa, p, q);
|
||||
RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
|
||||
}
|
||||
if (!RSA_set0_key(rsa, n, e, d))
|
||||
goto memerr;
|
||||
n = e = d = NULL;
|
||||
RSA_set0_key(rsa, n, e, d);
|
||||
|
||||
if (!EVP_PKEY_set1_RSA(ret, rsa))
|
||||
goto memerr;
|
||||
EVP_PKEY_set1_RSA(ret, rsa);
|
||||
RSA_free(rsa);
|
||||
*in = pin;
|
||||
return ret;
|
||||
|
||||
Reference in New Issue
Block a user