p_lib.c 9.3 KB
Newer Older
1
/* crypto/evp/p_lib.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 * 
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 * 
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from 
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 * 
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

#include <stdio.h>
#include "cryptlib.h"
B
Bodo Möller 已提交
61 62
#include <openssl/bn.h>
#include <openssl/err.h>
63 64 65 66
#include <openssl/objects.h>
#include <openssl/evp.h>
#include <openssl/asn1_mac.h>
#include <openssl/x509.h>
N
make  
Nils Larsch 已提交
67
#ifndef OPENSSL_NO_RSA
68
#include <openssl/rsa.h>
N
make  
Nils Larsch 已提交
69 70
#endif
#ifndef OPENSSL_NO_DSA
71
#include <openssl/dsa.h>
N
make  
Nils Larsch 已提交
72 73
#endif
#ifndef OPENSSL_NO_DH
74
#include <openssl/dh.h>
N
make  
Nils Larsch 已提交
75
#endif
76

77 78
#include "asn1_locl.h"

79
static void EVP_PKEY_free_it(EVP_PKEY *x);
80

U
Ulf Möller 已提交
81
int EVP_PKEY_bits(EVP_PKEY *pkey)
82
	{
83 84 85
	if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
		return pkey->ameth->pkey_bits(pkey);
	return 0;
86 87
	}

U
Ulf Möller 已提交
88
int EVP_PKEY_size(EVP_PKEY *pkey)
89
	{
90 91 92
	if (pkey && pkey->ameth && pkey->ameth->pkey_size)
		return pkey->ameth->pkey_size(pkey);
	return 0;
93 94
	}

U
Ulf Möller 已提交
95
int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
96
	{
97
#ifndef OPENSSL_NO_DSA
98 99
	if (pkey->type == EVP_PKEY_DSA)
		{
100
		int ret=pkey->save_parameters;
101 102 103 104 105

		if (mode >= 0)
			pkey->save_parameters=mode;
		return(ret);
		}
B
Bodo Möller 已提交
106
#endif
107 108
#ifndef OPENSSL_NO_EC
	if (pkey->type == EVP_PKEY_EC)
B
Bodo Möller 已提交
109 110 111 112 113 114 115
		{
		int ret = pkey->save_parameters;

		if (mode >= 0)
			pkey->save_parameters = mode;
		return(ret);
		}
116 117 118 119
#endif
	return(0);
	}

R
Richard Levitte 已提交
120
int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
121 122 123 124
	{
	if (to->type != from->type)
		{
		EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES);
125
		goto err;
126 127 128 129
		}

	if (EVP_PKEY_missing_parameters(from))
		{
U
Ulf Möller 已提交
130
		EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS);
131
		goto err;
132
		}
133 134
	if (from->ameth && from->ameth->param_copy)
		return from->ameth->param_copy(to, from);
135
err:
136
	return 0;
137 138
	}

R
Richard Levitte 已提交
139
int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
140
	{
141 142 143
	if (pkey->ameth && pkey->ameth->param_missing)
		return pkey->ameth->param_missing(pkey);
	return 0;
144 145
	}

R
Richard Levitte 已提交
146
int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
147
	{
148 149 150 151
	if (a->type != b->type)
		return -1;
	if (a->ameth && a->ameth->param_cmp)
		return a->ameth->param_cmp(a, b);
152
	return -2;
153 154
	}

R
Richard Levitte 已提交
155
int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
156 157 158 159
	{
	if (a->type != b->type)
		return -1;

160 161
	if (EVP_PKEY_cmp_parameters(a, b) == 0)
		return 0;
R
Richard Levitte 已提交
162

163 164
	if (a->ameth && a->ameth->pub_cmp)
		return a->ameth->pub_cmp(a, b);
165

166
	return -2;
167 168
	}

U
Ulf Möller 已提交
169
EVP_PKEY *EVP_PKEY_new(void)
170 171 172
	{
	EVP_PKEY *ret;

173
	ret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY));
174 175 176 177 178 179 180
	if (ret == NULL)
		{
		EVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);
		return(NULL);
		}
	ret->type=EVP_PKEY_NONE;
	ret->references=1;
181
	ret->ameth=NULL;
182 183 184 185 186 187
	ret->pkey.ptr=NULL;
	ret->attributes=NULL;
	ret->save_parameters=1;
	return(ret);
	}

188
int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
189
	{
190
	const EVP_PKEY_ASN1_METHOD *ameth;
191 192 193
	if (pkey == NULL) return(0);
	if (pkey->pkey.ptr != NULL)
		EVP_PKEY_free_it(pkey);
194
	ameth = EVP_PKEY_asn1_find(type);
195 196
	pkey->ameth = ameth;
	pkey->type = ameth->pkey_id;
197 198
	pkey->save_type=type;
	pkey->pkey.ptr=key;
199
	return(key != NULL);
200 201
	}

202 203 204 205 206
void *EVP_PKEY_get0(EVP_PKEY *pkey)
	{
	return pkey->pkey.ptr;
	}

207
#ifndef OPENSSL_NO_RSA
208
int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
209
{
210
	int ret = EVP_PKEY_assign_RSA(pkey, key);
211
	if(ret)
212
		RSA_up_ref(key);
213
	return ret;
214 215
}

216
RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
217 218
	{
	if(pkey->type != EVP_PKEY_RSA) {
219
		EVPerr(EVP_F_EVP_PKEY_GET1_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
220 221
		return NULL;
	}
222
	RSA_up_ref(pkey->pkey.rsa);
223 224 225 226
	return pkey->pkey.rsa;
}
#endif

227
#ifndef OPENSSL_NO_DSA
228
int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
229
{
230
	int ret = EVP_PKEY_assign_DSA(pkey, key);
231
	if(ret)
232
		DSA_up_ref(key);
233
	return ret;
234 235
}

236
DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
237 238
	{
	if(pkey->type != EVP_PKEY_DSA) {
239
		EVPerr(EVP_F_EVP_PKEY_GET1_DSA, EVP_R_EXPECTING_A_DSA_KEY);
240 241
		return NULL;
	}
242
	DSA_up_ref(pkey->pkey.dsa);
243 244 245 246
	return pkey->pkey.dsa;
}
#endif

247
#ifndef OPENSSL_NO_EC
B
Bodo Möller 已提交
248

249
int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
B
Bodo Möller 已提交
250
{
251
	int ret = EVP_PKEY_assign_EC_KEY(pkey,key);
N
Nils Larsch 已提交
252 253 254
	if (ret)
		EC_KEY_up_ref(key);
	return ret;
B
Bodo Möller 已提交
255 256
}

257
EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
B
Bodo Möller 已提交
258
{
259
	if (pkey->type != EVP_PKEY_EC)
B
Bodo Möller 已提交
260
	{
261
		EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
B
Bodo Möller 已提交
262 263
		return NULL;
	}
N
Nils Larsch 已提交
264 265
	EC_KEY_up_ref(pkey->pkey.ec);
	return pkey->pkey.ec;
B
Bodo Möller 已提交
266 267 268 269
}
#endif


270
#ifndef OPENSSL_NO_DH
271

272
int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
273
{
274
	int ret = EVP_PKEY_assign_DH(pkey, key);
275
	if(ret)
G
Geoff Thorpe 已提交
276
		DH_up_ref(key);
277
	return ret;
278 279
}

280
DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
281 282
	{
	if(pkey->type != EVP_PKEY_DH) {
283
		EVPerr(EVP_F_EVP_PKEY_GET1_DH, EVP_R_EXPECTING_A_DH_KEY);
284 285
		return NULL;
	}
G
Geoff Thorpe 已提交
286
	DH_up_ref(pkey->pkey.dh);
287 288 289 290
	return pkey->pkey.dh;
}
#endif

U
Ulf Möller 已提交
291
int EVP_PKEY_type(int type)
292
	{
293
	const EVP_PKEY_ASN1_METHOD *ameth;
294
	ameth = EVP_PKEY_asn1_find(type);
295 296 297
	if (ameth)
		return ameth->pkey_id;
	return NID_undef;
298 299
	}

U
Ulf Möller 已提交
300
void EVP_PKEY_free(EVP_PKEY *x)
301 302 303 304 305 306
	{
	int i;

	if (x == NULL) return;

	i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY);
307 308 309
#ifdef REF_PRINT
	REF_PRINT("EVP_PKEY",x);
#endif
310 311 312 313 314 315 316 317 318
	if (i > 0) return;
#ifdef REF_CHECK
	if (i < 0)
		{
		fprintf(stderr,"EVP_PKEY_free, bad reference count\n");
		abort();
		}
#endif
	EVP_PKEY_free_it(x);
319 320
	if (x->attributes)
		sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
321
	OPENSSL_free(x);
322 323
	}

U
Ulf Möller 已提交
324
static void EVP_PKEY_free_it(EVP_PKEY *x)
325
	{
326 327
	if (x->ameth && x->ameth->pkey_free)
		x->ameth->pkey_free(x);
328 329
	}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
				const char *kstr)
	{
	BIO_indent(out, indent, 128);
	BIO_printf(out, "%s %s, algorithm, unsupported\n",
						OBJ_nid2ln(pkey->type), kstr);
	return 1;
	}

int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
				int indent, ASN1_PCTX *pctx)
	{
	if (pkey->ameth && pkey->ameth->pub_print)
		return pkey->ameth->pub_print(out, pkey, indent, pctx);
	
	return unsup_alg(out, pkey, indent, "Public Key");
	}

int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
				int indent, ASN1_PCTX *pctx)
	{
	if (pkey->ameth && pkey->ameth->priv_print)
		return pkey->ameth->priv_print(out, pkey, indent, pctx);
	
	return unsup_alg(out, pkey, indent, "Private Key");
	}

int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
				int indent, ASN1_PCTX *pctx)
	{
	if (pkey->ameth && pkey->ameth->param_print)
		return pkey->ameth->param_print(out, pkey, indent, pctx);
	return unsup_alg(out, pkey, indent, "Parameters");
	}
364 365 366 367 368 369 370 371 372

int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
	{
	if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
		return -2;
	return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
						0, pnid);
	}