cmp_ctx.c 29 KB

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