| 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 | // extension creation utilities |
| 16 |
| 17 | #include <ctype.h> |
| 18 | #include <limits.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 |
| 22 | #include <openssl/conf.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/obj.h> |
| 26 | #include <openssl/x509.h> |
| 27 |
| 28 | #include "../internal.h" |
| 29 | #include "internal.h" |
| 30 |
| 31 | static int v3_check_critical(const char **value); |
| 32 | static int v3_check_generic(const char **value); |
| 33 | static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 34 | int ext_nid, int crit, const char *value); |
| 35 | static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, |
| 36 | int crit, int type, |
| 37 | const X509V3_CTX *ctx); |
| 38 | static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, |
| 39 | int crit, void *ext_struc); |
| 40 | static unsigned char *generic_asn1(const char *value, const X509V3_CTX *ctx, |
| 41 | size_t *ext_len); |
| 42 |
| 43 | X509_EXTENSION *X509V3_EXT_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 44 | const char *name, const char *value) { |
| 45 | // If omitted, fill in an empty |X509V3_CTX|. |
| 46 | X509V3_CTX ctx_tmp; |
| 47 | if (ctx == NULL) { |
| 48 | X509V3_set_ctx(&ctx_tmp, NULL, NULL, NULL, NULL, 0); |
| 49 | X509V3_set_nconf(&ctx_tmp, conf); |
| 50 | ctx = &ctx_tmp; |
| 51 | } |
| 52 |
| 53 | int crit = v3_check_critical(&value); |
| 54 | int ext_type = v3_check_generic(&value); |
| 55 | if (ext_type != 0) { |
| 56 | return v3_generic_extension(name, value, crit, ext_type, ctx); |
| 57 | } |
| 58 | X509_EXTENSION *ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value); |
| 59 | if (!ret) { |
| 60 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_IN_EXTENSION); |
| 61 | ERR_add_error_data(4, "name=", name, ", value=", value); |
| 62 | } |
| 63 | return ret; |
| 64 | } |
| 65 |
| 66 | X509_EXTENSION *X509V3_EXT_nconf_nid(const CONF *conf, const X509V3_CTX *ctx, |
| 67 | int ext_nid, const char *value) { |
| 68 | // If omitted, fill in an empty |X509V3_CTX|. |
| 69 | X509V3_CTX ctx_tmp; |
| 70 | if (ctx == NULL) { |
| 71 | X509V3_set_ctx(&ctx_tmp, NULL, NULL, NULL, NULL, 0); |
| 72 | X509V3_set_nconf(&ctx_tmp, conf); |
| 73 | ctx = &ctx_tmp; |
| 74 | } |
| 75 |
| 76 | int crit = v3_check_critical(&value); |
| 77 | int ext_type = v3_check_generic(&value); |
| 78 | if (ext_type != 0) { |
| 79 | return v3_generic_extension(OBJ_nid2sn(ext_nid), value, crit, ext_type, |
| 80 | ctx); |
| 81 | } |
| 82 | return do_ext_nconf(conf, ctx, ext_nid, crit, value); |
| 83 | } |
| 84 |
| 85 | // CONF *conf: Config file |
| 86 | // char *value: Value |
| 87 | static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 88 | int ext_nid, int crit, const char *value) { |
| 89 | const X509V3_EXT_METHOD *method; |
| 90 | X509_EXTENSION *ext; |
| 91 | const STACK_OF(CONF_VALUE) *nval; |
| 92 | STACK_OF(CONF_VALUE) *nval_owned = NULL; |
| 93 | void *ext_struc; |
| 94 | if (ext_nid == NID_undef) { |
| 95 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION_NAME); |
| 96 | return NULL; |
| 97 | } |
| 98 | if (!(method = X509V3_EXT_get_nid(ext_nid))) { |
| 99 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION); |
| 100 | return NULL; |
| 101 | } |
| 102 | // Now get internal extension representation based on type |
| 103 | if (method->v2i) { |
| 104 | if (*value == '@') { |
| 105 | // TODO(davidben): This is the only place where |X509V3_EXT_nconf|'s |
| 106 | // |conf| parameter is used. All other codepaths use the copy inside |
| 107 | // |ctx|. Should this be switched and then the parameter ignored? |
| 108 | if (conf == NULL) { |
| 109 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_CONFIG_DATABASE); |
| 110 | return NULL; |
| 111 | } |
| 112 | nval = NCONF_get_section(conf, value + 1); |
| 113 | } else { |
| 114 | nval_owned = X509V3_parse_list(value); |
| 115 | nval = nval_owned; |
| 116 | } |
| 117 | if (nval == NULL || sk_CONF_VALUE_num(nval) <= 0) { |
| 118 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_EXTENSION_STRING); |
| 119 | ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=", value); |
| 120 | sk_CONF_VALUE_pop_free(nval_owned, X509V3_conf_free); |
| 121 | return NULL; |
| 122 | } |
| 123 | ext_struc = method->v2i(method, ctx, nval); |
| 124 | sk_CONF_VALUE_pop_free(nval_owned, X509V3_conf_free); |
| 125 | if (!ext_struc) { |
| 126 | return NULL; |
| 127 | } |
| 128 | } else if (method->s2i) { |
| 129 | if (!(ext_struc = method->s2i(method, ctx, value))) { |
| 130 | return NULL; |
| 131 | } |
| 132 | } else if (method->r2i) { |
| 133 | // TODO(davidben): Should this check be removed? This matches OpenSSL, but |
| 134 | // r2i-based extensions do not necessarily require a config database. The |
| 135 | // two built-in extensions only use it some of the time, and already handle |
| 136 | // |X509V3_get_section| returning NULL. |
| 137 | if (!ctx->db) { |
| 138 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_CONFIG_DATABASE); |
| 139 | return NULL; |
| 140 | } |
| 141 | if (!(ext_struc = method->r2i(method, ctx, value))) { |
| 142 | return NULL; |
| 143 | } |
| 144 | } else { |
| 145 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED); |
| 146 | ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid)); |
| 147 | return NULL; |
| 148 | } |
| 149 |
| 150 | ext = do_ext_i2d(method, ext_nid, crit, ext_struc); |
| 151 | ASN1_item_free(reinterpret_cast<ASN1_VALUE *>(ext_struc), |
| 152 | ASN1_ITEM_ptr(method->it)); |
| 153 | return ext; |
| 154 | } |
| 155 |
| 156 | static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, |
| 157 | int crit, void *ext_struc) { |
| 158 | // Convert the extension's internal representation to DER. |
| 159 | unsigned char *ext_der = NULL; |
| 160 | int ext_len = ASN1_item_i2d(reinterpret_cast<ASN1_VALUE *>(ext_struc), |
| 161 | &ext_der, ASN1_ITEM_ptr(method->it)); |
| 162 | if (ext_len < 0) { |
| 163 | return NULL; |
| 164 | } |
| 165 |
| 166 | ASN1_OCTET_STRING *ext_oct = ASN1_OCTET_STRING_new(); |
| 167 | if (ext_oct == NULL) { |
| 168 | OPENSSL_free(ext_der); |
| 169 | return NULL; |
| 170 | } |
| 171 | ASN1_STRING_set0(ext_oct, ext_der, ext_len); |
| 172 |
| 173 | X509_EXTENSION *ext = |
| 174 | X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct); |
| 175 | ASN1_OCTET_STRING_free(ext_oct); |
| 176 | return ext; |
| 177 | } |
| 178 |
| 179 | // Given an internal structure, nid and critical flag create an extension |
| 180 |
| 181 | X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc) { |
| 182 | const X509V3_EXT_METHOD *method; |
| 183 | if (!(method = X509V3_EXT_get_nid(ext_nid))) { |
| 184 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION); |
| 185 | return NULL; |
| 186 | } |
| 187 | return do_ext_i2d(method, ext_nid, crit, ext_struc); |
| 188 | } |
| 189 |
| 190 | // Check the extension string for critical flag |
| 191 | static int v3_check_critical(const char **value) { |
| 192 | const char *p = *value; |
| 193 | if ((strlen(p) < 9) || strncmp(p, "critical,", 9)) { |
| 194 | return 0; |
| 195 | } |
| 196 | p += 9; |
| 197 | while (OPENSSL_isspace((unsigned char)*p)) { |
| 198 | p++; |
| 199 | } |
| 200 | *value = p; |
| 201 | return 1; |
| 202 | } |
| 203 |
| 204 | // Check extension string for generic extension and return the type |
| 205 | static int v3_check_generic(const char **value) { |
| 206 | int gen_type = 0; |
| 207 | const char *p = *value; |
| 208 | if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) { |
| 209 | p += 4; |
| 210 | gen_type = 1; |
| 211 | } else if ((strlen(p) >= 5) && !strncmp(p, "ASN1:", 5)) { |
| 212 | p += 5; |
| 213 | gen_type = 2; |
| 214 | } else { |
| 215 | return 0; |
| 216 | } |
| 217 |
| 218 | while (OPENSSL_isspace((unsigned char)*p)) { |
| 219 | p++; |
| 220 | } |
| 221 | *value = p; |
| 222 | return gen_type; |
| 223 | } |
| 224 |
| 225 | // Create a generic extension: for now just handle DER type |
| 226 | static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, |
| 227 | int crit, int gen_type, |
| 228 | const X509V3_CTX *ctx) { |
| 229 | unsigned char *ext_der = NULL; |
| 230 | size_t ext_len = 0; |
| 231 | ASN1_OBJECT *obj = NULL; |
| 232 | ASN1_OCTET_STRING *oct = NULL; |
| 233 | X509_EXTENSION *extension = NULL; |
| 234 | if (!(obj = OBJ_txt2obj(ext, 0))) { |
| 235 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NAME_ERROR); |
| 236 | ERR_add_error_data(2, "name=", ext); |
| 237 | goto err; |
| 238 | } |
| 239 |
| 240 | if (gen_type == 1) { |
| 241 | ext_der = x509v3_hex_to_bytes(value, &ext_len); |
| 242 | } else if (gen_type == 2) { |
| 243 | ext_der = generic_asn1(value, ctx, &ext_len); |
| 244 | } |
| 245 |
| 246 | if (ext_der == NULL) { |
| 247 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_VALUE_ERROR); |
| 248 | ERR_add_error_data(2, "value=", value); |
| 249 | goto err; |
| 250 | } |
| 251 |
| 252 | if (ext_len > INT_MAX) { |
| 253 | OPENSSL_PUT_ERROR(X509V3, ERR_R_OVERFLOW); |
| 254 | goto err; |
| 255 | } |
| 256 |
| 257 | oct = ASN1_OCTET_STRING_new(); |
| 258 | if (oct == NULL) { |
| 259 | goto err; |
| 260 | } |
| 261 |
| 262 | ASN1_STRING_set0(oct, ext_der, (int)ext_len); |
| 263 | ext_der = NULL; |
| 264 |
| 265 | extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct); |
| 266 |
| 267 | err: |
| 268 | ASN1_OBJECT_free(obj); |
| 269 | ASN1_OCTET_STRING_free(oct); |
| 270 | OPENSSL_free(ext_der); |
| 271 | return extension; |
| 272 | } |
| 273 |
| 274 | static unsigned char *generic_asn1(const char *value, const X509V3_CTX *ctx, |
| 275 | size_t *ext_len) { |
| 276 | ASN1_TYPE *typ = ASN1_generate_v3(value, ctx); |
| 277 | if (typ == NULL) { |
| 278 | return NULL; |
| 279 | } |
| 280 | unsigned char *ext_der = NULL; |
| 281 | int len = i2d_ASN1_TYPE(typ, &ext_der); |
| 282 | ASN1_TYPE_free(typ); |
| 283 | if (len < 0) { |
| 284 | return NULL; |
| 285 | } |
| 286 | *ext_len = len; |
| 287 | return ext_der; |
| 288 | } |
| 289 |
| 290 | // This is the main function: add a bunch of extensions based on a config |
| 291 | // file section to an extension STACK. |
| 292 |
| 293 | int X509V3_EXT_add_nconf_sk(const CONF *conf, const X509V3_CTX *ctx, |
| 294 | const char *section, |
| 295 | STACK_OF(X509_EXTENSION) **sk) { |
| 296 | const STACK_OF(CONF_VALUE) *nval = NCONF_get_section(conf, section); |
| 297 | if (nval == NULL) { |
| 298 | return 0; |
| 299 | } |
| 300 | for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
| 301 | const CONF_VALUE *val = sk_CONF_VALUE_value(nval, i); |
| 302 | X509_EXTENSION *ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value); |
| 303 | int ok = ext != NULL && // |
| 304 | (sk == NULL || X509v3_add_ext(sk, ext, -1) != NULL); |
| 305 | X509_EXTENSION_free(ext); |
| 306 | if (!ok) { |
| 307 | return 0; |
| 308 | } |
| 309 | } |
| 310 | return 1; |
| 311 | } |
| 312 |
| 313 | // Convenience functions to add extensions to a certificate, CRL and request |
| 314 |
| 315 | int X509V3_EXT_add_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 316 | const char *section, X509 *cert) { |
| 317 | STACK_OF(X509_EXTENSION) **sk = NULL; |
| 318 | if (cert) { |
| 319 | sk = &cert->cert_info->extensions; |
| 320 | } |
| 321 | return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); |
| 322 | } |
| 323 |
| 324 | // Same as above but for a CRL |
| 325 |
| 326 | int X509V3_EXT_CRL_add_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 327 | const char *section, X509_CRL *crl) { |
| 328 | STACK_OF(X509_EXTENSION) **sk = NULL; |
| 329 | if (crl) { |
| 330 | sk = &crl->crl->extensions; |
| 331 | } |
| 332 | return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); |
| 333 | } |
| 334 |
| 335 | // Add extensions to certificate request |
| 336 |
| 337 | int X509V3_EXT_REQ_add_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 338 | const char *section, X509_REQ *req) { |
| 339 | STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL; |
| 340 | int i; |
| 341 | if (req) { |
| 342 | sk = &extlist; |
| 343 | } |
| 344 | i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); |
| 345 | if (!i || !sk) { |
| 346 | return i; |
| 347 | } |
| 348 | i = X509_REQ_add_extensions(req, extlist); |
| 349 | sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free); |
| 350 | return i; |
| 351 | } |
| 352 |
| 353 | // Config database functions |
| 354 |
| 355 | const STACK_OF(CONF_VALUE) *X509V3_get_section(const X509V3_CTX *ctx, |
| 356 | const char *section) { |
| 357 | if (ctx->db == NULL) { |
| 358 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED); |
| 359 | return NULL; |
| 360 | } |
| 361 | return NCONF_get_section(ctx->db, section); |
| 362 | } |
| 363 |
| 364 | void X509V3_set_nconf(X509V3_CTX *ctx, const CONF *conf) { ctx->db = conf; } |
| 365 |
| 366 | void X509V3_set_ctx(X509V3_CTX *ctx, const X509 *issuer, const X509 *subj, |
| 367 | const X509_REQ *req, const X509_CRL *crl, int flags) { |
| 368 | OPENSSL_memset(ctx, 0, sizeof(*ctx)); |
| 369 | ctx->issuer_cert = issuer; |
| 370 | ctx->subject_cert = subj; |
| 371 | ctx->crl = crl; |
| 372 | ctx->subject_req = req; |
| 373 | ctx->flags = flags; |
| 374 | } |
| 375 |