Latest update.
This commit is contained in:
+14
-17
@@ -17,6 +17,7 @@
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/dsa.h>
|
||||
#include <openssl/dh.h>
|
||||
#include "pem_local.h"
|
||||
|
||||
#ifndef OPENSSL_NO_RSA
|
||||
static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
|
||||
@@ -33,6 +34,7 @@ IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
|
||||
|
||||
IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
|
||||
IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
|
||||
IMPLEMENT_PEM_rw(X509_PUBKEY, X509_PUBKEY, PEM_STRING_PUBLIC, X509_PUBKEY)
|
||||
IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
|
||||
|
||||
IMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
|
||||
@@ -79,14 +81,9 @@ RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
|
||||
|
||||
# endif
|
||||
|
||||
IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA,
|
||||
RSAPrivateKey)
|
||||
|
||||
|
||||
IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC,
|
||||
RSAPublicKey) IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA,
|
||||
PEM_STRING_PUBLIC,
|
||||
RSA_PUBKEY)
|
||||
IMPLEMENT_PEM_write_cb(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
|
||||
IMPLEMENT_PEM_rw(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
|
||||
IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_DSA
|
||||
static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
|
||||
@@ -113,9 +110,8 @@ DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
|
||||
return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
|
||||
}
|
||||
|
||||
IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA,
|
||||
DSAPrivateKey)
|
||||
IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
|
||||
IMPLEMENT_PEM_write_cb(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
|
||||
IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
|
||||
# ifndef OPENSSL_NO_STDIO
|
||||
DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
|
||||
{
|
||||
@@ -126,7 +122,7 @@ DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
|
||||
|
||||
# endif
|
||||
|
||||
IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
|
||||
IMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_EC
|
||||
static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
|
||||
@@ -153,8 +149,8 @@ EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
|
||||
return pkey_get_eckey(pktmp, key); /* will free pktmp */
|
||||
}
|
||||
|
||||
IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
|
||||
ECPKParameters)
|
||||
IMPLEMENT_PEM_rw(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
|
||||
ECPKParameters)
|
||||
|
||||
|
||||
IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
|
||||
@@ -175,7 +171,8 @@ EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
|
||||
|
||||
#ifndef OPENSSL_NO_DH
|
||||
|
||||
IMPLEMENT_PEM_write_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
|
||||
IMPLEMENT_PEM_write_const(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
|
||||
IMPLEMENT_PEM_write(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
|
||||
IMPLEMENT_PEM_write(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
|
||||
#endif
|
||||
IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
|
||||
IMPLEMENT_PEM_provided_write(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
|
||||
IMPLEMENT_PEM_read(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
|
||||
@@ -244,15 +244,15 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
|
||||
}
|
||||
|
||||
/* A TJH addition */
|
||||
int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
|
||||
unsigned char *kstr, int klen,
|
||||
int PEM_X509_INFO_write_bio(BIO *bp, const X509_INFO *xi, EVP_CIPHER *enc,
|
||||
const unsigned char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
int i, ret = 0;
|
||||
unsigned char *data = NULL;
|
||||
const char *objstr = NULL;
|
||||
char buf[PEM_BUFSIZE];
|
||||
unsigned char *iv = NULL;
|
||||
const unsigned char *iv = NULL;
|
||||
|
||||
if (enc != NULL) {
|
||||
objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
|
||||
@@ -301,7 +301,7 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
|
||||
buf[0] = '\0';
|
||||
PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
|
||||
PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc),
|
||||
(char *)iv);
|
||||
(const char *)iv);
|
||||
|
||||
/* use the normal code to write things out */
|
||||
i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
|
||||
|
||||
+23
-8
@@ -80,7 +80,7 @@ void PEM_proc_type(char *buf, int type)
|
||||
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)
|
||||
void PEM_dek_info(char *buf, const char *type, int len, const char *str)
|
||||
{
|
||||
long i;
|
||||
char *p = buf + strlen(buf);
|
||||
@@ -288,8 +288,9 @@ int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
|
||||
|
||||
#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,
|
||||
int klen, pem_password_cb *callback, void *u)
|
||||
const void *x, const EVP_CIPHER *enc,
|
||||
const unsigned char *kstr, int klen,
|
||||
pem_password_cb *callback, void *u)
|
||||
{
|
||||
BIO *b;
|
||||
int ret;
|
||||
@@ -306,8 +307,9 @@ int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
|
||||
#endif
|
||||
|
||||
int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
|
||||
void *x, const EVP_CIPHER *enc, unsigned char *kstr,
|
||||
int klen, pem_password_cb *callback, void *u)
|
||||
const void *x, const EVP_CIPHER *enc,
|
||||
const unsigned char *kstr, int klen,
|
||||
pem_password_cb *callback, void *u)
|
||||
{
|
||||
EVP_CIPHER_CTX *ctx = NULL;
|
||||
int dsize = 0, i = 0, j = 0, ret = 0;
|
||||
@@ -680,9 +682,20 @@ int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
|
||||
#endif
|
||||
|
||||
/* Some helpers for PEM_read_bio_ex(). */
|
||||
static int sanitize_line(char *linebuf, int len, unsigned int flags)
|
||||
static int sanitize_line(char *linebuf, int len, unsigned int flags, int first_call)
|
||||
{
|
||||
int i;
|
||||
if (first_call) {
|
||||
/* Other BOMs imply unsupported multibyte encoding,
|
||||
* so don't strip them and let the error raise */
|
||||
const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
|
||||
|
||||
if (len > 3 && memcmp(linebuf, utf8_bom, 3) == 0) {
|
||||
memmove(linebuf, linebuf + 3, len - 3);
|
||||
linebuf[len - 3] = 0;
|
||||
len -= 3;
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & PEM_FLAG_EAY_COMPATIBLE) {
|
||||
/* Strip trailing whitespace */
|
||||
@@ -727,6 +740,7 @@ static int get_name(BIO *bp, char **name, unsigned int flags)
|
||||
char *linebuf;
|
||||
int ret = 0;
|
||||
int len;
|
||||
int first_call = 1;
|
||||
|
||||
/*
|
||||
* Need to hold trailing NUL (accounted for by BIO_gets() and the newline
|
||||
@@ -747,7 +761,8 @@ static int get_name(BIO *bp, char **name, unsigned int flags)
|
||||
}
|
||||
|
||||
/* Strip trailing garbage and standardize ending. */
|
||||
len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64);
|
||||
len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64, first_call);
|
||||
first_call = 0;
|
||||
|
||||
/* Allow leading empty or non-matching lines. */
|
||||
} while (strncmp(linebuf, beginstr, BEGINLEN) != 0
|
||||
@@ -819,7 +834,7 @@ static int get_header_and_data(BIO *bp, BIO **header, BIO **data, char *name,
|
||||
}
|
||||
if (!strncmp(linebuf, endstr, ENDLEN) || got_header == IN_HEADER)
|
||||
flags_mask &= ~PEM_FLAG_ONLY_B64;
|
||||
len = sanitize_line(linebuf, len, flags & flags_mask);
|
||||
len = sanitize_line(linebuf, len, flags & flags_mask, 0);
|
||||
|
||||
/* Check for end of header. */
|
||||
if (linebuf[0] == '\n') {
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
* in the file LICENSE in the source distribution or at
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
/*
|
||||
* TODO(v3.0): the IMPLEMENT macros in include/openssl/pem.h should be
|
||||
* moved here.
|
||||
*/
|
||||
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/serializer.h>
|
||||
|
||||
/* Alternative IMPLEMENT macros for provided serializers */
|
||||
|
||||
# define IMPLEMENT_PEM_provided_write_body_vars(type, asn1) \
|
||||
int ret = 0; \
|
||||
const char *pq = OSSL_SERIALIZER_##asn1##_TO_PEM_PQ; \
|
||||
OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_##type(x, pq); \
|
||||
\
|
||||
if (ctx != NULL && OSSL_SERIALIZER_CTX_get_serializer(ctx) == NULL) { \
|
||||
OSSL_SERIALIZER_CTX_free(ctx); \
|
||||
goto legacy; \
|
||||
}
|
||||
# define IMPLEMENT_PEM_provided_write_body_pass() \
|
||||
ret = 1; \
|
||||
if (kstr == NULL && cb == NULL) { \
|
||||
if (u != NULL) { \
|
||||
kstr = u; \
|
||||
klen = strlen(u); \
|
||||
} else { \
|
||||
cb = PEM_def_callback; \
|
||||
} \
|
||||
} \
|
||||
if (enc != NULL) { \
|
||||
ret = 0; \
|
||||
if (OSSL_SERIALIZER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc), \
|
||||
NULL)) { \
|
||||
ret = 1; \
|
||||
if (kstr != NULL \
|
||||
&& !OSSL_SERIALIZER_CTX_set_passphrase(ctx, kstr, klen)) \
|
||||
ret = 0; \
|
||||
else if (cb != NULL \
|
||||
&& !OSSL_SERIALIZER_CTX_set_passphrase_cb(ctx, 1, \
|
||||
cb, u)) \
|
||||
ret = 0; \
|
||||
} \
|
||||
} \
|
||||
if (!ret) { \
|
||||
OSSL_SERIALIZER_CTX_free(ctx); \
|
||||
return 0; \
|
||||
}
|
||||
# define IMPLEMENT_PEM_provided_write_body_main(type, outtype) \
|
||||
ret = OSSL_SERIALIZER_to_##outtype(ctx, out); \
|
||||
OSSL_SERIALIZER_CTX_free(ctx); \
|
||||
return ret
|
||||
# define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
|
||||
writename) \
|
||||
legacy: \
|
||||
return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
|
||||
x, NULL, NULL, 0, NULL, NULL)
|
||||
# define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
|
||||
writename) \
|
||||
legacy: \
|
||||
return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
|
||||
x, enc, kstr, klen, cb, u)
|
||||
|
||||
# define IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, \
|
||||
OUTTYPE, outtype, writename) \
|
||||
PEM_write_fnsig(name, type, OUTTYPE, writename) \
|
||||
{ \
|
||||
IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \
|
||||
IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
|
||||
IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
|
||||
writename); \
|
||||
}
|
||||
|
||||
|
||||
# define IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, \
|
||||
OUTTYPE, outtype, writename) \
|
||||
PEM_write_cb_fnsig(name, type, OUTTYPE, writename) \
|
||||
{ \
|
||||
IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \
|
||||
IMPLEMENT_PEM_provided_write_body_pass(); \
|
||||
IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
|
||||
IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
|
||||
writename); \
|
||||
}
|
||||
|
||||
# ifdef OPENSSL_NO_STDIO
|
||||
|
||||
# define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
|
||||
# define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
|
||||
|
||||
# else
|
||||
|
||||
# define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, FILE, fp, write)
|
||||
# define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, FILE, fp, write)
|
||||
|
||||
# endif
|
||||
|
||||
# define IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, BIO, bio, write_bio)
|
||||
# define IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, BIO, bio, write_bio)
|
||||
|
||||
# define IMPLEMENT_PEM_provided_write(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
|
||||
|
||||
# define IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
|
||||
|
||||
# define IMPLEMENT_PEM_provided_rw(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_read(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write(name, type, str, asn1)
|
||||
|
||||
# define IMPLEMENT_PEM_provided_rw_cb(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_read(name, type, str, asn1) \
|
||||
IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1)
|
||||
|
||||
+16
-13
@@ -18,12 +18,14 @@
|
||||
|
||||
static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder,
|
||||
int nid, const EVP_CIPHER *enc,
|
||||
char *kstr, int klen, pem_password_cb *cb, void *u);
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u);
|
||||
|
||||
#ifndef OPENSSL_NO_STDIO
|
||||
static int do_pk8pkey_fp(FILE *bp, const EVP_PKEY *x, int isder,
|
||||
int nid, const EVP_CIPHER *enc,
|
||||
char *kstr, int klen, pem_password_cb *cb, void *u);
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u);
|
||||
#endif
|
||||
/*
|
||||
* These functions write a private key in PKCS#8 format: it is a "drop in"
|
||||
@@ -33,35 +35,35 @@ static int do_pk8pkey_fp(FILE *bp, const EVP_PKEY *x, int isder,
|
||||
*/
|
||||
|
||||
int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
|
||||
char *kstr, int klen,
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
char *kstr, int klen,
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
char *kstr, int klen,
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
|
||||
char *kstr, int klen,
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid,
|
||||
const EVP_CIPHER *enc, char *kstr, int klen,
|
||||
const EVP_CIPHER *enc, const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
X509_SIG *p8;
|
||||
@@ -149,34 +151,35 @@ EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
|
||||
#ifndef OPENSSL_NO_STDIO
|
||||
|
||||
int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
char *kstr, int klen, pem_password_cb *cb, void *u)
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
|
||||
char *kstr, int klen,
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
|
||||
char *kstr, int klen,
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
char *kstr, int klen, pem_password_cb *cb,
|
||||
void *u)
|
||||
const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
static int do_pk8pkey_fp(FILE *fp, const EVP_PKEY *x, int isder, int nid,
|
||||
const EVP_CIPHER *enc, char *kstr, int klen,
|
||||
const EVP_CIPHER *enc, const char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
BIO *bp;
|
||||
|
||||
+93
-110
@@ -17,94 +17,72 @@
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/engine.h>
|
||||
#include <openssl/dh.h>
|
||||
#include <openssl/store.h>
|
||||
#include <openssl/ui.h>
|
||||
#include <openssl/serializer.h>
|
||||
#include "crypto/store.h"
|
||||
#include "crypto/asn1.h"
|
||||
#include "crypto/evp.h"
|
||||
#include "pem_local.h"
|
||||
|
||||
int pem_check_suffix(const char *pem_str, const char *suffix);
|
||||
|
||||
EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
|
||||
void *u)
|
||||
{
|
||||
char *nm = NULL;
|
||||
const unsigned char *p = NULL;
|
||||
unsigned char *data = NULL;
|
||||
long len;
|
||||
int slen;
|
||||
EVP_PKEY *ret = NULL;
|
||||
OSSL_STORE_CTX *ctx = NULL;
|
||||
OSSL_STORE_INFO *info = NULL;
|
||||
UI_METHOD *ui_method = NULL;
|
||||
|
||||
if (!PEM_bytes_read_bio_secmem(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp,
|
||||
cb, u))
|
||||
if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
|
||||
return NULL;
|
||||
p = data;
|
||||
|
||||
if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
|
||||
PKCS8_PRIV_KEY_INFO *p8inf;
|
||||
p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
|
||||
if (p8inf == NULL)
|
||||
goto p8err;
|
||||
ret = EVP_PKCS82PKEY(p8inf);
|
||||
if (x != NULL) {
|
||||
EVP_PKEY_free((EVP_PKEY *)*x);
|
||||
*x = ret;
|
||||
}
|
||||
PKCS8_PRIV_KEY_INFO_free(p8inf);
|
||||
} else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
|
||||
PKCS8_PRIV_KEY_INFO *p8inf;
|
||||
X509_SIG *p8;
|
||||
int klen;
|
||||
char psbuf[PEM_BUFSIZE];
|
||||
p8 = d2i_X509_SIG(NULL, &p, len);
|
||||
if (p8 == NULL)
|
||||
goto p8err;
|
||||
if (cb)
|
||||
klen = cb(psbuf, PEM_BUFSIZE, 0, u);
|
||||
else
|
||||
klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
|
||||
if (klen < 0) {
|
||||
PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, PEM_R_BAD_PASSWORD_READ);
|
||||
X509_SIG_free(p8);
|
||||
if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL)
|
||||
goto err;
|
||||
#ifndef OPENSSL_NO_SECURE_HEAP
|
||||
{
|
||||
int on = 1;
|
||||
if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
|
||||
goto err;
|
||||
}
|
||||
p8inf = PKCS8_decrypt(p8, psbuf, klen);
|
||||
X509_SIG_free(p8);
|
||||
OPENSSL_cleanse(psbuf, klen);
|
||||
if (p8inf == NULL)
|
||||
goto p8err;
|
||||
ret = EVP_PKCS82PKEY(p8inf);
|
||||
if (x) {
|
||||
EVP_PKEY_free((EVP_PKEY *)*x);
|
||||
*x = ret;
|
||||
}
|
||||
PKCS8_PRIV_KEY_INFO_free(p8inf);
|
||||
} else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) {
|
||||
const EVP_PKEY_ASN1_METHOD *ameth;
|
||||
ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
|
||||
if (!ameth || !ameth->old_priv_decode)
|
||||
goto p8err;
|
||||
ret = d2i_PrivateKey(ameth->pkey_id, x, &p, len);
|
||||
}
|
||||
p8err:
|
||||
if (ret == NULL)
|
||||
PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB);
|
||||
#endif
|
||||
|
||||
while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
|
||||
if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) {
|
||||
ret = OSSL_STORE_INFO_get1_PKEY(info);
|
||||
break;
|
||||
}
|
||||
OSSL_STORE_INFO_free(info);
|
||||
}
|
||||
|
||||
if (ret != NULL && x != NULL)
|
||||
*x = ret;
|
||||
|
||||
err:
|
||||
OPENSSL_secure_free(nm);
|
||||
OPENSSL_secure_clear_free(data, len);
|
||||
ossl_store_detach_pem_bio(ctx);
|
||||
UI_destroy_method(ui_method);
|
||||
OSSL_STORE_INFO_free(info);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
unsigned char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
|
||||
{
|
||||
IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey);
|
||||
|
||||
IMPLEMENT_PEM_provided_write_body_pass();
|
||||
IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
|
||||
|
||||
legacy:
|
||||
if (x->ameth == NULL || x->ameth->priv_encode != NULL)
|
||||
return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
|
||||
(char *)kstr, klen, cb, u);
|
||||
return PEM_write_bio_PrivateKey_traditional(bp, x, enc, kstr, klen, cb, u);
|
||||
return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
|
||||
(const char *)kstr, klen, cb, u);
|
||||
return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
|
||||
}
|
||||
|
||||
int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
|
||||
int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
|
||||
const EVP_CIPHER *enc,
|
||||
unsigned char *kstr, int klen,
|
||||
const unsigned char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
char pem_str[80];
|
||||
@@ -115,51 +93,44 @@ int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
|
||||
|
||||
EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
|
||||
{
|
||||
char *nm = NULL;
|
||||
const unsigned char *p = NULL;
|
||||
unsigned char *data = NULL;
|
||||
long len;
|
||||
int slen;
|
||||
EVP_PKEY *ret = NULL;
|
||||
OSSL_STORE_CTX *ctx = NULL;
|
||||
OSSL_STORE_INFO *info = NULL;
|
||||
|
||||
if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS,
|
||||
bp, 0, NULL))
|
||||
return NULL;
|
||||
p = data;
|
||||
if ((ctx = ossl_store_attach_pem_bio(bp, UI_null(), NULL)) == NULL)
|
||||
goto err;
|
||||
|
||||
if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0) {
|
||||
ret = EVP_PKEY_new();
|
||||
if (ret == NULL)
|
||||
goto err;
|
||||
if (!EVP_PKEY_set_type_str(ret, nm, slen)
|
||||
|| !ret->ameth->param_decode
|
||||
|| !ret->ameth->param_decode(ret, &p, len)) {
|
||||
EVP_PKEY_free(ret);
|
||||
ret = NULL;
|
||||
goto err;
|
||||
}
|
||||
if (x) {
|
||||
EVP_PKEY_free((EVP_PKEY *)*x);
|
||||
*x = ret;
|
||||
while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
|
||||
if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
|
||||
ret = OSSL_STORE_INFO_get1_PARAMS(info);
|
||||
break;
|
||||
}
|
||||
OSSL_STORE_INFO_free(info);
|
||||
}
|
||||
|
||||
if (ret != NULL && x != NULL)
|
||||
*x = ret;
|
||||
|
||||
err:
|
||||
if (ret == NULL)
|
||||
PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS, ERR_R_ASN1_LIB);
|
||||
OPENSSL_free(nm);
|
||||
OPENSSL_free(data);
|
||||
ossl_store_detach_pem_bio(ctx);
|
||||
OSSL_STORE_INFO_free(info);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
|
||||
PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
|
||||
{
|
||||
char pem_str[80];
|
||||
IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters);
|
||||
|
||||
IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
|
||||
|
||||
legacy:
|
||||
if (!x->ameth || !x->ameth->param_encode)
|
||||
return 0;
|
||||
|
||||
BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
|
||||
return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
|
||||
pem_str, bp, x, NULL, NULL, 0, 0, NULL);
|
||||
pem_str, out, x, NULL, NULL, 0, 0, NULL);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_STDIO
|
||||
@@ -179,8 +150,8 @@ EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
unsigned char *kstr, int klen,
|
||||
int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
const unsigned char *kstr, int klen,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
BIO *b;
|
||||
@@ -203,25 +174,37 @@ int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
|
||||
|
||||
DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
|
||||
{
|
||||
char *nm = NULL;
|
||||
const unsigned char *p = NULL;
|
||||
unsigned char *data = NULL;
|
||||
long len;
|
||||
DH *ret = NULL;
|
||||
EVP_PKEY *pkey = NULL;
|
||||
OSSL_STORE_CTX *ctx = NULL;
|
||||
OSSL_STORE_INFO *info = NULL;
|
||||
UI_METHOD *ui_method = NULL;
|
||||
|
||||
if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u))
|
||||
if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
|
||||
return NULL;
|
||||
p = data;
|
||||
|
||||
if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0)
|
||||
ret = d2i_DHxparams(x, &p, len);
|
||||
else
|
||||
ret = d2i_DHparams(x, &p, len);
|
||||
if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL)
|
||||
goto err;
|
||||
|
||||
if (ret == NULL)
|
||||
PEMerr(PEM_F_PEM_READ_BIO_DHPARAMS, ERR_R_ASN1_LIB);
|
||||
OPENSSL_free(nm);
|
||||
OPENSSL_free(data);
|
||||
while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
|
||||
if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
|
||||
pkey = OSSL_STORE_INFO_get0_PARAMS(info);
|
||||
if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX
|
||||
|| EVP_PKEY_id(pkey) == EVP_PKEY_DH) {
|
||||
ret = EVP_PKEY_get1_DH(pkey);
|
||||
break;
|
||||
}
|
||||
}
|
||||
OSSL_STORE_INFO_free(info);
|
||||
}
|
||||
|
||||
if (ret != NULL && x != NULL)
|
||||
*x = ret;
|
||||
|
||||
err:
|
||||
ossl_store_detach_pem_bio(ctx);
|
||||
UI_destroy_method(ui_method);
|
||||
OSSL_STORE_INFO_free(info);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,8 @@ int PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type)
|
||||
return EVP_DigestInit_ex(ctx, type, NULL);
|
||||
}
|
||||
|
||||
int PEM_SignUpdate(EVP_MD_CTX *ctx, unsigned char *data, unsigned int count)
|
||||
int PEM_SignUpdate(EVP_MD_CTX *ctx,
|
||||
const unsigned char *data, unsigned int count)
|
||||
{
|
||||
return EVP_DigestUpdate(ctx, data, count);
|
||||
}
|
||||
|
||||
+6
-6
@@ -424,7 +424,7 @@ static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
|
||||
static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
|
||||
static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
|
||||
|
||||
static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
|
||||
static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub)
|
||||
{
|
||||
unsigned char *p;
|
||||
unsigned int bitlen, magic = 0, keyalg;
|
||||
@@ -473,7 +473,7 @@ static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
|
||||
return outlen;
|
||||
}
|
||||
|
||||
static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
|
||||
static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub)
|
||||
{
|
||||
unsigned char *tmp = NULL;
|
||||
int outlen, wrlen;
|
||||
@@ -599,12 +599,12 @@ static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
|
||||
return;
|
||||
}
|
||||
|
||||
int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
|
||||
int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk)
|
||||
{
|
||||
return do_i2b_bio(out, pk, 0);
|
||||
}
|
||||
|
||||
int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
|
||||
int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk)
|
||||
{
|
||||
return do_i2b_bio(out, pk, 1);
|
||||
}
|
||||
@@ -780,7 +780,7 @@ EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
|
||||
static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
int outlen = 24, pklen;
|
||||
@@ -865,7 +865,7 @@ static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
|
||||
return -1;
|
||||
}
|
||||
|
||||
int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
|
||||
int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
|
||||
pem_password_cb *cb, void *u)
|
||||
{
|
||||
unsigned char *tmp = NULL;
|
||||
|
||||
Reference in New Issue
Block a user