lws-gencrypto.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * lws-gencrypto openssl-specific common code
  25. */
  26. #include "private-lib-core.h"
  27. #include "private-lib-tls-openssl.h"
  28. /*
  29. * Care: many openssl apis return 1 for success. These are translated to the
  30. * lws convention of 0 for success.
  31. */
  32. int
  33. lws_gencrypto_openssl_hash_to_NID(enum lws_genhash_types hash_type)
  34. {
  35. int h = -1;
  36. switch (hash_type) {
  37. case LWS_GENHASH_TYPE_UNKNOWN:
  38. break;
  39. case LWS_GENHASH_TYPE_MD5:
  40. h = NID_md5;
  41. break;
  42. case LWS_GENHASH_TYPE_SHA1:
  43. h = NID_sha1;
  44. break;
  45. case LWS_GENHASH_TYPE_SHA256:
  46. h = NID_sha256;
  47. break;
  48. case LWS_GENHASH_TYPE_SHA384:
  49. h = NID_sha384;
  50. break;
  51. case LWS_GENHASH_TYPE_SHA512:
  52. h = NID_sha512;
  53. break;
  54. }
  55. return h;
  56. }
  57. const EVP_MD *
  58. lws_gencrypto_openssl_hash_to_EVP_MD(enum lws_genhash_types hash_type)
  59. {
  60. const EVP_MD *h = NULL;
  61. switch (hash_type) {
  62. case LWS_GENHASH_TYPE_UNKNOWN:
  63. break;
  64. case LWS_GENHASH_TYPE_MD5:
  65. h = EVP_md5();
  66. break;
  67. case LWS_GENHASH_TYPE_SHA1:
  68. h = EVP_sha1();
  69. break;
  70. case LWS_GENHASH_TYPE_SHA256:
  71. h = EVP_sha256();
  72. break;
  73. case LWS_GENHASH_TYPE_SHA384:
  74. h = EVP_sha384();
  75. break;
  76. case LWS_GENHASH_TYPE_SHA512:
  77. h = EVP_sha512();
  78. break;
  79. }
  80. return h;
  81. }