cmp_msg.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. const X509_NAME *issuer = NULL;
  450. const X509_NAME *subject = NULL;
  451. const ASN1_INTEGER *serialNumber = NULL;
  452. EVP_PKEY *pubkey = NULL;
  453. OSSL_CMP_REVDETAILS *rd;
  454. int ret;
  455. if (!ossl_assert(ctx != NULL
  456. && (ctx->oldCert != NULL || ctx->p10CSR != NULL
  457. || (ctx->serialNumber != NULL && ctx->issuer != NULL))))
  458. return NULL;
  459. if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL)
  460. goto err;
  461. if (ctx->serialNumber != NULL && ctx->issuer != NULL) {
  462. issuer = ctx->issuer;
  463. serialNumber = ctx->serialNumber;
  464. } else if (ctx->oldCert != NULL) {
  465. issuer = X509_get_issuer_name(ctx->oldCert);
  466. serialNumber = X509_get0_serialNumber(ctx->oldCert);
  467. } else if (ctx->p10CSR != NULL) {
  468. pubkey = X509_REQ_get0_pubkey(ctx->p10CSR);
  469. subject = X509_REQ_get_subject_name(ctx->p10CSR);
  470. }
  471. else {
  472. goto err;
  473. }
  474. /* Fill the template from the contents of the certificate to be revoked */
  475. ret = OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails, pubkey, subject,
  476. issuer, serialNumber);
  477. if (!ret)
  478. goto err;
  479. /* revocation reason code is optional */
  480. if (ctx->revocationReason != CRL_REASON_NONE
  481. && !add_crl_reason_extension(&rd->crlEntryDetails,
  482. ctx->revocationReason))
  483. goto err;
  484. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL)
  485. goto err;
  486. if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd))
  487. goto err;
  488. rd = NULL;
  489. /* Revocation Passphrase according to section 5.3.19.9 could be set here */
  490. if (!ossl_cmp_msg_protect(ctx, msg))
  491. goto err;
  492. return msg;
  493. err:
  494. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
  495. OSSL_CMP_MSG_free(msg);
  496. OSSL_CMP_REVDETAILS_free(rd);
  497. return NULL;
  498. }
  499. OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
  500. const OSSL_CRMF_CERTID *cid, int unprotectedErrors)
  501. {
  502. OSSL_CMP_REVREPCONTENT *rep = NULL;
  503. OSSL_CMP_PKISI *si1 = NULL;
  504. OSSL_CRMF_CERTID *cid_copy = NULL;
  505. OSSL_CMP_MSG *msg = NULL;
  506. if (!ossl_assert(ctx != NULL && si != NULL))
  507. return NULL;
  508. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL)
  509. goto err;
  510. rep = msg->body->value.rp;
  511. if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL)
  512. goto err;
  513. if (!sk_OSSL_CMP_PKISI_push(rep->status, si1)) {
  514. OSSL_CMP_PKISI_free(si1);
  515. goto err;
  516. }
  517. if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL)
  518. goto err;
  519. if (cid != NULL) {
  520. if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL)
  521. goto err;
  522. if (!sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) {
  523. OSSL_CRMF_CERTID_free(cid_copy);
  524. goto err;
  525. }
  526. }
  527. if (!unprotectedErrors
  528. || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
  529. if (!ossl_cmp_msg_protect(ctx, msg))
  530. goto err;
  531. return msg;
  532. err:
  533. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RP);
  534. OSSL_CMP_MSG_free(msg);
  535. return NULL;
  536. }
  537. OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx)
  538. {
  539. OSSL_CMP_MSG *msg;
  540. if (!ossl_assert(ctx != NULL))
  541. return NULL;
  542. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL)
  543. goto err;
  544. if (ossl_cmp_msg_protect(ctx, msg))
  545. return msg;
  546. err:
  547. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
  548. OSSL_CMP_MSG_free(msg);
  549. return NULL;
  550. }
  551. int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav)
  552. {
  553. int bodytype;
  554. if (!ossl_assert(msg != NULL && itav != NULL))
  555. return 0;
  556. bodytype = OSSL_CMP_MSG_get_bodytype(msg);
  557. if (bodytype != OSSL_CMP_PKIBODY_GENM
  558. && bodytype != OSSL_CMP_PKIBODY_GENP) {
  559. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  560. return 0;
  561. }
  562. /* value.genp has the same structure, so this works for genp as well */
  563. return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav);
  564. }
  565. int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg,
  566. const STACK_OF(OSSL_CMP_ITAV) *itavs)
  567. {
  568. int i;
  569. OSSL_CMP_ITAV *itav = NULL;
  570. if (!ossl_assert(msg != NULL))
  571. return 0;
  572. for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
  573. itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
  574. if (itav == NULL
  575. || !ossl_cmp_msg_gen_push0_ITAV(msg, itav)) {
  576. OSSL_CMP_ITAV_free(itav);
  577. return 0;
  578. }
  579. }
  580. return 1;
  581. }
  582. /*
  583. * Creates a new General Message/Response with a copy of the given itav stack
  584. * returns a pointer to the PKIMessage on success, NULL on error
  585. */
  586. static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx,
  587. const STACK_OF(OSSL_CMP_ITAV) *itavs,
  588. int body_type, int err_code)
  589. {
  590. OSSL_CMP_MSG *msg = NULL;
  591. if (!ossl_assert(ctx != NULL))
  592. return NULL;
  593. if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL)
  594. return NULL;
  595. if (itavs != NULL && !ossl_cmp_msg_gen_push1_ITAVs(msg, itavs))
  596. goto err;
  597. if (!ossl_cmp_msg_protect(ctx, msg))
  598. goto err;
  599. return msg;
  600. err:
  601. ERR_raise(ERR_LIB_CMP, err_code);
  602. OSSL_CMP_MSG_free(msg);
  603. return NULL;
  604. }
  605. OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx)
  606. {
  607. return gen_new(ctx, ctx->genm_ITAVs,
  608. OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM);
  609. }
  610. OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx,
  611. const STACK_OF(OSSL_CMP_ITAV) *itavs)
  612. {
  613. return gen_new(ctx, itavs,
  614. OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP);
  615. }
  616. OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
  617. int64_t errorCode, const char *details,
  618. int unprotected)
  619. {
  620. OSSL_CMP_MSG *msg = NULL;
  621. const char *lib = NULL, *reason = NULL;
  622. OSSL_CMP_PKIFREETEXT *ft;
  623. if (!ossl_assert(ctx != NULL && si != NULL))
  624. return NULL;
  625. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL)
  626. goto err;
  627. OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo);
  628. if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si))
  629. == NULL)
  630. goto err;
  631. if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL)
  632. goto err;
  633. if (!ASN1_INTEGER_set_int64(msg->body->value.error->errorCode, errorCode))
  634. goto err;
  635. if (errorCode > 0
  636. && (uint64_t)errorCode < ((uint64_t)ERR_SYSTEM_FLAG << 1)) {
  637. lib = ERR_lib_error_string((unsigned long)errorCode);
  638. reason = ERR_reason_error_string((unsigned long)errorCode);
  639. }
  640. if (lib != NULL || reason != NULL || details != NULL) {
  641. if ((ft = sk_ASN1_UTF8STRING_new_null()) == NULL)
  642. goto err;
  643. msg->body->value.error->errorDetails = ft;
  644. if (lib != NULL && *lib != '\0'
  645. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, lib, -1))
  646. goto err;
  647. if (reason != NULL && *reason != '\0'
  648. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, reason, -1))
  649. goto err;
  650. if (details != NULL
  651. && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, details, -1))
  652. goto err;
  653. }
  654. if (!unprotected && !ossl_cmp_msg_protect(ctx, msg))
  655. goto err;
  656. return msg;
  657. err:
  658. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_ERROR);
  659. OSSL_CMP_MSG_free(msg);
  660. return NULL;
  661. }
  662. /*
  663. * Set the certHash field of a OSSL_CMP_CERTSTATUS structure.
  664. * This is used in the certConf message, for example,
  665. * to confirm that the certificate was received successfully.
  666. */
  667. int ossl_cmp_certstatus_set0_certHash(OSSL_CMP_CERTSTATUS *certStatus,
  668. ASN1_OCTET_STRING *hash)
  669. {
  670. if (!ossl_assert(certStatus != NULL))
  671. return 0;
  672. ASN1_OCTET_STRING_free(certStatus->certHash);
  673. certStatus->certHash = hash;
  674. return 1;
  675. }
  676. OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int certReqId,
  677. int fail_info, const char *text)
  678. {
  679. OSSL_CMP_MSG *msg = NULL;
  680. OSSL_CMP_CERTSTATUS *certStatus = NULL;
  681. EVP_MD *md;
  682. int is_fallback;
  683. ASN1_OCTET_STRING *certHash = NULL;
  684. OSSL_CMP_PKISI *sinfo;
  685. if (!ossl_assert(ctx != NULL && ctx->newCert != NULL
  686. && (certReqId == OSSL_CMP_CERTREQID
  687. || certReqId == OSSL_CMP_CERTREQID_NONE)))
  688. return NULL;
  689. if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
  690. ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE);
  691. return NULL;
  692. }
  693. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL)
  694. goto err;
  695. if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL)
  696. goto err;
  697. /* consume certStatus into msg right away so it gets deallocated with msg */
  698. if (sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus) < 1) {
  699. OSSL_CMP_CERTSTATUS_free(certStatus);
  700. goto err;
  701. }
  702. /* set the ID of the certReq */
  703. if (!ASN1_INTEGER_set(certStatus->certReqId, certReqId))
  704. goto err;
  705. certStatus->hashAlg = NULL;
  706. /*
  707. * The hash of the certificate, using the same hash algorithm
  708. * as is used to create and verify the certificate signature.
  709. * If not available, a fallback hash algorithm is used.
  710. */
  711. if ((certHash = X509_digest_sig(ctx->newCert, &md, &is_fallback)) == NULL)
  712. goto err;
  713. if (is_fallback) {
  714. if (!ossl_cmp_hdr_set_pvno(msg->header, OSSL_CMP_PVNO_3))
  715. goto err;
  716. if ((certStatus->hashAlg = X509_ALGOR_new()) == NULL)
  717. goto err;
  718. X509_ALGOR_set_md(certStatus->hashAlg, md);
  719. }
  720. EVP_MD_free(md);
  721. if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash))
  722. goto err;
  723. certHash = NULL;
  724. /*
  725. * For any particular CertStatus, omission of the statusInfo field
  726. * indicates ACCEPTANCE of the specified certificate. Alternatively,
  727. * explicit status details (with respect to acceptance or rejection) MAY
  728. * be provided in the statusInfo field, perhaps for auditing purposes at
  729. * the CA/RA.
  730. */
  731. sinfo = fail_info != 0 ?
  732. OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) :
  733. OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text);
  734. if (sinfo == NULL)
  735. goto err;
  736. certStatus->statusInfo = sinfo;
  737. if (!ossl_cmp_msg_protect(ctx, msg))
  738. goto err;
  739. return msg;
  740. err:
  741. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF);
  742. OSSL_CMP_MSG_free(msg);
  743. ASN1_OCTET_STRING_free(certHash);
  744. return NULL;
  745. }
  746. OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid)
  747. {
  748. OSSL_CMP_MSG *msg = NULL;
  749. OSSL_CMP_POLLREQ *preq = NULL;
  750. if (!ossl_assert(ctx != NULL))
  751. return NULL;
  752. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL)
  753. goto err;
  754. if ((preq = OSSL_CMP_POLLREQ_new()) == NULL
  755. || !ASN1_INTEGER_set(preq->certReqId, crid)
  756. || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq))
  757. goto err;
  758. preq = NULL;
  759. if (!ossl_cmp_msg_protect(ctx, msg))
  760. goto err;
  761. return msg;
  762. err:
  763. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ);
  764. OSSL_CMP_POLLREQ_free(preq);
  765. OSSL_CMP_MSG_free(msg);
  766. return NULL;
  767. }
  768. OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid,
  769. int64_t poll_after)
  770. {
  771. OSSL_CMP_MSG *msg;
  772. OSSL_CMP_POLLREP *prep;
  773. if (!ossl_assert(ctx != NULL))
  774. return NULL;
  775. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL)
  776. goto err;
  777. if ((prep = OSSL_CMP_POLLREP_new()) == NULL)
  778. goto err;
  779. if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep))
  780. goto err;
  781. if (!ASN1_INTEGER_set(prep->certReqId, crid))
  782. goto err;
  783. if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after))
  784. goto err;
  785. if (!ossl_cmp_msg_protect(ctx, msg))
  786. goto err;
  787. return msg;
  788. err:
  789. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
  790. OSSL_CMP_MSG_free(msg);
  791. return NULL;
  792. }
  793. /*-
  794. * returns the status field of the RevRepContent with the given
  795. * request/sequence id inside a revocation response.
  796. * RevRepContent has the revocation statuses in same order as they were sent in
  797. * RevReqContent.
  798. * returns NULL on error
  799. */
  800. OSSL_CMP_PKISI *
  801. ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  802. {
  803. OSSL_CMP_PKISI *status;
  804. if (!ossl_assert(rrep != NULL))
  805. return NULL;
  806. if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL)
  807. return status;
  808. ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND);
  809. return NULL;
  810. }
  811. /*
  812. * returns the CertId field in the revCerts part of the RevRepContent
  813. * with the given request/sequence id inside a revocation response.
  814. * RevRepContent has the CertIds in same order as they were sent in
  815. * RevReqContent.
  816. * returns NULL on error
  817. */
  818. OSSL_CRMF_CERTID *
  819. ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  820. {
  821. OSSL_CRMF_CERTID *cid = NULL;
  822. if (!ossl_assert(rrep != NULL))
  823. return NULL;
  824. if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL)
  825. return cid;
  826. ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND);
  827. return NULL;
  828. }
  829. static int suitable_rid(const ASN1_INTEGER *certReqId, int rid)
  830. {
  831. int trid;
  832. if (rid == OSSL_CMP_CERTREQID_NONE)
  833. return 1;
  834. trid = ossl_cmp_asn1_get_int(certReqId);
  835. if (trid == OSSL_CMP_CERTREQID_NONE) {
  836. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
  837. return 0;
  838. }
  839. return rid == trid;
  840. }
  841. /*
  842. * returns a pointer to the PollResponse with the given CertReqId
  843. * (or the first one in case -1) inside a PollRepContent
  844. * returns NULL on error or if no suitable PollResponse available
  845. */
  846. OSSL_CMP_POLLREP *
  847. ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc,
  848. int rid)
  849. {
  850. OSSL_CMP_POLLREP *pollRep = NULL;
  851. int i;
  852. if (!ossl_assert(prc != NULL))
  853. return NULL;
  854. for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) {
  855. pollRep = sk_OSSL_CMP_POLLREP_value(prc, i);
  856. if (suitable_rid(pollRep->certReqId, rid))
  857. return pollRep;
  858. }
  859. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
  860. "expected certReqId = %d", rid);
  861. return NULL;
  862. }
  863. /*
  864. * returns a pointer to the CertResponse with the given CertReqId
  865. * (or the first one in case -1) inside a CertRepMessage
  866. * returns NULL on error or if no suitable CertResponse available
  867. */
  868. OSSL_CMP_CERTRESPONSE *
  869. ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm,
  870. int rid)
  871. {
  872. OSSL_CMP_CERTRESPONSE *crep = NULL;
  873. int i;
  874. if (!ossl_assert(crm != NULL && crm->response != NULL))
  875. return NULL;
  876. for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) {
  877. crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i);
  878. if (suitable_rid(crep->certReqId, rid))
  879. return crep;
  880. }
  881. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
  882. "expected certReqId = %d", rid);
  883. return NULL;
  884. }
  885. /*-
  886. * Retrieve the newly enrolled certificate from the given certResponse crep.
  887. * Uses libctx and propq from ctx, in case of indirect POPO also private key.
  888. * Returns a pointer to a copy of the found certificate, or NULL if not found.
  889. */
  890. X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CTX *ctx,
  891. const OSSL_CMP_CERTRESPONSE *crep)
  892. {
  893. OSSL_CMP_CERTORENCCERT *coec;
  894. X509 *crt = NULL;
  895. EVP_PKEY *pkey;
  896. if (!ossl_assert(crep != NULL && ctx != NULL))
  897. return NULL;
  898. if (crep->certifiedKeyPair
  899. && (coec = crep->certifiedKeyPair->certOrEncCert) != NULL) {
  900. switch (coec->type) {
  901. case OSSL_CMP_CERTORENCCERT_CERTIFICATE:
  902. crt = X509_dup(coec->value.certificate);
  903. break;
  904. case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT:
  905. /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */
  906. pkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
  907. /* pkey is ctx->newPkey (if private, else NULL) or ctx->pkey */
  908. if (pkey == NULL) {
  909. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY);
  910. return NULL;
  911. }
  912. crt =
  913. OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(coec->value.encryptedCert,
  914. ctx->libctx, ctx->propq,
  915. pkey);
  916. break;
  917. default:
  918. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE);
  919. return NULL;
  920. }
  921. }
  922. if (crt == NULL)
  923. ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
  924. else
  925. (void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq);
  926. return crt;
  927. }
  928. int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  929. {
  930. if (ctx == NULL || msg == NULL) {
  931. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  932. return 0;
  933. }
  934. if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header))
  935. return 0;
  936. return msg->header->protectionAlg == NULL
  937. || ossl_cmp_msg_protect(ctx, msg);
  938. }
  939. int OSSL_CMP_MSG_update_recipNonce(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  940. {
  941. if (ctx == NULL || msg == NULL || msg->header == NULL) {
  942. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  943. return 0;
  944. }
  945. if (ctx->recipNonce == NULL) /* nothing to do for 1st msg in transaction */
  946. return 1;
  947. if (!ossl_cmp_asn1_octet_string_set1(&msg->header->recipNonce,
  948. ctx->recipNonce))
  949. return 0;
  950. return msg->header->protectionAlg == NULL || ossl_cmp_msg_protect(ctx, msg);
  951. }
  952. OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx,
  953. const char *propq)
  954. {
  955. OSSL_CMP_MSG *msg;
  956. BIO *bio = NULL;
  957. if (file == NULL) {
  958. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  959. return NULL;
  960. }
  961. msg = OSSL_CMP_MSG_new(libctx, propq);
  962. if (msg == NULL) {
  963. ERR_raise(ERR_LIB_CMP, ERR_R_CMP_LIB);
  964. return NULL;
  965. }
  966. if ((bio = BIO_new_file(file, "rb")) == NULL
  967. || d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) {
  968. OSSL_CMP_MSG_free(msg);
  969. msg = NULL;
  970. }
  971. BIO_free(bio);
  972. return msg;
  973. }
  974. int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg)
  975. {
  976. BIO *bio;
  977. int res;
  978. if (file == NULL || msg == NULL) {
  979. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  980. return -1;
  981. }
  982. bio = BIO_new_file(file, "wb");
  983. if (bio == NULL)
  984. return -2;
  985. res = i2d_OSSL_CMP_MSG_bio(bio, msg);
  986. BIO_free(bio);
  987. return res;
  988. }
  989. OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in,
  990. long len)
  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 (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len,
  999. ASN1_ITEM_rptr(OSSL_CMP_MSG),
  1000. libctx, propq);
  1001. }
  1002. int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out)
  1003. {
  1004. return ASN1_item_i2d((const ASN1_VALUE *)msg, out,
  1005. ASN1_ITEM_rptr(OSSL_CMP_MSG));
  1006. }
  1007. OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg)
  1008. {
  1009. OSSL_LIB_CTX *libctx = NULL;
  1010. const char *propq = NULL;
  1011. if (msg != NULL && *msg != NULL) {
  1012. libctx = (*msg)->libctx;
  1013. propq = (*msg)->propq;
  1014. }
  1015. return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx,
  1016. propq);
  1017. }
  1018. int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg)
  1019. {
  1020. return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg);
  1021. }