d1_srtp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. {0}
  36. };
  37. static int find_profile_by_name(char *profile_name,
  38. SRTP_PROTECTION_PROFILE **pptr, size_t len)
  39. {
  40. SRTP_PROTECTION_PROFILE *p;
  41. p = srtp_known_profiles;
  42. while (p->name) {
  43. if ((len == strlen(p->name))
  44. && strncmp(p->name, profile_name, len) == 0) {
  45. *pptr = p;
  46. return 0;
  47. }
  48. p++;
  49. }
  50. return 1;
  51. }
  52. static int ssl_ctx_make_profiles(const char *profiles_string,
  53. STACK_OF(SRTP_PROTECTION_PROFILE) **out)
  54. {
  55. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
  56. char *col;
  57. char *ptr = (char *)profiles_string;
  58. SRTP_PROTECTION_PROFILE *p;
  59. if ((profiles = sk_SRTP_PROTECTION_PROFILE_new_null()) == NULL) {
  60. SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
  61. SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
  62. return 1;
  63. }
  64. do {
  65. col = strchr(ptr, ':');
  66. if (!find_profile_by_name(ptr, &p, col ? (size_t)(col - ptr)
  67. : strlen(ptr))) {
  68. if (sk_SRTP_PROTECTION_PROFILE_find(profiles, p) >= 0) {
  69. SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
  70. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  71. goto err;
  72. }
  73. if (!sk_SRTP_PROTECTION_PROFILE_push(profiles, p)) {
  74. SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
  75. SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
  76. goto err;
  77. }
  78. } else {
  79. SSLerr(SSL_F_SSL_CTX_MAKE_PROFILES,
  80. SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
  81. goto err;
  82. }
  83. if (col)
  84. ptr = col + 1;
  85. } while (col);
  86. sk_SRTP_PROTECTION_PROFILE_free(*out);
  87. *out = profiles;
  88. return 0;
  89. err:
  90. sk_SRTP_PROTECTION_PROFILE_free(profiles);
  91. return 1;
  92. }
  93. int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
  94. {
  95. return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
  96. }
  97. int SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
  98. {
  99. return ssl_ctx_make_profiles(profiles, &s->srtp_profiles);
  100. }
  101. STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *s)
  102. {
  103. if (s != NULL) {
  104. if (s->srtp_profiles != NULL) {
  105. return s->srtp_profiles;
  106. } else if ((s->ctx != NULL) && (s->ctx->srtp_profiles != NULL)) {
  107. return s->ctx->srtp_profiles;
  108. }
  109. }
  110. return NULL;
  111. }
  112. SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s)
  113. {
  114. return s->srtp_profile;
  115. }
  116. #endif