cmp_ctx.c 31 KB

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