/src/openssl36/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 | 2.45M | #define ASN1_BUF_PRINT_WIDTH 15 |
18 | | /* Maximum indent */ |
19 | 253k | #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.6k | { |
23 | 15.6k | size_t i; |
24 | | |
25 | 2.47M | for (i = 0; i < buflen; i++) { |
26 | 2.45M | if ((i % ASN1_BUF_PRINT_WIDTH) == 0) { |
27 | 169k | if (i > 0 && BIO_puts(bp, "\n") <= 0) |
28 | 0 | return 0; |
29 | 169k | if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT)) |
30 | 0 | return 0; |
31 | 169k | } |
32 | | /* |
33 | | * Use colon separators for each octet for compatibility as |
34 | | * this function is used to print out key components. |
35 | | */ |
36 | 2.45M | if (BIO_printf(bp, "%02x%s", buf[i], |
37 | 2.45M | (i == buflen - 1) ? "" : ":") |
38 | 2.45M | <= 0) |
39 | 0 | return 0; |
40 | 2.45M | } |
41 | 15.6k | if (BIO_write(bp, "\n", 1) <= 0) |
42 | 0 | return 0; |
43 | 15.6k | return 1; |
44 | 15.6k | } |
45 | | |
46 | | int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num, |
47 | | unsigned char *ign, int indent) |
48 | 84.0k | { |
49 | 84.0k | int n, rv = 0; |
50 | 84.0k | const char *neg; |
51 | 84.0k | unsigned char *buf = NULL, *tmp = NULL; |
52 | 84.0k | int buflen; |
53 | | |
54 | 84.0k | if (num == NULL) |
55 | 0 | return 1; |
56 | 84.0k | neg = BN_is_negative(num) ? "-" : ""; |
57 | 84.0k | if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT)) |
58 | 0 | return 0; |
59 | 84.0k | if (BN_is_zero(num)) { |
60 | 10.7k | if (BIO_printf(bp, "%s 0\n", number) <= 0) |
61 | 0 | return 0; |
62 | 10.7k | return 1; |
63 | 10.7k | } |
64 | | |
65 | 73.3k | if (BN_num_bytes(num) <= BN_BYTES) { |
66 | 66.0k | if (BIO_printf(bp, "%s %s%lu (%s0x%lx)\n", number, neg, |
67 | 66.0k | (unsigned long)bn_get_words(num)[0], neg, |
68 | 66.0k | (unsigned long)bn_get_words(num)[0]) |
69 | 66.0k | <= 0) |
70 | 0 | return 0; |
71 | 66.0k | return 1; |
72 | 66.0k | } |
73 | | |
74 | 7.31k | buflen = BN_num_bytes(num) + 1; |
75 | 7.31k | buf = tmp = OPENSSL_malloc(buflen); |
76 | 7.31k | if (buf == NULL) |
77 | 0 | goto err; |
78 | 7.31k | buf[0] = 0; |
79 | 7.31k | if (BIO_printf(bp, "%s%s\n", number, |
80 | 7.31k | (neg[0] == '-') ? " (Negative)" : "") |
81 | 7.31k | <= 0) |
82 | 0 | goto err; |
83 | 7.31k | n = BN_bn2bin(num, buf + 1); |
84 | | |
85 | 7.31k | if (buf[1] & 0x80) |
86 | 4.00k | n++; |
87 | 3.30k | else |
88 | 3.30k | tmp++; |
89 | | |
90 | 7.31k | if (ASN1_buf_print(bp, tmp, n, indent + 4) == 0) |
91 | 0 | goto err; |
92 | 7.31k | rv = 1; |
93 | 7.31k | err: |
94 | 7.31k | OPENSSL_clear_free(buf, buflen); |
95 | 7.31k | return rv; |
96 | 7.31k | } |