| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999. |
| 2 |
// Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include <openssl/asn1.h> |
| 9 |
#include <openssl/asn1t.h> |
| 10 |
#include <openssl/conf.h> |
| 11 |
#include <openssl/err.h> |
| 12 |
#include <openssl/mem.h> |
| 13 |
#include <openssl/obj.h> |
| 14 |
#include <openssl/stack.h> |
| 15 |
#include <openssl/x509.h> |
| 16 |
|
| 17 |
#include "internal.h" |
| 18 |
|
| 19 |
// Certificate policies extension support: this one is a bit complex... |
| 20 |
|
| 21 |
static int i2r_certpol(const X509V3_EXT_METHOD *method, void *ext, BIO *out, |
| 22 |
int indent); |
| 23 |
static void *r2i_certpol(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, |
| 24 |
const char *value); |
| 25 |
static void print_qualifiers(BIO *out, const STACK_OF(POLICYQUALINFO) *quals, |
| 26 |
int indent); |
| 27 |
static void print_notice(BIO *out, const USERNOTICE *notice, int indent); |
| 28 |
static POLICYINFO *policy_section(const X509V3_CTX *ctx, |
| 29 |
const STACK_OF(CONF_VALUE) *polstrs, |
| 30 |
int ia5org); |
| 31 |
static POLICYQUALINFO *notice_section(const X509V3_CTX *ctx, |
| 32 |
const STACK_OF(CONF_VALUE) *unot, |
| 33 |
int ia5org); |
| 34 |
static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, |
| 35 |
const STACK_OF(CONF_VALUE) *nos); |
| 36 |
|
| 37 |
const X509V3_EXT_METHOD v3_cpols = { |
| 38 |
NID_certificate_policies, |
| 39 |
0, |
| 40 |
ASN1_ITEM_ref(CERTIFICATEPOLICIES), |
| 41 |
0, |
| 42 |
0, |
| 43 |
0, |
| 44 |
0, |
| 45 |
0, |
| 46 |
0, |
| 47 |
0, |
| 48 |
0, |
| 49 |
i2r_certpol, |
| 50 |
r2i_certpol, |
| 51 |
NULL, |
| 52 |
}; |
| 53 |
|
| 54 |
DECLARE_ASN1_ITEM(POLICYINFO) |
| 55 |
DECLARE_ASN1_ITEM(POLICYQUALINFO) |
| 56 |
DECLARE_ASN1_ITEM(USERNOTICE) |
| 57 |
DECLARE_ASN1_ITEM(NOTICEREF) |
| 58 |
|
| 59 |
ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) = ASN1_EX_TEMPLATE_TYPE( |
| 60 |
ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO) |
| 61 |
ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES) |
| 62 |
|
| 63 |
IMPLEMENT_ASN1_FUNCTIONS_const(CERTIFICATEPOLICIES) |
| 64 |
|
| 65 |
ASN1_SEQUENCE(POLICYINFO) = { |
| 66 |
ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT), |
| 67 |
ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO), |
| 68 |
} ASN1_SEQUENCE_END(POLICYINFO) |
| 69 |
|
| 70 |
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICYINFO) |
| 71 |
|
| 72 |
ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, |
| 73 |
ASN1_ANY); |
| 74 |
|
| 75 |
ASN1_ADB(POLICYQUALINFO) = { |
| 76 |
ADB_ENTRY(NID_id_qt_cps, |
| 77 |
ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)), |
| 78 |
ADB_ENTRY(NID_id_qt_unotice, |
| 79 |
ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE)), |
| 80 |
} ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL); |
| 81 |
|
| 82 |
ASN1_SEQUENCE(POLICYQUALINFO) = { |
| 83 |
ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT), |
| 84 |
ASN1_ADB_OBJECT(POLICYQUALINFO), |
| 85 |
} ASN1_SEQUENCE_END(POLICYQUALINFO) |
| 86 |
|
| 87 |
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICYQUALINFO) |
| 88 |
|
| 89 |
ASN1_SEQUENCE(USERNOTICE) = { |
| 90 |
ASN1_OPT(USERNOTICE, noticeref, NOTICEREF), |
| 91 |
ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT), |
| 92 |
} ASN1_SEQUENCE_END(USERNOTICE) |
| 93 |
|
| 94 |
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(USERNOTICE) |
| 95 |
|
| 96 |
ASN1_SEQUENCE(NOTICEREF) = { |
| 97 |
ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT), |
| 98 |
ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER), |
| 99 |
} ASN1_SEQUENCE_END(NOTICEREF) |
| 100 |
|
| 101 |
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NOTICEREF) |
| 102 |
|
| 103 |
static void *r2i_certpol(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, |
| 104 |
const char *value) { |
| 105 |
STACK_OF(POLICYINFO) *pols = sk_POLICYINFO_new_null(); |
| 106 |
if (pols == NULL) { |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value); |
| 110 |
if (vals == NULL) { |
| 111 |
OPENSSL_PUT_ERROR(X509V3, ERR_R_X509V3_LIB); |
| 112 |
goto err; |
| 113 |
} |
| 114 |
int ia5org = 0; |
| 115 |
for (size_t i = 0; i < sk_CONF_VALUE_num(vals); i++) { |
| 116 |
const CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i); |
| 117 |
if (cnf->value || !cnf->name) { |
| 118 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_POLICY_IDENTIFIER); |
| 119 |
X509V3_conf_err(cnf); |
| 120 |
goto err; |
| 121 |
} |
| 122 |
POLICYINFO *pol; |
| 123 |
const char *pstr = cnf->name; |
| 124 |
if (!strcmp(pstr, "ia5org")) { |
| 125 |
ia5org = 1; |
| 126 |
continue; |
| 127 |
} else if (*pstr == '@') { |
| 128 |
const STACK_OF(CONF_VALUE) *polsect = X509V3_get_section(ctx, pstr + 1); |
| 129 |
if (!polsect) { |
| 130 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION); |
| 131 |
|
| 132 |
X509V3_conf_err(cnf); |
| 133 |
goto err; |
| 134 |
} |
| 135 |
pol = policy_section(ctx, polsect, ia5org); |
| 136 |
if (!pol) { |
| 137 |
goto err; |
| 138 |
} |
| 139 |
} else { |
| 140 |
ASN1_OBJECT *pobj = OBJ_txt2obj(cnf->name, 0); |
| 141 |
if (pobj == NULL) { |
| 142 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER); |
| 143 |
X509V3_conf_err(cnf); |
| 144 |
goto err; |
| 145 |
} |
| 146 |
pol = POLICYINFO_new(); |
| 147 |
if (pol == NULL) { |
| 148 |
ASN1_OBJECT_free(pobj); |
| 149 |
goto err; |
| 150 |
} |
| 151 |
pol->policyid = pobj; |
| 152 |
} |
| 153 |
if (!sk_POLICYINFO_push(pols, pol)) { |
| 154 |
POLICYINFO_free(pol); |
| 155 |
goto err; |
| 156 |
} |
| 157 |
} |
| 158 |
sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); |
| 159 |
return pols; |
| 160 |
err: |
| 161 |
sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); |
| 162 |
sk_POLICYINFO_pop_free(pols, POLICYINFO_free); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
|
| 166 |
static POLICYINFO *policy_section(const X509V3_CTX *ctx, |
| 167 |
const STACK_OF(CONF_VALUE) *polstrs, |
| 168 |
int ia5org) { |
| 169 |
POLICYINFO *pol; |
| 170 |
POLICYQUALINFO *qual; |
| 171 |
if (!(pol = POLICYINFO_new())) { |
| 172 |
goto err; |
| 173 |
} |
| 174 |
for (size_t i = 0; i < sk_CONF_VALUE_num(polstrs); i++) { |
| 175 |
const CONF_VALUE *cnf = sk_CONF_VALUE_value(polstrs, i); |
| 176 |
if (!strcmp(cnf->name, "policyIdentifier")) { |
| 177 |
ASN1_OBJECT *pobj; |
| 178 |
if (!(pobj = OBJ_txt2obj(cnf->value, 0))) { |
| 179 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER); |
| 180 |
X509V3_conf_err(cnf); |
| 181 |
goto err; |
| 182 |
} |
| 183 |
pol->policyid = pobj; |
| 184 |
|
| 185 |
} else if (x509v3_conf_name_matches(cnf->name, "CPS")) { |
| 186 |
if (!pol->qualifiers) { |
| 187 |
pol->qualifiers = sk_POLICYQUALINFO_new_null(); |
| 188 |
} |
| 189 |
if (!(qual = POLICYQUALINFO_new())) { |
| 190 |
goto err; |
| 191 |
} |
| 192 |
if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) { |
| 193 |
goto err; |
| 194 |
} |
| 195 |
qual->pqualid = OBJ_nid2obj(NID_id_qt_cps); |
| 196 |
if (qual->pqualid == NULL) { |
| 197 |
OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR); |
| 198 |
goto err; |
| 199 |
} |
| 200 |
qual->d.cpsuri = ASN1_IA5STRING_new(); |
| 201 |
if (qual->d.cpsuri == NULL) { |
| 202 |
goto err; |
| 203 |
} |
| 204 |
if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value, strlen(cnf->value))) { |
| 205 |
goto err; |
| 206 |
} |
| 207 |
} else if (x509v3_conf_name_matches(cnf->name, "userNotice")) { |
| 208 |
if (*cnf->value != '@') { |
| 209 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXPECTED_A_SECTION_NAME); |
| 210 |
X509V3_conf_err(cnf); |
| 211 |
goto err; |
| 212 |
} |
| 213 |
const STACK_OF(CONF_VALUE) *unot = |
| 214 |
X509V3_get_section(ctx, cnf->value + 1); |
| 215 |
if (!unot) { |
| 216 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION); |
| 217 |
X509V3_conf_err(cnf); |
| 218 |
goto err; |
| 219 |
} |
| 220 |
qual = notice_section(ctx, unot, ia5org); |
| 221 |
if (!qual) { |
| 222 |
goto err; |
| 223 |
} |
| 224 |
if (!pol->qualifiers) { |
| 225 |
pol->qualifiers = sk_POLICYQUALINFO_new_null(); |
| 226 |
} |
| 227 |
if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) { |
| 228 |
goto err; |
| 229 |
} |
| 230 |
} else { |
| 231 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION); |
| 232 |
|
| 233 |
X509V3_conf_err(cnf); |
| 234 |
goto err; |
| 235 |
} |
| 236 |
} |
| 237 |
if (!pol->policyid) { |
| 238 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_POLICY_IDENTIFIER); |
| 239 |
goto err; |
| 240 |
} |
| 241 |
|
| 242 |
return pol; |
| 243 |
|
| 244 |
err: |
| 245 |
POLICYINFO_free(pol); |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
|
| 249 |
static POLICYQUALINFO *notice_section(const X509V3_CTX *ctx, |
| 250 |
const STACK_OF(CONF_VALUE) *unot, |
| 251 |
int ia5org) { |
| 252 |
USERNOTICE *notice; |
| 253 |
POLICYQUALINFO *qual; |
| 254 |
if (!(qual = POLICYQUALINFO_new())) { |
| 255 |
goto err; |
| 256 |
} |
| 257 |
qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice); |
| 258 |
if (qual->pqualid == NULL) { |
| 259 |
OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR); |
| 260 |
goto err; |
| 261 |
} |
| 262 |
if (!(notice = USERNOTICE_new())) { |
| 263 |
goto err; |
| 264 |
} |
| 265 |
qual->d.usernotice = notice; |
| 266 |
for (size_t i = 0; i < sk_CONF_VALUE_num(unot); i++) { |
| 267 |
const CONF_VALUE *cnf = sk_CONF_VALUE_value(unot, i); |
| 268 |
if (!strcmp(cnf->name, "explicitText")) { |
| 269 |
notice->exptext = ASN1_VISIBLESTRING_new(); |
| 270 |
if (notice->exptext == NULL) { |
| 271 |
goto err; |
| 272 |
} |
| 273 |
if (!ASN1_STRING_set(notice->exptext, cnf->value, strlen(cnf->value))) { |
| 274 |
goto err; |
| 275 |
} |
| 276 |
} else if (!strcmp(cnf->name, "organization")) { |
| 277 |
NOTICEREF *nref; |
| 278 |
if (!notice->noticeref) { |
| 279 |
if (!(nref = NOTICEREF_new())) { |
| 280 |
goto err; |
| 281 |
} |
| 282 |
notice->noticeref = nref; |
| 283 |
} else { |
| 284 |
nref = notice->noticeref; |
| 285 |
} |
| 286 |
if (ia5org) { |
| 287 |
nref->organization->type = V_ASN1_IA5STRING; |
| 288 |
} else { |
| 289 |
nref->organization->type = V_ASN1_VISIBLESTRING; |
| 290 |
} |
| 291 |
if (!ASN1_STRING_set(nref->organization, cnf->value, |
| 292 |
strlen(cnf->value))) { |
| 293 |
goto err; |
| 294 |
} |
| 295 |
} else if (!strcmp(cnf->name, "noticeNumbers")) { |
| 296 |
NOTICEREF *nref; |
| 297 |
STACK_OF(CONF_VALUE) *nos; |
| 298 |
if (!notice->noticeref) { |
| 299 |
if (!(nref = NOTICEREF_new())) { |
| 300 |
goto err; |
| 301 |
} |
| 302 |
notice->noticeref = nref; |
| 303 |
} else { |
| 304 |
nref = notice->noticeref; |
| 305 |
} |
| 306 |
nos = X509V3_parse_list(cnf->value); |
| 307 |
if (!nos || !sk_CONF_VALUE_num(nos)) { |
| 308 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBERS); |
| 309 |
X509V3_conf_err(cnf); |
| 310 |
goto err; |
| 311 |
} |
| 312 |
int ret = nref_nos(nref->noticenos, nos); |
| 313 |
sk_CONF_VALUE_pop_free(nos, X509V3_conf_free); |
| 314 |
if (!ret) { |
| 315 |
goto err; |
| 316 |
} |
| 317 |
} else { |
| 318 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION); |
| 319 |
X509V3_conf_err(cnf); |
| 320 |
goto err; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if (notice->noticeref && |
| 325 |
(!notice->noticeref->noticenos || !notice->noticeref->organization)) { |
| 326 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_NEED_ORGANIZATION_AND_NUMBERS); |
| 327 |
goto err; |
| 328 |
} |
| 329 |
|
| 330 |
return qual; |
| 331 |
|
| 332 |
err: |
| 333 |
POLICYQUALINFO_free(qual); |
| 334 |
return NULL; |
| 335 |
} |
| 336 |
|
| 337 |
static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, |
| 338 |
const STACK_OF(CONF_VALUE) *nos) { |
| 339 |
for (size_t i = 0; i < sk_CONF_VALUE_num(nos); i++) { |
| 340 |
const CONF_VALUE *cnf = sk_CONF_VALUE_value(nos, i); |
| 341 |
ASN1_INTEGER *aint = s2i_ASN1_INTEGER(NULL, cnf->name); |
| 342 |
if (aint == NULL) { |
| 343 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBER); |
| 344 |
return 0; |
| 345 |
} |
| 346 |
if (!sk_ASN1_INTEGER_push(nnums, aint)) { |
| 347 |
ASN1_INTEGER_free(aint); |
| 348 |
return 0; |
| 349 |
} |
| 350 |
} |
| 351 |
return 1; |
| 352 |
} |
| 353 |
|
| 354 |
static int i2r_certpol(const X509V3_EXT_METHOD *method, void *ext, BIO *out, |
| 355 |
int indent) { |
| 356 |
const STACK_OF(POLICYINFO) *pol = ext; |
| 357 |
// First print out the policy OIDs |
| 358 |
for (size_t i = 0; i < sk_POLICYINFO_num(pol); i++) { |
| 359 |
const POLICYINFO *pinfo = sk_POLICYINFO_value(pol, i); |
| 360 |
BIO_printf(out, "%*sPolicy: ", indent, ""); |
| 361 |
i2a_ASN1_OBJECT(out, pinfo->policyid); |
| 362 |
BIO_puts(out, "\n"); |
| 363 |
if (pinfo->qualifiers) { |
| 364 |
print_qualifiers(out, pinfo->qualifiers, indent + 2); |
| 365 |
} |
| 366 |
} |
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
static void print_qualifiers(BIO *out, const STACK_OF(POLICYQUALINFO) *quals, |
| 371 |
int indent) { |
| 372 |
for (size_t i = 0; i < sk_POLICYQUALINFO_num(quals); i++) { |
| 373 |
const POLICYQUALINFO *qualinfo = sk_POLICYQUALINFO_value(quals, i); |
| 374 |
switch (OBJ_obj2nid(qualinfo->pqualid)) { |
| 375 |
case NID_id_qt_cps: |
| 376 |
BIO_printf(out, "%*sCPS: %.*s\n", indent, "", |
| 377 |
qualinfo->d.cpsuri->length, qualinfo->d.cpsuri->data); |
| 378 |
break; |
| 379 |
|
| 380 |
case NID_id_qt_unotice: |
| 381 |
BIO_printf(out, "%*sUser Notice:\n", indent, ""); |
| 382 |
print_notice(out, qualinfo->d.usernotice, indent + 2); |
| 383 |
break; |
| 384 |
|
| 385 |
default: |
| 386 |
BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, ""); |
| 387 |
|
| 388 |
i2a_ASN1_OBJECT(out, qualinfo->pqualid); |
| 389 |
BIO_puts(out, "\n"); |
| 390 |
break; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
static void print_notice(BIO *out, const USERNOTICE *notice, int indent) { |
| 396 |
if (notice->noticeref) { |
| 397 |
NOTICEREF *ref; |
| 398 |
ref = notice->noticeref; |
| 399 |
BIO_printf(out, "%*sOrganization: %.*s\n", indent, "", |
| 400 |
ref->organization->length, ref->organization->data); |
| 401 |
BIO_printf(out, "%*sNumber%s: ", indent, "", |
| 402 |
sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : ""); |
| 403 |
for (size_t i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) { |
| 404 |
ASN1_INTEGER *num; |
| 405 |
char *tmp; |
| 406 |
num = sk_ASN1_INTEGER_value(ref->noticenos, i); |
| 407 |
if (i) { |
| 408 |
BIO_puts(out, ", "); |
| 409 |
} |
| 410 |
if (num == NULL) { |
| 411 |
BIO_puts(out, "(null)"); |
| 412 |
} else { |
| 413 |
tmp = i2s_ASN1_INTEGER(NULL, num); |
| 414 |
if (tmp == NULL) { |
| 415 |
return; |
| 416 |
} |
| 417 |
BIO_puts(out, tmp); |
| 418 |
OPENSSL_free(tmp); |
| 419 |
} |
| 420 |
} |
| 421 |
BIO_puts(out, "\n"); |
| 422 |
} |
| 423 |
if (notice->exptext) { |
| 424 |
BIO_printf(out, "%*sExplicit Text: %.*s\n", indent, "", |
| 425 |
notice->exptext->length, notice->exptext->data); |
| 426 |
} |
| 427 |
} |