Coverage Report

Created: 2025-06-13 06:56

/src/openssl/crypto/asn1/f_int.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2024 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 "crypto/ctype.h"
12
#include "internal/cryptlib.h"
13
#include <openssl/buffer.h>
14
#include <openssl/asn1.h>
15
16
int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
17
0
{
18
0
    int i, n = 0;
19
0
    char buf[2];
20
21
0
    if (a == NULL)
22
0
        return 0;
23
24
0
    if (a->type & V_ASN1_NEG) {
25
0
        if (BIO_write(bp, "-", 1) != 1)
26
0
            goto err;
27
0
        n = 1;
28
0
    }
29
30
0
    if (a->length == 0) {
31
0
        if (BIO_write(bp, "00", 2) != 2)
32
0
            goto err;
33
0
        n += 2;
34
0
    } else {
35
0
        for (i = 0; i < a->length; i++) {
36
0
            if ((i != 0) && (i % 35 == 0)) {
37
0
                if (BIO_write(bp, "\\\n", 2) != 2)
38
0
                    goto err;
39
0
                n += 2;
40
0
            }
41
0
            ossl_to_hex(buf, a->data[i]);
42
0
            if (BIO_write(bp, buf, 2) != 2)
43
0
                goto err;
44
0
            n += 2;
45
0
        }
46
0
    }
47
0
    return n;
48
0
 err:
49
0
    return -1;
50
0
}
51
52
int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
53
0
{
54
0
    int i, j, k, m, n, again, bufsize;
55
0
    unsigned char *s = NULL, *sp;
56
0
    unsigned char *bufp;
57
0
    int num = 0, slen = 0, first = 1;
58
59
0
    bs->type = V_ASN1_INTEGER;
60
61
0
    bufsize = BIO_gets(bp, buf, size);
62
0
    for (;;) {
63
0
        if (bufsize < 1)
64
0
            goto err;
65
0
        i = bufsize;
66
0
        if (buf[i - 1] == '\n')
67
0
            buf[--i] = '\0';
68
0
        if (i == 0)
69
0
            goto err;
70
0
        if (buf[i - 1] == '\r')
71
0
            buf[--i] = '\0';
72
0
        if (i == 0)
73
0
            goto err;
74
0
        again = (buf[i - 1] == '\\');
75
76
0
        for (j = 0; j < i; j++) {
77
0
            if (!ossl_isxdigit(buf[j])) {
78
0
                i = j;
79
0
                break;
80
0
            }
81
0
        }
82
0
        buf[i] = '\0';
83
        /*
84
         * We have now cleared all the crap off the end of the line
85
         */
86
0
        if (i < 2)
87
0
            goto err;
88
89
0
        bufp = (unsigned char *)buf;
90
0
        if (first) {
91
0
            first = 0;
92
0
            if ((bufp[0] == '0') && (bufp[1] == '0')) {
93
0
                bufp += 2;
94
0
                i -= 2;
95
0
            }
96
0
        }
97
0
        k = 0;
98
0
        i -= again;
99
0
        if (i % 2 != 0) {
100
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ODD_NUMBER_OF_CHARS);
101
0
            OPENSSL_free(s);
102
0
            return 0;
103
0
        }
104
0
        i /= 2;
105
0
        if (num + i > slen) {
106
0
            sp = OPENSSL_clear_realloc(s, slen, num + i * 2);
107
0
            if (sp == NULL) {
108
0
                OPENSSL_free(s);
109
0
                return 0;
110
0
            }
111
0
            s = sp;
112
0
            slen = num + i * 2;
113
0
        }
114
0
        for (j = 0; j < i; j++, k += 2) {
115
0
            for (n = 0; n < 2; n++) {
116
0
                m = OPENSSL_hexchar2int(bufp[k + n]);
117
0
                if (m < 0) {
118
0
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_NON_HEX_CHARACTERS);
119
0
                    goto err;
120
0
                }
121
0
                s[num + j] <<= 4;
122
0
                s[num + j] |= m;
123
0
            }
124
0
        }
125
0
        num += i;
126
0
        if (again)
127
0
            bufsize = BIO_gets(bp, buf, size);
128
0
        else
129
0
            break;
130
0
    }
131
0
    bs->length = num;
132
0
    bs->data = s;
133
0
    return 1;
134
0
 err:
135
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_SHORT_LINE);
136
0
    OPENSSL_free(s);
137
0
    return 0;
138
0
}
139
140
int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a)
141
0
{
142
0
    return i2a_ASN1_INTEGER(bp, a);
143
0
}
144
145
int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
146
0
{
147
0
    int rv = a2i_ASN1_INTEGER(bp, bs, buf, size);
148
0
    if (rv == 1)
149
0
        bs->type = V_ASN1_INTEGER | (bs->type & V_ASN1_NEG);
150
0
    return rv;
151
0
}