cmp_ctx.c 31 KB

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