Coverage Report

Created: 2026-11-16 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/rsa/rsa_asn1.c
Line
Count
Source
1
/*
2
 * Copyright 2000-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
/*
11
 * RSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/bn.h>
19
#include <openssl/x509.h>
20
#include <openssl/asn1t.h>
21
#include "rsa_local.h"
22
23
/*
24
 * Override the default free and new methods,
25
 * and calculate helper products for multi-prime
26
 * RSA keys.
27
 */
28
static int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
29
    void *exarg)
30
177k
{
31
177k
    if (operation == ASN1_OP_NEW_PRE) {
32
53.4k
        *pval = (ASN1_VALUE *)RSA_new();
33
53.4k
        if (*pval != NULL)
34
53.4k
            return 2;
35
0
        return 0;
36
123k
    } else if (operation == ASN1_OP_FREE_PRE) {
37
11.1k
        RSA_free((RSA *)*pval);
38
11.1k
        *pval = NULL;
39
11.1k
        return 2;
40
112k
    } else if (operation == ASN1_OP_D2I_POST) {
41
42.2k
        if (((RSA *)*pval)->version != RSA_ASN1_VERSION_MULTI) {
42
            /* not a multi-prime key, skip */
43
42.2k
            return 1;
44
42.2k
        }
45
        /*
46
         * A product is kept for each extra prime, so refuse more of them
47
         * than the rest of the RSA code supports before computing any.
48
         */
49
0
        if (sk_RSA_PRIME_INFO_num(((RSA *)*pval)->prime_infos)
50
0
            > RSA_MAX_PRIME_NUM - 2) {
51
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MULTI_PRIME_KEY);
52
0
            return 0;
53
0
        }
54
0
        return (ossl_rsa_multip_calc_product((RSA *)*pval) == 1) ? 2 : 0;
55
0
    }
56
70.3k
    return 1;
57
177k
}
58
59
/* Based on definitions in RFC 8017 appendix A.1.2 */
60
ASN1_SEQUENCE(RSA_PRIME_INFO) = {
61
    ASN1_SIMPLE(RSA_PRIME_INFO, r, CBIGNUM),
62
    ASN1_SIMPLE(RSA_PRIME_INFO, d, CBIGNUM),
63
    ASN1_SIMPLE(RSA_PRIME_INFO, t, CBIGNUM),
64
0
} ASN1_SEQUENCE_END(RSA_PRIME_INFO)
65
0
66
0
ASN1_SEQUENCE_cb(RSAPrivateKey, rsa_cb) = {
67
0
    ASN1_EMBED(RSA, version, INT32),
68
0
    ASN1_SIMPLE(RSA, n, BIGNUM),
69
0
    ASN1_SIMPLE(RSA, e, BIGNUM),
70
0
    ASN1_SIMPLE(RSA, d, CBIGNUM),
71
0
    ASN1_SIMPLE(RSA, p, CBIGNUM),
72
0
    ASN1_SIMPLE(RSA, q, CBIGNUM),
73
0
    ASN1_SIMPLE(RSA, dmp1, CBIGNUM),
74
0
    ASN1_SIMPLE(RSA, dmq1, CBIGNUM),
75
0
    ASN1_SIMPLE(RSA, iqmp, CBIGNUM),
76
0
    ASN1_SEQUENCE_OF_OPT(RSA, prime_infos, RSA_PRIME_INFO)
77
0
} ASN1_SEQUENCE_END_cb(RSA, RSAPrivateKey)
78
0
79
0
ASN1_SEQUENCE_cb(RSAPublicKey, rsa_cb) = {
80
0
    ASN1_SIMPLE(RSA, n, BIGNUM),
81
0
    ASN1_SIMPLE(RSA, e, BIGNUM),
82
0
} ASN1_SEQUENCE_END_cb(RSA, RSAPublicKey)
83
0
84
0
/* Free up maskHash */
85
0
static int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
86
0
    void *exarg)
87
0
{
88
0
    if (operation == ASN1_OP_FREE_PRE) {
89
0
        RSA_PSS_PARAMS *pss = (RSA_PSS_PARAMS *)*pval;
90
0
        X509_ALGOR_free(pss->maskHash);
91
0
    }
92
0
    return 1;
93
0
}
94
95
ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = {
96
    ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR, 0),
97
    ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR, 1),
98
    ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER, 2),
99
    ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER, 3)
100
} ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS)
101
102
IMPLEMENT_ASN1_FUNCTIONS(RSA_PSS_PARAMS)
103
IMPLEMENT_ASN1_DUP_FUNCTION(RSA_PSS_PARAMS)
104
105
/* Free up maskHash */
106
static int rsa_oaep_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
107
    void *exarg)
108
0
{
109
0
    if (operation == ASN1_OP_FREE_PRE) {
110
0
        RSA_OAEP_PARAMS *oaep = (RSA_OAEP_PARAMS *)*pval;
111
0
        X509_ALGOR_free(oaep->maskHash);
112
0
    }
113
0
    return 1;
114
0
}
115
116
ASN1_SEQUENCE_cb(RSA_OAEP_PARAMS, rsa_oaep_cb) = {
117
    ASN1_EXP_OPT(RSA_OAEP_PARAMS, hashFunc, X509_ALGOR, 0),
118
    ASN1_EXP_OPT(RSA_OAEP_PARAMS, maskGenFunc, X509_ALGOR, 1),
119
    ASN1_EXP_OPT(RSA_OAEP_PARAMS, pSourceFunc, X509_ALGOR, 2),
120
} ASN1_SEQUENCE_END_cb(RSA_OAEP_PARAMS, RSA_OAEP_PARAMS)
121
122
IMPLEMENT_ASN1_FUNCTIONS(RSA_OAEP_PARAMS)
123
124
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(RSA, RSAPrivateKey, RSAPrivateKey)
125
126
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(RSA, RSAPublicKey, RSAPublicKey)
127
128
RSA *RSAPublicKey_dup(const RSA *rsa)
129
0
{
130
0
    return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa);
131
0
}
132
133
RSA *RSAPrivateKey_dup(const RSA *rsa)
134
0
{
135
0
    return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa);
136
0
}