155714Skris/* p8_pkey.c */
2194206Ssimon/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
355714Skris * project 1999.
455714Skris */
555714Skris/* ====================================================================
6238405Sjkim * Copyright (c) 1999-2005 The OpenSSL Project.  All rights reserved.
755714Skris *
855714Skris * Redistribution and use in source and binary forms, with or without
955714Skris * modification, are permitted provided that the following conditions
1055714Skris * are met:
1155714Skris *
1255714Skris * 1. Redistributions of source code must retain the above copyright
1355714Skris *    notice, this list of conditions and the following disclaimer.
1455714Skris *
1555714Skris * 2. Redistributions in binary form must reproduce the above copyright
1655714Skris *    notice, this list of conditions and the following disclaimer in
1755714Skris *    the documentation and/or other materials provided with the
1855714Skris *    distribution.
1955714Skris *
2055714Skris * 3. All advertising materials mentioning features or use of this
2155714Skris *    software must display the following acknowledgment:
2255714Skris *    "This product includes software developed by the OpenSSL Project
2355714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2455714Skris *
2555714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2655714Skris *    endorse or promote products derived from this software without
2755714Skris *    prior written permission. For written permission, please contact
2855714Skris *    licensing@OpenSSL.org.
2955714Skris *
3055714Skris * 5. Products derived from this software may not be called "OpenSSL"
3155714Skris *    nor may "OpenSSL" appear in their names without prior written
3255714Skris *    permission of the OpenSSL Project.
3355714Skris *
3455714Skris * 6. Redistributions of any form whatsoever must retain the following
3555714Skris *    acknowledgment:
3655714Skris *    "This product includes software developed by the OpenSSL Project
3755714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3855714Skris *
3955714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4055714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4155714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4255714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4355714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4455714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4555714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4655714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4755714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4855714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4955714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5055714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5155714Skris * ====================================================================
5255714Skris *
5355714Skris * This product includes cryptographic software written by Eric Young
5455714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5555714Skris * Hudson (tjh@cryptsoft.com).
5655714Skris *
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
61109998Smarkm#include <openssl/asn1t.h>
6255714Skris#include <openssl/x509.h>
6355714Skris
64109998Smarkm/* Minor tweak to operation: zero private key data */
65238405Sjkimstatic int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
66238405Sjkim							void *exarg)
6755714Skris{
68109998Smarkm	/* Since the structure must still be valid use ASN1_OP_FREE_PRE */
69109998Smarkm	if(operation == ASN1_OP_FREE_PRE) {
70109998Smarkm		PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval;
71109998Smarkm		if (key->pkey->value.octet_string)
72109998Smarkm		OPENSSL_cleanse(key->pkey->value.octet_string->data,
73109998Smarkm			key->pkey->value.octet_string->length);
74109998Smarkm	}
75109998Smarkm	return 1;
7655714Skris}
7755714Skris
78109998SmarkmASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = {
79109998Smarkm	ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER),
80109998Smarkm	ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR),
81109998Smarkm	ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_ANY),
82109998Smarkm	ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0)
83109998Smarkm} ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
8455714Skris
85109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
86238405Sjkim
87238405Sjkimint PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,
88238405Sjkim					int version,
89238405Sjkim					int ptype, void *pval,
90238405Sjkim					unsigned char *penc, int penclen)
91238405Sjkim	{
92238405Sjkim	unsigned char **ppenc = NULL;
93238405Sjkim	if (version >= 0)
94238405Sjkim		{
95238405Sjkim		if (!ASN1_INTEGER_set(priv->version, version))
96238405Sjkim			return 0;
97238405Sjkim		}
98238405Sjkim	if (penc)
99238405Sjkim		{
100238405Sjkim		int pmtype;
101238405Sjkim		ASN1_OCTET_STRING *oct;
102238405Sjkim		oct = ASN1_OCTET_STRING_new();
103238405Sjkim		if (!oct)
104238405Sjkim			return 0;
105238405Sjkim		oct->data = penc;
106238405Sjkim		ppenc = &oct->data;
107238405Sjkim		oct->length = penclen;
108238405Sjkim		if (priv->broken == PKCS8_NO_OCTET)
109238405Sjkim			pmtype = V_ASN1_SEQUENCE;
110238405Sjkim		else
111238405Sjkim			pmtype = V_ASN1_OCTET_STRING;
112238405Sjkim		ASN1_TYPE_set(priv->pkey, pmtype, oct);
113238405Sjkim		}
114238405Sjkim	if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval))
115238405Sjkim		{
116238405Sjkim		/* If call fails do not swallow 'enc' */
117238405Sjkim		if (ppenc)
118238405Sjkim			*ppenc = NULL;
119238405Sjkim		return 0;
120238405Sjkim		}
121238405Sjkim	return 1;
122238405Sjkim	}
123238405Sjkim
124238405Sjkimint PKCS8_pkey_get0(ASN1_OBJECT **ppkalg,
125238405Sjkim		const unsigned char **pk, int *ppklen,
126238405Sjkim		X509_ALGOR **pa,
127238405Sjkim		PKCS8_PRIV_KEY_INFO *p8)
128238405Sjkim	{
129238405Sjkim	if (ppkalg)
130238405Sjkim		*ppkalg = p8->pkeyalg->algorithm;
131238405Sjkim	if(p8->pkey->type == V_ASN1_OCTET_STRING)
132238405Sjkim		{
133238405Sjkim		p8->broken = PKCS8_OK;
134238405Sjkim		if (pk)
135238405Sjkim			{
136238405Sjkim			*pk = p8->pkey->value.octet_string->data;
137238405Sjkim			*ppklen = p8->pkey->value.octet_string->length;
138238405Sjkim			}
139238405Sjkim		}
140238405Sjkim	else if (p8->pkey->type == V_ASN1_SEQUENCE)
141238405Sjkim		{
142238405Sjkim		p8->broken = PKCS8_NO_OCTET;
143238405Sjkim		if (pk)
144238405Sjkim			{
145238405Sjkim			*pk = p8->pkey->value.sequence->data;
146238405Sjkim			*ppklen = p8->pkey->value.sequence->length;
147238405Sjkim			}
148238405Sjkim		}
149238405Sjkim	else
150238405Sjkim		return 0;
151238405Sjkim	if (pa)
152238405Sjkim		*pa = p8->pkeyalg;
153238405Sjkim	return 1;
154238405Sjkim	}
155238405Sjkim
156