2
0

cmp_ctx.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 err;
  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. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  107. goto err;
  108. }
  109. ctx->pbm_slen = 16;
  110. if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, NID_sha256))
  111. goto err;
  112. ctx->pbm_itercnt = 500;
  113. ctx->pbm_mac = NID_hmac_sha1;
  114. if (!cmp_ctx_set_md(ctx, &ctx->digest, NID_sha256))
  115. goto err;
  116. ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
  117. ctx->revocationReason = CRL_REASON_NONE;
  118. /* all other elements are initialized to 0 or NULL, respectively */
  119. return ctx;
  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_set1_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. /* Set callback function for checking if the cert is ok or should be rejected */
  236. DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb, OSSL_CMP_certConf_cb_t)
  237. /*
  238. * Set argument, respectively a pointer to a structure containing arguments,
  239. * optionally to be used by the certConf callback.
  240. */
  241. DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb_arg, void *)
  242. /*
  243. * Get argument, respectively the pointer to a structure containing arguments,
  244. * optionally to be used by certConf callback.
  245. * Returns callback argument set previously (NULL if not set or on error)
  246. */
  247. DEFINE_OSSL_get(OSSL_CMP_CTX, certConf_cb_arg, void *, NULL)
  248. #ifndef OPENSSL_NO_TRACE
  249. static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
  250. int category, int cmd, void *vdata)
  251. {
  252. OSSL_CMP_CTX *ctx = vdata;
  253. const char *msg;
  254. OSSL_CMP_severity level = -1;
  255. char *func = NULL;
  256. char *file = NULL;
  257. int line = 0;
  258. if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
  259. return 0;
  260. if (ctx->log_cb == NULL)
  261. return 1; /* silently drop message */
  262. msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
  263. if (level > ctx->log_verbosity) /* excludes the case level is unknown */
  264. goto end; /* suppress output since severity is not sufficient */
  265. if (!ctx->log_cb(func != NULL ? func : "(no func)",
  266. file != NULL ? file : "(no file)",
  267. line, level, msg))
  268. cnt = 0;
  269. end:
  270. OPENSSL_free(func);
  271. OPENSSL_free(file);
  272. return cnt;
  273. }
  274. #endif
  275. /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
  276. int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
  277. const char *func, const char *file, int line,
  278. const char *level_str, const char *format, ...)
  279. {
  280. va_list args;
  281. char hugebuf[1024 * 2];
  282. int res = 0;
  283. if (ctx == NULL || ctx->log_cb == NULL)
  284. return 1; /* silently drop message */
  285. if (level > ctx->log_verbosity) /* excludes the case level is unknown */
  286. return 1; /* suppress output since severity is not sufficient */
  287. if (format == NULL)
  288. return 0;
  289. va_start(args, format);
  290. if (func == NULL)
  291. func = "(unset function name)";
  292. if (file == NULL)
  293. file = "(unset file name)";
  294. if (level_str == NULL)
  295. level_str = "(unset level string)";
  296. #ifndef OPENSSL_NO_TRACE
  297. if (OSSL_TRACE_ENABLED(CMP)) {
  298. OSSL_TRACE_BEGIN(CMP) {
  299. int printed =
  300. BIO_snprintf(hugebuf, sizeof(hugebuf),
  301. "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
  302. func, file, line, level_str);
  303. if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
  304. if (BIO_vsnprintf(hugebuf + printed,
  305. sizeof(hugebuf) - printed, format, args) > 0)
  306. res = BIO_puts(trc_out, hugebuf) > 0;
  307. }
  308. } OSSL_TRACE_END(CMP);
  309. }
  310. #else /* compensate for disabled trace API */
  311. {
  312. if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
  313. res = ctx->log_cb(func, file, line, level, hugebuf);
  314. }
  315. #endif
  316. va_end(args);
  317. return res;
  318. }
  319. /* Set a callback function for error reporting and logging messages */
  320. int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
  321. {
  322. if (ctx == NULL) {
  323. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  324. return 0;
  325. }
  326. ctx->log_cb = cb;
  327. #ifndef OPENSSL_NO_TRACE
  328. /* do also in case cb == NULL, to switch off logging output: */
  329. if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
  330. ossl_cmp_log_trace_cb, ctx))
  331. return 0;
  332. #endif
  333. return 1;
  334. }
  335. /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
  336. void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX *ctx)
  337. {
  338. if (ctx != NULL && OSSL_CMP_LOG_ERR > ctx->log_verbosity)
  339. return; /* suppress output since severity is not sufficient */
  340. OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
  341. }
  342. /*
  343. * Set or clear the reference value to be used for identification
  344. * (i.e., the user name) when using PBMAC.
  345. */
  346. int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
  347. const unsigned char *ref, int len)
  348. {
  349. if (ctx == NULL) {
  350. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  351. return 0;
  352. }
  353. return
  354. ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref, len);
  355. }
  356. /* Set or clear the password to be used for protecting messages with PBMAC */
  357. int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
  358. int len)
  359. {
  360. ASN1_OCTET_STRING *secretValue = NULL;
  361. if (ctx == NULL) {
  362. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  363. return 0;
  364. }
  365. if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
  366. return 0;
  367. if (ctx->secretValue != NULL) {
  368. OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
  369. ASN1_OCTET_STRING_free(ctx->secretValue);
  370. }
  371. ctx->secretValue = secretValue;
  372. return 1;
  373. }
  374. #define DEFINE_OSSL_CMP_CTX_get1_certs(FIELD) \
  375. STACK_OF(X509) *OSSL_CMP_CTX_get1_##FIELD(const OSSL_CMP_CTX *ctx) \
  376. { \
  377. if (ctx == NULL) { \
  378. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  379. return NULL; \
  380. } \
  381. return X509_chain_up_ref(ctx->FIELD); \
  382. }
  383. /* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
  384. DEFINE_OSSL_CMP_CTX_get1_certs(newChain)
  385. #define DEFINE_OSSL_set1_certs(PREFIX, FIELD) \
  386. int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs) \
  387. { \
  388. if (ctx == NULL) { \
  389. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  390. return 0; \
  391. } \
  392. OSSL_STACK_OF_X509_free(ctx->FIELD); \
  393. ctx->FIELD = NULL; \
  394. return certs == NULL || (ctx->FIELD = X509_chain_up_ref(certs)) != NULL; \
  395. }
  396. /*
  397. * Copies any given stack of inbound X509 certificates to newChain
  398. * of the OSSL_CMP_CTX structure so that they may be retrieved later.
  399. */
  400. DEFINE_OSSL_set1_certs(ossl_cmp_ctx, newChain)
  401. /* Returns the stack of extraCerts received in CertRepMessage, NULL on error */
  402. DEFINE_OSSL_CMP_CTX_get1_certs(extraCertsIn)
  403. /*
  404. * Copies any given stack of inbound X509 certificates to extraCertsIn
  405. * of the OSSL_CMP_CTX structure so that they may be retrieved later.
  406. */
  407. DEFINE_OSSL_set1_certs(ossl_cmp_ctx, extraCertsIn)
  408. /*
  409. * Copies any given stack as the new stack of X509
  410. * certificates to send out in the extraCerts field.
  411. */
  412. DEFINE_OSSL_set1_certs(OSSL_CMP_CTX, extraCertsOut)
  413. /*
  414. * Add the given policy info object
  415. * to the X509_EXTENSIONS of the requested certificate template.
  416. */
  417. int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
  418. {
  419. if (ctx == NULL || pinfo == NULL) {
  420. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  421. return 0;
  422. }
  423. if (ctx->policies == NULL
  424. && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
  425. return 0;
  426. return sk_POLICYINFO_push(ctx->policies, pinfo);
  427. }
  428. /* Add an ITAV for geninfo of the PKI message header */
  429. int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
  430. {
  431. if (ctx == NULL) {
  432. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  433. return 0;
  434. }
  435. return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
  436. }
  437. /* Add an itav for the body of outgoing general messages */
  438. int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
  439. {
  440. if (ctx == NULL) {
  441. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  442. return 0;
  443. }
  444. return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
  445. }
  446. /*
  447. * Returns a duplicate of the stack of X509 certificates that
  448. * were received in the caPubs field of the last CertRepMessage.
  449. * Returns NULL on error
  450. */
  451. DEFINE_OSSL_CMP_CTX_get1_certs(caPubs)
  452. /*
  453. * Copies any given stack of certificates to the given
  454. * OSSL_CMP_CTX structure so that they may be retrieved later.
  455. */
  456. DEFINE_OSSL_set1_certs(ossl_cmp_ctx, caPubs)
  457. #define char_dup OPENSSL_strdup
  458. #define char_free OPENSSL_free
  459. #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
  460. int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
  461. { \
  462. TYPE *val_dup = NULL; \
  463. \
  464. if (ctx == NULL) { \
  465. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  466. return 0; \
  467. } \
  468. \
  469. if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
  470. return 0; \
  471. TYPE##_free(ctx->FIELD); \
  472. ctx->FIELD = val_dup; \
  473. return 1; \
  474. }
  475. #define X509_invalid(cert) (!ossl_x509v3_cache_extensions(cert))
  476. #define EVP_PKEY_invalid(key) 0
  477. #define DEFINE_OSSL_set1_up_ref(PREFIX, FIELD, TYPE) \
  478. int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
  479. { \
  480. if (ctx == NULL) { \
  481. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  482. return 0; \
  483. } \
  484. \
  485. /* prevent misleading error later on malformed cert or provider issue */ \
  486. if (val != NULL && TYPE##_invalid(val)) { \
  487. ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); \
  488. return 0; \
  489. } \
  490. if (val != NULL && !TYPE##_up_ref(val)) \
  491. return 0; \
  492. TYPE##_free(ctx->FIELD); \
  493. ctx->FIELD = val; \
  494. return 1; \
  495. }
  496. DEFINE_OSSL_set1_up_ref(ossl_cmp_ctx, validatedSrvCert, X509)
  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. */
  502. DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, srvCert, X509)
  503. /* Set the X509 name of the recipient. Set in the PKIHeader */
  504. DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
  505. /* Store the X509 name of the expected sender in the PKIHeader of responses */
  506. DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
  507. /* Set the X509 name of the issuer. Set in the PKIHeader */
  508. DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
  509. /*
  510. * Set the subject name that will be placed in the certificate
  511. * request. This will be the subject name on the received certificate.
  512. */
  513. DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
  514. /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
  515. int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
  516. {
  517. if (ctx == NULL) {
  518. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  519. return 0;
  520. }
  521. if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
  522. && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
  523. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
  524. return 0;
  525. }
  526. X509_EXTENSIONS_free(ctx->reqExtensions);
  527. ctx->reqExtensions = exts;
  528. return 1;
  529. }
  530. /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
  531. int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
  532. {
  533. if (ctx == NULL) {
  534. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  535. return -1;
  536. }
  537. /* if one of the following conditions 'fail' this is not an error */
  538. return ctx->reqExtensions != NULL
  539. && X509v3_get_ext_by_NID(ctx->reqExtensions,
  540. NID_subject_alt_name, -1) >= 0;
  541. }
  542. /*
  543. * Add a GENERAL_NAME structure that will be added to the CRMF
  544. * request's extensions field to request subject alternative names.
  545. */
  546. int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
  547. const GENERAL_NAME *name)
  548. {
  549. GENERAL_NAME *name_dup;
  550. if (ctx == NULL || name == NULL) {
  551. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  552. return 0;
  553. }
  554. if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
  555. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
  556. return 0;
  557. }
  558. if (ctx->subjectAltNames == NULL
  559. && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
  560. return 0;
  561. if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
  562. return 0;
  563. if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
  564. GENERAL_NAME_free(name_dup);
  565. return 0;
  566. }
  567. return 1;
  568. }
  569. /*
  570. * Set our own client certificate, used for example in KUR and when
  571. * doing the IR with existing certificate.
  572. */
  573. DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, cert, X509)
  574. int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX *ctx, X509_STORE *own_trusted,
  575. STACK_OF(X509) *candidates)
  576. {
  577. STACK_OF(X509) *chain;
  578. if (ctx == NULL) {
  579. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  580. return 0;
  581. }
  582. if (!ossl_x509_add_certs_new(&ctx->untrusted, candidates,
  583. X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
  584. return 0;
  585. ossl_cmp_debug(ctx, "trying to build chain for own CMP signer cert");
  586. chain = X509_build_chain(ctx->cert, ctx->untrusted, own_trusted, 0,
  587. ctx->libctx, ctx->propq);
  588. if (chain == NULL) {
  589. ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_BUILDING_OWN_CHAIN);
  590. return 0;
  591. }
  592. ossl_cmp_debug(ctx, "success building chain for own CMP signer cert");
  593. ctx->chain = chain;
  594. return 1;
  595. }
  596. /*
  597. * Set the old certificate that we are updating in KUR
  598. * or the certificate to be revoked in RR, respectively.
  599. * Also used as reference cert (defaulting to cert) for deriving subject DN
  600. * and SANs. Its issuer is used as default recipient in the CMP message header.
  601. */
  602. DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, oldCert, X509)
  603. /* Set the PKCS#10 CSR to be sent in P10CR */
  604. DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
  605. /*
  606. * Set the (newly received in IP/KUP/CP) certificate in the context.
  607. * This only permits for one cert to be enrolled at a time.
  608. */
  609. DEFINE_OSSL_set0(ossl_cmp_ctx, newCert, X509)
  610. /* Get successfully validated server cert, if any, of current transaction */
  611. DEFINE_OSSL_CMP_CTX_get0(validatedSrvCert, X509)
  612. /*
  613. * Get the (newly received in IP/KUP/CP) client certificate from the context
  614. * This only permits for one client cert to be received...
  615. */
  616. DEFINE_OSSL_CMP_CTX_get0(newCert, X509)
  617. /* Set the client's current private key */
  618. DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, pkey, EVP_PKEY)
  619. /* Set new key pair. Used e.g. when doing Key Update */
  620. int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
  621. {
  622. if (ctx == NULL) {
  623. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  624. return 0;
  625. }
  626. EVP_PKEY_free(ctx->newPkey);
  627. ctx->newPkey = pkey;
  628. ctx->newPkey_priv = priv;
  629. return 1;
  630. }
  631. /* Get the private/public key to use for cert enrollment, or NULL on error */
  632. EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
  633. {
  634. if (ctx == NULL) {
  635. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  636. return NULL;
  637. }
  638. if (ctx->newPkey != NULL)
  639. return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
  640. if (ctx->p10CSR != NULL)
  641. return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
  642. return ctx->pkey; /* may be NULL */
  643. }
  644. #define DEFINE_set1_ASN1_OCTET_STRING(PREFIX, FIELD) \
  645. int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, const ASN1_OCTET_STRING *id) \
  646. { \
  647. if (ctx == NULL) { \
  648. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  649. return 0; \
  650. } \
  651. return ossl_cmp_asn1_octet_string_set1(&ctx->FIELD, id); \
  652. }
  653. /* Set the given transactionID to the context */
  654. DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, transactionID)
  655. /* Set the nonce to be used for the recipNonce in the message created next */
  656. DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx, recipNonce)
  657. /* Stores the given nonce as the last senderNonce sent out */
  658. DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, senderNonce)
  659. /* Set the proxy server to use for HTTP(S) connections */
  660. DEFINE_OSSL_CMP_CTX_set1(proxy, char)
  661. /* Set the (HTTP) hostname of the CMP server */
  662. DEFINE_OSSL_CMP_CTX_set1(server, char)
  663. /* Set the server exclusion list of the HTTP proxy server */
  664. DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
  665. /* Set the http connect/disconnect callback function to be used for HTTP(S) */
  666. DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb, OSSL_HTTP_bio_cb_t)
  667. /* Set argument optionally to be used by the http connect/disconnect callback */
  668. DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb_arg, void *)
  669. /*
  670. * Get argument optionally to be used by the http connect/disconnect callback
  671. * Returns callback argument set previously (NULL if not set or on error)
  672. */
  673. DEFINE_OSSL_get(OSSL_CMP_CTX, http_cb_arg, void *, NULL)
  674. /* Set callback function for sending CMP request and receiving response */
  675. DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb, OSSL_CMP_transfer_cb_t)
  676. /* Set argument optionally to be used by the transfer callback */
  677. DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb_arg, void *)
  678. /*
  679. * Get argument optionally to be used by the transfer callback.
  680. * Returns callback argument set previously (NULL if not set or on error)
  681. */
  682. DEFINE_OSSL_get(OSSL_CMP_CTX, transfer_cb_arg, void *, NULL)
  683. /** Set the HTTP server port to be used */
  684. DEFINE_OSSL_set(OSSL_CMP_CTX, serverPort, int)
  685. /* Set the HTTP path to be used on the server (e.g "pkix/") */
  686. DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
  687. /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
  688. DEFINE_OSSL_set(ossl_cmp_ctx, failInfoCode, int)
  689. /*
  690. * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
  691. * Returns bit string as integer on success, -1 on error
  692. */
  693. DEFINE_OSSL_get(OSSL_CMP_CTX, failInfoCode, int, -1)
  694. /* Set a Boolean or integer option of the context to the "val" arg */
  695. int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
  696. {
  697. int min_val;
  698. if (ctx == NULL) {
  699. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  700. return 0;
  701. }
  702. switch (opt) {
  703. case OSSL_CMP_OPT_REVOCATION_REASON:
  704. min_val = OCSP_REVOKED_STATUS_NOSTATUS;
  705. break;
  706. case OSSL_CMP_OPT_POPO_METHOD:
  707. min_val = OSSL_CRMF_POPO_NONE;
  708. break;
  709. default:
  710. min_val = 0;
  711. break;
  712. }
  713. if (val < min_val) {
  714. ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_SMALL);
  715. return 0;
  716. }
  717. switch (opt) {
  718. case OSSL_CMP_OPT_LOG_VERBOSITY:
  719. if (val > OSSL_CMP_LOG_MAX) {
  720. ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
  721. return 0;
  722. }
  723. ctx->log_verbosity = val;
  724. break;
  725. case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
  726. ctx->implicitConfirm = val;
  727. break;
  728. case OSSL_CMP_OPT_DISABLE_CONFIRM:
  729. ctx->disableConfirm = val;
  730. break;
  731. case OSSL_CMP_OPT_UNPROTECTED_SEND:
  732. ctx->unprotectedSend = val;
  733. break;
  734. case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
  735. ctx->unprotectedErrors = val;
  736. break;
  737. case OSSL_CMP_OPT_VALIDITY_DAYS:
  738. ctx->days = val;
  739. break;
  740. case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
  741. ctx->SubjectAltName_nodefault = val;
  742. break;
  743. case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
  744. ctx->setSubjectAltNameCritical = val;
  745. break;
  746. case OSSL_CMP_OPT_POLICIES_CRITICAL:
  747. ctx->setPoliciesCritical = val;
  748. break;
  749. case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
  750. ctx->ignore_keyusage = val;
  751. break;
  752. case OSSL_CMP_OPT_POPO_METHOD:
  753. if (val > OSSL_CRMF_POPO_KEYAGREE) {
  754. ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
  755. return 0;
  756. }
  757. ctx->popoMethod = val;
  758. break;
  759. case OSSL_CMP_OPT_DIGEST_ALGNID:
  760. if (!cmp_ctx_set_md(ctx, &ctx->digest, val))
  761. return 0;
  762. break;
  763. case OSSL_CMP_OPT_OWF_ALGNID:
  764. if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, val))
  765. return 0;
  766. break;
  767. case OSSL_CMP_OPT_MAC_ALGNID:
  768. ctx->pbm_mac = val;
  769. break;
  770. case OSSL_CMP_OPT_KEEP_ALIVE:
  771. ctx->keep_alive = val;
  772. break;
  773. case OSSL_CMP_OPT_MSG_TIMEOUT:
  774. ctx->msg_timeout = val;
  775. break;
  776. case OSSL_CMP_OPT_TOTAL_TIMEOUT:
  777. ctx->total_timeout = val;
  778. break;
  779. case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
  780. ctx->permitTAInExtraCertsForIR = val;
  781. break;
  782. case OSSL_CMP_OPT_REVOCATION_REASON:
  783. if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
  784. ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
  785. return 0;
  786. }
  787. ctx->revocationReason = val;
  788. break;
  789. default:
  790. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);
  791. return 0;
  792. }
  793. return 1;
  794. }
  795. /*
  796. * Reads a Boolean or integer option value from the context.
  797. * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
  798. */
  799. int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
  800. {
  801. if (ctx == NULL) {
  802. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  803. return -1;
  804. }
  805. switch (opt) {
  806. case OSSL_CMP_OPT_LOG_VERBOSITY:
  807. return ctx->log_verbosity;
  808. case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
  809. return ctx->implicitConfirm;
  810. case OSSL_CMP_OPT_DISABLE_CONFIRM:
  811. return ctx->disableConfirm;
  812. case OSSL_CMP_OPT_UNPROTECTED_SEND:
  813. return ctx->unprotectedSend;
  814. case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
  815. return ctx->unprotectedErrors;
  816. case OSSL_CMP_OPT_VALIDITY_DAYS:
  817. return ctx->days;
  818. case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
  819. return ctx->SubjectAltName_nodefault;
  820. case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
  821. return ctx->setSubjectAltNameCritical;
  822. case OSSL_CMP_OPT_POLICIES_CRITICAL:
  823. return ctx->setPoliciesCritical;
  824. case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
  825. return ctx->ignore_keyusage;
  826. case OSSL_CMP_OPT_POPO_METHOD:
  827. return ctx->popoMethod;
  828. case OSSL_CMP_OPT_DIGEST_ALGNID:
  829. return EVP_MD_get_type(ctx->digest);
  830. case OSSL_CMP_OPT_OWF_ALGNID:
  831. return EVP_MD_get_type(ctx->pbm_owf);
  832. case OSSL_CMP_OPT_MAC_ALGNID:
  833. return ctx->pbm_mac;
  834. case OSSL_CMP_OPT_KEEP_ALIVE:
  835. return ctx->keep_alive;
  836. case OSSL_CMP_OPT_MSG_TIMEOUT:
  837. return ctx->msg_timeout;
  838. case OSSL_CMP_OPT_TOTAL_TIMEOUT:
  839. return ctx->total_timeout;
  840. case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
  841. return ctx->permitTAInExtraCertsForIR;
  842. case OSSL_CMP_OPT_REVOCATION_REASON:
  843. return ctx->revocationReason;
  844. default:
  845. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);
  846. return -1;
  847. }
  848. }