summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorschwarze <>2023-09-05 13:50:22 +0000
committerschwarze <>2023-09-05 13:50:22 +0000
commitf3e03e30764769d345b4643fb2d65b6b5b758f5a (patch)
tree25e6bcfc3b4d16fc849914ae43946ea38f4f88d8 /src/lib
parent33b0f5cf9e5572bcf6800932c6a58e5e35485dbb (diff)
downloadopenbsd-f3e03e30764769d345b4643fb2d65b6b5b758f5a.tar.gz
openbsd-f3e03e30764769d345b4643fb2d65b6b5b758f5a.tar.bz2
openbsd-f3e03e30764769d345b4643fb2d65b6b5b758f5a.zip
Partial rewrite:
* Integrate the leftovers of the former NOTES section into the main text, resulting in a more logical order of information. * Make many descriptions more precise and tweak many wordings. For example, the description of OBJ_cmp(3) was totally misleading. Add a CAVEATS section explaining the scary ownership contracts of the functions returning ASN1_OBJECT pointers. Move the discussion of NID_undef to the BUGS section because the statement "objects which are not in the table have the NID value NID_undef" was misleading in more than one way. Considering that an API as fundamental as this one contains such a gigantic amount of quirks and traps and gaps makes me shudder.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/man/OBJ_nid2obj.3371
1 files changed, 231 insertions, 140 deletions
diff --git a/src/lib/libcrypto/man/OBJ_nid2obj.3 b/src/lib/libcrypto/man/OBJ_nid2obj.3
index 62d67ac740..4e420b8311 100644
--- a/src/lib/libcrypto/man/OBJ_nid2obj.3
+++ b/src/lib/libcrypto/man/OBJ_nid2obj.3
@@ -1,11 +1,11 @@
1.\" $OpenBSD: OBJ_nid2obj.3,v 1.20 2023/07/21 05:02:53 tb Exp $ 1.\" $OpenBSD: OBJ_nid2obj.3,v 1.21 2023/09/05 13:50:22 schwarze Exp $
2.\" full merge up to: OpenSSL c264592d May 14 11:28:00 2006 +0000 2.\" full merge up to: OpenSSL c264592d May 14 11:28:00 2006 +0000
3.\" selective merge up to: OpenSSL 35fd9953 May 28 14:49:38 2019 +0200 3.\" selective merge up to: OpenSSL 35fd9953 May 28 14:49:38 2019 +0200
4.\" 4.\"
5.\" This file is a derived work. 5.\" This file is a derived work.
6.\" The changes are covered by the following Copyright and license: 6.\" The changes are covered by the following Copyright and license:
7.\" 7.\"
8.\" Copyright (c) 2017, 2021 Ingo Schwarze <schwarze@openbsd.org> 8.\" Copyright (c) 2017, 2021, 2023 Ingo Schwarze <schwarze@openbsd.org>
9.\" 9.\"
10.\" Permission to use, copy, modify, and distribute this software for any 10.\" Permission to use, copy, modify, and distribute this software for any
11.\" purpose with or without fee is hereby granted, provided that the above 11.\" purpose with or without fee is hereby granted, provided that the above
@@ -67,7 +67,7 @@
67.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 67.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
68.\" OF THE POSSIBILITY OF SUCH DAMAGE. 68.\" OF THE POSSIBILITY OF SUCH DAMAGE.
69.\" 69.\"
70.Dd $Mdocdate: July 21 2023 $ 70.Dd $Mdocdate: September 5 2023 $
71.Dt OBJ_NID2OBJ 3 71.Dt OBJ_NID2OBJ 3
72.Os 72.Os
73.Sh NAME 73.Sh NAME
@@ -89,19 +89,19 @@
89.In openssl/objects.h 89.In openssl/objects.h
90.Ft ASN1_OBJECT * 90.Ft ASN1_OBJECT *
91.Fo OBJ_nid2obj 91.Fo OBJ_nid2obj
92.Fa "int n" 92.Fa "int nid"
93.Fc 93.Fc
94.Ft const char * 94.Ft const char *
95.Fo OBJ_nid2ln 95.Fo OBJ_nid2ln
96.Fa "int n" 96.Fa "int nid"
97.Fc 97.Fc
98.Ft const char * 98.Ft const char *
99.Fo OBJ_nid2sn 99.Fo OBJ_nid2sn
100.Fa "int n" 100.Fa "int nid"
101.Fc 101.Fc
102.Ft int 102.Ft int
103.Fo OBJ_obj2nid 103.Fo OBJ_obj2nid
104.Fa "const ASN1_OBJECT *o" 104.Fa "const ASN1_OBJECT *object"
105.Fc 105.Fc
106.Ft int 106.Ft int
107.Fo OBJ_ln2nid 107.Fo OBJ_ln2nid
@@ -124,7 +124,7 @@
124.Fo OBJ_obj2txt 124.Fo OBJ_obj2txt
125.Fa "char *buf" 125.Fa "char *buf"
126.Fa "int buf_len" 126.Fa "int buf_len"
127.Fa "const ASN1_OBJECT *a" 127.Fa "const ASN1_OBJECT *object"
128.Fa "int no_name" 128.Fa "int no_name"
129.Fc 129.Fc
130.Ft int 130.Ft int
@@ -134,86 +134,102 @@
134.Fc 134.Fc
135.Ft ASN1_OBJECT * 135.Ft ASN1_OBJECT *
136.Fo OBJ_dup 136.Fo OBJ_dup
137.Fa "const ASN1_OBJECT *o" 137.Fa "const ASN1_OBJECT *object"
138.Fc 138.Fc
139.In openssl/asn1.h 139.In openssl/asn1.h
140.Ft int 140.Ft int
141.Fo i2t_ASN1_OBJECT 141.Fo i2t_ASN1_OBJECT
142.Fa "char *buf" 142.Fa "char *buf"
143.Fa "int buf_len" 143.Fa "int buf_len"
144.Fa "const ASN1_OBJECT *a" 144.Fa "const ASN1_OBJECT *object"
145.Fc 145.Fc
146.Ft int 146.Ft int
147.Fo i2a_ASN1_OBJECT 147.Fo i2a_ASN1_OBJECT
148.Fa "BIO *out_bio" 148.Fa "BIO *out_bio"
149.Fa "const ASN1_OBJECT *a" 149.Fa "const ASN1_OBJECT *object"
150.Fc 150.Fc
151.Sh DESCRIPTION 151.Sh DESCRIPTION
152The ASN.1 object utility functions process 152The ASN.1 object utility functions process
153.Vt ASN1_OBJECT 153.Vt ASN1_OBJECT
154structures which are a representation of the ASN.1 OBJECT IDENTIFIER 154structures, in the following called
155(OID) type. 155.Dq objects .
156For convenience, OIDs are usually represented in source code as 156An object represents an ASN.1
157numeric identifiers, or NIDs. 157.Vt OBJECT IDENTIFIER
158OpenSSL has an internal table of OIDs that are generated when the 158.Pq OID .
159library is built, and their corresponding NIDs are available as 159The library maintains an internal global table of objects.
160defined constants. 160Many of these objects are built into the library
161For the functions below, application code should treat all returned 161and contained in the global table by default.
162values \(em OIDs, NIDs, or names \(em as constants. 162The application program can add additional objects to the global table
163by using functions documented in the
164.Xr OBJ_create 3
165manual page.
166Consequently, there are three classes of objects:
167built-in table objects, user-defined table objects, and non-table objects.
163.Pp 168.Pp
164.Fn OBJ_nid2obj , 169In addition to the OID, each object can hold
165.Fn OBJ_nid2ln , 170a long name, a short name, and a numerical identifier (NID).
171Even though the concept of NIDs is specific to the library
172and not standardized, using the NID is often the most convenient way
173for source code to refer to a specific OID.
174The NIDs of the built-in objects are available as defined constants.
175.Pp
176Built-in table objects have certain advantages
177over objects that are not in the global table:
178for example, their NIDs can be used in C language switch statements.
179They are also shared:
180there is only a single static constant structure for each built-on OID.
181.Pp
182Some functions operate on table objects only:
183.Pp
184.Fn OBJ_nid2obj
185retrieves the table object associated with the
186.Fa nid .
187.Fn OBJ_nid2ln
166and 188and
167.Fn OBJ_nid2sn 189.Fn OBJ_nid2sn
168convert the NID 190retrieve its long and short name, respectively.
169.Fa n
170to an
171.Vt ASN1_OBJECT
172structure, its long name, and its short name, respectively, or return
173.Dv NULL
174if an error occurred.
175.Pp 191.Pp
176.Fn OBJ_obj2nid , 192.Fn OBJ_obj2nid
177.Fn OBJ_ln2nid , 193retrieves the NID associated with the given
194.Fa object ,
195which is either the NID stored in the
196.Fa object
197itself, if any, or otherwise the NID stored in a table object
198containing the same OID.
199.Pp
200.Fn OBJ_ln2nid
178and 201and
179.Fn OBJ_sn2nid 202.Fn OBJ_sn2nid
180return the corresponding NID for the object 203retrieve the NID from the table object with the long name
181.Fa o , 204.Fa ln
182the long name
183.Fa ln ,
184or the short name 205or the short name
185.Fa sn , 206.Fa sn ,
186respectively, or 207respectively.
187.Dv NID_undef
188if an error occurred.
189.Pp 208.Pp
190.Fn OBJ_txt2nid 209.Fn OBJ_txt2nid
191returns the NID corresponding to text string 210retrieves the NID from the table object described by the text string
192.Fa s . 211.Fa s ,
193.Fa s 212which can be a long name, a short name,
194can be a long name, a short name, or the numerical representation 213or the numerical representation of an OID.
195of an object. 214.Pp
215The remaining functions can be used both on table objects
216and on objects that are not in the global table:
196.Pp 217.Pp
197.Fn OBJ_txt2obj 218.Fn OBJ_txt2obj
198converts the text string 219retrieves or creates an object matching the text string
199.Fa s 220.Fa s .
200into an
201.Vt ASN1_OBJECT
202structure.
203If 221If
204.Fa no_name 222.Fa no_name
205is 0 then long names and short names will be interpreted as well as 223is 1, only the numerical representation of an OID is accepted.
206numerical forms.
207If 224If
208.Fa no_name 225.Fa no_name
209is 1, only the numerical form is acceptable. 226is 0, long names and short names are accepted as well.
210.Pp 227.Pp
211.Fn OBJ_obj2txt 228.Fn OBJ_obj2txt
212converts the 229writes a NUL terminated textual representation
213.Vt ASN1_OBJECT 230of the OID contained in the given
214.Fa a 231.Fa object
215into a textual representation. 232into
216The representation is written as a NUL terminated string to
217.Fa buf . 233.Fa buf .
218At most 234At most
219.Fa buf_len 235.Fa buf_len
@@ -221,8 +237,13 @@ bytes are written, truncating the result if necessary.
221The total amount of space required is returned. 237The total amount of space required is returned.
222If 238If
223.Fa no_name 239.Fa no_name
224is 0 and the object has a long or short name, then that will be used, 240is 0 and the table object containing the same OID
225otherwise the numerical form will be used. 241contains a long name, the long name is written.
242Otherwise, if
243.Fa no_name
244is 0 and the table object containing the same OID
245contains a short name, the short name is written.
246Otherwise, the numerical representation of the OID is written.
226.Pp 247.Pp
227.Fn i2t_ASN1_OBJECT 248.Fn i2t_ASN1_OBJECT
228is the same as 249is the same as
@@ -232,18 +253,18 @@ with
232set to 0. 253set to 0.
233.Pp 254.Pp
234.Fn i2a_ASN1_OBJECT 255.Fn i2a_ASN1_OBJECT
235writes a textual representation of 256writes a textual representation of the OID contained in the given
236.Fa a 257.Fa object
237to 258to
238.Fa out_bio 259.Fa out_bio
239using 260using
240.Xr BIO_write 3 . 261.Xr BIO_write 3 .
241It does not write a terminating NUL byte. 262It does not write a terminating NUL byte.
242If 263If the
243.Fa a 264.Fa object
244is 265argument is
245.Dv NULL 266.Dv NULL
246or contains no data, it writes the 4-byte string 267or contains no OID, it writes the 4-byte string
247.Qq NULL . 268.Qq NULL .
248If 269If
249.Fn i2t_ASN1_OBJECT 270.Fn i2t_ASN1_OBJECT
@@ -255,104 +276,129 @@ Otherwise, it writes the string constructed with
255.Fn i2t_ASN1_OBJECT . 276.Fn i2t_ASN1_OBJECT .
256.Pp 277.Pp
257.Fn OBJ_cmp 278.Fn OBJ_cmp
258compares 279tests whether
259.Fa a 280.Fa a
260to 281and
261.Fa b . 282.Fa b
262If the two are identical, 0 is returned. 283represent the same ASN.1
284.Vt OBJECT IDENTIFIER .
285Any names and NIDs contained in the two objects are ignored,
286even if they differ between both objects.
263.Pp 287.Pp
264.Fn OBJ_dup 288.Fn OBJ_dup
265returns a deep copy of 289returns a deep copy of the given
266.Fa o 290.Fa object
267if 291if it is marked as dynamically allocated.
268.Fa o 292The new object and all data contained in it are marked as dynamically
269is marked as dynamically allocated.
270The new object and all data contained in it is marked as dynamically
271allocated. 293allocated.
272If 294If the given
273.Fa o 295.Fa object
274is not marked as dynamically allocated, 296is not marked as dynamically allocated,
275.Fn OBJ_dup 297.Fn OBJ_dup
276just returns 298just returns a pointer to the
277.Fa o 299.Fa object
278itself. 300itself.
279.Pp
280Objects can have a short name, a long name, and a numerical
281identifier (NID) associated with them.
282A standard set of objects is represented in an internal table.
283The appropriate values are defined in the header file
284.In openssl/objects.h .
285.Pp
286For example, the OID for commonName has the following definitions:
287.Bd -literal
288#define SN_commonName "CN"
289#define LN_commonName "commonName"
290#define NID_commonName 13
291.Ed
292.Pp
293New objects can be added by calling
294.Xr OBJ_create 3 .
295.Pp
296Table objects have certain advantages over other objects: for example
297their NIDs can be used in a C language switch statement.
298They are also static constant structures which are shared: that is there
299is only a single constant structure for each table object.
300.Pp
301Objects which are not in the table have the NID value
302.Dv NID_undef .
303.Pp
304Objects do not need to be in the internal tables to be processed:
305the functions
306.Fn OBJ_txt2obj
307and
308.Fn OBJ_obj2txt
309can process the numerical form of an OID.
310.Sh RETURN VALUES 301.Sh RETURN VALUES
311.Fn OBJ_nid2obj , 302Application code should treat all returned values \(em
312.Fn OBJ_txt2obj , 303objects, names, and NIDs \(em as constants.
313and 304.Pp
314.Fn OBJ_dup 305.Fn OBJ_nid2obj
315return an 306returns a pointer to a table object owned by the library or
316.Vt ASN1_OBJECT
317object or
318.Dv NULL 307.Dv NULL
319if an error occurs. 308if no matching table object is found.
320.Pp 309.Pp
321.Fn OBJ_nid2ln 310.Fn OBJ_nid2ln
322and 311and
323.Fn OBJ_nid2sn 312.Fn OBJ_nid2sn
324return a valid string or 313return a pointer to a string owned by a table object or
325.Dv NULL 314.Dv NULL
326on error. 315if no matching table object is found.
316For
317.Dv NID_undef ,
318they return the constant static strings
319.Qq undefined
320and
321.Qq UNDEF ,
322respectively.
327.Pp 323.Pp
328.Fn OBJ_obj2nid , 324.Fn OBJ_obj2nid
329.Fn OBJ_ln2nid , 325returns an NID on success, or
330.Fn OBJ_sn2nid , 326.Dv NID_undef
327if
328.Fa object
329is
330.Dv NULL ,
331does not contain an OID,
332if no table object matching the OID is found,
333or if the matching object does not contain an NID.
334.Pp
335.Fn OBJ_ln2nid
331and 336and
337.Fn OBJ_sn2nid
338return an NID on success or
339.Dv NID_undef
340if no matching table object is found
341or if the matching object does not contain an NID.
342.Pp
332.Fn OBJ_txt2nid 343.Fn OBJ_txt2nid
333return a NID or 344returns an NID on success or
334.Dv NID_undef 345.Dv NID_undef
335on error. 346if parsing of
347.Fa s
348or memory allocation fails, if no matching table object is found,
349or if the matching object does not contain an NID.
350.Pp
351.Fn OBJ_txt2obj
352returns a pointer to a table object owned by the library if lookup of
353.Fa s
354as a long or short name succeeds.
355Otherwise, it returns a newly created object,
356transferring ownership to the caller, or
357.Dv NULL
358if parsing of
359.Fa s
360or memory allocation fails.
336.Pp 361.Pp
337.Fn OBJ_obj2txt 362.Fn OBJ_obj2txt
338and 363and
339.Fn i2t_ASN1_OBJECT 364.Fn i2t_ASN1_OBJECT
340return the amount of space required in bytes, 365return the amount of space required in bytes,
341including the terminating NUL byte. 366including the terminating NUL byte,
367or zero if an error occurs before the required space can be calculated,
368in particular if
369.Fa buf_len
370is negative,
371.Fa object
372is
373.Dv NULL
374or does not contain an OID,
375or if memory allocation fails.
376.Pp
377.Fn OBJ_cmp
378returns 0 if both objects refer to the same OID
379or neither of them are associated with any OID,
380or a non-zero value if at least one of them refers to an OID
381but the other one does not refer to the same OID.
382.Pp
383.Fn OBJ_dup
384returns the pointer to the original
385.Fa object
386if it is not marked as dynamically allocated.
387Otherwise, it returns a newly created object,
388transferring ownership to the caller, or
389.Dv NULL
390if
391.Fa object
392is
393.Dv NULL
394or memory allocation fails.
342.Pp 395.Pp
343.Fn i2a_ASN1_OBJECT 396.Fn i2a_ASN1_OBJECT
344returns the number of bytes written, even if 397returns the number of bytes written, even if the given
345.Fa a 398.Fa object
346is invalid or contains invalid data, 399is invalid or contains invalid data,
347but a negative value if memory allocation or a write operation fails. 400but a negative value if memory allocation or a write operation fails.
348.Pp 401.Pp
349.Fn OBJ_cmp
350returns 0 if the contents of
351.Fa a
352and
353.Fa b
354are identical, or non-zero otherwise.
355.Pp
356In some cases of failure of 402In some cases of failure of
357.Fn OBJ_nid2obj , 403.Fn OBJ_nid2obj ,
358.Fn OBJ_nid2ln , 404.Fn OBJ_nid2ln ,
@@ -367,23 +413,23 @@ and
367the reason can be determined with 413the reason can be determined with
368.Xr ERR_get_error 3 . 414.Xr ERR_get_error 3 .
369.Sh EXAMPLES 415.Sh EXAMPLES
370Create an object for 416Retrieve the object for
371.Sy commonName : 417.Sy commonName :
372.Bd -literal -offset indent 418.Bd -literal -offset indent
373ASN1_OBJECT *o; 419ASN1_OBJECT *object;
374o = OBJ_nid2obj(NID_commonName); 420object = OBJ_nid2obj(NID_commonName);
375.Ed 421.Ed
376.Pp 422.Pp
377Check if an object is 423Check whether an object contains the OID for
378.Sy commonName : 424.Sy commonName :
379.Bd -literal -offset indent 425.Bd -literal -offset indent
380if (OBJ_obj2nid(obj) == NID_commonName) 426if (OBJ_obj2nid(object) == NID_commonName)
381 /* Do something */ 427 /* Do something */
382.Ed 428.Ed
383.Pp 429.Pp
384Create a new object directly: 430Create a new object directly:
385.Bd -literal -offset indent 431.Bd -literal -offset indent
386obj = OBJ_txt2obj("1.2.3.4", 1); 432object = OBJ_txt2obj("1.2.3.4", 1);
387.Ed 433.Ed
388.Sh SEE ALSO 434.Sh SEE ALSO
389.Xr ASN1_OBJECT_new 3 , 435.Xr ASN1_OBJECT_new 3 ,
@@ -416,7 +462,52 @@ first appeared in OpenSSL 0.9.2b.
416first appeared in OpenSSL 0.9.4. 462first appeared in OpenSSL 0.9.4.
417Both functions have been available since 463Both functions have been available since
418.Ox 2.6 . 464.Ox 2.6 .
465.Sh CAVEATS
466The API contract of
467.Fn OBJ_txt2obj
468when called with a
469.Fa no_name
470argument of 0 and of
471.Fn OBJ_dup
472is scary in so far as the caller cannot find out from the returned
473object whether it is owned by the library or whether ownership was
474transferred to the caller.
475Consequently, it is best practice to assume that ownership of the object
476may have been transferred and call
477.Xr ASN1_OBJECT_free 3
478on the returned object when the caller no longer needs it.
479In case the library retained ownership of the returned object,
480.Xr ASN1_OBJECT_free 3
481has no effect and is harmless.
482.Pp
483Objects returned from
484.Fn OBJ_txt2obj
485with a
486.Fa no_name
487argument of 1 always require
488.Xr ASN1_OBJECT_free 3
489to prevent memory leaks.
490.Pp
491Objects returned from
492.Fn OBJ_nid2obj
493never require
494.Xr ASN1_OBJECT_free 3 ,
495but calling it anyway has no effect and is harmless.
419.Sh BUGS 496.Sh BUGS
497Usually, an object is expected to contain an NID other than
498.Dv NID_undef
499if and only if it is a table object.
500However, this is not an invariant guaranteed by the API.
501In particular,
502.Xr ASN1_OBJECT_create 3
503allows the creation of non-table objects containing bogus NIDs.
504.Fn OBJ_obj2nid
505returns such bogus NIDs even though
506.Fn OBJ_nid2obj
507cannot use them for retrieval.
508On top of that, the global table contains one built-in object with an NID of
509.Dv NID_undef .
510.Pp
420.Fn OBJ_obj2txt 511.Fn OBJ_obj2txt
421is awkward and messy to use: it doesn't follow the convention of other 512is awkward and messy to use: it doesn't follow the convention of other
422OpenSSL functions where the buffer can be set to 513OpenSSL functions where the buffer can be set to