cmp_msg.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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,
  217. const X509_NAME *ref_subj,
  218. int for_KUR)
  219. {
  220. if (ctx->subjectName != NULL)
  221. return IS_NULL_DN(ctx->subjectName) ? NULL : ctx->subjectName;
  222. if (ref_subj != NULL && (ctx->p10CSR != NULL || for_KUR || !HAS_SAN(ctx)))
  223. /*
  224. * For KUR, copy subject from the reference.
  225. * For IR or CR, do the same only if there is no subjectAltName.
  226. */
  227. return ref_subj;
  228. return NULL;
  229. }
  230. OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid)
  231. {
  232. OSSL_CRMF_MSG *crm = NULL;
  233. X509 *refcert = ctx->oldCert != NULL ? ctx->oldCert : ctx->cert;
  234. /* refcert defaults to current client cert */
  235. EVP_PKEY *rkey = OSSL_CMP_CTX_get0_newPkey(ctx, 0);
  236. STACK_OF(GENERAL_NAME) *default_sans = NULL;
  237. const X509_NAME *ref_subj =
  238. ctx->p10CSR != NULL ? X509_REQ_get_subject_name(ctx->p10CSR) :
  239. refcert != NULL ? X509_get_subject_name(refcert) : NULL;
  240. const X509_NAME *subject = determine_subj(ctx, ref_subj, for_KUR);
  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. ASN1_OCTET_STRING *certHash = NULL;
  685. OSSL_CMP_PKISI *sinfo;
  686. if (!ossl_assert(ctx != NULL && ctx->newCert != NULL))
  687. return NULL;
  688. if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
  689. ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE);
  690. return NULL;
  691. }
  692. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL)
  693. goto err;
  694. if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL)
  695. goto err;
  696. /* consume certStatus into msg right away so it gets deallocated with msg */
  697. if (!sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus))
  698. goto err;
  699. /* set the ID of the certReq */
  700. if (!ASN1_INTEGER_set(certStatus->certReqId, OSSL_CMP_CERTREQID))
  701. goto err;
  702. /*
  703. * The hash of the certificate, using the same hash algorithm
  704. * as is used to create and verify the certificate signature.
  705. * If not available, a default hash algorithm is used.
  706. */
  707. if ((certHash = X509_digest_sig(ctx->newCert, NULL, NULL)) == NULL)
  708. goto err;
  709. if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash))
  710. goto err;
  711. certHash = NULL;
  712. /*
  713. * For any particular CertStatus, omission of the statusInfo field
  714. * indicates ACCEPTANCE of the specified certificate. Alternatively,
  715. * explicit status details (with respect to acceptance or rejection) MAY
  716. * be provided in the statusInfo field, perhaps for auditing purposes at
  717. * the CA/RA.
  718. */
  719. sinfo = fail_info != 0 ?
  720. OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) :
  721. OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text);
  722. if (sinfo == NULL)
  723. goto err;
  724. certStatus->statusInfo = sinfo;
  725. if (!ossl_cmp_msg_protect(ctx, msg))
  726. goto err;
  727. return msg;
  728. err:
  729. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF);
  730. OSSL_CMP_MSG_free(msg);
  731. ASN1_OCTET_STRING_free(certHash);
  732. return NULL;
  733. }
  734. OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid)
  735. {
  736. OSSL_CMP_MSG *msg = NULL;
  737. OSSL_CMP_POLLREQ *preq = NULL;
  738. if (!ossl_assert(ctx != NULL))
  739. return NULL;
  740. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL)
  741. goto err;
  742. if ((preq = OSSL_CMP_POLLREQ_new()) == NULL
  743. || !ASN1_INTEGER_set(preq->certReqId, crid)
  744. || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq))
  745. goto err;
  746. preq = NULL;
  747. if (!ossl_cmp_msg_protect(ctx, msg))
  748. goto err;
  749. return msg;
  750. err:
  751. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ);
  752. OSSL_CMP_POLLREQ_free(preq);
  753. OSSL_CMP_MSG_free(msg);
  754. return NULL;
  755. }
  756. OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid,
  757. int64_t poll_after)
  758. {
  759. OSSL_CMP_MSG *msg;
  760. OSSL_CMP_POLLREP *prep;
  761. if (!ossl_assert(ctx != NULL))
  762. return NULL;
  763. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL)
  764. goto err;
  765. if ((prep = OSSL_CMP_POLLREP_new()) == NULL)
  766. goto err;
  767. if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep))
  768. goto err;
  769. if (!ASN1_INTEGER_set(prep->certReqId, crid))
  770. goto err;
  771. if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after))
  772. goto err;
  773. if (!ossl_cmp_msg_protect(ctx, msg))
  774. goto err;
  775. return msg;
  776. err:
  777. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
  778. OSSL_CMP_MSG_free(msg);
  779. return NULL;
  780. }
  781. /*-
  782. * returns the status field of the RevRepContent with the given
  783. * request/sequence id inside a revocation response.
  784. * RevRepContent has the revocation statuses in same order as they were sent in
  785. * RevReqContent.
  786. * returns NULL on error
  787. */
  788. OSSL_CMP_PKISI *
  789. ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  790. {
  791. OSSL_CMP_PKISI *status;
  792. if (!ossl_assert(rrep != NULL))
  793. return NULL;
  794. if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL)
  795. return status;
  796. ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND);
  797. return NULL;
  798. }
  799. /*
  800. * returns the CertId field in the revCerts part of the RevRepContent
  801. * with the given request/sequence id inside a revocation response.
  802. * RevRepContent has the CertIds in same order as they were sent in
  803. * RevReqContent.
  804. * returns NULL on error
  805. */
  806. OSSL_CRMF_CERTID *
  807. ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  808. {
  809. OSSL_CRMF_CERTID *cid = NULL;
  810. if (!ossl_assert(rrep != NULL))
  811. return NULL;
  812. if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL)
  813. return cid;
  814. ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND);
  815. return NULL;
  816. }
  817. static int suitable_rid(const ASN1_INTEGER *certReqId, int rid)
  818. {
  819. int trid;
  820. if (rid == -1)
  821. return 1;
  822. trid = ossl_cmp_asn1_get_int(certReqId);
  823. if (trid == -1) {
  824. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
  825. return 0;
  826. }
  827. return rid == trid;
  828. }
  829. /*
  830. * returns a pointer to the PollResponse with the given CertReqId
  831. * (or the first one in case -1) inside a PollRepContent
  832. * returns NULL on error or if no suitable PollResponse available
  833. */
  834. OSSL_CMP_POLLREP *
  835. ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc,
  836. int rid)
  837. {
  838. OSSL_CMP_POLLREP *pollRep = NULL;
  839. int i;
  840. if (!ossl_assert(prc != NULL))
  841. return NULL;
  842. for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) {
  843. pollRep = sk_OSSL_CMP_POLLREP_value(prc, i);
  844. if (suitable_rid(pollRep->certReqId, rid))
  845. return pollRep;
  846. }
  847. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
  848. "expected certReqId = %d", rid);
  849. return NULL;
  850. }
  851. /*
  852. * returns a pointer to the CertResponse with the given CertReqId
  853. * (or the first one in case -1) inside a CertRepMessage
  854. * returns NULL on error or if no suitable CertResponse available
  855. */
  856. OSSL_CMP_CERTRESPONSE *
  857. ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm,
  858. int rid)
  859. {
  860. OSSL_CMP_CERTRESPONSE *crep = NULL;
  861. int i;
  862. if (!ossl_assert(crm != NULL && crm->response != NULL))
  863. return NULL;
  864. for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) {
  865. crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i);
  866. if (suitable_rid(crep->certReqId, rid))
  867. return crep;
  868. }
  869. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
  870. "expected certReqId = %d", rid);
  871. return NULL;
  872. }
  873. /*-
  874. * Retrieve the newly enrolled certificate from the given certResponse crep.
  875. * In case of indirect POPO uses the libctx and propq from ctx and private key.
  876. * Returns a pointer to a copy of the found certificate, or NULL if not found.
  877. */
  878. X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CERTRESPONSE *crep,
  879. const OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
  880. {
  881. OSSL_CMP_CERTORENCCERT *coec;
  882. X509 *crt = NULL;
  883. if (!ossl_assert(crep != NULL && ctx != NULL))
  884. return NULL;
  885. if (crep->certifiedKeyPair
  886. && (coec = crep->certifiedKeyPair->certOrEncCert) != NULL) {
  887. switch (coec->type) {
  888. case OSSL_CMP_CERTORENCCERT_CERTIFICATE:
  889. crt = X509_dup(coec->value.certificate);
  890. break;
  891. case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT:
  892. /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */
  893. if (pkey == NULL) {
  894. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY);
  895. return NULL;
  896. }
  897. crt =
  898. OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(coec->value.encryptedCert,
  899. ctx->libctx, ctx->propq,
  900. pkey);
  901. break;
  902. default:
  903. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE);
  904. return NULL;
  905. }
  906. }
  907. if (crt == NULL)
  908. ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
  909. else
  910. (void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq);
  911. return crt;
  912. }
  913. int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  914. {
  915. if (ctx == NULL || msg == NULL) {
  916. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  917. return 0;
  918. }
  919. if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header))
  920. return 0;
  921. return msg->header->protectionAlg == NULL
  922. || ossl_cmp_msg_protect(ctx, msg);
  923. }
  924. OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx,
  925. const char *propq)
  926. {
  927. OSSL_CMP_MSG *msg;
  928. BIO *bio = NULL;
  929. if (file == NULL) {
  930. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  931. return NULL;
  932. }
  933. msg = OSSL_CMP_MSG_new(libctx, propq);
  934. if (msg == NULL) {
  935. ERR_raise(ERR_LIB_CMP, ERR_R_MALLOC_FAILURE);
  936. return NULL;
  937. }
  938. if ((bio = BIO_new_file(file, "rb")) == NULL
  939. || d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) {
  940. OSSL_CMP_MSG_free(msg);
  941. msg = NULL;
  942. }
  943. BIO_free(bio);
  944. return msg;
  945. }
  946. int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg)
  947. {
  948. BIO *bio;
  949. int res;
  950. if (file == NULL || msg == NULL) {
  951. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  952. return -1;
  953. }
  954. bio = BIO_new_file(file, "wb");
  955. if (bio == NULL)
  956. return -2;
  957. res = i2d_OSSL_CMP_MSG_bio(bio, msg);
  958. BIO_free(bio);
  959. return res;
  960. }
  961. OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in,
  962. long len)
  963. {
  964. OSSL_LIB_CTX *libctx = NULL;
  965. const char *propq = NULL;
  966. if (msg != NULL && *msg != NULL) {
  967. libctx = (*msg)->libctx;
  968. propq = (*msg)->propq;
  969. }
  970. return (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len,
  971. ASN1_ITEM_rptr(OSSL_CMP_MSG),
  972. libctx, propq);
  973. }
  974. int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out)
  975. {
  976. return ASN1_item_i2d((const ASN1_VALUE *)msg, out,
  977. ASN1_ITEM_rptr(OSSL_CMP_MSG));
  978. }
  979. OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg)
  980. {
  981. OSSL_LIB_CTX *libctx = NULL;
  982. const char *propq = NULL;
  983. if (msg != NULL && *msg != NULL) {
  984. libctx = (*msg)->libctx;
  985. propq = (*msg)->propq;
  986. }
  987. return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx,
  988. propq);
  989. }
  990. int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg)
  991. {
  992. return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg);
  993. }