tb_cipher.c 2.4 KB

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