crmf_lib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*-
  2. * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2018
  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. * CRMF implementation by Martin Peylo, Miikka Viljanen, and David von Oheimb.
  12. */
  13. /*
  14. * This file contains the functions that handle the individual items inside
  15. * the CRMF structures
  16. */
  17. /*
  18. * NAMING
  19. *
  20. * The 0 functions use the supplied structure pointer directly in the parent and
  21. * it will be freed up when the parent is freed.
  22. *
  23. * The 1 functions use a copy of the supplied structure pointer (or in some
  24. * cases increases its link count) in the parent and so both should be freed up.
  25. */
  26. #include <openssl/asn1t.h>
  27. #include "crmf_local.h"
  28. #include "internal/constant_time.h"
  29. /* explicit #includes not strictly needed since implied by the above: */
  30. #include <openssl/crmf.h>
  31. #include <openssl/err.h>
  32. #include <openssl/evp.h>
  33. DEFINE_STACK_OF(X509_EXTENSION)
  34. DEFINE_STACK_OF(OSSL_CRMF_MSG)
  35. /*-
  36. * atyp = Attribute Type
  37. * valt = Value Type
  38. * ctrlinf = "regCtrl" or "regInfo"
  39. */
  40. #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \
  41. int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, \
  42. const valt *in) \
  43. { \
  44. OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
  45. \
  46. if (msg == NULL || in == NULL) \
  47. goto err; \
  48. if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \
  49. goto err; \
  50. if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \
  51. goto err; \
  52. if ((atav->value.atyp = valt##_dup(in)) == NULL) \
  53. goto err; \
  54. if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \
  55. goto err; \
  56. return 1; \
  57. err: \
  58. OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \
  59. return 0; \
  60. }
  61. /*-
  62. * Pushes the given control attribute into the controls stack of a CertRequest
  63. * (section 6)
  64. * returns 1 on success, 0 on error
  65. */
  66. static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm,
  67. OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl)
  68. {
  69. int new = 0;
  70. if (crm == NULL || crm->certReq == NULL || ctrl == NULL) {
  71. CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL, CRMF_R_NULL_ARGUMENT);
  72. return 0;
  73. }
  74. if (crm->certReq->controls == NULL) {
  75. crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
  76. if (crm->certReq->controls == NULL)
  77. goto err;
  78. new = 1;
  79. }
  80. if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl))
  81. goto err;
  82. return 1;
  83. err:
  84. if (new != 0) {
  85. sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls);
  86. crm->certReq->controls = NULL;
  87. }
  88. return 0;
  89. }
  90. /* id-regCtrl-regToken Control (section 6.1) */
  91. IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl)
  92. /* id-regCtrl-authenticator Control (section 6.2) */
  93. #define ASN1_UTF8STRING_dup ASN1_STRING_dup
  94. IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl)
  95. int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi,
  96. int method, GENERAL_NAME *nm)
  97. {
  98. if (spi == NULL
  99. || method < OSSL_CRMF_PUB_METHOD_DONTCARE
  100. || method > OSSL_CRMF_PUB_METHOD_LDAP) {
  101. CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO,
  102. ERR_R_PASSED_INVALID_ARGUMENT);
  103. return 0;
  104. }
  105. if (!ASN1_INTEGER_set(spi->pubMethod, method))
  106. return 0;
  107. GENERAL_NAME_free(spi->pubLocation);
  108. spi->pubLocation = nm;
  109. return 1;
  110. }
  111. int
  112. OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
  113. OSSL_CRMF_SINGLEPUBINFO *spi)
  114. {
  115. if (pi == NULL || spi == NULL) {
  116. CRMFerr(CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO,
  117. CRMF_R_NULL_ARGUMENT);
  118. return 0;
  119. }
  120. if (pi->pubInfos == NULL)
  121. pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
  122. if (pi->pubInfos == NULL)
  123. return 0;
  124. return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi);
  125. }
  126. int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
  127. int action)
  128. {
  129. if (pi == NULL
  130. || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH
  131. || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) {
  132. CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION,
  133. ERR_R_PASSED_INVALID_ARGUMENT);
  134. return 0;
  135. }
  136. return ASN1_INTEGER_set(pi->action, action);
  137. }
  138. /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
  139. IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO,
  140. regCtrl)
  141. /* id-regCtrl-oldCertID Control (section 6.5) from the given */
  142. IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
  143. OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer,
  144. const ASN1_INTEGER *serial)
  145. {
  146. OSSL_CRMF_CERTID *cid = NULL;
  147. if (issuer == NULL || serial == NULL) {
  148. CRMFerr(CRMF_F_OSSL_CRMF_CERTID_GEN, CRMF_R_NULL_ARGUMENT);
  149. return NULL;
  150. }
  151. if ((cid = OSSL_CRMF_CERTID_new()) == NULL)
  152. goto err;
  153. if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
  154. goto err;
  155. cid->issuer->type = GEN_DIRNAME;
  156. ASN1_INTEGER_free(cid->serialNumber);
  157. if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
  158. goto err;
  159. return cid;
  160. err:
  161. OSSL_CRMF_CERTID_free(cid);
  162. return NULL;
  163. }
  164. /*
  165. * id-regCtrl-protocolEncrKey Control (section 6.6)
  166. */
  167. IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl)
  168. /*-
  169. * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
  170. * (section 7)
  171. * returns 1 on success, 0 on error
  172. */
  173. static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm,
  174. OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri)
  175. {
  176. STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL;
  177. if (crm == NULL || ri == NULL) {
  178. CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO, CRMF_R_NULL_ARGUMENT);
  179. return 0;
  180. }
  181. if (crm->regInfo == NULL)
  182. crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
  183. if (crm->regInfo == NULL)
  184. goto err;
  185. if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri))
  186. goto err;
  187. return 1;
  188. err:
  189. if (info != NULL)
  190. crm->regInfo = NULL;
  191. sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info);
  192. return 0;
  193. }
  194. /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
  195. IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo)
  196. /* id-regInfo-certReq to regInfo (section 7.2) */
  197. IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo)
  198. /* retrieves the certificate template of crm */
  199. OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm)
  200. {
  201. if (crm == NULL || crm->certReq == NULL) {
  202. CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET0_TMPL, CRMF_R_NULL_ARGUMENT);
  203. return NULL;
  204. }
  205. return crm->certReq->certTemplate;
  206. }
  207. int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG *crm,
  208. ASN1_TIME *notBefore, ASN1_TIME *notAfter)
  209. {
  210. OSSL_CRMF_OPTIONALVALIDITY *vld;
  211. OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
  212. if (tmpl == NULL) { /* also crm == NULL implies this */
  213. CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_VALIDITY, CRMF_R_NULL_ARGUMENT);
  214. return 0;
  215. }
  216. if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL)
  217. return 0;
  218. vld->notBefore = notBefore;
  219. vld->notAfter = notAfter;
  220. tmpl->validity = vld;
  221. return 1;
  222. }
  223. int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid)
  224. {
  225. if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) {
  226. CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID, CRMF_R_NULL_ARGUMENT);
  227. return 0;
  228. }
  229. return ASN1_INTEGER_set(crm->certReq->certReqId, rid);
  230. }
  231. /* get ASN.1 encoded integer, return -1 on error */
  232. static int crmf_asn1_get_int(const ASN1_INTEGER *a)
  233. {
  234. int64_t res;
  235. if (!ASN1_INTEGER_get_int64(&res, a)) {
  236. CRMFerr(0, ASN1_R_INVALID_NUMBER);
  237. return -1;
  238. }
  239. if (res < INT_MIN) {
  240. CRMFerr(0, ASN1_R_TOO_SMALL);
  241. return -1;
  242. }
  243. if (res > INT_MAX) {
  244. CRMFerr(0, ASN1_R_TOO_LARGE);
  245. return -1;
  246. }
  247. return (int)res;
  248. }
  249. int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm)
  250. {
  251. if (crm == NULL || /* not really needed: */ crm->certReq == NULL) {
  252. CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID, CRMF_R_NULL_ARGUMENT);
  253. return -1;
  254. }
  255. return crmf_asn1_get_int(crm->certReq->certReqId);
  256. }
  257. int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm,
  258. X509_EXTENSIONS *exts)
  259. {
  260. OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
  261. if (tmpl == NULL) { /* also crm == NULL implies this */
  262. CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS, CRMF_R_NULL_ARGUMENT);
  263. return 0;
  264. }
  265. if (sk_X509_EXTENSION_num(exts) == 0) {
  266. sk_X509_EXTENSION_free(exts);
  267. exts = NULL; /* do not include empty extensions list */
  268. }
  269. sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free);
  270. tmpl->extensions = exts;
  271. return 1;
  272. }
  273. int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm,
  274. X509_EXTENSION *ext)
  275. {
  276. int new = 0;
  277. OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
  278. if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */
  279. CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION, CRMF_R_NULL_ARGUMENT);
  280. return 0;
  281. }
  282. if (tmpl->extensions == NULL) {
  283. if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL)
  284. goto err;
  285. new = 1;
  286. }
  287. if (!sk_X509_EXTENSION_push(tmpl->extensions, ext))
  288. goto err;
  289. return 1;
  290. err:
  291. if (new != 0) {
  292. sk_X509_EXTENSION_free(tmpl->extensions);
  293. tmpl->extensions = NULL;
  294. }
  295. return 0;
  296. }
  297. /* TODO: support cases 1+2 (besides case 3) defined in RFC 4211, section 4.1. */
  298. static int CRMF_poposigningkey_init(OSSL_CRMF_POPOSIGNINGKEY *ps,
  299. OSSL_CRMF_CERTREQUEST *cr,
  300. EVP_PKEY *pkey, int dgst)
  301. {
  302. int ret = 0;
  303. EVP_MD *fetched_md = NULL;
  304. const EVP_MD *md = EVP_get_digestbynid(dgst);
  305. if (ps == NULL || cr == NULL || pkey == NULL) {
  306. CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_NULL_ARGUMENT);
  307. return 0;
  308. }
  309. /* If we didn't find legacy MD, we try an implicit fetch */
  310. if (md == NULL)
  311. md = fetched_md = EVP_MD_fetch(NULL, OBJ_nid2sn(dgst), NULL);
  312. if (md == NULL) {
  313. CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT,
  314. CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY);
  315. return 0;
  316. }
  317. ret = ASN1_item_sign(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
  318. ps->algorithmIdentifier, NULL, ps->signature,
  319. cr, pkey, md);
  320. EVP_MD_free(fetched_md);
  321. return ret;
  322. }
  323. int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG *crm, EVP_PKEY *pkey,
  324. int dgst, int ppmtd)
  325. {
  326. OSSL_CRMF_POPO *pp = NULL;
  327. ASN1_INTEGER *tag = NULL;
  328. if (crm == NULL || (ppmtd == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
  329. CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, CRMF_R_NULL_ARGUMENT);
  330. return 0;
  331. }
  332. if (ppmtd == OSSL_CRMF_POPO_NONE)
  333. goto end;
  334. if ((pp = OSSL_CRMF_POPO_new()) == NULL)
  335. goto err;
  336. pp->type = ppmtd;
  337. switch (ppmtd) {
  338. case OSSL_CRMF_POPO_RAVERIFIED:
  339. if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
  340. goto err;
  341. break;
  342. case OSSL_CRMF_POPO_SIGNATURE:
  343. {
  344. OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
  345. if (ps == NULL
  346. || !CRMF_poposigningkey_init(ps, crm->certReq, pkey, dgst)) {
  347. OSSL_CRMF_POPOSIGNINGKEY_free(ps);
  348. goto err;
  349. }
  350. pp->value.signature = ps;
  351. }
  352. break;
  353. case OSSL_CRMF_POPO_KEYENC:
  354. if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
  355. goto err;
  356. tag = ASN1_INTEGER_new();
  357. pp->value.keyEncipherment->type =
  358. OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
  359. pp->value.keyEncipherment->value.subsequentMessage = tag;
  360. if (tag == NULL
  361. || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
  362. goto err;
  363. break;
  364. default:
  365. CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO,
  366. CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
  367. goto err;
  368. }
  369. end:
  370. OSSL_CRMF_POPO_free(crm->popo);
  371. crm->popo = pp;
  372. return 1;
  373. err:
  374. OSSL_CRMF_POPO_free(pp);
  375. return 0;
  376. }
  377. /* verifies the Proof-of-Possession of the request with the given rid in reqs */
  378. int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
  379. int rid, int acceptRAVerified)
  380. {
  381. OSSL_CRMF_MSG *req = NULL;
  382. X509_PUBKEY *pubkey = NULL;
  383. OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
  384. if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) {
  385. CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO, CRMF_R_NULL_ARGUMENT);
  386. return 0;
  387. }
  388. if (req->popo == NULL) {
  389. CRMFerr(0, CRMF_R_POPO_MISSING);
  390. return 0;
  391. }
  392. switch (req->popo->type) {
  393. case OSSL_CRMF_POPO_RAVERIFIED:
  394. if (!acceptRAVerified) {
  395. CRMFerr(0, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED);
  396. return 0;
  397. }
  398. break;
  399. case OSSL_CRMF_POPO_SIGNATURE:
  400. pubkey = req->certReq->certTemplate->publicKey;
  401. if (pubkey == NULL) {
  402. CRMFerr(0, CRMF_R_POPO_MISSING_PUBLIC_KEY);
  403. return 0;
  404. }
  405. sig = req->popo->value.signature;
  406. if (sig->poposkInput != NULL) {
  407. /*
  408. * According to RFC 4211: publicKey contains a copy of
  409. * the public key from the certificate template. This MUST be
  410. * exactly the same value as contained in the certificate template.
  411. */
  412. if (sig->poposkInput->publicKey == NULL) {
  413. CRMFerr(0, CRMF_R_POPO_MISSING_PUBLIC_KEY);
  414. return 0;
  415. }
  416. if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) {
  417. CRMFerr(0, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY);
  418. return 0;
  419. }
  420. /*
  421. * TODO check the contents of the authInfo sub-field,
  422. * see RFC 4211 https://tools.ietf.org/html/rfc4211#section-4.1
  423. */
  424. if (ASN1_item_verify(ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT),
  425. sig->algorithmIdentifier, sig->signature,
  426. sig->poposkInput,
  427. X509_PUBKEY_get0(pubkey)) < 1)
  428. return 0;
  429. } else {
  430. if (req->certReq->certTemplate->subject == NULL) {
  431. CRMFerr(0, CRMF_R_POPO_MISSING_SUBJECT);
  432. return 0;
  433. }
  434. if (ASN1_item_verify(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
  435. sig->algorithmIdentifier, sig->signature,
  436. req->certReq, X509_PUBKEY_get0(pubkey)) < 1)
  437. return 0;
  438. }
  439. break;
  440. case OSSL_CRMF_POPO_KEYENC:
  441. /*
  442. * TODO: when OSSL_CMP_certrep_new() supports encrypted certs,
  443. * return 1 if the type of req->popo->value.keyEncipherment
  444. * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
  445. * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
  446. */
  447. case OSSL_CRMF_POPO_KEYAGREE:
  448. default:
  449. CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
  450. CRMF_R_UNSUPPORTED_POPO_METHOD);
  451. return 0;
  452. }
  453. return 1;
  454. }
  455. /* retrieves the serialNumber of the given cert template or NULL on error */
  456. ASN1_INTEGER
  457. *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl)
  458. {
  459. return tmpl != NULL ? tmpl->serialNumber : NULL;
  460. }
  461. /* retrieves the issuer name of the given cert template or NULL on error */
  462. const X509_NAME
  463. *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl)
  464. {
  465. return tmpl != NULL ? tmpl->issuer : NULL;
  466. }
  467. /* retrieves the issuer name of the given CertId or NULL on error */
  468. const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid)
  469. {
  470. return cid != NULL && cid->issuer->type == GEN_DIRNAME ?
  471. cid->issuer->d.directoryName : NULL;
  472. }
  473. /* retrieves the serialNumber of the given CertId or NULL on error */
  474. ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID *cid)
  475. {
  476. return cid != NULL ? cid->serialNumber : NULL;
  477. }
  478. /*-
  479. * fill in certificate template.
  480. * Any value argument that is NULL will leave the respective field unchanged.
  481. */
  482. int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
  483. EVP_PKEY *pubkey,
  484. const X509_NAME *subject,
  485. const X509_NAME *issuer,
  486. const ASN1_INTEGER *serial)
  487. {
  488. if (tmpl == NULL) {
  489. CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, CRMF_R_NULL_ARGUMENT);
  490. return 0;
  491. }
  492. if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject))
  493. return 0;
  494. if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer))
  495. return 0;
  496. if (serial != NULL) {
  497. ASN1_INTEGER_free(tmpl->serialNumber);
  498. if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
  499. return 0;
  500. }
  501. if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
  502. return 0;
  503. return 1;
  504. }
  505. /*-
  506. * Decrypts the certificate in the given encryptedValue using private key pkey.
  507. * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2.
  508. *
  509. * returns a pointer to the decrypted certificate
  510. * returns NULL on error or if no certificate available
  511. */
  512. X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert,
  513. EVP_PKEY *pkey)
  514. {
  515. X509 *cert = NULL; /* decrypted certificate */
  516. EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
  517. unsigned char *ek = NULL; /* decrypted symmetric encryption key */
  518. size_t eksize = 0; /* size of decrypted symmetric encryption key */
  519. const EVP_CIPHER *cipher = NULL; /* used cipher */
  520. int cikeysize = 0; /* key size from cipher */
  521. unsigned char *iv = NULL; /* initial vector for symmetric encryption */
  522. unsigned char *outbuf = NULL; /* decryption output buffer */
  523. const unsigned char *p = NULL; /* needed for decoding ASN1 */
  524. int symmAlg = 0; /* NIDs for symmetric algorithm */
  525. int n, outlen = 0;
  526. EVP_PKEY_CTX *pkctx = NULL; /* private key context */
  527. if (ecert == NULL || ecert->symmAlg == NULL || ecert->encSymmKey == NULL
  528. || ecert->encValue == NULL || pkey == NULL) {
  529. CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
  530. CRMF_R_NULL_ARGUMENT);
  531. return NULL;
  532. }
  533. if ((symmAlg = OBJ_obj2nid(ecert->symmAlg->algorithm)) == 0) {
  534. CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
  535. CRMF_R_UNSUPPORTED_CIPHER);
  536. return NULL;
  537. }
  538. /* select symmetric cipher based on algorithm given in message */
  539. if ((cipher = EVP_get_cipherbynid(symmAlg)) == NULL) {
  540. CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
  541. CRMF_R_UNSUPPORTED_CIPHER);
  542. goto end;
  543. }
  544. cikeysize = EVP_CIPHER_key_length(cipher);
  545. /* first the symmetric key needs to be decrypted */
  546. pkctx = EVP_PKEY_CTX_new(pkey, NULL);
  547. if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx)) {
  548. ASN1_BIT_STRING *encKey = ecert->encSymmKey;
  549. size_t failure;
  550. int retval;
  551. if (EVP_PKEY_decrypt(pkctx, NULL, &eksize,
  552. encKey->data, encKey->length) <= 0
  553. || (ek = OPENSSL_malloc(eksize)) == NULL)
  554. goto end;
  555. retval = EVP_PKEY_decrypt(pkctx, ek, &eksize,
  556. encKey->data, encKey->length);
  557. ERR_clear_error(); /* error state may have sensitive information */
  558. failure = ~constant_time_is_zero_s(constant_time_msb(retval)
  559. | constant_time_is_zero(retval));
  560. failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize);
  561. if (failure) {
  562. CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
  563. CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
  564. goto end;
  565. }
  566. } else {
  567. goto end;
  568. }
  569. if ((iv = OPENSSL_malloc(EVP_CIPHER_iv_length(cipher))) == NULL)
  570. goto end;
  571. if (ASN1_TYPE_get_octetstring(ecert->symmAlg->parameter, iv,
  572. EVP_CIPHER_iv_length(cipher))
  573. != EVP_CIPHER_iv_length(cipher)) {
  574. CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
  575. CRMF_R_MALFORMED_IV);
  576. goto end;
  577. }
  578. /*
  579. * d2i_X509 changes the given pointer, so use p for decoding the message and
  580. * keep the original pointer in outbuf so the memory can be freed later
  581. */
  582. if ((p = outbuf = OPENSSL_malloc(ecert->encValue->length +
  583. EVP_CIPHER_block_size(cipher))) == NULL
  584. || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
  585. goto end;
  586. EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
  587. if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
  588. || !EVP_DecryptUpdate(evp_ctx, outbuf, &outlen,
  589. ecert->encValue->data,
  590. ecert->encValue->length)
  591. || !EVP_DecryptFinal(evp_ctx, outbuf + outlen, &n)) {
  592. CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
  593. CRMF_R_ERROR_DECRYPTING_CERTIFICATE);
  594. goto end;
  595. }
  596. outlen += n;
  597. /* convert decrypted certificate from DER to internal ASN.1 structure */
  598. if ((cert = d2i_X509(NULL, &p, outlen)) == NULL) {
  599. CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
  600. CRMF_R_ERROR_DECODING_CERTIFICATE);
  601. }
  602. end:
  603. EVP_PKEY_CTX_free(pkctx);
  604. OPENSSL_free(outbuf);
  605. EVP_CIPHER_CTX_free(evp_ctx);
  606. OPENSSL_clear_free(ek, eksize);
  607. OPENSSL_free(iv);
  608. return cert;
  609. }