| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | /* X509 v3 extension utilities */ |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <stdio.h> |
| 19 |
| 20 | #include <openssl/conf.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/obj.h> |
| 24 | #include <openssl/x509.h> |
| 25 |
| 26 | #include "internal.h" |
| 27 |
| 28 | #include "ext_dat.h" |
| 29 |
| 30 | DEFINE_STACK_OF(X509V3_EXT_METHOD) |
| 31 |
| 32 | static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL; |
| 33 |
| 34 | static int ext_stack_cmp(const X509V3_EXT_METHOD *const *a, |
| 35 | const X509V3_EXT_METHOD *const *b) { |
| 36 | return ((*a)->ext_nid - (*b)->ext_nid); |
| 37 | } |
| 38 |
| 39 | int X509V3_EXT_add(X509V3_EXT_METHOD *ext) { |
| 40 | // We only support |ASN1_ITEM|-based extensions. |
| 41 | assert(ext->it != NULL); |
| 42 |
| 43 | // TODO(davidben): This should be locked. Also check for duplicates. |
| 44 | if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_stack_cmp))) { |
| 45 | return 0; |
| 46 | } |
| 47 | if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) { |
| 48 | return 0; |
| 49 | } |
| 50 | sk_X509V3_EXT_METHOD_sort(ext_list); |
| 51 | return 1; |
| 52 | } |
| 53 |
| 54 | static int ext_cmp(const void *void_a, const void *void_b) { |
| 55 | const X509V3_EXT_METHOD **a = (const X509V3_EXT_METHOD **)void_a; |
| 56 | const X509V3_EXT_METHOD **b = (const X509V3_EXT_METHOD **)void_b; |
| 57 | return ext_stack_cmp(a, b); |
| 58 | } |
| 59 |
| 60 | const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) { |
| 61 | X509V3_EXT_METHOD tmp; |
| 62 | const X509V3_EXT_METHOD *t = &tmp, *const * ret; |
| 63 | size_t idx; |
| 64 |
| 65 | if (nid < 0) { |
| 66 | return NULL; |
| 67 | } |
| 68 | tmp.ext_nid = nid; |
| 69 | ret = reinterpret_cast<X509V3_EXT_METHOD **>( |
| 70 | bsearch(&t, standard_exts, STANDARD_EXTENSION_COUNT, |
| 71 | sizeof(X509V3_EXT_METHOD *), ext_cmp)); |
| 72 | if (ret) { |
| 73 | return *ret; |
| 74 | } |
| 75 | if (!ext_list) { |
| 76 | return NULL; |
| 77 | } |
| 78 |
| 79 | if (!sk_X509V3_EXT_METHOD_find(ext_list, &idx, &tmp)) { |
| 80 | return NULL; |
| 81 | } |
| 82 | return sk_X509V3_EXT_METHOD_value(ext_list, idx); |
| 83 | } |
| 84 |
| 85 | const X509V3_EXT_METHOD *X509V3_EXT_get(const X509_EXTENSION *ext) { |
| 86 | int nid; |
| 87 | if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) { |
| 88 | return NULL; |
| 89 | } |
| 90 | return X509V3_EXT_get_nid(nid); |
| 91 | } |
| 92 |
| 93 | int X509V3_EXT_free(int nid, void *ext_data) { |
| 94 | const X509V3_EXT_METHOD *ext_method = X509V3_EXT_get_nid(nid); |
| 95 | if (ext_method == NULL) { |
| 96 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_CANNOT_FIND_FREE_FUNCTION); |
| 97 | return 0; |
| 98 | } |
| 99 |
| 100 | ASN1_item_free(reinterpret_cast<ASN1_VALUE *>(ext_data), |
| 101 | ASN1_ITEM_ptr(ext_method->it)); |
| 102 | return 1; |
| 103 | } |
| 104 |
| 105 | int X509V3_EXT_add_alias(int nid_to, int nid_from) { |
| 106 | OPENSSL_BEGIN_ALLOW_DEPRECATED |
| 107 | const X509V3_EXT_METHOD *ext; |
| 108 | X509V3_EXT_METHOD *tmpext; |
| 109 |
| 110 | if (!(ext = X509V3_EXT_get_nid(nid_from))) { |
| 111 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NOT_FOUND); |
| 112 | return 0; |
| 113 | } |
| 114 | if (!(tmpext = |
| 115 | (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) { |
| 116 | return 0; |
| 117 | } |
| 118 | *tmpext = *ext; |
| 119 | tmpext->ext_nid = nid_to; |
| 120 | if (!X509V3_EXT_add(tmpext)) { |
| 121 | OPENSSL_free(tmpext); |
| 122 | return 0; |
| 123 | } |
| 124 | return 1; |
| 125 | OPENSSL_END_ALLOW_DEPRECATED |
| 126 | } |
| 127 |
| 128 | // Legacy function: we don't need to add standard extensions any more because |
| 129 | // they are now kept in ext_dat.h. |
| 130 |
| 131 | int X509V3_add_standard_extensions(void) { return 1; } |
| 132 |
| 133 | // Return an extension internal structure |
| 134 |
| 135 | void *X509V3_EXT_d2i(const X509_EXTENSION *ext) { |
| 136 | const X509V3_EXT_METHOD *method; |
| 137 | const unsigned char *p; |
| 138 |
| 139 | if (!(method = X509V3_EXT_get(ext))) { |
| 140 | return NULL; |
| 141 | } |
| 142 | p = ext->value->data; |
| 143 | void *ret = |
| 144 | ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it)); |
| 145 | if (ret == NULL) { |
| 146 | return NULL; |
| 147 | } |
| 148 | // Check for trailing data. |
| 149 | if (p != ext->value->data + ext->value->length) { |
| 150 | ASN1_item_free(reinterpret_cast<ASN1_VALUE *>(ret), |
| 151 | ASN1_ITEM_ptr(method->it)); |
| 152 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_TRAILING_DATA_IN_EXTENSION); |
| 153 | return NULL; |
| 154 | } |
| 155 | return ret; |
| 156 | } |
| 157 |
| 158 | void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions, int nid, |
| 159 | int *out_critical, int *out_idx) { |
| 160 | int lastpos; |
| 161 | X509_EXTENSION *ex, *found_ex = NULL; |
| 162 | if (!extensions) { |
| 163 | if (out_idx) { |
| 164 | *out_idx = -1; |
| 165 | } |
| 166 | if (out_critical) { |
| 167 | *out_critical = -1; |
| 168 | } |
| 169 | return NULL; |
| 170 | } |
| 171 | if (out_idx) { |
| 172 | lastpos = *out_idx + 1; |
| 173 | } else { |
| 174 | lastpos = 0; |
| 175 | } |
| 176 | if (lastpos < 0) { |
| 177 | lastpos = 0; |
| 178 | } |
| 179 | for (size_t i = lastpos; i < sk_X509_EXTENSION_num(extensions); i++) { |
| 180 | ex = sk_X509_EXTENSION_value(extensions, i); |
| 181 | if (OBJ_obj2nid(ex->object) == nid) { |
| 182 | if (out_idx) { |
| 183 | // TODO(https://crbug.com/boringssl/379): Consistently reject |
| 184 | // duplicate extensions. |
| 185 | *out_idx = (int)i; |
| 186 | found_ex = ex; |
| 187 | break; |
| 188 | } else if (found_ex) { |
| 189 | // Found more than one |
| 190 | if (out_critical) { |
| 191 | *out_critical = -2; |
| 192 | } |
| 193 | return NULL; |
| 194 | } |
| 195 | found_ex = ex; |
| 196 | } |
| 197 | } |
| 198 | if (found_ex) { |
| 199 | // Found it |
| 200 | if (out_critical) { |
| 201 | *out_critical = X509_EXTENSION_get_critical(found_ex); |
| 202 | } |
| 203 | return X509V3_EXT_d2i(found_ex); |
| 204 | } |
| 205 |
| 206 | // Extension not found |
| 207 | if (out_idx) { |
| 208 | *out_idx = -1; |
| 209 | } |
| 210 | if (out_critical) { |
| 211 | *out_critical = -1; |
| 212 | } |
| 213 | return NULL; |
| 214 | } |
| 215 |
| 216 | // This function is a general extension append, replace and delete utility. |
| 217 | // The precise operation is governed by the 'flags' value. The 'crit' and |
| 218 | // 'value' arguments (if relevant) are the extensions internal structure. |
| 219 |
| 220 | int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, |
| 221 | int crit, unsigned long flags) { |
| 222 | int errcode, extidx = -1; |
| 223 | X509_EXTENSION *ext = NULL, *extmp; |
| 224 | STACK_OF(X509_EXTENSION) *ret = NULL; |
| 225 | unsigned long ext_op = flags & X509V3_ADD_OP_MASK; |
| 226 |
| 227 | // If appending we don't care if it exists, otherwise look for existing |
| 228 | // extension. |
| 229 | if (ext_op != X509V3_ADD_APPEND) { |
| 230 | extidx = X509v3_get_ext_by_NID(*x, nid, -1); |
| 231 | } |
| 232 |
| 233 | // See if extension exists |
| 234 | if (extidx >= 0) { |
| 235 | // If keep existing, nothing to do |
| 236 | if (ext_op == X509V3_ADD_KEEP_EXISTING) { |
| 237 | return 1; |
| 238 | } |
| 239 | // If default then its an error |
| 240 | if (ext_op == X509V3_ADD_DEFAULT) { |
| 241 | errcode = X509V3_R_EXTENSION_EXISTS; |
| 242 | goto err; |
| 243 | } |
| 244 | // If delete, just delete it |
| 245 | if (ext_op == X509V3_ADD_DELETE) { |
| 246 | X509_EXTENSION *prev_ext = sk_X509_EXTENSION_delete(*x, extidx); |
| 247 | if (prev_ext == NULL) { |
| 248 | return -1; |
| 249 | } |
| 250 | X509_EXTENSION_free(prev_ext); |
| 251 | return 1; |
| 252 | } |
| 253 | } else { |
| 254 | // If replace existing or delete, error since extension must exist |
| 255 | if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || |
| 256 | (ext_op == X509V3_ADD_DELETE)) { |
| 257 | errcode = X509V3_R_EXTENSION_NOT_FOUND; |
| 258 | goto err; |
| 259 | } |
| 260 | } |
| 261 |
| 262 | // If we get this far then we have to create an extension: could have |
| 263 | // some flags for alternative encoding schemes... |
| 264 |
| 265 | ext = X509V3_EXT_i2d(nid, crit, value); |
| 266 |
| 267 | if (!ext) { |
| 268 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CREATING_EXTENSION); |
| 269 | return 0; |
| 270 | } |
| 271 |
| 272 | // If extension exists replace it.. |
| 273 | if (extidx >= 0) { |
| 274 | extmp = sk_X509_EXTENSION_value(*x, extidx); |
| 275 | X509_EXTENSION_free(extmp); |
| 276 | if (!sk_X509_EXTENSION_set(*x, extidx, ext)) { |
| 277 | return -1; |
| 278 | } |
| 279 | return 1; |
| 280 | } |
| 281 |
| 282 | if ((ret = *x) == NULL && (ret = sk_X509_EXTENSION_new_null()) == NULL) { |
| 283 | goto m_fail; |
| 284 | } |
| 285 | if (!sk_X509_EXTENSION_push(ret, ext)) { |
| 286 | goto m_fail; |
| 287 | } |
| 288 |
| 289 | *x = ret; |
| 290 | return 1; |
| 291 |
| 292 | m_fail: |
| 293 | if (ret != *x) { |
| 294 | sk_X509_EXTENSION_free(ret); |
| 295 | } |
| 296 | X509_EXTENSION_free(ext); |
| 297 | return -1; |
| 298 |
| 299 | err: |
| 300 | if (!(flags & X509V3_ADD_SILENT)) { |
| 301 | OPENSSL_PUT_ERROR(X509V3, errcode); |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 |