1.\" $OpenBSD: i2a_ASN1_STRING.3,v 1.4 2022/09/10 12:36:18 jsg Exp $ 2.\" 3.\" Copyright (c) 2019, 2021 Ingo Schwarze <schwarze@openbsd.org> 4.\" 5.\" Permission to use, copy, modify, and distribute this software for any 6.\" purpose with or without fee is hereby granted, provided that the above 7.\" copyright notice and this permission notice appear in all copies. 8.\" 9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16.\" 17.Dd $Mdocdate: September 10 2022 $ 18.Dt I2A_ASN1_STRING 3 19.Os 20.Sh NAME 21.Nm i2a_ASN1_STRING , 22.Nm i2a_ASN1_INTEGER , 23.Nm i2a_ASN1_ENUMERATED , 24.Nm a2i_ASN1_STRING , 25.Nm a2i_ASN1_INTEGER , 26.Nm a2i_ASN1_ENUMERATED 27.Nd hexadecimal dump of an ASN.1 string 28.Sh SYNOPSIS 29.In openssl/asn1.h 30.Ft int 31.Fo i2a_ASN1_STRING 32.Fa "BIO *out_bio" 33.Fa "const ASN1_STRING *a" 34.Fa "int type" 35.Fc 36.Ft int 37.Fo i2a_ASN1_INTEGER 38.Fa "BIO *out_bio" 39.Fa "const ASN1_INTEGER *a" 40.Fc 41.Ft int 42.Fo i2a_ASN1_ENUMERATED 43.Fa "BIO *out_bio" 44.Fa "const i2a_ASN1_ENUMERATED *a" 45.Fc 46.Ft int 47.Fo a2i_ASN1_STRING 48.Fa "BIO *in_bio" 49.Fa "ASN1_STRING *out_string" 50.Fa "char *buffer" 51.Fa "int size" 52.Fc 53.Ft int 54.Fo a2i_ASN1_INTEGER 55.Fa "BIO *in_bio" 56.Fa "ASN1_INTEGER *out_string" 57.Fa "char *buffer" 58.Fa "int size" 59.Fc 60.Ft int 61.Fo a2i_ASN1_ENUMERATED 62.Fa "BIO *in_bio" 63.Fa "ASN1_ENUMERATED *out_string" 64.Fa "char *buffer" 65.Fa "int size" 66.Fc 67.Sh DESCRIPTION 68The functions 69.Fn i2a_ASN1_STRING , 70.Fn i2a_ASN1_INTEGER , 71and 72.Fn i2a_ASN1_ENUMERATED 73write a hexadecimal representation of 74.Fa a 75to 76.Fa out_bio . 77The 78.Fa type 79argument is ignored. 80.Pp 81Each byte of 82.Xr ASN1_STRING_get0_data 3 83is written as a number consisting of two upper-case hexadecimal digits. 84After each group of 70 digits, a backslash and a linefeed 85are inserted before the next digit. 86.Pp 87If the 88.Xr ASN1_STRING_length 3 89of 90.Fa a 91is 0, instead a pair of zero digits 92.Pq Qq 00 93is written by 94.Fn i2a_ASN1_INTEGER 95and 96.Fn i2a_ASN1_ENUMERATED 97and a single zero digit 98.Pq Qq 0 99by 100.Fn i2a_ASN1_STRING . 101If 102.Fa a 103is a 104.Dv NULL 105pointer, nothing is written. 106.Pp 107If 108.Fa a 109represents a negative integer, 110.Fn i2a_ASN1_INTEGER 111prepends a minus sign to the output. 112.Pp 113The functions 114.Fn a2i_ASN1_STRING , 115.Fn a2i_ASN1_INTEGER , 116and 117.Fn a2i_ASN1_ENUMERATED 118parse a hexadecimal representation of an ASN.1 string into 119.Fa out_string . 120Both lower-case and upper-case hexadecimal digits are accepted. 121Every pair of input digits is converted into one output byte. 122.Pp 123On every input line, the trailing newline character and an optional 124carriage return character preceding it are ignored. 125The trailing newline need not be present on the last line. 126If there is a backslash character before the newline character, 127parsing is continued on the next input line. 128.Pp 129At least one pair of input digits is required by 130.Fn a2i_ASN1_INTEGER 131and 132.Fn a2i_ASN1_ENUMERATED , 133whereas 134.Fn a2i_ASN1_STRING 135converts empty input to an empty string. 136.Pp 137These functions are able to parse the output of 138.Fn i2a_ASN1_ENUMERATED . 139They can parse the output of 140.Fn i2a_ASN1_INTEGER 141unless 142.Fa a 143was negative, and they can parse the output of 144.Fn i2a_ASN1_STRING 145unless the 146.Xr ASN1_STRING_length 3 147of 148.Fa a 149was 0. 150.Pp 151Parsing fails if an input line contains an odd number of input 152digits or if memory allocation fails. 153.Pp 154These functions use the 155.Fa buffer 156provided by the caller and assume it is at least 157.Fa size 158bytes long. 159It is unspecified what the buffer contains after the functions return. 160.Sh RETURN VALUES 161The functions 162.Fn i2a_ASN1_STRING , 163.Fn i2a_ASN1_INTEGER , 164and 165.Fn i2a_ASN1_ENUMERATED 166return the number of bytes written or \-1 if 167.Xr BIO_write 3 168fails. 169In particular, they all return 0 when 170.Fa a 171is a 172.Dv NULL 173pointer. 174.Fn i2a_ASN1_STRING 175returns 1 for an empty string or an even number greater than 1 176for a string that is not empty. 177.Fn i2a_ASN1_INTEGER 178returns an even number greater than 1 for positive input 179or an odd number greater than 2 for negative input. 180.Fn i2a_ASN1_ENUMERATED 181always returns a non-negative even number when successful. 182.Pp 183The functions 184.Fn a2i_ASN1_STRING , 185.Fn a2i_ASN1_INTEGER , 186and 187.Fn a2i_ASN1_ENUMERATED 188are intended to return 1 for success or 0 for failure, but see the 189.Sx BUGS 190section for a number of traps. 191.Sh SEE ALSO 192.Xr ASN1_STRING_length 3 , 193.Xr ASN1_STRING_new 3 , 194.Xr ASN1_STRING_print_ex 3 , 195.Xr i2a_ASN1_OBJECT 3 196.Sh HISTORY 197.Fn i2a_ASN1_INTEGER 198and 199.Fn a2i_ASN1_INTEGER 200first appeared in SSLeay 0.6.0. 201.Fn i2a_ASN1_STRING 202and 203.Fn a2i_ASN1_STRING 204first appeared in SSLeay 0.6.5. 205.Fn a2i_ASN1_STRING 206has been part of the public API since SSLeay 0.6.5 and 207.Fn i2a_ASN1_STRING 208since SSLeay 0.8.0. 209These functions have been available since 210.Ox 2.4 . 211.Pp 212.Fn i2a_ASN1_ENUMERATED 213and 214.Fn a2i_ASN1_ENUMERATED 215first appeared in OpenSSL 0.9.2 and have been available since 216.Ox 2.6 . 217.Sh BUGS 218If the first call to 219.Xr BIO_gets 3 220does not return any data, even if that is caused by a fatal I/O error, 221if the BIO type does not support the 222.Dq gets 223operation, or if it is caused by the BIO being non-blocking, 224.Fn a2i_ASN1_STRING 225immediately succeeds and returns an empty 226.Fa out_string . 227.Pp 228If 229.Fn BIO_gets 3 230returns a partial line, for example because the given 231.Fa size 232is insufficient to contain one of the input lines 233or for reasons specific to the BIO type, 234.Fn a2i_ASN1_STRING , 235.Fn a2i_ASN1_INTEGER , 236and 237.Fn a2i_ASN1_ENUMERATED 238may fail or silently return a truncated result. 239The caller is responsible for providing a 240.Fa buffer 241of sufficient size to contain the longest possible input line 242and for choosing a BIO of a type that only returns complete 243input lines and does not perform partial reads. 244.Pp 245The functions 246.Fn a2i_ASN1_STRING , 247.Fn a2i_ASN1_INTEGER , 248and 249.Fn a2i_ASN1_ENUMERATED 250do not support non-blocking BIOs. 251Reading is terminated as soon as 252.Xr BIO_gets 3 253returns a value less than 1. 254