obj.h (9472B)
1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef OPENSSL_HEADER_OBJ_H 16 #define OPENSSL_HEADER_OBJ_H 17 18 #include <openssl/base.h> // IWYU pragma: export 19 20 #include <openssl/bytestring.h> 21 #include <openssl/nid.h> // IWYU pragma: export 22 23 #if defined(__cplusplus) 24 extern "C" { 25 #endif 26 27 28 // The objects library deals with the registration and indexing of ASN.1 object 29 // identifiers. These values are often written as a dotted sequence of numbers, 30 // e.g. 1.2.840.113549.1.9.16.3.9. 31 // 32 // Internally, OpenSSL likes to deal with these values by numbering them with 33 // numbers called "nids". OpenSSL has a large, built-in database of common 34 // object identifiers and also has both short and long names for them. 35 // 36 // This library provides functions for translating between object identifiers, 37 // nids, short names and long names. 38 // 39 // The nid values should not be used outside of a single process: they are not 40 // stable identifiers. 41 42 43 // Basic operations. 44 45 // OBJ_dup returns a duplicate copy of |obj| or NULL on allocation failure. The 46 // caller must call |ASN1_OBJECT_free| on the result to release it. 47 OPENSSL_EXPORT ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *obj); 48 49 // OBJ_cmp returns a value less than, equal to or greater than zero if |a| is 50 // less than, equal to or greater than |b|, respectively. 51 OPENSSL_EXPORT int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b); 52 53 // OBJ_get0_data returns a pointer to the DER representation of |obj|. This is 54 // the contents of the DER-encoded identifier, not including the tag and length. 55 // If |obj| does not have an associated object identifier (i.e. it is a nid-only 56 // value), this value is the empty string. 57 OPENSSL_EXPORT const uint8_t *OBJ_get0_data(const ASN1_OBJECT *obj); 58 59 // OBJ_length returns the length of the DER representation of |obj|. This is the 60 // contents of the DER-encoded identifier, not including the tag and length. If 61 // |obj| does not have an associated object identifier (i.e. it is a nid-only 62 // value), this value is the empty string. 63 OPENSSL_EXPORT size_t OBJ_length(const ASN1_OBJECT *obj); 64 65 66 // Looking up nids. 67 68 // OBJ_obj2nid returns the nid corresponding to |obj|, or |NID_undef| if no 69 // such object is known. 70 OPENSSL_EXPORT int OBJ_obj2nid(const ASN1_OBJECT *obj); 71 72 // OBJ_cbs2nid returns the nid corresponding to the DER data in |cbs|, or 73 // |NID_undef| if no such object is known. 74 OPENSSL_EXPORT int OBJ_cbs2nid(const CBS *cbs); 75 76 // OBJ_sn2nid returns the nid corresponding to |short_name|, or |NID_undef| if 77 // no such short name is known. 78 OPENSSL_EXPORT int OBJ_sn2nid(const char *short_name); 79 80 // OBJ_ln2nid returns the nid corresponding to |long_name|, or |NID_undef| if 81 // no such long name is known. 82 OPENSSL_EXPORT int OBJ_ln2nid(const char *long_name); 83 84 // OBJ_txt2nid returns the nid corresponding to |s|, which may be a short name, 85 // long name, or an ASCII string containing a dotted sequence of numbers. It 86 // returns the nid or NID_undef if unknown. 87 OPENSSL_EXPORT int OBJ_txt2nid(const char *s); 88 89 90 // Getting information about nids. 91 92 // OBJ_nid2obj returns the |ASN1_OBJECT| corresponding to |nid|, or NULL if 93 // |nid| is unknown. 94 // 95 // Although the output is not const, this function returns a static, immutable 96 // |ASN1_OBJECT|. It is not necessary to release the object with 97 // |ASN1_OBJECT_free|. 98 // 99 // However, functions like |X509_ALGOR_set0| expect to take ownership of a 100 // possibly dynamically-allocated |ASN1_OBJECT|. |ASN1_OBJECT_free| is a no-op 101 // for static |ASN1_OBJECT|s, so |OBJ_nid2obj| is compatible with such 102 // functions. 103 // 104 // Callers are encouraged to store the result of this function in a const 105 // pointer. However, if using functions like |X509_ALGOR_set0|, callers may use 106 // a non-const pointer and manage ownership. 107 OPENSSL_EXPORT ASN1_OBJECT *OBJ_nid2obj(int nid); 108 109 // OBJ_get_undef returns the object for |NID_undef|. Prefer this function over 110 // |OBJ_nid2obj| to avoid pulling in the full OID table. 111 OPENSSL_EXPORT const ASN1_OBJECT *OBJ_get_undef(void); 112 113 // OBJ_nid2sn returns the short name for |nid|, or NULL if |nid| is unknown. 114 OPENSSL_EXPORT const char *OBJ_nid2sn(int nid); 115 116 // OBJ_nid2ln returns the long name for |nid|, or NULL if |nid| is unknown. 117 OPENSSL_EXPORT const char *OBJ_nid2ln(int nid); 118 119 // OBJ_nid2cbb writes |nid| as an ASN.1 OBJECT IDENTIFIER to |out|. It returns 120 // one on success or zero otherwise. 121 OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid); 122 123 124 // Dealing with textual representations of object identifiers. 125 126 // OBJ_txt2obj returns an ASN1_OBJECT for the textual representation in |s|. 127 // If |dont_search_names| is zero, then |s| will be matched against the long 128 // and short names of a known objects to find a match. Otherwise |s| must 129 // contain an ASCII string with a dotted sequence of numbers. The resulting 130 // object need not be previously known. It returns a freshly allocated 131 // |ASN1_OBJECT| or NULL on error. 132 OPENSSL_EXPORT ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names); 133 134 // OBJ_obj2txt converts |obj| to a textual representation. If 135 // |always_return_oid| is zero then |obj| will be matched against known objects 136 // and the long (preferably) or short name will be used if found. Otherwise 137 // |obj| will be converted into a dotted sequence of integers. If |out| is not 138 // NULL, then at most |out_len| bytes of the textual form will be written 139 // there. If |out_len| is at least one, then string written to |out| will 140 // always be NUL terminated. It returns the number of characters that could 141 // have been written, not including the final NUL, or -1 on error. 142 OPENSSL_EXPORT int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, 143 int always_return_oid); 144 145 146 // Adding objects at runtime. 147 148 // OBJ_create adds a known object and returns the NID of the new object, or 149 // NID_undef on error. 150 // 151 // WARNING: This function modifies global state. The table cannot contain 152 // duplicate OIDs, short names, or long names. If two callers in the same 153 // address space add conflicting values, only one registration will take effect. 154 // Avoid this function if possible. Instead, callers can process OIDs unknown to 155 // BoringSSL by acting on the byte representation directly. See 156 // |ASN1_OBJECT_create|, |OBJ_get0_data|, and |OBJ_length|. 157 OPENSSL_EXPORT int OBJ_create(const char *oid, const char *short_name, 158 const char *long_name); 159 160 161 // Handling signature algorithm identifiers. 162 // 163 // Some NIDs (e.g. sha256WithRSAEncryption) specify both a digest algorithm and 164 // a public key algorithm. The following functions map between pairs of digest 165 // and public-key algorithms and the NIDs that specify their combination. 166 // 167 // Sometimes the combination NID leaves the digest unspecified (e.g. 168 // rsassaPss). In these cases, the digest NID is |NID_undef|. 169 170 // OBJ_find_sigid_algs finds the digest and public-key NIDs that correspond to 171 // the signing algorithm |sign_nid|. If successful, it sets |*out_digest_nid| 172 // and |*out_pkey_nid| and returns one. Otherwise it returns zero. Any of 173 // |out_digest_nid| or |out_pkey_nid| can be NULL if the caller doesn't need 174 // that output value. 175 OPENSSL_EXPORT int OBJ_find_sigid_algs(int sign_nid, int *out_digest_nid, 176 int *out_pkey_nid); 177 178 // OBJ_find_sigid_by_algs finds the signature NID that corresponds to the 179 // combination of |digest_nid| and |pkey_nid|. If success, it sets 180 // |*out_sign_nid| and returns one. Otherwise it returns zero. The 181 // |out_sign_nid| argument can be NULL if the caller only wishes to learn 182 // whether the combination is valid. 183 OPENSSL_EXPORT int OBJ_find_sigid_by_algs(int *out_sign_nid, int digest_nid, 184 int pkey_nid); 185 186 187 // Deprecated functions. 188 189 typedef struct obj_name_st { 190 int type; 191 int alias; 192 const char *name; 193 const char *data; 194 } OBJ_NAME; 195 196 #define OBJ_NAME_TYPE_MD_METH 1 197 #define OBJ_NAME_TYPE_CIPHER_METH 2 198 199 // OBJ_NAME_do_all_sorted calls |callback| zero or more times, each time with 200 // the name of a different primitive. If |type| is |OBJ_NAME_TYPE_MD_METH| then 201 // the primitives will be hash functions, alternatively if |type| is 202 // |OBJ_NAME_TYPE_CIPHER_METH| then the primitives will be ciphers or cipher 203 // modes. 204 // 205 // This function is ill-specified and should never be used. 206 OPENSSL_EXPORT void OBJ_NAME_do_all_sorted( 207 int type, void (*callback)(const OBJ_NAME *, void *arg), void *arg); 208 209 // OBJ_NAME_do_all calls |OBJ_NAME_do_all_sorted|. 210 OPENSSL_EXPORT void OBJ_NAME_do_all(int type, void (*callback)(const OBJ_NAME *, 211 void *arg), 212 void *arg); 213 214 // OBJ_cleanup does nothing. 215 OPENSSL_EXPORT void OBJ_cleanup(void); 216 217 218 #if defined(__cplusplus) 219 } // extern C 220 #endif 221 222 #define OBJ_R_UNKNOWN_NID 100 223 #define OBJ_R_INVALID_OID_STRING 101 224 225 #endif // OPENSSL_HEADER_OBJ_H