d1_srtp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. DEFINE_STACK_OF(SRTP_PROTECTION_PROFILE)
  19. static SRTP_PROTECTION_PROFILE srtp_known_profiles[] = {
  20. {
  21. "SRTP_AES128_CM_SHA1_80",
  22. SRTP_AES128_CM_SHA1_80,
  23. },
  24. {
  25. "SRTP_AES128_CM_SHA1_32",
  26. SRTP_AES128_CM_SHA1_32,
  27. },
  28. {
  29. "SRTP_AEAD_AES_128_GCM",
  30. SRTP_AEAD_AES_128_GCM,
  31. },
  32. {
  33. "SRTP_AEAD_AES_256_GCM",
  34. SRTP_AEAD_AES_256_GCM,
  35. },
  36. {0}
  37. };
  38. static int find_profile_by_name(char *profile_name,
  39. SRTP_PROTECTION_PROFILE **pptr, size_t len)
  40. {
  41. SRTP_PROTECTION_PROFILE *p;
  42. p = srtp_known_profiles;
  43. while (p->name) {
  44. if ((len == strlen(p->name))
  45. && strncmp(p->name, profile_name, len) == 0) {
  46. *pptr = p;
  47. return 0;
  48. }
  49. p++;
  50. }
  51. return 1;
  52. }
  53. static int ssl_ctx_make_profiles(const char *profiles_string,
  54. STACK_OF(SRTP_PROTECTION_PROFILE) **out)
  55. {
  56. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
  57. char *col;
  58. char *ptr = (char *)profiles_string;
  59. SRTP_PROTECTION_PROFILE *p;
  60. if ((profiles = sk_SRTP_PROTECTION_PROFILE_new_null()) == NULL) {
  61. SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
  62. SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
  63. return 1;
  64. }
  65. do {
  66. col = strchr(ptr, ':');
  67. if (!find_profile_by_name(ptr, &p, col ? (size_t)(col - ptr)
  68. : strlen(ptr))) {
  69. if (sk_SRTP_PROTECTION_PROFILE_find(profiles, p) >= 0) {
  70. SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
  71. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  72. goto err;
  73. }
  74. if (!sk_SRTP_PROTECTION_PROFILE_push(profiles, p)) {
  75. SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
  76. SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
  77. goto err;
  78. }
  79. } else {
  80. SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
  81. SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
  82. goto err;
  83. }
  84. if (col)
  85. ptr = col + 1;
  86. } while (col);
  87. sk_SRTP_PROTECTION_PROFILE_free(*out);
  88. *out = profiles;
  89. return 0;
  90. err:
  91. sk_SRTP_PROTECTION_PROFILE_free(profiles);
  92. return 1;
  93. }
  94. int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
  95. {
  96. return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
  97. }
  98. int SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
  99. {
  100. return ssl_ctx_make_profiles(profiles, &s->srtp_profiles);
  101. }
  102. STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *s)
  103. {
  104. if (s != NULL) {
  105. if (s->srtp_profiles != NULL) {
  106. return s->srtp_profiles;
  107. } else if ((s->ctx != NULL) && (s->ctx->srtp_profiles != NULL)) {
  108. return s->ctx->srtp_profiles;
  109. }
  110. }
  111. return NULL;
  112. }
  113. SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s)
  114. {
  115. return s->srtp_profile;
  116. }
  117. #endif