xref: /openbsd-src/lib/libcrypto/man/s2i_ASN1_INTEGER.3 (revision 6695bae3549cf267576fa3056e858c56d76ac42b)
1.\" $OpenBSD: s2i_ASN1_INTEGER.3,v 1.9 2024/12/27 15:30:17 schwarze Exp $
2.\"
3.\" Copyright (c) 2023 Theo Buehler <tb@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: December 27 2024 $
18.Dt S2I_ASN1_INTEGER 3
19.Os
20.Sh NAME
21.Nm i2s_ASN1_ENUMERATED ,
22.Nm i2s_ASN1_ENUMERATED_TABLE ,
23.Nm i2s_ASN1_INTEGER ,
24.Nm s2i_ASN1_INTEGER ,
25.Nm i2s_ASN1_OCTET_STRING ,
26.Nm s2i_ASN1_OCTET_STRING
27.Nd ASN.1 data type conversion utilities for certificate extensions
28.Sh SYNOPSIS
29.In openssl/asn1.h
30.In openssl/x509v3.h
31.Ft "char *"
32.Fo i2s_ASN1_ENUMERATED
33.Fa "X509V3_EXT_METHOD *method"
34.Fa "const ASN1_ENUMERATED *a"
35.Fc
36.Ft "char *"
37.Fo i2s_ASN1_INTEGER
38.Fa "X509V3_EXT_METHOD *method"
39.Fa "const ASN1_INTEGER *a"
40.Fc
41.Ft "ASN1_INTEGER *"
42.Fo s2i_ASN1_INTEGER
43.Fa "X509V3_EXT_METHOD *method"
44.Fa "const char *value"
45.Fc
46.Ft "char *"
47.Fo i2s_ASN1_OCTET_STRING
48.Fa "X509V3_EXT_METHOD *method"
49.Fa "const ASN1_OCTET_STRING *aos"
50.Fc
51.Ft "ASN1_OCTET_STRING *"
52.Fo s2i_ASN1_OCTET_STRING
53.Fa "X509V3_EXT_METHOD *method"
54.Fa "X509V3_CTX *ctx"
55.Fa "const char *value"
56.Fc
57.Ft "char *"
58.Fo i2s_ASN1_ENUMERATED_TABLE
59.Fa "X509V3_EXT_METHOD *method"
60.Fa "const ASN1_ENUMERATED *a"
61.Fc
62.Sh DESCRIPTION
63These functions convert to and from
64.Vt ASN1_ENUMERATED ,
65.Vt ASN1_INTEGER ,
66and
67.Vt ASN1_OCTET_STRING
68objects.
69They are primarily used internally for parsing configuration files and
70displaying X.509v3 certificate extensions.
71With the exception of
72.Fn i2s_ASN1_ENUMERATED_TABLE ,
73these functions ignore the
74.Fa method
75argument.
76Any object or string returned by these functions must be freed by the caller.
77.Pp
78.Fn i2s_ASN1_ENUMERATED
79and
80.Fn i2s_ASN1_INTEGER
81first convert
82.Fa a
83into a
84.Vt BIGNUM
85object with
86.Xr ASN1_ENUMERATED_to_BN 3
87or
88.Xr ASN1_INTEGER_to_BN 3
89and then derive a string representation using
90.Xr BN_bn2dec 3
91or
92.Xr BN_bn2hex 3 .
93Decimal representation is used if the number has less than 128 bits,
94otherwise hexadecimal representation is used to avoid excessive conversion cost.
95.Pp
96.Fn s2i_ASN1_INTEGER
97converts the NUL-terminated decimal or hexadecimal string representation of
98an integer in
99.Fa value
100into an
101.Vt ASN1_INTEGER
102object.
103A sign prefix of
104.Sq -
105indicates a negative number and the base prefixes
106.Sq 0x
107and
108.Sq 0X
109indicate hexadecimal representation,
110otherwise decimal representation is assumed.
111After skipping the sign and base prefixes, an intermediate conversion into a
112.Vt BIGNUM
113is performed using
114.Xr BN_dec2bn 3
115or
116.Xr BN_hex2bn 3
117and the
118.Vt ASN1_INTEGER
119is then obtained with
120.Xr BN_to_ASN1_INTEGER 3 .
121.Pp
122.Fn i2s_ASN1_OCTET_STRING
123converts the octets in
124.Fa aos
125into a string where the octets are colon-separated and
126represented as pairs of uppercase hexadecimal digits.
127.Pp
128.Fn s2i_ASN1_OCTET_STRING
129converts the NUL-terminated string
130.Fa str
131into an
132.Vt ASN1_OCTET_STRING .
133The
134.Fa method
135and
136.Fa ctx
137arguments are ignored.
138Every pair of hexadecimal digits is converted into an octet.
139Colons are ignored if they are at the start, the end or
140if they separate two pairs of digits.
141.Pp
142.Fn i2s_ASN1_ENUMERATED_TABLE
143looks up the value of
144.Fa a
145in the
146.Fa usr_data
147field of the
148.Pf non- Dv NULL
149.Fa method
150and returns a copy of the associated long name.
151If no match is found,
152.Fa a
153is passed to
154.Fn i2s_ASN1_ENUMERATED .
155The
156.Fa method
157argument can be provided by application programs or it can be a
158default method obtained from
159.Xr X509V3_EXT_get_nid 3 .
160The default
161.Fa methods
162corresponding to the following
163.Fa nid
164arguments have strings configured in their usr_data field:
165.Pp
166.Bl -column NID_netscape_cert_type "Netscape certificate type (obsolete)" -compact
167.It Dv NID_crl_reason           Ta reason codes, RFC 5280, 5.3.1
168.It Dv NID_key_usage            Ta key usage, RFC 5280, 4.2.1.3
169.It Dv NID_netscape_cert_type   Ta Netscape certificate type (obsolete)
170.El
171.Sh RETURN VALUES
172.Fn i2s_ASN1_ENUMERATED ,
173.Fn i2s_ASN1_ENUMERATED_TABLE ,
174.Fn i2s_ASN1_INTEGER ,
175and
176.Fn i2s_ASN1_OCTET_STRING
177return a NUL-terminated string, or NULL on memory allocation failure.
178.Pp
179.Fn s2i_ASN1_INTEGER
180returns an
181.Vt ASN1_INTEGER ,
182or NULL on error.
183Error conditions are memory allocation failure or if
184.Fa value
185is not a valid decimal or hexadecimal encoding of an integer.
186.Pp
187.Fn s2i_ASN1_OCTET_STRING
188returns an
189.Vt ASN1_OCTET_STRING ,
190or NULL on error.
191Error conditions are memory allocation failure or if
192.Fa value
193contains an odd number of hexadecimal digits or anything except
194colons at the start, the end or between pairs of hexadecimal digits.
195.Pp
196Error codes can sometimes be obtained by
197.Xr ERR_get_error 3 .
198.Sh SEE ALSO
199.Xr a2i_ASN1_INTEGER 3 ,
200.Xr a2i_ipadd 3 ,
201.Xr ASN1_INTEGER_new 3 ,
202.Xr ASN1_INTEGER_to_BN 3 ,
203.Xr ASN1_OCTET_STRING_new 3 ,
204.Xr crypto 3 ,
205.Xr v2i_ASN1_BIT_STRING 3 ,
206.Xr X509V3_get_d2i 3
207.Sh HISTORY
208These functions first appeared in OpenSSL 0.9.4 and
209have been available since
210.Ox 2.6 .
211.Sh BUGS
212Of these functions at least
213.Fn i2s_ASN1_ENUMERATED_TABLE
214can succeed while setting an error and fail without setting an error
215on the error stack.
216