pmeth_lib.c revision 312826
1/* pmeth_lib.c */
2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 2006.
5 */
6/* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in
18 *    the documentation and/or other materials provided with the
19 *    distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 *    software must display the following acknowledgment:
23 *    "This product includes software developed by the OpenSSL Project
24 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 *    endorse or promote products derived from this software without
28 *    prior written permission. For written permission, please contact
29 *    licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 *    nor may "OpenSSL" appear in their names without prior written
33 *    permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 *    acknowledgment:
37 *    "This product includes software developed by the OpenSSL Project
38 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com).  This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
60#include <stdio.h>
61#include <stdlib.h>
62#include "cryptlib.h"
63#include <openssl/objects.h>
64#include <openssl/evp.h>
65#ifndef OPENSSL_NO_ENGINE
66# include <openssl/engine.h>
67#endif
68#include "asn1_locl.h"
69#include "evp_locl.h"
70
71typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
72
73DECLARE_STACK_OF(EVP_PKEY_METHOD)
74STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
75
76extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth;
77extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth, cmac_pkey_meth;
78extern const EVP_PKEY_METHOD dhx_pkey_meth;
79
80static const EVP_PKEY_METHOD *standard_methods[] = {
81#ifndef OPENSSL_NO_RSA
82    &rsa_pkey_meth,
83#endif
84#ifndef OPENSSL_NO_DH
85    &dh_pkey_meth,
86#endif
87#ifndef OPENSSL_NO_DSA
88    &dsa_pkey_meth,
89#endif
90#ifndef OPENSSL_NO_EC
91    &ec_pkey_meth,
92#endif
93    &hmac_pkey_meth,
94#ifndef OPENSSL_NO_CMAC
95    &cmac_pkey_meth,
96#endif
97#ifndef OPENSSL_NO_DH
98    &dhx_pkey_meth
99#endif
100};
101
102DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
103                           pmeth);
104
105static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
106                     const EVP_PKEY_METHOD *const *b)
107{
108    return ((*a)->pkey_id - (*b)->pkey_id);
109}
110
111IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
112                             pmeth);
113
114const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
115{
116    EVP_PKEY_METHOD tmp;
117    const EVP_PKEY_METHOD *t = &tmp, **ret;
118    tmp.pkey_id = type;
119    if (app_pkey_methods) {
120        int idx;
121        idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
122        if (idx >= 0)
123            return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
124    }
125    ret = OBJ_bsearch_pmeth(&t, standard_methods,
126                            sizeof(standard_methods) /
127                            sizeof(EVP_PKEY_METHOD *));
128    if (!ret || !*ret)
129        return NULL;
130    return *ret;
131}
132
133static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
134{
135    EVP_PKEY_CTX *ret;
136    const EVP_PKEY_METHOD *pmeth;
137    if (id == -1) {
138        if (!pkey || !pkey->ameth)
139            return NULL;
140        id = pkey->ameth->pkey_id;
141    }
142#ifndef OPENSSL_NO_ENGINE
143    if (pkey && pkey->engine)
144        e = pkey->engine;
145    /* Try to find an ENGINE which implements this method */
146    if (e) {
147        if (!ENGINE_init(e)) {
148            EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
149            return NULL;
150        }
151    } else
152        e = ENGINE_get_pkey_meth_engine(id);
153
154    /*
155     * If an ENGINE handled this method look it up. Othewise use internal
156     * tables.
157     */
158
159    if (e)
160        pmeth = ENGINE_get_pkey_meth(e, id);
161    else
162#endif
163        pmeth = EVP_PKEY_meth_find(id);
164
165    if (pmeth == NULL) {
166        EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
167        return NULL;
168    }
169
170    ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
171    if (!ret) {
172#ifndef OPENSSL_NO_ENGINE
173        if (e)
174            ENGINE_finish(e);
175#endif
176        EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
177        return NULL;
178    }
179    ret->engine = e;
180    ret->pmeth = pmeth;
181    ret->operation = EVP_PKEY_OP_UNDEFINED;
182    ret->pkey = pkey;
183    ret->peerkey = NULL;
184    ret->pkey_gencb = 0;
185    if (pkey)
186        CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
187    ret->data = NULL;
188
189    if (pmeth->init) {
190        if (pmeth->init(ret) <= 0) {
191            EVP_PKEY_CTX_free(ret);
192            return NULL;
193        }
194    }
195
196    return ret;
197}
198
199EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
200{
201    EVP_PKEY_METHOD *pmeth;
202
203    pmeth = OPENSSL_malloc(sizeof(EVP_PKEY_METHOD));
204    if (!pmeth)
205        return NULL;
206
207    memset(pmeth, 0, sizeof(EVP_PKEY_METHOD));
208
209    pmeth->pkey_id = id;
210    pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
211    return pmeth;
212}
213
214void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
215                             const EVP_PKEY_METHOD *meth)
216{
217    if (ppkey_id)
218        *ppkey_id = meth->pkey_id;
219    if (pflags)
220        *pflags = meth->flags;
221}
222
223void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
224{
225
226    dst->init = src->init;
227    dst->copy = src->copy;
228    dst->cleanup = src->cleanup;
229
230    dst->paramgen_init = src->paramgen_init;
231    dst->paramgen = src->paramgen;
232
233    dst->keygen_init = src->keygen_init;
234    dst->keygen = src->keygen;
235
236    dst->sign_init = src->sign_init;
237    dst->sign = src->sign;
238
239    dst->verify_init = src->verify_init;
240    dst->verify = src->verify;
241
242    dst->verify_recover_init = src->verify_recover_init;
243    dst->verify_recover = src->verify_recover;
244
245    dst->signctx_init = src->signctx_init;
246    dst->signctx = src->signctx;
247
248    dst->verifyctx_init = src->verifyctx_init;
249    dst->verifyctx = src->verifyctx;
250
251    dst->encrypt_init = src->encrypt_init;
252    dst->encrypt = src->encrypt;
253
254    dst->decrypt_init = src->decrypt_init;
255    dst->decrypt = src->decrypt;
256
257    dst->derive_init = src->derive_init;
258    dst->derive = src->derive;
259
260    dst->ctrl = src->ctrl;
261    dst->ctrl_str = src->ctrl_str;
262}
263
264void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
265{
266    if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
267        OPENSSL_free(pmeth);
268}
269
270EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
271{
272    return int_ctx_new(pkey, e, -1);
273}
274
275EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
276{
277    return int_ctx_new(NULL, e, id);
278}
279
280EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
281{
282    EVP_PKEY_CTX *rctx;
283    if (!pctx->pmeth || !pctx->pmeth->copy)
284        return NULL;
285#ifndef OPENSSL_NO_ENGINE
286    /* Make sure it's safe to copy a pkey context using an ENGINE */
287    if (pctx->engine && !ENGINE_init(pctx->engine)) {
288        EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
289        return 0;
290    }
291#endif
292    rctx = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
293    if (!rctx)
294        return NULL;
295
296    rctx->pmeth = pctx->pmeth;
297#ifndef OPENSSL_NO_ENGINE
298    rctx->engine = pctx->engine;
299#endif
300
301    if (pctx->pkey)
302        CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
303
304    rctx->pkey = pctx->pkey;
305
306    if (pctx->peerkey)
307        CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
308
309    rctx->peerkey = pctx->peerkey;
310
311    rctx->data = NULL;
312    rctx->app_data = NULL;
313    rctx->operation = pctx->operation;
314
315    if (pctx->pmeth->copy(rctx, pctx) > 0)
316        return rctx;
317
318    EVP_PKEY_CTX_free(rctx);
319    return NULL;
320
321}
322
323int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
324{
325    if (app_pkey_methods == NULL) {
326        app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
327        if (!app_pkey_methods)
328            return 0;
329    }
330    if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
331        return 0;
332    sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
333    return 1;
334}
335
336void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
337{
338    if (ctx == NULL)
339        return;
340    if (ctx->pmeth && ctx->pmeth->cleanup)
341        ctx->pmeth->cleanup(ctx);
342    if (ctx->pkey)
343        EVP_PKEY_free(ctx->pkey);
344    if (ctx->peerkey)
345        EVP_PKEY_free(ctx->peerkey);
346#ifndef OPENSSL_NO_ENGINE
347    if (ctx->engine)
348        /*
349         * The EVP_PKEY_CTX we used belongs to an ENGINE, release the
350         * functional reference we held for this reason.
351         */
352        ENGINE_finish(ctx->engine);
353#endif
354    OPENSSL_free(ctx);
355}
356
357int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
358                      int cmd, int p1, void *p2)
359{
360    int ret;
361    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
362        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
363        return -2;
364    }
365    if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
366        return -1;
367
368    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
369        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
370        return -1;
371    }
372
373    if ((optype != -1) && !(ctx->operation & optype)) {
374        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
375        return -1;
376    }
377
378    ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
379
380    if (ret == -2)
381        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
382
383    return ret;
384
385}
386
387int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
388                          const char *name, const char *value)
389{
390    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
391        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
392        return -2;
393    }
394    if (!strcmp(name, "digest")) {
395        const EVP_MD *md;
396        if (!value || !(md = EVP_get_digestbyname(value))) {
397            EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);
398            return 0;
399        }
400        return EVP_PKEY_CTX_set_signature_md(ctx, md);
401    }
402    return ctx->pmeth->ctrl_str(ctx, name, value);
403}
404
405int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
406{
407    return ctx->operation;
408}
409
410void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
411{
412    ctx->keygen_info = dat;
413    ctx->keygen_info_count = datlen;
414}
415
416void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
417{
418    ctx->data = data;
419}
420
421void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
422{
423    return ctx->data;
424}
425
426EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
427{
428    return ctx->pkey;
429}
430
431EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
432{
433    return ctx->peerkey;
434}
435
436void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
437{
438    ctx->app_data = data;
439}
440
441void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
442{
443    return ctx->app_data;
444}
445
446void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
447                            int (*init) (EVP_PKEY_CTX *ctx))
448{
449    pmeth->init = init;
450}
451
452void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
453                            int (*copy) (EVP_PKEY_CTX *dst,
454                                         EVP_PKEY_CTX *src))
455{
456    pmeth->copy = copy;
457}
458
459void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
460                               void (*cleanup) (EVP_PKEY_CTX *ctx))
461{
462    pmeth->cleanup = cleanup;
463}
464
465void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
466                                int (*paramgen_init) (EVP_PKEY_CTX *ctx),
467                                int (*paramgen) (EVP_PKEY_CTX *ctx,
468                                                 EVP_PKEY *pkey))
469{
470    pmeth->paramgen_init = paramgen_init;
471    pmeth->paramgen = paramgen;
472}
473
474void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
475                              int (*keygen_init) (EVP_PKEY_CTX *ctx),
476                              int (*keygen) (EVP_PKEY_CTX *ctx,
477                                             EVP_PKEY *pkey))
478{
479    pmeth->keygen_init = keygen_init;
480    pmeth->keygen = keygen;
481}
482
483void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
484                            int (*sign_init) (EVP_PKEY_CTX *ctx),
485                            int (*sign) (EVP_PKEY_CTX *ctx,
486                                         unsigned char *sig, size_t *siglen,
487                                         const unsigned char *tbs,
488                                         size_t tbslen))
489{
490    pmeth->sign_init = sign_init;
491    pmeth->sign = sign;
492}
493
494void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
495                              int (*verify_init) (EVP_PKEY_CTX *ctx),
496                              int (*verify) (EVP_PKEY_CTX *ctx,
497                                             const unsigned char *sig,
498                                             size_t siglen,
499                                             const unsigned char *tbs,
500                                             size_t tbslen))
501{
502    pmeth->verify_init = verify_init;
503    pmeth->verify = verify;
504}
505
506void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
507                                      int (*verify_recover_init) (EVP_PKEY_CTX
508                                                                  *ctx),
509                                      int (*verify_recover) (EVP_PKEY_CTX
510                                                             *ctx,
511                                                             unsigned char
512                                                             *sig,
513                                                             size_t *siglen,
514                                                             const unsigned
515                                                             char *tbs,
516                                                             size_t tbslen))
517{
518    pmeth->verify_recover_init = verify_recover_init;
519    pmeth->verify_recover = verify_recover;
520}
521
522void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
523                               int (*signctx_init) (EVP_PKEY_CTX *ctx,
524                                                    EVP_MD_CTX *mctx),
525                               int (*signctx) (EVP_PKEY_CTX *ctx,
526                                               unsigned char *sig,
527                                               size_t *siglen,
528                                               EVP_MD_CTX *mctx))
529{
530    pmeth->signctx_init = signctx_init;
531    pmeth->signctx = signctx;
532}
533
534void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
535                                 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
536                                                        EVP_MD_CTX *mctx),
537                                 int (*verifyctx) (EVP_PKEY_CTX *ctx,
538                                                   const unsigned char *sig,
539                                                   int siglen,
540                                                   EVP_MD_CTX *mctx))
541{
542    pmeth->verifyctx_init = verifyctx_init;
543    pmeth->verifyctx = verifyctx;
544}
545
546void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
547                               int (*encrypt_init) (EVP_PKEY_CTX *ctx),
548                               int (*encryptfn) (EVP_PKEY_CTX *ctx,
549                                                 unsigned char *out,
550                                                 size_t *outlen,
551                                                 const unsigned char *in,
552                                                 size_t inlen))
553{
554    pmeth->encrypt_init = encrypt_init;
555    pmeth->encrypt = encryptfn;
556}
557
558void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
559                               int (*decrypt_init) (EVP_PKEY_CTX *ctx),
560                               int (*decrypt) (EVP_PKEY_CTX *ctx,
561                                               unsigned char *out,
562                                               size_t *outlen,
563                                               const unsigned char *in,
564                                               size_t inlen))
565{
566    pmeth->decrypt_init = decrypt_init;
567    pmeth->decrypt = decrypt;
568}
569
570void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
571                              int (*derive_init) (EVP_PKEY_CTX *ctx),
572                              int (*derive) (EVP_PKEY_CTX *ctx,
573                                             unsigned char *key,
574                                             size_t *keylen))
575{
576    pmeth->derive_init = derive_init;
577    pmeth->derive = derive;
578}
579
580void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
581                            int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
582                                         void *p2),
583                            int (*ctrl_str) (EVP_PKEY_CTX *ctx,
584                                             const char *type,
585                                             const char *value))
586{
587    pmeth->ctrl = ctrl;
588    pmeth->ctrl_str = ctrl_str;
589}
590