cmp_msg.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. * Copyright 2007-2022 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. /* CMP functions for PKIMessage construction */
  12. #include "cmp_local.h"
  13. /* explicit #includes not strictly needed since implied by the above: */
  14. #include <openssl/asn1t.h>
  15. #include <openssl/cmp.h>
  16. #include <openssl/crmf.h>
  17. #include <openssl/err.h>
  18. #include <openssl/x509.h>
  19. OSSL_CMP_MSG *OSSL_CMP_MSG_new(OSSL_LIB_CTX *libctx, const char *propq)
  20. {
  21. OSSL_CMP_MSG *msg = NULL;
  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. void OSSL_CMP_MSG_free(OSSL_CMP_MSG *msg)
  31. {
  32. ASN1_item_free((ASN1_VALUE *)msg, ASN1_ITEM_rptr(OSSL_CMP_MSG));
  33. }
  34. /*
  35. * This should only be used if the X509 object was embedded inside another
  36. * asn1 object and it needs a libctx to operate.
  37. * Use OSSL_CMP_MSG_new() instead if possible.
  38. */
  39. int ossl_cmp_msg_set0_libctx(OSSL_CMP_MSG *msg, OSSL_LIB_CTX *libctx,
  40. const char *propq)
  41. {
  42. if (msg != NULL) {
  43. msg->libctx = libctx;
  44. OPENSSL_free(msg->propq);
  45. msg->propq = NULL;
  46. if (propq != NULL) {
  47. msg->propq = OPENSSL_strdup(propq);
  48. if (msg->propq == NULL)
  49. return 0;
  50. }
  51. }
  52. return 1;
  53. }
  54. OSSL_CMP_PKIHEADER *OSSL_CMP_MSG_get0_header(const OSSL_CMP_MSG *msg)
  55. {
  56. if (msg == NULL) {
  57. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  58. return NULL;
  59. }
  60. return msg->header;
  61. }
  62. const char *ossl_cmp_bodytype_to_string(int type)
  63. {
  64. static const char *type_names[] = {
  65. "IR", "IP", "CR", "CP", "P10CR",
  66. "POPDECC", "POPDECR", "KUR", "KUP",
  67. "KRR", "KRP", "RR", "RP", "CCR", "CCP",
  68. "CKUANN", "CANN", "RANN", "CRLANN", "PKICONF", "NESTED",
  69. "GENM", "GENP", "ERROR", "CERTCONF", "POLLREQ", "POLLREP",
  70. };
  71. if (type < 0 || type > OSSL_CMP_PKIBODY_TYPE_MAX)
  72. return "illegal body type";
  73. return type_names[type];
  74. }
  75. int ossl_cmp_msg_set_bodytype(OSSL_CMP_MSG *msg, int type)
  76. {
  77. if (!ossl_assert(msg != NULL && msg->body != NULL))
  78. return 0;
  79. msg->body->type = type;
  80. return 1;
  81. }
  82. int OSSL_CMP_MSG_get_bodytype(const OSSL_CMP_MSG *msg)
  83. {
  84. if (!ossl_assert(msg != NULL && msg->body != NULL))
  85. return -1;
  86. return msg->body->type;
  87. }
  88. /* Add an extension to the referenced extension stack, which may be NULL */
  89. static int add1_extension(X509_EXTENSIONS **pexts, int nid, int crit, void *ex)
  90. {
  91. X509_EXTENSION *ext;
  92. int res;
  93. if (!ossl_assert(pexts != NULL)) /* pointer to var must not be NULL */
  94. return 0;
  95. if ((ext = X509V3_EXT_i2d(nid, crit, ex)) == NULL)
  96. return 0;
  97. res = X509v3_add_ext(pexts, ext, 0) != NULL;
  98. X509_EXTENSION_free(ext);
  99. return res;
  100. }
  101. /* Add extension list to the referenced extension stack, which may be NULL */
  102. static int add_extensions(STACK_OF(X509_EXTENSION) **target,
  103. const STACK_OF(X509_EXTENSION) *exts)
  104. {
  105. int i;
  106. if (target == NULL)
  107. return 0;
  108. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  109. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  110. ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext);
  111. int idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
  112. /* Does extension exist in target? */
  113. if (idx != -1) {
  114. /* Delete all extensions of same type */
  115. do {
  116. X509_EXTENSION_free(sk_X509_EXTENSION_delete(*target, idx));
  117. idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
  118. } while (idx != -1);
  119. }
  120. if (!X509v3_add_ext(target, ext, -1))
  121. return 0;
  122. }
  123. return 1;
  124. }
  125. /* Add a CRL revocation reason code to extension stack, which may be NULL */
  126. static int add_crl_reason_extension(X509_EXTENSIONS **pexts, int reason_code)
  127. {
  128. ASN1_ENUMERATED *val = ASN1_ENUMERATED_new();
  129. int res = 0;
  130. if (val != NULL && ASN1_ENUMERATED_set(val, reason_code))
  131. res = add1_extension(pexts, NID_crl_reason, 0 /* non-critical */, val);
  132. ASN1_ENUMERATED_free(val);
  133. return res;
  134. }
  135. OSSL_CMP_MSG *ossl_cmp_msg_create(OSSL_CMP_CTX *ctx, int bodytype)
  136. {
  137. OSSL_CMP_MSG *msg = NULL;
  138. if (!ossl_assert(ctx != NULL))
  139. return NULL;
  140. if ((msg = OSSL_CMP_MSG_new(ctx->libctx, ctx->propq)) == NULL)
  141. return NULL;
  142. if (!ossl_cmp_hdr_init(ctx, msg->header)
  143. || !ossl_cmp_msg_set_bodytype(msg, bodytype))
  144. goto err;
  145. if (ctx->geninfo_ITAVs != NULL
  146. && !ossl_cmp_hdr_generalInfo_push1_items(msg->header,
  147. ctx->geninfo_ITAVs))
  148. goto err;
  149. switch (bodytype) {
  150. case OSSL_CMP_PKIBODY_IR:
  151. case OSSL_CMP_PKIBODY_CR:
  152. case OSSL_CMP_PKIBODY_KUR:
  153. if ((msg->body->value.ir = OSSL_CRMF_MSGS_new()) == NULL)
  154. goto err;
  155. return msg;
  156. case OSSL_CMP_PKIBODY_P10CR:
  157. if (ctx->p10CSR == NULL) {
  158. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_P10CSR);
  159. goto err;
  160. }
  161. if ((msg->body->value.p10cr = X509_REQ_dup(ctx->p10CSR)) == NULL)
  162. goto err;
  163. return msg;
  164. case OSSL_CMP_PKIBODY_IP:
  165. case OSSL_CMP_PKIBODY_CP:
  166. case OSSL_CMP_PKIBODY_KUP:
  167. if ((msg->body->value.ip = OSSL_CMP_CERTREPMESSAGE_new()) == NULL)
  168. goto err;
  169. return msg;
  170. case OSSL_CMP_PKIBODY_RR:
  171. if ((msg->body->value.rr = sk_OSSL_CMP_REVDETAILS_new_null()) == NULL)
  172. goto err;
  173. return msg;
  174. case OSSL_CMP_PKIBODY_RP:
  175. if ((msg->body->value.rp = OSSL_CMP_REVREPCONTENT_new()) == NULL)
  176. goto err;
  177. return msg;
  178. case OSSL_CMP_PKIBODY_CERTCONF:
  179. if ((msg->body->value.certConf =
  180. sk_OSSL_CMP_CERTSTATUS_new_null()) == NULL)
  181. goto err;
  182. return msg;
  183. case OSSL_CMP_PKIBODY_PKICONF:
  184. if ((msg->body->value.pkiconf = ASN1_TYPE_new()) == NULL)
  185. goto err;
  186. ASN1_TYPE_set(msg->body->value.pkiconf, V_ASN1_NULL, NULL);
  187. return msg;
  188. case OSSL_CMP_PKIBODY_POLLREQ:
  189. if ((msg->body->value.pollReq = sk_OSSL_CMP_POLLREQ_new_null()) == NULL)
  190. goto err;
  191. return msg;
  192. case OSSL_CMP_PKIBODY_POLLREP:
  193. if ((msg->body->value.pollRep = sk_OSSL_CMP_POLLREP_new_null()) == NULL)
  194. goto err;
  195. return msg;
  196. case OSSL_CMP_PKIBODY_GENM:
  197. case OSSL_CMP_PKIBODY_GENP:
  198. if ((msg->body->value.genm = sk_OSSL_CMP_ITAV_new_null()) == NULL)
  199. goto err;
  200. return msg;
  201. case OSSL_CMP_PKIBODY_ERROR:
  202. if ((msg->body->value.error = OSSL_CMP_ERRORMSGCONTENT_new()) == NULL)
  203. goto err;
  204. return msg;
  205. default:
  206. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  207. goto err;
  208. }
  209. err:
  210. OSSL_CMP_MSG_free(msg);
  211. return NULL;
  212. }
  213. #define HAS_SAN(ctx) \
  214. (sk_GENERAL_NAME_num((ctx)->subjectAltNames) > 0 \
  215. || OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1)
  216. static const X509_NAME *determine_subj(OSSL_CMP_CTX *ctx, int for_KUR,
  217. const X509_NAME *ref_subj)
  218. {
  219. if (ctx->subjectName != NULL)
  220. return IS_NULL_DN(ctx->subjectName) ? NULL : ctx->subjectName;
  221. if (ctx->p10CSR != NULL) /* first default is from any given CSR */
  222. return X509_REQ_get_subject_name(ctx->p10CSR);
  223. if (for_KUR || !HAS_SAN(ctx))
  224. /*
  225. * For KUR, copy subject from any reference cert as fallback.
  226. * For IR or CR, do the same only if there is no subjectAltName.
  227. */
  228. return ref_subj;
  229. return NULL;
  230. }
  231. OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid)
  232. {
  233. OSSL_CRMF_MSG *crm = NULL;
  234. X509 *refcert = ctx->oldCert != NULL ? ctx->oldCert : ctx->cert;
  235. /* refcert defaults to current client cert */
  236. EVP_PKEY *rkey = OSSL_CMP_CTX_get0_newPkey(ctx, 0);
  237. STACK_OF(GENERAL_NAME) *default_sans = NULL;
  238. const X509_NAME *ref_subj =
  239. refcert != NULL ? X509_get_subject_name(refcert) : NULL;
  240. const X509_NAME *subject = determine_subj(ctx, for_KUR, ref_subj);
  241. const X509_NAME *issuer = ctx->issuer != NULL || refcert == NULL
  242. ? (IS_NULL_DN(ctx->issuer) ? NULL : ctx->issuer)
  243. : X509_get_issuer_name(refcert);
  244. int crit = ctx->setSubjectAltNameCritical || subject == NULL;
  245. /* RFC5280: subjectAltName MUST be critical if subject is null */
  246. X509_EXTENSIONS *exts = NULL;
  247. if (rkey == NULL && ctx->p10CSR != NULL)
  248. rkey = X509_REQ_get0_pubkey(ctx->p10CSR);
  249. if (rkey == NULL && refcert != NULL)
  250. rkey = X509_get0_pubkey(refcert);
  251. if (rkey == NULL)
  252. rkey = ctx->pkey; /* default is independent of ctx->oldCert */
  253. if (rkey == NULL) {
  254. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  255. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  256. return NULL;
  257. #endif
  258. }
  259. if (for_KUR && refcert == NULL && ctx->p10CSR == NULL) {
  260. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT);
  261. return NULL;
  262. }
  263. if ((crm = OSSL_CRMF_MSG_new()) == NULL)
  264. return NULL;
  265. if (!OSSL_CRMF_MSG_set_certReqId(crm, rid)
  266. /*
  267. * fill certTemplate, corresponding to CertificationRequestInfo
  268. * of PKCS#10. The rkey param cannot be NULL so far -
  269. * it could be NULL if centralized key creation was supported
  270. */
  271. || !OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_MSG_get0_tmpl(crm), rkey,
  272. subject, issuer, NULL /* serial */))
  273. goto err;
  274. if (ctx->days != 0) {
  275. time_t now = time(NULL);
  276. ASN1_TIME *notBefore = ASN1_TIME_adj(NULL, now, 0, 0);
  277. ASN1_TIME *notAfter = ASN1_TIME_adj(NULL, now, ctx->days, 0);
  278. if (notBefore == NULL
  279. || notAfter == NULL
  280. || !OSSL_CRMF_MSG_set0_validity(crm, notBefore, notAfter)) {
  281. ASN1_TIME_free(notBefore);
  282. ASN1_TIME_free(notAfter);
  283. goto err;
  284. }
  285. }
  286. /* extensions */
  287. if (ctx->p10CSR != NULL
  288. && (exts = X509_REQ_get_extensions(ctx->p10CSR)) == NULL)
  289. goto err;
  290. if (!ctx->SubjectAltName_nodefault && !HAS_SAN(ctx) && refcert != NULL
  291. && (default_sans = X509V3_get_d2i(X509_get0_extensions(refcert),
  292. NID_subject_alt_name, NULL, NULL))
  293. != NULL
  294. && !add1_extension(&exts, NID_subject_alt_name, crit, default_sans))
  295. goto err;
  296. if (ctx->reqExtensions != NULL /* augment/override existing ones */
  297. && !add_extensions(&exts, ctx->reqExtensions))
  298. goto err;
  299. if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0
  300. && !add1_extension(&exts, NID_subject_alt_name,
  301. crit, ctx->subjectAltNames))
  302. goto err;
  303. if (ctx->policies != NULL
  304. && !add1_extension(&exts, NID_certificate_policies,
  305. ctx->setPoliciesCritical, ctx->policies))
  306. goto err;
  307. if (!OSSL_CRMF_MSG_set0_extensions(crm, exts))
  308. goto err;
  309. exts = NULL;
  310. /* end fill certTemplate, now set any controls */
  311. /* for KUR, set OldCertId according to D.6 */
  312. if (for_KUR && refcert != NULL) {
  313. OSSL_CRMF_CERTID *cid =
  314. OSSL_CRMF_CERTID_gen(X509_get_issuer_name(refcert),
  315. X509_get0_serialNumber(refcert));
  316. int ret;
  317. if (cid == NULL)
  318. goto err;
  319. ret = OSSL_CRMF_MSG_set1_regCtrl_oldCertID(crm, cid);
  320. OSSL_CRMF_CERTID_free(cid);
  321. if (ret == 0)
  322. goto err;
  323. }
  324. goto end;
  325. err:
  326. OSSL_CRMF_MSG_free(crm);
  327. crm = NULL;
  328. end:
  329. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  330. sk_GENERAL_NAME_pop_free(default_sans, GENERAL_NAME_free);
  331. return crm;
  332. }
  333. OSSL_CMP_MSG *ossl_cmp_certreq_new(OSSL_CMP_CTX *ctx, int type,
  334. const OSSL_CRMF_MSG *crm)
  335. {
  336. OSSL_CMP_MSG *msg;
  337. OSSL_CRMF_MSG *local_crm = NULL;
  338. if (!ossl_assert(ctx != NULL))
  339. return NULL;
  340. if (type != OSSL_CMP_PKIBODY_IR && type != OSSL_CMP_PKIBODY_CR
  341. && type != OSSL_CMP_PKIBODY_KUR && type != OSSL_CMP_PKIBODY_P10CR) {
  342. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  343. return NULL;
  344. }
  345. if (type == OSSL_CMP_PKIBODY_P10CR && crm != NULL) {
  346. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  347. return NULL;
  348. }
  349. if ((msg = ossl_cmp_msg_create(ctx, type)) == NULL)
  350. goto err;
  351. /* header */
  352. if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
  353. goto err;
  354. /* body */
  355. /* For P10CR the content has already been set in OSSL_CMP_MSG_create */
  356. if (type != OSSL_CMP_PKIBODY_P10CR) {
  357. EVP_PKEY *privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
  358. /*
  359. * privkey is NULL in case ctx->newPkey does not include a private key.
  360. * We then may try to use ctx->pkey as fallback/default, but only
  361. * if ctx-> newPkey does not include a (non-matching) public key:
  362. */
  363. if (privkey == NULL && OSSL_CMP_CTX_get0_newPkey(ctx, 0) == NULL)
  364. privkey = ctx->pkey; /* default is independent of ctx->oldCert */
  365. if (ctx->popoMethod == OSSL_CRMF_POPO_SIGNATURE && privkey == NULL) {
  366. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY);
  367. goto err;
  368. }
  369. if (crm == NULL) {
  370. local_crm = OSSL_CMP_CTX_setup_CRM(ctx,
  371. type == OSSL_CMP_PKIBODY_KUR,
  372. OSSL_CMP_CERTREQID);
  373. if (local_crm == NULL
  374. || !OSSL_CRMF_MSG_create_popo(ctx->popoMethod, local_crm,
  375. privkey, ctx->digest,
  376. ctx->libctx, ctx->propq))
  377. goto err;
  378. } else {
  379. if ((local_crm = OSSL_CRMF_MSG_dup(crm)) == NULL)
  380. goto err;
  381. }
  382. /* value.ir is same for cr and kur */
  383. if (!sk_OSSL_CRMF_MSG_push(msg->body->value.ir, local_crm))
  384. goto err;
  385. local_crm = NULL;
  386. }
  387. if (!ossl_cmp_msg_protect(ctx, msg))
  388. goto err;
  389. return msg;
  390. err:
  391. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREQ);
  392. OSSL_CRMF_MSG_free(local_crm);
  393. OSSL_CMP_MSG_free(msg);
  394. return NULL;
  395. }
  396. OSSL_CMP_MSG *ossl_cmp_certrep_new(OSSL_CMP_CTX *ctx, int bodytype,
  397. int certReqId, const OSSL_CMP_PKISI *si,
  398. X509 *cert, const X509 *encryption_recip,
  399. STACK_OF(X509) *chain, STACK_OF(X509) *caPubs,
  400. int unprotectedErrors)
  401. {
  402. OSSL_CMP_MSG *msg = NULL;
  403. OSSL_CMP_CERTREPMESSAGE *repMsg = NULL;
  404. OSSL_CMP_CERTRESPONSE *resp = NULL;
  405. int status = -1;
  406. if (!ossl_assert(ctx != NULL && si != NULL))
  407. return NULL;
  408. if ((msg = ossl_cmp_msg_create(ctx, bodytype)) == NULL)
  409. goto err;
  410. repMsg = msg->body->value.ip; /* value.ip is same for cp and kup */
  411. /* header */
  412. if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
  413. goto err;
  414. /* body */
  415. if ((resp = OSSL_CMP_CERTRESPONSE_new()) == NULL)
  416. goto err;
  417. OSSL_CMP_PKISI_free(resp->status);
  418. if ((resp->status = OSSL_CMP_PKISI_dup(si)) == NULL
  419. || !ASN1_INTEGER_set(resp->certReqId, certReqId))
  420. goto err;
  421. status = ossl_cmp_pkisi_get_status(resp->status);
  422. if (status != OSSL_CMP_PKISTATUS_rejection
  423. && status != OSSL_CMP_PKISTATUS_waiting && cert != NULL) {
  424. if (encryption_recip != NULL) {
  425. ERR_raise(ERR_LIB_CMP, ERR_R_UNSUPPORTED);
  426. goto err;
  427. }
  428. if ((resp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new())
  429. == NULL)
  430. goto err;
  431. resp->certifiedKeyPair->certOrEncCert->type =
  432. OSSL_CMP_CERTORENCCERT_CERTIFICATE;
  433. if (!X509_up_ref(cert))
  434. goto err;
  435. resp->certifiedKeyPair->certOrEncCert->value.certificate = cert;
  436. }
  437. if (!sk_OSSL_CMP_CERTRESPONSE_push(repMsg->response, resp))
  438. goto err;
  439. resp = NULL;
  440. if (bodytype == OSSL_CMP_PKIBODY_IP && caPubs != NULL
  441. && (repMsg->caPubs = X509_chain_up_ref(caPubs)) == NULL)
  442. goto err;
  443. if (sk_X509_num(chain) > 0
  444. && !ossl_x509_add_certs_new(&msg->extraCerts, chain,
  445. X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
  446. goto err;
  447. if (!unprotectedErrors
  448. || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
  449. if (!ossl_cmp_msg_protect(ctx, msg))
  450. goto err;
  451. return msg;
  452. err:
  453. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
  454. OSSL_CMP_CERTRESPONSE_free(resp);
  455. OSSL_CMP_MSG_free(msg);
  456. return NULL;
  457. }
  458. OSSL_CMP_MSG *ossl_cmp_rr_new(OSSL_CMP_CTX *ctx)
  459. {
  460. OSSL_CMP_MSG *msg = NULL;
  461. OSSL_CMP_REVDETAILS *rd;
  462. int ret;
  463. if (!ossl_assert(ctx != NULL && (ctx->oldCert != NULL
  464. || ctx->p10CSR != NULL)))
  465. return NULL;
  466. if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL)
  467. goto err;
  468. /* Fill the template from the contents of the certificate to be revoked */
  469. ret = ctx->oldCert != NULL
  470. ? OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails,
  471. NULL /* pubkey would be redundant */,
  472. NULL /* subject would be redundant */,
  473. X509_get_issuer_name(ctx->oldCert),
  474. X509_get0_serialNumber(ctx->oldCert))
  475. : OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails,
  476. X509_REQ_get0_pubkey(ctx->p10CSR),
  477. X509_REQ_get_subject_name(ctx->p10CSR),
  478. NULL, NULL);
  479. if (!ret)
  480. goto err;
  481. /* revocation reason code is optional */
  482. if (ctx->revocationReason != CRL_REASON_NONE
  483. && !add_crl_reason_extension(&rd->crlEntryDetails,
  484. ctx->revocationReason))
  485. goto err;
  486. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL)
  487. goto err;
  488. if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd))
  489. goto err;
  490. rd = NULL;
  491. /* Revocation Passphrase according to section 5.3.19.9 could be set here */
  492. if (!ossl_cmp_msg_protect(ctx, msg))
  493. goto err;
  494. return msg;
  495. err:
  496. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
  497. OSSL_CMP_MSG_free(msg);
  498. OSSL_CMP_REVDETAILS_free(rd);
  499. return NULL;
  500. }
  501. OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
  502. const OSSL_CRMF_CERTID *cid, int unprotectedErrors)
  503. {
  504. OSSL_CMP_REVREPCONTENT *rep = NULL;
  505. OSSL_CMP_PKISI *si1 = NULL;
  506. OSSL_CRMF_CERTID *cid_copy = NULL;
  507. OSSL_CMP_MSG *msg = NULL;
  508. if (!ossl_assert(ctx != NULL && si != NULL))
  509. return NULL;
  510. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL)
  511. goto err;
  512. rep = msg->body->value.rp;
  513. if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL)
  514. goto err;
  515. if (!sk_OSSL_CMP_PKISI_push(rep->status, si1)) {
  516. OSSL_CMP_PKISI_free(si1);
  517. goto err;
  518. }
  519. if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL)
  520. goto err;
  521. if (cid != NULL) {
  522. if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL)
  523. goto err;
  524. if (!sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) {
  525. OSSL_CRMF_CERTID_free(cid_copy);
  526. goto err;
  527. }
  528. }
  529. if (!unprotectedErrors
  530. || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
  531. if (!ossl_cmp_msg_protect(ctx, msg))
  532. goto err;
  533. return msg;
  534. err:
  535. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RP);
  536. OSSL_CMP_MSG_free(msg);
  537. return NULL;
  538. }
  539. OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx)
  540. {
  541. OSSL_CMP_MSG *msg;
  542. if (!ossl_assert(ctx != NULL))
  543. return NULL;
  544. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL)
  545. goto err;
  546. if (ossl_cmp_msg_protect(ctx, msg))
  547. return msg;
  548. err:
  549. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
  550. OSSL_CMP_MSG_free(msg);
  551. return NULL;
  552. }
  553. int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav)
  554. {
  555. int bodytype;
  556. if (!ossl_assert(msg != NULL && itav != NULL))
  557. return 0;
  558. bodytype = OSSL_CMP_MSG_get_bodytype(msg);
  559. if (bodytype != OSSL_CMP_PKIBODY_GENM
  560. && bodytype != OSSL_CMP_PKIBODY_GENP) {
  561. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  562. return 0;
  563. }
  564. /* value.genp has the same structure, so this works for genp as well */
  565. return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav);
  566. }
  567. int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg,
  568. const STACK_OF(OSSL_CMP_ITAV) *itavs)
  569. {
  570. int i;
  571. OSSL_CMP_ITAV *itav = NULL;
  572. if (!ossl_assert(msg != NULL))
  573. return 0;
  574. for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
  575. itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
  576. if (itav == NULL
  577. || !ossl_cmp_msg_gen_push0_ITAV(msg, itav)) {
  578. OSSL_CMP_ITAV_free(itav);
  579. return 0;
  580. }
  581. }
  582. return 1;
  583. }
  584. /*
  585. * Creates a new General Message/Response with an empty itav stack
  586. * returns a pointer to the PKIMessage on success, NULL on error
  587. */
  588. static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx,
  589. const STACK_OF(OSSL_CMP_ITAV) *itavs,
  590. int body_type, int err_code)
  591. {
  592. OSSL_CMP_MSG *msg = NULL;
  593. if (!ossl_assert(ctx != NULL))
  594. return NULL;
  595. if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL)
  596. return NULL;
  597. if (ctx->genm_ITAVs != NULL
  598. && !ossl_cmp_msg_gen_push1_ITAVs(msg, itavs))
  599. goto err;
  600. if (!ossl_cmp_msg_protect(ctx, msg))
  601. goto err;
  602. return msg;
  603. err:
  604. ERR_raise(ERR_LIB_CMP, err_code);
  605. OSSL_CMP_MSG_free(msg);
  606. return NULL;
  607. }
  608. OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx)
  609. {
  610. return gen_new(ctx, ctx->genm_ITAVs,
  611. OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM);
  612. }
  613. OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx,
  614. const STACK_OF(OSSL_CMP_ITAV) *itavs)
  615. {
  616. return gen_new(ctx, itavs,
  617. OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP);
  618. }
  619. OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
  620. int64_t errorCode, const char *details,
  621. int unprotected)
  622. {
  623. OSSL_CMP_MSG *msg = NULL;
  624. const char *lib = NULL, *reason = NULL;
  625. OSSL_CMP_PKIFREETEXT *ft;
  626. if (!ossl_assert(ctx != NULL && si != NULL))
  627. return NULL;
  628. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL)
  629. goto err;
  630. OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo);
  631. if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si))
  632. == NULL)
  633. goto err;
  634. if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL)
  635. goto err;
  636. if (!ASN1_INTEGER_set_int64(msg->body->value.error->errorCode, errorCode))
  637. goto err;
  638. if (errorCode > 0
  639. && (uint64_t)errorCode < ((uint64_t)ERR_SYSTEM_FLAG << 1)) {
  640. lib = ERR_lib_error_string((unsigned long)errorCode);
  641. reason = ERR_reason_error_string((unsigned long)errorCode);
  642. }
  643. if (lib != NULL || reason != NULL || details != NULL) {
  644. if ((ft = sk_ASN1_UTF8STRING_new_null()) == NULL)
  645. goto err;
  646. msg->body->value.error->errorDetails = ft;
  647. if (lib != NULL && *lib != '\0'
  648. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, lib, -1))
  649. goto err;
  650. if (reason != NULL && *reason != '\0'
  651. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, reason, -1))
  652. goto err;
  653. if (details != NULL
  654. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, details, -1))
  655. goto err;
  656. }
  657. if (!unprotected && !ossl_cmp_msg_protect(ctx, msg))
  658. goto err;
  659. return msg;
  660. err:
  661. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_ERROR);
  662. OSSL_CMP_MSG_free(msg);
  663. return NULL;
  664. }
  665. /*
  666. * Set the certHash field of a OSSL_CMP_CERTSTATUS structure.
  667. * This is used in the certConf message, for example,
  668. * to confirm that the certificate was received successfully.
  669. */
  670. int ossl_cmp_certstatus_set0_certHash(OSSL_CMP_CERTSTATUS *certStatus,
  671. ASN1_OCTET_STRING *hash)
  672. {
  673. if (!ossl_assert(certStatus != NULL))
  674. return 0;
  675. ASN1_OCTET_STRING_free(certStatus->certHash);
  676. certStatus->certHash = hash;
  677. return 1;
  678. }
  679. OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int fail_info,
  680. const char *text)
  681. {
  682. OSSL_CMP_MSG *msg = NULL;
  683. OSSL_CMP_CERTSTATUS *certStatus = NULL;
  684. EVP_MD *md;
  685. int is_fallback;
  686. ASN1_OCTET_STRING *certHash = NULL;
  687. OSSL_CMP_PKISI *sinfo;
  688. if (!ossl_assert(ctx != NULL && ctx->newCert != NULL))
  689. return NULL;
  690. if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
  691. ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE);
  692. return NULL;
  693. }
  694. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL)
  695. goto err;
  696. if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL)
  697. goto err;
  698. /* consume certStatus into msg right away so it gets deallocated with msg */
  699. if (!sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus))
  700. goto err;
  701. /* set the ID of the certReq */
  702. if (!ASN1_INTEGER_set(certStatus->certReqId, OSSL_CMP_CERTREQID))
  703. goto err;
  704. certStatus->hashAlg = NULL;
  705. /*
  706. * The hash of the certificate, using the same hash algorithm
  707. * as is used to create and verify the certificate signature.
  708. * If not available, a fallback hash algorithm is used.
  709. */
  710. if ((certHash = X509_digest_sig(ctx->newCert, &md, &is_fallback)) == NULL)
  711. goto err;
  712. if (is_fallback) {
  713. if (!ossl_cmp_hdr_set_pvno(msg->header, OSSL_CMP_PVNO_3))
  714. goto err;
  715. if ((certStatus->hashAlg = X509_ALGOR_new()) == NULL)
  716. goto err;
  717. X509_ALGOR_set_md(certStatus->hashAlg, md);
  718. }
  719. EVP_MD_free(md);
  720. if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash))
  721. goto err;
  722. certHash = NULL;
  723. /*
  724. * For any particular CertStatus, omission of the statusInfo field
  725. * indicates ACCEPTANCE of the specified certificate. Alternatively,
  726. * explicit status details (with respect to acceptance or rejection) MAY
  727. * be provided in the statusInfo field, perhaps for auditing purposes at
  728. * the CA/RA.
  729. */
  730. sinfo = fail_info != 0 ?
  731. OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) :
  732. OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text);
  733. if (sinfo == NULL)
  734. goto err;
  735. certStatus->statusInfo = sinfo;
  736. if (!ossl_cmp_msg_protect(ctx, msg))
  737. goto err;
  738. return msg;
  739. err:
  740. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF);
  741. OSSL_CMP_MSG_free(msg);
  742. ASN1_OCTET_STRING_free(certHash);
  743. return NULL;
  744. }
  745. OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid)
  746. {
  747. OSSL_CMP_MSG *msg = NULL;
  748. OSSL_CMP_POLLREQ *preq = NULL;
  749. if (!ossl_assert(ctx != NULL))
  750. return NULL;
  751. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL)
  752. goto err;
  753. if ((preq = OSSL_CMP_POLLREQ_new()) == NULL
  754. || !ASN1_INTEGER_set(preq->certReqId, crid)
  755. || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq))
  756. goto err;
  757. preq = NULL;
  758. if (!ossl_cmp_msg_protect(ctx, msg))
  759. goto err;
  760. return msg;
  761. err:
  762. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ);
  763. OSSL_CMP_POLLREQ_free(preq);
  764. OSSL_CMP_MSG_free(msg);
  765. return NULL;
  766. }
  767. OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid,
  768. int64_t poll_after)
  769. {
  770. OSSL_CMP_MSG *msg;
  771. OSSL_CMP_POLLREP *prep;
  772. if (!ossl_assert(ctx != NULL))
  773. return NULL;
  774. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL)
  775. goto err;
  776. if ((prep = OSSL_CMP_POLLREP_new()) == NULL)
  777. goto err;
  778. if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep))
  779. goto err;
  780. if (!ASN1_INTEGER_set(prep->certReqId, crid))
  781. goto err;
  782. if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after))
  783. goto err;
  784. if (!ossl_cmp_msg_protect(ctx, msg))
  785. goto err;
  786. return msg;
  787. err:
  788. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
  789. OSSL_CMP_MSG_free(msg);
  790. return NULL;
  791. }
  792. /*-
  793. * returns the status field of the RevRepContent with the given
  794. * request/sequence id inside a revocation response.
  795. * RevRepContent has the revocation statuses in same order as they were sent in
  796. * RevReqContent.
  797. * returns NULL on error
  798. */
  799. OSSL_CMP_PKISI *
  800. ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  801. {
  802. OSSL_CMP_PKISI *status;
  803. if (!ossl_assert(rrep != NULL))
  804. return NULL;
  805. if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL)
  806. return status;
  807. ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND);
  808. return NULL;
  809. }
  810. /*
  811. * returns the CertId field in the revCerts part of the RevRepContent
  812. * with the given request/sequence id inside a revocation response.
  813. * RevRepContent has the CertIds in same order as they were sent in
  814. * RevReqContent.
  815. * returns NULL on error
  816. */
  817. OSSL_CRMF_CERTID *
  818. ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  819. {
  820. OSSL_CRMF_CERTID *cid = NULL;
  821. if (!ossl_assert(rrep != NULL))
  822. return NULL;
  823. if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL)
  824. return cid;
  825. ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND);
  826. return NULL;
  827. }
  828. static int suitable_rid(const ASN1_INTEGER *certReqId, int rid)
  829. {
  830. int trid;
  831. if (rid == -1)
  832. return 1;
  833. trid = ossl_cmp_asn1_get_int(certReqId);
  834. if (trid == -1) {
  835. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
  836. return 0;
  837. }
  838. return rid == trid;
  839. }
  840. /*
  841. * returns a pointer to the PollResponse with the given CertReqId
  842. * (or the first one in case -1) inside a PollRepContent
  843. * returns NULL on error or if no suitable PollResponse available
  844. */
  845. OSSL_CMP_POLLREP *
  846. ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc,
  847. int rid)
  848. {
  849. OSSL_CMP_POLLREP *pollRep = NULL;
  850. int i;
  851. if (!ossl_assert(prc != NULL))
  852. return NULL;
  853. for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) {
  854. pollRep = sk_OSSL_CMP_POLLREP_value(prc, i);
  855. if (suitable_rid(pollRep->certReqId, rid))
  856. return pollRep;
  857. }
  858. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
  859. "expected certReqId = %d", rid);
  860. return NULL;
  861. }
  862. /*
  863. * returns a pointer to the CertResponse with the given CertReqId
  864. * (or the first one in case -1) inside a CertRepMessage
  865. * returns NULL on error or if no suitable CertResponse available
  866. */
  867. OSSL_CMP_CERTRESPONSE *
  868. ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm,
  869. int rid)
  870. {
  871. OSSL_CMP_CERTRESPONSE *crep = NULL;
  872. int i;
  873. if (!ossl_assert(crm != NULL && crm->response != NULL))
  874. return NULL;
  875. for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) {
  876. crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i);
  877. if (suitable_rid(crep->certReqId, rid))
  878. return crep;
  879. }
  880. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
  881. "expected certReqId = %d", rid);
  882. return NULL;
  883. }
  884. /*-
  885. * Retrieve the newly enrolled certificate from the given certResponse crep.
  886. * In case of indirect POPO uses the libctx and propq from ctx and private key.
  887. * Returns a pointer to a copy of the found certificate, or NULL if not found.
  888. */
  889. X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CERTRESPONSE *crep,
  890. const OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
  891. {
  892. OSSL_CMP_CERTORENCCERT *coec;
  893. X509 *crt = NULL;
  894. if (!ossl_assert(crep != NULL && ctx != NULL))
  895. return NULL;
  896. if (crep->certifiedKeyPair
  897. && (coec = crep->certifiedKeyPair->certOrEncCert) != NULL) {
  898. switch (coec->type) {
  899. case OSSL_CMP_CERTORENCCERT_CERTIFICATE:
  900. crt = X509_dup(coec->value.certificate);
  901. break;
  902. case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT:
  903. /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */
  904. if (pkey == NULL) {
  905. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY);
  906. return NULL;
  907. }
  908. crt =
  909. OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(coec->value.encryptedCert,
  910. ctx->libctx, ctx->propq,
  911. pkey);
  912. break;
  913. default:
  914. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE);
  915. return NULL;
  916. }
  917. }
  918. if (crt == NULL)
  919. ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
  920. else
  921. (void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq);
  922. return crt;
  923. }
  924. int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  925. {
  926. if (ctx == NULL || msg == NULL) {
  927. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  928. return 0;
  929. }
  930. if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header))
  931. return 0;
  932. return msg->header->protectionAlg == NULL
  933. || ossl_cmp_msg_protect(ctx, msg);
  934. }
  935. OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx,
  936. const char *propq)
  937. {
  938. OSSL_CMP_MSG *msg;
  939. BIO *bio = NULL;
  940. if (file == NULL) {
  941. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  942. return NULL;
  943. }
  944. msg = OSSL_CMP_MSG_new(libctx, propq);
  945. if (msg == NULL) {
  946. ERR_raise(ERR_LIB_CMP, ERR_R_CMP_LIB);
  947. return NULL;
  948. }
  949. if ((bio = BIO_new_file(file, "rb")) == NULL
  950. || d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) {
  951. OSSL_CMP_MSG_free(msg);
  952. msg = NULL;
  953. }
  954. BIO_free(bio);
  955. return msg;
  956. }
  957. int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg)
  958. {
  959. BIO *bio;
  960. int res;
  961. if (file == NULL || msg == NULL) {
  962. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  963. return -1;
  964. }
  965. bio = BIO_new_file(file, "wb");
  966. if (bio == NULL)
  967. return -2;
  968. res = i2d_OSSL_CMP_MSG_bio(bio, msg);
  969. BIO_free(bio);
  970. return res;
  971. }
  972. OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in,
  973. long len)
  974. {
  975. OSSL_LIB_CTX *libctx = NULL;
  976. const char *propq = NULL;
  977. if (msg != NULL && *msg != NULL) {
  978. libctx = (*msg)->libctx;
  979. propq = (*msg)->propq;
  980. }
  981. return (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len,
  982. ASN1_ITEM_rptr(OSSL_CMP_MSG),
  983. libctx, propq);
  984. }
  985. int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out)
  986. {
  987. return ASN1_item_i2d((const ASN1_VALUE *)msg, out,
  988. ASN1_ITEM_rptr(OSSL_CMP_MSG));
  989. }
  990. OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg)
  991. {
  992. OSSL_LIB_CTX *libctx = NULL;
  993. const char *propq = NULL;
  994. if (msg != NULL && *msg != NULL) {
  995. libctx = (*msg)->libctx;
  996. propq = (*msg)->propq;
  997. }
  998. return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx,
  999. propq);
  1000. }
  1001. int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg)
  1002. {
  1003. return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg);
  1004. }