155714Skris/* p8_pkey.c */ 2280304Sjkim/* 3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4280304Sjkim * 1999. 555714Skris */ 655714Skris/* ==================================================================== 7238405Sjkim * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 855714Skris * 955714Skris * Redistribution and use in source and binary forms, with or without 1055714Skris * modification, are permitted provided that the following conditions 1155714Skris * are met: 1255714Skris * 1355714Skris * 1. Redistributions of source code must retain the above copyright 14280304Sjkim * notice, this list of conditions and the following disclaimer. 1555714Skris * 1655714Skris * 2. Redistributions in binary form must reproduce the above copyright 1755714Skris * notice, this list of conditions and the following disclaimer in 1855714Skris * the documentation and/or other materials provided with the 1955714Skris * distribution. 2055714Skris * 2155714Skris * 3. All advertising materials mentioning features or use of this 2255714Skris * software must display the following acknowledgment: 2355714Skris * "This product includes software developed by the OpenSSL Project 2455714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2555714Skris * 2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2755714Skris * endorse or promote products derived from this software without 2855714Skris * prior written permission. For written permission, please contact 2955714Skris * licensing@OpenSSL.org. 3055714Skris * 3155714Skris * 5. Products derived from this software may not be called "OpenSSL" 3255714Skris * nor may "OpenSSL" appear in their names without prior written 3355714Skris * permission of the OpenSSL Project. 3455714Skris * 3555714Skris * 6. Redistributions of any form whatsoever must retain the following 3655714Skris * acknowledgment: 3755714Skris * "This product includes software developed by the OpenSSL Project 3855714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3955714Skris * 4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4355714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5255714Skris * ==================================================================== 5355714Skris * 5455714Skris * This product includes cryptographic software written by Eric Young 5555714Skris * (eay@cryptsoft.com). This product includes software written by Tim 5655714Skris * Hudson (tjh@cryptsoft.com). 5755714Skris * 5855714Skris */ 5955714Skris 6055714Skris#include <stdio.h> 6155714Skris#include "cryptlib.h" 62109998Smarkm#include <openssl/asn1t.h> 6355714Skris#include <openssl/x509.h> 6455714Skris 65109998Smarkm/* Minor tweak to operation: zero private key data */ 66238405Sjkimstatic int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 67280304Sjkim void *exarg) 6855714Skris{ 69280304Sjkim /* Since the structure must still be valid use ASN1_OP_FREE_PRE */ 70280304Sjkim if (operation == ASN1_OP_FREE_PRE) { 71280304Sjkim PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval; 72280304Sjkim if (key->pkey && key->pkey->type == V_ASN1_OCTET_STRING 73280304Sjkim && key->pkey->value.octet_string != NULL) 74280304Sjkim OPENSSL_cleanse(key->pkey->value.octet_string->data, 75280304Sjkim key->pkey->value.octet_string->length); 76280304Sjkim } 77280304Sjkim return 1; 7855714Skris} 7955714Skris 80109998SmarkmASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = { 81280304Sjkim ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER), 82280304Sjkim ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR), 83280304Sjkim ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_ANY), 84280304Sjkim ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0) 85109998Smarkm} ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO) 8655714Skris 87109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO) 88238405Sjkim 89238405Sjkimint PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj, 90280304Sjkim int version, 91280304Sjkim int ptype, void *pval, unsigned char *penc, int penclen) 92280304Sjkim{ 93280304Sjkim unsigned char **ppenc = NULL; 94280304Sjkim if (version >= 0) { 95280304Sjkim if (!ASN1_INTEGER_set(priv->version, version)) 96280304Sjkim return 0; 97280304Sjkim } 98280304Sjkim if (penc) { 99280304Sjkim int pmtype; 100280304Sjkim ASN1_OCTET_STRING *oct; 101280304Sjkim oct = ASN1_OCTET_STRING_new(); 102280304Sjkim if (!oct) 103280304Sjkim return 0; 104280304Sjkim oct->data = penc; 105280304Sjkim ppenc = &oct->data; 106280304Sjkim oct->length = penclen; 107280304Sjkim if (priv->broken == PKCS8_NO_OCTET) 108280304Sjkim pmtype = V_ASN1_SEQUENCE; 109280304Sjkim else 110280304Sjkim pmtype = V_ASN1_OCTET_STRING; 111280304Sjkim ASN1_TYPE_set(priv->pkey, pmtype, oct); 112280304Sjkim } 113280304Sjkim if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval)) { 114280304Sjkim /* If call fails do not swallow 'enc' */ 115280304Sjkim if (ppenc) 116280304Sjkim *ppenc = NULL; 117280304Sjkim return 0; 118280304Sjkim } 119280304Sjkim return 1; 120280304Sjkim} 121238405Sjkim 122238405Sjkimint PKCS8_pkey_get0(ASN1_OBJECT **ppkalg, 123280304Sjkim const unsigned char **pk, int *ppklen, 124280304Sjkim X509_ALGOR **pa, PKCS8_PRIV_KEY_INFO *p8) 125280304Sjkim{ 126280304Sjkim if (ppkalg) 127280304Sjkim *ppkalg = p8->pkeyalg->algorithm; 128280304Sjkim if (p8->pkey->type == V_ASN1_OCTET_STRING) { 129280304Sjkim p8->broken = PKCS8_OK; 130280304Sjkim if (pk) { 131280304Sjkim *pk = p8->pkey->value.octet_string->data; 132280304Sjkim *ppklen = p8->pkey->value.octet_string->length; 133280304Sjkim } 134280304Sjkim } else if (p8->pkey->type == V_ASN1_SEQUENCE) { 135280304Sjkim p8->broken = PKCS8_NO_OCTET; 136280304Sjkim if (pk) { 137280304Sjkim *pk = p8->pkey->value.sequence->data; 138280304Sjkim *ppklen = p8->pkey->value.sequence->length; 139280304Sjkim } 140280304Sjkim } else 141280304Sjkim return 0; 142280304Sjkim if (pa) 143280304Sjkim *pa = p8->pkeyalg; 144280304Sjkim return 1; 145280304Sjkim} 146