]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_gn.c
GH715: ENGINE_finish can take NULL
[thirdparty/openssl.git] / crypto / evp / pmeth_gn.c
CommitLineData
0f113f3e
MC
1/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 2006.
f5cda4cb
DSH
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
f5cda4cb
DSH
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 <stdlib.h>
b39fc560 61#include "internal/cryptlib.h"
c20276e4 62#include <openssl/objects.h>
f5cda4cb 63#include <openssl/evp.h>
68c29f61 64#include "internal/bn_int.h"
27af42f9 65#include "internal/evp_int.h"
f5cda4cb
DSH
66
67int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
68{
69 int ret;
70 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
71 EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
72 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
73 return -2;
74 }
75 ctx->operation = EVP_PKEY_OP_PARAMGEN;
76 if (!ctx->pmeth->paramgen_init)
77 return 1;
78 ret = ctx->pmeth->paramgen_init(ctx);
79 if (ret <= 0)
80 ctx->operation = EVP_PKEY_OP_UNDEFINED;
81 return ret;
82}
f5cda4cb
DSH
83
84int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
0f113f3e
MC
85{
86 int ret;
87 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
88 EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
89 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
90 return -2;
91 }
92
93 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
94 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
95 return -1;
96 }
97
e34c66c6 98 if (ppkey == NULL)
0f113f3e
MC
99 return -1;
100
e34c66c6 101 if (*ppkey == NULL)
0f113f3e
MC
102 *ppkey = EVP_PKEY_new();
103
e34c66c6
EK
104 if (*ppkey == NULL) {
105 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
106 return -1;
107 }
108
0f113f3e
MC
109 ret = ctx->pmeth->paramgen(ctx, *ppkey);
110 if (ret <= 0) {
111 EVP_PKEY_free(*ppkey);
112 *ppkey = NULL;
113 }
114 return ret;
115}
f5cda4cb
DSH
116
117int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
118{
119 int ret;
120 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
121 EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
122 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
123 return -2;
124 }
125 ctx->operation = EVP_PKEY_OP_KEYGEN;
126 if (!ctx->pmeth->keygen_init)
127 return 1;
128 ret = ctx->pmeth->keygen_init(ctx);
129 if (ret <= 0)
130 ctx->operation = EVP_PKEY_OP_UNDEFINED;
131 return ret;
132}
f5cda4cb
DSH
133
134int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
0f113f3e
MC
135{
136 int ret;
137
138 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
139 EVPerr(EVP_F_EVP_PKEY_KEYGEN,
140 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
141 return -2;
142 }
143 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
144 EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
145 return -1;
146 }
147
90945fa3 148 if (ppkey == NULL)
0f113f3e
MC
149 return -1;
150
90945fa3 151 if (*ppkey == NULL)
0f113f3e 152 *ppkey = EVP_PKEY_new();
90945fa3
MC
153 if (*ppkey == NULL)
154 return -1;
0f113f3e
MC
155
156 ret = ctx->pmeth->keygen(ctx, *ppkey);
157 if (ret <= 0) {
158 EVP_PKEY_free(*ppkey);
159 *ppkey = NULL;
160 }
161 return ret;
162}
f5cda4cb
DSH
163
164void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
0f113f3e
MC
165{
166 ctx->pkey_gencb = cb;
167}
f5cda4cb 168
b28dea4e 169EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
0f113f3e
MC
170{
171 return ctx->pkey_gencb;
172}
b28dea4e 173
0f113f3e
MC
174/*
175 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
176 * callbacks.
f5cda4cb
DSH
177 */
178
179static int trans_cb(int a, int b, BN_GENCB *gcb)
0f113f3e
MC
180{
181 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
182 ctx->keygen_info[0] = a;
183 ctx->keygen_info[1] = b;
184 return ctx->pkey_gencb(ctx);
185}
f5cda4cb
DSH
186
187void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
0f113f3e
MC
188{
189 BN_GENCB_set(cb, trans_cb, ctx);
190}
f5cda4cb
DSH
191
192int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
0f113f3e
MC
193{
194 if (idx == -1)
195 return ctx->keygen_info_count;
196 if (idx < 0 || idx > ctx->keygen_info_count)
197 return 0;
198 return ctx->keygen_info[idx];
199}
2022cfe0
DSH
200
201EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
0f113f3e
MC
202 const unsigned char *key, int keylen)
203{
204 EVP_PKEY_CTX *mac_ctx = NULL;
205 EVP_PKEY *mac_key = NULL;
206 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
207 if (!mac_ctx)
208 return NULL;
209 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
210 goto merr;
eff1a4d2 211 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
0f113f3e
MC
212 goto merr;
213 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
214 goto merr;
215 merr:
c5ba2d99 216 EVP_PKEY_CTX_free(mac_ctx);
0f113f3e
MC
217 return mac_key;
218}