1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* pkcs8.c */ 2e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * project 1999-2004. 4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */ 5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* ==================================================================== 6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without 9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions 10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met: 11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the above copyright 13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * notice, this list of conditions and the following disclaimer. 14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright 16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * notice, this list of conditions and the following disclaimer in 17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the documentation and/or other materials provided with the 18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * distribution. 19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this 21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * software must display the following acknowledgment: 22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * "This product includes software developed by the OpenSSL Project 23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * endorse or promote products derived from this software without 27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * prior written permission. For written permission, please contact 28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * licensing@OpenSSL.org. 29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 5. Products derived from this software may not be called "OpenSSL" 31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * nor may "OpenSSL" appear in their names without prior written 32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * permission of the OpenSSL Project. 33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 6. Redistributions of any form whatsoever must retain the following 35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * acknowledgment: 36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * "This product includes software developed by the OpenSSL Project 37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OF THE POSSIBILITY OF SUCH DAMAGE. 51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ==================================================================== 52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This product includes cryptographic software written by Eric Young 54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * (eay@cryptsoft.com). This product includes software written by Tim 55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Hudson (tjh@cryptsoft.com). 56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */ 58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stdio.h> 59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <string.h> 60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "apps.h" 61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/pem.h> 62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/err.h> 63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/evp.h> 64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/pkcs12.h> 65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define PROG pkcs8_main 67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint MAIN(int, char **); 69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint MAIN(int argc, char **argv) 71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ENGINE *e = NULL; 73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project char **args, *infile = NULL, *outfile = NULL; 74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project char *passargin = NULL, *passargout = NULL; 75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO *in = NULL, *out = NULL; 76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int topk8 = 0; 77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int pbe_nid = -1; 78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project const EVP_CIPHER *cipher = NULL; 79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int iter = PKCS12_DEFAULT_ITER; 80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int informat, outformat; 81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int p8_broken = PKCS8_OK; 82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int nocrypt = 0; 83221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom X509_SIG *p8 = NULL; 84221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom PKCS8_PRIV_KEY_INFO *p8inf = NULL; 85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project EVP_PKEY *pkey=NULL; 86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL; 87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int badarg = 0; 88221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom int ret = 1; 89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef OPENSSL_NO_ENGINE 90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project char *engine=NULL; 91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (bio_err == NULL) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE); 94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!load_config(bio_err, NULL)) 96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto end; 97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project informat=FORMAT_PEM; 99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project outformat=FORMAT_PEM; 100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ERR_load_crypto_strings(); 102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project OpenSSL_add_all_algorithms(); 103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project args = argv + 1; 104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project while (!badarg && *args && *args[0] == '-') 105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!strcmp(*args,"-v2")) 107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (args[1]) 109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project args++; 111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project cipher=EVP_get_cipherbyname(*args); 112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!cipher) 113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, 115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "Unknown cipher %s\n", *args); 116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project badarg = 1; 117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project badarg = 1; 121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp(*args,"-v1")) 123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (args[1]) 125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project args++; 127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project pbe_nid=OBJ_txt2nid(*args); 128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (pbe_nid == NID_undef) 129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, 131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "Unknown PBE algorithm %s\n", *args); 132656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project badarg = 1; 133656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project badarg = 1; 137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp(*args,"-inform")) 139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (args[1]) 141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project args++; 143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project informat=str2fmt(*args); 144656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 145656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else badarg = 1; 146656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 147656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp(*args,"-outform")) 148656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 149656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (args[1]) 150656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 151656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project args++; 152656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project outformat=str2fmt(*args); 153656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 154656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else badarg = 1; 155656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 156656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp (*args, "-topk8")) 157656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project topk8 = 1; 158656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp (*args, "-noiter")) 159656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project iter = 1; 160656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp (*args, "-nocrypt")) 161656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project nocrypt = 1; 162656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp (*args, "-nooct")) 163656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8_broken = PKCS8_NO_OCTET; 164656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp (*args, "-nsdb")) 165656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8_broken = PKCS8_NS_DB; 166656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp (*args, "-embed")) 167656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8_broken = PKCS8_EMBEDDED_PARAM; 168656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp(*args,"-passin")) 169656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 170656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!args[1]) goto bad; 171656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project passargin= *(++args); 172656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 173656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp(*args,"-passout")) 174656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 175656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!args[1]) goto bad; 176656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project passargout= *(++args); 177656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 178656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef OPENSSL_NO_ENGINE 179656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (strcmp(*args,"-engine") == 0) 180656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 181656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!args[1]) goto bad; 182656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project engine= *(++args); 183656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 184656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 185656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp (*args, "-in")) 186656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 187656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (args[1]) 188656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 189656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project args++; 190656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project infile = *args; 191656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 192656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else badarg = 1; 193656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 194656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (!strcmp (*args, "-out")) 195656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 196656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (args[1]) 197656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 198656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project args++; 199656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project outfile = *args; 200656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 201656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else badarg = 1; 202656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 203656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else badarg = 1; 204656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project args++; 205656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 206656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 207656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (badarg) 208656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 209656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project bad: 210656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Usage pkcs8 [options]\n"); 211656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "where options are\n"); 212656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-in file input file\n"); 213656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-inform X input format (DER or PEM)\n"); 214656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-passin arg input file pass phrase source\n"); 215656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-outform X output format (DER or PEM)\n"); 216656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-out file output file\n"); 217656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-passout arg output file pass phrase source\n"); 218656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-topk8 output PKCS8 file\n"); 219656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-nooct use (nonstandard) no octet format\n"); 220656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-embed use (nonstandard) embedded DSA parameters format\n"); 221656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-nsdb use (nonstandard) DSA Netscape DB format\n"); 222656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-noiter use 1 as iteration count\n"); 223656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-nocrypt use or expect unencrypted private key\n"); 224656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-v2 alg use PKCS#5 v2.0 and cipher \"alg\"\n"); 225656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "-v1 obj use PKCS#5 v1.5 and cipher \"alg\"\n"); 226656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef OPENSSL_NO_ENGINE 227656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n"); 228656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 229221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 230656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 231656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 232656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef OPENSSL_NO_ENGINE 233656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project e = setup_engine(bio_err, engine, 0); 234656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 235656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 236656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) 237656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 238656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Error getting passwords\n"); 239221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 240656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 241656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 242656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((pbe_nid == -1) && !cipher) 243656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project pbe_nid = NID_pbeWithMD5AndDES_CBC; 244656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 245656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (infile) 246656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 247656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!(in = BIO_new_file(infile, "rb"))) 248656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 249656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, 250656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "Can't open input file %s\n", infile); 251221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 252656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 253656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 254656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 255656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project in = BIO_new_fp (stdin, BIO_NOCLOSE); 256656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 257656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (outfile) 258656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 259656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!(out = BIO_new_file (outfile, "wb"))) 260656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 261656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, 262656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "Can't open output file %s\n", outfile); 263221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 264656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 265656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 266656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 267656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 268656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project out = BIO_new_fp (stdout, BIO_NOCLOSE); 269656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifdef OPENSSL_SYS_VMS 270656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 271656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 272656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project out = BIO_push(tmpbio, out); 273656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 274656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 275656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 276656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (topk8) 277656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 278656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project pkey = load_key(bio_err, infile, informat, 1, 279656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project passin, e, "key"); 280656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!pkey) 281221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 282656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) 283656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 284656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Error converting key\n"); 285656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ERR_print_errors(bio_err); 286221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 287656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 288656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (nocrypt) 289656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 290656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (outformat == FORMAT_PEM) 291656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf); 292656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (outformat == FORMAT_ASN1) 293656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf); 294656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 295656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 296656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Bad format specified for key\n"); 297221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 298656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 299656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 300656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 301656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 302656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (passout) 303656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8pass = passout; 304656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 305656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 306656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8pass = pass; 307656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (EVP_read_pw_string(pass, sizeof pass, "Enter Encryption Password:", 1)) 308221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 309656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 310656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project app_RAND_load_file(NULL, bio_err, 0); 311656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!(p8 = PKCS8_encrypt(pbe_nid, cipher, 312656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8pass, strlen(p8pass), 313656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project NULL, 0, iter, p8inf))) 314656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 315656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Error encrypting key\n"); 316656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ERR_print_errors(bio_err); 317221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 318656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 319656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project app_RAND_write_file(NULL, bio_err); 320656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (outformat == FORMAT_PEM) 321656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project PEM_write_bio_PKCS8(out, p8); 322656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (outformat == FORMAT_ASN1) 323656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project i2d_PKCS8_bio(out, p8); 324656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 325656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 326656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Bad format specified for key\n"); 327221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 328656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 329656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 330656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 331221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom ret = 0; 332221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 333656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 334656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 335656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (nocrypt) 336656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 337656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (informat == FORMAT_PEM) 338656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in,NULL,NULL, NULL); 339656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (informat == FORMAT_ASN1) 340656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL); 341656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 342656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 343656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Bad format specified for key\n"); 344221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 345656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 346656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 347656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 348656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 349656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (informat == FORMAT_PEM) 350656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL); 351656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (informat == FORMAT_ASN1) 352656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8 = d2i_PKCS8_bio(in, NULL); 353656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 354656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 355656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Bad format specified for key\n"); 356221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 357656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 358656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 359656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!p8) 360656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 361656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf (bio_err, "Error reading key\n"); 362656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ERR_print_errors(bio_err); 363221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 364656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 365656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (passin) 366656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8pass = passin; 367656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 368656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 369656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8pass = pass; 370656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0); 371656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 372656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass)); 373656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 374656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 375656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!p8inf) 376656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 377656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Error decrypting key\n"); 378656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ERR_print_errors(bio_err); 379221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 380656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 381656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 382656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!(pkey = EVP_PKCS82PKEY(p8inf))) 383656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 384656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Error converting key\n"); 385656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ERR_print_errors(bio_err); 386221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 387656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 388656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 389656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (p8inf->broken) 390656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 391656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Warning: broken key encoding: "); 392656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project switch (p8inf->broken) 393656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 394656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project case PKCS8_NO_OCTET: 395656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "No Octet String in PrivateKey\n"); 396656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project break; 397656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 398656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project case PKCS8_EMBEDDED_PARAM: 399656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "DSA parameters included in PrivateKey\n"); 400656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project break; 401656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 402656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project case PKCS8_NS_DB: 403656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "DSA public key include in PrivateKey\n"); 404656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project break; 405656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 406221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom case PKCS8_NEG_PRIVKEY: 407221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom BIO_printf(bio_err, "DSA private key value is negative\n"); 408221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom break; 409221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 410656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project default: 411656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Unknown broken type\n"); 412656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project break; 413656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 414656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 415656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 416656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (outformat == FORMAT_PEM) 417656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout); 418656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else if (outformat == FORMAT_ASN1) 419656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project i2d_PrivateKey_bio(out, pkey); 420656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 421656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 422656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_printf(bio_err, "Bad format specified for key\n"); 423221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom goto end; 424656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 425221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom ret = 0; 426656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 427656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project end: 428221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom X509_SIG_free(p8); 429221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom PKCS8_PRIV_KEY_INFO_free(p8inf); 430656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project EVP_PKEY_free(pkey); 431656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_free_all(out); 432656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIO_free(in); 433656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (passin) 434656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project OPENSSL_free(passin); 435656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (passout) 436656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project OPENSSL_free(passout); 437656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 438221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return ret; 439656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 440