Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/asn1/a_print.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 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/asn1.h>
14
15
int ASN1_PRINTABLE_type(const unsigned char *s, int len)
16
0
{
17
0
    int c;
18
0
    int ia5 = 0;
19
0
    int t61 = 0;
20
21
0
    if (s == NULL)
22
0
        return V_ASN1_PRINTABLESTRING;
23
24
0
    if (len < 0)
25
0
        len = strlen((const char *)s);
26
27
0
    while (len-- > 0) {
28
0
        c = *(s++);
29
0
        if (!ossl_isasn1print(c))
30
0
            ia5 = 1;
31
0
        if (!ossl_isascii(c))
32
0
            t61 = 1;
33
0
    }
34
0
    if (t61)
35
0
        return V_ASN1_T61STRING;
36
0
    if (ia5)
37
0
        return V_ASN1_IA5STRING;
38
0
    return V_ASN1_PRINTABLESTRING;
39
0
}
40
41
int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s)
42
0
{
43
0
    int i;
44
0
    unsigned char *p;
45
46
0
    if (s->type != V_ASN1_UNIVERSALSTRING)
47
0
        return 0;
48
0
    if ((s->length % 4) != 0)
49
0
        return 0;
50
0
    p = s->data;
51
0
    for (i = 0; i < s->length; i += 4) {
52
0
        if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
53
0
            break;
54
0
        else
55
0
            p += 4;
56
0
    }
57
0
    if (i < s->length)
58
0
        return 0;
59
0
    p = s->data;
60
0
    for (i = 3; i < s->length; i += 4) {
61
0
        *(p++) = s->data[i];
62
0
    }
63
0
    *(p) = '\0';
64
0
    s->length /= 4;
65
0
    s->type = ASN1_PRINTABLE_type(s->data, s->length);
66
0
    return 1;
67
0
}
68
69
int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
70
747k
{
71
747k
    int i, n;
72
747k
    char buf[80];
73
747k
    const char *p;
74
75
747k
    if (v == NULL)
76
0
        return 0;
77
747k
    n = 0;
78
747k
    p = (const char *)v->data;
79
35.6M
    for (i = 0; i < v->length; i++) {
80
34.8M
        if ((p[i] > '~') || ((p[i] < ' ') &&
81
34.8M
                             (p[i] != '\n') && (p[i] != '\r')))
82
23.4M
            buf[n] = '.';
83
11.3M
        else
84
11.3M
            buf[n] = p[i];
85
34.8M
        n++;
86
34.8M
        if (n >= 80) {
87
251k
            if (BIO_write(bp, buf, n) <= 0)
88
0
                return 0;
89
251k
            n = 0;
90
251k
        }
91
34.8M
    }
92
747k
    if (n > 0)
93
671k
        if (BIO_write(bp, buf, n) <= 0)
94
0
            return 0;
95
747k
    return 1;
96
747k
}