cmp_msg.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. * Copyright 2007-2023 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_newPubkey(ctx);
  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) {
  248. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  249. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PUBLIC_KEY);
  250. return NULL;
  251. #endif
  252. }
  253. if (for_KUR && refcert == NULL && ctx->p10CSR == NULL) {
  254. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT);
  255. return NULL;
  256. }
  257. if ((crm = OSSL_CRMF_MSG_new()) == NULL)
  258. return NULL;
  259. if (!OSSL_CRMF_MSG_set_certReqId(crm, rid)
  260. /*
  261. * fill certTemplate, corresponding to CertificationRequestInfo
  262. * of PKCS#10. The rkey param cannot be NULL so far -
  263. * it could be NULL if centralized key creation was supported
  264. */
  265. || !OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_MSG_get0_tmpl(crm), rkey,
  266. subject, issuer, NULL /* serial */))
  267. goto err;
  268. if (ctx->days != 0) {
  269. time_t now = time(NULL);
  270. ASN1_TIME *notBefore = ASN1_TIME_adj(NULL, now, 0, 0);
  271. ASN1_TIME *notAfter = ASN1_TIME_adj(NULL, now, ctx->days, 0);
  272. if (notBefore == NULL
  273. || notAfter == NULL
  274. || !OSSL_CRMF_MSG_set0_validity(crm, notBefore, notAfter)) {
  275. ASN1_TIME_free(notBefore);
  276. ASN1_TIME_free(notAfter);
  277. goto err;
  278. }
  279. }
  280. /* extensions */
  281. if (ctx->p10CSR != NULL
  282. && (exts = X509_REQ_get_extensions(ctx->p10CSR)) == NULL)
  283. goto err;
  284. if (!ctx->SubjectAltName_nodefault && !HAS_SAN(ctx) && refcert != NULL
  285. && (default_sans = X509V3_get_d2i(X509_get0_extensions(refcert),
  286. NID_subject_alt_name, NULL, NULL))
  287. != NULL
  288. && !add1_extension(&exts, NID_subject_alt_name, crit, default_sans))
  289. goto err;
  290. if (ctx->reqExtensions != NULL /* augment/override existing ones */
  291. && !add_extensions(&exts, ctx->reqExtensions))
  292. goto err;
  293. if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0
  294. && !add1_extension(&exts, NID_subject_alt_name,
  295. crit, ctx->subjectAltNames))
  296. goto err;
  297. if (ctx->policies != NULL
  298. && !add1_extension(&exts, NID_certificate_policies,
  299. ctx->setPoliciesCritical, ctx->policies))
  300. goto err;
  301. if (!OSSL_CRMF_MSG_set0_extensions(crm, exts))
  302. goto err;
  303. exts = NULL;
  304. /* end fill certTemplate, now set any controls */
  305. /* for KUR, set OldCertId according to D.6 */
  306. if (for_KUR && refcert != NULL) {
  307. OSSL_CRMF_CERTID *cid =
  308. OSSL_CRMF_CERTID_gen(X509_get_issuer_name(refcert),
  309. X509_get0_serialNumber(refcert));
  310. int ret;
  311. if (cid == NULL)
  312. goto err;
  313. ret = OSSL_CRMF_MSG_set1_regCtrl_oldCertID(crm, cid);
  314. OSSL_CRMF_CERTID_free(cid);
  315. if (ret == 0)
  316. goto err;
  317. }
  318. goto end;
  319. err:
  320. OSSL_CRMF_MSG_free(crm);
  321. crm = NULL;
  322. end:
  323. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  324. sk_GENERAL_NAME_pop_free(default_sans, GENERAL_NAME_free);
  325. return crm;
  326. }
  327. OSSL_CMP_MSG *ossl_cmp_certreq_new(OSSL_CMP_CTX *ctx, int type,
  328. const OSSL_CRMF_MSG *crm)
  329. {
  330. OSSL_CMP_MSG *msg;
  331. OSSL_CRMF_MSG *local_crm = NULL;
  332. if (!ossl_assert(ctx != NULL))
  333. return NULL;
  334. if (type != OSSL_CMP_PKIBODY_IR && type != OSSL_CMP_PKIBODY_CR
  335. && type != OSSL_CMP_PKIBODY_KUR && type != OSSL_CMP_PKIBODY_P10CR) {
  336. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  337. return NULL;
  338. }
  339. if (type == OSSL_CMP_PKIBODY_P10CR && crm != NULL) {
  340. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  341. return NULL;
  342. }
  343. if ((msg = ossl_cmp_msg_create(ctx, type)) == NULL)
  344. goto err;
  345. /* header */
  346. if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
  347. goto err;
  348. /* body */
  349. /* For P10CR the content has already been set in OSSL_CMP_MSG_create */
  350. if (type != OSSL_CMP_PKIBODY_P10CR) {
  351. EVP_PKEY *privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
  352. /* privkey is ctx->newPkey (if private, else NULL) or ctx->pkey */
  353. if (ctx->popoMethod >= OSSL_CRMF_POPO_SIGNATURE && privkey == NULL) {
  354. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY_FOR_POPO);
  355. goto err;
  356. }
  357. if (crm == NULL) {
  358. local_crm = OSSL_CMP_CTX_setup_CRM(ctx,
  359. type == OSSL_CMP_PKIBODY_KUR,
  360. OSSL_CMP_CERTREQID);
  361. if (local_crm == NULL
  362. || !OSSL_CRMF_MSG_create_popo(ctx->popoMethod, local_crm,
  363. privkey, ctx->digest,
  364. ctx->libctx, ctx->propq))
  365. goto err;
  366. } else {
  367. if ((local_crm = OSSL_CRMF_MSG_dup(crm)) == NULL)
  368. goto err;
  369. }
  370. /* value.ir is same for cr and kur */
  371. if (!sk_OSSL_CRMF_MSG_push(msg->body->value.ir, local_crm))
  372. goto err;
  373. local_crm = NULL;
  374. }
  375. if (!ossl_cmp_msg_protect(ctx, msg))
  376. goto err;
  377. return msg;
  378. err:
  379. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREQ);
  380. OSSL_CRMF_MSG_free(local_crm);
  381. OSSL_CMP_MSG_free(msg);
  382. return NULL;
  383. }
  384. OSSL_CMP_MSG *ossl_cmp_certrep_new(OSSL_CMP_CTX *ctx, int bodytype,
  385. int certReqId, const OSSL_CMP_PKISI *si,
  386. X509 *cert, const X509 *encryption_recip,
  387. STACK_OF(X509) *chain, STACK_OF(X509) *caPubs,
  388. int unprotectedErrors)
  389. {
  390. OSSL_CMP_MSG *msg = NULL;
  391. OSSL_CMP_CERTREPMESSAGE *repMsg = NULL;
  392. OSSL_CMP_CERTRESPONSE *resp = NULL;
  393. int status = OSSL_CMP_PKISTATUS_unspecified;
  394. if (!ossl_assert(ctx != NULL && si != NULL))
  395. return NULL;
  396. if ((msg = ossl_cmp_msg_create(ctx, bodytype)) == NULL)
  397. goto err;
  398. repMsg = msg->body->value.ip; /* value.ip is same for cp and kup */
  399. /* header */
  400. if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
  401. goto err;
  402. /* body */
  403. if ((resp = OSSL_CMP_CERTRESPONSE_new()) == NULL)
  404. goto err;
  405. OSSL_CMP_PKISI_free(resp->status);
  406. if ((resp->status = OSSL_CMP_PKISI_dup(si)) == NULL
  407. || !ASN1_INTEGER_set(resp->certReqId, certReqId))
  408. goto err;
  409. status = ossl_cmp_pkisi_get_status(resp->status);
  410. if (status != OSSL_CMP_PKISTATUS_rejection
  411. && status != OSSL_CMP_PKISTATUS_waiting && cert != NULL) {
  412. if (encryption_recip != NULL) {
  413. ERR_raise(ERR_LIB_CMP, ERR_R_UNSUPPORTED);
  414. goto err;
  415. }
  416. if ((resp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new())
  417. == NULL)
  418. goto err;
  419. resp->certifiedKeyPair->certOrEncCert->type =
  420. OSSL_CMP_CERTORENCCERT_CERTIFICATE;
  421. if (!X509_up_ref(cert))
  422. goto err;
  423. resp->certifiedKeyPair->certOrEncCert->value.certificate = cert;
  424. }
  425. if (!sk_OSSL_CMP_CERTRESPONSE_push(repMsg->response, resp))
  426. goto err;
  427. resp = NULL;
  428. if (bodytype == OSSL_CMP_PKIBODY_IP && caPubs != NULL
  429. && (repMsg->caPubs = X509_chain_up_ref(caPubs)) == NULL)
  430. goto err;
  431. if (sk_X509_num(chain) > 0
  432. && !ossl_x509_add_certs_new(&msg->extraCerts, chain,
  433. X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
  434. goto err;
  435. if (!unprotectedErrors
  436. || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
  437. if (!ossl_cmp_msg_protect(ctx, msg))
  438. goto err;
  439. return msg;
  440. err:
  441. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
  442. OSSL_CMP_CERTRESPONSE_free(resp);
  443. OSSL_CMP_MSG_free(msg);
  444. return NULL;
  445. }
  446. OSSL_CMP_MSG *ossl_cmp_rr_new(OSSL_CMP_CTX *ctx)
  447. {
  448. OSSL_CMP_MSG *msg = NULL;
  449. OSSL_CMP_REVDETAILS *rd;
  450. int ret;
  451. if (!ossl_assert(ctx != NULL && (ctx->oldCert != NULL
  452. || ctx->p10CSR != NULL)))
  453. return NULL;
  454. if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL)
  455. goto err;
  456. /* Fill the template from the contents of the certificate to be revoked */
  457. ret = ctx->oldCert != NULL
  458. ? OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails,
  459. NULL /* pubkey would be redundant */,
  460. NULL /* subject would be redundant */,
  461. X509_get_issuer_name(ctx->oldCert),
  462. X509_get0_serialNumber(ctx->oldCert))
  463. : OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails,
  464. X509_REQ_get0_pubkey(ctx->p10CSR),
  465. X509_REQ_get_subject_name(ctx->p10CSR),
  466. NULL, NULL);
  467. if (!ret)
  468. goto err;
  469. /* revocation reason code is optional */
  470. if (ctx->revocationReason != CRL_REASON_NONE
  471. && !add_crl_reason_extension(&rd->crlEntryDetails,
  472. ctx->revocationReason))
  473. goto err;
  474. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL)
  475. goto err;
  476. if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd))
  477. goto err;
  478. rd = NULL;
  479. /* Revocation Passphrase according to section 5.3.19.9 could be set here */
  480. if (!ossl_cmp_msg_protect(ctx, msg))
  481. goto err;
  482. return msg;
  483. err:
  484. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
  485. OSSL_CMP_MSG_free(msg);
  486. OSSL_CMP_REVDETAILS_free(rd);
  487. return NULL;
  488. }
  489. OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
  490. const OSSL_CRMF_CERTID *cid, int unprotectedErrors)
  491. {
  492. OSSL_CMP_REVREPCONTENT *rep = NULL;
  493. OSSL_CMP_PKISI *si1 = NULL;
  494. OSSL_CRMF_CERTID *cid_copy = NULL;
  495. OSSL_CMP_MSG *msg = NULL;
  496. if (!ossl_assert(ctx != NULL && si != NULL))
  497. return NULL;
  498. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL)
  499. goto err;
  500. rep = msg->body->value.rp;
  501. if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL)
  502. goto err;
  503. if (!sk_OSSL_CMP_PKISI_push(rep->status, si1)) {
  504. OSSL_CMP_PKISI_free(si1);
  505. goto err;
  506. }
  507. if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL)
  508. goto err;
  509. if (cid != NULL) {
  510. if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL)
  511. goto err;
  512. if (!sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) {
  513. OSSL_CRMF_CERTID_free(cid_copy);
  514. goto err;
  515. }
  516. }
  517. if (!unprotectedErrors
  518. || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
  519. if (!ossl_cmp_msg_protect(ctx, msg))
  520. goto err;
  521. return msg;
  522. err:
  523. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RP);
  524. OSSL_CMP_MSG_free(msg);
  525. return NULL;
  526. }
  527. OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx)
  528. {
  529. OSSL_CMP_MSG *msg;
  530. if (!ossl_assert(ctx != NULL))
  531. return NULL;
  532. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL)
  533. goto err;
  534. if (ossl_cmp_msg_protect(ctx, msg))
  535. return msg;
  536. err:
  537. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
  538. OSSL_CMP_MSG_free(msg);
  539. return NULL;
  540. }
  541. int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav)
  542. {
  543. int bodytype;
  544. if (!ossl_assert(msg != NULL && itav != NULL))
  545. return 0;
  546. bodytype = OSSL_CMP_MSG_get_bodytype(msg);
  547. if (bodytype != OSSL_CMP_PKIBODY_GENM
  548. && bodytype != OSSL_CMP_PKIBODY_GENP) {
  549. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  550. return 0;
  551. }
  552. /* value.genp has the same structure, so this works for genp as well */
  553. return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav);
  554. }
  555. int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg,
  556. const STACK_OF(OSSL_CMP_ITAV) *itavs)
  557. {
  558. int i;
  559. OSSL_CMP_ITAV *itav = NULL;
  560. if (!ossl_assert(msg != NULL))
  561. return 0;
  562. for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
  563. itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
  564. if (itav == NULL
  565. || !ossl_cmp_msg_gen_push0_ITAV(msg, itav)) {
  566. OSSL_CMP_ITAV_free(itav);
  567. return 0;
  568. }
  569. }
  570. return 1;
  571. }
  572. /*
  573. * Creates a new General Message/Response with an empty itav stack
  574. * returns a pointer to the PKIMessage on success, NULL on error
  575. */
  576. static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx,
  577. const STACK_OF(OSSL_CMP_ITAV) *itavs,
  578. int body_type, int err_code)
  579. {
  580. OSSL_CMP_MSG *msg = NULL;
  581. if (!ossl_assert(ctx != NULL))
  582. return NULL;
  583. if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL)
  584. return NULL;
  585. if (itavs != NULL && !ossl_cmp_msg_gen_push1_ITAVs(msg, itavs))
  586. goto err;
  587. if (!ossl_cmp_msg_protect(ctx, msg))
  588. goto err;
  589. return msg;
  590. err:
  591. ERR_raise(ERR_LIB_CMP, err_code);
  592. OSSL_CMP_MSG_free(msg);
  593. return NULL;
  594. }
  595. OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx)
  596. {
  597. return gen_new(ctx, ctx->genm_ITAVs,
  598. OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM);
  599. }
  600. OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx,
  601. const STACK_OF(OSSL_CMP_ITAV) *itavs)
  602. {
  603. return gen_new(ctx, itavs,
  604. OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP);
  605. }
  606. OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
  607. int64_t errorCode, const char *details,
  608. int unprotected)
  609. {
  610. OSSL_CMP_MSG *msg = NULL;
  611. const char *lib = NULL, *reason = NULL;
  612. OSSL_CMP_PKIFREETEXT *ft;
  613. if (!ossl_assert(ctx != NULL && si != NULL))
  614. return NULL;
  615. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL)
  616. goto err;
  617. OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo);
  618. if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si))
  619. == NULL)
  620. goto err;
  621. if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL)
  622. goto err;
  623. if (!ASN1_INTEGER_set_int64(msg->body->value.error->errorCode, errorCode))
  624. goto err;
  625. if (errorCode > 0
  626. && (uint64_t)errorCode < ((uint64_t)ERR_SYSTEM_FLAG << 1)) {
  627. lib = ERR_lib_error_string((unsigned long)errorCode);
  628. reason = ERR_reason_error_string((unsigned long)errorCode);
  629. }
  630. if (lib != NULL || reason != NULL || details != NULL) {
  631. if ((ft = sk_ASN1_UTF8STRING_new_null()) == NULL)
  632. goto err;
  633. msg->body->value.error->errorDetails = ft;
  634. if (lib != NULL && *lib != '\0'
  635. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, lib, -1))
  636. goto err;
  637. if (reason != NULL && *reason != '\0'
  638. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, reason, -1))
  639. goto err;
  640. if (details != NULL
  641. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, details, -1))
  642. goto err;
  643. }
  644. if (!unprotected && !ossl_cmp_msg_protect(ctx, msg))
  645. goto err;
  646. return msg;
  647. err:
  648. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_ERROR);
  649. OSSL_CMP_MSG_free(msg);
  650. return NULL;
  651. }
  652. /*
  653. * Set the certHash field of a OSSL_CMP_CERTSTATUS structure.
  654. * This is used in the certConf message, for example,
  655. * to confirm that the certificate was received successfully.
  656. */
  657. int ossl_cmp_certstatus_set0_certHash(OSSL_CMP_CERTSTATUS *certStatus,
  658. ASN1_OCTET_STRING *hash)
  659. {
  660. if (!ossl_assert(certStatus != NULL))
  661. return 0;
  662. ASN1_OCTET_STRING_free(certStatus->certHash);
  663. certStatus->certHash = hash;
  664. return 1;
  665. }
  666. OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int certReqId,
  667. int fail_info, const char *text)
  668. {
  669. OSSL_CMP_MSG *msg = NULL;
  670. OSSL_CMP_CERTSTATUS *certStatus = NULL;
  671. ASN1_OCTET_STRING *certHash = NULL;
  672. OSSL_CMP_PKISI *sinfo;
  673. if (!ossl_assert(ctx != NULL && ctx->newCert != NULL
  674. && (certReqId == OSSL_CMP_CERTREQID
  675. || certReqId == OSSL_CMP_CERTREQID_NONE)))
  676. return NULL;
  677. if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
  678. ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE);
  679. return NULL;
  680. }
  681. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL)
  682. goto err;
  683. if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL)
  684. goto err;
  685. /* consume certStatus into msg right away so it gets deallocated with msg */
  686. if (sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus) < 1) {
  687. OSSL_CMP_CERTSTATUS_free(certStatus);
  688. goto err;
  689. }
  690. /* set the ID of the certReq */
  691. if (!ASN1_INTEGER_set(certStatus->certReqId, certReqId))
  692. goto err;
  693. /*
  694. * The hash of the certificate, using the same hash algorithm
  695. * as is used to create and verify the certificate signature.
  696. * If not available, a default hash algorithm is used.
  697. */
  698. if ((certHash = X509_digest_sig(ctx->newCert, NULL, NULL)) == NULL)
  699. goto err;
  700. if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash))
  701. goto err;
  702. certHash = NULL;
  703. /*
  704. * For any particular CertStatus, omission of the statusInfo field
  705. * indicates ACCEPTANCE of the specified certificate. Alternatively,
  706. * explicit status details (with respect to acceptance or rejection) MAY
  707. * be provided in the statusInfo field, perhaps for auditing purposes at
  708. * the CA/RA.
  709. */
  710. sinfo = fail_info != 0 ?
  711. OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) :
  712. OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text);
  713. if (sinfo == NULL)
  714. goto err;
  715. certStatus->statusInfo = sinfo;
  716. if (!ossl_cmp_msg_protect(ctx, msg))
  717. goto err;
  718. return msg;
  719. err:
  720. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF);
  721. OSSL_CMP_MSG_free(msg);
  722. ASN1_OCTET_STRING_free(certHash);
  723. return NULL;
  724. }
  725. OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid)
  726. {
  727. OSSL_CMP_MSG *msg = NULL;
  728. OSSL_CMP_POLLREQ *preq = NULL;
  729. if (!ossl_assert(ctx != NULL))
  730. return NULL;
  731. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL)
  732. goto err;
  733. if ((preq = OSSL_CMP_POLLREQ_new()) == NULL
  734. || !ASN1_INTEGER_set(preq->certReqId, crid)
  735. || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq))
  736. goto err;
  737. preq = NULL;
  738. if (!ossl_cmp_msg_protect(ctx, msg))
  739. goto err;
  740. return msg;
  741. err:
  742. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ);
  743. OSSL_CMP_POLLREQ_free(preq);
  744. OSSL_CMP_MSG_free(msg);
  745. return NULL;
  746. }
  747. OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid,
  748. int64_t poll_after)
  749. {
  750. OSSL_CMP_MSG *msg;
  751. OSSL_CMP_POLLREP *prep;
  752. if (!ossl_assert(ctx != NULL))
  753. return NULL;
  754. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL)
  755. goto err;
  756. if ((prep = OSSL_CMP_POLLREP_new()) == NULL)
  757. goto err;
  758. if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep))
  759. goto err;
  760. if (!ASN1_INTEGER_set(prep->certReqId, crid))
  761. goto err;
  762. if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after))
  763. goto err;
  764. if (!ossl_cmp_msg_protect(ctx, msg))
  765. goto err;
  766. return msg;
  767. err:
  768. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
  769. OSSL_CMP_MSG_free(msg);
  770. return NULL;
  771. }
  772. /*-
  773. * returns the status field of the RevRepContent with the given
  774. * request/sequence id inside a revocation response.
  775. * RevRepContent has the revocation statuses in same order as they were sent in
  776. * RevReqContent.
  777. * returns NULL on error
  778. */
  779. OSSL_CMP_PKISI *
  780. ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  781. {
  782. OSSL_CMP_PKISI *status;
  783. if (!ossl_assert(rrep != NULL))
  784. return NULL;
  785. if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL)
  786. return status;
  787. ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND);
  788. return NULL;
  789. }
  790. /*
  791. * returns the CertId field in the revCerts part of the RevRepContent
  792. * with the given request/sequence id inside a revocation response.
  793. * RevRepContent has the CertIds in same order as they were sent in
  794. * RevReqContent.
  795. * returns NULL on error
  796. */
  797. OSSL_CRMF_CERTID *
  798. ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  799. {
  800. OSSL_CRMF_CERTID *cid = NULL;
  801. if (!ossl_assert(rrep != NULL))
  802. return NULL;
  803. if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL)
  804. return cid;
  805. ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND);
  806. return NULL;
  807. }
  808. static int suitable_rid(const ASN1_INTEGER *certReqId, int rid)
  809. {
  810. int trid;
  811. if (rid == OSSL_CMP_CERTREQID_NONE)
  812. return 1;
  813. trid = ossl_cmp_asn1_get_int(certReqId);
  814. if (trid == OSSL_CMP_CERTREQID_NONE) {
  815. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
  816. return 0;
  817. }
  818. return rid == trid;
  819. }
  820. /*
  821. * returns a pointer to the PollResponse with the given CertReqId
  822. * (or the first one in case -1) inside a PollRepContent
  823. * returns NULL on error or if no suitable PollResponse available
  824. */
  825. OSSL_CMP_POLLREP *
  826. ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc,
  827. int rid)
  828. {
  829. OSSL_CMP_POLLREP *pollRep = NULL;
  830. int i;
  831. if (!ossl_assert(prc != NULL))
  832. return NULL;
  833. for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) {
  834. pollRep = sk_OSSL_CMP_POLLREP_value(prc, i);
  835. if (suitable_rid(pollRep->certReqId, rid))
  836. return pollRep;
  837. }
  838. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
  839. "expected certReqId = %d", rid);
  840. return NULL;
  841. }
  842. /*
  843. * returns a pointer to the CertResponse with the given CertReqId
  844. * (or the first one in case -1) inside a CertRepMessage
  845. * returns NULL on error or if no suitable CertResponse available
  846. */
  847. OSSL_CMP_CERTRESPONSE *
  848. ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm,
  849. int rid)
  850. {
  851. OSSL_CMP_CERTRESPONSE *crep = NULL;
  852. int i;
  853. if (!ossl_assert(crm != NULL && crm->response != NULL))
  854. return NULL;
  855. for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) {
  856. crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i);
  857. if (suitable_rid(crep->certReqId, rid))
  858. return crep;
  859. }
  860. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
  861. "expected certReqId = %d", rid);
  862. return NULL;
  863. }
  864. /*-
  865. * Retrieve the newly enrolled certificate from the given certResponse crep.
  866. * Uses libctx and propq from ctx, in case of indirect POPO also private key.
  867. * Returns a pointer to a copy of the found certificate, or NULL if not found.
  868. */
  869. X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CTX *ctx,
  870. const OSSL_CMP_CERTRESPONSE *crep)
  871. {
  872. OSSL_CMP_CERTORENCCERT *coec;
  873. X509 *crt = NULL;
  874. EVP_PKEY *pkey;
  875. if (!ossl_assert(crep != NULL && ctx != NULL))
  876. return NULL;
  877. if (crep->certifiedKeyPair
  878. && (coec = crep->certifiedKeyPair->certOrEncCert) != NULL) {
  879. switch (coec->type) {
  880. case OSSL_CMP_CERTORENCCERT_CERTIFICATE:
  881. crt = X509_dup(coec->value.certificate);
  882. break;
  883. case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT:
  884. /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */
  885. pkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
  886. /* pkey is ctx->newPkey (if private, else NULL) or ctx->pkey */
  887. if (pkey == NULL) {
  888. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY);
  889. return NULL;
  890. }
  891. crt =
  892. OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(coec->value.encryptedCert,
  893. ctx->libctx, ctx->propq,
  894. pkey);
  895. break;
  896. default:
  897. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE);
  898. return NULL;
  899. }
  900. }
  901. if (crt == NULL)
  902. ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
  903. else
  904. (void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq);
  905. return crt;
  906. }
  907. int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  908. {
  909. if (ctx == NULL || msg == NULL) {
  910. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  911. return 0;
  912. }
  913. if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header))
  914. return 0;
  915. return msg->header->protectionAlg == NULL
  916. || ossl_cmp_msg_protect(ctx, msg);
  917. }
  918. int OSSL_CMP_MSG_update_recipNonce(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  919. {
  920. if (ctx == NULL || msg == NULL || msg->header == NULL) {
  921. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  922. return 0;
  923. }
  924. if (ctx->recipNonce == NULL) /* nothing to do for 1st msg in transaction */
  925. return 1;
  926. if (!ossl_cmp_asn1_octet_string_set1(&msg->header->recipNonce,
  927. ctx->recipNonce))
  928. return 0;
  929. return msg->header->protectionAlg == NULL || ossl_cmp_msg_protect(ctx, msg);
  930. }
  931. OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx,
  932. const char *propq)
  933. {
  934. OSSL_CMP_MSG *msg;
  935. BIO *bio = NULL;
  936. if (file == NULL) {
  937. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  938. return NULL;
  939. }
  940. msg = OSSL_CMP_MSG_new(libctx, propq);
  941. if (msg == NULL){
  942. ERR_raise(ERR_LIB_CMP, ERR_R_MALLOC_FAILURE);
  943. return NULL;
  944. }
  945. if ((bio = BIO_new_file(file, "rb")) == NULL
  946. || d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) {
  947. OSSL_CMP_MSG_free(msg);
  948. msg = NULL;
  949. }
  950. BIO_free(bio);
  951. return msg;
  952. }
  953. int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg)
  954. {
  955. BIO *bio;
  956. int res;
  957. if (file == NULL || msg == NULL) {
  958. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  959. return -1;
  960. }
  961. bio = BIO_new_file(file, "wb");
  962. if (bio == NULL)
  963. return -2;
  964. res = i2d_OSSL_CMP_MSG_bio(bio, msg);
  965. BIO_free(bio);
  966. return res;
  967. }
  968. OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in,
  969. long len)
  970. {
  971. OSSL_LIB_CTX *libctx = NULL;
  972. const char *propq = NULL;
  973. if (msg != NULL && *msg != NULL) {
  974. libctx = (*msg)->libctx;
  975. propq = (*msg)->propq;
  976. }
  977. return (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len,
  978. ASN1_ITEM_rptr(OSSL_CMP_MSG),
  979. libctx, propq);
  980. }
  981. int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out)
  982. {
  983. return ASN1_item_i2d((const ASN1_VALUE *)msg, out,
  984. ASN1_ITEM_rptr(OSSL_CMP_MSG));
  985. }
  986. OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg)
  987. {
  988. OSSL_LIB_CTX *libctx = NULL;
  989. const char *propq = NULL;
  990. if (msg != NULL && *msg != NULL) {
  991. libctx = (*msg)->libctx;
  992. propq = (*msg)->propq;
  993. }
  994. return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx,
  995. propq);
  996. }
  997. int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg)
  998. {
  999. return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg);
  1000. }