Coverage Report

Created: 2025-06-13 06:55

/src/openssl/crypto/asn1/f_string.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_STRING(BIO *bp, const ASN1_STRING *a, int type)
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->length == 0) {
25
0
        if (BIO_write(bp, "0", 1) != 1)
26
0
            goto err;
27
0
        n = 1;
28
0
    } else {
29
0
        for (i = 0; i < a->length; i++) {
30
0
            if ((i != 0) && (i % 35 == 0)) {
31
0
                if (BIO_write(bp, "\\\n", 2) != 2)
32
0
                    goto err;
33
0
                n += 2;
34
0
            }
35
0
            ossl_to_hex(buf, a->data[i]);
36
0
            if (BIO_write(bp, buf, 2) != 2)
37
0
                goto err;
38
0
            n += 2;
39
0
        }
40
0
    }
41
0
    return n;
42
0
 err:
43
0
    return -1;
44
0
}
45
46
int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
47
0
{
48
0
    int i, j, k, m, n, again, bufsize;
49
0
    unsigned char *s = NULL, *sp;
50
0
    unsigned char *bufp;
51
0
    int num = 0, slen = 0, first = 1;
52
53
0
    bufsize = BIO_gets(bp, buf, size);
54
0
    for (;;) {
55
0
        if (bufsize < 1) {
56
0
            if (first)
57
0
                break;
58
0
            else
59
0
                goto err;
60
0
        }
61
0
        first = 0;
62
63
0
        i = bufsize;
64
0
        if (buf[i - 1] == '\n')
65
0
            buf[--i] = '\0';
66
0
        if (i == 0)
67
0
            goto err;
68
0
        if (buf[i - 1] == '\r')
69
0
            buf[--i] = '\0';
70
0
        if (i == 0)
71
0
            goto err;
72
0
        again = (buf[i - 1] == '\\');
73
74
0
        for (j = i - 1; j > 0; j--) {
75
0
            if (!ossl_isxdigit(buf[j])) {
76
0
                i = j;
77
0
                break;
78
0
            }
79
0
        }
80
0
        buf[i] = '\0';
81
        /*
82
         * We have now cleared all the crap off the end of the line
83
         */
84
0
        if (i < 2)
85
0
            goto err;
86
87
0
        bufp = (unsigned char *)buf;
88
89
0
        k = 0;
90
0
        i -= again;
91
0
        if (i % 2 != 0) {
92
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ODD_NUMBER_OF_CHARS);
93
0
            OPENSSL_free(s);
94
0
            return 0;
95
0
        }
96
0
        i /= 2;
97
0
        if (num + i > slen) {
98
0
            sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
99
0
            if (sp == NULL) {
100
0
                OPENSSL_free(s);
101
0
                return 0;
102
0
            }
103
0
            s = sp;
104
0
            slen = num + i * 2;
105
0
        }
106
0
        for (j = 0; j < i; j++, k += 2) {
107
0
            for (n = 0; n < 2; n++) {
108
0
                m = OPENSSL_hexchar2int(bufp[k + n]);
109
0
                if (m < 0) {
110
0
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_NON_HEX_CHARACTERS);
111
0
                    OPENSSL_free(s);
112
0
                    return 0;
113
0
                }
114
0
                s[num + j] <<= 4;
115
0
                s[num + j] |= m;
116
0
            }
117
0
        }
118
0
        num += i;
119
0
        if (again)
120
0
            bufsize = BIO_gets(bp, buf, size);
121
0
        else
122
0
            break;
123
0
    }
124
0
    bs->length = num;
125
0
    bs->data = s;
126
0
    return 1;
127
128
0
 err:
129
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_SHORT_LINE);
130
0
    OPENSSL_free(s);
131
0
    return 0;
132
0
}