2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "crypto/ctype.h"
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/asn1.h>
16 int i2a_ASN1_INTEGER(BIO
*bp
, const ASN1_INTEGER
*a
)
19 static const char *h
= "0123456789ABCDEF";
25 if (a
->type
& V_ASN1_NEG
) {
26 if (BIO_write(bp
, "-", 1) != 1)
32 if (BIO_write(bp
, "00", 2) != 2)
36 for (i
= 0; i
< a
->length
; i
++) {
37 if ((i
!= 0) && (i
% 35 == 0)) {
38 if (BIO_write(bp
, "\\\n", 2) != 2)
42 buf
[0] = h
[((unsigned char)a
->data
[i
] >> 4) & 0x0f];
43 buf
[1] = h
[((unsigned char)a
->data
[i
]) & 0x0f];
44 if (BIO_write(bp
, buf
, 2) != 2)
54 int a2i_ASN1_INTEGER(BIO
*bp
, ASN1_INTEGER
*bs
, char *buf
, int size
)
56 int i
, j
, k
, m
, n
, again
, bufsize
;
57 unsigned char *s
= NULL
, *sp
;
59 int num
= 0, slen
= 0, first
= 1;
61 bs
->type
= V_ASN1_INTEGER
;
63 bufsize
= BIO_gets(bp
, buf
, size
);
68 if (buf
[i
- 1] == '\n')
72 if (buf
[i
- 1] == '\r')
76 again
= (buf
[i
- 1] == '\\');
78 for (j
= 0; j
< i
; j
++) {
79 if (!ossl_isxdigit(buf
[j
]))
87 * We have now cleared all the crap off the end of the line
92 bufp
= (unsigned char *)buf
;
95 if ((bufp
[0] == '0') && (bufp
[1] == '0')) {
103 ERR_raise(ERR_LIB_ASN1
, ASN1_R_ODD_NUMBER_OF_CHARS
);
108 if (num
+ i
> slen
) {
109 sp
= OPENSSL_clear_realloc(s
, slen
, num
+ i
* 2);
111 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
118 for (j
= 0; j
< i
; j
++, k
+= 2) {
119 for (n
= 0; n
< 2; n
++) {
120 m
= OPENSSL_hexchar2int(bufp
[k
+ n
]);
122 ERR_raise(ERR_LIB_ASN1
, ASN1_R_NON_HEX_CHARACTERS
);
131 bufsize
= BIO_gets(bp
, buf
, size
);
139 ERR_raise(ERR_LIB_ASN1
, ASN1_R_SHORT_LINE
);
144 int i2a_ASN1_ENUMERATED(BIO
*bp
, const ASN1_ENUMERATED
*a
)
146 return i2a_ASN1_INTEGER(bp
, a
);
149 int a2i_ASN1_ENUMERATED(BIO
*bp
, ASN1_ENUMERATED
*bs
, char *buf
, int size
)
151 int rv
= a2i_ASN1_INTEGER(bp
, bs
, buf
, size
);
153 bs
->type
= V_ASN1_INTEGER
| (bs
->type
& V_ASN1_NEG
);