p_lib.c 12.9 KB
Newer Older
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 3 4 5 6
 * 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.
7
 *
8 9 10 11 12 13
 * 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).
14
 *
15 16 17 18 19 20
 * 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.
21
 *
22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * 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 :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37 38
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40 41 42 43 44 45 46 47 48 49 50
 * 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.
51
 *
52 53 54 55 56 57 58
 * 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>
59
#include "internal/cryptlib.h"
B
Bodo Möller 已提交
60 61
#include <openssl/bn.h>
#include <openssl/err.h>
62 63 64
#include <openssl/objects.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
N
make  
Nils Larsch 已提交
65
#ifndef OPENSSL_NO_RSA
66
# include <openssl/rsa.h>
N
make  
Nils Larsch 已提交
67 68
#endif
#ifndef OPENSSL_NO_DSA
69
# include <openssl/dsa.h>
N
make  
Nils Larsch 已提交
70 71
#endif
#ifndef OPENSSL_NO_DH
72
# include <openssl/dh.h>
N
make  
Nils Larsch 已提交
73
#endif
74

75
#ifndef OPENSSL_NO_ENGINE
76
# include <openssl/engine.h>
77 78
#endif

79
#include "internal/asn1_int.h"
D
Dr. Stephen Henson 已提交
80
#include "internal/evp_int.h"
81

82
static void EVP_PKEY_free_it(EVP_PKEY *x);
83

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

91
int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
92 93 94 95 96 97 98
{
    if (pkey == NULL)
        return 0;
    if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
        return -2;
    return pkey->ameth->pkey_security_bits(pkey);
}
99

U
Ulf Möller 已提交
100
int EVP_PKEY_size(EVP_PKEY *pkey)
101 102 103 104 105
{
    if (pkey && pkey->ameth && pkey->ameth->pkey_size)
        return pkey->ameth->pkey_size(pkey);
    return 0;
}
106

U
Ulf Möller 已提交
107
int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
108
{
109
#ifndef OPENSSL_NO_DSA
110 111 112 113 114 115 116
    if (pkey->type == EVP_PKEY_DSA) {
        int ret = pkey->save_parameters;

        if (mode >= 0)
            pkey->save_parameters = mode;
        return (ret);
    }
B
Bodo Möller 已提交
117
#endif
118
#ifndef OPENSSL_NO_EC
119 120 121 122 123 124 125
    if (pkey->type == EVP_PKEY_EC) {
        int ret = pkey->save_parameters;

        if (mode >= 0)
            pkey->save_parameters = mode;
        return (ret);
    }
126
#endif
127 128
    return (0);
}
129

R
Richard Levitte 已提交
130
int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
131
{
132 133 134 135
    if (to->type == EVP_PKEY_NONE) {
        if (EVP_PKEY_set_type(to, from->type) == 0)
            return 0;
    } else if (to->type != from->type) {
136 137 138 139 140 141 142 143 144 145 146 147 148
        EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
        goto err;
    }

    if (EVP_PKEY_missing_parameters(from)) {
        EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
        goto err;
    }
    if (from->ameth && from->ameth->param_copy)
        return from->ameth->param_copy(to, from);
 err:
    return 0;
}
149

R
Richard Levitte 已提交
150
int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
151 152 153 154 155
{
    if (pkey->ameth && pkey->ameth->param_missing)
        return pkey->ameth->param_missing(pkey);
    return 0;
}
156

R
Richard Levitte 已提交
157
int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
158 159 160 161 162 163 164
{
    if (a->type != b->type)
        return -1;
    if (a->ameth && a->ameth->param_cmp)
        return a->ameth->param_cmp(a, b);
    return -2;
}
165

R
Richard Levitte 已提交
166
int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
{
    if (a->type != b->type)
        return -1;

    if (a->ameth) {
        int ret;
        /* Compare parameters if the algorithm has them */
        if (a->ameth->param_cmp) {
            ret = a->ameth->param_cmp(a, b);
            if (ret <= 0)
                return ret;
        }

        if (a->ameth->pub_cmp)
            return a->ameth->pub_cmp(a, b);
    }

    return -2;
}
186

U
Ulf Möller 已提交
187
EVP_PKEY *EVP_PKEY_new(void)
188 189 190
{
    EVP_PKEY *ret;

R
Rich Salz 已提交
191
    ret = OPENSSL_malloc(sizeof(*ret));
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    if (ret == NULL) {
        EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
        return (NULL);
    }
    ret->type = EVP_PKEY_NONE;
    ret->save_type = EVP_PKEY_NONE;
    ret->references = 1;
    ret->ameth = NULL;
    ret->engine = NULL;
    ret->pkey.ptr = NULL;
    ret->attributes = NULL;
    ret->save_parameters = 1;
    return (ret);
}

207 208 209 210 211
void EVP_PKEY_up_ref(EVP_PKEY *pkey)
{
    CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
}

212 213 214
/*
 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
 * is NULL just return 1 or 0 if the algorithm exists.
215 216 217
 */

static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
218 219 220 221 222 223 224 225 226 227 228 229
{
    const EVP_PKEY_ASN1_METHOD *ameth;
    ENGINE *e = NULL;
    if (pkey) {
        if (pkey->pkey.ptr)
            EVP_PKEY_free_it(pkey);
        /*
         * If key type matches and a method exists then this lookup has
         * succeeded once so just indicate success.
         */
        if ((type == pkey->save_type) && pkey->ameth)
            return 1;
230
#ifndef OPENSSL_NO_ENGINE
231 232 233 234 235
        /* If we have an ENGINE release it */
        if (pkey->engine) {
            ENGINE_finish(pkey->engine);
            pkey->engine = NULL;
        }
236
#endif
237 238 239 240 241
    }
    if (str)
        ameth = EVP_PKEY_asn1_find_str(&e, str, len);
    else
        ameth = EVP_PKEY_asn1_find(&e, type);
242
#ifndef OPENSSL_NO_ENGINE
243 244
    if (!pkey && e)
        ENGINE_finish(e);
245
#endif
246 247 248 249 250 251 252 253 254 255 256 257 258
    if (!ameth) {
        EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
        return 0;
    }
    if (pkey) {
        pkey->ameth = ameth;
        pkey->engine = e;

        pkey->type = pkey->ameth->pkey_id;
        pkey->save_type = type;
    }
    return 1;
}
259 260

int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
261 262 263
{
    return pkey_set_type(pkey, type, NULL, -1);
}
264 265

int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
266 267 268
{
    return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
}
269 270

int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
271
{
E
Emilia Kasper 已提交
272
    if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
273 274 275 276
        return 0;
    pkey->pkey.ptr = key;
    return (key != NULL);
}
277

D
Dr. Stephen Henson 已提交
278
void *EVP_PKEY_get0(const EVP_PKEY *pkey)
279 280 281
{
    return pkey->pkey.ptr;
}
282

283
#ifndef OPENSSL_NO_RSA
284
int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
285
{
286 287 288 289
    int ret = EVP_PKEY_assign_RSA(pkey, key);
    if (ret)
        RSA_up_ref(key);
    return ret;
290 291
}

292
RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
293 294
{
    if (pkey->type != EVP_PKEY_RSA) {
295
        EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
296 297 298
        return NULL;
    }
    return pkey->pkey.rsa;
299
}
300 301 302 303 304 305 306 307

RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
{
    RSA *ret = EVP_PKEY_get0_RSA(pkey);
    if (ret != NULL)
        RSA_up_ref(ret);
    return ret;
}
308 309
#endif

310
#ifndef OPENSSL_NO_DSA
311
int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
312
{
313 314 315 316
    int ret = EVP_PKEY_assign_DSA(pkey, key);
    if (ret)
        DSA_up_ref(key);
    return ret;
317 318
}

319
DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
320 321
{
    if (pkey->type != EVP_PKEY_DSA) {
322
        EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
323 324 325
        return NULL;
    }
    return pkey->pkey.dsa;
326
}
327 328 329 330 331 332 333 334

DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
{
    DSA *ret = EVP_PKEY_get0_DSA(pkey);
    if (ret != NULL)
        DSA_up_ref(ret);
    return ret;
}
335 336
#endif

337
#ifndef OPENSSL_NO_EC
B
Bodo Möller 已提交
338

339
int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
B
Bodo Möller 已提交
340
{
341 342 343 344
    int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
    if (ret)
        EC_KEY_up_ref(key);
    return ret;
B
Bodo Möller 已提交
345 346
}

347
EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
B
Bodo Möller 已提交
348
{
349
    if (pkey->type != EVP_PKEY_EC) {
350
        EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
351 352 353
        return NULL;
    }
    return pkey->pkey.ec;
B
Bodo Möller 已提交
354
}
355 356 357 358 359 360 361 362

EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
{
    EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
    if (ret != NULL)
        EC_KEY_up_ref(ret);
    return ret;
}
B
Bodo Möller 已提交
363 364
#endif

365
#ifndef OPENSSL_NO_DH
366

367
int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
368
{
369 370 371 372
    int ret = EVP_PKEY_assign_DH(pkey, key);
    if (ret)
        DH_up_ref(key);
    return ret;
373 374
}

375
DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
376 377
{
    if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
378
        EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
379 380 381
        return NULL;
    }
    return pkey->pkey.dh;
382
}
383 384 385 386 387 388 389 390

DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
{
    DH *ret = EVP_PKEY_get0_DH(pkey);
    if (ret != NULL)
        DH_up_ref(ret);
    return ret;
}
391 392
#endif

U
Ulf Möller 已提交
393
int EVP_PKEY_type(int type)
394 395 396 397 398 399 400 401 402
{
    int ret;
    const EVP_PKEY_ASN1_METHOD *ameth;
    ENGINE *e;
    ameth = EVP_PKEY_asn1_find(&e, type);
    if (ameth)
        ret = ameth->pkey_id;
    else
        ret = NID_undef;
403
#ifndef OPENSSL_NO_ENGINE
404 405
    if (e)
        ENGINE_finish(e);
406
#endif
407 408
    return ret;
}
409

410
int EVP_PKEY_id(const EVP_PKEY *pkey)
411 412 413
{
    return pkey->type;
}
414 415

int EVP_PKEY_base_id(const EVP_PKEY *pkey)
416 417 418
{
    return EVP_PKEY_type(pkey->type);
}
419

U
Ulf Möller 已提交
420
void EVP_PKEY_free(EVP_PKEY *x)
421 422
{
    int i;
423

424 425
    if (x == NULL)
        return;
426

427
    i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY);
428
#ifdef REF_PRINT
429
    REF_PRINT("EVP_PKEY", x);
430
#endif
431 432
    if (i > 0)
        return;
433
#ifdef REF_CHECK
434 435 436 437
    if (i < 0) {
        fprintf(stderr, "EVP_PKEY_free, bad reference count\n");
        abort();
    }
438
#endif
439
    EVP_PKEY_free_it(x);
R
Rich Salz 已提交
440
    sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
441 442
    OPENSSL_free(x);
}
443

U
Ulf Möller 已提交
444
static void EVP_PKEY_free_it(EVP_PKEY *x)
445
{
R
Rich Salz 已提交
446
    /* internal function; x is never NULL */
447 448 449 450
    if (x->ameth && x->ameth->pkey_free) {
        x->ameth->pkey_free(x);
        x->pkey.ptr = NULL;
    }
451
#ifndef OPENSSL_NO_ENGINE
452 453 454 455
    if (x->engine) {
        ENGINE_finish(x->engine);
        x->engine = NULL;
    }
456
#endif
457
}
458

459
static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
460 461 462 463 464 465 466
                     const char *kstr)
{
    BIO_indent(out, indent, 128);
    BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
               kstr, OBJ_nid2ln(pkey->type));
    return 1;
}
467 468

int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
469 470 471 472 473 474 475
                          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");
}
476 477

int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
478 479 480 481 482 483 484
                           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");
}
485 486

int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
487 488 489 490 491 492
                          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");
}
493 494

int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
495 496 497 498 499 500
{
    if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
        return -2;
    return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
                                  0, pnid);
}