cmp_msg.c 36 KB

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