crmf_lib.c 24 KB

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