Coverage Report

Created: 2026-01-09 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/asn1/x_bignum.c
Line
Count
Source
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
0
#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
0
ASN1_ITEM_start(BIGNUM)
58
0
    ASN1_ITYPE_PRIMITIVE,
59
0
    V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM" ASN1_ITEM_end(BIGNUM)
60
61
0
                                                ASN1_ITEM_start(CBIGNUM) ASN1_ITYPE_PRIMITIVE,
62
0
    V_ASN1_INTEGER, NULL, 0, &cbignum_pf, BN_SENSITIVE, "CBIGNUM" ASN1_ITEM_end(CBIGNUM)
63
64
                                                            static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
65
0
{
66
0
    *pval = (ASN1_VALUE *)BN_new();
67
0
    if (*pval != NULL)
68
0
        return 1;
69
0
    else
70
0
        return 0;
71
0
}
72
73
static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
74
0
{
75
0
    *pval = (ASN1_VALUE *)BN_secure_new();
76
0
    if (*pval != NULL)
77
0
        return 1;
78
0
    else
79
0
        return 0;
80
0
}
81
82
static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
83
0
{
84
0
    if (*pval == NULL)
85
0
        return;
86
0
    if (it->size & BN_SENSITIVE)
87
0
        BN_clear_free((BIGNUM *)*pval);
88
0
    else
89
0
        BN_free((BIGNUM *)*pval);
90
0
    *pval = NULL;
91
0
}
92
93
static int bn_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
94
    const ASN1_ITEM *it)
95
0
{
96
0
    BIGNUM *bn;
97
0
    int pad;
98
0
    if (*pval == NULL)
99
0
        return -1;
100
0
    bn = (BIGNUM *)*pval;
101
    /* If MSB set in an octet we need a padding byte */
102
0
    if (BN_num_bits(bn) & 0x7)
103
0
        pad = 0;
104
0
    else
105
0
        pad = 1;
106
0
    if (cont) {
107
0
        if (pad)
108
0
            *cont++ = 0;
109
0
        BN_bn2bin(bn, cont);
110
0
    }
111
0
    return pad + BN_num_bytes(bn);
112
0
}
113
114
static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
115
    int utype, char *free_cont, const ASN1_ITEM *it)
116
0
{
117
0
    int allocated = 0;
118
0
    BIGNUM *bn;
119
120
    /* Reject encodings that imply a negative number. */
121
0
    if (len == 0 || (*cont & 0x80) != 0) {
122
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_VALUE);
123
0
        return 0;
124
0
    }
125
126
0
    if (*pval == NULL && (allocated = bn_new(pval, it)) == 0)
127
0
        return 0;
128
0
    bn = (BIGNUM *)*pval;
129
0
    if (!BN_bin2bn(cont, len, bn)) {
130
0
        if (allocated != 0)
131
0
            bn_free(pval, it);
132
0
        return 0;
133
0
    }
134
0
    return 1;
135
0
}
136
137
static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
138
    int utype, char *free_cont, const ASN1_ITEM *it)
139
0
{
140
0
    int ret, allocated = 0;
141
0
    BIGNUM *bn;
142
143
0
    if (*pval == NULL && (allocated = bn_secure_new(pval, it)) == 0)
144
0
        return 0;
145
146
0
    ret = bn_c2i(pval, cont, len, utype, free_cont, it);
147
0
    if (!ret) {
148
0
        if (allocated != 0)
149
0
            bn_free(pval, it);
150
151
0
        return 0;
152
0
    }
153
154
    /* Set constant-time flag for all secure BIGNUMS */
155
0
    bn = (BIGNUM *)*pval;
156
0
    BN_set_flags(bn, BN_FLG_CONSTTIME);
157
0
    return ret;
158
0
}
159
160
static int bn_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
161
    int indent, const ASN1_PCTX *pctx)
162
0
{
163
0
    if (!BN_print(out, *(BIGNUM **)pval))
164
0
        return 0;
165
0
    if (BIO_puts(out, "\n") <= 0)
166
0
        return 0;
167
0
    return 1;
168
0
}