d1_srtp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2011-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. /*
  10. * DTLS code by Eric Rescorla <ekr@rtfm.com>
  11. *
  12. * Copyright (C) 2006, Network Resonance, Inc. Copyright (C) 2011, RTFM, Inc.
  13. */
  14. #include <stdio.h>
  15. #include <openssl/objects.h>
  16. #include "ssl_local.h"
  17. #ifndef OPENSSL_NO_SRTP
  18. static SRTP_PROTECTION_PROFILE srtp_known_profiles[] = {
  19. {
  20. "SRTP_AES128_CM_SHA1_80",
  21. SRTP_AES128_CM_SHA1_80,
  22. },
  23. {
  24. "SRTP_AES128_CM_SHA1_32",
  25. SRTP_AES128_CM_SHA1_32,
  26. },
  27. {
  28. "SRTP_AEAD_AES_128_GCM",
  29. SRTP_AEAD_AES_128_GCM,
  30. },
  31. {
  32. "SRTP_AEAD_AES_256_GCM",
  33. SRTP_AEAD_AES_256_GCM,
  34. },
  35. {
  36. "SRTP_DOUBLE_AEAD_AES_128_GCM_AEAD_AES_128_GCM",
  37. SRTP_DOUBLE_AEAD_AES_128_GCM_AEAD_AES_128_GCM,
  38. },
  39. {
  40. "SRTP_DOUBLE_AEAD_AES_256_GCM_AEAD_AES_256_GCM",
  41. SRTP_DOUBLE_AEAD_AES_256_GCM_AEAD_AES_256_GCM,
  42. },
  43. {
  44. "SRTP_ARIA_128_CTR_HMAC_SHA1_80",
  45. SRTP_ARIA_128_CTR_HMAC_SHA1_80,
  46. },
  47. {
  48. "SRTP_ARIA_128_CTR_HMAC_SHA1_32",
  49. SRTP_ARIA_128_CTR_HMAC_SHA1_32,
  50. },
  51. {
  52. "SRTP_ARIA_256_CTR_HMAC_SHA1_80",
  53. SRTP_ARIA_256_CTR_HMAC_SHA1_80,
  54. },
  55. {
  56. "SRTP_ARIA_256_CTR_HMAC_SHA1_32",
  57. SRTP_ARIA_256_CTR_HMAC_SHA1_32,
  58. },
  59. {
  60. "SRTP_AEAD_ARIA_128_GCM",
  61. SRTP_AEAD_ARIA_128_GCM,
  62. },
  63. {
  64. "SRTP_AEAD_ARIA_256_GCM",
  65. SRTP_AEAD_ARIA_256_GCM,
  66. },
  67. {0}
  68. };
  69. static int find_profile_by_name(char *profile_name,
  70. SRTP_PROTECTION_PROFILE **pptr, size_t len)
  71. {
  72. SRTP_PROTECTION_PROFILE *p;
  73. p = srtp_known_profiles;
  74. while (p->name) {
  75. if ((len == strlen(p->name))
  76. && strncmp(p->name, profile_name, len) == 0) {
  77. *pptr = p;
  78. return 0;
  79. }
  80. p++;
  81. }
  82. return 1;
  83. }
  84. static int ssl_ctx_make_profiles(const char *profiles_string,
  85. STACK_OF(SRTP_PROTECTION_PROFILE) **out)
  86. {
  87. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
  88. char *col;
  89. char *ptr = (char *)profiles_string;
  90. SRTP_PROTECTION_PROFILE *p;
  91. if ((profiles = sk_SRTP_PROTECTION_PROFILE_new_null()) == NULL) {
  92. ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
  93. return 1;
  94. }
  95. do {
  96. col = strchr(ptr, ':');
  97. if (!find_profile_by_name(ptr, &p, col ? (size_t)(col - ptr)
  98. : strlen(ptr))) {
  99. if (sk_SRTP_PROTECTION_PROFILE_find(profiles, p) >= 0) {
  100. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  101. goto err;
  102. }
  103. if (!sk_SRTP_PROTECTION_PROFILE_push(profiles, p)) {
  104. ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
  105. goto err;
  106. }
  107. } else {
  108. ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
  109. goto err;
  110. }
  111. if (col)
  112. ptr = col + 1;
  113. } while (col);
  114. sk_SRTP_PROTECTION_PROFILE_free(*out);
  115. *out = profiles;
  116. return 0;
  117. err:
  118. sk_SRTP_PROTECTION_PROFILE_free(profiles);
  119. return 1;
  120. }
  121. int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
  122. {
  123. return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
  124. }
  125. int SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
  126. {
  127. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  128. if (sc == NULL)
  129. return 0;
  130. return ssl_ctx_make_profiles(profiles, &sc->srtp_profiles);
  131. }
  132. STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *s)
  133. {
  134. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  135. if (sc != NULL) {
  136. if (sc->srtp_profiles != NULL) {
  137. return sc->srtp_profiles;
  138. } else if ((s->ctx != NULL) && (s->ctx->srtp_profiles != NULL)) {
  139. return s->ctx->srtp_profiles;
  140. }
  141. }
  142. return NULL;
  143. }
  144. SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s)
  145. {
  146. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  147. if (sc == NULL)
  148. return 0;
  149. return sc->srtp_profile;
  150. }
  151. #endif