pkcs12.c revision 9dc0df1bac950d6e491f9a7c7e4888f2b301cb15
/* pkcs12.c */
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
* project.
*/
/* ====================================================================
* Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <openssl/opensslconf.h>
#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "apps.h"
#define PROG pkcs12_main
#ifdef SOLARIS_OPENSSL
extern int SUNWcry_installed;
#endif
const EVP_CIPHER *enc;
#define NOKEYS 0x1
#define NOCERTS 0x2
#define INFO 0x4
#define CLCERTS 0x8
#define CACERTS 0x10
int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen, int options, char *pempass);
int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass, int passlen, int options, char *pempass);
int MAIN(int, char **);
{
char **args;
int export_cert = 0;
int options = 0;
int chain = 0;
int badarg = 0;
int iter = PKCS12_DEFAULT_ITER;
int maciter = PKCS12_DEFAULT_ITER;
int twopass = 0;
int keytype = 0;
int ret = 1;
int macver = 1;
int noprompt = 0;
#ifndef OPENSSL_NO_ENGINE
#endif
apps_startup();
enc = EVP_des_ede3_cbc();
goto end;
while (*args) {
if (*args[0] == '-') {
#ifndef OPENSSL_NO_IDEA
#endif
#ifndef OPENSSL_NO_AES
#ifdef SOLARIS_OPENSSL
{
if (!SUNWcry_installed)
{
goto end;
}
}
{
if (!SUNWcry_installed)
{
goto end;
}
}
#else
#endif
#endif
maciter = 1;
maciter = -1;
if (args[1]) {
args++;
cert_pbe = -1;
"Unknown PBE algorithm %s\n", *args);
badarg = 1;
}
} else badarg = 1;
if (args[1]) {
args++;
key_pbe = -1;
else
"Unknown PBE algorithm %s\n", *args);
badarg = 1;
}
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
passargout = *args;
} else badarg = 1;
if (args[1]) {
args++;
noprompt = 1;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
if (args[1]) {
args++;
} else badarg = 1;
#ifndef OPENSSL_NO_ENGINE
if (args[1]) {
args++;
} else badarg = 1;
#endif
} else badarg = 1;
} else badarg = 1;
args++;
}
if (badarg) {
#ifndef OPENSSL_NO_IDEA
#endif
#ifndef OPENSSL_NO_AES
#endif
#ifndef OPENSSL_NO_ENGINE
#endif
goto end;
}
#ifndef OPENSSL_NO_ENGINE
#endif
if(passarg) {
}
goto end;
}
if(!cpass) {
}
if(cpass) {
noprompt = 1;
} else {
}
if(export_cert || inrand) {
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("read files");
#endif
if (!in) {
goto end;
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("write files");
#endif
if (!outfile) {
#ifdef OPENSSL_SYS_VMS
{
}
#endif
if (!out) {
goto end;
}
if (twopass) {
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("read MAC password");
#endif
{
goto end;
}
#ifdef CRYPTO_MDEBUG
#endif
}
if (export_cert) {
int i;
{
goto export_end;
}
chain = 0;
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("process -export_cert");
CRYPTO_push_info("reading private key");
#endif
{
if (!key)
goto export_end;
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("reading certs from input");
#endif
/* Load in all certs in input file */
{
"certificates");
if (!certs)
goto export_end;
if (key)
{
/* Look for matching private key */
for(i = 0; i < sk_X509_num(certs); i++)
{
x = sk_X509_value(certs, i);
if(X509_check_private_key(x, key))
{
ucert = x;
/* Zero keyid and alias */
/* Remove from list */
sk_X509_delete(certs, i);
break;
}
}
if (!ucert)
{
goto export_end;
}
}
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("reading certs from input 2");
#endif
/* Add any more certificates asked for */
if(certfile)
{
NULL, e,
"certificates from certfile")))
goto export_end;
while(sk_X509_num(morecerts) > 0)
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("reading certs from certfile");
#endif
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("building chain");
#endif
/* If chaining get chain from user cert */
if (chain) {
int vret;
if (!store)
{
goto export_end;
}
if (!vret) {
/* Exclude verified certificate */
/* Free first certificate */
} else {
goto export_end;
}
}
/* Add any CA names */
{
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("reading password");
#endif
if(!noprompt &&
{
goto export_end;
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("creating PKCS#12 structure");
#endif
if (!p12)
{
goto export_end;
}
if (maciter != -1)
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("writing pkcs12");
#endif
ret = 0;
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("process -export_cert: freeing");
#endif
#ifdef CRYPTO_MDEBUG
#endif
goto end;
}
goto end;
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("read import password");
#endif
goto end;
}
#ifdef CRYPTO_MDEBUG
#endif
if (options & INFO) BIO_printf (bio_err, "MAC Iteration %ld\n", p12->mac->iter ? ASN1_INTEGER_get (p12->mac->iter) : 1);
if(macver) {
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("verify MAC");
#endif
/* If we enter empty password try no password first */
/* If mac and crypto pass the same set it to NULL too */
goto end;
}
#ifdef CRYPTO_MDEBUG
#endif
}
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("output keys and certificates");
#endif
goto end;
}
#ifdef CRYPTO_MDEBUG
#endif
ret = 0;
end:
#ifdef CRYPTO_MDEBUG
#endif
}
{
int i, bagnid;
int ret = 0;
for (i = 0; i < sk_PKCS7_num (asafes); i++) {
if (bagnid == NID_pkcs7_data) {
} else if (bagnid == NID_pkcs7_encrypted) {
}
} else continue;
goto err;
}
}
ret = 1;
err:
if (asafes)
return ret;
}
{
int i;
for (i = 0; i < sk_PKCS12_SAFEBAG_num (bags); i++) {
if (!dump_certs_pkeys_bag (out,
sk_PKCS12_SAFEBAG_value (bags, i),
return 0;
}
return 1;
}
{
switch (M_PKCS12_bag_type(bag))
{
case NID_keyBag:
break;
case NID_pkcs8ShroudedKeyBag:
}
return 0;
return 0;
}
break;
case NID_certBag:
return 1;
break;
case NID_safeContentsBag:
default:
return 1;
break;
}
return 1;
}
/* Given a single certificate return a verified chain or NULL if error */
/* Hope this is OK .... */
{
int i;
/* FIXME: Should really check the return status of X509_STORE_CTX_init
* for an error, but how that fits into the return value of this
* function is less obvious. */
if (X509_verify_cert(&store_ctx) <= 0) {
i = X509_STORE_CTX_get_error (&store_ctx);
goto err;
}
i = 0;
err:
return i;
}
{
const unsigned char *p;
PBEPARAM_free (pbe);
return 0;
}
/* Load all certificates from a given file */
{
int ret;
ret = 0;
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("cert_load(): reading one cert");
#endif
#ifdef CRYPTO_MDEBUG
#endif
ret = 1;
#ifdef CRYPTO_MDEBUG
CRYPTO_push_info("cert_load(): reading one cert");
#endif
}
#ifdef CRYPTO_MDEBUG
#endif
if(ret) ERR_clear_error();
return ret;
}
/* Generalised attribute print: handle PKCS#8 and bag attributes */
{
char *value;
int i, attr_nid;
if(!attrlst) {
return 1;
}
if(!sk_X509_ATTRIBUTE_num(attrlst)) {
return 1;
}
for(i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
case V_ASN1_BMPSTRING:
break;
case V_ASN1_OCTET_STRING:
break;
case V_ASN1_BIT_STRING:
break;
default:
break;
}
}
return 1;
}
{
int i;
}
#endif