1.\" $OpenBSD: OBJ_NAME_add.3,v 1.6 2024/01/31 08:02:53 tb Exp $ 2.\" 3.\" Copyright (c) 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: January 31 2024 $ 18.Dt OBJ_NAME_ADD 3 19.Os 20.Sh NAME 21.Nm OBJ_NAME_add , 22.Nm OBJ_NAME_remove , 23.Nm OBJ_NAME_get , 24.Nm OBJ_NAME_new_index , 25.Nm OBJ_NAME_init , 26.Nm OBJ_NAME_cleanup 27.Nd global associative array 28.Sh SYNOPSIS 29.In openssl/objects.h 30.Ft int 31.Fo OBJ_NAME_add 32.Fa "const char *name" 33.Fa "int type" 34.Fa "const char *value" 35.Fc 36.Ft int 37.Fo OBJ_NAME_remove 38.Fa "const char *name" 39.Fa "int type" 40.Fc 41.Ft const char * 42.Fo OBJ_NAME_get 43.Fa "const char *name" 44.Fa "int type" 45.Fc 46.Ft int 47.Fo OBJ_NAME_new_index 48.Fa "unsigned long (*hash_func)(const char *name)" 49.Fa "int (*cmp_func)(const char *name1, const char *name2)" 50.Fa "void (*free_func)(const char *name, int type, const char *value)" 51.Fc 52.Ft int 53.Fn OBJ_NAME_init void 54.Ft void 55.Fn OBJ_NAME_cleanup "int type" 56.Bd -literal 57typedef struct { 58 int type; 59 int alias; 60 const char *name; 61 const char *data; 62} OBJ_NAME; 63.Ed 64.Sh DESCRIPTION 65These functions implement a single, static associative array 66with the following properties: 67.Bl -bullet 68.It 69The keys are ordered pairs consisting of a NUL-terminated string 70.Pq called the Fa name 71and an 72.Vt int 73number 74.Pq called the Fa type . 75Two types are predefined and used internally by the library: 76.Dv OBJ_NAME_TYPE_MD_METH 77and 78.Dv OBJ_NAME_TYPE_CIPHER_METH . 79Two additional types are predefined but not used internally: 80.Dv OBJ_NAME_TYPE_PKEY_METH 81and 82.Dv OBJ_NAME_TYPE_COMP_METH . 83All predefined types are greater than 84.Dv OBJ_NAME_TYPE_UNDEF 85and smaller than 86.Dv OBJ_NAME_TYPE_NUM . 87.It 88The values are pointers. 89Formally, they are of the type 90.Vt const char * , 91but in practice, pointers of other types, for example 92.Vt EVP_CIPHER * 93or 94.Vt EVP_MD * , 95are often stored as values 96and cast back to the correct type on retrieval. 97.It 98The array supports type-specific aliases for names. 99.El 100.Pp 101.Fn OBJ_NAME_add 102removes the key-value pair or alias with the key 103.Pq Fa name , type 104in the same way as 105.Fn OBJ_NAME_remove 106and inserts a key-value pair with the specified 107.Fa name , 108.Fa type , 109and 110.Fa value . 111If the bit 112.Dv OBJ_NAME_ALIAS 113is set in the 114.Fa type 115argument, that bit is cleared before using the 116.Fa type 117and the key 118.Pq Fa name , type 119becomes an alias for the key 120.Pq Fa value , type 121instead of setting a value. 122It is not checked whether the key 123.Pq Fa value , type 124already exists. 125Consequently, it is possible to define an alias 126before setting the associated value. 127.Pp 128.Fn OBJ_NAME_remove 129removes the key-value pair or alias with the key 130.Pq Fa name , type 131from the array, if it exists. 132Otherwise, it has no effect. 133If the bit 134.Dv OBJ_NAME_ALIAS 135is set in the 136.Fa type 137argument, it is ignored and cleared before using the 138.Fa type . 139If the 140.Fa type 141is an application-defined type added with 142.Fn OBJ_NAME_new_index 143and the 144.Fa free_func 145associated with the 146.Fa type 147is not a 148.Dv NULL 149pointer, it is called with the 150.Fa name , 151.Fa type , 152and 153.Fa value 154of the key-value pair being removed or with the 155.Fa name , 156.Fa type , 157and alias target name of the alias being removed. 158In typical usage, this function might free the 159.Fa name , 160and it might free the 161.Fa value 162in a type-specific way. 163.Pp 164.Fn OBJ_NAME_get 165looks up the key 166.Pq Fa name , type , 167recursively resolving up to ten aliases if needed. 168If the bit 169.Dv OBJ_NAME_ALIAS 170is set in the 171.Fa type 172argument, it is cleared before using the 173.Fa type , 174processing of aliases is disabled, and if 175.Pq Fa name , type 176is an alias, the target name of the alias is returned instead of a value. 177.Pp 178.Fn OBJ_NAME_new_index 179assigns the smallest unassigned positive integer number 180to represent a new, application-defined 181.Fa type . 182The three function pointers will be used, respectively, 183to hash a name for this type, to compare two names for this type, 184and to free the contents of a key-value pair holding the given 185.Fa name , 186.Fa type , 187and 188.Fa value . 189If the 190.Fa hash_func 191argument is a 192.Dv NULL 193pointer, 194.Xr lh_strhash 3 195is used instead. 196If the 197.Fa cmp_func 198argument is a 199.Dv NULL 200pointer, 201.Xr strcmp 3 202is used instead. 203If the 204.Fa free_func 205argument is a 206.Dv NULL 207pointer, the 208.Fa name 209and 210.Fa value 211pointers contained in the key-value pair are not freed, 212only the structure representing the pair itself is. 213This default behaviour is also used for the built-in types. 214.Pp 215.Fn OBJ_NAME_init 216initializes the array. 217After initialization, the array is empty. 218Calling 219.Fn OBJ_NAME_init 220when the array is already initialized has no effect. 221Application programs do not need to call this function because 222.Fn OBJ_NAME_add 223and 224.Fn OBJ_NAME_get 225automatically call it whenever needed. 226.Pp 227.Fn OBJ_NAME_cleanup 228removes all key-value pairs and aliases of the given 229.Fa type 230from the array by calling 231.Fn OBJ_NAME_remove 232on every such pair and alias. 233If the 234.Fa type 235argument is negative, it removes all key-value pairs and aliases 236of any type and also reverses all effects of 237.Fn OBJ_NAME_new_index 238and 239.Fn OBJ_NAME_init , 240in particular resetting the list of types to the predefined types 241and releasing all memory reserved by these functions. 242.Pp 243The 244.Vt OBJ_NAME 245structure represents one key-value pair or one alias with the key 246.Pq Fa name , type . 247If the 248.Fa alias 249field is 0, the 250.Fa data 251field contains the value; otherwise, it contains the alias target name. 252.Sh RETURN VALUES 253.Fn OBJ_NAME_add 254and 255.Fn OBJ_NAME_init 256return 1 on success or 0 if memory allocation fails. 257.Pp 258.Fn OBJ_NAME_remove 259returns 1 if one key-value pair or alias was removed or 0 otherwise. 260.Pp 261.Fn OBJ_NAME_get 262returns the 263.Fa value 264associated with the key 265.Pq Fa name , type 266or 267.Dv NULL 268if 269.Fa name 270is 271.Dv NULL , 272if the array does not contain a value for this key, 273or if more than ten aliases are encountered before finding a value. 274.Pp 275.Fn OBJ_NAME_new_index 276returns a positive integer greater than or equal to 277.Dv OBJ_NAME_TYPE_NUM 278representing the new type or 0 if memory allocation fails. 279.Sh SEE ALSO 280.Xr EVP_cleanup 3 , 281.Xr EVP_get_cipherbyname 3 , 282.Xr EVP_get_digestbyname 3 , 283.Xr lh_new 3 , 284.Xr OBJ_create 3 , 285.Xr OBJ_nid2obj 3 286.Sh BUGS 287Calling 288.Fn OBJ_NAME_get 289with the bit 290.Dv OBJ_NAME_ALIAS 291is not very useful because there is no way to tell 292whether the returned pointer points to a value or to a name, 293short of calling the function again without setting the bit 294and comparing the two returned pointers. 295.Pp 296The 297.Fa free_func 298has no way to tell whether its 299.Fa value 300argument is indeed of the given 301.Fa type 302or whether it is merely the target name of an alias. 303Consequently, to use values of a type 304that requires more cleanup than merely calling 305.Xr free 3 306on it, instances of the type need to begin with a magic number or string 307that cannot occur at the beginning of a name. 308