tb_cipher.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2001-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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include "eng_local.h"
  12. static ENGINE_TABLE *cipher_table = NULL;
  13. void ENGINE_unregister_ciphers(ENGINE *e)
  14. {
  15. engine_table_unregister(&cipher_table, e);
  16. }
  17. static void engine_unregister_all_ciphers(void)
  18. {
  19. engine_table_cleanup(&cipher_table);
  20. }
  21. int ENGINE_register_ciphers(ENGINE *e)
  22. {
  23. if (e->ciphers) {
  24. const int *nids;
  25. int num_nids = e->ciphers(e, NULL, &nids, 0);
  26. if (num_nids > 0)
  27. return engine_table_register(&cipher_table,
  28. engine_unregister_all_ciphers, e,
  29. nids, num_nids, 0);
  30. }
  31. return 1;
  32. }
  33. void ENGINE_register_all_ciphers(void)
  34. {
  35. ENGINE *e;
  36. for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
  37. ENGINE_register_ciphers(e);
  38. }
  39. int ENGINE_set_default_ciphers(ENGINE *e)
  40. {
  41. if (e->ciphers) {
  42. const int *nids;
  43. int num_nids = e->ciphers(e, NULL, &nids, 0);
  44. if (num_nids > 0)
  45. return engine_table_register(&cipher_table,
  46. engine_unregister_all_ciphers, e,
  47. nids, num_nids, 1);
  48. }
  49. return 1;
  50. }
  51. /*
  52. * Exposed API function to get a functional reference from the implementation
  53. * table (ie. try to get a functional reference from the tabled structural
  54. * references) for a given cipher 'nid'
  55. */
  56. ENGINE *ENGINE_get_cipher_engine(int nid)
  57. {
  58. return engine_table_select(&cipher_table, nid);
  59. }
  60. /* Obtains a cipher implementation from an ENGINE functional reference */
  61. const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid)
  62. {
  63. const EVP_CIPHER *ret;
  64. ENGINE_CIPHERS_PTR fn = ENGINE_get_ciphers(e);
  65. if (!fn || !fn(e, &ret, NULL, nid)) {
  66. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_CIPHER);
  67. return NULL;
  68. }
  69. return ret;
  70. }
  71. /* Gets the cipher callback from an ENGINE structure */
  72. ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e)
  73. {
  74. return e->ciphers;
  75. }
  76. /* Sets the cipher callback in an ENGINE structure */
  77. int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f)
  78. {
  79. e->ciphers = f;
  80. return 1;
  81. }