summaryrefslogtreecommitdiff
path: root/crypto/evp/pmeth_check.c
blob: 1186ad2b12434db8e6ab10dd460c31a84ccb3055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdio.h>
#include <stdlib.h>
#include "internal/cryptlib.h"
#include <openssl/objects.h>
#include <openssl/evp.h>
#include "crypto/bn.h"
#include "crypto/asn1.h"
#include "crypto/evp.h"
#include "evp_local.h"

int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
{
    EVP_PKEY *pkey = ctx->pkey;
    void *key;
    EVP_KEYMGMT *keymgmt;

    if (pkey == NULL) {
        EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
        return 0;
    }

    keymgmt = pkey->pkeys[0].keymgmt;
    key = pkey->pkeys[0].keydata;

    if (key != NULL && keymgmt != NULL)
        return evp_keymgmt_validate(keymgmt, key,
                                    OSSL_KEYMGMT_SELECT_PUBLIC_KEY);

    /* legacy */
    /* call customized public key check function first */
    if (ctx->pmeth->public_check != NULL)
        return ctx->pmeth->public_check(pkey);

    /* use default public key check function in ameth */
    if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
        EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
        return -2;
    }

    return pkey->ameth->pkey_public_check(pkey);
}

int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
{
    EVP_PKEY *pkey = ctx->pkey;
    void *key;
    EVP_KEYMGMT *keymgmt;

    if (pkey == NULL) {
        EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
        return 0;
    }

    keymgmt = pkey->pkeys[0].keymgmt;
    key = pkey->pkeys[0].keydata;

    if (key != NULL && keymgmt != NULL)
        return evp_keymgmt_validate(keymgmt, key,
                                    OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);

    /* call customized param check function first */
    if (ctx->pmeth->param_check != NULL)
        return ctx->pmeth->param_check(pkey);

    /* legacy */
    /* use default param check function in ameth */
    if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
        EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
        return -2;
    }

    return pkey->ameth->pkey_param_check(pkey);
}

int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
{
    EVP_PKEY *pkey = ctx->pkey;
    void *key;
    EVP_KEYMGMT *keymgmt;

    if (pkey == NULL) {
        EVPerr(0, EVP_R_NO_KEY_SET);
        return 0;
    }

    keymgmt = pkey->pkeys[0].keymgmt;
    key = pkey->pkeys[0].keydata;

    if (key != NULL && keymgmt != NULL)
        return evp_keymgmt_validate(keymgmt, key,
                                    OSSL_KEYMGMT_SELECT_PRIVATE_KEY);
    /* not supported for legacy keys */
    return -2;
}

int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
{
    EVP_PKEY *pkey = ctx->pkey;
    void *key;
    EVP_KEYMGMT *keymgmt;

    if (pkey == NULL) {
        EVPerr(0, EVP_R_NO_KEY_SET);
        return 0;
    }

    keymgmt = pkey->pkeys[0].keymgmt;
    key = pkey->pkeys[0].keydata;

    if (key != NULL && keymgmt != NULL)
        return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_KEYPAIR);
    /* not supported for legacy keys */
    return -2;
}

int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
{
    EVP_PKEY *pkey = ctx->pkey;
    void *key;
    EVP_KEYMGMT *keymgmt;

    if (pkey == NULL) {
        EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
        return 0;
    }

    keymgmt = pkey->pkeys[0].keymgmt;
    key = pkey->pkeys[0].keydata;

    if (key != NULL && keymgmt != NULL)
        return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_ALL);

    /* legacy */
    /* call customized check function first */
    if (ctx->pmeth->check != NULL)
        return ctx->pmeth->check(pkey);

    /* use default check function in ameth */
    if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
        EVPerr(EVP_F_EVP_PKEY_CHECK,
               EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
        return -2;
    }

    return pkey->ameth->pkey_check(pkey);
}