cmp_ctx.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <openssl/trace.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/ocsp.h> /* for OCSP_REVOKED_STATUS_* */
  14. #include "cmp_local.h"
  15. /* explicit #includes not strictly needed since implied by the above: */
  16. #include <openssl/cmp.h>
  17. #include <openssl/crmf.h>
  18. #include <openssl/err.h>
  19. #define DEFINE_OSSL_CMP_CTX_get0(FIELD, TYPE) \
  20. DEFINE_OSSL_CMP_CTX_get0_NAME(FIELD, FIELD, TYPE)
  21. #define DEFINE_OSSL_CMP_CTX_get0_NAME(NAME, FIELD, TYPE) \
  22. TYPE *OSSL_CMP_CTX_get0_##NAME(const OSSL_CMP_CTX *ctx) \
  23. { \
  24. if (ctx == NULL) { \
  25. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  26. return NULL; \
  27. } \
  28. return ctx->FIELD; \
  29. }
  30. /*
  31. * Get current certificate store containing trusted root CA certs
  32. */
  33. DEFINE_OSSL_CMP_CTX_get0_NAME(trusted, trusted, X509_STORE)
  34. #define DEFINE_OSSL_set0(PREFIX, FIELD, TYPE) \
  35. DEFINE_OSSL_set0_NAME(PREFIX, FIELD, FIELD, TYPE)
  36. #define DEFINE_OSSL_set0_NAME(PREFIX, NAME, FIELD, TYPE) \
  37. int PREFIX##_set0##_##NAME(OSSL_CMP_CTX *ctx, TYPE *val) \
  38. { \
  39. if (ctx == NULL) { \
  40. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  41. return 0; \
  42. } \
  43. TYPE##_free(ctx->FIELD); \
  44. ctx->FIELD = val; \
  45. return 1; \
  46. }
  47. /*
  48. * Set certificate store containing trusted (root) CA certs and possibly CRLs
  49. * and a cert verification callback function used for CMP server authentication.
  50. * Any already existing store entry is freed. Given NULL, the entry is reset.
  51. */
  52. DEFINE_OSSL_set0_NAME(OSSL_CMP_CTX, trusted, trusted, X509_STORE)
  53. /* Get current list of non-trusted intermediate certs */
  54. DEFINE_OSSL_CMP_CTX_get0(untrusted, STACK_OF(X509))
  55. /*
  56. * Set untrusted certificates for path construction in authentication of
  57. * the CMP server and potentially others (TLS server, newly enrolled cert).
  58. */
  59. int OSSL_CMP_CTX_set1_untrusted(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
  60. {
  61. STACK_OF(X509) *untrusted = NULL;
  62. if (ctx == NULL) {
  63. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  64. return 0;
  65. }
  66. if (!ossl_x509_add_certs_new(&untrusted, certs,
  67. X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
  68. goto err;
  69. OSSL_STACK_OF_X509_free(ctx->untrusted);
  70. ctx->untrusted = untrusted;
  71. return 1;
  72. err:
  73. OSSL_STACK_OF_X509_free(untrusted);
  74. return 0;
  75. }
  76. static int cmp_ctx_set_md(OSSL_CMP_CTX *ctx, EVP_MD **pmd, int nid)
  77. {
  78. EVP_MD *md = EVP_MD_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propq);
  79. /* fetching in advance to be able to throw error early if unsupported */
  80. if (md == NULL) {
  81. ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_ALGORITHM);
  82. return 0;
  83. }
  84. EVP_MD_free(*pmd);
  85. *pmd = md;
  86. return 1;
  87. }
  88. /*
  89. * Allocates and initializes OSSL_CMP_CTX context structure with default values.
  90. * Returns new context on success, NULL on error
  91. */
  92. OSSL_CMP_CTX *OSSL_CMP_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
  93. {
  94. OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  95. if (ctx == NULL)
  96. goto err;
  97. ctx->libctx = libctx;
  98. if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)
  99. goto oom;
  100. ctx->log_verbosity = OSSL_CMP_LOG_INFO;
  101. ctx->status = -1;
  102. ctx->failInfoCode = -1;
  103. ctx->keep_alive = 1;
  104. ctx->msg_timeout = -1;
  105. if ((ctx->untrusted = sk_X509_new_null()) == NULL)
  106. goto oom;
  107. ctx->pbm_slen = 16;
  108. if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, NID_sha256))
  109. goto err;
  110. ctx->pbm_itercnt = 500;
  111. ctx->pbm_mac = NID_hmac_sha1;
  112. if (!cmp_ctx_set_md(ctx, &ctx->digest, NID_sha256))
  113. goto err;
  114. ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
  115. ctx->revocationReason = CRL_REASON_NONE;
  116. /* all other elements are initialized to 0 or NULL, respectively */
  117. return ctx;
  118. oom:
  119. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  120. err:
  121. OSSL_CMP_CTX_free(ctx);
  122. return NULL;
  123. }
  124. /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
  125. int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
  126. {
  127. if (ctx == NULL) {
  128. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  129. return 0;
  130. }
  131. if (ctx->http_ctx != NULL) {
  132. (void)OSSL_HTTP_close(ctx->http_ctx, 1);
  133. ossl_cmp_debug(ctx, "disconnected from CMP server");
  134. ctx->http_ctx = NULL;
  135. }
  136. ctx->status = -1;
  137. ctx->failInfoCode = -1;
  138. return ossl_cmp_ctx_set0_statusString(ctx, NULL)
  139. && ossl_cmp_ctx_set0_newCert(ctx, NULL)
  140. && ossl_cmp_ctx_set1_newChain(ctx, NULL)
  141. && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
  142. && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
  143. && ossl_cmp_ctx_set0_validatedSrvCert(ctx, NULL)
  144. && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
  145. && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
  146. && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
  147. }
  148. #define OSSL_CMP_ITAVs_free(itavs) \
  149. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  150. #define X509_EXTENSIONS_free(exts) \
  151. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free)
  152. #define OSSL_CMP_PKIFREETEXT_free(text) \
  153. sk_ASN1_UTF8STRING_pop_free(text, ASN1_UTF8STRING_free)
  154. /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
  155. void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
  156. {
  157. if (ctx == NULL)
  158. return;
  159. if (ctx->http_ctx != NULL) {
  160. (void)OSSL_HTTP_close(ctx->http_ctx, 1);
  161. ossl_cmp_debug(ctx, "disconnected from CMP server");
  162. }
  163. OPENSSL_free(ctx->propq);
  164. OPENSSL_free(ctx->serverPath);
  165. OPENSSL_free(ctx->server);
  166. OPENSSL_free(ctx->proxy);
  167. OPENSSL_free(ctx->no_proxy);
  168. X509_free(ctx->srvCert);
  169. X509_free(ctx->validatedSrvCert);
  170. X509_NAME_free(ctx->expected_sender);
  171. X509_STORE_free(ctx->trusted);
  172. OSSL_STACK_OF_X509_free(ctx->untrusted);
  173. X509_free(ctx->cert);
  174. OSSL_STACK_OF_X509_free(ctx->chain);
  175. EVP_PKEY_free(ctx->pkey);
  176. ASN1_OCTET_STRING_free(ctx->referenceValue);
  177. if (ctx->secretValue != NULL)
  178. OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
  179. ASN1_OCTET_STRING_free(ctx->secretValue);
  180. EVP_MD_free(ctx->pbm_owf);
  181. X509_NAME_free(ctx->recipient);
  182. EVP_MD_free(ctx->digest);
  183. ASN1_OCTET_STRING_free(ctx->transactionID);
  184. ASN1_OCTET_STRING_free(ctx->senderNonce);
  185. ASN1_OCTET_STRING_free(ctx->recipNonce);
  186. OSSL_CMP_ITAVs_free(ctx->geninfo_ITAVs);
  187. OSSL_STACK_OF_X509_free(ctx->extraCertsOut);
  188. EVP_PKEY_free(ctx->newPkey);
  189. X509_NAME_free(ctx->issuer);
  190. X509_NAME_free(ctx->subjectName);
  191. sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
  192. X509_EXTENSIONS_free(ctx->reqExtensions);
  193. sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
  194. X509_free(ctx->oldCert);
  195. X509_REQ_free(ctx->p10CSR);
  196. OSSL_CMP_ITAVs_free(ctx->genm_ITAVs);
  197. OSSL_CMP_PKIFREETEXT_free(ctx->statusString);
  198. X509_free(ctx->newCert);
  199. OSSL_STACK_OF_X509_free(ctx->newChain);
  200. OSSL_STACK_OF_X509_free(ctx->caPubs);
  201. OSSL_STACK_OF_X509_free(ctx->extraCertsIn);
  202. OPENSSL_free(ctx);
  203. }
  204. #define DEFINE_OSSL_set(PREFIX, FIELD, TYPE) \
  205. int PREFIX##_set_##FIELD(OSSL_CMP_CTX *ctx, TYPE val) \
  206. { \
  207. if (ctx == NULL) { \
  208. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  209. return 0; \
  210. } \
  211. ctx->FIELD = val; \
  212. return 1; \
  213. }
  214. DEFINE_OSSL_set(ossl_cmp_ctx, status, int)
  215. #define DEFINE_OSSL_get(PREFIX, FIELD, TYPE, ERR_RET) \
  216. TYPE PREFIX##_get_##FIELD(const OSSL_CMP_CTX *ctx) \
  217. { \
  218. if (ctx == NULL) { \
  219. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  220. return ERR_RET; \
  221. } \
  222. return ctx->FIELD; \
  223. }
  224. /*
  225. * Returns the PKIStatus from the last CertRepMessage
  226. * or Revocation Response or error message, -1 on error
  227. */
  228. DEFINE_OSSL_get(OSSL_CMP_CTX, status, int, -1)
  229. /*
  230. * Returns the statusString from the last CertRepMessage
  231. * or Revocation Response or error message, NULL on error
  232. */
  233. DEFINE_OSSL_CMP_CTX_get0(statusString, OSSL_CMP_PKIFREETEXT)
  234. DEFINE_OSSL_set0(ossl_cmp_ctx, statusString, OSSL_CMP_PKIFREETEXT)
  235. int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX *ctx, X509 *cert)
  236. {
  237. if (!ossl_assert(ctx != NULL))
  238. return 0;
  239. X509_free(ctx->validatedSrvCert);
  240. ctx->validatedSrvCert = cert;
  241. return 1;
  242. }
  243. /* Set callback function for checking if the cert is ok or should be rejected */
  244. DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb, OSSL_CMP_certConf_cb_t)
  245. /*
  246. * Set argument, respectively a pointer to a structure containing arguments,
  247. * optionally to be used by the certConf callback.
  248. */
  249. DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb_arg, void *)
  250. /*
  251. * Get argument, respectively the pointer to a structure containing arguments,
  252. * optionally to be used by certConf callback.
  253. * Returns callback argument set previously (NULL if not set or on error)
  254. */
  255. DEFINE_OSSL_get(OSSL_CMP_CTX, certConf_cb_arg, void *, NULL)
  256. #ifndef OPENSSL_NO_TRACE
  257. static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
  258. int category, int cmd, void *vdata)
  259. {
  260. OSSL_CMP_CTX *ctx = vdata;
  261. const char *msg;
  262. OSSL_CMP_severity level = -1;
  263. char *func = NULL;
  264. char *file = NULL;
  265. int line = 0;
  266. if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
  267. return 0;
  268. if (ctx->log_cb == NULL)
  269. return 1; /* silently drop message */
  270. msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
  271. if (level > ctx->log_verbosity) /* excludes the case level is unknown */
  272. goto end; /* suppress output since severity is not sufficient */
  273. if (!ctx->log_cb(func != NULL ? func : "(no func)",
  274. file != NULL ? file : "(no file)",
  275. line, level, msg))
  276. cnt = 0;
  277. end:
  278. OPENSSL_free(func);
  279. OPENSSL_free(file);
  280. return cnt;
  281. }
  282. #endif
  283. /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
  284. int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
  285. const char *func, const char *file, int line,
  286. const char *level_str, const char *format, ...)
  287. {
  288. va_list args;
  289. char hugebuf[1024 * 2];
  290. int res = 0;
  291. if (ctx == NULL || ctx->log_cb == NULL)
  292. return 1; /* silently drop message */
  293. if (level > ctx->log_verbosity) /* excludes the case level is unknown */
  294. return 1; /* suppress output since severity is not sufficient */
  295. if (format == NULL)
  296. return 0;
  297. va_start(args, format);
  298. if (func == NULL)
  299. func = "(unset function name)";
  300. if (file == NULL)
  301. file = "(unset file name)";
  302. if (level_str == NULL)
  303. level_str = "(unset level string)";
  304. #ifndef OPENSSL_NO_TRACE
  305. if (OSSL_TRACE_ENABLED(CMP)) {
  306. OSSL_TRACE_BEGIN(CMP) {
  307. int printed =
  308. BIO_snprintf(hugebuf, sizeof(hugebuf),
  309. "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
  310. func, file, line, level_str);
  311. if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
  312. if (BIO_vsnprintf(hugebuf + printed,
  313. sizeof(hugebuf) - printed, format, args) > 0)
  314. res = BIO_puts(trc_out, hugebuf) > 0;
  315. }
  316. } OSSL_TRACE_END(CMP);
  317. }
  318. #else /* compensate for disabled trace API */
  319. {
  320. if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
  321. res = ctx->log_cb(func, file, line, level, hugebuf);
  322. }
  323. #endif
  324. va_end(args);
  325. return res;
  326. }
  327. /* Set a callback function for error reporting and logging messages */
  328. int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
  329. {
  330. if (ctx == NULL) {
  331. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  332. return 0;
  333. }
  334. ctx->log_cb = cb;
  335. #ifndef OPENSSL_NO_TRACE
  336. /* do also in case cb == NULL, to switch off logging output: */
  337. if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
  338. ossl_cmp_log_trace_cb, ctx))
  339. return 0;
  340. #endif
  341. return 1;
  342. }
  343. /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
  344. void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX *ctx)
  345. {
  346. if (ctx != NULL && OSSL_CMP_LOG_ERR > ctx->log_verbosity)
  347. return; /* suppress output since severity is not sufficient */
  348. OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
  349. }
  350. /*
  351. * Set or clear the reference value to be used for identification
  352. * (i.e., the user name) when using PBMAC.
  353. */
  354. int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
  355. const unsigned char *ref, int len)
  356. {
  357. if (ctx == NULL) {
  358. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  359. return 0;
  360. }
  361. return
  362. ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref, len);
  363. }
  364. /* Set or clear the password to be used for protecting messages with PBMAC */
  365. int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
  366. int len)
  367. {
  368. ASN1_OCTET_STRING *secretValue = NULL;
  369. if (ctx == NULL) {
  370. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  371. return 0;
  372. }
  373. if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
  374. return 0;
  375. if (ctx->secretValue != NULL) {
  376. OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
  377. ASN1_OCTET_STRING_free(ctx->secretValue);
  378. }
  379. ctx->secretValue = secretValue;
  380. return 1;
  381. }
  382. #define DEFINE_OSSL_CMP_CTX_get1_certs(FIELD) \
  383. STACK_OF(X509) *OSSL_CMP_CTX_get1_##FIELD(const OSSL_CMP_CTX *ctx) \
  384. { \
  385. if (ctx == NULL) { \
  386. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  387. return NULL; \
  388. } \
  389. return X509_chain_up_ref(ctx->FIELD); \
  390. }
  391. /* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
  392. DEFINE_OSSL_CMP_CTX_get1_certs(newChain)
  393. #define DEFINE_OSSL_set1_certs(PREFIX, FIELD) \
  394. int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs) \
  395. { \
  396. if (ctx == NULL) { \
  397. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  398. return 0; \
  399. } \
  400. OSSL_STACK_OF_X509_free(ctx->FIELD); \
  401. ctx->FIELD = NULL; \
  402. return certs == NULL || (ctx->FIELD = X509_chain_up_ref(certs)) != NULL; \
  403. }
  404. /*
  405. * Copies any given stack of inbound X509 certificates to newChain
  406. * of the OSSL_CMP_CTX structure so that they may be retrieved later.
  407. */
  408. DEFINE_OSSL_set1_certs(ossl_cmp_ctx, newChain)
  409. /* Returns the stack of extraCerts received in CertRepMessage, NULL on error */
  410. DEFINE_OSSL_CMP_CTX_get1_certs(extraCertsIn)
  411. /*
  412. * Copies any given stack of inbound X509 certificates to extraCertsIn
  413. * of the OSSL_CMP_CTX structure so that they may be retrieved later.
  414. */
  415. DEFINE_OSSL_set1_certs(ossl_cmp_ctx, extraCertsIn)
  416. /*
  417. * Copies any given stack as the new stack of X509
  418. * certificates to send out in the extraCerts field.
  419. */
  420. DEFINE_OSSL_set1_certs(OSSL_CMP_CTX, extraCertsOut)
  421. /*
  422. * Add the given policy info object
  423. * to the X509_EXTENSIONS of the requested certificate template.
  424. */
  425. int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
  426. {
  427. if (ctx == NULL || pinfo == NULL) {
  428. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  429. return 0;
  430. }
  431. if (ctx->policies == NULL
  432. && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
  433. return 0;
  434. return sk_POLICYINFO_push(ctx->policies, pinfo);
  435. }
  436. /* Add an ITAV for geninfo of the PKI message header */
  437. int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
  438. {
  439. if (ctx == NULL) {
  440. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  441. return 0;
  442. }
  443. return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
  444. }
  445. /* Add an itav for the body of outgoing general messages */
  446. int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
  447. {
  448. if (ctx == NULL) {
  449. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  450. return 0;
  451. }
  452. return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
  453. }
  454. /*
  455. * Returns a duplicate of the stack of X509 certificates that
  456. * were received in the caPubs field of the last CertRepMessage.
  457. * Returns NULL on error
  458. */
  459. DEFINE_OSSL_CMP_CTX_get1_certs(caPubs)
  460. /*
  461. * Copies any given stack of certificates to the given
  462. * OSSL_CMP_CTX structure so that they may be retrieved later.
  463. */
  464. DEFINE_OSSL_set1_certs(ossl_cmp_ctx, caPubs)
  465. #define char_dup OPENSSL_strdup
  466. #define char_free OPENSSL_free
  467. #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
  468. int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
  469. { \
  470. TYPE *val_dup = NULL; \
  471. \
  472. if (ctx == NULL) { \
  473. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  474. return 0; \
  475. } \
  476. \
  477. if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
  478. return 0; \
  479. TYPE##_free(ctx->FIELD); \
  480. ctx->FIELD = val_dup; \
  481. return 1; \
  482. }
  483. #define X509_invalid(cert) (!ossl_x509v3_cache_extensions(cert))
  484. #define EVP_PKEY_invalid(key) 0
  485. #define DEFINE_OSSL_set1_up_ref(PREFIX, FIELD, TYPE) \
  486. int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
  487. { \
  488. if (ctx == NULL) { \
  489. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  490. return 0; \
  491. } \
  492. \
  493. /* prevent misleading error later on malformed cert or provider issue */ \
  494. if (val != NULL && TYPE##_invalid(val)) { \
  495. ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); \
  496. return 0; \
  497. } \
  498. if (val != NULL && !TYPE##_up_ref(val)) \
  499. return 0; \
  500. TYPE##_free(ctx->FIELD); \
  501. ctx->FIELD = val; \
  502. return 1; \
  503. }
  504. /*
  505. * Pins the server certificate to be directly trusted (even if it is expired)
  506. * for verifying response messages.
  507. * Cert pointer is not consumed. It may be NULL to clear the entry.
  508. */
  509. DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, srvCert, X509)
  510. /* Set the X509 name of the recipient. Set in the PKIHeader */
  511. DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
  512. /* Store the X509 name of the expected sender in the PKIHeader of responses */
  513. DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
  514. /* Set the X509 name of the issuer. Set in the PKIHeader */
  515. DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
  516. /*
  517. * Set the subject name that will be placed in the certificate
  518. * request. This will be the subject name on the received certificate.
  519. */
  520. DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
  521. /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
  522. int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
  523. {
  524. if (ctx == NULL) {
  525. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  526. return 0;
  527. }
  528. if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
  529. && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
  530. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
  531. return 0;
  532. }
  533. X509_EXTENSIONS_free(ctx->reqExtensions);
  534. ctx->reqExtensions = exts;
  535. return 1;
  536. }
  537. /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
  538. int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
  539. {
  540. if (ctx == NULL) {
  541. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  542. return -1;
  543. }
  544. /* if one of the following conditions 'fail' this is not an error */
  545. return ctx->reqExtensions != NULL
  546. && X509v3_get_ext_by_NID(ctx->reqExtensions,
  547. NID_subject_alt_name, -1) >= 0;
  548. }
  549. /*
  550. * Add a GENERAL_NAME structure that will be added to the CRMF
  551. * request's extensions field to request subject alternative names.
  552. */
  553. int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
  554. const GENERAL_NAME *name)
  555. {
  556. GENERAL_NAME *name_dup;
  557. if (ctx == NULL || name == NULL) {
  558. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  559. return 0;
  560. }
  561. if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
  562. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
  563. return 0;
  564. }
  565. if (ctx->subjectAltNames == NULL
  566. && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
  567. return 0;
  568. if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
  569. return 0;
  570. if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
  571. GENERAL_NAME_free(name_dup);
  572. return 0;
  573. }
  574. return 1;
  575. }
  576. /*
  577. * Set our own client certificate, used for example in KUR and when
  578. * doing the IR with existing certificate.
  579. */
  580. DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, cert, X509)
  581. int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX *ctx, X509_STORE *own_trusted,
  582. STACK_OF(X509) *candidates)
  583. {
  584. STACK_OF(X509) *chain;
  585. if (ctx == NULL) {
  586. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  587. return 0;
  588. }
  589. if (!ossl_x509_add_certs_new(&ctx->untrusted, candidates,
  590. X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
  591. return 0;
  592. ossl_cmp_debug(ctx, "trying to build chain for own CMP signer cert");
  593. chain = X509_build_chain(ctx->cert, ctx->untrusted, own_trusted, 0,
  594. ctx->libctx, ctx->propq);
  595. if (chain == NULL) {
  596. ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_BUILDING_OWN_CHAIN);
  597. return 0;
  598. }
  599. ossl_cmp_debug(ctx, "success building chain for own CMP signer cert");
  600. ctx->chain = chain;
  601. return 1;
  602. }
  603. /*
  604. * Set the old certificate that we are updating in KUR
  605. * or the certificate to be revoked in RR, respectively.
  606. * Also used as reference cert (defaulting to cert) for deriving subject DN
  607. * and SANs. Its issuer is used as default recipient in the CMP message header.
  608. */
  609. DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, oldCert, X509)
  610. /* Set the PKCS#10 CSR to be sent in P10CR */
  611. DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
  612. /*
  613. * Set the (newly received in IP/KUP/CP) certificate in the context.
  614. * This only permits for one cert to be enrolled at a time.
  615. */
  616. DEFINE_OSSL_set0(ossl_cmp_ctx, newCert, X509)
  617. /*
  618. * Get the (newly received in IP/KUP/CP) client certificate from the context
  619. * This only permits for one client cert to be received...
  620. */
  621. DEFINE_OSSL_CMP_CTX_get0(newCert, X509)
  622. /* Set the client's current private key */
  623. DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, pkey, EVP_PKEY)
  624. /* Set new key pair. Used e.g. when doing Key Update */
  625. int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
  626. {
  627. if (ctx == NULL) {
  628. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  629. return 0;
  630. }
  631. EVP_PKEY_free(ctx->newPkey);
  632. ctx->newPkey = pkey;
  633. ctx->newPkey_priv = priv;
  634. return 1;
  635. }
  636. /* Get the private/public key to use for cert enrollment, or NULL on error */
  637. EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
  638. {
  639. if (ctx == NULL) {
  640. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  641. return NULL;
  642. }
  643. if (ctx->newPkey != NULL)
  644. return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
  645. if (ctx->p10CSR != NULL)
  646. return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
  647. return ctx->pkey; /* may be NULL */
  648. }
  649. #define DEFINE_set1_ASN1_OCTET_STRING(PREFIX, FIELD) \
  650. int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, const ASN1_OCTET_STRING *id) \
  651. { \
  652. if (ctx == NULL) { \
  653. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  654. return 0; \
  655. } \
  656. return ossl_cmp_asn1_octet_string_set1(&ctx->FIELD, id); \
  657. }
  658. /* Set the given transactionID to the context */
  659. DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, transactionID)
  660. /* Set the nonce to be used for the recipNonce in the message created next */
  661. DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx, recipNonce)
  662. /* Stores the given nonce as the last senderNonce sent out */
  663. DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, senderNonce)
  664. /* Set the proxy server to use for HTTP(S) connections */
  665. DEFINE_OSSL_CMP_CTX_set1(proxy, char)
  666. /* Set the (HTTP) host name of the CMP server */
  667. DEFINE_OSSL_CMP_CTX_set1(server, char)
  668. /* Set the server exclusion list of the HTTP proxy server */
  669. DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
  670. /* Set the http connect/disconnect callback function to be used for HTTP(S) */
  671. DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb, OSSL_HTTP_bio_cb_t)
  672. /* Set argument optionally to be used by the http connect/disconnect callback */
  673. DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb_arg, void *)
  674. /*
  675. * Get argument optionally to be used by the http connect/disconnect callback
  676. * Returns callback argument set previously (NULL if not set or on error)
  677. */
  678. DEFINE_OSSL_get(OSSL_CMP_CTX, http_cb_arg, void *, NULL)
  679. /* Set callback function for sending CMP request and receiving response */
  680. DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb, OSSL_CMP_transfer_cb_t)
  681. /* Set argument optionally to be used by the transfer callback */
  682. DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb_arg, void *)
  683. /*
  684. * Get argument optionally to be used by the transfer callback.
  685. * Returns callback argument set previously (NULL if not set or on error)
  686. */
  687. DEFINE_OSSL_get(OSSL_CMP_CTX, transfer_cb_arg, void *, NULL)
  688. /** Set the HTTP server port to be used */
  689. DEFINE_OSSL_set(OSSL_CMP_CTX, serverPort, int)
  690. /* Set the HTTP path to be used on the server (e.g "pkix/") */
  691. DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
  692. /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
  693. DEFINE_OSSL_set(ossl_cmp_ctx, failInfoCode, int)
  694. /*
  695. * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
  696. * Returns bit string as integer on success, -1 on error
  697. */
  698. DEFINE_OSSL_get(OSSL_CMP_CTX, failInfoCode, int, -1)
  699. /* Set a Boolean or integer option of the context to the "val" arg */
  700. int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
  701. {
  702. int min_val;
  703. if (ctx == NULL) {
  704. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  705. return 0;
  706. }
  707. switch (opt) {
  708. case OSSL_CMP_OPT_REVOCATION_REASON:
  709. min_val = OCSP_REVOKED_STATUS_NOSTATUS;
  710. break;
  711. case OSSL_CMP_OPT_POPO_METHOD:
  712. min_val = OSSL_CRMF_POPO_NONE;
  713. break;
  714. default:
  715. min_val = 0;
  716. break;
  717. }
  718. if (val < min_val) {
  719. ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_SMALL);
  720. return 0;
  721. }
  722. switch (opt) {
  723. case OSSL_CMP_OPT_LOG_VERBOSITY:
  724. if (val > OSSL_CMP_LOG_MAX) {
  725. ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
  726. return 0;
  727. }
  728. ctx->log_verbosity = val;
  729. break;
  730. case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
  731. ctx->implicitConfirm = val;
  732. break;
  733. case OSSL_CMP_OPT_DISABLE_CONFIRM:
  734. ctx->disableConfirm = val;
  735. break;
  736. case OSSL_CMP_OPT_UNPROTECTED_SEND:
  737. ctx->unprotectedSend = val;
  738. break;
  739. case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
  740. ctx->unprotectedErrors = val;
  741. break;
  742. case OSSL_CMP_OPT_VALIDITY_DAYS:
  743. ctx->days = val;
  744. break;
  745. case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
  746. ctx->SubjectAltName_nodefault = val;
  747. break;
  748. case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
  749. ctx->setSubjectAltNameCritical = val;
  750. break;
  751. case OSSL_CMP_OPT_POLICIES_CRITICAL:
  752. ctx->setPoliciesCritical = val;
  753. break;
  754. case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
  755. ctx->ignore_keyusage = val;
  756. break;
  757. case OSSL_CMP_OPT_POPO_METHOD:
  758. if (val > OSSL_CRMF_POPO_KEYAGREE) {
  759. ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
  760. return 0;
  761. }
  762. ctx->popoMethod = val;
  763. break;
  764. case OSSL_CMP_OPT_DIGEST_ALGNID:
  765. if (!cmp_ctx_set_md(ctx, &ctx->digest, val))
  766. return 0;
  767. break;
  768. case OSSL_CMP_OPT_OWF_ALGNID:
  769. if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, val))
  770. return 0;
  771. break;
  772. case OSSL_CMP_OPT_MAC_ALGNID:
  773. ctx->pbm_mac = val;
  774. break;
  775. case OSSL_CMP_OPT_KEEP_ALIVE:
  776. ctx->keep_alive = val;
  777. break;
  778. case OSSL_CMP_OPT_MSG_TIMEOUT:
  779. ctx->msg_timeout = val;
  780. break;
  781. case OSSL_CMP_OPT_TOTAL_TIMEOUT:
  782. ctx->total_timeout = val;
  783. break;
  784. case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
  785. ctx->permitTAInExtraCertsForIR = val;
  786. break;
  787. case OSSL_CMP_OPT_REVOCATION_REASON:
  788. if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
  789. ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
  790. return 0;
  791. }
  792. ctx->revocationReason = val;
  793. break;
  794. default:
  795. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);
  796. return 0;
  797. }
  798. return 1;
  799. }
  800. /*
  801. * Reads a Boolean or integer option value from the context.
  802. * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
  803. */
  804. int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
  805. {
  806. if (ctx == NULL) {
  807. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  808. return -1;
  809. }
  810. switch (opt) {
  811. case OSSL_CMP_OPT_LOG_VERBOSITY:
  812. return ctx->log_verbosity;
  813. case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
  814. return ctx->implicitConfirm;
  815. case OSSL_CMP_OPT_DISABLE_CONFIRM:
  816. return ctx->disableConfirm;
  817. case OSSL_CMP_OPT_UNPROTECTED_SEND:
  818. return ctx->unprotectedSend;
  819. case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
  820. return ctx->unprotectedErrors;
  821. case OSSL_CMP_OPT_VALIDITY_DAYS:
  822. return ctx->days;
  823. case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
  824. return ctx->SubjectAltName_nodefault;
  825. case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
  826. return ctx->setSubjectAltNameCritical;
  827. case OSSL_CMP_OPT_POLICIES_CRITICAL:
  828. return ctx->setPoliciesCritical;
  829. case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
  830. return ctx->ignore_keyusage;
  831. case OSSL_CMP_OPT_POPO_METHOD:
  832. return ctx->popoMethod;
  833. case OSSL_CMP_OPT_DIGEST_ALGNID:
  834. return EVP_MD_get_type(ctx->digest);
  835. case OSSL_CMP_OPT_OWF_ALGNID:
  836. return EVP_MD_get_type(ctx->pbm_owf);
  837. case OSSL_CMP_OPT_MAC_ALGNID:
  838. return ctx->pbm_mac;
  839. case OSSL_CMP_OPT_KEEP_ALIVE:
  840. return ctx->keep_alive;
  841. case OSSL_CMP_OPT_MSG_TIMEOUT:
  842. return ctx->msg_timeout;
  843. case OSSL_CMP_OPT_TOTAL_TIMEOUT:
  844. return ctx->total_timeout;
  845. case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
  846. return ctx->permitTAInExtraCertsForIR;
  847. case OSSL_CMP_OPT_REVOCATION_REASON:
  848. return ctx->revocationReason;
  849. default:
  850. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);
  851. return -1;
  852. }
  853. }