crmf_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*-
  2. * Copyright 2007-2021 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. ERR_raise(ERR_LIB_CRMF, 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. ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT);
  100. return 0;
  101. }
  102. if (!ASN1_INTEGER_set(spi->pubMethod, method))
  103. return 0;
  104. GENERAL_NAME_free(spi->pubLocation);
  105. spi->pubLocation = nm;
  106. return 1;
  107. }
  108. int
  109. OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
  110. OSSL_CRMF_SINGLEPUBINFO *spi)
  111. {
  112. if (pi == NULL || spi == NULL) {
  113. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  114. return 0;
  115. }
  116. if (pi->pubInfos == NULL)
  117. pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
  118. if (pi->pubInfos == NULL)
  119. return 0;
  120. return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi);
  121. }
  122. int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
  123. int action)
  124. {
  125. if (pi == NULL
  126. || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH
  127. || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) {
  128. ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT);
  129. return 0;
  130. }
  131. return ASN1_INTEGER_set(pi->action, action);
  132. }
  133. /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
  134. IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO,
  135. regCtrl)
  136. /* id-regCtrl-oldCertID Control (section 6.5) from the given */
  137. IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
  138. OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer,
  139. const ASN1_INTEGER *serial)
  140. {
  141. OSSL_CRMF_CERTID *cid = NULL;
  142. if (issuer == NULL || serial == NULL) {
  143. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  144. return NULL;
  145. }
  146. if ((cid = OSSL_CRMF_CERTID_new()) == NULL)
  147. goto err;
  148. if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
  149. goto err;
  150. cid->issuer->type = GEN_DIRNAME;
  151. ASN1_INTEGER_free(cid->serialNumber);
  152. if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
  153. goto err;
  154. return cid;
  155. err:
  156. OSSL_CRMF_CERTID_free(cid);
  157. return NULL;
  158. }
  159. /*
  160. * id-regCtrl-protocolEncrKey Control (section 6.6)
  161. */
  162. IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl)
  163. /*-
  164. * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
  165. * (section 7)
  166. * returns 1 on success, 0 on error
  167. */
  168. static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm,
  169. OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri)
  170. {
  171. STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL;
  172. if (crm == NULL || ri == NULL) {
  173. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  174. return 0;
  175. }
  176. if (crm->regInfo == NULL)
  177. crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
  178. if (crm->regInfo == NULL)
  179. goto err;
  180. if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri))
  181. goto err;
  182. return 1;
  183. err:
  184. if (info != NULL)
  185. crm->regInfo = NULL;
  186. sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info);
  187. return 0;
  188. }
  189. /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
  190. IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo)
  191. /* id-regInfo-certReq to regInfo (section 7.2) */
  192. IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo)
  193. /* retrieves the certificate template of crm */
  194. OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm)
  195. {
  196. if (crm == NULL || crm->certReq == NULL) {
  197. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  198. return NULL;
  199. }
  200. return crm->certReq->certTemplate;
  201. }
  202. int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG *crm,
  203. ASN1_TIME *notBefore, ASN1_TIME *notAfter)
  204. {
  205. OSSL_CRMF_OPTIONALVALIDITY *vld;
  206. OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
  207. if (tmpl == NULL) { /* also crm == NULL implies this */
  208. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  209. return 0;
  210. }
  211. if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL)
  212. return 0;
  213. vld->notBefore = notBefore;
  214. vld->notAfter = notAfter;
  215. tmpl->validity = vld;
  216. return 1;
  217. }
  218. int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid)
  219. {
  220. if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) {
  221. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  222. return 0;
  223. }
  224. return ASN1_INTEGER_set(crm->certReq->certReqId, rid);
  225. }
  226. /* get ASN.1 encoded integer, return -1 on error */
  227. static int crmf_asn1_get_int(const ASN1_INTEGER *a)
  228. {
  229. int64_t res;
  230. if (!ASN1_INTEGER_get_int64(&res, a)) {
  231. ERR_raise(ERR_LIB_CRMF, ASN1_R_INVALID_NUMBER);
  232. return -1;
  233. }
  234. if (res < INT_MIN) {
  235. ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_SMALL);
  236. return -1;
  237. }
  238. if (res > INT_MAX) {
  239. ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_LARGE);
  240. return -1;
  241. }
  242. return (int)res;
  243. }
  244. int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm)
  245. {
  246. if (crm == NULL || /* not really needed: */ crm->certReq == NULL) {
  247. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  248. return -1;
  249. }
  250. return crmf_asn1_get_int(crm->certReq->certReqId);
  251. }
  252. int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm,
  253. X509_EXTENSIONS *exts)
  254. {
  255. OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
  256. if (tmpl == NULL) { /* also crm == NULL implies this */
  257. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  258. return 0;
  259. }
  260. if (sk_X509_EXTENSION_num(exts) == 0) {
  261. sk_X509_EXTENSION_free(exts);
  262. exts = NULL; /* do not include empty extensions list */
  263. }
  264. sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free);
  265. tmpl->extensions = exts;
  266. return 1;
  267. }
  268. int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm,
  269. X509_EXTENSION *ext)
  270. {
  271. int new = 0;
  272. OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
  273. if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */
  274. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  275. return 0;
  276. }
  277. if (tmpl->extensions == NULL) {
  278. if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL)
  279. goto err;
  280. new = 1;
  281. }
  282. if (!sk_X509_EXTENSION_push(tmpl->extensions, ext))
  283. goto err;
  284. return 1;
  285. err:
  286. if (new != 0) {
  287. sk_X509_EXTENSION_free(tmpl->extensions);
  288. tmpl->extensions = NULL;
  289. }
  290. return 0;
  291. }
  292. static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY *ps,
  293. const OSSL_CRMF_CERTREQUEST *cr,
  294. EVP_PKEY *pkey, const EVP_MD *digest,
  295. OSSL_LIB_CTX *libctx, const char *propq)
  296. {
  297. if (ps == NULL || cr == NULL || pkey == NULL) {
  298. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  299. return 0;
  300. }
  301. if (ps->poposkInput != NULL) {
  302. /* TODO: support cases 1+2 defined in RFC 4211, section 4.1 */
  303. ERR_raise(ERR_LIB_CRMF, CRMF_R_POPOSKINPUT_NOT_SUPPORTED);
  304. return 0;
  305. }
  306. return ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
  307. ps->algorithmIdentifier, NULL, ps->signature, cr,
  308. NULL, pkey, digest, libctx, propq);
  309. }
  310. int OSSL_CRMF_MSG_create_popo(int meth, OSSL_CRMF_MSG *crm,
  311. EVP_PKEY *pkey, const EVP_MD *digest,
  312. OSSL_LIB_CTX *libctx, const char *propq)
  313. {
  314. OSSL_CRMF_POPO *pp = NULL;
  315. ASN1_INTEGER *tag = NULL;
  316. if (crm == NULL || (meth == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
  317. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  318. return 0;
  319. }
  320. if (meth == OSSL_CRMF_POPO_NONE)
  321. goto end;
  322. if ((pp = OSSL_CRMF_POPO_new()) == NULL)
  323. goto err;
  324. pp->type = meth;
  325. switch (meth) {
  326. case OSSL_CRMF_POPO_RAVERIFIED:
  327. if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
  328. goto err;
  329. break;
  330. case OSSL_CRMF_POPO_SIGNATURE:
  331. {
  332. OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
  333. if (ps == NULL)
  334. goto err;
  335. if (!create_popo_signature(ps, crm->certReq, pkey, digest,
  336. libctx, propq)) {
  337. OSSL_CRMF_POPOSIGNINGKEY_free(ps);
  338. goto err;
  339. }
  340. pp->value.signature = ps;
  341. }
  342. break;
  343. case OSSL_CRMF_POPO_KEYENC:
  344. if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
  345. goto err;
  346. tag = ASN1_INTEGER_new();
  347. pp->value.keyEncipherment->type =
  348. OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
  349. pp->value.keyEncipherment->value.subsequentMessage = tag;
  350. if (tag == NULL
  351. || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
  352. goto err;
  353. break;
  354. default:
  355. ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
  356. goto err;
  357. }
  358. end:
  359. OSSL_CRMF_POPO_free(crm->popo);
  360. crm->popo = pp;
  361. return 1;
  362. err:
  363. OSSL_CRMF_POPO_free(pp);
  364. return 0;
  365. }
  366. /* verifies the Proof-of-Possession of the request with the given rid in reqs */
  367. int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
  368. int rid, int acceptRAVerified,
  369. OSSL_LIB_CTX *libctx, const char *propq)
  370. {
  371. OSSL_CRMF_MSG *req = NULL;
  372. X509_PUBKEY *pubkey = NULL;
  373. OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
  374. const ASN1_ITEM *it;
  375. void *asn;
  376. if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) {
  377. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  378. return 0;
  379. }
  380. if (req->popo == NULL) {
  381. ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING);
  382. return 0;
  383. }
  384. switch (req->popo->type) {
  385. case OSSL_CRMF_POPO_RAVERIFIED:
  386. if (!acceptRAVerified) {
  387. ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED);
  388. return 0;
  389. }
  390. break;
  391. case OSSL_CRMF_POPO_SIGNATURE:
  392. pubkey = req->certReq->certTemplate->publicKey;
  393. if (pubkey == NULL) {
  394. ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY);
  395. return 0;
  396. }
  397. sig = req->popo->value.signature;
  398. if (sig->poposkInput != NULL) {
  399. /*
  400. * According to RFC 4211: publicKey contains a copy of
  401. * the public key from the certificate template. This MUST be
  402. * exactly the same value as contained in the certificate template.
  403. */
  404. if (sig->poposkInput->publicKey == NULL) {
  405. ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY);
  406. return 0;
  407. }
  408. if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) {
  409. ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY);
  410. return 0;
  411. }
  412. /*
  413. * TODO check the contents of the authInfo sub-field,
  414. * see RFC 4211 https://tools.ietf.org/html/rfc4211#section-4.1
  415. */
  416. it = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT);
  417. asn = sig->poposkInput;
  418. } else {
  419. if (req->certReq->certTemplate->subject == NULL) {
  420. ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_SUBJECT);
  421. return 0;
  422. }
  423. it = ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST);
  424. asn = req->certReq;
  425. }
  426. if (ASN1_item_verify_ex(it, sig->algorithmIdentifier, sig->signature,
  427. asn, NULL, X509_PUBKEY_get0(pubkey), libctx,
  428. propq) < 1)
  429. return 0;
  430. break;
  431. case OSSL_CRMF_POPO_KEYENC:
  432. /*
  433. * TODO: when OSSL_CMP_certrep_new() supports encrypted certs,
  434. * return 1 if the type of req->popo->value.keyEncipherment
  435. * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
  436. * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
  437. */
  438. case OSSL_CRMF_POPO_KEYAGREE:
  439. default:
  440. ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_POPO_METHOD);
  441. return 0;
  442. }
  443. return 1;
  444. }
  445. /* retrieves the serialNumber of the given cert template or NULL on error */
  446. ASN1_INTEGER
  447. *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl)
  448. {
  449. return tmpl != NULL ? tmpl->serialNumber : NULL;
  450. }
  451. /* retrieves the issuer name of the given cert template or NULL on error */
  452. const X509_NAME
  453. *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl)
  454. {
  455. return tmpl != NULL ? tmpl->issuer : NULL;
  456. }
  457. /* retrieves the issuer name of the given CertId or NULL on error */
  458. const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid)
  459. {
  460. return cid != NULL && cid->issuer->type == GEN_DIRNAME ?
  461. cid->issuer->d.directoryName : NULL;
  462. }
  463. /* retrieves the serialNumber of the given CertId or NULL on error */
  464. ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID *cid)
  465. {
  466. return cid != NULL ? cid->serialNumber : NULL;
  467. }
  468. /*-
  469. * fill in certificate template.
  470. * Any value argument that is NULL will leave the respective field unchanged.
  471. */
  472. int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
  473. EVP_PKEY *pubkey,
  474. const X509_NAME *subject,
  475. const X509_NAME *issuer,
  476. const ASN1_INTEGER *serial)
  477. {
  478. if (tmpl == NULL) {
  479. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  480. return 0;
  481. }
  482. if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject))
  483. return 0;
  484. if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer))
  485. return 0;
  486. if (serial != NULL) {
  487. ASN1_INTEGER_free(tmpl->serialNumber);
  488. if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
  489. return 0;
  490. }
  491. if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
  492. return 0;
  493. return 1;
  494. }
  495. /*-
  496. * Decrypts the certificate in the given encryptedValue using private key pkey.
  497. * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2.
  498. *
  499. * returns a pointer to the decrypted certificate
  500. * returns NULL on error or if no certificate available
  501. */
  502. X509
  503. *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert,
  504. OSSL_LIB_CTX *libctx, const char *propq,
  505. EVP_PKEY *pkey)
  506. {
  507. X509 *cert = NULL; /* decrypted certificate */
  508. EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
  509. unsigned char *ek = NULL; /* decrypted symmetric encryption key */
  510. size_t eksize = 0; /* size of decrypted symmetric encryption key */
  511. const EVP_CIPHER *cipher = NULL; /* used cipher */
  512. int cikeysize = 0; /* key size from cipher */
  513. unsigned char *iv = NULL; /* initial vector for symmetric encryption */
  514. unsigned char *outbuf = NULL; /* decryption output buffer */
  515. const unsigned char *p = NULL; /* needed for decoding ASN1 */
  516. int n, outlen = 0;
  517. EVP_PKEY_CTX *pkctx = NULL; /* private key context */
  518. if (ecert == NULL || ecert->symmAlg == NULL || ecert->encSymmKey == NULL
  519. || ecert->encValue == NULL || pkey == NULL) {
  520. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  521. return NULL;
  522. }
  523. /* select symmetric cipher based on algorithm given in message */
  524. if ((cipher = EVP_get_cipherbyobj(ecert->symmAlg->algorithm)) == NULL) {
  525. ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_CIPHER);
  526. goto end;
  527. }
  528. cikeysize = EVP_CIPHER_key_length(cipher);
  529. /* first the symmetric key needs to be decrypted */
  530. pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
  531. if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx)) {
  532. ASN1_BIT_STRING *encKey = ecert->encSymmKey;
  533. size_t failure;
  534. int retval;
  535. if (EVP_PKEY_decrypt(pkctx, NULL, &eksize,
  536. encKey->data, encKey->length) <= 0
  537. || (ek = OPENSSL_malloc(eksize)) == NULL)
  538. goto end;
  539. retval = EVP_PKEY_decrypt(pkctx, ek, &eksize,
  540. encKey->data, encKey->length);
  541. ERR_clear_error(); /* error state may have sensitive information */
  542. failure = ~constant_time_is_zero_s(constant_time_msb(retval)
  543. | constant_time_is_zero(retval));
  544. failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize);
  545. if (failure) {
  546. ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
  547. goto end;
  548. }
  549. } else {
  550. goto end;
  551. }
  552. if ((iv = OPENSSL_malloc(EVP_CIPHER_iv_length(cipher))) == NULL)
  553. goto end;
  554. if (ASN1_TYPE_get_octetstring(ecert->symmAlg->parameter, iv,
  555. EVP_CIPHER_iv_length(cipher))
  556. != EVP_CIPHER_iv_length(cipher)) {
  557. ERR_raise(ERR_LIB_CRMF, CRMF_R_MALFORMED_IV);
  558. goto end;
  559. }
  560. /*
  561. * d2i_X509 changes the given pointer, so use p for decoding the message and
  562. * keep the original pointer in outbuf so the memory can be freed later
  563. */
  564. if ((p = outbuf = OPENSSL_malloc(ecert->encValue->length +
  565. EVP_CIPHER_block_size(cipher))) == NULL
  566. || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
  567. goto end;
  568. EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
  569. if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
  570. || !EVP_DecryptUpdate(evp_ctx, outbuf, &outlen,
  571. ecert->encValue->data,
  572. ecert->encValue->length)
  573. || !EVP_DecryptFinal(evp_ctx, outbuf + outlen, &n)) {
  574. ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_CERTIFICATE);
  575. goto end;
  576. }
  577. outlen += n;
  578. /* convert decrypted certificate from DER to internal ASN.1 structure */
  579. if ((cert = X509_new_ex(libctx, propq)) == NULL)
  580. goto end;
  581. if (d2i_X509(&cert, &p, outlen) == NULL)
  582. ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE);
  583. end:
  584. EVP_PKEY_CTX_free(pkctx);
  585. OPENSSL_free(outbuf);
  586. EVP_CIPHER_CTX_free(evp_ctx);
  587. OPENSSL_clear_free(ek, eksize);
  588. OPENSSL_free(iv);
  589. return cert;
  590. }