| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999. |
| 2 |
// Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
|
| 7 |
#include <openssl/asn1t.h> |
| 8 |
#include <openssl/evp.h> |
| 9 |
#include <openssl/obj.h> |
| 10 |
#include <openssl/x509.h> |
| 11 |
|
| 12 |
#include "internal.h" |
| 13 |
|
| 14 |
|
| 15 |
// X509_CERT_AUX routines. These are used to encode additional user |
| 16 |
// modifiable data about a certificate. This data is appended to the X509 |
| 17 |
// encoding when the *_X509_AUX routines are used. This means that the |
| 18 |
// "traditional" X509 routines will simply ignore the extra data. |
| 19 |
|
| 20 |
static X509_CERT_AUX *aux_get(X509 *x); |
| 21 |
|
| 22 |
ASN1_SEQUENCE(X509_CERT_AUX) = { |
| 23 |
ASN1_SEQUENCE_OF_OPT(X509_CERT_AUX, trust, ASN1_OBJECT), |
| 24 |
ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0), |
| 25 |
ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING), |
| 26 |
ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING), |
| 27 |
} ASN1_SEQUENCE_END(X509_CERT_AUX) |
| 28 |
|
| 29 |
IMPLEMENT_ASN1_FUNCTIONS_const(X509_CERT_AUX) |
| 30 |
|
| 31 |
static X509_CERT_AUX *aux_get(X509 *x) { |
| 32 |
if (!x) { |
| 33 |
return NULL; |
| 34 |
} |
| 35 |
if (!x->aux && !(x->aux = X509_CERT_AUX_new())) { |
| 36 |
return NULL; |
| 37 |
} |
| 38 |
return x->aux; |
| 39 |
} |
| 40 |
|
| 41 |
int X509_alias_set1(X509 *x, const uint8_t *name, ossl_ssize_t len) { |
| 42 |
X509_CERT_AUX *aux; |
| 43 |
// TODO(davidben): Empty aliases are not meaningful in PKCS#12, and the |
| 44 |
// getters cannot quite represent them. Also erase the object if |len| is |
| 45 |
// zero. |
| 46 |
if (!name) { |
| 47 |
if (!x || !x->aux || !x->aux->alias) { |
| 48 |
return 1; |
| 49 |
} |
| 50 |
ASN1_UTF8STRING_free(x->aux->alias); |
| 51 |
x->aux->alias = NULL; |
| 52 |
return 1; |
| 53 |
} |
| 54 |
if (!(aux = aux_get(x))) { |
| 55 |
return 0; |
| 56 |
} |
| 57 |
if (!aux->alias && !(aux->alias = ASN1_UTF8STRING_new())) { |
| 58 |
return 0; |
| 59 |
} |
| 60 |
return ASN1_STRING_set(aux->alias, name, len); |
| 61 |
} |
| 62 |
|
| 63 |
int X509_keyid_set1(X509 *x, const uint8_t *id, ossl_ssize_t len) { |
| 64 |
X509_CERT_AUX *aux; |
| 65 |
// TODO(davidben): Empty key IDs are not meaningful in PKCS#12, and the |
| 66 |
// getters cannot quite represent them. Also erase the object if |len| is |
| 67 |
// zero. |
| 68 |
if (!id) { |
| 69 |
if (!x || !x->aux || !x->aux->keyid) { |
| 70 |
return 1; |
| 71 |
} |
| 72 |
ASN1_OCTET_STRING_free(x->aux->keyid); |
| 73 |
x->aux->keyid = NULL; |
| 74 |
return 1; |
| 75 |
} |
| 76 |
if (!(aux = aux_get(x))) { |
| 77 |
return 0; |
| 78 |
} |
| 79 |
if (!aux->keyid && !(aux->keyid = ASN1_OCTET_STRING_new())) { |
| 80 |
return 0; |
| 81 |
} |
| 82 |
return ASN1_STRING_set(aux->keyid, id, len); |
| 83 |
} |
| 84 |
|
| 85 |
const uint8_t *X509_alias_get0(const X509 *x, int *out_len) { |
| 86 |
const ASN1_UTF8STRING *alias = x->aux != NULL ? x->aux->alias : NULL; |
| 87 |
if (out_len != NULL) { |
| 88 |
*out_len = alias != NULL ? alias->length : 0; |
| 89 |
} |
| 90 |
return alias != NULL ? alias->data : NULL; |
| 91 |
} |
| 92 |
|
| 93 |
const uint8_t *X509_keyid_get0(const X509 *x, int *out_len) { |
| 94 |
const ASN1_OCTET_STRING *keyid = x->aux != NULL ? x->aux->keyid : NULL; |
| 95 |
if (out_len != NULL) { |
| 96 |
*out_len = keyid != NULL ? keyid->length : 0; |
| 97 |
} |
| 98 |
return keyid != NULL ? keyid->data : NULL; |
| 99 |
} |
| 100 |
|
| 101 |
int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj) { |
| 102 |
ASN1_OBJECT *objtmp = OBJ_dup(obj); |
| 103 |
if (objtmp == NULL) { |
| 104 |
goto err; |
| 105 |
} |
| 106 |
X509_CERT_AUX *aux = aux_get(x); |
| 107 |
if (aux->trust == NULL) { |
| 108 |
aux->trust = sk_ASN1_OBJECT_new_null(); |
| 109 |
if (aux->trust == NULL) { |
| 110 |
goto err; |
| 111 |
} |
| 112 |
} |
| 113 |
if (!sk_ASN1_OBJECT_push(aux->trust, objtmp)) { |
| 114 |
goto err; |
| 115 |
} |
| 116 |
return 1; |
| 117 |
|
| 118 |
err: |
| 119 |
ASN1_OBJECT_free(objtmp); |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
int X509_add1_reject_object(X509 *x, const ASN1_OBJECT *obj) { |
| 124 |
ASN1_OBJECT *objtmp = OBJ_dup(obj); |
| 125 |
if (objtmp == NULL) { |
| 126 |
goto err; |
| 127 |
} |
| 128 |
X509_CERT_AUX *aux = aux_get(x); |
| 129 |
if (aux->reject == NULL) { |
| 130 |
aux->reject = sk_ASN1_OBJECT_new_null(); |
| 131 |
if (aux->reject == NULL) { |
| 132 |
goto err; |
| 133 |
} |
| 134 |
} |
| 135 |
if (!sk_ASN1_OBJECT_push(aux->reject, objtmp)) { |
| 136 |
goto err; |
| 137 |
} |
| 138 |
return 1; |
| 139 |
|
| 140 |
err: |
| 141 |
ASN1_OBJECT_free(objtmp); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
void X509_trust_clear(X509 *x) { |
| 146 |
if (x->aux && x->aux->trust) { |
| 147 |
sk_ASN1_OBJECT_pop_free(x->aux->trust, ASN1_OBJECT_free); |
| 148 |
x->aux->trust = NULL; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void X509_reject_clear(X509 *x) { |
| 153 |
if (x->aux && x->aux->reject) { |
| 154 |
sk_ASN1_OBJECT_pop_free(x->aux->reject, ASN1_OBJECT_free); |
| 155 |
x->aux->reject = NULL; |
| 156 |
} |
| 157 |
} |