Make EVP_PKEY_CTX_[get|set]_group_name work for DH too
[openssl.git] / crypto / evp / pmeth_check.c
1 /*
2  * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "crypto/bn.h"
16 #include "crypto/asn1.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
19
20 int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
21 {
22     EVP_PKEY *pkey = ctx->pkey;
23     void *key;
24     EVP_KEYMGMT *keymgmt;
25
26     if (pkey == NULL) {
27         EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
28         return 0;
29     }
30
31     keymgmt = pkey->keymgmt;
32     key = pkey->keydata;
33
34     if (key != NULL && keymgmt != NULL)
35         return evp_keymgmt_validate(keymgmt, key,
36                                     OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
37
38     if (pkey->type == EVP_PKEY_NONE)
39         goto not_supported;
40
41 #ifndef FIPS_MODULE
42     /* legacy */
43     /* call customized public key check function first */
44     if (ctx->pmeth->public_check != NULL)
45         return ctx->pmeth->public_check(pkey);
46
47     /* use default public key check function in ameth */
48     if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL)
49         goto not_supported;
50
51     return pkey->ameth->pkey_public_check(pkey);
52 #endif
53  not_supported:
54     EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
55     return -2;
56 }
57
58 int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
59 {
60     EVP_PKEY *pkey = ctx->pkey;
61     void *key;
62     EVP_KEYMGMT *keymgmt;
63
64     if (pkey == NULL) {
65         EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
66         return 0;
67     }
68
69     keymgmt = pkey->keymgmt;
70     key = pkey->keydata;
71
72     if (key != NULL && keymgmt != NULL)
73         return evp_keymgmt_validate(keymgmt, key,
74                                     OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
75
76     if (pkey->type == EVP_PKEY_NONE)
77         goto not_supported;
78
79 #ifndef FIPS_MODULE
80     /* legacy */
81     /* call customized param check function first */
82     if (ctx->pmeth->param_check != NULL)
83         return ctx->pmeth->param_check(pkey);
84
85     /* use default param check function in ameth */
86     if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL)
87         goto not_supported;
88
89     return pkey->ameth->pkey_param_check(pkey);
90 #endif
91  not_supported:
92     EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
93     return -2;
94 }
95
96 int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
97 {
98     EVP_PKEY *pkey = ctx->pkey;
99     void *key;
100     EVP_KEYMGMT *keymgmt;
101
102     if (pkey == NULL) {
103         EVPerr(0, EVP_R_NO_KEY_SET);
104         return 0;
105     }
106
107     keymgmt = pkey->keymgmt;
108     key = pkey->keydata;
109
110     if (key != NULL && keymgmt != NULL)
111         return evp_keymgmt_validate(keymgmt, key,
112                                     OSSL_KEYMGMT_SELECT_PRIVATE_KEY);
113     /* not supported for legacy keys */
114     EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
115     return -2;
116 }
117
118 int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
119 {
120     EVP_PKEY *pkey = ctx->pkey;
121     void *key;
122     EVP_KEYMGMT *keymgmt;
123
124     if (pkey == NULL) {
125         EVPerr(0, EVP_R_NO_KEY_SET);
126         return 0;
127     }
128
129     keymgmt = pkey->keymgmt;
130     key = pkey->keydata;
131
132     if (key != NULL && keymgmt != NULL)
133         return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_KEYPAIR);
134     /* not supported for legacy keys */
135     EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
136     return -2;
137 }
138
139 int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
140 {
141     EVP_PKEY *pkey = ctx->pkey;
142     void *key;
143     EVP_KEYMGMT *keymgmt;
144
145     if (pkey == NULL) {
146         EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
147         return 0;
148     }
149
150     keymgmt = pkey->keymgmt;
151     key = pkey->keydata;
152
153     if (key != NULL && keymgmt != NULL)
154         return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_ALL);
155
156     if (pkey->type == EVP_PKEY_NONE)
157         goto not_supported;
158
159 #ifndef FIPS_MODULE
160     /* legacy */
161     /* call customized check function first */
162     if (ctx->pmeth->check != NULL)
163         return ctx->pmeth->check(pkey);
164
165     /* use default check function in ameth */
166     if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL)
167         goto not_supported;
168
169     return pkey->ameth->pkey_check(pkey);
170 #endif
171  not_supported:
172     EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
173     return -2;
174 }
175