cmp_ctx.c 30 KB

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