174664626SKris Kennaway /* p8_pkey.c */ 274664626SKris Kennaway /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 374664626SKris Kennaway * project 1999. 474664626SKris Kennaway */ 574664626SKris Kennaway /* ==================================================================== 674664626SKris Kennaway * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 774664626SKris Kennaway * 874664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 974664626SKris Kennaway * modification, are permitted provided that the following conditions 1074664626SKris Kennaway * are met: 1174664626SKris Kennaway * 1274664626SKris Kennaway * 1. Redistributions of source code must retain the above copyright 1374664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 1474664626SKris Kennaway * 1574664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 1674664626SKris Kennaway * notice, this list of conditions and the following disclaimer in 1774664626SKris Kennaway * the documentation and/or other materials provided with the 1874664626SKris Kennaway * distribution. 1974664626SKris Kennaway * 2074664626SKris Kennaway * 3. All advertising materials mentioning features or use of this 2174664626SKris Kennaway * software must display the following acknowledgment: 2274664626SKris Kennaway * "This product includes software developed by the OpenSSL Project 2374664626SKris Kennaway * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2474664626SKris Kennaway * 2574664626SKris Kennaway * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2674664626SKris Kennaway * endorse or promote products derived from this software without 2774664626SKris Kennaway * prior written permission. For written permission, please contact 2874664626SKris Kennaway * licensing@OpenSSL.org. 2974664626SKris Kennaway * 3074664626SKris Kennaway * 5. Products derived from this software may not be called "OpenSSL" 3174664626SKris Kennaway * nor may "OpenSSL" appear in their names without prior written 3274664626SKris Kennaway * permission of the OpenSSL Project. 3374664626SKris Kennaway * 3474664626SKris Kennaway * 6. Redistributions of any form whatsoever must retain the following 3574664626SKris Kennaway * acknowledgment: 3674664626SKris Kennaway * "This product includes software developed by the OpenSSL Project 3774664626SKris Kennaway * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3874664626SKris Kennaway * 3974664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4074664626SKris Kennaway * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4174664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4274664626SKris Kennaway * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4374664626SKris Kennaway * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4474664626SKris Kennaway * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4574664626SKris Kennaway * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4674664626SKris Kennaway * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4774664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4874664626SKris Kennaway * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4974664626SKris Kennaway * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5074664626SKris Kennaway * OF THE POSSIBILITY OF SUCH DAMAGE. 5174664626SKris Kennaway * ==================================================================== 5274664626SKris Kennaway * 5374664626SKris Kennaway * This product includes cryptographic software written by Eric Young 5474664626SKris Kennaway * (eay@cryptsoft.com). This product includes software written by Tim 5574664626SKris Kennaway * Hudson (tjh@cryptsoft.com). 5674664626SKris Kennaway * 5774664626SKris Kennaway */ 5874664626SKris Kennaway 5974664626SKris Kennaway #include <stdio.h> 6074664626SKris Kennaway #include "cryptlib.h" 6174664626SKris Kennaway #include <openssl/asn1_mac.h> 6274664626SKris Kennaway #include <openssl/x509.h> 6374664626SKris Kennaway 6474664626SKris Kennaway int i2d_PKCS8_PRIV_KEY_INFO (PKCS8_PRIV_KEY_INFO *a, unsigned char **pp) 6574664626SKris Kennaway { 6674664626SKris Kennaway 6774664626SKris Kennaway M_ASN1_I2D_vars(a); 6874664626SKris Kennaway 6974664626SKris Kennaway M_ASN1_I2D_len (a->version, i2d_ASN1_INTEGER); 7074664626SKris Kennaway M_ASN1_I2D_len (a->pkeyalg, i2d_X509_ALGOR); 7174664626SKris Kennaway M_ASN1_I2D_len (a->pkey, i2d_ASN1_TYPE); 7274664626SKris Kennaway M_ASN1_I2D_len_IMP_SET_opt_type (X509_ATTRIBUTE, a->attributes, 7374664626SKris Kennaway i2d_X509_ATTRIBUTE, 0); 7474664626SKris Kennaway 7574664626SKris Kennaway M_ASN1_I2D_seq_total (); 7674664626SKris Kennaway 7774664626SKris Kennaway M_ASN1_I2D_put (a->version, i2d_ASN1_INTEGER); 7874664626SKris Kennaway M_ASN1_I2D_put (a->pkeyalg, i2d_X509_ALGOR); 7974664626SKris Kennaway M_ASN1_I2D_put (a->pkey, i2d_ASN1_TYPE); 8074664626SKris Kennaway M_ASN1_I2D_put_IMP_SET_opt_type (X509_ATTRIBUTE, a->attributes, 8174664626SKris Kennaway i2d_X509_ATTRIBUTE, 0); 8274664626SKris Kennaway 8374664626SKris Kennaway M_ASN1_I2D_finish(); 8474664626SKris Kennaway } 8574664626SKris Kennaway 8674664626SKris Kennaway PKCS8_PRIV_KEY_INFO *PKCS8_PRIV_KEY_INFO_new(void) 8774664626SKris Kennaway { 8874664626SKris Kennaway PKCS8_PRIV_KEY_INFO *ret=NULL; 8974664626SKris Kennaway ASN1_CTX c; 9074664626SKris Kennaway M_ASN1_New_Malloc(ret, PKCS8_PRIV_KEY_INFO); 9174664626SKris Kennaway M_ASN1_New (ret->version, ASN1_INTEGER_new); 9274664626SKris Kennaway M_ASN1_New (ret->pkeyalg, X509_ALGOR_new); 9374664626SKris Kennaway M_ASN1_New (ret->pkey, ASN1_TYPE_new); 9474664626SKris Kennaway ret->attributes = NULL; 9574664626SKris Kennaway ret->broken = PKCS8_OK; 9674664626SKris Kennaway return (ret); 9774664626SKris Kennaway M_ASN1_New_Error(ASN1_F_PKCS8_PRIV_KEY_INFO_NEW); 9874664626SKris Kennaway } 9974664626SKris Kennaway 10074664626SKris Kennaway PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO(PKCS8_PRIV_KEY_INFO **a, 10174664626SKris Kennaway unsigned char **pp, long length) 10274664626SKris Kennaway { 10374664626SKris Kennaway M_ASN1_D2I_vars(a,PKCS8_PRIV_KEY_INFO *,PKCS8_PRIV_KEY_INFO_new); 10474664626SKris Kennaway M_ASN1_D2I_Init(); 10574664626SKris Kennaway M_ASN1_D2I_start_sequence(); 10674664626SKris Kennaway M_ASN1_D2I_get (ret->version, d2i_ASN1_INTEGER); 10774664626SKris Kennaway M_ASN1_D2I_get (ret->pkeyalg, d2i_X509_ALGOR); 10874664626SKris Kennaway M_ASN1_D2I_get (ret->pkey, d2i_ASN1_TYPE); 10974664626SKris Kennaway M_ASN1_D2I_get_IMP_set_opt_type(X509_ATTRIBUTE, ret->attributes, 11074664626SKris Kennaway d2i_X509_ATTRIBUTE, 11174664626SKris Kennaway X509_ATTRIBUTE_free, 0); 11274664626SKris Kennaway if (ASN1_TYPE_get(ret->pkey) == V_ASN1_SEQUENCE) 11374664626SKris Kennaway ret->broken = PKCS8_NO_OCTET; 11474664626SKris Kennaway M_ASN1_D2I_Finish(a, PKCS8_PRIV_KEY_INFO_free, ASN1_F_D2I_PKCS8_PRIV_KEY_INFO); 11574664626SKris Kennaway } 11674664626SKris Kennaway 11774664626SKris Kennaway void PKCS8_PRIV_KEY_INFO_free (PKCS8_PRIV_KEY_INFO *a) 11874664626SKris Kennaway { 11974664626SKris Kennaway if (a == NULL) return; 12074664626SKris Kennaway ASN1_INTEGER_free (a->version); 12174664626SKris Kennaway X509_ALGOR_free(a->pkeyalg); 12274664626SKris Kennaway /* Clear sensitive data */ 12374664626SKris Kennaway if (a->pkey->value.octet_string) 12474664626SKris Kennaway memset (a->pkey->value.octet_string->data, 12574664626SKris Kennaway 0, a->pkey->value.octet_string->length); 12674664626SKris Kennaway ASN1_TYPE_free (a->pkey); 12774664626SKris Kennaway sk_X509_ATTRIBUTE_pop_free (a->attributes, X509_ATTRIBUTE_free); 12874664626SKris Kennaway Free (a); 12974664626SKris Kennaway } 130