Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/asn1/x_bignum.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2016 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 "internal/cryptlib.h"
12
#include <openssl/asn1t.h>
13
#include <openssl/bn.h>
14
15
/*
16
 * Custom primitive type for BIGNUM handling. This reads in an ASN1_INTEGER
17
 * as a BIGNUM directly. Currently it ignores the sign which isn't a problem
18
 * since all BIGNUMs used are non negative and anything that looks negative
19
 * is normally due to an encoding error.
20
 */
21
22
4.69M
#define BN_SENSITIVE    1
23
24
static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
25
static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
26
static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
27
28
static int bn_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
29
                  const ASN1_ITEM *it);
30
static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
31
                  int utype, char *free_cont, const ASN1_ITEM *it);
32
static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
33
                         int utype, char *free_cont, const ASN1_ITEM *it);
34
static int bn_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
35
                    int indent, const ASN1_PCTX *pctx);
36
37
static ASN1_PRIMITIVE_FUNCS bignum_pf = {
38
    NULL, 0,
39
    bn_new,
40
    bn_free,
41
    0,
42
    bn_c2i,
43
    bn_i2c,
44
    bn_print
45
};
46
47
static ASN1_PRIMITIVE_FUNCS cbignum_pf = {
48
    NULL, 0,
49
    bn_secure_new,
50
    bn_free,
51
    0,
52
    bn_secure_c2i,
53
    bn_i2c,
54
    bn_print
55
};
56
57
2.32M
ASN1_ITEM_start(BIGNUM)
58
2.32M
        ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM"
59
2.32M
ASN1_ITEM_end(BIGNUM)
60
61
4.46M
ASN1_ITEM_start(CBIGNUM)
62
4.46M
        ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &cbignum_pf, BN_SENSITIVE, "CBIGNUM"
63
4.46M
ASN1_ITEM_end(CBIGNUM)
64
65
static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
66
1.10M
{
67
1.10M
    *pval = (ASN1_VALUE *)BN_new();
68
1.10M
    if (*pval != NULL)
69
1.10M
        return 1;
70
0
    else
71
0
        return 0;
72
1.10M
}
73
74
static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
75
1.01M
{
76
1.01M
    *pval = (ASN1_VALUE *)BN_secure_new();
77
1.01M
    if (*pval != NULL)
78
1.01M
        return 1;
79
0
    else
80
0
        return 0;
81
1.01M
}
82
83
static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
84
419k
{
85
419k
    if (*pval == NULL)
86
189k
        return;
87
230k
    if (it->size & BN_SENSITIVE)
88
4.10k
        BN_clear_free((BIGNUM *)*pval);
89
226k
    else
90
226k
        BN_free((BIGNUM *)*pval);
91
230k
    *pval = NULL;
92
230k
}
93
94
static int bn_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
95
                  const ASN1_ITEM *it)
96
3.05M
{
97
3.05M
    BIGNUM *bn;
98
3.05M
    int pad;
99
3.05M
    if (*pval == NULL)
100
558
        return -1;
101
3.05M
    bn = (BIGNUM *)*pval;
102
    /* If MSB set in an octet we need a padding byte */
103
3.05M
    if (BN_num_bits(bn) & 0x7)
104
1.21M
        pad = 0;
105
1.84M
    else
106
1.84M
        pad = 1;
107
3.05M
    if (cont) {
108
525k
        if (pad)
109
312k
            *cont++ = 0;
110
525k
        BN_bn2bin(bn, cont);
111
525k
    }
112
3.05M
    return pad + BN_num_bytes(bn);
113
3.05M
}
114
115
static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
116
                  int utype, char *free_cont, const ASN1_ITEM *it)
117
1.97M
{
118
1.97M
    BIGNUM *bn;
119
120
1.97M
    if (*pval == NULL && !bn_new(pval, it))
121
0
        return 0;
122
1.97M
    bn = (BIGNUM *)*pval;
123
1.97M
    if (!BN_bin2bn(cont, len, bn)) {
124
0
        bn_free(pval, it);
125
0
        return 0;
126
0
    }
127
1.97M
    return 1;
128
1.97M
}
129
130
static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
131
                         int utype, char *free_cont, const ASN1_ITEM *it)
132
1.00M
{
133
1.00M
    int ret;
134
1.00M
    BIGNUM *bn;
135
136
1.00M
    if (*pval == NULL && !bn_secure_new(pval, it))
137
0
        return 0;
138
139
1.00M
    ret = bn_c2i(pval, cont, len, utype, free_cont, it);
140
1.00M
    if (!ret)
141
0
        return 0;
142
143
    /* Set constant-time flag for all secure BIGNUMS */
144
1.00M
    bn = (BIGNUM *)*pval;
145
1.00M
    BN_set_flags(bn, BN_FLG_CONSTTIME);
146
1.00M
    return ret;
147
1.00M
}
148
149
static int bn_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
150
                    int indent, const ASN1_PCTX *pctx)
151
85.3k
{
152
85.3k
    if (!BN_print(out, *(BIGNUM **)pval))
153
0
        return 0;
154
85.3k
    if (BIO_puts(out, "\n") <= 0)
155
0
        return 0;
156
85.3k
    return 1;
157
85.3k
}