1 /* $OpenBSD: x_x509.c,v 1.35 2023/04/28 16:30:14 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/opensslconf.h> 62 63 #include <openssl/asn1t.h> 64 #include <openssl/evp.h> 65 #include <openssl/x509.h> 66 #include <openssl/x509v3.h> 67 68 #include "x509_local.h" 69 70 static const ASN1_AUX X509_CINF_aux = { 71 .flags = ASN1_AFLG_ENCODING, 72 .enc_offset = offsetof(X509_CINF, enc), 73 }; 74 static const ASN1_TEMPLATE X509_CINF_seq_tt[] = { 75 { 76 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL, 77 .offset = offsetof(X509_CINF, version), 78 .field_name = "version", 79 .item = &ASN1_INTEGER_it, 80 }, 81 { 82 .offset = offsetof(X509_CINF, serialNumber), 83 .field_name = "serialNumber", 84 .item = &ASN1_INTEGER_it, 85 }, 86 { 87 .offset = offsetof(X509_CINF, signature), 88 .field_name = "signature", 89 .item = &X509_ALGOR_it, 90 }, 91 { 92 .offset = offsetof(X509_CINF, issuer), 93 .field_name = "issuer", 94 .item = &X509_NAME_it, 95 }, 96 { 97 .offset = offsetof(X509_CINF, validity), 98 .field_name = "validity", 99 .item = &X509_VAL_it, 100 }, 101 { 102 .offset = offsetof(X509_CINF, subject), 103 .field_name = "subject", 104 .item = &X509_NAME_it, 105 }, 106 { 107 .offset = offsetof(X509_CINF, key), 108 .field_name = "key", 109 .item = &X509_PUBKEY_it, 110 }, 111 { 112 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL, 113 .tag = 1, 114 .offset = offsetof(X509_CINF, issuerUID), 115 .field_name = "issuerUID", 116 .item = &ASN1_BIT_STRING_it, 117 }, 118 { 119 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL, 120 .tag = 2, 121 .offset = offsetof(X509_CINF, subjectUID), 122 .field_name = "subjectUID", 123 .item = &ASN1_BIT_STRING_it, 124 }, 125 { 126 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_SEQUENCE_OF | 127 ASN1_TFLG_OPTIONAL, 128 .tag = 3, 129 .offset = offsetof(X509_CINF, extensions), 130 .field_name = "extensions", 131 .item = &X509_EXTENSION_it, 132 }, 133 }; 134 135 const ASN1_ITEM X509_CINF_it = { 136 .itype = ASN1_ITYPE_SEQUENCE, 137 .utype = V_ASN1_SEQUENCE, 138 .templates = X509_CINF_seq_tt, 139 .tcount = sizeof(X509_CINF_seq_tt) / sizeof(ASN1_TEMPLATE), 140 .funcs = &X509_CINF_aux, 141 .size = sizeof(X509_CINF), 142 .sname = "X509_CINF", 143 }; 144 145 146 X509_CINF * 147 d2i_X509_CINF(X509_CINF **a, const unsigned char **in, long len) 148 { 149 return (X509_CINF *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 150 &X509_CINF_it); 151 } 152 153 int 154 i2d_X509_CINF(X509_CINF *a, unsigned char **out) 155 { 156 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_CINF_it); 157 } 158 159 X509_CINF * 160 X509_CINF_new(void) 161 { 162 return (X509_CINF *)ASN1_item_new(&X509_CINF_it); 163 } 164 165 void 166 X509_CINF_free(X509_CINF *a) 167 { 168 ASN1_item_free((ASN1_VALUE *)a, &X509_CINF_it); 169 } 170 /* X509 top level structure needs a bit of customisation */ 171 172 static int 173 x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg) 174 { 175 X509 *ret = (X509 *)*pval; 176 177 switch (operation) { 178 179 case ASN1_OP_NEW_POST: 180 ret->valid = 0; 181 ret->name = NULL; 182 ret->ex_flags = 0; 183 ret->ex_pathlen = -1; 184 ret->skid = NULL; 185 ret->akid = NULL; 186 ret->aux = NULL; 187 ret->crldp = NULL; 188 #ifndef OPENSSL_NO_RFC3779 189 ret->rfc3779_addr = NULL; 190 ret->rfc3779_asid = NULL; 191 #endif 192 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); 193 break; 194 195 case ASN1_OP_D2I_POST: 196 free(ret->name); 197 ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0); 198 break; 199 200 case ASN1_OP_FREE_POST: 201 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); 202 X509_CERT_AUX_free(ret->aux); 203 ASN1_OCTET_STRING_free(ret->skid); 204 AUTHORITY_KEYID_free(ret->akid); 205 CRL_DIST_POINTS_free(ret->crldp); 206 GENERAL_NAMES_free(ret->altname); 207 NAME_CONSTRAINTS_free(ret->nc); 208 #ifndef OPENSSL_NO_RFC3779 209 sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free); 210 ASIdentifiers_free(ret->rfc3779_asid); 211 #endif 212 free(ret->name); 213 ret->name = NULL; 214 break; 215 } 216 217 return 1; 218 } 219 220 static const ASN1_AUX X509_aux = { 221 .app_data = NULL, 222 .flags = ASN1_AFLG_REFCOUNT, 223 .ref_offset = offsetof(X509, references), 224 .ref_lock = CRYPTO_LOCK_X509, 225 .asn1_cb = x509_cb, 226 }; 227 static const ASN1_TEMPLATE X509_seq_tt[] = { 228 { 229 .offset = offsetof(X509, cert_info), 230 .field_name = "cert_info", 231 .item = &X509_CINF_it, 232 }, 233 { 234 .offset = offsetof(X509, sig_alg), 235 .field_name = "sig_alg", 236 .item = &X509_ALGOR_it, 237 }, 238 { 239 .offset = offsetof(X509, signature), 240 .field_name = "signature", 241 .item = &ASN1_BIT_STRING_it, 242 }, 243 }; 244 245 const ASN1_ITEM X509_it = { 246 .itype = ASN1_ITYPE_SEQUENCE, 247 .utype = V_ASN1_SEQUENCE, 248 .templates = X509_seq_tt, 249 .tcount = sizeof(X509_seq_tt) / sizeof(ASN1_TEMPLATE), 250 .funcs = &X509_aux, 251 .size = sizeof(X509), 252 .sname = "X509", 253 }; 254 255 256 X509 * 257 d2i_X509(X509 **a, const unsigned char **in, long len) 258 { 259 return (X509 *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 260 &X509_it); 261 } 262 263 int 264 i2d_X509(X509 *a, unsigned char **out) 265 { 266 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_it); 267 } 268 269 X509 * 270 X509_new(void) 271 { 272 return (X509 *)ASN1_item_new(&X509_it); 273 } 274 275 void 276 X509_free(X509 *a) 277 { 278 ASN1_item_free((ASN1_VALUE *)a, &X509_it); 279 } 280 281 X509 * 282 X509_dup(X509 *x) 283 { 284 return ASN1_item_dup(&X509_it, x); 285 } 286 287 int 288 X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 289 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 290 { 291 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509, argl, argp, 292 new_func, dup_func, free_func); 293 } 294 295 int 296 X509_set_ex_data(X509 *r, int idx, void *arg) 297 { 298 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg)); 299 } 300 301 void * 302 X509_get_ex_data(X509 *r, int idx) 303 { 304 return (CRYPTO_get_ex_data(&r->ex_data, idx)); 305 } 306 307 /* X509_AUX ASN1 routines. X509_AUX is the name given to 308 * a certificate with extra info tagged on the end. Since these 309 * functions set how a certificate is trusted they should only 310 * be used when the certificate comes from a reliable source 311 * such as local storage. 312 * 313 */ 314 315 X509 * 316 d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) 317 { 318 const unsigned char *q; 319 X509 *ret; 320 321 /* Save start position */ 322 q = *pp; 323 ret = d2i_X509(NULL, pp, length); 324 /* If certificate unreadable then forget it */ 325 if (!ret) 326 return NULL; 327 /* update length */ 328 length -= *pp - q; 329 if (length > 0) { 330 if (!d2i_X509_CERT_AUX(&ret->aux, pp, length)) 331 goto err; 332 } 333 if (a != NULL) { 334 X509_free(*a); 335 *a = ret; 336 } 337 return ret; 338 339 err: 340 X509_free(ret); 341 return NULL; 342 } 343 344 int 345 i2d_X509_AUX(X509 *a, unsigned char **pp) 346 { 347 int length; 348 349 length = i2d_X509(a, pp); 350 if (a) 351 length += i2d_X509_CERT_AUX(a->aux, pp); 352 return length; 353 } 354 355 int 356 i2d_re_X509_tbs(X509 *x, unsigned char **pp) 357 { 358 x->cert_info->enc.modified = 1; 359 return i2d_X509_CINF(x->cert_info, pp); 360 } 361 362 void 363 X509_get0_signature(const ASN1_BIT_STRING **psig, const X509_ALGOR **palg, 364 const X509 *x) 365 { 366 if (psig != NULL) 367 *psig = x->signature; 368 if (palg != NULL) 369 *palg = x->sig_alg; 370 } 371 372 int 373 X509_get_signature_nid(const X509 *x) 374 { 375 return OBJ_obj2nid(x->sig_alg->algorithm); 376 } 377