d1_srtp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. {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. ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
  61. return 1;
  62. }
  63. do {
  64. col = strchr(ptr, ':');
  65. if (!find_profile_by_name(ptr, &p, col ? (size_t)(col - ptr)
  66. : strlen(ptr))) {
  67. if (sk_SRTP_PROTECTION_PROFILE_find(profiles, p) >= 0) {
  68. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  69. goto err;
  70. }
  71. if (!sk_SRTP_PROTECTION_PROFILE_push(profiles, p)) {
  72. ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
  73. goto err;
  74. }
  75. } else {
  76. ERR_raise(ERR_LIB_SSL, SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
  77. goto err;
  78. }
  79. if (col)
  80. ptr = col + 1;
  81. } while (col);
  82. sk_SRTP_PROTECTION_PROFILE_free(*out);
  83. *out = profiles;
  84. return 0;
  85. err:
  86. sk_SRTP_PROTECTION_PROFILE_free(profiles);
  87. return 1;
  88. }
  89. int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles)
  90. {
  91. return ssl_ctx_make_profiles(profiles, &ctx->srtp_profiles);
  92. }
  93. int SSL_set_tlsext_use_srtp(SSL *s, const char *profiles)
  94. {
  95. return ssl_ctx_make_profiles(profiles, &s->srtp_profiles);
  96. }
  97. STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *s)
  98. {
  99. if (s != NULL) {
  100. if (s->srtp_profiles != NULL) {
  101. return s->srtp_profiles;
  102. } else if ((s->ctx != NULL) && (s->ctx->srtp_profiles != NULL)) {
  103. return s->ctx->srtp_profiles;
  104. }
  105. }
  106. return NULL;
  107. }
  108. SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s)
  109. {
  110. return s->srtp_profile;
  111. }
  112. #endif