]>
Commit | Line | Data |
---|---|---|
2039c421 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
2039c421 RS |
4 | * Licensed under the OpenSSL license (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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
5f3d6f70 | 12 | #include <openssl/objects.h> |
ec577822 | 13 | #include <openssl/buffer.h> |
76b2a022 | 14 | #include "internal/bn_int.h" |
4d94ae00 | 15 | |
26c255fc DSH |
16 | /* Number of octets per line */ |
17 | #define ASN1_BUF_PRINT_WIDTH 15 | |
18 | /* Maximum indent */ | |
19 | #define ASN1_PRINT_MAX_INDENT 128 | |
20 | ||
21 | int ASN1_buf_print(BIO *bp, unsigned char *buf, size_t buflen, int indent) | |
22 | { | |
23 | size_t i; | |
24 | ||
25 | for (i = 0; i < buflen; i++) { | |
26 | if ((i % ASN1_BUF_PRINT_WIDTH) == 0) { | |
27 | if (i > 0 && BIO_puts(bp, "\n") <= 0) | |
28 | return 0; | |
29 | if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT)) | |
30 | return 0; | |
31 | } | |
32 | /* | |
33 | * Use colon separators for each octet for compatibility as | |
34 | * this fuction is used to print out key components. | |
35 | */ | |
36 | if (BIO_printf(bp, "%02x%s", buf[i], | |
37 | (i == buflen - 1) ? "" : ":") <= 0) | |
38 | return 0; | |
39 | } | |
40 | if (BIO_write(bp, "\n", 1) <= 0) | |
41 | return 0; | |
42 | return 1; | |
43 | } | |
44 | ||
5950bf79 | 45 | int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num, |
ac3e3665 | 46 | unsigned char *ign, int indent) |
0f113f3e | 47 | { |
ac3e3665 | 48 | int n, rv = 0; |
0f113f3e | 49 | const char *neg; |
ac3e3665 DSH |
50 | unsigned char *buf = NULL, *tmp = NULL; |
51 | int buflen; | |
0f113f3e MC |
52 | |
53 | if (num == NULL) | |
26c255fc | 54 | return 1; |
ac3e3665 | 55 | neg = BN_is_negative(num) ? "-" : ""; |
26c255fc | 56 | if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT)) |
0f113f3e MC |
57 | return 0; |
58 | if (BN_is_zero(num)) { | |
59 | if (BIO_printf(bp, "%s 0\n", number) <= 0) | |
60 | return 0; | |
61 | return 1; | |
62 | } | |
d02b48c6 | 63 | |
0f113f3e MC |
64 | if (BN_num_bytes(num) <= BN_BYTES) { |
65 | if (BIO_printf(bp, "%s %s%lu (%s0x%lx)\n", number, neg, | |
66 | (unsigned long)bn_get_words(num)[0], neg, | |
67 | (unsigned long)bn_get_words(num)[0]) <= 0) | |
26c255fc | 68 | return 0; |
ac3e3665 | 69 | return 1; |
0f113f3e | 70 | } |
ac3e3665 DSH |
71 | |
72 | buflen = BN_num_bytes(num) + 1; | |
73 | buf = tmp = OPENSSL_malloc(buflen); | |
74 | if (buf == NULL) | |
75 | goto err; | |
76 | buf[0] = 0; | |
77 | if (BIO_printf(bp, "%s%s\n", number, | |
78 | (neg[0] == '-') ? " (Negative)" : "") <= 0) | |
79 | goto err; | |
80 | n = BN_bn2bin(num, buf + 1); | |
81 | ||
82 | if (buf[1] & 0x80) | |
83 | n++; | |
84 | else | |
85 | tmp++; | |
86 | ||
87 | if (ASN1_buf_print(bp, tmp, n, indent + 4) == 0) | |
88 | goto err; | |
89 | rv = 1; | |
90 | err: | |
91 | OPENSSL_clear_free(buf, buflen); | |
92 | return rv; | |
0f113f3e | 93 | } |