Coverage Report

Created: 2025-06-13 06:58

/src/openssl/crypto/asn1/t_pkey.c
Line
Count
Source (jump to first uncovered line)
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
1.37M
#define ASN1_BUF_PRINT_WIDTH    15
18
/* Maximum indent */
19
191k
#define ASN1_PRINT_MAX_INDENT 128
20
21
int ASN1_buf_print(BIO *bp, const unsigned char *buf, size_t buflen, int indent)
22
10.1k
{
23
10.1k
    size_t i;
24
25
1.38M
    for (i = 0; i < buflen; i++) {
26
1.37M
        if ((i % ASN1_BUF_PRINT_WIDTH) == 0) {
27
95.5k
            if (i > 0 && BIO_puts(bp, "\n") <= 0)
28
0
                return 0;
29
95.5k
            if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT))
30
0
                return 0;
31
95.5k
        }
32
        /*
33
         * Use colon separators for each octet for compatibility as
34
         * this function is used to print out key components.
35
         */
36
1.37M
        if (BIO_printf(bp, "%02x%s", buf[i],
37
1.37M
                       (i == buflen - 1) ? "" : ":") <= 0)
38
0
                return 0;
39
1.37M
    }
40
10.1k
    if (BIO_write(bp, "\n", 1) <= 0)
41
0
        return 0;
42
10.1k
    return 1;
43
10.1k
}
44
45
int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num,
46
                  unsigned char *ign, int indent)
47
95.9k
{
48
95.9k
    int n, rv = 0;
49
95.9k
    const char *neg;
50
95.9k
    unsigned char *buf = NULL, *tmp = NULL;
51
95.9k
    int buflen;
52
53
95.9k
    if (num == NULL)
54
0
        return 1;
55
95.9k
    neg = BN_is_negative(num) ? "-" : "";
56
95.9k
    if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT))
57
0
        return 0;
58
95.9k
    if (BN_is_zero(num)) {
59
41.9k
        if (BIO_printf(bp, "%s 0\n", number) <= 0)
60
0
            return 0;
61
41.9k
        return 1;
62
41.9k
    }
63
64
54.0k
    if (BN_num_bytes(num) <= BN_BYTES) {
65
48.1k
        if (BIO_printf(bp, "%s %s%lu (%s0x%lx)\n", number, neg,
66
48.1k
                       (unsigned long)bn_get_words(num)[0], neg,
67
48.1k
                       (unsigned long)bn_get_words(num)[0]) <= 0)
68
0
            return 0;
69
48.1k
        return 1;
70
48.1k
    }
71
72
5.83k
    buflen = BN_num_bytes(num) + 1;
73
5.83k
    buf = tmp = OPENSSL_malloc(buflen);
74
5.83k
    if (buf == NULL)
75
0
        goto err;
76
5.83k
    buf[0] = 0;
77
5.83k
    if (BIO_printf(bp, "%s%s\n", number,
78
5.83k
                   (neg[0] == '-') ? " (Negative)" : "") <= 0)
79
0
        goto err;
80
5.83k
    n = BN_bn2bin(num, buf + 1);
81
82
5.83k
    if (buf[1] & 0x80)
83
3.02k
        n++;
84
2.81k
    else
85
2.81k
        tmp++;
86
87
5.83k
    if (ASN1_buf_print(bp, tmp, n, indent + 4) == 0)
88
0
        goto err;
89
5.83k
    rv = 1;
90
5.83k
    err:
91
5.83k
    OPENSSL_clear_free(buf, buflen);
92
5.83k
    return rv;
93
5.83k
}