Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/evp/pmeth_gn.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/objects.h>
14
#include <openssl/evp.h>
15
#include "internal/bn_int.h"
16
#include "internal/asn1_int.h"
17
#include "internal/evp_int.h"
18
19
int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
20
0
{
21
0
    int ret;
22
0
    if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
23
0
        EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
24
0
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
25
0
        return -2;
26
0
    }
27
0
    ctx->operation = EVP_PKEY_OP_PARAMGEN;
28
0
    if (!ctx->pmeth->paramgen_init)
29
0
        return 1;
30
0
    ret = ctx->pmeth->paramgen_init(ctx);
31
0
    if (ret <= 0)
32
0
        ctx->operation = EVP_PKEY_OP_UNDEFINED;
33
0
    return ret;
34
0
}
35
36
int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
37
0
{
38
0
    int ret;
39
0
    if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
40
0
        EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
41
0
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
42
0
        return -2;
43
0
    }
44
0
45
0
    if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
46
0
        EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
47
0
        return -1;
48
0
    }
49
0
50
0
    if (ppkey == NULL)
51
0
        return -1;
52
0
53
0
    if (*ppkey == NULL)
54
0
        *ppkey = EVP_PKEY_new();
55
0
56
0
    if (*ppkey == NULL) {
57
0
        EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
58
0
        return -1;
59
0
    }
60
0
61
0
    ret = ctx->pmeth->paramgen(ctx, *ppkey);
62
0
    if (ret <= 0) {
63
0
        EVP_PKEY_free(*ppkey);
64
0
        *ppkey = NULL;
65
0
    }
66
0
    return ret;
67
0
}
68
69
int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
70
0
{
71
0
    int ret;
72
0
    if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
73
0
        EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
74
0
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
75
0
        return -2;
76
0
    }
77
0
    ctx->operation = EVP_PKEY_OP_KEYGEN;
78
0
    if (!ctx->pmeth->keygen_init)
79
0
        return 1;
80
0
    ret = ctx->pmeth->keygen_init(ctx);
81
0
    if (ret <= 0)
82
0
        ctx->operation = EVP_PKEY_OP_UNDEFINED;
83
0
    return ret;
84
0
}
85
86
int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
87
0
{
88
0
    int ret;
89
0
90
0
    if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
91
0
        EVPerr(EVP_F_EVP_PKEY_KEYGEN,
92
0
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
93
0
        return -2;
94
0
    }
95
0
    if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
96
0
        EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
97
0
        return -1;
98
0
    }
99
0
100
0
    if (ppkey == NULL)
101
0
        return -1;
102
0
103
0
    if (*ppkey == NULL)
104
0
        *ppkey = EVP_PKEY_new();
105
0
    if (*ppkey == NULL)
106
0
        return -1;
107
0
108
0
    ret = ctx->pmeth->keygen(ctx, *ppkey);
109
0
    if (ret <= 0) {
110
0
        EVP_PKEY_free(*ppkey);
111
0
        *ppkey = NULL;
112
0
    }
113
0
    return ret;
114
0
}
115
116
void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
117
0
{
118
0
    ctx->pkey_gencb = cb;
119
0
}
120
121
EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
122
0
{
123
0
    return ctx->pkey_gencb;
124
0
}
125
126
/*
127
 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
128
 * callbacks.
129
 */
130
131
static int trans_cb(int a, int b, BN_GENCB *gcb)
132
0
{
133
0
    EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
134
0
    ctx->keygen_info[0] = a;
135
0
    ctx->keygen_info[1] = b;
136
0
    return ctx->pkey_gencb(ctx);
137
0
}
138
139
void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
140
0
{
141
0
    BN_GENCB_set(cb, trans_cb, ctx);
142
0
}
143
144
int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
145
0
{
146
0
    if (idx == -1)
147
0
        return ctx->keygen_info_count;
148
0
    if (idx < 0 || idx > ctx->keygen_info_count)
149
0
        return 0;
150
0
    return ctx->keygen_info[idx];
151
0
}
152
153
EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
154
                               const unsigned char *key, int keylen)
155
0
{
156
0
    EVP_PKEY_CTX *mac_ctx = NULL;
157
0
    EVP_PKEY *mac_key = NULL;
158
0
    mac_ctx = EVP_PKEY_CTX_new_id(type, e);
159
0
    if (!mac_ctx)
160
0
        return NULL;
161
0
    if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
162
0
        goto merr;
163
0
    if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
164
0
        goto merr;
165
0
    if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
166
0
        goto merr;
167
0
 merr:
168
0
    EVP_PKEY_CTX_free(mac_ctx);
169
0
    return mac_key;
170
0
}
171
172
int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
173
0
{
174
0
    EVP_PKEY *pkey = ctx->pkey;
175
0
176
0
    if (pkey == NULL) {
177
0
        EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
178
0
        return 0;
179
0
    }
180
0
181
0
    /* call customized check function first */
182
0
    if (ctx->pmeth->check != NULL)
183
0
        return ctx->pmeth->check(pkey);
184
0
185
0
    /* use default check function in ameth */
186
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
187
0
        EVPerr(EVP_F_EVP_PKEY_CHECK,
188
0
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
189
0
        return -2;
190
0
    }
191
0
192
0
    return pkey->ameth->pkey_check(pkey);
193
0
}
194
195
int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
196
0
{
197
0
    EVP_PKEY *pkey = ctx->pkey;
198
0
199
0
    if (pkey == NULL) {
200
0
        EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
201
0
        return 0;
202
0
    }
203
0
204
0
    /* call customized public key check function first */
205
0
    if (ctx->pmeth->public_check != NULL)
206
0
        return ctx->pmeth->public_check(pkey);
207
0
208
0
    /* use default public key check function in ameth */
209
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
210
0
        EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
211
0
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
212
0
        return -2;
213
0
    }
214
0
215
0
    return pkey->ameth->pkey_public_check(pkey);
216
0
}
217
218
int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
219
0
{
220
0
    EVP_PKEY *pkey = ctx->pkey;
221
0
222
0
    if (pkey == NULL) {
223
0
        EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
224
0
        return 0;
225
0
    }
226
0
227
0
    /* call customized param check function first */
228
0
    if (ctx->pmeth->param_check != NULL)
229
0
        return ctx->pmeth->param_check(pkey);
230
0
231
0
    /* use default param check function in ameth */
232
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
233
0
        EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
234
0
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
235
0
        return -2;
236
0
    }
237
0
238
0
    return pkey->ameth->pkey_param_check(pkey);
239
0
}