| 1 |
/* |
| 2 |
* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include "internal/cryptlib.h" |
| 12 |
#include <openssl/evp.h> |
| 13 |
#include <openssl/asn1t.h> |
| 14 |
#include <openssl/x509.h> |
| 15 |
#include <openssl/x509v3.h> |
| 16 |
#include "crypto/x509.h" |
| 17 |
|
| 18 |
ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = { |
| 19 |
ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0), |
| 20 |
ASN1_EMBED(X509_CINF, serialNumber, ASN1_INTEGER), |
| 21 |
ASN1_EMBED(X509_CINF, signature, X509_ALGOR), |
| 22 |
ASN1_SIMPLE(X509_CINF, issuer, X509_NAME), |
| 23 |
ASN1_EMBED(X509_CINF, validity, X509_VAL), |
| 24 |
ASN1_SIMPLE(X509_CINF, subject, X509_NAME), |
| 25 |
ASN1_SIMPLE(X509_CINF, key, X509_PUBKEY), |
| 26 |
ASN1_IMP_OPT(X509_CINF, issuerUID, ASN1_BIT_STRING, 1), |
| 27 |
ASN1_IMP_OPT(X509_CINF, subjectUID, ASN1_BIT_STRING, 2), |
| 28 |
ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF, extensions, X509_EXTENSION, 3) |
| 29 |
} ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF) |
| 30 |
|
| 31 |
IMPLEMENT_ASN1_FUNCTIONS(X509_CINF) |
| 32 |
/* X509 top level structure needs a bit of customisation */ |
| 33 |
|
| 34 |
extern void ossl_policy_cache_free(X509_POLICY_CACHE *cache); |
| 35 |
|
| 36 |
static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 37 |
void *exarg) |
| 38 |
{ |
| 39 |
X509 *ret = (X509 *)*pval; |
| 40 |
|
| 41 |
switch (operation) { |
| 42 |
|
| 43 |
case ASN1_OP_D2I_PRE: |
| 44 |
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); |
| 45 |
X509_CERT_AUX_free(ret->aux); |
| 46 |
ASN1_OCTET_STRING_free(ret->skid); |
| 47 |
AUTHORITY_KEYID_free(ret->akid); |
| 48 |
CRL_DIST_POINTS_free(ret->crldp); |
| 49 |
ossl_policy_cache_free(ret->policy_cache); |
| 50 |
GENERAL_NAMES_free(ret->altname); |
| 51 |
NAME_CONSTRAINTS_free(ret->nc); |
| 52 |
#ifndef OPENSSL_NO_RFC3779 |
| 53 |
sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free); |
| 54 |
ASIdentifiers_free(ret->rfc3779_asid); |
| 55 |
#endif |
| 56 |
ASN1_OCTET_STRING_free(ret->distinguishing_id); |
| 57 |
|
| 58 |
/* fall through */ |
| 59 |
|
| 60 |
case ASN1_OP_NEW_POST: |
| 61 |
ret->ex_cached = 0; |
| 62 |
ret->ex_kusage = 0; |
| 63 |
ret->ex_xkusage = 0; |
| 64 |
ret->ex_nscert = 0; |
| 65 |
ret->ex_flags = 0; |
| 66 |
ret->ex_pathlen = -1; |
| 67 |
ret->ex_pcpathlen = -1; |
| 68 |
ret->skid = NULL; |
| 69 |
ret->akid = NULL; |
| 70 |
ret->policy_cache = NULL; |
| 71 |
ret->altname = NULL; |
| 72 |
ret->nc = NULL; |
| 73 |
#ifndef OPENSSL_NO_RFC3779 |
| 74 |
ret->rfc3779_addr = NULL; |
| 75 |
ret->rfc3779_asid = NULL; |
| 76 |
#endif |
| 77 |
ret->distinguishing_id = NULL; |
| 78 |
ret->aux = NULL; |
| 79 |
ret->crldp = NULL; |
| 80 |
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data)) |
| 81 |
return 0; |
| 82 |
break; |
| 83 |
|
| 84 |
case ASN1_OP_FREE_POST: |
| 85 |
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); |
| 86 |
X509_CERT_AUX_free(ret->aux); |
| 87 |
ASN1_OCTET_STRING_free(ret->skid); |
| 88 |
AUTHORITY_KEYID_free(ret->akid); |
| 89 |
CRL_DIST_POINTS_free(ret->crldp); |
| 90 |
ossl_policy_cache_free(ret->policy_cache); |
| 91 |
GENERAL_NAMES_free(ret->altname); |
| 92 |
NAME_CONSTRAINTS_free(ret->nc); |
| 93 |
#ifndef OPENSSL_NO_RFC3779 |
| 94 |
sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free); |
| 95 |
ASIdentifiers_free(ret->rfc3779_asid); |
| 96 |
#endif |
| 97 |
ASN1_OCTET_STRING_free(ret->distinguishing_id); |
| 98 |
OPENSSL_free(ret->propq); |
| 99 |
break; |
| 100 |
|
| 101 |
case ASN1_OP_DUP_POST: { |
| 102 |
X509 *old = exarg; |
| 103 |
|
| 104 |
if (!ossl_x509_set0_libctx(ret, old->libctx, old->propq)) |
| 105 |
return 0; |
| 106 |
} break; |
| 107 |
case ASN1_OP_GET0_LIBCTX: { |
| 108 |
OSSL_LIB_CTX **libctx = exarg; |
| 109 |
|
| 110 |
*libctx = ret->libctx; |
| 111 |
} break; |
| 112 |
|
| 113 |
case ASN1_OP_GET0_PROPQ: { |
| 114 |
const char **propq = exarg; |
| 115 |
|
| 116 |
*propq = ret->propq; |
| 117 |
} break; |
| 118 |
|
| 119 |
default: |
| 120 |
break; |
| 121 |
} |
| 122 |
|
| 123 |
return 1; |
| 124 |
} |
| 125 |
|
| 126 |
ASN1_SEQUENCE_ref(X509, x509_cb) = { |
| 127 |
ASN1_EMBED(X509, cert_info, X509_CINF), |
| 128 |
ASN1_EMBED(X509, sig_alg, X509_ALGOR), |
| 129 |
ASN1_EMBED(X509, signature, ASN1_BIT_STRING) |
| 130 |
} ASN1_SEQUENCE_END_ref(X509, X509) |
| 131 |
|
| 132 |
IMPLEMENT_ASN1_FUNCTIONS(X509) |
| 133 |
IMPLEMENT_ASN1_DUP_FUNCTION(X509) |
| 134 |
|
| 135 |
/* |
| 136 |
* This should only be used if the X509 object was embedded inside another |
| 137 |
* asn1 object and it needs a libctx to operate. |
| 138 |
* Use X509_new_ex() instead if possible. |
| 139 |
*/ |
| 140 |
int ossl_x509_set0_libctx(X509 *x, OSSL_LIB_CTX *libctx, const char *propq) |
| 141 |
{ |
| 142 |
if (x != NULL) { |
| 143 |
x->libctx = libctx; |
| 144 |
OPENSSL_free(x->propq); |
| 145 |
x->propq = NULL; |
| 146 |
if (propq != NULL) { |
| 147 |
x->propq = OPENSSL_strdup(propq); |
| 148 |
if (x->propq == NULL) |
| 149 |
return 0; |
| 150 |
} |
| 151 |
} |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
X509 *X509_new_ex(OSSL_LIB_CTX *libctx, const char *propq) |
| 156 |
{ |
| 157 |
X509 *cert = NULL; |
| 158 |
|
| 159 |
cert = (X509 *)ASN1_item_new_ex(X509_it(), libctx, propq); |
| 160 |
if (!ossl_x509_set0_libctx(cert, libctx, propq)) { |
| 161 |
X509_free(cert); |
| 162 |
cert = NULL; |
| 163 |
} |
| 164 |
return cert; |
| 165 |
} |
| 166 |
|
| 167 |
int X509_set_ex_data(X509 *r, int idx, void *arg) |
| 168 |
{ |
| 169 |
return CRYPTO_set_ex_data(&r->ex_data, idx, arg); |
| 170 |
} |
| 171 |
|
| 172 |
void *X509_get_ex_data(const X509 *r, int idx) |
| 173 |
{ |
| 174 |
return CRYPTO_get_ex_data(&r->ex_data, idx); |
| 175 |
} |
| 176 |
|
| 177 |
/* |
| 178 |
* X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with |
| 179 |
* extra info tagged on the end. Since these functions set how a certificate |
| 180 |
* is trusted they should only be used when the certificate comes from a |
| 181 |
* reliable source such as local storage. |
| 182 |
*/ |
| 183 |
|
| 184 |
X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) |
| 185 |
{ |
| 186 |
const unsigned char *q; |
| 187 |
X509 *ret; |
| 188 |
int freeret = 0; |
| 189 |
|
| 190 |
/* Save start position */ |
| 191 |
q = *pp; |
| 192 |
|
| 193 |
if (a == NULL || *a == NULL) |
| 194 |
freeret = 1; |
| 195 |
ret = d2i_X509(a, &q, length); |
| 196 |
/* If certificate unreadable then forget it */ |
| 197 |
if (ret == NULL) |
| 198 |
return NULL; |
| 199 |
/* update length */ |
| 200 |
length -= (long)(q - *pp); |
| 201 |
if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length)) |
| 202 |
goto err; |
| 203 |
*pp = q; |
| 204 |
return ret; |
| 205 |
err: |
| 206 |
if (freeret) { |
| 207 |
X509_free(ret); |
| 208 |
if (a) |
| 209 |
*a = NULL; |
| 210 |
} |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
* Serialize trusted certificate to *pp or just return the required buffer |
| 216 |
* length if pp == NULL. We ultimately want to avoid modifying *pp in the |
| 217 |
* error path, but that depends on similar hygiene in lower-level functions. |
| 218 |
* Here we avoid compounding the problem. |
| 219 |
*/ |
| 220 |
static int i2d_x509_aux_internal(const X509 *a, unsigned char **pp) |
| 221 |
{ |
| 222 |
int length, tmplen; |
| 223 |
unsigned char *start = pp != NULL ? *pp : NULL; |
| 224 |
|
| 225 |
/* |
| 226 |
* This might perturb *pp on error, but fixing that belongs in i2d_X509() |
| 227 |
* not here. It should be that if a == NULL length is zero, but we check |
| 228 |
* both just in case. |
| 229 |
*/ |
| 230 |
length = i2d_X509(a, pp); |
| 231 |
if (length <= 0 || a == NULL) |
| 232 |
return length; |
| 233 |
|
| 234 |
tmplen = i2d_X509_CERT_AUX(a->aux, pp); |
| 235 |
if (tmplen < 0) { |
| 236 |
if (start != NULL) |
| 237 |
*pp = start; |
| 238 |
return tmplen; |
| 239 |
} |
| 240 |
length += tmplen; |
| 241 |
|
| 242 |
return length; |
| 243 |
} |
| 244 |
|
| 245 |
/* |
| 246 |
* Serialize trusted certificate to *pp, or just return the required buffer |
| 247 |
* length if pp == NULL. |
| 248 |
* |
| 249 |
* When pp is not NULL, but *pp == NULL, we allocate the buffer, but since |
| 250 |
* we're writing two ASN.1 objects back to back, we can't have i2d_X509() do |
| 251 |
* the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the |
| 252 |
* allocated buffer. |
| 253 |
*/ |
| 254 |
int i2d_X509_AUX(const X509 *a, unsigned char **pp) |
| 255 |
{ |
| 256 |
int length; |
| 257 |
unsigned char *tmp; |
| 258 |
|
| 259 |
/* Buffer provided by caller */ |
| 260 |
if (pp == NULL || *pp != NULL) |
| 261 |
return i2d_x509_aux_internal(a, pp); |
| 262 |
|
| 263 |
/* Obtain the combined length */ |
| 264 |
if ((length = i2d_x509_aux_internal(a, NULL)) <= 0) |
| 265 |
return length; |
| 266 |
|
| 267 |
/* Allocate requisite combined storage */ |
| 268 |
*pp = tmp = OPENSSL_malloc(length); |
| 269 |
if (tmp == NULL) |
| 270 |
return -1; |
| 271 |
|
| 272 |
/* Encode, but keep *pp at the originally malloced pointer */ |
| 273 |
length = i2d_x509_aux_internal(a, &tmp); |
| 274 |
if (length <= 0) { |
| 275 |
OPENSSL_free(*pp); |
| 276 |
*pp = NULL; |
| 277 |
} |
| 278 |
return length; |
| 279 |
} |
| 280 |
|
| 281 |
int i2d_re_X509_tbs(X509 *x, unsigned char **pp) |
| 282 |
{ |
| 283 |
x->cert_info.enc.modified = 1; |
| 284 |
return i2d_X509_CINF(&x->cert_info, pp); |
| 285 |
} |
| 286 |
|
| 287 |
void X509_get0_signature(const ASN1_BIT_STRING **psig, |
| 288 |
const X509_ALGOR **palg, const X509 *x) |
| 289 |
{ |
| 290 |
if (psig) |
| 291 |
*psig = &x->signature; |
| 292 |
if (palg) |
| 293 |
*palg = &x->sig_alg; |
| 294 |
} |
| 295 |
|
| 296 |
int X509_get_signature_nid(const X509 *x) |
| 297 |
{ |
| 298 |
return OBJ_obj2nid(x->sig_alg.algorithm); |
| 299 |
} |
| 300 |
|
| 301 |
void X509_set0_distinguishing_id(X509 *x, ASN1_OCTET_STRING *d_id) |
| 302 |
{ |
| 303 |
ASN1_OCTET_STRING_free(x->distinguishing_id); |
| 304 |
x->distinguishing_id = d_id; |
| 305 |
} |
| 306 |
|
| 307 |
ASN1_OCTET_STRING *X509_get0_distinguishing_id(X509 *x) |
| 308 |
{ |
| 309 |
return x->distinguishing_id; |
| 310 |
} |