GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/asn1/p8_pkey.c Lines: 39 51 76.5 %
Date: 2017-11-07 Branches: 19 36 52.8 %

Line Branch Exec Source
1
/* $OpenBSD: p8_pkey.c,v 1.17 2015/09/10 15:56:25 jsing Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 1999.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 1999-2005 The OpenSSL Project.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this
21
 *    software must display the following acknowledgment:
22
 *    "This product includes software developed by the OpenSSL Project
23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24
 *
25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    licensing@OpenSSL.org.
29
 *
30
 * 5. Products derived from this software may not be called "OpenSSL"
31
 *    nor may "OpenSSL" appear in their names without prior written
32
 *    permission of the OpenSSL Project.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *    "This product includes software developed by the OpenSSL Project
37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
51
 * ====================================================================
52
 *
53
 * This product includes cryptographic software written by Eric Young
54
 * (eay@cryptsoft.com).  This product includes software written by Tim
55
 * Hudson (tjh@cryptsoft.com).
56
 *
57
 */
58
59
#include <stdio.h>
60
#include <string.h>
61
62
#include <openssl/asn1t.h>
63
#include <openssl/x509.h>
64
65
/* Minor tweak to operation: zero private key data */
66
static int
67
pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
68
{
69
	/* Since the structure must still be valid use ASN1_OP_FREE_PRE */
70
4704
	if (operation == ASN1_OP_FREE_PRE) {
71
370
		PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval;
72

740
		if (key->pkey != NULL &&
73
370
		    key->pkey->type == V_ASN1_OCTET_STRING &&
74
370
		    key->pkey->value.octet_string != NULL)
75
740
			explicit_bzero(key->pkey->value.octet_string->data,
76
370
			    key->pkey->value.octet_string->length);
77
370
	}
78
2352
	return 1;
79
}
80
81
static const ASN1_AUX PKCS8_PRIV_KEY_INFO_aux = {
82
	.asn1_cb = pkey_cb,
83
};
84
static const ASN1_TEMPLATE PKCS8_PRIV_KEY_INFO_seq_tt[] = {
85
	{
86
		.offset = offsetof(PKCS8_PRIV_KEY_INFO, version),
87
		.field_name = "version",
88
		.item = &ASN1_INTEGER_it,
89
	},
90
	{
91
		.offset = offsetof(PKCS8_PRIV_KEY_INFO, pkeyalg),
92
		.field_name = "pkeyalg",
93
		.item = &X509_ALGOR_it,
94
	},
95
	{
96
		.offset = offsetof(PKCS8_PRIV_KEY_INFO, pkey),
97
		.field_name = "pkey",
98
		.item = &ASN1_ANY_it,
99
	},
100
	{
101
		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL,
102
		.offset = offsetof(PKCS8_PRIV_KEY_INFO, attributes),
103
		.field_name = "attributes",
104
		.item = &X509_ATTRIBUTE_it,
105
	},
106
};
107
108
const ASN1_ITEM PKCS8_PRIV_KEY_INFO_it = {
109
	.itype = ASN1_ITYPE_SEQUENCE,
110
	.utype = V_ASN1_SEQUENCE,
111
	.templates = PKCS8_PRIV_KEY_INFO_seq_tt,
112
	.tcount = sizeof(PKCS8_PRIV_KEY_INFO_seq_tt) / sizeof(ASN1_TEMPLATE),
113
	.funcs = &PKCS8_PRIV_KEY_INFO_aux,
114
	.size = sizeof(PKCS8_PRIV_KEY_INFO),
115
	.sname = "PKCS8_PRIV_KEY_INFO",
116
};
117
118
119
PKCS8_PRIV_KEY_INFO *
120
d2i_PKCS8_PRIV_KEY_INFO(PKCS8_PRIV_KEY_INFO **a, const unsigned char **in, long len)
121
{
122
420
	return (PKCS8_PRIV_KEY_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
123
	    &PKCS8_PRIV_KEY_INFO_it);
124
}
125
126
int
127
i2d_PKCS8_PRIV_KEY_INFO(PKCS8_PRIV_KEY_INFO *a, unsigned char **out)
128
{
129
464
	return ASN1_item_i2d((ASN1_VALUE *)a, out, &PKCS8_PRIV_KEY_INFO_it);
130
}
131
132
PKCS8_PRIV_KEY_INFO *
133
PKCS8_PRIV_KEY_INFO_new(void)
134
{
135
264
	return (PKCS8_PRIV_KEY_INFO *)ASN1_item_new(&PKCS8_PRIV_KEY_INFO_it);
136
}
137
138
void
139
PKCS8_PRIV_KEY_INFO_free(PKCS8_PRIV_KEY_INFO *a)
140
{
141
740
	ASN1_item_free((ASN1_VALUE *)a, &PKCS8_PRIV_KEY_INFO_it);
142
370
}
143
144
int
145
PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj, int version,
146
    int ptype, void *pval, unsigned char *penc, int penclen)
147
{
148
	unsigned char **ppenc = NULL;
149
150
264
	if (version >= 0) {
151
132
		if (!ASN1_INTEGER_set(priv->version, version))
152
			return 0;
153
	}
154
132
	if (penc) {
155
		int pmtype;
156
		ASN1_OCTET_STRING *oct;
157
132
		oct = ASN1_OCTET_STRING_new();
158
132
		if (!oct)
159
			return 0;
160
132
		oct->data = penc;
161
		ppenc = &oct->data;
162
132
		oct->length = penclen;
163
132
		if (priv->broken == PKCS8_NO_OCTET)
164
			pmtype = V_ASN1_SEQUENCE;
165
		else
166
			pmtype = V_ASN1_OCTET_STRING;
167
132
		ASN1_TYPE_set(priv->pkey, pmtype, oct);
168
132
	}
169
132
	if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval)) {
170
		/* If call fails do not swallow 'enc' */
171
		if (ppenc)
172
			*ppenc = NULL;
173
		return 0;
174
	}
175
132
	return 1;
176
132
}
177
178
int
179
PKCS8_pkey_get0(ASN1_OBJECT **ppkalg, const unsigned char **pk, int *ppklen,
180
    X509_ALGOR **pa, PKCS8_PRIV_KEY_INFO *p8)
181
{
182
952
	if (ppkalg)
183
238
		*ppkalg = p8->pkeyalg->algorithm;
184
476
	if (p8->pkey->type == V_ASN1_OCTET_STRING) {
185
476
		p8->broken = PKCS8_OK;
186
476
		if (pk) {
187
238
			*pk = p8->pkey->value.octet_string->data;
188
238
			*ppklen = p8->pkey->value.octet_string->length;
189
238
		}
190
	} else if (p8->pkey->type == V_ASN1_SEQUENCE) {
191
		p8->broken = PKCS8_NO_OCTET;
192
		if (pk) {
193
			*pk = p8->pkey->value.sequence->data;
194
			*ppklen = p8->pkey->value.sequence->length;
195
		}
196
	} else
197
		return 0;
198
476
	if (pa)
199
6
		*pa = p8->pkeyalg;
200
476
	return 1;
201
476
}