Handle mdname in legacy EVP_DigestSignInit_ex codepaths
[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     /* legacy */
39     /* call customized public key check function first */
40     if (ctx->pmeth->public_check != NULL)
41         return ctx->pmeth->public_check(pkey);
42
43     /* use default public key check function in ameth */
44     if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
45         EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
46                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
47         return -2;
48     }
49
50     return pkey->ameth->pkey_public_check(pkey);
51 }
52
53 int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
54 {
55     EVP_PKEY *pkey = ctx->pkey;
56     void *key;
57     EVP_KEYMGMT *keymgmt;
58
59     if (pkey == NULL) {
60         EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
61         return 0;
62     }
63
64     keymgmt = pkey->keymgmt;
65     key = pkey->keydata;
66
67     if (key != NULL && keymgmt != NULL)
68         return evp_keymgmt_validate(keymgmt, key,
69                                     OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
70
71     /* call customized param check function first */
72     if (ctx->pmeth->param_check != NULL)
73         return ctx->pmeth->param_check(pkey);
74
75     /* legacy */
76     /* use default param check function in ameth */
77     if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
78         EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
79                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
80         return -2;
81     }
82
83     return pkey->ameth->pkey_param_check(pkey);
84 }
85
86 int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
87 {
88     EVP_PKEY *pkey = ctx->pkey;
89     void *key;
90     EVP_KEYMGMT *keymgmt;
91
92     if (pkey == NULL) {
93         EVPerr(0, EVP_R_NO_KEY_SET);
94         return 0;
95     }
96
97     keymgmt = pkey->keymgmt;
98     key = pkey->keydata;
99
100     if (key != NULL && keymgmt != NULL)
101         return evp_keymgmt_validate(keymgmt, key,
102                                     OSSL_KEYMGMT_SELECT_PRIVATE_KEY);
103     /* not supported for legacy keys */
104     return -2;
105 }
106
107 int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
108 {
109     EVP_PKEY *pkey = ctx->pkey;
110     void *key;
111     EVP_KEYMGMT *keymgmt;
112
113     if (pkey == NULL) {
114         EVPerr(0, EVP_R_NO_KEY_SET);
115         return 0;
116     }
117
118     keymgmt = pkey->keymgmt;
119     key = pkey->keydata;
120
121     if (key != NULL && keymgmt != NULL)
122         return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_KEYPAIR);
123     /* not supported for legacy keys */
124     return -2;
125 }
126
127 int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
128 {
129     EVP_PKEY *pkey = ctx->pkey;
130     void *key;
131     EVP_KEYMGMT *keymgmt;
132
133     if (pkey == NULL) {
134         EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
135         return 0;
136     }
137
138     keymgmt = pkey->keymgmt;
139     key = pkey->keydata;
140
141     if (key != NULL && keymgmt != NULL)
142         return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_ALL);
143
144     /* legacy */
145     /* call customized check function first */
146     if (ctx->pmeth->check != NULL)
147         return ctx->pmeth->check(pkey);
148
149     /* use default check function in ameth */
150     if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
151         EVPerr(EVP_F_EVP_PKEY_CHECK,
152                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
153         return -2;
154     }
155
156     return pkey->ameth->pkey_check(pkey);
157 }
158