Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/asn1/a_sign.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-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 <time.h>
12
#include <sys/types.h>
13
14
#include "internal/cryptlib.h"
15
16
#include <openssl/bn.h>
17
#include <openssl/evp.h>
18
#include <openssl/x509.h>
19
#include <openssl/objects.h>
20
#include <openssl/buffer.h>
21
#include <openssl/core_names.h>
22
#include "crypto/asn1.h"
23
#include "crypto/evp.h"
24
25
#ifndef OPENSSL_NO_DEPRECATED_3_0
26
27
int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
28
              ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey,
29
              const EVP_MD *type)
30
0
{
31
0
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
32
0
    unsigned char *p, *buf_in = NULL, *buf_out = NULL;
33
0
    int i, inl = 0, outl = 0;
34
0
    size_t inll = 0, outll = 0;
35
0
    X509_ALGOR *a;
36
37
0
    if (ctx == NULL) {
38
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
39
0
        goto err;
40
0
    }
41
0
    for (i = 0; i < 2; i++) {
42
0
        if (i == 0)
43
0
            a = algor1;
44
0
        else
45
0
            a = algor2;
46
0
        if (a == NULL)
47
0
            continue;
48
0
        if (type->pkey_type == NID_dsaWithSHA1) {
49
            /*
50
             * special case: RFC 2459 tells us to omit 'parameters' with
51
             * id-dsa-with-sha1
52
             */
53
0
            ASN1_TYPE_free(a->parameter);
54
0
            a->parameter = NULL;
55
0
        } else if ((a->parameter == NULL) ||
56
0
                   (a->parameter->type != V_ASN1_NULL)) {
57
0
            ASN1_TYPE_free(a->parameter);
58
0
            if ((a->parameter = ASN1_TYPE_new()) == NULL)
59
0
                goto err;
60
0
            a->parameter->type = V_ASN1_NULL;
61
0
        }
62
0
        ASN1_OBJECT_free(a->algorithm);
63
0
        a->algorithm = OBJ_nid2obj(type->pkey_type);
64
0
        if (a->algorithm == NULL) {
65
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_OBJECT_TYPE);
66
0
            goto err;
67
0
        }
68
0
        if (a->algorithm->length == 0) {
69
0
            ERR_raise(ERR_LIB_ASN1,
70
0
                      ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
71
0
            goto err;
72
0
        }
73
0
    }
74
0
    inl = i2d(data, NULL);
75
0
    if (inl <= 0) {
76
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
77
0
        goto err;
78
0
    }
79
0
    inll = (size_t)inl;
80
0
    buf_in = OPENSSL_malloc(inll);
81
0
    outll = outl = EVP_PKEY_get_size(pkey);
82
0
    buf_out = OPENSSL_malloc(outll);
83
0
    if (buf_in == NULL || buf_out == NULL) {
84
0
        outl = 0;
85
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
86
0
        goto err;
87
0
    }
88
0
    p = buf_in;
89
90
0
    i2d(data, &p);
91
0
    if (!EVP_SignInit_ex(ctx, type, NULL)
92
0
        || !EVP_SignUpdate(ctx, (unsigned char *)buf_in, inl)
93
0
        || !EVP_SignFinal(ctx, (unsigned char *)buf_out,
94
0
                          (unsigned int *)&outl, pkey)) {
95
0
        outl = 0;
96
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
97
0
        goto err;
98
0
    }
99
0
    OPENSSL_free(signature->data);
100
0
    signature->data = buf_out;
101
0
    buf_out = NULL;
102
0
    signature->length = outl;
103
    /*
104
     * In the interests of compatibility, I'll make sure that the bit string
105
     * has a 'not-used bits' value of 0
106
     */
107
0
    signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
108
0
    signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
109
0
 err:
110
0
    EVP_MD_CTX_free(ctx);
111
0
    OPENSSL_clear_free((char *)buf_in, inll);
112
0
    OPENSSL_clear_free((char *)buf_out, outll);
113
0
    return outl;
114
0
}
115
116
#endif
117
118
int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
119
                   ASN1_BIT_STRING *signature, const void *data,
120
                   EVP_PKEY *pkey, const EVP_MD *md)
121
0
{
122
0
    return ASN1_item_sign_ex(it, algor1, algor2, signature, data, NULL, pkey,
123
0
                             md, NULL, NULL);
124
0
}
125
126
int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1,
127
                      X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
128
                      const void *data, const ASN1_OCTET_STRING *id,
129
                      EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx,
130
                      const char *propq)
131
0
{
132
0
    int rv = 0;
133
0
    EVP_MD_CTX *ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq);
134
135
0
    if (ctx == NULL) {
136
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
137
0
        return 0;
138
0
    }
139
    /* We can use the non _ex variant here since the pkey is already setup */
140
0
    if (!EVP_DigestSignInit(ctx, NULL, md, NULL, pkey))
141
0
        goto err;
142
143
0
    rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, data, ctx);
144
145
0
 err:
146
0
    EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx));
147
0
    EVP_MD_CTX_free(ctx);
148
0
    return rv;
149
0
}
150
151
int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
152
                       X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
153
                       const void *data, EVP_MD_CTX *ctx)
154
0
{
155
0
    const EVP_MD *md;
156
0
    EVP_PKEY *pkey;
157
0
    unsigned char *buf_in = NULL, *buf_out = NULL;
158
0
    size_t inl = 0, outl = 0, outll = 0;
159
0
    int signid, paramtype, buf_len = 0;
160
0
    int rv, pkey_id;
161
162
0
    md = EVP_MD_CTX_get0_md(ctx);
163
0
    pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx));
164
165
0
    if (pkey == NULL) {
166
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
167
0
        goto err;
168
0
    }
169
170
0
    if (pkey->ameth == NULL) {
171
0
        EVP_PKEY_CTX *pctx = EVP_MD_CTX_get_pkey_ctx(ctx);
172
0
        OSSL_PARAM params[2];
173
0
        unsigned char aid[128];
174
0
        size_t aid_len = 0;
175
176
0
        if (pctx == NULL
177
0
            || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
178
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
179
0
            goto err;
180
0
        }
181
182
0
        params[0] =
183
0
            OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID,
184
0
                                              aid, sizeof(aid));
185
0
        params[1] = OSSL_PARAM_construct_end();
186
187
0
        if (EVP_PKEY_CTX_get_params(pctx, params) <= 0)
188
0
            goto err;
189
190
0
        if ((aid_len = params[0].return_size) == 0) {
191
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
192
0
            goto err;
193
0
        }
194
195
0
        if (algor1 != NULL) {
196
0
            const unsigned char *pp = aid;
197
198
0
            if (d2i_X509_ALGOR(&algor1, &pp, aid_len) == NULL) {
199
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
200
0
                goto err;
201
0
            }
202
0
        }
203
204
0
        if (algor2 != NULL) {
205
0
            const unsigned char *pp = aid;
206
207
0
            if (d2i_X509_ALGOR(&algor2, &pp, aid_len) == NULL) {
208
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
209
0
                goto err;
210
0
            }
211
0
        }
212
213
0
        rv = 3;
214
0
    } else if (pkey->ameth->item_sign) {
215
0
        rv = pkey->ameth->item_sign(ctx, it, data, algor1, algor2, signature);
216
0
        if (rv == 1)
217
0
            outl = signature->length;
218
        /*-
219
         * Return value meanings:
220
         * <=0: error.
221
         *   1: method does everything.
222
         *   2: carry on as normal.
223
         *   3: ASN1 method sets algorithm identifiers: just sign.
224
         */
225
0
        if (rv <= 0)
226
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
227
0
        if (rv <= 1)
228
0
            goto err;
229
0
    } else {
230
0
        rv = 2;
231
0
    }
232
233
0
    if (rv == 2) {
234
0
        if (md == NULL) {
235
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
236
0
            goto err;
237
0
        }
238
239
0
        pkey_id =
240
0
#ifndef OPENSSL_NO_SM2
241
0
            EVP_PKEY_get_id(pkey) == NID_sm2 ? NID_sm2 :
242
0
#endif
243
0
            pkey->ameth->pkey_id;
244
245
0
        if (!OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(md), pkey_id)) {
246
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
247
0
            goto err;
248
0
        }
249
250
0
        if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL)
251
0
            paramtype = V_ASN1_NULL;
252
0
        else
253
0
            paramtype = V_ASN1_UNDEF;
254
255
0
        if (algor1)
256
0
            X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL);
257
0
        if (algor2)
258
0
            X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL);
259
260
0
    }
261
262
0
    buf_len = ASN1_item_i2d(data, &buf_in, it);
263
0
    if (buf_len <= 0) {
264
0
        outl = 0;
265
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
266
0
        goto err;
267
0
    }
268
0
    inl = buf_len;
269
0
    if (!EVP_DigestSign(ctx, NULL, &outll, buf_in, inl)) {
270
0
        outl = 0;
271
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
272
0
        goto err;
273
0
    }
274
0
    outl = outll;
275
0
    buf_out = OPENSSL_malloc(outll);
276
0
    if (buf_in == NULL || buf_out == NULL) {
277
0
        outl = 0;
278
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
279
0
        goto err;
280
0
    }
281
282
0
    if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) {
283
0
        outl = 0;
284
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
285
0
        goto err;
286
0
    }
287
0
    OPENSSL_free(signature->data);
288
0
    signature->data = buf_out;
289
0
    buf_out = NULL;
290
0
    signature->length = outl;
291
    /*
292
     * In the interests of compatibility, I'll make sure that the bit string
293
     * has a 'not-used bits' value of 0
294
     */
295
0
    signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
296
0
    signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
297
0
 err:
298
0
    OPENSSL_clear_free((char *)buf_in, inl);
299
0
    OPENSSL_clear_free((char *)buf_out, outll);
300
0
    return outl;
301
0
}