| 1 |
/* |
| 2 |
* Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* Copyright Nokia 2007-2019 |
| 4 |
* Copyright Siemens AG 2015-2019 |
| 5 |
* |
| 6 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 7 |
* this file except in compliance with the License. You can obtain a copy |
| 8 |
* in the file LICENSE in the source distribution or at |
| 9 |
* https://www.openssl.org/source/license.html |
| 10 |
*/ |
| 11 |
|
| 12 |
/* CMP functions for PKIMessage construction */ |
| 13 |
|
| 14 |
#include "cmp_local.h" |
| 15 |
|
| 16 |
#include <internal/cms.h> /* for ossl_cms_sign_encrypt() */ |
| 17 |
|
| 18 |
OSSL_CMP_MSG *OSSL_CMP_MSG_new(OSSL_LIB_CTX *libctx, const char *propq) |
| 19 |
{ |
| 20 |
OSSL_CMP_MSG *msg = NULL; |
| 21 |
|
| 22 |
msg = (OSSL_CMP_MSG *)ASN1_item_new_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), |
| 23 |
libctx, propq); |
| 24 |
if (!ossl_cmp_msg_set0_libctx(msg, libctx, propq)) { |
| 25 |
OSSL_CMP_MSG_free(msg); |
| 26 |
msg = NULL; |
| 27 |
} |
| 28 |
return msg; |
| 29 |
} |
| 30 |
|
| 31 |
void OSSL_CMP_MSG_free(OSSL_CMP_MSG *msg) |
| 32 |
{ |
| 33 |
ASN1_item_free((ASN1_VALUE *)msg, ASN1_ITEM_rptr(OSSL_CMP_MSG)); |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* This should only be used if the X509 object was embedded inside another |
| 38 |
* asn1 object and it needs a libctx to operate. |
| 39 |
* Use OSSL_CMP_MSG_new() instead if possible. |
| 40 |
*/ |
| 41 |
int ossl_cmp_msg_set0_libctx(OSSL_CMP_MSG *msg, OSSL_LIB_CTX *libctx, |
| 42 |
const char *propq) |
| 43 |
{ |
| 44 |
if (msg != NULL) { |
| 45 |
msg->libctx = libctx; |
| 46 |
OPENSSL_free(msg->propq); |
| 47 |
msg->propq = NULL; |
| 48 |
if (propq != NULL) { |
| 49 |
msg->propq = OPENSSL_strdup(propq); |
| 50 |
if (msg->propq == NULL) |
| 51 |
return 0; |
| 52 |
} |
| 53 |
} |
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
OSSL_CMP_PKIHEADER *OSSL_CMP_MSG_get0_header(const OSSL_CMP_MSG *msg) |
| 58 |
{ |
| 59 |
if (msg == NULL) { |
| 60 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
return msg->header; |
| 64 |
} |
| 65 |
|
| 66 |
const char *ossl_cmp_bodytype_to_string(int type) |
| 67 |
{ |
| 68 |
static const char *type_names[] = { |
| 69 |
"IR", |
| 70 |
"IP", |
| 71 |
"CR", |
| 72 |
"CP", |
| 73 |
"P10CR", |
| 74 |
"POPDECC", |
| 75 |
"POPDECR", |
| 76 |
"KUR", |
| 77 |
"KUP", |
| 78 |
"KRR", |
| 79 |
"KRP", |
| 80 |
"RR", |
| 81 |
"RP", |
| 82 |
"CCR", |
| 83 |
"CCP", |
| 84 |
"CKUANN", |
| 85 |
"CANN", |
| 86 |
"RANN", |
| 87 |
"CRLANN", |
| 88 |
"PKICONF", |
| 89 |
"NESTED", |
| 90 |
"GENM", |
| 91 |
"GENP", |
| 92 |
"ERROR", |
| 93 |
"CERTCONF", |
| 94 |
"POLLREQ", |
| 95 |
"POLLREP", |
| 96 |
}; |
| 97 |
|
| 98 |
if (type < 0 || type > OSSL_CMP_PKIBODY_TYPE_MAX) |
| 99 |
return "illegal body type"; |
| 100 |
return type_names[type]; |
| 101 |
} |
| 102 |
|
| 103 |
int ossl_cmp_msg_set_bodytype(OSSL_CMP_MSG *msg, int type) |
| 104 |
{ |
| 105 |
if (!ossl_assert(msg != NULL && msg->body != NULL)) |
| 106 |
return 0; |
| 107 |
|
| 108 |
msg->body->type = type; |
| 109 |
return 1; |
| 110 |
} |
| 111 |
|
| 112 |
int OSSL_CMP_MSG_get_bodytype(const OSSL_CMP_MSG *msg) |
| 113 |
{ |
| 114 |
if (!ossl_assert(msg != NULL && msg->body != NULL)) |
| 115 |
return -1; |
| 116 |
|
| 117 |
return msg->body->type; |
| 118 |
} |
| 119 |
|
| 120 |
X509_PUBKEY *OSSL_CMP_MSG_get0_certreq_publickey(const OSSL_CMP_MSG *msg) |
| 121 |
{ |
| 122 |
const OSSL_CRMF_MSGS *reqs; |
| 123 |
const OSSL_CRMF_MSG *crm; |
| 124 |
const OSSL_CRMF_CERTTEMPLATE *tmpl; |
| 125 |
X509_PUBKEY *pubkey; |
| 126 |
|
| 127 |
switch (OSSL_CMP_MSG_get_bodytype(msg)) { |
| 128 |
case OSSL_CMP_PKIBODY_IR: |
| 129 |
case OSSL_CMP_PKIBODY_CR: |
| 130 |
case OSSL_CMP_PKIBODY_KUR: |
| 131 |
reqs = msg->body->value.ir; /* value.ir is same for cr and kur */ |
| 132 |
if ((crm = sk_OSSL_CRMF_MSG_value(reqs, 0)) == NULL) { |
| 133 |
ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
if ((tmpl = OSSL_CRMF_MSG_get0_tmpl(crm)) == NULL |
| 137 |
|| (pubkey = OSSL_CRMF_CERTTEMPLATE_get0_publicKey(tmpl)) == NULL) { |
| 138 |
ERR_raise(ERR_LIB_CMP, CRMF_R_POPO_MISSING_PUBLIC_KEY); |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
return pubkey; |
| 142 |
default: |
| 143 |
ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/* Add an extension to the referenced extension stack, which may be NULL */ |
| 149 |
static int add1_extension(X509_EXTENSIONS **pexts, int nid, int crit, void *ex) |
| 150 |
{ |
| 151 |
X509_EXTENSION *ext; |
| 152 |
int res; |
| 153 |
|
| 154 |
if (!ossl_assert(pexts != NULL)) /* pointer to var must not be NULL */ |
| 155 |
return 0; |
| 156 |
|
| 157 |
if ((ext = X509V3_EXT_i2d(nid, crit, ex)) == NULL) |
| 158 |
return 0; |
| 159 |
|
| 160 |
res = X509v3_add_ext(pexts, ext, 0) != NULL; |
| 161 |
X509_EXTENSION_free(ext); |
| 162 |
return res; |
| 163 |
} |
| 164 |
|
| 165 |
/* Add a CRL revocation reason code to extension stack, which may be NULL */ |
| 166 |
static int add_crl_reason_extension(X509_EXTENSIONS **pexts, int reason_code) |
| 167 |
{ |
| 168 |
ASN1_ENUMERATED *val = ASN1_ENUMERATED_new(); |
| 169 |
int res = 0; |
| 170 |
|
| 171 |
if (val != NULL && ASN1_ENUMERATED_set(val, reason_code)) |
| 172 |
res = add1_extension(pexts, NID_crl_reason, 0 /* non-critical */, val); |
| 173 |
ASN1_ENUMERATED_free(val); |
| 174 |
return res; |
| 175 |
} |
| 176 |
|
| 177 |
OSSL_CMP_MSG *ossl_cmp_msg_create(OSSL_CMP_CTX *ctx, int bodytype) |
| 178 |
{ |
| 179 |
OSSL_CMP_MSG *msg = NULL; |
| 180 |
|
| 181 |
if (!ossl_assert(ctx != NULL)) |
| 182 |
return NULL; |
| 183 |
|
| 184 |
if ((msg = OSSL_CMP_MSG_new(ctx->libctx, ctx->propq)) == NULL) |
| 185 |
return NULL; |
| 186 |
if (!ossl_cmp_hdr_init(ctx, msg->header) |
| 187 |
|| !ossl_cmp_msg_set_bodytype(msg, bodytype)) |
| 188 |
goto err; |
| 189 |
if (ctx->geninfo_ITAVs != NULL |
| 190 |
&& !ossl_cmp_hdr_generalInfo_push1_items(msg->header, |
| 191 |
ctx->geninfo_ITAVs)) |
| 192 |
goto err; |
| 193 |
|
| 194 |
switch (bodytype) { |
| 195 |
case OSSL_CMP_PKIBODY_IR: |
| 196 |
case OSSL_CMP_PKIBODY_CR: |
| 197 |
case OSSL_CMP_PKIBODY_KUR: |
| 198 |
if ((msg->body->value.ir = OSSL_CRMF_MSGS_new()) == NULL) |
| 199 |
goto err; |
| 200 |
return msg; |
| 201 |
|
| 202 |
case OSSL_CMP_PKIBODY_P10CR: |
| 203 |
if (ctx->p10CSR == NULL) { |
| 204 |
ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_P10CSR); |
| 205 |
goto err; |
| 206 |
} |
| 207 |
if ((msg->body->value.p10cr = X509_REQ_dup(ctx->p10CSR)) == NULL) |
| 208 |
goto err; |
| 209 |
return msg; |
| 210 |
|
| 211 |
case OSSL_CMP_PKIBODY_IP: |
| 212 |
case OSSL_CMP_PKIBODY_CP: |
| 213 |
case OSSL_CMP_PKIBODY_KUP: |
| 214 |
if ((msg->body->value.ip = OSSL_CMP_CERTREPMESSAGE_new()) == NULL) |
| 215 |
goto err; |
| 216 |
return msg; |
| 217 |
|
| 218 |
case OSSL_CMP_PKIBODY_RR: |
| 219 |
if ((msg->body->value.rr = sk_OSSL_CMP_REVDETAILS_new_null()) == NULL) |
| 220 |
goto err; |
| 221 |
return msg; |
| 222 |
case OSSL_CMP_PKIBODY_RP: |
| 223 |
if ((msg->body->value.rp = OSSL_CMP_REVREPCONTENT_new()) == NULL) |
| 224 |
goto err; |
| 225 |
return msg; |
| 226 |
|
| 227 |
case OSSL_CMP_PKIBODY_CERTCONF: |
| 228 |
if ((msg->body->value.certConf = sk_OSSL_CMP_CERTSTATUS_new_null()) == NULL) |
| 229 |
goto err; |
| 230 |
return msg; |
| 231 |
case OSSL_CMP_PKIBODY_PKICONF: |
| 232 |
if ((msg->body->value.pkiconf = ASN1_TYPE_new()) == NULL) |
| 233 |
goto err; |
| 234 |
ASN1_TYPE_set(msg->body->value.pkiconf, V_ASN1_NULL, NULL); |
| 235 |
return msg; |
| 236 |
|
| 237 |
case OSSL_CMP_PKIBODY_POLLREQ: |
| 238 |
if ((msg->body->value.pollReq = sk_OSSL_CMP_POLLREQ_new_null()) == NULL) |
| 239 |
goto err; |
| 240 |
return msg; |
| 241 |
case OSSL_CMP_PKIBODY_POLLREP: |
| 242 |
if ((msg->body->value.pollRep = sk_OSSL_CMP_POLLREP_new_null()) == NULL) |
| 243 |
goto err; |
| 244 |
return msg; |
| 245 |
|
| 246 |
case OSSL_CMP_PKIBODY_GENM: |
| 247 |
case OSSL_CMP_PKIBODY_GENP: |
| 248 |
if ((msg->body->value.genm = sk_OSSL_CMP_ITAV_new_null()) == NULL) |
| 249 |
goto err; |
| 250 |
return msg; |
| 251 |
|
| 252 |
case OSSL_CMP_PKIBODY_ERROR: |
| 253 |
if ((msg->body->value.error = OSSL_CMP_ERRORMSGCONTENT_new()) == NULL) |
| 254 |
goto err; |
| 255 |
return msg; |
| 256 |
|
| 257 |
default: |
| 258 |
ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
| 259 |
goto err; |
| 260 |
} |
| 261 |
|
| 262 |
err: |
| 263 |
OSSL_CMP_MSG_free(msg); |
| 264 |
return NULL; |
| 265 |
} |
| 266 |
|
| 267 |
#define HAS_SAN(ctx) \ |
| 268 |
(sk_GENERAL_NAME_num((ctx)->subjectAltNames) > 0 \ |
| 269 |
|| OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) |
| 270 |
|
| 271 |
static const X509_NAME *determine_subj(OSSL_CMP_CTX *ctx, int for_KUR, |
| 272 |
const X509_NAME *ref_subj) |
| 273 |
{ |
| 274 |
if (ctx->subjectName != NULL) |
| 275 |
return IS_NULL_DN(ctx->subjectName) ? NULL : ctx->subjectName; |
| 276 |
if (ctx->p10CSR != NULL) /* first default is from any given CSR */ |
| 277 |
return X509_REQ_get_subject_name(ctx->p10CSR); |
| 278 |
if (for_KUR || !HAS_SAN(ctx)) |
| 279 |
/* |
| 280 |
* For KUR, copy subject from any reference cert as fallback. |
| 281 |
* For IR or CR, do the same only if there is no subjectAltName. |
| 282 |
*/ |
| 283 |
return ref_subj; |
| 284 |
return NULL; |
| 285 |
} |
| 286 |
|
| 287 |
OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid) |
| 288 |
{ |
| 289 |
OSSL_CRMF_MSG *crm = NULL; |
| 290 |
int central_keygen = OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_POPO_METHOD) |
| 291 |
== OSSL_CRMF_POPO_NONE; |
| 292 |
X509 *refcert = ctx->oldCert != NULL ? ctx->oldCert : ctx->cert; |
| 293 |
/* refcert defaults to current client cert */ |
| 294 |
EVP_PKEY *rkey = ossl_cmp_ctx_get0_newPubkey(ctx); |
| 295 |
STACK_OF(GENERAL_NAME) *default_sans = NULL; |
| 296 |
const X509_NAME *ref_subj = refcert != NULL ? X509_get_subject_name(refcert) : NULL; |
| 297 |
const X509_NAME *subject = determine_subj(ctx, for_KUR, ref_subj); |
| 298 |
const X509_NAME *issuer = ctx->issuer != NULL || refcert == NULL |
| 299 |
? (IS_NULL_DN(ctx->issuer) ? NULL : ctx->issuer) |
| 300 |
: X509_get_issuer_name(refcert); |
| 301 |
int crit = ctx->setSubjectAltNameCritical || subject == NULL; |
| 302 |
/* RFC5280: subjectAltName MUST be critical if subject is null */ |
| 303 |
OSSL_CRMF_CERTTEMPLATE *tmpl; |
| 304 |
X509_EXTENSIONS *exts = NULL; |
| 305 |
|
| 306 |
if (rkey == NULL && !central_keygen) { |
| 307 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 308 |
ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PUBLIC_KEY); |
| 309 |
return NULL; |
| 310 |
#endif |
| 311 |
} |
| 312 |
if (for_KUR && refcert == NULL && ctx->p10CSR == NULL) { |
| 313 |
ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT); |
| 314 |
return NULL; |
| 315 |
} |
| 316 |
if ((crm = OSSL_CRMF_MSG_new()) == NULL) |
| 317 |
return NULL; |
| 318 |
tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); |
| 319 |
if (!OSSL_CRMF_MSG_set_certReqId(crm, rid) |
| 320 |
/* |
| 321 |
* fill certTemplate, corresponding to CertificationRequestInfo |
| 322 |
* of PKCS#10. The rkey param cannot be NULL so far - |
| 323 |
* it could be NULL if centralized key creation was supported |
| 324 |
*/ |
| 325 |
|| !OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_MSG_get0_tmpl(crm), rkey, |
| 326 |
subject, issuer, NULL /* serial */)) |
| 327 |
goto err; |
| 328 |
if (rkey != NULL && central_keygen) |
| 329 |
X509_PUBKEY_set0_public_key(OSSL_CRMF_CERTTEMPLATE_get0_publicKey(tmpl), |
| 330 |
NULL, 0); |
| 331 |
|
| 332 |
if (ctx->days != 0) { |
| 333 |
time_t now = time(NULL); |
| 334 |
ASN1_TIME *notBefore = ASN1_TIME_adj(NULL, now, 0, 0); |
| 335 |
ASN1_TIME *notAfter = ASN1_TIME_adj(NULL, now, ctx->days, 0); |
| 336 |
|
| 337 |
if (notBefore == NULL |
| 338 |
|| notAfter == NULL |
| 339 |
|| !OSSL_CRMF_MSG_set0_validity(crm, notBefore, notAfter)) { |
| 340 |
ASN1_TIME_free(notBefore); |
| 341 |
ASN1_TIME_free(notAfter); |
| 342 |
goto err; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/* extensions */ |
| 347 |
if (ctx->p10CSR != NULL |
| 348 |
&& (exts = X509_REQ_get_extensions(ctx->p10CSR)) == NULL) |
| 349 |
goto err; |
| 350 |
if (!ctx->SubjectAltName_nodefault && !HAS_SAN(ctx) && refcert != NULL |
| 351 |
&& (default_sans = X509V3_get_d2i(X509_get0_extensions(refcert), |
| 352 |
NID_subject_alt_name, NULL, NULL)) |
| 353 |
!= NULL |
| 354 |
&& !add1_extension(&exts, NID_subject_alt_name, crit, default_sans)) |
| 355 |
goto err; |
| 356 |
if (sk_X509_EXTENSION_num(ctx->reqExtensions) > 0 /* augment/override existing ones */ |
| 357 |
&& X509v3_add_extensions(&exts, ctx->reqExtensions) == NULL) |
| 358 |
goto err; |
| 359 |
if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 |
| 360 |
&& !add1_extension(&exts, NID_subject_alt_name, |
| 361 |
crit, ctx->subjectAltNames)) |
| 362 |
goto err; |
| 363 |
if (ctx->policies != NULL |
| 364 |
&& !add1_extension(&exts, NID_certificate_policies, |
| 365 |
ctx->setPoliciesCritical, ctx->policies)) |
| 366 |
goto err; |
| 367 |
if (!OSSL_CRMF_MSG_set0_extensions(crm, exts)) |
| 368 |
goto err; |
| 369 |
exts = NULL; |
| 370 |
/* end fill certTemplate, now set any controls */ |
| 371 |
|
| 372 |
/* for KUR, set OldCertId according to D.6 */ |
| 373 |
if (for_KUR && refcert != NULL) { |
| 374 |
OSSL_CRMF_CERTID *cid = OSSL_CRMF_CERTID_gen(X509_get_issuer_name(refcert), |
| 375 |
X509_get0_serialNumber(refcert)); |
| 376 |
int ret; |
| 377 |
|
| 378 |
if (cid == NULL) |
| 379 |
goto err; |
| 380 |
ret = OSSL_CRMF_MSG_set1_regCtrl_oldCertID(crm, cid); |
| 381 |
OSSL_CRMF_CERTID_free(cid); |
| 382 |
if (ret == 0) |
| 383 |
goto err; |
| 384 |
} |
| 385 |
|
| 386 |
goto end; |
| 387 |
|
| 388 |
err: |
| 389 |
OSSL_CRMF_MSG_free(crm); |
| 390 |
crm = NULL; |
| 391 |
|
| 392 |
end: |
| 393 |
sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); |
| 394 |
sk_GENERAL_NAME_pop_free(default_sans, GENERAL_NAME_free); |
| 395 |
return crm; |
| 396 |
} |
| 397 |
|
| 398 |
OSSL_CMP_MSG *ossl_cmp_certreq_new(OSSL_CMP_CTX *ctx, int type, |
| 399 |
const OSSL_CRMF_MSG *crm) |
| 400 |
{ |
| 401 |
OSSL_CMP_MSG *msg; |
| 402 |
OSSL_CRMF_MSG *local_crm = NULL; |
| 403 |
|
| 404 |
if (!ossl_assert(ctx != NULL)) |
| 405 |
return NULL; |
| 406 |
|
| 407 |
if (type != OSSL_CMP_PKIBODY_IR && type != OSSL_CMP_PKIBODY_CR |
| 408 |
&& type != OSSL_CMP_PKIBODY_KUR && type != OSSL_CMP_PKIBODY_P10CR) { |
| 409 |
ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
| 410 |
return NULL; |
| 411 |
} |
| 412 |
if (type == OSSL_CMP_PKIBODY_P10CR && crm != NULL) { |
| 413 |
ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
| 414 |
return NULL; |
| 415 |
} |
| 416 |
|
| 417 |
if ((msg = ossl_cmp_msg_create(ctx, type)) == NULL) |
| 418 |
goto err; |
| 419 |
|
| 420 |
/* header */ |
| 421 |
if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header)) |
| 422 |
goto err; |
| 423 |
|
| 424 |
/* body */ |
| 425 |
/* For P10CR the content has already been set in OSSL_CMP_MSG_create */ |
| 426 |
if (type != OSSL_CMP_PKIBODY_P10CR) { |
| 427 |
EVP_PKEY *privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1); |
| 428 |
|
| 429 |
/* privkey is ctx->newPkey (if private, else NULL) or ctx->pkey */ |
| 430 |
if (ctx->popoMethod >= OSSL_CRMF_POPO_SIGNATURE && privkey == NULL) { |
| 431 |
ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY_FOR_POPO); |
| 432 |
goto err; |
| 433 |
} |
| 434 |
if (crm == NULL) { |
| 435 |
local_crm = OSSL_CMP_CTX_setup_CRM(ctx, |
| 436 |
type == OSSL_CMP_PKIBODY_KUR, |
| 437 |
OSSL_CMP_CERTREQID); |
| 438 |
if (local_crm == NULL |
| 439 |
|| !OSSL_CRMF_MSG_create_popo(ctx->popoMethod, local_crm, |
| 440 |
privkey, ctx->digest, |
| 441 |
ctx->libctx, ctx->propq)) |
| 442 |
goto err; |
| 443 |
} else { |
| 444 |
if ((local_crm = OSSL_CRMF_MSG_dup(crm)) == NULL) |
| 445 |
goto err; |
| 446 |
} |
| 447 |
|
| 448 |
/* value.ir is same for cr and kur */ |
| 449 |
if (!sk_OSSL_CRMF_MSG_push(msg->body->value.ir, local_crm)) |
| 450 |
goto err; |
| 451 |
local_crm = NULL; |
| 452 |
} |
| 453 |
|
| 454 |
if (!ossl_cmp_msg_protect(ctx, msg)) |
| 455 |
goto err; |
| 456 |
|
| 457 |
return msg; |
| 458 |
|
| 459 |
err: |
| 460 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREQ); |
| 461 |
OSSL_CRMF_MSG_free(local_crm); |
| 462 |
OSSL_CMP_MSG_free(msg); |
| 463 |
return NULL; |
| 464 |
} |
| 465 |
|
| 466 |
#ifndef OPENSSL_NO_CMS |
| 467 |
static OSSL_CRMF_ENCRYPTEDKEY *enc_privkey(OSSL_CMP_CTX *ctx, const EVP_PKEY *pkey) |
| 468 |
{ |
| 469 |
OSSL_CRMF_ENCRYPTEDKEY *ek = NULL; |
| 470 |
CMS_EnvelopedData *envData = NULL; |
| 471 |
BIO *privbio = NULL; |
| 472 |
EVP_CIPHER *cipher = NULL; |
| 473 |
X509 *recip = ctx->validatedSrvCert; /* this is the client cert */ |
| 474 |
STACK_OF(X509) *encryption_recips = sk_X509_new_reserve(NULL, 1); |
| 475 |
|
| 476 |
if (encryption_recips == NULL |
| 477 |
|| !X509_add_cert(encryption_recips, recip, X509_ADD_FLAG_UP_REF)) |
| 478 |
goto err; |
| 479 |
|
| 480 |
privbio = BIO_new(BIO_s_mem()); |
| 481 |
if (privbio == NULL || i2d_PrivateKey_bio(privbio, pkey) <= 0) |
| 482 |
goto err; |
| 483 |
ossl_cmp_set_own_chain(ctx); |
| 484 |
cipher = EVP_CIPHER_fetch(ctx->libctx, SN_aes_256_cbc, ctx->propq); |
| 485 |
envData = ossl_cms_sign_encrypt(privbio, ctx->cert, ctx->chain, ctx->pkey, CMS_BINARY, |
| 486 |
encryption_recips, cipher, CMS_BINARY, |
| 487 |
ctx->libctx, ctx->propq); |
| 488 |
EVP_CIPHER_free(cipher); |
| 489 |
if (envData == NULL) |
| 490 |
goto err; |
| 491 |
ek = OSSL_CRMF_ENCRYPTEDKEY_init_envdata(envData); |
| 492 |
|
| 493 |
err: |
| 494 |
sk_X509_pop_free(encryption_recips, X509_free); |
| 495 |
BIO_free(privbio); |
| 496 |
if (ek == NULL) |
| 497 |
M_ASN1_free_of(envData, CMS_EnvelopedData); |
| 498 |
|
| 499 |
return ek; |
| 500 |
} |
| 501 |
#endif |
| 502 |
|
| 503 |
OSSL_CMP_MSG *ossl_cmp_certrep_new(OSSL_CMP_CTX *ctx, int bodytype, |
| 504 |
int certReqId, const OSSL_CMP_PKISI *si, |
| 505 |
X509 *cert, const EVP_PKEY *pkey, |
| 506 |
const X509 *encryption_recip, |
| 507 |
STACK_OF(X509) *chain, STACK_OF(X509) *caPubs, |
| 508 |
int unprotectedErrors) |
| 509 |
{ |
| 510 |
OSSL_CMP_MSG *msg = NULL; |
| 511 |
OSSL_CMP_CERTREPMESSAGE *repMsg = NULL; |
| 512 |
OSSL_CMP_CERTRESPONSE *resp = NULL; |
| 513 |
int status = OSSL_CMP_PKISTATUS_unspecified; |
| 514 |
|
| 515 |
if (!ossl_assert(ctx != NULL && si != NULL)) |
| 516 |
return NULL; |
| 517 |
|
| 518 |
if ((msg = ossl_cmp_msg_create(ctx, bodytype)) == NULL) |
| 519 |
goto err; |
| 520 |
repMsg = msg->body->value.ip; /* value.ip is same for cp and kup */ |
| 521 |
|
| 522 |
/* header */ |
| 523 |
if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header)) |
| 524 |
goto err; |
| 525 |
|
| 526 |
/* body */ |
| 527 |
if ((resp = OSSL_CMP_CERTRESPONSE_new()) == NULL) |
| 528 |
goto err; |
| 529 |
OSSL_CMP_PKISI_free(resp->status); |
| 530 |
if ((resp->status = OSSL_CMP_PKISI_dup(si)) == NULL |
| 531 |
|| !ASN1_INTEGER_set(resp->certReqId, certReqId)) |
| 532 |
goto err; |
| 533 |
|
| 534 |
status = ossl_cmp_pkisi_get_status(resp->status); |
| 535 |
if (status != OSSL_CMP_PKISTATUS_rejection |
| 536 |
&& status != OSSL_CMP_PKISTATUS_waiting && cert != NULL) { |
| 537 |
if (encryption_recip != NULL) { |
| 538 |
ERR_raise(ERR_LIB_CMP, ERR_R_UNSUPPORTED); |
| 539 |
goto err; |
| 540 |
} |
| 541 |
|
| 542 |
if ((resp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new()) |
| 543 |
== NULL) |
| 544 |
goto err; |
| 545 |
resp->certifiedKeyPair->certOrEncCert->type = OSSL_CMP_CERTORENCCERT_CERTIFICATE; |
| 546 |
if (!X509_up_ref(cert)) |
| 547 |
goto err; |
| 548 |
resp->certifiedKeyPair->certOrEncCert->value.certificate = cert; |
| 549 |
|
| 550 |
if (pkey != NULL) { |
| 551 |
#ifndef OPENSSL_NO_CMS |
| 552 |
if ((resp->certifiedKeyPair->privateKey = enc_privkey(ctx, pkey)) == NULL) |
| 553 |
goto err; |
| 554 |
#else |
| 555 |
ERR_raise(ERR_LIB_CMP, ERR_R_UNSUPPORTED); |
| 556 |
goto err; |
| 557 |
#endif |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
if (!sk_OSSL_CMP_CERTRESPONSE_push(repMsg->response, resp)) |
| 562 |
goto err; |
| 563 |
resp = NULL; |
| 564 |
|
| 565 |
if (bodytype == OSSL_CMP_PKIBODY_IP && caPubs != NULL |
| 566 |
&& (repMsg->caPubs = X509_chain_up_ref(caPubs)) == NULL) |
| 567 |
goto err; |
| 568 |
if (sk_X509_num(chain) > 0 |
| 569 |
&& !ossl_x509_add_certs_new(&msg->extraCerts, chain, |
| 570 |
X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP)) |
| 571 |
goto err; |
| 572 |
|
| 573 |
if (!unprotectedErrors |
| 574 |
|| ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection) |
| 575 |
if (!ossl_cmp_msg_protect(ctx, msg)) |
| 576 |
goto err; |
| 577 |
|
| 578 |
return msg; |
| 579 |
|
| 580 |
err: |
| 581 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP); |
| 582 |
OSSL_CMP_CERTRESPONSE_free(resp); |
| 583 |
OSSL_CMP_MSG_free(msg); |
| 584 |
return NULL; |
| 585 |
} |
| 586 |
|
| 587 |
OSSL_CMP_MSG *ossl_cmp_rr_new(OSSL_CMP_CTX *ctx) |
| 588 |
{ |
| 589 |
OSSL_CMP_MSG *msg = NULL; |
| 590 |
const X509_NAME *issuer = NULL; |
| 591 |
const X509_NAME *subject = NULL; |
| 592 |
const ASN1_INTEGER *serialNumber = NULL; |
| 593 |
EVP_PKEY *pubkey = NULL; |
| 594 |
OSSL_CMP_REVDETAILS *rd; |
| 595 |
int ret; |
| 596 |
|
| 597 |
if (!ossl_assert(ctx != NULL |
| 598 |
&& (ctx->oldCert != NULL || ctx->p10CSR != NULL |
| 599 |
|| (ctx->serialNumber != NULL && ctx->issuer != NULL)))) |
| 600 |
return NULL; |
| 601 |
|
| 602 |
if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL) |
| 603 |
goto err; |
| 604 |
|
| 605 |
if (ctx->serialNumber != NULL && ctx->issuer != NULL) { |
| 606 |
issuer = ctx->issuer; |
| 607 |
serialNumber = ctx->serialNumber; |
| 608 |
} else if (ctx->oldCert != NULL) { |
| 609 |
issuer = X509_get_issuer_name(ctx->oldCert); |
| 610 |
serialNumber = X509_get0_serialNumber(ctx->oldCert); |
| 611 |
} else if (ctx->p10CSR != NULL) { |
| 612 |
pubkey = X509_REQ_get0_pubkey(ctx->p10CSR); |
| 613 |
subject = X509_REQ_get_subject_name(ctx->p10CSR); |
| 614 |
} else { |
| 615 |
goto err; |
| 616 |
} |
| 617 |
|
| 618 |
/* Fill the template from the contents of the certificate to be revoked */ |
| 619 |
ret = OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails, pubkey, subject, |
| 620 |
issuer, serialNumber); |
| 621 |
if (!ret) |
| 622 |
goto err; |
| 623 |
|
| 624 |
/* revocation reason code is optional */ |
| 625 |
if (ctx->revocationReason != CRL_REASON_NONE |
| 626 |
&& !add_crl_reason_extension(&rd->crlEntryDetails, |
| 627 |
ctx->revocationReason)) |
| 628 |
goto err; |
| 629 |
|
| 630 |
if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL) |
| 631 |
goto err; |
| 632 |
|
| 633 |
if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd)) |
| 634 |
goto err; |
| 635 |
rd = NULL; |
| 636 |
/* Revocation Passphrase according to section 5.3.19.9 could be set here */ |
| 637 |
|
| 638 |
if (!ossl_cmp_msg_protect(ctx, msg)) |
| 639 |
goto err; |
| 640 |
|
| 641 |
return msg; |
| 642 |
|
| 643 |
err: |
| 644 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR); |
| 645 |
OSSL_CMP_MSG_free(msg); |
| 646 |
OSSL_CMP_REVDETAILS_free(rd); |
| 647 |
return NULL; |
| 648 |
} |
| 649 |
|
| 650 |
OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si, |
| 651 |
const OSSL_CRMF_CERTID *cid, int unprotectedErrors) |
| 652 |
{ |
| 653 |
OSSL_CMP_REVREPCONTENT *rep = NULL; |
| 654 |
OSSL_CMP_PKISI *si1 = NULL; |
| 655 |
OSSL_CRMF_CERTID *cid_copy = NULL; |
| 656 |
OSSL_CMP_MSG *msg = NULL; |
| 657 |
|
| 658 |
if (!ossl_assert(ctx != NULL && si != NULL)) |
| 659 |
return NULL; |
| 660 |
|
| 661 |
if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL) |
| 662 |
goto err; |
| 663 |
rep = msg->body->value.rp; |
| 664 |
|
| 665 |
if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL |
| 666 |
|| !sk_OSSL_CMP_PKISI_push(rep->status, si1)) |
| 667 |
goto err; |
| 668 |
|
| 669 |
si1 = NULL; /* ownership transferred to rep->status */ |
| 670 |
|
| 671 |
if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL) |
| 672 |
goto err; |
| 673 |
if (cid != NULL) { |
| 674 |
if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL |
| 675 |
|| !sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) |
| 676 |
goto err; |
| 677 |
|
| 678 |
cid_copy = NULL; /* ownership transferred to rep->revCerts */ |
| 679 |
} |
| 680 |
|
| 681 |
if (!unprotectedErrors |
| 682 |
|| ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection) |
| 683 |
if (!ossl_cmp_msg_protect(ctx, msg)) |
| 684 |
goto err; |
| 685 |
|
| 686 |
return msg; |
| 687 |
|
| 688 |
err: |
| 689 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RP); |
| 690 |
OSSL_CMP_PKISI_free(si1); |
| 691 |
OSSL_CRMF_CERTID_free(cid_copy); |
| 692 |
OSSL_CMP_MSG_free(msg); |
| 693 |
return NULL; |
| 694 |
} |
| 695 |
|
| 696 |
OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx) |
| 697 |
{ |
| 698 |
OSSL_CMP_MSG *msg; |
| 699 |
|
| 700 |
if (!ossl_assert(ctx != NULL)) |
| 701 |
return NULL; |
| 702 |
|
| 703 |
if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL) |
| 704 |
goto err; |
| 705 |
if (ossl_cmp_msg_protect(ctx, msg)) |
| 706 |
return msg; |
| 707 |
|
| 708 |
err: |
| 709 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF); |
| 710 |
OSSL_CMP_MSG_free(msg); |
| 711 |
return NULL; |
| 712 |
} |
| 713 |
|
| 714 |
int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav) |
| 715 |
{ |
| 716 |
int bodytype; |
| 717 |
|
| 718 |
if (!ossl_assert(msg != NULL && itav != NULL)) |
| 719 |
return 0; |
| 720 |
|
| 721 |
bodytype = OSSL_CMP_MSG_get_bodytype(msg); |
| 722 |
if (bodytype != OSSL_CMP_PKIBODY_GENM |
| 723 |
&& bodytype != OSSL_CMP_PKIBODY_GENP) { |
| 724 |
ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
/* value.genp has the same structure, so this works for genp as well */ |
| 729 |
return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav); |
| 730 |
} |
| 731 |
|
| 732 |
int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg, |
| 733 |
const STACK_OF(OSSL_CMP_ITAV) *itavs) |
| 734 |
{ |
| 735 |
int i; |
| 736 |
OSSL_CMP_ITAV *itav = NULL; |
| 737 |
|
| 738 |
if (!ossl_assert(msg != NULL)) |
| 739 |
return 0; |
| 740 |
|
| 741 |
for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) { |
| 742 |
itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i)); |
| 743 |
if (itav == NULL |
| 744 |
|| !ossl_cmp_msg_gen_push0_ITAV(msg, itav)) { |
| 745 |
OSSL_CMP_ITAV_free(itav); |
| 746 |
return 0; |
| 747 |
} |
| 748 |
} |
| 749 |
return 1; |
| 750 |
} |
| 751 |
|
| 752 |
/* |
| 753 |
* Creates a new General Message/Response with a copy of the given itav stack |
| 754 |
* returns a pointer to the PKIMessage on success, NULL on error |
| 755 |
*/ |
| 756 |
static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx, |
| 757 |
const STACK_OF(OSSL_CMP_ITAV) *itavs, |
| 758 |
int body_type, int err_code) |
| 759 |
{ |
| 760 |
OSSL_CMP_MSG *msg = NULL; |
| 761 |
|
| 762 |
if (!ossl_assert(ctx != NULL)) |
| 763 |
return NULL; |
| 764 |
|
| 765 |
if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL) |
| 766 |
return NULL; |
| 767 |
|
| 768 |
if (itavs != NULL && !ossl_cmp_msg_gen_push1_ITAVs(msg, itavs)) |
| 769 |
goto err; |
| 770 |
|
| 771 |
if (!ossl_cmp_msg_protect(ctx, msg)) |
| 772 |
goto err; |
| 773 |
|
| 774 |
return msg; |
| 775 |
|
| 776 |
err: |
| 777 |
ERR_raise(ERR_LIB_CMP, err_code); |
| 778 |
OSSL_CMP_MSG_free(msg); |
| 779 |
return NULL; |
| 780 |
} |
| 781 |
|
| 782 |
OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx) |
| 783 |
{ |
| 784 |
return gen_new(ctx, ctx->genm_ITAVs, |
| 785 |
OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM); |
| 786 |
} |
| 787 |
|
| 788 |
OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx, |
| 789 |
const STACK_OF(OSSL_CMP_ITAV) *itavs) |
| 790 |
{ |
| 791 |
return gen_new(ctx, itavs, |
| 792 |
OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP); |
| 793 |
} |
| 794 |
|
| 795 |
OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si, |
| 796 |
int64_t errorCode, const char *details, |
| 797 |
int unprotected) |
| 798 |
{ |
| 799 |
OSSL_CMP_MSG *msg = NULL; |
| 800 |
const char *lib = NULL, *reason = NULL; |
| 801 |
OSSL_CMP_PKIFREETEXT *ft; |
| 802 |
|
| 803 |
if (!ossl_assert(ctx != NULL && si != NULL)) |
| 804 |
return NULL; |
| 805 |
|
| 806 |
if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL) |
| 807 |
goto err; |
| 808 |
|
| 809 |
OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo); |
| 810 |
if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si)) |
| 811 |
== NULL) |
| 812 |
goto err; |
| 813 |
if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL) |
| 814 |
goto err; |
| 815 |
if (!ASN1_INTEGER_set_int64(msg->body->value.error->errorCode, errorCode)) |
| 816 |
goto err; |
| 817 |
if (errorCode > 0 |
| 818 |
&& (uint64_t)errorCode < ((uint64_t)ERR_SYSTEM_FLAG << 1)) { |
| 819 |
lib = ERR_lib_error_string((unsigned long)errorCode); |
| 820 |
reason = ERR_reason_error_string((unsigned long)errorCode); |
| 821 |
} |
| 822 |
if (lib != NULL || reason != NULL || details != NULL) { |
| 823 |
if ((ft = sk_ASN1_UTF8STRING_new_null()) == NULL) |
| 824 |
goto err; |
| 825 |
msg->body->value.error->errorDetails = ft; |
| 826 |
if (lib != NULL && *lib != '\0' |
| 827 |
&& !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, lib, -1)) |
| 828 |
goto err; |
| 829 |
if (reason != NULL && *reason != '\0' |
| 830 |
&& !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, reason, -1)) |
| 831 |
goto err; |
| 832 |
if (details != NULL |
| 833 |
&& !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, details, -1)) |
| 834 |
goto err; |
| 835 |
} |
| 836 |
|
| 837 |
if (!unprotected && !ossl_cmp_msg_protect(ctx, msg)) |
| 838 |
goto err; |
| 839 |
return msg; |
| 840 |
|
| 841 |
err: |
| 842 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_ERROR); |
| 843 |
OSSL_CMP_MSG_free(msg); |
| 844 |
return NULL; |
| 845 |
} |
| 846 |
|
| 847 |
/* |
| 848 |
* Set the certHash field of a OSSL_CMP_CERTSTATUS structure. |
| 849 |
* This is used in the certConf message, for example, |
| 850 |
* to confirm that the certificate was received successfully. |
| 851 |
*/ |
| 852 |
int ossl_cmp_certstatus_set0_certHash(OSSL_CMP_CERTSTATUS *certStatus, |
| 853 |
ASN1_OCTET_STRING *hash) |
| 854 |
{ |
| 855 |
if (!ossl_assert(certStatus != NULL)) |
| 856 |
return 0; |
| 857 |
ASN1_OCTET_STRING_free(certStatus->certHash); |
| 858 |
certStatus->certHash = hash; |
| 859 |
return 1; |
| 860 |
} |
| 861 |
|
| 862 |
OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int certReqId, |
| 863 |
int fail_info, const char *text) |
| 864 |
{ |
| 865 |
OSSL_CMP_MSG *msg = NULL; |
| 866 |
OSSL_CMP_CERTSTATUS *certStatus = NULL; |
| 867 |
EVP_MD *md; |
| 868 |
int is_fallback; |
| 869 |
ASN1_OCTET_STRING *certHash = NULL; |
| 870 |
OSSL_CMP_PKISI *sinfo; |
| 871 |
|
| 872 |
if (!ossl_assert(ctx != NULL && ctx->newCert != NULL |
| 873 |
&& (certReqId == OSSL_CMP_CERTREQID |
| 874 |
|| certReqId == OSSL_CMP_CERTREQID_NONE))) |
| 875 |
return NULL; |
| 876 |
|
| 877 |
if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) { |
| 878 |
ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE); |
| 879 |
return NULL; |
| 880 |
} |
| 881 |
|
| 882 |
if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL) |
| 883 |
goto err; |
| 884 |
|
| 885 |
if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL) |
| 886 |
goto err; |
| 887 |
/* consume certStatus into msg right away so it gets deallocated with msg */ |
| 888 |
if (sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus) < 1) { |
| 889 |
OSSL_CMP_CERTSTATUS_free(certStatus); |
| 890 |
goto err; |
| 891 |
} |
| 892 |
|
| 893 |
/* set the ID of the certReq */ |
| 894 |
if (!ASN1_INTEGER_set(certStatus->certReqId, certReqId)) |
| 895 |
goto err; |
| 896 |
|
| 897 |
certStatus->hashAlg = NULL; |
| 898 |
/* |
| 899 |
* The hash of the certificate, using the same hash algorithm |
| 900 |
* as is used to create and verify the certificate signature. |
| 901 |
* If not available, a fallback hash algorithm is used. |
| 902 |
*/ |
| 903 |
if ((certHash = X509_digest_sig(ctx->newCert, &md, &is_fallback)) == NULL) |
| 904 |
goto err; |
| 905 |
if (is_fallback) { |
| 906 |
if (!ossl_cmp_hdr_set_pvno(msg->header, OSSL_CMP_PVNO_3)) |
| 907 |
goto err; |
| 908 |
if ((certStatus->hashAlg = X509_ALGOR_new()) == NULL) |
| 909 |
goto err; |
| 910 |
X509_ALGOR_set_md(certStatus->hashAlg, md); |
| 911 |
} |
| 912 |
EVP_MD_free(md); |
| 913 |
|
| 914 |
if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash)) |
| 915 |
goto err; |
| 916 |
certHash = NULL; |
| 917 |
/* |
| 918 |
* For any particular CertStatus, omission of the statusInfo field |
| 919 |
* indicates ACCEPTANCE of the specified certificate. Alternatively, |
| 920 |
* explicit status details (with respect to acceptance or rejection) MAY |
| 921 |
* be provided in the statusInfo field, perhaps for auditing purposes at |
| 922 |
* the CA/RA. |
| 923 |
*/ |
| 924 |
sinfo = fail_info != 0 ? OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) : OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text); |
| 925 |
if (sinfo == NULL) |
| 926 |
goto err; |
| 927 |
certStatus->statusInfo = sinfo; |
| 928 |
|
| 929 |
if (!ossl_cmp_msg_protect(ctx, msg)) |
| 930 |
goto err; |
| 931 |
|
| 932 |
return msg; |
| 933 |
|
| 934 |
err: |
| 935 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF); |
| 936 |
OSSL_CMP_MSG_free(msg); |
| 937 |
ASN1_OCTET_STRING_free(certHash); |
| 938 |
return NULL; |
| 939 |
} |
| 940 |
|
| 941 |
OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid) |
| 942 |
{ |
| 943 |
OSSL_CMP_MSG *msg = NULL; |
| 944 |
OSSL_CMP_POLLREQ *preq = NULL; |
| 945 |
|
| 946 |
if (!ossl_assert(ctx != NULL)) |
| 947 |
return NULL; |
| 948 |
|
| 949 |
if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL) |
| 950 |
goto err; |
| 951 |
|
| 952 |
if ((preq = OSSL_CMP_POLLREQ_new()) == NULL |
| 953 |
|| !ASN1_INTEGER_set(preq->certReqId, crid) |
| 954 |
|| !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq)) |
| 955 |
goto err; |
| 956 |
|
| 957 |
preq = NULL; |
| 958 |
if (!ossl_cmp_msg_protect(ctx, msg)) |
| 959 |
goto err; |
| 960 |
|
| 961 |
return msg; |
| 962 |
|
| 963 |
err: |
| 964 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ); |
| 965 |
OSSL_CMP_POLLREQ_free(preq); |
| 966 |
OSSL_CMP_MSG_free(msg); |
| 967 |
return NULL; |
| 968 |
} |
| 969 |
|
| 970 |
OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid, |
| 971 |
int64_t poll_after) |
| 972 |
{ |
| 973 |
OSSL_CMP_MSG *msg; |
| 974 |
OSSL_CMP_POLLREP *prep; |
| 975 |
|
| 976 |
if (!ossl_assert(ctx != NULL)) |
| 977 |
return NULL; |
| 978 |
|
| 979 |
if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL) |
| 980 |
goto err; |
| 981 |
if ((prep = OSSL_CMP_POLLREP_new()) == NULL) |
| 982 |
goto err; |
| 983 |
if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep)) |
| 984 |
goto err; |
| 985 |
if (!ASN1_INTEGER_set(prep->certReqId, crid)) |
| 986 |
goto err; |
| 987 |
if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after)) |
| 988 |
goto err; |
| 989 |
|
| 990 |
if (!ossl_cmp_msg_protect(ctx, msg)) |
| 991 |
goto err; |
| 992 |
return msg; |
| 993 |
|
| 994 |
err: |
| 995 |
ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP); |
| 996 |
OSSL_CMP_MSG_free(msg); |
| 997 |
return NULL; |
| 998 |
} |
| 999 |
|
| 1000 |
/*- |
| 1001 |
* returns the status field of the RevRepContent with the given |
| 1002 |
* request/sequence id inside a revocation response. |
| 1003 |
* RevRepContent has the revocation statuses in same order as they were sent in |
| 1004 |
* RevReqContent. |
| 1005 |
* returns NULL on error |
| 1006 |
*/ |
| 1007 |
OSSL_CMP_PKISI * |
| 1008 |
ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid) |
| 1009 |
{ |
| 1010 |
OSSL_CMP_PKISI *status; |
| 1011 |
|
| 1012 |
if (!ossl_assert(rrep != NULL)) |
| 1013 |
return NULL; |
| 1014 |
|
| 1015 |
if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL) |
| 1016 |
return status; |
| 1017 |
|
| 1018 |
ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND); |
| 1019 |
return NULL; |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* |
| 1023 |
* returns the CertId field in the revCerts part of the RevRepContent |
| 1024 |
* with the given request/sequence id inside a revocation response. |
| 1025 |
* RevRepContent has the CertIds in same order as they were sent in |
| 1026 |
* RevReqContent. |
| 1027 |
* returns NULL on error |
| 1028 |
*/ |
| 1029 |
OSSL_CRMF_CERTID * |
| 1030 |
ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid) |
| 1031 |
{ |
| 1032 |
OSSL_CRMF_CERTID *cid = NULL; |
| 1033 |
|
| 1034 |
if (!ossl_assert(rrep != NULL)) |
| 1035 |
return NULL; |
| 1036 |
|
| 1037 |
if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL) |
| 1038 |
return cid; |
| 1039 |
|
| 1040 |
ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND); |
| 1041 |
return NULL; |
| 1042 |
} |
| 1043 |
|
| 1044 |
static int suitable_rid(const ASN1_INTEGER *certReqId, int rid) |
| 1045 |
{ |
| 1046 |
int trid; |
| 1047 |
|
| 1048 |
if (rid == OSSL_CMP_CERTREQID_NONE) |
| 1049 |
return 1; |
| 1050 |
|
| 1051 |
trid = ossl_cmp_asn1_get_int(certReqId); |
| 1052 |
if (trid <= OSSL_CMP_CERTREQID_INVALID) { |
| 1053 |
ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID); |
| 1054 |
return 0; |
| 1055 |
} |
| 1056 |
return rid == trid; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* |
| 1060 |
* returns a pointer to the PollResponse with the given CertReqId |
| 1061 |
* (or the first one in case -1) inside a PollRepContent |
| 1062 |
* returns NULL on error or if no suitable PollResponse available |
| 1063 |
*/ |
| 1064 |
OSSL_CMP_POLLREP * |
| 1065 |
ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc, |
| 1066 |
int rid) |
| 1067 |
{ |
| 1068 |
OSSL_CMP_POLLREP *pollRep = NULL; |
| 1069 |
int i; |
| 1070 |
|
| 1071 |
if (!ossl_assert(prc != NULL)) |
| 1072 |
return NULL; |
| 1073 |
|
| 1074 |
for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) { |
| 1075 |
pollRep = sk_OSSL_CMP_POLLREP_value(prc, i); |
| 1076 |
if (suitable_rid(pollRep->certReqId, rid)) |
| 1077 |
return pollRep; |
| 1078 |
} |
| 1079 |
|
| 1080 |
ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND, |
| 1081 |
"expected certReqId = %d", rid); |
| 1082 |
return NULL; |
| 1083 |
} |
| 1084 |
|
| 1085 |
/* |
| 1086 |
* returns a pointer to the CertResponse with the given CertReqId |
| 1087 |
* (or the first one in case -1) inside a CertRepMessage |
| 1088 |
* returns NULL on error or if no suitable CertResponse available |
| 1089 |
*/ |
| 1090 |
OSSL_CMP_CERTRESPONSE * |
| 1091 |
ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm, |
| 1092 |
int rid) |
| 1093 |
{ |
| 1094 |
OSSL_CMP_CERTRESPONSE *crep = NULL; |
| 1095 |
int i; |
| 1096 |
|
| 1097 |
if (!ossl_assert(crm != NULL && crm->response != NULL)) |
| 1098 |
return NULL; |
| 1099 |
|
| 1100 |
for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) { |
| 1101 |
crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i); |
| 1102 |
if (suitable_rid(crep->certReqId, rid)) |
| 1103 |
return crep; |
| 1104 |
} |
| 1105 |
|
| 1106 |
ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND, |
| 1107 |
"expected certReqId = %d", rid); |
| 1108 |
return NULL; |
| 1109 |
} |
| 1110 |
|
| 1111 |
/*- |
| 1112 |
* Retrieve newly enrolled certificate and key from the given certResponse crep. |
| 1113 |
* Stores any centrally generated key in ctx->newPkey. |
| 1114 |
* In case of indirect POPO uses ctx->newPkey to decrypt the new certificate. |
| 1115 |
* Returns a pointer to a copy of the found certificate, or NULL if not found. |
| 1116 |
*/ |
| 1117 |
X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CTX *ctx, const OSSL_CMP_CERTRESPONSE *crep) |
| 1118 |
{ |
| 1119 |
OSSL_CMP_CERTORENCCERT *coec; |
| 1120 |
X509 *crt = NULL; |
| 1121 |
OSSL_CRMF_ENCRYPTEDKEY *encr_key; |
| 1122 |
EVP_PKEY *pkey = NULL; |
| 1123 |
int central_keygen = OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_POPO_METHOD) |
| 1124 |
== OSSL_CRMF_POPO_NONE; |
| 1125 |
|
| 1126 |
if (crep->certifiedKeyPair == NULL) { |
| 1127 |
ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND); |
| 1128 |
return NULL; |
| 1129 |
} |
| 1130 |
encr_key = crep->certifiedKeyPair->privateKey; |
| 1131 |
if (encr_key == NULL && central_keygen) { |
| 1132 |
ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_CENTRAL_GEN_KEY); |
| 1133 |
return NULL; |
| 1134 |
} |
| 1135 |
if (encr_key != NULL) { |
| 1136 |
if (!central_keygen) { |
| 1137 |
ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_CENTRAL_GEN_KEY); |
| 1138 |
return NULL; |
| 1139 |
} |
| 1140 |
/* found encrypted private key, try to extract */ |
| 1141 |
pkey = OSSL_CRMF_ENCRYPTEDKEY_get1_pkey(encr_key, ctx->trusted, |
| 1142 |
ctx->untrusted, |
| 1143 |
ctx->pkey, ctx->cert, |
| 1144 |
ctx->secretValue, |
| 1145 |
ctx->libctx, ctx->propq); |
| 1146 |
if (pkey == NULL) { |
| 1147 |
ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_EXTRACTING_CENTRAL_GEN_KEY); |
| 1148 |
return NULL; |
| 1149 |
} |
| 1150 |
OSSL_CMP_CTX_set0_newPkey((OSSL_CMP_CTX *)ctx, 1, pkey); |
| 1151 |
} |
| 1152 |
|
| 1153 |
if (!ossl_assert(crep != NULL && ctx != NULL)) |
| 1154 |
return NULL; |
| 1155 |
|
| 1156 |
if ((coec = crep->certifiedKeyPair->certOrEncCert) != NULL) { |
| 1157 |
switch (coec->type) { |
| 1158 |
case OSSL_CMP_CERTORENCCERT_CERTIFICATE: |
| 1159 |
crt = X509_dup(coec->value.certificate); |
| 1160 |
break; |
| 1161 |
case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT: |
| 1162 |
/* cert encrypted for indirect PoP; RFC 9810, 5.2.8.3.2 */ |
| 1163 |
pkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1); |
| 1164 |
/* pkey is ctx->newPkey (if private, else NULL) or ctx->pkey */ |
| 1165 |
if (pkey == NULL) { |
| 1166 |
ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY); |
| 1167 |
return NULL; |
| 1168 |
} |
| 1169 |
crt = OSSL_CRMF_ENCRYPTEDKEY_get1_encCert(coec->value.encryptedCert, |
| 1170 |
ctx->libctx, ctx->propq, pkey, 0); |
| 1171 |
break; |
| 1172 |
default: |
| 1173 |
ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE); |
| 1174 |
return NULL; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
if (crt == NULL) |
| 1178 |
ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND); |
| 1179 |
else |
| 1180 |
(void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq); |
| 1181 |
return crt; |
| 1182 |
} |
| 1183 |
|
| 1184 |
int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg) |
| 1185 |
{ |
| 1186 |
if (ctx == NULL || msg == NULL) { |
| 1187 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 1188 |
return 0; |
| 1189 |
} |
| 1190 |
if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header)) |
| 1191 |
return 0; |
| 1192 |
return msg->header->protectionAlg == NULL |
| 1193 |
|| ossl_cmp_msg_protect(ctx, msg); |
| 1194 |
} |
| 1195 |
|
| 1196 |
int OSSL_CMP_MSG_update_recipNonce(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg) |
| 1197 |
{ |
| 1198 |
if (ctx == NULL || msg == NULL || msg->header == NULL) { |
| 1199 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 1200 |
return 0; |
| 1201 |
} |
| 1202 |
if (ctx->recipNonce == NULL) /* nothing to do for 1st msg in transaction */ |
| 1203 |
return 1; |
| 1204 |
if (!ossl_cmp_asn1_octet_string_set1(&msg->header->recipNonce, |
| 1205 |
ctx->recipNonce)) |
| 1206 |
return 0; |
| 1207 |
return msg->header->protectionAlg == NULL || ossl_cmp_msg_protect(ctx, msg); |
| 1208 |
} |
| 1209 |
|
| 1210 |
OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx, |
| 1211 |
const char *propq) |
| 1212 |
{ |
| 1213 |
OSSL_CMP_MSG *msg; |
| 1214 |
BIO *bio = NULL; |
| 1215 |
|
| 1216 |
if (file == NULL) { |
| 1217 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 1218 |
return NULL; |
| 1219 |
} |
| 1220 |
|
| 1221 |
msg = OSSL_CMP_MSG_new(libctx, propq); |
| 1222 |
if (msg == NULL) { |
| 1223 |
ERR_raise(ERR_LIB_CMP, ERR_R_CMP_LIB); |
| 1224 |
return NULL; |
| 1225 |
} |
| 1226 |
|
| 1227 |
if ((bio = BIO_new_file(file, "rb")) == NULL |
| 1228 |
|| d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) { |
| 1229 |
OSSL_CMP_MSG_free(msg); |
| 1230 |
msg = NULL; |
| 1231 |
} |
| 1232 |
BIO_free(bio); |
| 1233 |
return msg; |
| 1234 |
} |
| 1235 |
|
| 1236 |
int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg) |
| 1237 |
{ |
| 1238 |
BIO *bio; |
| 1239 |
int res; |
| 1240 |
|
| 1241 |
if (file == NULL || msg == NULL) { |
| 1242 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 1243 |
return -1; |
| 1244 |
} |
| 1245 |
|
| 1246 |
bio = BIO_new_file(file, "wb"); |
| 1247 |
if (bio == NULL) |
| 1248 |
return -2; |
| 1249 |
res = i2d_OSSL_CMP_MSG_bio(bio, msg); |
| 1250 |
BIO_free(bio); |
| 1251 |
return res; |
| 1252 |
} |
| 1253 |
|
| 1254 |
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in, |
| 1255 |
long len) |
| 1256 |
{ |
| 1257 |
OSSL_LIB_CTX *libctx = NULL; |
| 1258 |
const char *propq = NULL; |
| 1259 |
|
| 1260 |
if (msg != NULL && *msg != NULL) { |
| 1261 |
libctx = (*msg)->libctx; |
| 1262 |
propq = (*msg)->propq; |
| 1263 |
} |
| 1264 |
|
| 1265 |
return (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len, |
| 1266 |
ASN1_ITEM_rptr(OSSL_CMP_MSG), |
| 1267 |
libctx, propq); |
| 1268 |
} |
| 1269 |
|
| 1270 |
int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out) |
| 1271 |
{ |
| 1272 |
return ASN1_item_i2d((const ASN1_VALUE *)msg, out, |
| 1273 |
ASN1_ITEM_rptr(OSSL_CMP_MSG)); |
| 1274 |
} |
| 1275 |
|
| 1276 |
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg) |
| 1277 |
{ |
| 1278 |
OSSL_LIB_CTX *libctx = NULL; |
| 1279 |
const char *propq = NULL; |
| 1280 |
|
| 1281 |
if (msg != NULL && *msg != NULL) { |
| 1282 |
libctx = (*msg)->libctx; |
| 1283 |
propq = (*msg)->propq; |
| 1284 |
} |
| 1285 |
|
| 1286 |
return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx, |
| 1287 |
propq); |
| 1288 |
} |
| 1289 |
|
| 1290 |
int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg) |
| 1291 |
{ |
| 1292 |
return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg); |
| 1293 |
} |
| 1294 |
|
| 1295 |
int ossl_cmp_is_error_with_waiting(const OSSL_CMP_MSG *msg) |
| 1296 |
{ |
| 1297 |
if (!ossl_assert(msg != NULL)) |
| 1298 |
return 0; |
| 1299 |
|
| 1300 |
return (OSSL_CMP_MSG_get_bodytype(msg) == OSSL_CMP_PKIBODY_ERROR |
| 1301 |
&& ossl_cmp_pkisi_get_status(msg->body->value.error->pKIStatusInfo) |
| 1302 |
== OSSL_CMP_PKISTATUS_waiting); |
| 1303 |
} |