/*
- * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1999-2024 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
#include <string.h>
#include "apps.h"
#include "progs.h"
+#include <openssl/asn1.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/pkcs12.h>
#include <openssl/provider.h>
#include <openssl/kdf.h>
+#include <openssl/rand.h>
#define NOKEYS 0x1
#define NOCERTS 0x2
static int alg_print(const X509_ALGOR *alg);
int cert_load(BIO *in, STACK_OF(X509) *sk);
static int set_pbe(int *ppbe, const char *str);
+static int jdk_trust(PKCS12_SAFEBAG *bag, void *cbarg);
typedef enum OPTION_choice {
OPT_COMMON,
#ifndef OPENSSL_NO_DES
OPT_DESCERT,
#endif
- OPT_EXPORT, OPT_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
+ OPT_EXPORT, OPT_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER, OPT_MACSALTLEN,
OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_NOENC, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
OPT_INKEY, OPT_CERTFILE, OPT_UNTRUSTED, OPT_PASSCERTS,
OPT_NAME, OPT_CSP, OPT_CANAME,
OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
OPT_CAFILE, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_ENGINE,
- OPT_R_ENUM, OPT_PROV_ENUM,
+ OPT_R_ENUM, OPT_PROV_ENUM, OPT_JDKTRUST,
#ifndef OPENSSL_NO_DES
OPT_LEGACY_ALG
#endif
"Encrypt output with 3DES (default PBES2 with PBKDF2 and AES-256 CBC)"},
#endif
{"macalg", OPT_MACALG, 's',
- "Digest algorithm to use in MAC (default SHA1)"},
+ "Digest algorithm to use in MAC (default SHA256)"},
{"iter", OPT_ITER, 'p', "Specify the iteration count for encryption and MAC"},
{"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
{"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration)"},
{"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"},
+ {"macsaltlen", OPT_MACSALTLEN, 'p', "Specify the salt len for MAC"},
{"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
+ {"jdktrust", OPT_JDKTRUST, 's', "Mark certificate in PKCS#12 store as trusted for JDK compatibility"},
{NULL}
};
char *name = NULL, *csp_name = NULL;
char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
int export_pkcs12 = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
+ char *jdktrust = NULL;
#ifndef OPENSSL_NO_DES
int use_legacy = 0;
#endif
/* use library defaults for the iter, maciter, cert, and key PBE */
int iter = 0, maciter = 0;
+ int macsaltlen = PKCS12_SALT_LEN;
int cert_pbe = NID_undef;
int key_pbe = NID_undef;
int ret = 1, macver = 1, add_lmk = 0, private = 0;
EVP_CIPHER *enc = (EVP_CIPHER *)default_enc;
OPTION_CHOICE o;
+ opt_set_unknown_name("cipher");
prog = opt_init(argc, argv, pkcs12_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_NOOUT:
options |= (NOKEYS | NOCERTS);
break;
+ case OPT_JDKTRUST:
+ jdktrust = opt_arg();
+ /* Adding jdk trust implies nokeys */
+ options |= NOKEYS;
+ break;
case OPT_INFO:
options |= INFO;
break;
case OPT_NOMACITER:
maciter = 1;
break;
+ case OPT_MACSALTLEN:
+ macsaltlen = opt_int_arg();
+ break;
case OPT_NOMAC:
cert_pbe = -1;
maciter = -1;
WARN_NO_EXPORT("nomaciter");
if (cert_pbe == -1 && maciter == -1)
WARN_NO_EXPORT("nomac");
+ if (macsaltlen != PKCS12_SALT_LEN)
+ WARN_NO_EXPORT("macsaltlen");
}
#ifndef OPENSSL_NO_DES
if (use_legacy) {
EVP_MD *macmd = NULL;
unsigned char *catmp = NULL;
int i;
+ ASN1_OBJECT *obj = NULL;
if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
BIO_printf(bio_err, "Nothing to export due to -noout or -nocerts and -nokeys\n");
if (!twopass)
OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
- p12 = PKCS12_create_ex(cpass, name, key, ee_cert, certs,
- key_pbe, cert_pbe, iter, -1, keytype,
- app_get0_libctx(), app_get0_propq());
+ if (jdktrust != NULL) {
+ obj = OBJ_txt2obj(jdktrust, 0);
+ }
+
+ p12 = PKCS12_create_ex2(cpass, name, key, ee_cert, certs,
+ key_pbe, cert_pbe, iter, -1, keytype,
+ app_get0_libctx(), app_get0_propq(),
+ jdk_trust, (void*)obj);
if (p12 == NULL) {
BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
goto opthelp;
}
- if (maciter != -1)
- if (!PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd)) {
+ if (maciter != -1) {
+ if (!PKCS12_set_mac(p12, mpass, -1, NULL, macsaltlen, maciter, macmd)) {
BIO_printf(bio_err, "Error creating PKCS12 MAC; no PKCS12KDF support?\n");
BIO_printf(bio_err, "Use -nomac if MAC not required and PKCS12KDF support not available.\n");
goto export_end;
}
-
+ }
assert(private);
out = bio_open_owner(outfile, FORMAT_PKCS12, private);
OSSL_STACK_OF_X509_free(certs);
OSSL_STACK_OF_X509_free(untrusted_certs);
X509_free(ee_cert);
-
+ ASN1_OBJECT_free(obj);
ERR_print_errors(bio_err);
goto end;
in = bio_open_default(infile, 'r', FORMAT_PKCS12);
if (in == NULL)
goto end;
- out = bio_open_owner(outfile, FORMAT_PEM, private);
- if (out == NULL)
- goto end;
p12 = PKCS12_init_ex(NID_pkcs7_data, app_get0_libctx(), app_get0_propq());
if (p12 == NULL) {
dump:
assert(private);
+
+ out = bio_open_owner(outfile, FORMAT_PEM, private);
+ if (out == NULL)
+ goto end;
+
if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
BIO_printf(bio_err, "Error outputting keys and certificates\n");
ERR_print_errors(bio_err);
return ret;
}
+static int jdk_trust(PKCS12_SAFEBAG *bag, void *cbarg)
+{
+ STACK_OF(X509_ATTRIBUTE) *attrs = NULL;
+ X509_ATTRIBUTE *attr = NULL;
+
+ /* Nothing to do */
+ if (cbarg == NULL)
+ return 1;
+
+ /* Get the current attrs */
+ attrs = (STACK_OF(X509_ATTRIBUTE)*)PKCS12_SAFEBAG_get0_attrs(bag);
+
+ /* Create a new attr for the JDK Trusted Usage and add it */
+ attr = X509_ATTRIBUTE_create(NID_oracle_jdk_trustedkeyusage, V_ASN1_OBJECT, (ASN1_OBJECT*)cbarg);
+
+ /* Add the new attr, if attrs is NULL, it'll be initialised */
+ X509at_add1_attr(&attrs, attr);
+
+ /* Set the bag attrs */
+ PKCS12_SAFEBAG_set0_attrs(bag, attrs);
+
+ X509_ATTRIBUTE_free(attr);
+ return 1;
+}
+
int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
int passlen, int options, char *pempass,
const EVP_CIPHER *enc)
{
STACK_OF(PKCS7) *asafes = NULL;
- STACK_OF(PKCS12_SAFEBAG) *bags;
int i, bagnid;
int ret = 0;
PKCS7 *p7;
if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
return 0;
for (i = 0; i < sk_PKCS7_num(asafes); i++) {
+ STACK_OF(PKCS12_SAFEBAG) *bags;
+
p7 = sk_PKCS7_value(asafes, i);
bagnid = OBJ_obj2nid(p7->type);
if (bagnid == NID_pkcs7_data) {
} else if (bagnid == NID_pkcs7_encrypted) {
if (options & INFO) {
BIO_printf(bio_err, "PKCS7 Encrypted data: ");
- alg_print(p7->d.encrypted->enc_data->algorithm);
+ if (p7->d.encrypted == NULL) {
+ BIO_printf(bio_err, "<no data>\n");
+ } else {
+ alg_print(p7->d.encrypted->enc_data->algorithm);
+ }
}
bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
} else {
continue;
}
- if (!bags)
+ if (bags == NULL)
goto err;
if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
options, pempass, enc)) {
goto err;
}
sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
- bags = NULL;
}
ret = 1;
void print_attribute(BIO *out, const ASN1_TYPE *av)
{
char *value;
+ const char *ln;
+ char objbuf[80];
switch (av->type) {
case V_ASN1_BMPSTRING:
BIO_printf(out, "\n");
break;
+ case V_ASN1_OBJECT:
+ ln = OBJ_nid2ln(OBJ_obj2nid(av->value.object));
+ if (!ln)
+ ln = "";
+ OBJ_obj2txt(objbuf, sizeof(objbuf), av->value.object, 1);
+ BIO_printf(out, "%s (%s)", ln, objbuf);
+ BIO_printf(out, "\n");
+ break;
+
default:
BIO_printf(out, "<Unsupported tag %d>\n", av->type);
break;