Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project |
| 3 | * 1999. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4 | */ |
| 5 | /* ==================================================================== |
| 6 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 13 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * |
| 20 | * 3. All advertising materials mentioning features or use of this |
| 21 | * software must display the following acknowledgment: |
| 22 | * "This product includes software developed by the OpenSSL Project |
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 24 | * |
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 26 | * endorse or promote products derived from this software without |
| 27 | * prior written permission. For written permission, please contact |
| 28 | * licensing@OpenSSL.org. |
| 29 | * |
| 30 | * 5. Products derived from this software may not be called "OpenSSL" |
| 31 | * nor may "OpenSSL" appear in their names without prior written |
| 32 | * permission of the OpenSSL Project. |
| 33 | * |
| 34 | * 6. Redistributions of any form whatsoever must retain the following |
| 35 | * acknowledgment: |
| 36 | * "This product includes software developed by the OpenSSL Project |
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 38 | * |
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | * ==================================================================== |
| 52 | * |
| 53 | * This product includes cryptographic software written by Eric Young |
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 55 | * Hudson (tjh@cryptsoft.com). |
| 56 | * |
| 57 | */ |
| 58 | /* X509 v3 extension utilities */ |
| 59 | |
David Benjamin | 8abd1b5 | 2023-04-12 10:07:53 -0400 | [diff] [blame] | 60 | #include <assert.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | #include <stdio.h> |
| 62 | |
| 63 | #include <openssl/conf.h> |
| 64 | #include <openssl/err.h> |
| 65 | #include <openssl/mem.h> |
| 66 | #include <openssl/obj.h> |
| 67 | #include <openssl/x509v3.h> |
| 68 | |
David Benjamin | 94a608a | 2021-07-14 13:46:27 -0400 | [diff] [blame] | 69 | #include "../x509/internal.h" |
| 70 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 71 | #include "ext_dat.h" |
| 72 | static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL; |
| 73 | |
David Benjamin | af0739f | 2023-01-09 10:03:02 -0800 | [diff] [blame] | 74 | static int ext_stack_cmp(const X509V3_EXT_METHOD *const *a, |
| 75 | const X509V3_EXT_METHOD *const *b) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 76 | return ((*a)->ext_nid - (*b)->ext_nid); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 77 | } |
| 78 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 79 | int X509V3_EXT_add(X509V3_EXT_METHOD *ext) { |
David Benjamin | 8abd1b5 | 2023-04-12 10:07:53 -0400 | [diff] [blame] | 80 | // We only support |ASN1_ITEM|-based extensions. |
| 81 | assert(ext->it != NULL); |
| 82 | |
David Benjamin | 97d48db | 2023-03-29 03:09:15 +0900 | [diff] [blame] | 83 | // TODO(davidben): This should be locked. Also check for duplicates. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 84 | if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_stack_cmp))) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 88 | return 0; |
| 89 | } |
David Benjamin | 97d48db | 2023-03-29 03:09:15 +0900 | [diff] [blame] | 90 | sk_X509V3_EXT_METHOD_sort(ext_list); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 91 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 94 | static int ext_cmp(const void *void_a, const void *void_b) { |
| 95 | const X509V3_EXT_METHOD **a = (const X509V3_EXT_METHOD **)void_a; |
| 96 | const X509V3_EXT_METHOD **b = (const X509V3_EXT_METHOD **)void_b; |
| 97 | return ext_stack_cmp(a, b); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 98 | } |
| 99 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 100 | const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) { |
| 101 | X509V3_EXT_METHOD tmp; |
| 102 | const X509V3_EXT_METHOD *t = &tmp, *const * ret; |
| 103 | size_t idx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 104 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 105 | if (nid < 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 106 | return NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 107 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 108 | tmp.ext_nid = nid; |
| 109 | ret = bsearch(&t, standard_exts, STANDARD_EXTENSION_COUNT, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 110 | sizeof(X509V3_EXT_METHOD *), ext_cmp); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 111 | if (ret) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 112 | return *ret; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 113 | } |
| 114 | if (!ext_list) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 115 | return NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 116 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 117 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 118 | if (!sk_X509V3_EXT_METHOD_find(ext_list, &idx, &tmp)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 119 | return NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 120 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 121 | return sk_X509V3_EXT_METHOD_value(ext_list, idx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | } |
| 123 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 124 | const X509V3_EXT_METHOD *X509V3_EXT_get(const X509_EXTENSION *ext) { |
| 125 | int nid; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 126 | if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 127 | return NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 128 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 129 | return X509V3_EXT_get_nid(nid); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | } |
| 131 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 132 | int X509V3_EXT_free(int nid, void *ext_data) { |
| 133 | const X509V3_EXT_METHOD *ext_method = X509V3_EXT_get_nid(nid); |
| 134 | if (ext_method == NULL) { |
| 135 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_CANNOT_FIND_FREE_FUNCTION); |
| 136 | return 0; |
| 137 | } |
Adam Langley | 6d43d0c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 138 | |
David Benjamin | 8abd1b5 | 2023-04-12 10:07:53 -0400 | [diff] [blame] | 139 | ASN1_item_free(ext_data, ASN1_ITEM_ptr(ext_method->it)); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 140 | return 1; |
Adam Langley | 6d43d0c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 143 | int X509V3_EXT_add_alias(int nid_to, int nid_from) { |
Bob Beck | ac6d558 | 2023-03-28 14:23:03 -0600 | [diff] [blame] | 144 | OPENSSL_BEGIN_ALLOW_DEPRECATED |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 145 | const X509V3_EXT_METHOD *ext; |
| 146 | X509V3_EXT_METHOD *tmpext; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 148 | if (!(ext = X509V3_EXT_get_nid(nid_from))) { |
| 149 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NOT_FOUND); |
| 150 | return 0; |
| 151 | } |
| 152 | if (!(tmpext = |
| 153 | (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | *tmpext = *ext; |
| 157 | tmpext->ext_nid = nid_to; |
David Benjamin | 5fb362c | 2023-04-13 10:21:47 -0400 | [diff] [blame] | 158 | if (!X509V3_EXT_add(tmpext)) { |
| 159 | OPENSSL_free(tmpext); |
| 160 | return 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 161 | } |
David Benjamin | 5fb362c | 2023-04-13 10:21:47 -0400 | [diff] [blame] | 162 | return 1; |
Bob Beck | ac6d558 | 2023-03-28 14:23:03 -0600 | [diff] [blame] | 163 | OPENSSL_END_ALLOW_DEPRECATED |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | } |
| 165 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 166 | // Legacy function: we don't need to add standard extensions any more because |
| 167 | // they are now kept in ext_dat.h. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 169 | int X509V3_add_standard_extensions(void) { return 1; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 171 | // Return an extension internal structure |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 173 | void *X509V3_EXT_d2i(const X509_EXTENSION *ext) { |
| 174 | const X509V3_EXT_METHOD *method; |
| 175 | const unsigned char *p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 176 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 177 | if (!(method = X509V3_EXT_get(ext))) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 178 | return NULL; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 179 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 180 | p = ext->value->data; |
David Benjamin | 8abd1b5 | 2023-04-12 10:07:53 -0400 | [diff] [blame] | 181 | void *ret = |
| 182 | ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it)); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 183 | if (ret == NULL) { |
| 184 | return NULL; |
| 185 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 186 | // Check for trailing data. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 187 | if (p != ext->value->data + ext->value->length) { |
David Benjamin | 8abd1b5 | 2023-04-12 10:07:53 -0400 | [diff] [blame] | 188 | ASN1_item_free(ret, ASN1_ITEM_ptr(method->it)); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 189 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_TRAILING_DATA_IN_EXTENSION); |
| 190 | return NULL; |
| 191 | } |
| 192 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
David Benjamin | 8591d53 | 2020-11-03 13:49:28 -0500 | [diff] [blame] | 195 | void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions, int nid, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 196 | int *out_critical, int *out_idx) { |
| 197 | int lastpos; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 198 | X509_EXTENSION *ex, *found_ex = NULL; |
| 199 | if (!extensions) { |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 200 | if (out_idx) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 201 | *out_idx = -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 202 | } |
| 203 | if (out_critical) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 204 | *out_critical = -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 205 | } |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 206 | return NULL; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 207 | } |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 208 | if (out_idx) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 209 | lastpos = *out_idx + 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 210 | } else { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 211 | lastpos = 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 212 | } |
| 213 | if (lastpos < 0) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 214 | lastpos = 0; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 215 | } |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 216 | for (size_t i = lastpos; i < sk_X509_EXTENSION_num(extensions); i++) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 217 | ex = sk_X509_EXTENSION_value(extensions, i); |
| 218 | if (OBJ_obj2nid(ex->object) == nid) { |
| 219 | if (out_idx) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 220 | // TODO(https://crbug.com/boringssl/379): Consistently reject |
| 221 | // duplicate extensions. |
David Benjamin | 89a6253 | 2023-05-23 11:45:10 -0400 | [diff] [blame] | 222 | *out_idx = (int)i; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 223 | found_ex = ex; |
| 224 | break; |
| 225 | } else if (found_ex) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 226 | // Found more than one |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 227 | if (out_critical) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 228 | *out_critical = -2; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 229 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 230 | return NULL; |
| 231 | } |
| 232 | found_ex = ex; |
| 233 | } |
| 234 | } |
| 235 | if (found_ex) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 236 | // Found it |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 237 | if (out_critical) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 238 | *out_critical = X509_EXTENSION_get_critical(found_ex); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 239 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 240 | return X509V3_EXT_d2i(found_ex); |
| 241 | } |
| 242 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 243 | // Extension not found |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 244 | if (out_idx) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 245 | *out_idx = -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 246 | } |
| 247 | if (out_critical) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 248 | *out_critical = -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 249 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 250 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | } |
| 252 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 253 | // This function is a general extension append, replace and delete utility. |
| 254 | // The precise operation is governed by the 'flags' value. The 'crit' and |
| 255 | // 'value' arguments (if relevant) are the extensions internal structure. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
| 257 | int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 258 | int crit, unsigned long flags) { |
| 259 | int errcode, extidx = -1; |
| 260 | X509_EXTENSION *ext = NULL, *extmp; |
| 261 | STACK_OF(X509_EXTENSION) *ret = NULL; |
| 262 | unsigned long ext_op = flags & X509V3_ADD_OP_MASK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 264 | // If appending we don't care if it exists, otherwise look for existing |
| 265 | // extension. |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 266 | if (ext_op != X509V3_ADD_APPEND) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 267 | extidx = X509v3_get_ext_by_NID(*x, nid, -1); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 268 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 269 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 270 | // See if extension exists |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 271 | if (extidx >= 0) { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 272 | // If keep existing, nothing to do |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 273 | if (ext_op == X509V3_ADD_KEEP_EXISTING) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 274 | return 1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 275 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 276 | // If default then its an error |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 277 | if (ext_op == X509V3_ADD_DEFAULT) { |
| 278 | errcode = X509V3_R_EXTENSION_EXISTS; |
| 279 | goto err; |
| 280 | } |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 281 | // If delete, just delete it |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 282 | if (ext_op == X509V3_ADD_DELETE) { |
David Benjamin | 7528f03 | 2022-07-08 15:43:46 -0400 | [diff] [blame] | 283 | X509_EXTENSION *prev_ext = sk_X509_EXTENSION_delete(*x, extidx); |
| 284 | if (prev_ext == NULL) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 285 | return -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 286 | } |
David Benjamin | 7528f03 | 2022-07-08 15:43:46 -0400 | [diff] [blame] | 287 | X509_EXTENSION_free(prev_ext); |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 288 | return 1; |
| 289 | } |
| 290 | } else { |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 291 | // If replace existing or delete, error since extension must exist |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 292 | if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || |
| 293 | (ext_op == X509V3_ADD_DELETE)) { |
| 294 | errcode = X509V3_R_EXTENSION_NOT_FOUND; |
| 295 | goto err; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 296 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 297 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 298 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 299 | // If we get this far then we have to create an extension: could have |
| 300 | // some flags for alternative encoding schemes... |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 301 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 302 | ext = X509V3_EXT_i2d(nid, crit, value); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 304 | if (!ext) { |
| 305 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CREATING_EXTENSION); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 306 | return 0; |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 307 | } |
| 308 | |
David Benjamin | 4635048 | 2022-06-16 14:03:12 -0400 | [diff] [blame] | 309 | // If extension exists replace it.. |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 310 | if (extidx >= 0) { |
| 311 | extmp = sk_X509_EXTENSION_value(*x, extidx); |
| 312 | X509_EXTENSION_free(extmp); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 313 | if (!sk_X509_EXTENSION_set(*x, extidx, ext)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 314 | return -1; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 315 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 316 | return 1; |
| 317 | } |
| 318 | |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 319 | if ((ret = *x) == NULL && (ret = sk_X509_EXTENSION_new_null()) == NULL) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 320 | goto m_fail; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 321 | } |
| 322 | if (!sk_X509_EXTENSION_push(ret, ext)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 323 | goto m_fail; |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 324 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 325 | |
| 326 | *x = ret; |
| 327 | return 1; |
| 328 | |
| 329 | m_fail: |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 330 | if (ret != *x) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 331 | sk_X509_EXTENSION_free(ret); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 332 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 333 | X509_EXTENSION_free(ext); |
| 334 | return -1; |
| 335 | |
| 336 | err: |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 337 | if (!(flags & X509V3_ADD_SILENT)) { |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 338 | OPENSSL_PUT_ERROR(X509V3, errcode); |
David Benjamin | c0b87a0 | 2022-06-16 14:02:15 -0400 | [diff] [blame] | 339 | } |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame] | 340 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 341 | } |