/src/openssl/crypto/evp/pmeth_check.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2006-2021 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  |  | #ifndef FIPS_MODULE  | 
17  |  | # include "crypto/asn1.h"  | 
18  |  | #endif  | 
19  |  | #include "crypto/evp.h"  | 
20  |  | #include "evp_local.h"  | 
21  |  |  | 
22  |  | /*  | 
23  |  |  * Returns:  | 
24  |  |  *  1   True  | 
25  |  |  *  0   False  | 
26  |  |  * -1   Unsupported (use legacy path)  | 
27  |  |  */  | 
28  |  | static int try_provided_check(EVP_PKEY_CTX *ctx, int selection, int checktype)  | 
29  | 0  | { | 
30  | 0  |     EVP_KEYMGMT *keymgmt;  | 
31  | 0  |     void *keydata;  | 
32  |  | 
  | 
33  | 0  |     if (evp_pkey_ctx_is_legacy(ctx))  | 
34  | 0  |         return -1;  | 
35  |  |  | 
36  | 0  |     keymgmt = ctx->keymgmt;  | 
37  | 0  |     keydata = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,  | 
38  | 0  |                                           &keymgmt, ctx->propquery);  | 
39  | 0  |     if (keydata == NULL) { | 
40  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);  | 
41  | 0  |         return 0;  | 
42  | 0  |     }  | 
43  |  |  | 
44  | 0  |     return evp_keymgmt_validate(keymgmt, keydata, selection, checktype);  | 
45  | 0  | }  | 
46  |  |  | 
47  |  | static int evp_pkey_public_check_combined(EVP_PKEY_CTX *ctx, int checktype)  | 
48  | 0  | { | 
49  | 0  |     EVP_PKEY *pkey = ctx->pkey;  | 
50  | 0  |     int ok;  | 
51  |  | 
  | 
52  | 0  |     if (pkey == NULL) { | 
53  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);  | 
54  | 0  |         return 0;  | 
55  | 0  |     }  | 
56  |  |  | 
57  | 0  |     if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,  | 
58  | 0  |                                  checktype)) != -1)  | 
59  | 0  |         return ok;  | 
60  |  |  | 
61  | 0  |     if (pkey->type == EVP_PKEY_NONE)  | 
62  | 0  |         goto not_supported;  | 
63  |  |  | 
64  | 0  | #ifndef FIPS_MODULE  | 
65  |  |     /* legacy */  | 
66  |  |     /* call customized public key check function first */  | 
67  | 0  |     if (ctx->pmeth->public_check != NULL)  | 
68  | 0  |         return ctx->pmeth->public_check(pkey);  | 
69  |  |  | 
70  |  |     /* use default public key check function in ameth */  | 
71  | 0  |     if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL)  | 
72  | 0  |         goto not_supported;  | 
73  |  |  | 
74  | 0  |     return pkey->ameth->pkey_public_check(pkey);  | 
75  | 0  | #endif  | 
76  | 0  |  not_supported:  | 
77  | 0  |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
78  | 0  |     return -2;  | 
79  | 0  | }  | 
80  |  |  | 
81  |  | int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)  | 
82  | 0  | { | 
83  | 0  |     return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);  | 
84  | 0  | }  | 
85  |  |  | 
86  |  | int EVP_PKEY_public_check_quick(EVP_PKEY_CTX *ctx)  | 
87  | 0  | { | 
88  | 0  |     return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);  | 
89  | 0  | }  | 
90  |  |  | 
91  |  | static int evp_pkey_param_check_combined(EVP_PKEY_CTX *ctx, int checktype)  | 
92  | 0  | { | 
93  | 0  |     EVP_PKEY *pkey = ctx->pkey;  | 
94  | 0  |     int ok;  | 
95  |  | 
  | 
96  | 0  |     if (pkey == NULL) { | 
97  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);  | 
98  | 0  |         return 0;  | 
99  | 0  |     }  | 
100  |  |  | 
101  | 0  |     if ((ok = try_provided_check(ctx,  | 
102  | 0  |                                  OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,  | 
103  | 0  |                                  checktype)) != -1)  | 
104  | 0  |         return ok;  | 
105  |  |  | 
106  | 0  |     if (pkey->type == EVP_PKEY_NONE)  | 
107  | 0  |         goto not_supported;  | 
108  |  |  | 
109  | 0  | #ifndef FIPS_MODULE  | 
110  |  |     /* legacy */  | 
111  |  |     /* call customized param check function first */  | 
112  | 0  |     if (ctx->pmeth->param_check != NULL)  | 
113  | 0  |         return ctx->pmeth->param_check(pkey);  | 
114  |  |  | 
115  |  |     /* use default param check function in ameth */  | 
116  | 0  |     if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL)  | 
117  | 0  |         goto not_supported;  | 
118  |  |  | 
119  | 0  |     return pkey->ameth->pkey_param_check(pkey);  | 
120  | 0  | #endif  | 
121  | 0  |  not_supported:  | 
122  | 0  |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
123  | 0  |     return -2;  | 
124  | 0  | }  | 
125  |  |  | 
126  |  | int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)  | 
127  | 0  | { | 
128  | 0  |     return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);  | 
129  | 0  | }  | 
130  |  |  | 
131  |  | int EVP_PKEY_param_check_quick(EVP_PKEY_CTX *ctx)  | 
132  | 0  | { | 
133  | 0  |     return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);  | 
134  | 0  | }  | 
135  |  |  | 
136  |  | int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)  | 
137  | 0  | { | 
138  | 0  |     EVP_PKEY *pkey = ctx->pkey;  | 
139  | 0  |     int ok;  | 
140  |  | 
  | 
141  | 0  |     if (pkey == NULL) { | 
142  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);  | 
143  | 0  |         return 0;  | 
144  | 0  |     }  | 
145  |  |  | 
146  | 0  |     if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,  | 
147  | 0  |                                  OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1)  | 
148  | 0  |         return ok;  | 
149  |  |  | 
150  |  |     /* not supported for legacy keys */  | 
151  | 0  |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
152  | 0  |     return -2;  | 
153  | 0  | }  | 
154  |  |  | 
155  |  | int EVP_PKEY_check(EVP_PKEY_CTX *ctx)  | 
156  | 0  | { | 
157  | 0  |     return EVP_PKEY_pairwise_check(ctx);  | 
158  | 0  | }  | 
159  |  |  | 
160  |  | int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)  | 
161  | 0  | { | 
162  | 0  |     EVP_PKEY *pkey = ctx->pkey;  | 
163  | 0  |     int ok;  | 
164  |  | 
  | 
165  | 0  |     if (pkey == NULL) { | 
166  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);  | 
167  | 0  |         return 0;  | 
168  | 0  |     }  | 
169  |  |  | 
170  | 0  |     if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_KEYPAIR,  | 
171  | 0  |                                  OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1)  | 
172  | 0  |         return ok;  | 
173  |  |  | 
174  | 0  |     if (pkey->type == EVP_PKEY_NONE)  | 
175  | 0  |         goto not_supported;  | 
176  |  |  | 
177  | 0  | #ifndef FIPS_MODULE  | 
178  |  |     /* legacy */  | 
179  |  |     /* call customized check function first */  | 
180  | 0  |     if (ctx->pmeth->check != NULL)  | 
181  | 0  |         return ctx->pmeth->check(pkey);  | 
182  |  |  | 
183  |  |     /* use default check function in ameth */  | 
184  | 0  |     if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL)  | 
185  | 0  |         goto not_supported;  | 
186  |  |  | 
187  | 0  |     return pkey->ameth->pkey_check(pkey);  | 
188  | 0  | #endif  | 
189  | 0  |  not_supported:  | 
190  | 0  |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
191  | 0  |     return -2;  | 
192  | 0  | }  | 
193  |  |  |