Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/asn1/t_pkey.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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/objects.h>
13
#include <openssl/buffer.h>
14
#include "crypto/bn.h"
15
16
/* Number of octets per line */
17
3.16M
#define ASN1_BUF_PRINT_WIDTH 15
18
/* Maximum indent */
19
351k
#define ASN1_PRINT_MAX_INDENT 128
20
21
int ASN1_buf_print(BIO *bp, const unsigned char *buf, size_t buflen, int indent)
22
15.7k
{
23
15.7k
    size_t i;
24
25
3.17M
    for (i = 0; i < buflen; i++) {
26
3.16M
        if ((i % ASN1_BUF_PRINT_WIDTH) == 0) {
27
216k
            if (i > 0 && BIO_puts(bp, "\n") <= 0)
28
0
                return 0;
29
216k
            if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT))
30
0
                return 0;
31
216k
        }
32
        /*
33
         * Use colon separators for each octet for compatibility as
34
         * this function is used to print out key components.
35
         */
36
3.16M
        if (BIO_printf(bp, "%02x%s", buf[i],
37
3.16M
                (i == buflen - 1) ? "" : ":")
38
3.16M
            <= 0)
39
0
            return 0;
40
3.16M
    }
41
15.7k
    if (BIO_write(bp, "\n", 1) <= 0)
42
0
        return 0;
43
15.7k
    return 1;
44
15.7k
}
45
46
int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num,
47
    unsigned char *ign, int indent)
48
134k
{
49
134k
    int n, rv = 0;
50
134k
    const char *neg;
51
134k
    unsigned char *buf = NULL, *tmp = NULL;
52
134k
    int buflen;
53
54
134k
    if (num == NULL)
55
0
        return 1;
56
134k
    neg = BN_is_negative(num) ? "-" : "";
57
134k
    if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT))
58
0
        return 0;
59
134k
    if (BN_is_zero(num)) {
60
35.0k
        if (BIO_printf(bp, "%s 0\n", number) <= 0)
61
0
            return 0;
62
35.0k
        return 1;
63
35.0k
    }
64
65
99.7k
    if (BN_num_bytes(num) <= BN_BYTES) {
66
91.5k
        if (BIO_printf(bp, "%s %s%lu (%s0x%lx)\n", number, neg,
67
91.5k
                (unsigned long)bn_get_words(num)[0], neg,
68
91.5k
                (unsigned long)bn_get_words(num)[0])
69
91.5k
            <= 0)
70
0
            return 0;
71
91.5k
        return 1;
72
91.5k
    }
73
74
8.25k
    buflen = BN_num_bytes(num) + 1;
75
8.25k
    buf = tmp = OPENSSL_malloc(buflen);
76
8.25k
    if (buf == NULL)
77
0
        goto err;
78
8.25k
    buf[0] = 0;
79
8.25k
    if (BIO_printf(bp, "%s%s\n", number,
80
8.25k
            (neg[0] == '-') ? " (Negative)" : "")
81
8.25k
        <= 0)
82
0
        goto err;
83
8.25k
    n = BN_bn2bin(num, buf + 1);
84
85
8.25k
    if (buf[1] & 0x80)
86
4.45k
        n++;
87
3.80k
    else
88
3.80k
        tmp++;
89
90
8.25k
    if (ASN1_buf_print(bp, tmp, n, indent + 4) == 0)
91
0
        goto err;
92
8.25k
    rv = 1;
93
8.25k
err:
94
8.25k
    OPENSSL_clear_free(buf, buflen);
95
8.25k
    return rv;
96
8.25k
}