Coverage Report

Created: 2025-06-13 06:58

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