decoder_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/core_names.h>
  10. #include <openssl/bio.h>
  11. #include <openssl/params.h>
  12. #include <openssl/provider.h>
  13. #include "encoder_local.h"
  14. #include "e_os.h"
  15. struct decoder_process_data_st {
  16. OSSL_DECODER_CTX *ctx;
  17. /* Current BIO */
  18. BIO *bio;
  19. /* Index of the current decoder instance to be processed */
  20. size_t current_deser_inst_index;
  21. };
  22. static int decoder_process(const OSSL_PARAM params[], void *arg);
  23. int OSSL_DECODER_from_bio(OSSL_DECODER_CTX *ctx, BIO *in)
  24. {
  25. struct decoder_process_data_st data;
  26. int ok = 0;
  27. memset(&data, 0, sizeof(data));
  28. data.ctx = ctx;
  29. data.bio = in;
  30. ok = decoder_process(NULL, &data);
  31. /* Clear any internally cached passphrase */
  32. if (!ctx->flag_user_passphrase) {
  33. OSSL_DECODER_CTX_set_passphrase(ctx, NULL, 0);
  34. ctx->flag_user_passphrase = 0;
  35. }
  36. return ok;
  37. }
  38. #ifndef OPENSSL_NO_STDIO
  39. static BIO *bio_from_file(FILE *fp)
  40. {
  41. BIO *b;
  42. if ((b = BIO_new(BIO_s_file())) == NULL) {
  43. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
  44. return NULL;
  45. }
  46. BIO_set_fp(b, fp, BIO_NOCLOSE);
  47. return b;
  48. }
  49. int OSSL_DECODER_from_fp(OSSL_DECODER_CTX *ctx, FILE *fp)
  50. {
  51. BIO *b = bio_from_file(fp);
  52. int ret = 0;
  53. if (b != NULL)
  54. ret = OSSL_DECODER_from_bio(ctx, b);
  55. BIO_free(b);
  56. return ret;
  57. }
  58. #endif
  59. int OSSL_DECODER_CTX_set_input_type(OSSL_DECODER_CTX *ctx,
  60. const char *input_type)
  61. {
  62. if (!ossl_assert(ctx != NULL)) {
  63. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  64. return 0;
  65. }
  66. /*
  67. * NULL is a valid starting input type, and means that the caller leaves
  68. * it to code to discover what the starting input type is.
  69. */
  70. ctx->start_input_type = input_type;
  71. return 1;
  72. }
  73. int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx, OSSL_DECODER *decoder)
  74. {
  75. OSSL_DECODER_INSTANCE *decoder_inst = NULL;
  76. const OSSL_PROVIDER *prov = NULL;
  77. OSSL_PARAM params[2];
  78. void *provctx = NULL;
  79. if (!ossl_assert(ctx != NULL) || !ossl_assert(decoder != NULL)) {
  80. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  81. return 0;
  82. }
  83. if (decoder->get_params == NULL) {
  84. ERR_raise(ERR_LIB_OSSL_DECODER,
  85. OSSL_DECODER_R_MISSING_GET_PARAMS);
  86. return 0;
  87. }
  88. if (ctx->decoder_insts == NULL
  89. && (ctx->decoder_insts =
  90. sk_OSSL_DECODER_INSTANCE_new_null()) == NULL) {
  91. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  92. return 0;
  93. }
  94. if ((decoder_inst = OPENSSL_zalloc(sizeof(*decoder_inst))) == NULL) {
  95. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  96. return 0;
  97. }
  98. if (!OSSL_DECODER_up_ref(decoder)) {
  99. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
  100. goto err;
  101. }
  102. decoder_inst->decoder = decoder;
  103. prov = OSSL_DECODER_provider(decoder_inst->decoder);
  104. provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  105. /* Cache the input type for this encoder */
  106. params[0] =
  107. OSSL_PARAM_construct_utf8_ptr(OSSL_DECODER_PARAM_INPUT_TYPE,
  108. (char **)&decoder_inst->input_type, 0);
  109. params[1] = OSSL_PARAM_construct_end();
  110. if (!decoder_inst->decoder->get_params(params)
  111. || !OSSL_PARAM_modified(&params[0]))
  112. goto err;
  113. if ((decoder_inst->deserctx = decoder_inst->decoder->newctx(provctx))
  114. == NULL)
  115. goto err;
  116. if (sk_OSSL_DECODER_INSTANCE_push(ctx->decoder_insts, decoder_inst) <= 0)
  117. goto err;
  118. return 1;
  119. err:
  120. if (decoder_inst != NULL) {
  121. if (decoder_inst->decoder != NULL)
  122. decoder_inst->decoder->freectx(decoder_inst->deserctx);
  123. OSSL_DECODER_free(decoder_inst->decoder);
  124. OPENSSL_free(decoder_inst);
  125. }
  126. return 0;
  127. }
  128. int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
  129. OPENSSL_CTX *libctx, const char *propq)
  130. {
  131. /*
  132. * This function goes through existing decoder methods in
  133. * |ctx->decoder_insts|, and tries to fetch new decoders that produce
  134. * what the existing ones want as input, and push those newly fetched
  135. * decoders on top of the same stack.
  136. * Then it does the same again, but looping over the newly fetched
  137. * decoders, until there are no more encoders to be fetched, or
  138. * when we have done this 10 times.
  139. *
  140. * we do this with sliding windows on the stack by keeping track of indexes
  141. * and of the end.
  142. *
  143. * +----------------+
  144. * | DER to RSA | <--- w_prev_start
  145. * +----------------+
  146. * | DER to DSA |
  147. * +----------------+
  148. * | DER to DH |
  149. * +----------------+
  150. * | PEM to DER | <--- w_prev_end, w_new_start
  151. * +----------------+
  152. * <--- w_new_end
  153. */
  154. size_t w_prev_start, w_prev_end; /* "previous" decoders */
  155. size_t w_new_start, w_new_end; /* "new" decoders */
  156. size_t count = 0; /* Calculates how many were added in each iteration */
  157. size_t depth = 0; /* Counts the number of iterations */
  158. if (!ossl_assert(ctx != NULL)) {
  159. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  160. return 0;
  161. }
  162. /*
  163. * If there is no stack of OSSL_DECODER_INSTANCE, we have nothing
  164. * more to add. That's fine.
  165. */
  166. if (ctx->decoder_insts == NULL)
  167. return 1;
  168. w_prev_start = 0;
  169. w_prev_end = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
  170. do {
  171. size_t i;
  172. w_new_start = w_new_end = w_prev_end;
  173. for (i = w_prev_start; i < w_prev_end; i++) {
  174. OSSL_DECODER_INSTANCE *decoder_inst =
  175. sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
  176. const char *name = decoder_inst->input_type;
  177. OSSL_DECODER *decoder = NULL;
  178. /*
  179. * If the caller has specified what the initial input should be,
  180. * and the decoder implementation we're looking at has that
  181. * input type, there's no point adding on more implementations
  182. * on top of this one, so we don't.
  183. */
  184. if (ctx->start_input_type != NULL
  185. && strcasecmp(ctx->start_input_type,
  186. decoder_inst->input_type) != 0)
  187. continue;
  188. ERR_set_mark();
  189. decoder = OSSL_DECODER_fetch(libctx, name, propq);
  190. ERR_pop_to_mark();
  191. if (decoder != NULL) {
  192. size_t j;
  193. /*
  194. * Check that we don't already have this decoder in our
  195. * stack We only need to check among the newly added ones.
  196. */
  197. for (j = w_new_start; j < w_new_end; j++) {
  198. OSSL_DECODER_INSTANCE *check_inst =
  199. sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, j);
  200. if (decoder == check_inst->decoder) {
  201. /* We found it, so drop the new fetch */
  202. OSSL_DECODER_free(decoder);
  203. decoder = NULL;
  204. break;
  205. }
  206. }
  207. }
  208. if (decoder == NULL)
  209. continue;
  210. /*
  211. * Apart from keeping w_new_end up to date, We don't care about
  212. * errors here. If it doesn't collect, then it doesn't...
  213. */
  214. if (OSSL_DECODER_CTX_add_decoder(ctx, decoder)) /* ref++ */
  215. w_new_end++;
  216. OSSL_DECODER_free(decoder); /* ref-- */
  217. }
  218. /* How many were added in this iteration */
  219. count = w_new_end - w_new_start;
  220. /* Slide the "previous decoder" windows */
  221. w_prev_start = w_new_start;
  222. w_prev_end = w_new_end;
  223. depth++;
  224. } while (count != 0 && depth <= 10);
  225. return 1;
  226. }
  227. int OSSL_DECODER_CTX_num_decoders(OSSL_DECODER_CTX *ctx)
  228. {
  229. if (ctx == NULL || ctx->decoder_insts == NULL)
  230. return 0;
  231. return sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
  232. }
  233. int OSSL_DECODER_CTX_set_construct(OSSL_DECODER_CTX *ctx,
  234. OSSL_DECODER_CONSTRUCT *construct)
  235. {
  236. if (!ossl_assert(ctx != NULL)) {
  237. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  238. return 0;
  239. }
  240. ctx->construct = construct;
  241. return 1;
  242. }
  243. int OSSL_DECODER_CTX_set_construct_data(OSSL_DECODER_CTX *ctx,
  244. void *construct_data)
  245. {
  246. if (!ossl_assert(ctx != NULL)) {
  247. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  248. return 0;
  249. }
  250. ctx->construct_data = construct_data;
  251. return 1;
  252. }
  253. int OSSL_DECODER_CTX_set_cleanup(OSSL_DECODER_CTX *ctx,
  254. OSSL_DECODER_CLEANUP *cleanup)
  255. {
  256. if (!ossl_assert(ctx != NULL)) {
  257. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  258. return 0;
  259. }
  260. ctx->cleanup = cleanup;
  261. return 1;
  262. }
  263. OSSL_DECODER_CONSTRUCT *
  264. OSSL_DECODER_CTX_get_construct(OSSL_DECODER_CTX *ctx)
  265. {
  266. if (ctx == NULL)
  267. return NULL;
  268. return ctx->construct;
  269. }
  270. void *OSSL_DECODER_CTX_get_construct_data(OSSL_DECODER_CTX *ctx)
  271. {
  272. if (ctx == NULL)
  273. return NULL;
  274. return ctx->construct_data;
  275. }
  276. OSSL_DECODER_CLEANUP *
  277. OSSL_DECODER_CTX_get_cleanup(OSSL_DECODER_CTX *ctx)
  278. {
  279. if (ctx == NULL)
  280. return NULL;
  281. return ctx->cleanup;
  282. }
  283. int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
  284. void *reference, size_t reference_sz,
  285. OSSL_CALLBACK *export_cb, void *export_cbarg)
  286. {
  287. if (!(ossl_assert(decoder_inst != NULL)
  288. && ossl_assert(reference != NULL)
  289. && ossl_assert(export_cb != NULL)
  290. && ossl_assert(export_cbarg != NULL))) {
  291. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  292. return 0;
  293. }
  294. return decoder_inst->decoder->export_object(decoder_inst->deserctx,
  295. reference, reference_sz,
  296. export_cb, export_cbarg);
  297. }
  298. OSSL_DECODER *OSSL_DECODER_INSTANCE_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
  299. {
  300. if (decoder_inst == NULL)
  301. return NULL;
  302. return decoder_inst->decoder;
  303. }
  304. void *OSSL_DECODER_INSTANCE_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
  305. {
  306. if (decoder_inst == NULL)
  307. return NULL;
  308. return decoder_inst->deserctx;
  309. }
  310. static int decoder_process(const OSSL_PARAM params[], void *arg)
  311. {
  312. struct decoder_process_data_st *data = arg;
  313. OSSL_DECODER_CTX *ctx = data->ctx;
  314. OSSL_DECODER_INSTANCE *decoder_inst = NULL;
  315. OSSL_DECODER *decoder = NULL;
  316. BIO *bio = data->bio;
  317. long loc;
  318. size_t i;
  319. int ok = 0;
  320. /* For recursions */
  321. struct decoder_process_data_st new_data;
  322. memset(&new_data, 0, sizeof(new_data));
  323. new_data.ctx = data->ctx;
  324. if (params == NULL) {
  325. /* First iteration, where we prepare for what is to come */
  326. data->current_deser_inst_index =
  327. OSSL_DECODER_CTX_num_decoders(ctx);
  328. bio = data->bio;
  329. } else {
  330. const OSSL_PARAM *p;
  331. decoder_inst =
  332. sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts,
  333. data->current_deser_inst_index);
  334. decoder = OSSL_DECODER_INSTANCE_decoder(decoder_inst);
  335. if (ctx->construct != NULL
  336. && ctx->construct(decoder_inst, params, ctx->construct_data)) {
  337. ok = 1;
  338. goto end;
  339. }
  340. /* The constructor didn't return success */
  341. /*
  342. * so we try to use the object we got and feed it to any next
  343. * decoder that will take it. Object references are not
  344. * allowed for this.
  345. * If this data isn't present, decoding has failed.
  346. */
  347. p = OSSL_PARAM_locate_const(params, OSSL_DECODER_PARAM_DATA);
  348. if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
  349. goto end;
  350. new_data.bio = BIO_new_mem_buf(p->data, (int)p->data_size);
  351. if (new_data.bio == NULL)
  352. goto end;
  353. bio = new_data.bio;
  354. }
  355. /*
  356. * If we have no more decoders to look through at this point,
  357. * we failed
  358. */
  359. if (data->current_deser_inst_index == 0)
  360. goto end;
  361. if ((loc = BIO_tell(bio)) < 0) {
  362. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
  363. goto end;
  364. }
  365. for (i = data->current_deser_inst_index; i-- > 0;) {
  366. OSSL_DECODER_INSTANCE *new_deser_inst =
  367. sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
  368. OSSL_DECODER *new_deser =
  369. OSSL_DECODER_INSTANCE_decoder(new_deser_inst);
  370. /*
  371. * If |decoder| is NULL, it means we've just started, and the caller
  372. * may have specified what it expects the initial input to be. If
  373. * that's the case, we do this extra check.
  374. */
  375. if (decoder == NULL && ctx->start_input_type != NULL
  376. && strcasecmp(ctx->start_input_type,
  377. new_deser_inst->input_type) != 0)
  378. continue;
  379. /*
  380. * If we have a previous decoder, we check that the input type
  381. * of the next to be used matches the type of this previous one.
  382. * decoder_inst->input_type is a cache of the parameter "input-type"
  383. * value for that decoder.
  384. */
  385. if (decoder != NULL
  386. && !OSSL_DECODER_is_a(decoder, new_deser_inst->input_type))
  387. continue;
  388. /*
  389. * Checking the return value of BIO_reset() or BIO_seek() is unsafe.
  390. * Furthermore, BIO_reset() is unsafe to use if the source BIO happens
  391. * to be a BIO_s_mem(), because the earlier BIO_tell() gives us zero
  392. * no matter where we are in the underlying buffer we're reading from.
  393. *
  394. * So, we simply do a BIO_seek(), and use BIO_tell() that we're back
  395. * at the same position. This is a best effort attempt, but BIO_seek()
  396. * and BIO_tell() should come as a pair...
  397. */
  398. (void)BIO_seek(bio, loc);
  399. if (BIO_tell(bio) != loc)
  400. goto end;
  401. /* Recurse */
  402. new_data.current_deser_inst_index = i;
  403. ok = new_deser->decode(new_deser_inst->deserctx, (OSSL_CORE_BIO *)bio,
  404. decoder_process, &new_data,
  405. ctx->passphrase_cb, new_data.ctx);
  406. if (ok)
  407. break;
  408. }
  409. end:
  410. BIO_free(new_data.bio);
  411. return ok;
  412. }