cmp_msg.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * Copyright 2007-2019 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_PKIHEADER *OSSL_CMP_MSG_get0_header(const OSSL_CMP_MSG *msg)
  20. {
  21. if (msg == NULL) {
  22. CMPerr(0, CMP_R_NULL_ARGUMENT);
  23. return NULL;
  24. }
  25. return msg->header;
  26. }
  27. const char *ossl_cmp_bodytype_to_string(int type)
  28. {
  29. static const char *type_names[] = {
  30. "IR", "IP", "CR", "CP", "P10CR",
  31. "POPDECC", "POPDECR", "KUR", "KUP",
  32. "KRR", "KRP", "RR", "RP", "CCR", "CCP",
  33. "CKUANN", "CANN", "RANN", "CRLANN", "PKICONF", "NESTED",
  34. "GENM", "GENP", "ERROR", "CERTCONF", "POLLREQ", "POLLREP",
  35. };
  36. if (type < 0 || type > OSSL_CMP_PKIBODY_TYPE_MAX)
  37. return "illegal body type";
  38. return type_names[type];
  39. }
  40. int ossl_cmp_msg_set_bodytype(OSSL_CMP_MSG *msg, int type)
  41. {
  42. if (!ossl_assert(msg != NULL && msg->body != NULL))
  43. return 0;
  44. msg->body->type = type;
  45. return 1;
  46. }
  47. int ossl_cmp_msg_get_bodytype(const OSSL_CMP_MSG *msg)
  48. {
  49. if (!ossl_assert(msg != NULL && msg->body != NULL))
  50. return -1;
  51. return msg->body->type;
  52. }
  53. /* Add an extension to the referenced extension stack, which may be NULL */
  54. static int add1_extension(X509_EXTENSIONS **pexts, int nid, int crit, void *ex)
  55. {
  56. X509_EXTENSION *ext;
  57. int res;
  58. if (!ossl_assert(pexts != NULL)) /* pointer to var must not be NULL */
  59. return 0;
  60. if ((ext = X509V3_EXT_i2d(nid, crit, ex)) == NULL)
  61. return 0;
  62. res = X509v3_add_ext(pexts, ext, 0) != NULL;
  63. X509_EXTENSION_free(ext);
  64. return res;
  65. }
  66. /* Add a CRL revocation reason code to extension stack, which may be NULL */
  67. static int add_crl_reason_extension(X509_EXTENSIONS **pexts, int reason_code)
  68. {
  69. ASN1_ENUMERATED *val = ASN1_ENUMERATED_new();
  70. int res = 0;
  71. if (val != NULL && ASN1_ENUMERATED_set(val, reason_code))
  72. res = add1_extension(pexts, NID_crl_reason, 0 /* non-critical */, val);
  73. ASN1_ENUMERATED_free(val);
  74. return res;
  75. }
  76. OSSL_CMP_MSG *ossl_cmp_msg_create(OSSL_CMP_CTX *ctx, int bodytype)
  77. {
  78. OSSL_CMP_MSG *msg = NULL;
  79. if (!ossl_assert(ctx != NULL))
  80. return NULL;
  81. if ((msg = OSSL_CMP_MSG_new()) == NULL)
  82. return NULL;
  83. if (!ossl_cmp_hdr_init(ctx, msg->header)
  84. || !ossl_cmp_msg_set_bodytype(msg, bodytype))
  85. goto err;
  86. if (ctx->geninfo_ITAVs != NULL
  87. && !ossl_cmp_hdr_generalInfo_push1_items(msg->header,
  88. ctx->geninfo_ITAVs))
  89. goto err;
  90. switch (bodytype) {
  91. case OSSL_CMP_PKIBODY_IR:
  92. case OSSL_CMP_PKIBODY_CR:
  93. case OSSL_CMP_PKIBODY_KUR:
  94. if ((msg->body->value.ir = OSSL_CRMF_MSGS_new()) == NULL)
  95. goto err;
  96. return msg;
  97. case OSSL_CMP_PKIBODY_P10CR:
  98. if (ctx->p10CSR == NULL) {
  99. CMPerr(0, CMP_R_ERROR_CREATING_P10CR);
  100. goto err;
  101. }
  102. if ((msg->body->value.p10cr = X509_REQ_dup(ctx->p10CSR)) == NULL)
  103. goto err;
  104. return msg;
  105. case OSSL_CMP_PKIBODY_IP:
  106. case OSSL_CMP_PKIBODY_CP:
  107. case OSSL_CMP_PKIBODY_KUP:
  108. if ((msg->body->value.ip = OSSL_CMP_CERTREPMESSAGE_new()) == NULL)
  109. goto err;
  110. return msg;
  111. case OSSL_CMP_PKIBODY_RR:
  112. if ((msg->body->value.rr = sk_OSSL_CMP_REVDETAILS_new_null()) == NULL)
  113. goto err;
  114. return msg;
  115. case OSSL_CMP_PKIBODY_RP:
  116. if ((msg->body->value.rp = OSSL_CMP_REVREPCONTENT_new()) == NULL)
  117. goto err;
  118. return msg;
  119. case OSSL_CMP_PKIBODY_CERTCONF:
  120. if ((msg->body->value.certConf =
  121. sk_OSSL_CMP_CERTSTATUS_new_null()) == NULL)
  122. goto err;
  123. return msg;
  124. case OSSL_CMP_PKIBODY_PKICONF:
  125. if ((msg->body->value.pkiconf = ASN1_TYPE_new()) == NULL)
  126. goto err;
  127. ASN1_TYPE_set(msg->body->value.pkiconf, V_ASN1_NULL, NULL);
  128. return msg;
  129. case OSSL_CMP_PKIBODY_POLLREQ:
  130. if ((msg->body->value.pollReq = sk_OSSL_CMP_POLLREQ_new_null()) == NULL)
  131. goto err;
  132. return msg;
  133. case OSSL_CMP_PKIBODY_POLLREP:
  134. if ((msg->body->value.pollRep = sk_OSSL_CMP_POLLREP_new_null()) == NULL)
  135. goto err;
  136. return msg;
  137. case OSSL_CMP_PKIBODY_GENM:
  138. case OSSL_CMP_PKIBODY_GENP:
  139. if ((msg->body->value.genm = sk_OSSL_CMP_ITAV_new_null()) == NULL)
  140. goto err;
  141. return msg;
  142. case OSSL_CMP_PKIBODY_ERROR:
  143. if ((msg->body->value.error = OSSL_CMP_ERRORMSGCONTENT_new()) == NULL)
  144. goto err;
  145. return msg;
  146. default:
  147. CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
  148. goto err;
  149. }
  150. err:
  151. OSSL_CMP_MSG_free(msg);
  152. return NULL;
  153. }
  154. #define HAS_SAN(ctx) \
  155. (sk_GENERAL_NAME_num((ctx)->subjectAltNames) > 0 \
  156. || OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1)
  157. static X509_NAME *determine_subj(OSSL_CMP_CTX *ctx, X509 *refcert,
  158. int bodytype)
  159. {
  160. if (ctx->subjectName != NULL)
  161. return ctx->subjectName;
  162. if (refcert != NULL
  163. && (bodytype == OSSL_CMP_PKIBODY_KUR || !HAS_SAN(ctx)))
  164. /*
  165. * For KUR, copy subjectName from reference certificate.
  166. * For IR or CR, do the same only if there is no subjectAltName.
  167. */
  168. return X509_get_subject_name(refcert);
  169. return NULL;
  170. }
  171. /*
  172. * Create CRMF certificate request message for IR/CR/KUR
  173. * returns a pointer to the OSSL_CRMF_MSG on success, NULL on error
  174. */
  175. static OSSL_CRMF_MSG *crm_new(OSSL_CMP_CTX *ctx, int bodytype,
  176. int rid, EVP_PKEY *rkey)
  177. {
  178. OSSL_CRMF_MSG *crm = NULL;
  179. X509 *refcert = ctx->oldCert != NULL ? ctx->oldCert : ctx->clCert;
  180. /* refcert defaults to current client cert */
  181. STACK_OF(GENERAL_NAME) *default_sans = NULL;
  182. X509_NAME *subject = determine_subj(ctx, refcert, bodytype);
  183. int crit = ctx->setSubjectAltNameCritical || subject == NULL;
  184. /* RFC5280: subjectAltName MUST be critical if subject is null */
  185. X509_EXTENSIONS *exts = NULL;
  186. if (rkey == NULL
  187. || (bodytype == OSSL_CMP_PKIBODY_KUR && refcert == NULL)) {
  188. CMPerr(0, CMP_R_INVALID_ARGS);
  189. return NULL;
  190. }
  191. if ((crm = OSSL_CRMF_MSG_new()) == NULL)
  192. return NULL;
  193. if (!OSSL_CRMF_MSG_set_certReqId(crm, rid)
  194. /*
  195. * fill certTemplate, corresponding to CertificationRequestInfo
  196. * of PKCS#10. The rkey param cannot be NULL so far -
  197. * it could be NULL if centralized key creation was supported
  198. */
  199. || !OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_MSG_get0_tmpl(crm), rkey,
  200. subject, ctx->issuer,
  201. NULL/* serial */))
  202. goto err;
  203. if (ctx->days != 0) {
  204. time_t notBefore, notAfter;
  205. notBefore = time(NULL);
  206. notAfter = notBefore + 60 * 60 * 24 * ctx->days;
  207. if (!OSSL_CRMF_MSG_set_validity(crm, notBefore, notAfter))
  208. goto err;
  209. }
  210. /* extensions */
  211. if (refcert != NULL && !ctx->SubjectAltName_nodefault)
  212. default_sans = X509V3_get_d2i(X509_get0_extensions(refcert),
  213. NID_subject_alt_name, NULL, NULL);
  214. /* exts are copied from ctx to allow reuse */
  215. if (ctx->reqExtensions != NULL) {
  216. exts = sk_X509_EXTENSION_deep_copy(ctx->reqExtensions,
  217. X509_EXTENSION_dup,
  218. X509_EXTENSION_free);
  219. if (exts == NULL)
  220. goto err;
  221. }
  222. if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0
  223. && !add1_extension(&exts, NID_subject_alt_name,
  224. crit, ctx->subjectAltNames))
  225. goto err;
  226. if (!HAS_SAN(ctx) && default_sans != NULL
  227. && !add1_extension(&exts, NID_subject_alt_name, crit, default_sans))
  228. goto err;
  229. if (ctx->policies != NULL
  230. && !add1_extension(&exts, NID_certificate_policies,
  231. ctx->setPoliciesCritical, ctx->policies))
  232. goto err;
  233. if (!OSSL_CRMF_MSG_set0_extensions(crm, exts))
  234. goto err;
  235. exts = NULL;
  236. /* end fill certTemplate, now set any controls */
  237. /* for KUR, set OldCertId according to D.6 */
  238. if (bodytype == OSSL_CMP_PKIBODY_KUR) {
  239. OSSL_CRMF_CERTID *cid =
  240. OSSL_CRMF_CERTID_gen(X509_get_issuer_name(refcert),
  241. X509_get_serialNumber(refcert));
  242. int ret;
  243. if (cid == NULL)
  244. goto err;
  245. ret = OSSL_CRMF_MSG_set1_regCtrl_oldCertID(crm, cid);
  246. OSSL_CRMF_CERTID_free(cid);
  247. if (ret == 0)
  248. goto err;
  249. }
  250. goto end;
  251. err:
  252. OSSL_CRMF_MSG_free(crm);
  253. crm = NULL;
  254. end:
  255. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  256. sk_GENERAL_NAME_pop_free(default_sans, GENERAL_NAME_free);
  257. return crm;
  258. }
  259. OSSL_CMP_MSG *ossl_cmp_certReq_new(OSSL_CMP_CTX *ctx, int type, int err_code)
  260. {
  261. EVP_PKEY *rkey;
  262. EVP_PKEY *privkey;
  263. OSSL_CMP_MSG *msg;
  264. OSSL_CRMF_MSG *crm = NULL;
  265. if (!ossl_assert(ctx != NULL))
  266. return NULL;
  267. rkey = OSSL_CMP_CTX_get0_newPkey(ctx, 0);
  268. if (rkey == NULL)
  269. return NULL;
  270. privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
  271. if (type != OSSL_CMP_PKIBODY_IR && type != OSSL_CMP_PKIBODY_CR
  272. && type != OSSL_CMP_PKIBODY_KUR && type != OSSL_CMP_PKIBODY_P10CR) {
  273. CMPerr(0, CMP_R_INVALID_ARGS);
  274. return NULL;
  275. }
  276. if ((msg = ossl_cmp_msg_create(ctx, type)) == NULL)
  277. goto err;
  278. /* header */
  279. if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
  280. goto err;
  281. /* body */
  282. /* For P10CR the content has already been set in OSSL_CMP_MSG_create */
  283. if (type != OSSL_CMP_PKIBODY_P10CR) {
  284. if (ctx->popoMethod == OSSL_CRMF_POPO_SIGNATURE && privkey == NULL) {
  285. CMPerr(0, CMP_R_MISSING_PRIVATE_KEY);
  286. goto err;
  287. }
  288. if ((crm = crm_new(ctx, type, OSSL_CMP_CERTREQID, rkey)) == NULL
  289. || !OSSL_CRMF_MSG_create_popo(crm, privkey, ctx->digest,
  290. ctx->popoMethod)
  291. /* value.ir is same for cr and kur */
  292. || !sk_OSSL_CRMF_MSG_push(msg->body->value.ir, crm))
  293. goto err;
  294. crm = NULL;
  295. /* TODO: here optional 2nd certreqmsg could be pushed to the stack */
  296. }
  297. if (!ossl_cmp_msg_protect(ctx, msg))
  298. goto err;
  299. return msg;
  300. err:
  301. CMPerr(0, err_code);
  302. OSSL_CRMF_MSG_free(crm);
  303. OSSL_CMP_MSG_free(msg);
  304. return NULL;
  305. }
  306. OSSL_CMP_MSG *ossl_cmp_certRep_new(OSSL_CMP_CTX *ctx, int bodytype,
  307. int certReqId, OSSL_CMP_PKISI *si,
  308. X509 *cert, STACK_OF(X509) *chain,
  309. STACK_OF(X509) *caPubs, int encrypted,
  310. int unprotectedErrors)
  311. {
  312. OSSL_CMP_MSG *msg = NULL;
  313. OSSL_CMP_CERTREPMESSAGE *repMsg = NULL;
  314. OSSL_CMP_CERTRESPONSE *resp = NULL;
  315. int status = -1;
  316. if (!ossl_assert(ctx != NULL && si != NULL))
  317. return NULL;
  318. if ((msg = ossl_cmp_msg_create(ctx, bodytype)) == NULL)
  319. goto err;
  320. repMsg = msg->body->value.ip; /* value.ip is same for cp and kup */
  321. /* header */
  322. if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
  323. goto err;
  324. /* body */
  325. if ((resp = OSSL_CMP_CERTRESPONSE_new()) == NULL)
  326. goto err;
  327. OSSL_CMP_PKISI_free(resp->status);
  328. if ((resp->status = OSSL_CMP_PKISI_dup(si)) == NULL
  329. || !ASN1_INTEGER_set(resp->certReqId, certReqId))
  330. goto err;
  331. status = ossl_cmp_pkisi_get_pkistatus(resp->status);
  332. if (status != OSSL_CMP_PKISTATUS_rejection
  333. && status != OSSL_CMP_PKISTATUS_waiting && cert != NULL) {
  334. if (encrypted) {
  335. CMPerr(0, CMP_R_INVALID_ARGS);
  336. goto err;
  337. }
  338. if ((resp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new())
  339. == NULL)
  340. goto err;
  341. resp->certifiedKeyPair->certOrEncCert->type =
  342. OSSL_CMP_CERTORENCCERT_CERTIFICATE;
  343. if (!X509_up_ref(cert))
  344. goto err;
  345. resp->certifiedKeyPair->certOrEncCert->value.certificate = cert;
  346. }
  347. if (!sk_OSSL_CMP_CERTRESPONSE_push(repMsg->response, resp))
  348. goto err;
  349. resp = NULL;
  350. /* TODO: here optional 2nd certrep could be pushed to the stack */
  351. if (bodytype == OSSL_CMP_PKIBODY_IP && caPubs != NULL
  352. && (repMsg->caPubs = X509_chain_up_ref(caPubs)) == NULL)
  353. goto err;
  354. if (chain != NULL
  355. && !ossl_cmp_sk_X509_add1_certs(msg->extraCerts, chain, 0, 1, 0))
  356. goto err;
  357. if (!unprotectedErrors
  358. || ossl_cmp_pkisi_get_pkistatus(si) != OSSL_CMP_PKISTATUS_rejection)
  359. if (!ossl_cmp_msg_protect(ctx, msg))
  360. goto err;
  361. return msg;
  362. err:
  363. CMPerr(0, CMP_R_ERROR_CREATING_CERTREP);
  364. OSSL_CMP_CERTRESPONSE_free(resp);
  365. OSSL_CMP_MSG_free(msg);
  366. return NULL;
  367. }
  368. OSSL_CMP_MSG *ossl_cmp_rr_new(OSSL_CMP_CTX *ctx)
  369. {
  370. OSSL_CMP_MSG *msg = NULL;
  371. OSSL_CMP_REVDETAILS *rd;
  372. if (!ossl_assert(ctx != NULL && ctx->oldCert != NULL))
  373. return NULL;
  374. if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL)
  375. goto err;
  376. /* Fill the template from the contents of the certificate to be revoked */
  377. if (!OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails,
  378. NULL/* pubkey would be redundant */,
  379. NULL/* subject would be redundant */,
  380. X509_get_issuer_name(ctx->oldCert),
  381. X509_get_serialNumber(ctx->oldCert)))
  382. goto err;
  383. /* revocation reason code is optional */
  384. if (ctx->revocationReason != CRL_REASON_NONE
  385. && !add_crl_reason_extension(&rd->crlEntryDetails,
  386. ctx->revocationReason))
  387. goto err;
  388. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL)
  389. goto err;
  390. if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd))
  391. goto err;
  392. rd = NULL;
  393. /*
  394. * TODO: the Revocation Passphrase according to section 5.3.19.9 could be
  395. * set here if set in ctx
  396. */
  397. if (!ossl_cmp_msg_protect(ctx, msg))
  398. goto err;
  399. return msg;
  400. err:
  401. CMPerr(0, CMP_R_ERROR_CREATING_RR);
  402. OSSL_CMP_MSG_free(msg);
  403. OSSL_CMP_REVDETAILS_free(rd);
  404. return NULL;
  405. }
  406. OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, OSSL_CMP_PKISI *si,
  407. OSSL_CRMF_CERTID *cid, int unprot_err)
  408. {
  409. OSSL_CMP_REVREPCONTENT *rep = NULL;
  410. OSSL_CMP_PKISI *si1 = NULL;
  411. OSSL_CRMF_CERTID *cid_copy = NULL;
  412. OSSL_CMP_MSG *msg = NULL;
  413. if (!ossl_assert(ctx != NULL && si != NULL && cid != NULL))
  414. return NULL;
  415. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL)
  416. goto err;
  417. rep = msg->body->value.rp;
  418. if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL)
  419. goto err;
  420. if (!sk_OSSL_CMP_PKISI_push(rep->status, si1)) {
  421. OSSL_CMP_PKISI_free(si1);
  422. goto err;
  423. }
  424. if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL)
  425. goto err;
  426. if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL)
  427. goto err;
  428. if (!sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) {
  429. OSSL_CRMF_CERTID_free(cid_copy);
  430. goto err;
  431. }
  432. if (!unprot_err
  433. || ossl_cmp_pkisi_get_pkistatus(si) != OSSL_CMP_PKISTATUS_rejection)
  434. if (!ossl_cmp_msg_protect(ctx, msg))
  435. goto err;
  436. return msg;
  437. err:
  438. CMPerr(0, CMP_R_ERROR_CREATING_RP);
  439. OSSL_CMP_MSG_free(msg);
  440. return NULL;
  441. }
  442. OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx)
  443. {
  444. OSSL_CMP_MSG *msg;
  445. if (!ossl_assert(ctx != NULL))
  446. return NULL;
  447. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL)
  448. goto err;
  449. if (ossl_cmp_msg_protect(ctx, msg))
  450. return msg;
  451. err:
  452. CMPerr(0, CMP_R_ERROR_CREATING_PKICONF);
  453. OSSL_CMP_MSG_free(msg);
  454. return NULL;
  455. }
  456. int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav)
  457. {
  458. int bodytype;
  459. if (!ossl_assert(msg != NULL && itav != NULL))
  460. return 0;
  461. bodytype = ossl_cmp_msg_get_bodytype(msg);
  462. if (bodytype != OSSL_CMP_PKIBODY_GENM
  463. && bodytype != OSSL_CMP_PKIBODY_GENP) {
  464. CMPerr(0, CMP_R_INVALID_ARGS);
  465. return 0;
  466. }
  467. /* value.genp has the same structure, so this works for genp as well */
  468. return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav);
  469. }
  470. int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg,
  471. STACK_OF(OSSL_CMP_ITAV) *itavs)
  472. {
  473. int i;
  474. OSSL_CMP_ITAV *itav = NULL;
  475. if (!ossl_assert(msg != NULL))
  476. return 0;
  477. for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
  478. if ((itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs,i))) == NULL)
  479. return 0;
  480. if (!ossl_cmp_msg_gen_push0_ITAV(msg, itav)) {
  481. OSSL_CMP_ITAV_free(itav);
  482. return 0;
  483. }
  484. }
  485. return 1;
  486. }
  487. /*
  488. * Creates a new General Message/Response with an empty itav stack
  489. * returns a pointer to the PKIMessage on success, NULL on error
  490. */
  491. static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx, int body_type, int err_code)
  492. {
  493. OSSL_CMP_MSG *msg = NULL;
  494. if (!ossl_assert(ctx != NULL))
  495. return NULL;
  496. if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL)
  497. return NULL;
  498. if (ctx->genm_ITAVs != NULL
  499. && !ossl_cmp_msg_gen_push1_ITAVs(msg, ctx->genm_ITAVs))
  500. goto err;
  501. if (!ossl_cmp_msg_protect(ctx, msg))
  502. goto err;
  503. return msg;
  504. err:
  505. CMPerr(0, err_code);
  506. OSSL_CMP_MSG_free(msg);
  507. return NULL;
  508. }
  509. OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx)
  510. {
  511. return gen_new(ctx, OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM);
  512. }
  513. OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx)
  514. {
  515. return gen_new(ctx, OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP);
  516. }
  517. OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, OSSL_CMP_PKISI *si,
  518. int errorCode,
  519. OSSL_CMP_PKIFREETEXT *errorDetails,
  520. int unprotected)
  521. {
  522. OSSL_CMP_MSG *msg = NULL;
  523. if (!ossl_assert(ctx != NULL && si != NULL))
  524. return NULL;
  525. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL)
  526. goto err;
  527. OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo);
  528. if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si))
  529. == NULL)
  530. goto err;
  531. if (errorCode >= 0) {
  532. if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL)
  533. goto err;
  534. if (!ASN1_INTEGER_set(msg->body->value.error->errorCode, errorCode))
  535. goto err;
  536. }
  537. if (errorDetails != NULL)
  538. if ((msg->body->value.error->errorDetails =
  539. sk_ASN1_UTF8STRING_deep_copy(errorDetails, ASN1_STRING_dup,
  540. ASN1_STRING_free)) == NULL)
  541. goto err;
  542. if (!unprotected && !ossl_cmp_msg_protect(ctx, msg))
  543. goto err;
  544. return msg;
  545. err:
  546. CMPerr(0, CMP_R_ERROR_CREATING_ERROR);
  547. OSSL_CMP_MSG_free(msg);
  548. return NULL;
  549. }
  550. /*
  551. * OSSL_CMP_CERTSTATUS_set_certHash() calculates a hash of the certificate,
  552. * using the same hash algorithm as is used to create and verify the
  553. * certificate signature, and places the hash into the certHash field of a
  554. * OSSL_CMP_CERTSTATUS structure. This is used in the certConf message,
  555. * for example, to confirm that the certificate was received successfully.
  556. */
  557. int ossl_cmp_certstatus_set_certHash(OSSL_CMP_CERTSTATUS *certStatus,
  558. const X509 *cert)
  559. {
  560. unsigned int len;
  561. unsigned char hash[EVP_MAX_MD_SIZE];
  562. int md_NID;
  563. const EVP_MD *md = NULL;
  564. if (!ossl_assert(certStatus != NULL && cert != NULL))
  565. return 0;
  566. /*-
  567. * select hash algorithm, as stated in Appendix F. Compilable ASN.1 defs:
  568. * the hash of the certificate, using the same hash algorithm
  569. * as is used to create and verify the certificate signature
  570. */
  571. if (OBJ_find_sigid_algs(X509_get_signature_nid(cert), &md_NID, NULL)
  572. && (md = EVP_get_digestbynid(md_NID)) != NULL) {
  573. if (!X509_digest(cert, md, hash, &len))
  574. goto err;
  575. if (!ossl_cmp_asn1_octet_string_set1_bytes(&certStatus->certHash, hash,
  576. len))
  577. goto err;
  578. } else {
  579. CMPerr(0, CMP_R_UNSUPPORTED_ALGORITHM);
  580. return 0;
  581. }
  582. return 1;
  583. err:
  584. CMPerr(0, CMP_R_ERROR_SETTING_CERTHASH);
  585. return 0;
  586. }
  587. /*
  588. * TODO: handle potential 2nd certificate when signing and encrypting
  589. * certificates have been requested/received
  590. */
  591. OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int fail_info,
  592. const char *text)
  593. {
  594. OSSL_CMP_MSG *msg = NULL;
  595. OSSL_CMP_CERTSTATUS *certStatus = NULL;
  596. OSSL_CMP_PKISI *sinfo;
  597. if (!ossl_assert(ctx != NULL && ctx->newCert != NULL))
  598. return NULL;
  599. if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
  600. CMPerr(0, CMP_R_FAIL_INFO_OUT_OF_RANGE);
  601. return NULL;
  602. }
  603. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL)
  604. goto err;
  605. if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL)
  606. goto err;
  607. /* consume certStatus into msg right away so it gets deallocated with msg */
  608. if (!sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus))
  609. goto err;
  610. /* set the ID of the certReq */
  611. if (!ASN1_INTEGER_set(certStatus->certReqId, OSSL_CMP_CERTREQID))
  612. goto err;
  613. /*
  614. * the hash of the certificate, using the same hash algorithm
  615. * as is used to create and verify the certificate signature
  616. */
  617. if (!ossl_cmp_certstatus_set_certHash(certStatus, ctx->newCert))
  618. goto err;
  619. /*
  620. * For any particular CertStatus, omission of the statusInfo field
  621. * indicates ACCEPTANCE of the specified certificate. Alternatively,
  622. * explicit status details (with respect to acceptance or rejection) MAY
  623. * be provided in the statusInfo field, perhaps for auditing purposes at
  624. * the CA/RA.
  625. */
  626. sinfo = fail_info != 0 ?
  627. ossl_cmp_statusinfo_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) :
  628. ossl_cmp_statusinfo_new(OSSL_CMP_PKISTATUS_accepted, 0, text);
  629. if (sinfo == NULL)
  630. goto err;
  631. certStatus->statusInfo = sinfo;
  632. if (!ossl_cmp_msg_protect(ctx, msg))
  633. goto err;
  634. return msg;
  635. err:
  636. CMPerr(0, CMP_R_ERROR_CREATING_CERTCONF);
  637. OSSL_CMP_MSG_free(msg);
  638. return NULL;
  639. }
  640. OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid)
  641. {
  642. OSSL_CMP_MSG *msg = NULL;
  643. OSSL_CMP_POLLREQ *preq = NULL;
  644. if (!ossl_assert(ctx != NULL))
  645. return NULL;
  646. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL)
  647. goto err;
  648. /* TODO: support multiple cert request IDs to poll */
  649. if ((preq = OSSL_CMP_POLLREQ_new()) == NULL
  650. || !ASN1_INTEGER_set(preq->certReqId, crid)
  651. || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq))
  652. goto err;
  653. preq = NULL;
  654. if (!ossl_cmp_msg_protect(ctx, msg))
  655. goto err;
  656. return msg;
  657. err:
  658. CMPerr(0, CMP_R_ERROR_CREATING_POLLREQ);
  659. OSSL_CMP_POLLREQ_free(preq);
  660. OSSL_CMP_MSG_free(msg);
  661. return NULL;
  662. }
  663. OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid,
  664. int64_t poll_after)
  665. {
  666. OSSL_CMP_MSG *msg;
  667. OSSL_CMP_POLLREP *prep;
  668. if (!ossl_assert(ctx != NULL))
  669. return NULL;
  670. if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL)
  671. goto err;
  672. if ((prep = OSSL_CMP_POLLREP_new()) == NULL)
  673. goto err;
  674. if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep))
  675. goto err;
  676. if (!ASN1_INTEGER_set(prep->certReqId, crid))
  677. goto err;
  678. if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after))
  679. goto err;
  680. if (!ossl_cmp_msg_protect(ctx, msg))
  681. goto err;
  682. return msg;
  683. err:
  684. CMPerr(0, CMP_R_ERROR_CREATING_POLLREP);
  685. OSSL_CMP_MSG_free(msg);
  686. return NULL;
  687. }
  688. /*-
  689. * returns the status field of the RevRepContent with the given
  690. * request/sequence id inside a revocation response.
  691. * RevRepContent has the revocation statuses in same order as they were sent in
  692. * RevReqContent.
  693. * returns NULL on error
  694. */
  695. OSSL_CMP_PKISI *
  696. ossl_cmp_revrepcontent_get_pkistatusinfo(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  697. {
  698. OSSL_CMP_PKISI *status;
  699. if (!ossl_assert(rrep != NULL))
  700. return NULL;
  701. if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL)
  702. return status;
  703. CMPerr(0, CMP_R_PKISTATUSINFO_NOT_FOUND);
  704. return NULL;
  705. }
  706. /*
  707. * returns the CertId field in the revCerts part of the RevRepContent
  708. * with the given request/sequence id inside a revocation response.
  709. * RevRepContent has the CertIds in same order as they were sent in
  710. * RevReqContent.
  711. * returns NULL on error
  712. */
  713. OSSL_CRMF_CERTID *
  714. ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
  715. {
  716. OSSL_CRMF_CERTID *cid = NULL;
  717. if (!ossl_assert(rrep != NULL))
  718. return NULL;
  719. if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL)
  720. return cid;
  721. CMPerr(0, CMP_R_CERTID_NOT_FOUND);
  722. return NULL;
  723. }
  724. static int suitable_rid(const ASN1_INTEGER *certReqId, int rid)
  725. {
  726. int trid;
  727. if (rid == -1)
  728. return 1;
  729. trid = ossl_cmp_asn1_get_int(certReqId);
  730. if (trid == -1) {
  731. CMPerr(0, CMP_R_BAD_REQUEST_ID);
  732. return 0;
  733. }
  734. return rid == trid;
  735. }
  736. static void add_expected_rid(int rid)
  737. {
  738. char str[DECIMAL_SIZE(rid) + 1];
  739. BIO_snprintf(str, sizeof(str), "%d", rid);
  740. ERR_add_error_data(2, "expected certReqId = ", str);
  741. }
  742. /*
  743. * returns a pointer to the PollResponse with the given CertReqId
  744. * (or the first one in case -1) inside a PollRepContent
  745. * returns NULL on error or if no suitable PollResponse available
  746. */
  747. OSSL_CMP_POLLREP *
  748. ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc,
  749. int rid)
  750. {
  751. OSSL_CMP_POLLREP *pollRep = NULL;
  752. int i;
  753. if (!ossl_assert(prc != NULL))
  754. return NULL;
  755. for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) {
  756. pollRep = sk_OSSL_CMP_POLLREP_value(prc, i);
  757. if (suitable_rid(pollRep->certReqId, rid))
  758. return pollRep;
  759. }
  760. CMPerr(0, CMP_R_CERTRESPONSE_NOT_FOUND);
  761. add_expected_rid(rid);
  762. return NULL;
  763. }
  764. /*
  765. * returns a pointer to the CertResponse with the given CertReqId
  766. * (or the first one in case -1) inside a CertRepMessage
  767. * returns NULL on error or if no suitable CertResponse available
  768. */
  769. OSSL_CMP_CERTRESPONSE *
  770. ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm,
  771. int rid)
  772. {
  773. OSSL_CMP_CERTRESPONSE *crep = NULL;
  774. int i;
  775. if (!ossl_assert(crm != NULL && crm->response != NULL))
  776. return NULL;
  777. for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) {
  778. crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i);
  779. if (suitable_rid(crep->certReqId, rid))
  780. return crep;
  781. }
  782. CMPerr(0, CMP_R_CERTRESPONSE_NOT_FOUND);
  783. add_expected_rid(rid);
  784. return NULL;
  785. }
  786. /*
  787. * CMP_CERTRESPONSE_get1_certificate() attempts to retrieve the returned
  788. * certificate from the given certResponse B<crep>.
  789. * Uses the privkey in case of indirect POP from B<ctx>.
  790. * Returns a pointer to a copy of the found certificate, or NULL if not found.
  791. */
  792. X509 *ossl_cmp_certresponse_get1_certificate(EVP_PKEY *privkey,
  793. const OSSL_CMP_CERTRESPONSE *crep)
  794. {
  795. OSSL_CMP_CERTORENCCERT *coec;
  796. X509 *crt = NULL;
  797. if (!ossl_assert(crep != NULL))
  798. return NULL;
  799. if (crep->certifiedKeyPair
  800. && (coec = crep->certifiedKeyPair->certOrEncCert) != NULL) {
  801. switch (coec->type) {
  802. case OSSL_CMP_CERTORENCCERT_CERTIFICATE:
  803. crt = X509_dup(coec->value.certificate);
  804. break;
  805. case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT:
  806. /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */
  807. if (privkey == NULL) {
  808. CMPerr(0, CMP_R_MISSING_PRIVATE_KEY);
  809. return NULL;
  810. }
  811. crt =
  812. OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(coec->value.encryptedCert,
  813. privkey);
  814. break;
  815. default:
  816. CMPerr(0, CMP_R_UNKNOWN_CERT_TYPE);
  817. return NULL;
  818. }
  819. }
  820. if (crt == NULL)
  821. CMPerr(0, CMP_R_CERTIFICATE_NOT_FOUND);
  822. return crt;
  823. }
  824. OSSL_CMP_MSG *ossl_cmp_msg_load(const char *file)
  825. {
  826. OSSL_CMP_MSG *msg = NULL;
  827. BIO *bio = NULL;
  828. if (!ossl_assert(file != NULL))
  829. return NULL;
  830. if ((bio = BIO_new_file(file, "rb")) == NULL)
  831. return NULL;
  832. msg = OSSL_d2i_CMP_MSG_bio(bio, NULL);
  833. BIO_free(bio);
  834. return msg;
  835. }