/src/openssl30/crypto/x509/x509_v3.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2020 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/safestack.h> | 
| 13 |  | #include <openssl/asn1.h> | 
| 14 |  | #include <openssl/objects.h> | 
| 15 |  | #include <openssl/evp.h> | 
| 16 |  | #include <openssl/x509.h> | 
| 17 |  | #include <openssl/x509v3.h> | 
| 18 |  | #include "x509_local.h" | 
| 19 |  |  | 
| 20 |  | int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x) | 
| 21 | 188k | { | 
| 22 | 188k |     int ret; | 
| 23 |  |  | 
| 24 | 188k |     if (x == NULL) | 
| 25 | 723 |         return 0; | 
| 26 | 187k |     ret = sk_X509_EXTENSION_num(x); | 
| 27 | 187k |     return ret > 0 ? ret : 0; | 
| 28 | 188k | } | 
| 29 |  |  | 
| 30 |  | int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid, | 
| 31 |  |                           int lastpos) | 
| 32 | 188 | { | 
| 33 | 188 |     ASN1_OBJECT *obj; | 
| 34 |  |  | 
| 35 | 188 |     obj = OBJ_nid2obj(nid); | 
| 36 | 188 |     if (obj == NULL) | 
| 37 | 0 |         return -2; | 
| 38 | 188 |     return X509v3_get_ext_by_OBJ(x, obj, lastpos); | 
| 39 | 188 | } | 
| 40 |  |  | 
| 41 |  | int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk, | 
| 42 |  |                           const ASN1_OBJECT *obj, int lastpos) | 
| 43 | 188 | { | 
| 44 | 188 |     int n; | 
| 45 | 188 |     X509_EXTENSION *ex; | 
| 46 |  |  | 
| 47 | 188 |     if (sk == NULL) | 
| 48 | 0 |         return -1; | 
| 49 | 188 |     lastpos++; | 
| 50 | 188 |     if (lastpos < 0) | 
| 51 | 0 |         lastpos = 0; | 
| 52 | 188 |     n = sk_X509_EXTENSION_num(sk); | 
| 53 | 1.10k |     for (; lastpos < n; lastpos++) { | 
| 54 | 973 |         ex = sk_X509_EXTENSION_value(sk, lastpos); | 
| 55 | 973 |         if (OBJ_cmp(ex->object, obj) == 0) | 
| 56 | 55 |             return lastpos; | 
| 57 | 973 |     } | 
| 58 | 133 |     return -1; | 
| 59 | 188 | } | 
| 60 |  |  | 
| 61 |  | int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit, | 
| 62 |  |                                int lastpos) | 
| 63 | 0 | { | 
| 64 | 0 |     int n; | 
| 65 | 0 |     X509_EXTENSION *ex; | 
| 66 |  | 
 | 
| 67 | 0 |     if (sk == NULL) | 
| 68 | 0 |         return -1; | 
| 69 | 0 |     lastpos++; | 
| 70 | 0 |     if (lastpos < 0) | 
| 71 | 0 |         lastpos = 0; | 
| 72 | 0 |     n = sk_X509_EXTENSION_num(sk); | 
| 73 | 0 |     for (; lastpos < n; lastpos++) { | 
| 74 | 0 |         ex = sk_X509_EXTENSION_value(sk, lastpos); | 
| 75 | 0 |         if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit)) | 
| 76 | 0 |             return lastpos; | 
| 77 | 0 |     } | 
| 78 | 0 |     return -1; | 
| 79 | 0 | } | 
| 80 |  |  | 
| 81 |  | X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc) | 
| 82 | 290k | { | 
| 83 | 290k |     if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0) | 
| 84 | 0 |         return NULL; | 
| 85 | 290k |     else | 
| 86 | 290k |         return sk_X509_EXTENSION_value(x, loc); | 
| 87 | 290k | } | 
| 88 |  |  | 
| 89 |  | X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc) | 
| 90 | 0 | { | 
| 91 | 0 |     X509_EXTENSION *ret; | 
| 92 |  | 
 | 
| 93 | 0 |     if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0) | 
| 94 | 0 |         return NULL; | 
| 95 | 0 |     ret = sk_X509_EXTENSION_delete(x, loc); | 
| 96 | 0 |     return ret; | 
| 97 | 0 | } | 
| 98 |  |  | 
| 99 |  | STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x, | 
| 100 |  |                                          X509_EXTENSION *ex, int loc) | 
| 101 | 0 | { | 
| 102 | 0 |     X509_EXTENSION *new_ex = NULL; | 
| 103 | 0 |     int n; | 
| 104 | 0 |     STACK_OF(X509_EXTENSION) *sk = NULL; | 
| 105 |  | 
 | 
| 106 | 0 |     if (x == NULL) { | 
| 107 | 0 |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); | 
| 108 | 0 |         goto err2; | 
| 109 | 0 |     } | 
| 110 |  |  | 
| 111 | 0 |     if (*x == NULL) { | 
| 112 | 0 |         if ((sk = sk_X509_EXTENSION_new_null()) == NULL) | 
| 113 | 0 |             goto err; | 
| 114 | 0 |     } else | 
| 115 | 0 |         sk = *x; | 
| 116 |  |  | 
| 117 | 0 |     n = sk_X509_EXTENSION_num(sk); | 
| 118 | 0 |     if (loc > n) | 
| 119 | 0 |         loc = n; | 
| 120 | 0 |     else if (loc < 0) | 
| 121 | 0 |         loc = n; | 
| 122 |  | 
 | 
| 123 | 0 |     if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) | 
| 124 | 0 |         goto err2; | 
| 125 | 0 |     if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) | 
| 126 | 0 |         goto err; | 
| 127 | 0 |     if (*x == NULL) | 
| 128 | 0 |         *x = sk; | 
| 129 | 0 |     return sk; | 
| 130 | 0 |  err: | 
| 131 | 0 |     ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); | 
| 132 | 0 |  err2: | 
| 133 | 0 |     X509_EXTENSION_free(new_ex); | 
| 134 | 0 |     if (x != NULL && *x == NULL) | 
| 135 | 0 |         sk_X509_EXTENSION_free(sk); | 
| 136 | 0 |     return NULL; | 
| 137 | 0 | } | 
| 138 |  |  | 
| 139 |  | X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid, | 
| 140 |  |                                              int crit, | 
| 141 |  |                                              ASN1_OCTET_STRING *data) | 
| 142 | 0 | { | 
| 143 | 0 |     ASN1_OBJECT *obj; | 
| 144 | 0 |     X509_EXTENSION *ret; | 
| 145 |  | 
 | 
| 146 | 0 |     obj = OBJ_nid2obj(nid); | 
| 147 | 0 |     if (obj == NULL) { | 
| 148 | 0 |         ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID); | 
| 149 | 0 |         return NULL; | 
| 150 | 0 |     } | 
| 151 | 0 |     ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data); | 
| 152 | 0 |     if (ret == NULL) | 
| 153 | 0 |         ASN1_OBJECT_free(obj); | 
| 154 | 0 |     return ret; | 
| 155 | 0 | } | 
| 156 |  |  | 
| 157 |  | X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex, | 
| 158 |  |                                              const ASN1_OBJECT *obj, int crit, | 
| 159 |  |                                              ASN1_OCTET_STRING *data) | 
| 160 | 0 | { | 
| 161 | 0 |     X509_EXTENSION *ret; | 
| 162 |  | 
 | 
| 163 | 0 |     if ((ex == NULL) || (*ex == NULL)) { | 
| 164 | 0 |         if ((ret = X509_EXTENSION_new()) == NULL) { | 
| 165 | 0 |             ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); | 
| 166 | 0 |             return NULL; | 
| 167 | 0 |         } | 
| 168 | 0 |     } else | 
| 169 | 0 |         ret = *ex; | 
| 170 |  |  | 
| 171 | 0 |     if (!X509_EXTENSION_set_object(ret, obj)) | 
| 172 | 0 |         goto err; | 
| 173 | 0 |     if (!X509_EXTENSION_set_critical(ret, crit)) | 
| 174 | 0 |         goto err; | 
| 175 | 0 |     if (!X509_EXTENSION_set_data(ret, data)) | 
| 176 | 0 |         goto err; | 
| 177 |  |  | 
| 178 | 0 |     if ((ex != NULL) && (*ex == NULL)) | 
| 179 | 0 |         *ex = ret; | 
| 180 | 0 |     return ret; | 
| 181 | 0 |  err: | 
| 182 | 0 |     if ((ex == NULL) || (ret != *ex)) | 
| 183 | 0 |         X509_EXTENSION_free(ret); | 
| 184 | 0 |     return NULL; | 
| 185 | 0 | } | 
| 186 |  |  | 
| 187 |  | int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj) | 
| 188 | 0 | { | 
| 189 | 0 |     if ((ex == NULL) || (obj == NULL)) | 
| 190 | 0 |         return 0; | 
| 191 | 0 |     ASN1_OBJECT_free(ex->object); | 
| 192 | 0 |     ex->object = OBJ_dup(obj); | 
| 193 | 0 |     return ex->object != NULL; | 
| 194 | 0 | } | 
| 195 |  |  | 
| 196 |  | int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit) | 
| 197 | 0 | { | 
| 198 | 0 |     if (ex == NULL) | 
| 199 | 0 |         return 0; | 
| 200 | 0 |     ex->critical = (crit) ? 0xFF : -1; | 
| 201 | 0 |     return 1; | 
| 202 | 0 | } | 
| 203 |  |  | 
| 204 |  | int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data) | 
| 205 | 0 | { | 
| 206 | 0 |     int i; | 
| 207 |  | 
 | 
| 208 | 0 |     if (ex == NULL) | 
| 209 | 0 |         return 0; | 
| 210 | 0 |     i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length); | 
| 211 | 0 |     if (!i) | 
| 212 | 0 |         return 0; | 
| 213 | 0 |     return 1; | 
| 214 | 0 | } | 
| 215 |  |  | 
| 216 |  | ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex) | 
| 217 | 6.86M | { | 
| 218 | 6.86M |     if (ex == NULL) | 
| 219 | 0 |         return NULL; | 
| 220 | 6.86M |     return ex->object; | 
| 221 | 6.86M | } | 
| 222 |  |  | 
| 223 |  | ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex) | 
| 224 | 1.55M | { | 
| 225 | 1.55M |     if (ex == NULL) | 
| 226 | 0 |         return NULL; | 
| 227 | 1.55M |     return &ex->value; | 
| 228 | 1.55M | } | 
| 229 |  |  | 
| 230 |  | int X509_EXTENSION_get_critical(const X509_EXTENSION *ex) | 
| 231 | 1.73M | { | 
| 232 | 1.73M |     if (ex == NULL) | 
| 233 | 0 |         return 0; | 
| 234 | 1.73M |     if (ex->critical > 0) | 
| 235 | 173k |         return 1; | 
| 236 | 1.56M |     return 0; | 
| 237 | 1.73M | } |