crmf_lib.c 24 KB

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