2016-05-17 18:51:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2002-06-18 08:38:59 +00:00
|
|
|
#include <openssl/objects.h>
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/buffer.h>
|
2014-10-31 00:01:18 +00:00
|
|
|
#include "internal/bn_int.h"
|
2002-02-13 18:21:51 +00:00
|
|
|
|
2016-01-27 14:34:36 +00:00
|
|
|
/* Number of octets per line */
|
|
|
|
#define ASN1_BUF_PRINT_WIDTH 15
|
|
|
|
/* Maximum indent */
|
|
|
|
#define ASN1_PRINT_MAX_INDENT 128
|
|
|
|
|
2016-07-07 22:43:15 +00:00
|
|
|
int ASN1_buf_print(BIO *bp, const unsigned char *buf, size_t buflen, int indent)
|
2016-01-27 14:34:36 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < buflen; i++) {
|
|
|
|
if ((i % ASN1_BUF_PRINT_WIDTH) == 0) {
|
|
|
|
if (i > 0 && BIO_puts(bp, "\n") <= 0)
|
|
|
|
return 0;
|
|
|
|
if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Use colon separators for each octet for compatibility as
|
2016-06-19 17:39:01 +00:00
|
|
|
* this function is used to print out key components.
|
2016-01-27 14:34:36 +00:00
|
|
|
*/
|
|
|
|
if (BIO_printf(bp, "%02x%s", buf[i],
|
|
|
|
(i == buflen - 1) ? "" : ":") <= 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (BIO_write(bp, "\n", 1) <= 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-04-15 13:15:25 +00:00
|
|
|
int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num,
|
2016-02-04 18:53:07 +00:00
|
|
|
unsigned char *ign, int indent)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-02-04 18:53:07 +00:00
|
|
|
int n, rv = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
const char *neg;
|
2016-02-04 18:53:07 +00:00
|
|
|
unsigned char *buf = NULL, *tmp = NULL;
|
|
|
|
int buflen;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (num == NULL)
|
2016-01-27 14:34:36 +00:00
|
|
|
return 1;
|
2016-02-04 18:53:07 +00:00
|
|
|
neg = BN_is_negative(num) ? "-" : "";
|
2016-01-27 14:34:36 +00:00
|
|
|
if (!BIO_indent(bp, indent, ASN1_PRINT_MAX_INDENT))
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
|
|
|
if (BN_is_zero(num)) {
|
|
|
|
if (BIO_printf(bp, "%s 0\n", number) <= 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (BN_num_bytes(num) <= BN_BYTES) {
|
|
|
|
if (BIO_printf(bp, "%s %s%lu (%s0x%lx)\n", number, neg,
|
|
|
|
(unsigned long)bn_get_words(num)[0], neg,
|
|
|
|
(unsigned long)bn_get_words(num)[0]) <= 0)
|
2016-01-27 14:34:36 +00:00
|
|
|
return 0;
|
2016-02-04 18:53:07 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-02-04 18:53:07 +00:00
|
|
|
|
|
|
|
buflen = BN_num_bytes(num) + 1;
|
|
|
|
buf = tmp = OPENSSL_malloc(buflen);
|
|
|
|
if (buf == NULL)
|
|
|
|
goto err;
|
|
|
|
buf[0] = 0;
|
|
|
|
if (BIO_printf(bp, "%s%s\n", number,
|
|
|
|
(neg[0] == '-') ? " (Negative)" : "") <= 0)
|
|
|
|
goto err;
|
|
|
|
n = BN_bn2bin(num, buf + 1);
|
|
|
|
|
|
|
|
if (buf[1] & 0x80)
|
|
|
|
n++;
|
|
|
|
else
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
if (ASN1_buf_print(bp, tmp, n, indent + 4) == 0)
|
|
|
|
goto err;
|
|
|
|
rv = 1;
|
|
|
|
err:
|
|
|
|
OPENSSL_clear_free(buf, buflen);
|
|
|
|
return rv;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|