tb_cipher.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2001-2021 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 ossl_engine_table_select(&cipher_table, nid,
  59. OPENSSL_FILE, OPENSSL_LINE);
  60. }
  61. /* Obtains a cipher implementation from an ENGINE functional reference */
  62. const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid)
  63. {
  64. const EVP_CIPHER *ret;
  65. ENGINE_CIPHERS_PTR fn = ENGINE_get_ciphers(e);
  66. if (!fn || !fn(e, &ret, NULL, nid)) {
  67. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_CIPHER);
  68. return NULL;
  69. }
  70. return ret;
  71. }
  72. /* Gets the cipher callback from an ENGINE structure */
  73. ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e)
  74. {
  75. return e->ciphers;
  76. }
  77. /* Sets the cipher callback in an ENGINE structure */
  78. int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f)
  79. {
  80. e->ciphers = f;
  81. return 1;
  82. }