1.\" $OpenBSD: OBJ_nid2obj.3,v 1.22 2024/01/31 08:02:53 tb Exp $ 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 4.\" 5.\" This file is a derived work. 6.\" The changes are covered by the following Copyright and license: 7.\" 8.\" Copyright (c) 2017, 2021, 2023 Ingo Schwarze <schwarze@openbsd.org> 9.\" 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 12.\" copyright notice and this permission notice appear in all copies. 13.\" 14.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 15.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 16.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 17.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 18.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 19.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 20.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21.\" 22.\" The original file was written by Dr. Stephen Henson <steve@openssl.org>. 23.\" Copyright (c) 2002, 2006, 2016 The OpenSSL Project. 24.\" All rights reserved. 25.\" 26.\" Redistribution and use in source and binary forms, with or without 27.\" modification, are permitted provided that the following conditions 28.\" are met: 29.\" 30.\" 1. Redistributions of source code must retain the above copyright 31.\" notice, this list of conditions and the following disclaimer. 32.\" 33.\" 2. Redistributions in binary form must reproduce the above copyright 34.\" notice, this list of conditions and the following disclaimer in 35.\" the documentation and/or other materials provided with the 36.\" distribution. 37.\" 38.\" 3. All advertising materials mentioning features or use of this 39.\" software must display the following acknowledgment: 40.\" "This product includes software developed by the OpenSSL Project 41.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 42.\" 43.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 44.\" endorse or promote products derived from this software without 45.\" prior written permission. For written permission, please contact 46.\" openssl-core@openssl.org. 47.\" 48.\" 5. Products derived from this software may not be called "OpenSSL" 49.\" nor may "OpenSSL" appear in their names without prior written 50.\" permission of the OpenSSL Project. 51.\" 52.\" 6. Redistributions of any form whatsoever must retain the following 53.\" acknowledgment: 54.\" "This product includes software developed by the OpenSSL Project 55.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 56.\" 57.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 58.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 60.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 61.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 62.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 63.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 64.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 66.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 67.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 68.\" OF THE POSSIBILITY OF SUCH DAMAGE. 69.\" 70.Dd $Mdocdate: January 31 2024 $ 71.Dt OBJ_NID2OBJ 3 72.Os 73.Sh NAME 74.Nm OBJ_nid2obj , 75.Nm OBJ_nid2ln , 76.Nm OBJ_nid2sn , 77.Nm OBJ_obj2nid , 78.Nm OBJ_ln2nid , 79.Nm OBJ_sn2nid , 80.Nm OBJ_txt2nid , 81.Nm OBJ_txt2obj , 82.Nm OBJ_obj2txt , 83.Nm OBJ_cmp , 84.Nm OBJ_dup , 85.Nm i2t_ASN1_OBJECT , 86.Nm i2a_ASN1_OBJECT 87.Nd inspect and create ASN.1 object identifiers 88.Sh SYNOPSIS 89.In openssl/objects.h 90.Ft ASN1_OBJECT * 91.Fo OBJ_nid2obj 92.Fa "int nid" 93.Fc 94.Ft const char * 95.Fo OBJ_nid2ln 96.Fa "int nid" 97.Fc 98.Ft const char * 99.Fo OBJ_nid2sn 100.Fa "int nid" 101.Fc 102.Ft int 103.Fo OBJ_obj2nid 104.Fa "const ASN1_OBJECT *object" 105.Fc 106.Ft int 107.Fo OBJ_ln2nid 108.Fa "const char *ln" 109.Fc 110.Ft int 111.Fo OBJ_sn2nid 112.Fa "const char *sn" 113.Fc 114.Ft int 115.Fo OBJ_txt2nid 116.Fa "const char *s" 117.Fc 118.Ft ASN1_OBJECT * 119.Fo OBJ_txt2obj 120.Fa "const char *s" 121.Fa "int no_name" 122.Fc 123.Ft int 124.Fo OBJ_obj2txt 125.Fa "char *buf" 126.Fa "int buf_len" 127.Fa "const ASN1_OBJECT *object" 128.Fa "int no_name" 129.Fc 130.Ft int 131.Fo OBJ_cmp 132.Fa "const ASN1_OBJECT *a" 133.Fa "const ASN1_OBJECT *b" 134.Fc 135.Ft ASN1_OBJECT * 136.Fo OBJ_dup 137.Fa "const ASN1_OBJECT *object" 138.Fc 139.In openssl/asn1.h 140.Ft int 141.Fo i2t_ASN1_OBJECT 142.Fa "char *buf" 143.Fa "int buf_len" 144.Fa "const ASN1_OBJECT *object" 145.Fc 146.Ft int 147.Fo i2a_ASN1_OBJECT 148.Fa "BIO *out_bio" 149.Fa "const ASN1_OBJECT *object" 150.Fc 151.Sh DESCRIPTION 152The ASN.1 object utility functions process 153.Vt ASN1_OBJECT 154structures, in the following called 155.Dq objects . 156An object represents an ASN.1 157.Vt OBJECT IDENTIFIER 158.Pq OID . 159The library maintains an internal global table of objects. 160Many of these objects are built into the library 161and contained in the global table by default. 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. 168.Pp 169In addition to the OID, each object can hold 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 188and 189.Fn OBJ_nid2sn 190retrieve its long and short name, respectively. 191.Pp 192.Fn OBJ_obj2nid 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 201and 202.Fn OBJ_sn2nid 203retrieve the NID from the table object with the long name 204.Fa ln 205or the short name 206.Fa sn , 207respectively. 208.Pp 209.Fn OBJ_txt2nid 210retrieves the NID from the table object described by the text string 211.Fa s , 212which can be a long name, a short name, 213or the numerical representation of an OID. 214.Pp 215The remaining functions can be used both on table objects 216and on objects that are not in the global table: 217.Pp 218.Fn OBJ_txt2obj 219retrieves or creates an object matching the text string 220.Fa s . 221If 222.Fa no_name 223is 1, only the numerical representation of an OID is accepted. 224If 225.Fa no_name 226is 0, long names and short names are accepted as well. 227.Pp 228.Fn OBJ_obj2txt 229writes a NUL terminated textual representation 230of the OID contained in the given 231.Fa object 232into 233.Fa buf . 234At most 235.Fa buf_len 236bytes are written, truncating the result if necessary. 237The total amount of space required is returned. 238If 239.Fa no_name 240is 0 and the table object containing the same OID 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. 247.Pp 248.Fn i2t_ASN1_OBJECT 249is the same as 250.Fn OBJ_obj2txt 251with 252.Fa no_name 253set to 0. 254.Pp 255.Fn i2a_ASN1_OBJECT 256writes a textual representation of the OID contained in the given 257.Fa object 258to 259.Fa out_bio 260using 261.Xr BIO_write 3 . 262It does not write a terminating NUL byte. 263If the 264.Fa object 265argument is 266.Dv NULL 267or contains no OID, it writes the 4-byte string 268.Qq NULL . 269If 270.Fn i2t_ASN1_OBJECT 271fails, 272.Fn i2a_ASN1_OBJECT 273writes the 9-byte string 274.Qq <INVALID> . 275Otherwise, it writes the string constructed with 276.Fn i2t_ASN1_OBJECT . 277.Pp 278.Fn OBJ_cmp 279tests whether 280.Fa a 281and 282.Fa b 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. 287.Pp 288.Fn OBJ_dup 289returns a deep copy of the given 290.Fa object 291if it is marked as dynamically allocated. 292The new object and all data contained in it are marked as dynamically 293allocated. 294If the given 295.Fa object 296is not marked as dynamically allocated, 297.Fn OBJ_dup 298just returns a pointer to the 299.Fa object 300itself. 301.Sh RETURN VALUES 302Application code should treat all returned values \(em 303objects, names, and NIDs \(em as constants. 304.Pp 305.Fn OBJ_nid2obj 306returns a pointer to a table object owned by the library or 307.Dv NULL 308if no matching table object is found. 309.Pp 310.Fn OBJ_nid2ln 311and 312.Fn OBJ_nid2sn 313return a pointer to a string owned by a table object or 314.Dv NULL 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. 323.Pp 324.Fn OBJ_obj2nid 325returns an NID on success, or 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 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 343.Fn OBJ_txt2nid 344returns an NID on success or 345.Dv NID_undef 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. 361.Pp 362.Fn OBJ_obj2txt 363and 364.Fn i2t_ASN1_OBJECT 365return the amount of space required in bytes, 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. 395.Pp 396.Fn i2a_ASN1_OBJECT 397returns the number of bytes written, even if the given 398.Fa object 399is invalid or contains invalid data, 400but a negative value if memory allocation or a write operation fails. 401.Pp 402In some cases of failure of 403.Fn OBJ_nid2obj , 404.Fn OBJ_nid2ln , 405.Fn OBJ_nid2sn , 406.Fn OBJ_txt2nid , 407.Fn OBJ_txt2obj , 408.Fn OBJ_obj2txt , 409.Fn OBJ_dup , 410.Fn i2t_ASN1_OBJECT , 411and 412.Fn i2a_ASN1_OBJECT , 413the reason can be determined with 414.Xr ERR_get_error 3 . 415.Sh EXAMPLES 416Retrieve the object for 417.Sy commonName : 418.Bd -literal -offset indent 419ASN1_OBJECT *object; 420object = OBJ_nid2obj(NID_commonName); 421.Ed 422.Pp 423Check whether an object contains the OID for 424.Sy commonName : 425.Bd -literal -offset indent 426if (OBJ_obj2nid(object) == NID_commonName) 427 /* Do something */ 428.Ed 429.Pp 430Create a new object directly: 431.Bd -literal -offset indent 432object = OBJ_txt2obj("1.2.3.4", 1); 433.Ed 434.Sh SEE ALSO 435.Xr ASN1_OBJECT_new 3 , 436.Xr BIO_new 3 , 437.Xr d2i_ASN1_OBJECT 3 , 438.Xr OBJ_create 3 439.Sh HISTORY 440.Fn OBJ_nid2obj , 441.Fn OBJ_nid2ln , 442.Fn OBJ_nid2sn , 443.Fn OBJ_obj2nid , 444.Fn OBJ_ln2nid , 445.Fn OBJ_sn2nid , 446.Fn OBJ_txt2nid , 447.Fn OBJ_cmp , 448and 449.Fn OBJ_dup 450first appeared in SSLeay 0.5.1. 451.Fn i2a_ASN1_OBJECT 452first appeared in SSLeay 0.6.0, and 453.Fn i2t_ASN1_OBJECT 454in SSLeay 0.9.0. 455All these functions have been available since 456.Ox 2.4 . 457.Pp 458.Fn OBJ_txt2obj 459first appeared in OpenSSL 0.9.2b. 460.Fn OBJ_obj2txt 461first appeared in OpenSSL 0.9.4. 462Both functions have been available since 463.Ox 2.6 . 464.Sh CAVEATS 465The API contract of 466.Fn OBJ_txt2obj 467when called with a 468.Fa no_name 469argument of 0 and of 470.Fn OBJ_dup 471is scary in so far as the caller cannot find out from the returned 472object whether it is owned by the library or whether ownership was 473transferred to the caller. 474Consequently, it is best practice to assume that ownership of the object 475may have been transferred and call 476.Xr ASN1_OBJECT_free 3 477on the returned object when the caller no longer needs it. 478In case the library retained ownership of the returned object, 479.Xr ASN1_OBJECT_free 3 480has no effect and is harmless. 481.Pp 482Objects returned from 483.Fn OBJ_txt2obj 484with a 485.Fa no_name 486argument of 1 always require 487.Xr ASN1_OBJECT_free 3 488to prevent memory leaks. 489.Pp 490Objects returned from 491.Fn OBJ_nid2obj 492never require 493.Xr ASN1_OBJECT_free 3 , 494but calling it anyway has no effect and is harmless. 495.Sh BUGS 496Usually, an object is expected to contain an NID other than 497.Dv NID_undef 498if and only if it is a table object. 499However, this is not an invariant guaranteed by the API. 500In particular, 501.Xr ASN1_OBJECT_create 3 502allows the creation of non-table objects containing bogus NIDs. 503.Fn OBJ_obj2nid 504returns such bogus NIDs even though 505.Fn OBJ_nid2obj 506cannot use them for retrieval. 507On top of that, the global table contains one built-in object with an NID of 508.Dv NID_undef . 509.Pp 510.Fn OBJ_obj2txt 511is awkward and messy to use: it doesn't follow the convention of other 512OpenSSL functions where the buffer can be set to 513.Dv NULL 514to determine the amount of data that should be written. 515Instead 516.Fa buf 517must point to a valid buffer and 518.Fa buf_len 519should be set to a positive value. 520A buffer length of 80 should be more than enough to handle any OID 521encountered in practice. 522