henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 08:05:01 | [diff] [blame] | 2 | * Copyright 2014 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 08:05:01 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 9 | */ |
| 10 | |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 05:47:59 | [diff] [blame] | 11 | #include "webrtc/pc/externalhmac.h" |
henrike@webrtc.org | 2d213e4 | 2014-03-06 18:51:21 | [diff] [blame] | 12 | |
| 13 | #include <stdlib.h> // For malloc/free. |
| 14 | |
mattdr | 51f2919 | 2016-09-28 21:08:46 | [diff] [blame] | 15 | #ifdef HAVE_SRTP |
jiayl@webrtc.org | a197a5e | 2015-03-23 22:11:49 | [diff] [blame] | 16 | extern "C" { |
kjellander | 7bc7c06 | 2016-04-22 11:57:49 | [diff] [blame] | 17 | #ifdef SRTP_RELATIVE_PATH |
| 18 | #include "crypto_kernel.h" // NOLINT |
| 19 | #include "srtp.h" // NOLINT |
| 20 | #else |
mattdr | 51f2919 | 2016-09-28 21:08:46 | [diff] [blame] | 21 | #include "third_party/libsrtp/crypto/include/crypto_kernel.h" |
| 22 | #include "third_party/libsrtp/include/srtp.h" |
kjellander | 7bc7c06 | 2016-04-22 11:57:49 | [diff] [blame] | 23 | #endif // SRTP_RELATIVE_PATH |
jiayl@webrtc.org | a197a5e | 2015-03-23 22:11:49 | [diff] [blame] | 24 | } |
mattdr | 51f2919 | 2016-09-28 21:08:46 | [diff] [blame] | 25 | #endif // HAVE_SRTP |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 26 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 27 | #include "webrtc/base/logging.h" |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 28 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 29 | #ifdef COMPILING_AGAINST_LIBSRTP1 |
| 30 | #define srtp_auth_type_t auth_type_t |
| 31 | |
| 32 | #define srtp_auth_init_func auth_init_func |
| 33 | #define srtp_auth_compute_func auth_compute_func |
| 34 | #define srtp_auth_update_func auth_update_func |
| 35 | #define srtp_auth_start_func auth_start_func |
| 36 | #define srtp_auth_test_case_t auth_test_case_t |
| 37 | |
| 38 | #define srtp_replace_auth_type crypto_kernel_replace_auth_type |
| 39 | #endif // COMPILING_AGAINST_LIBSRTP1 |
| 40 | |
mattdr | 51f2919 | 2016-09-28 21:08:46 | [diff] [blame] | 41 | #if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH) |
| 42 | |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 43 | // Begin test case 0 */ |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 44 | static const uint8_t kExternalHmacTestCase0Key[20] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 45 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 46 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 47 | 0x0b, 0x0b, 0x0b, 0x0b |
| 48 | }; |
| 49 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 50 | static const uint8_t kExternalHmacTestCase0Data[8] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 51 | 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There" |
| 52 | }; |
| 53 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 54 | static const uint8_t kExternalHmacFakeTag[10] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 55 | 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd |
| 56 | }; |
| 57 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 58 | static const srtp_auth_test_case_t kExternalHmacTestCase0 = { |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 59 | 20, // Octets in key |
| 60 | const_cast<uint8_t*>(kExternalHmacTestCase0Key), // Key |
| 61 | 8, // Octets in data |
| 62 | const_cast<uint8_t*>(kExternalHmacTestCase0Data), // Data |
| 63 | 10, // Octets in tag |
| 64 | const_cast<uint8_t*>(kExternalHmacFakeTag), // Tag |
| 65 | NULL // Pointer to next |
| 66 | // testcase |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 67 | }; |
| 68 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 69 | static const char kExternalHmacDescription[] = |
| 70 | "external hmac sha-1 authentication"; |
| 71 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 72 | // srtp_auth_type_t external_hmac is the hmac metaobject |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 73 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 74 | #ifdef COMPILING_AGAINST_LIBSRTP1 |
| 75 | static const srtp_auth_type_t external_hmac = { |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 76 | external_hmac_alloc, |
| 77 | external_hmac_dealloc, |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 78 | (srtp_auth_init_func) external_hmac_init, |
| 79 | (srtp_auth_compute_func) external_hmac_compute, |
| 80 | (srtp_auth_update_func) external_hmac_update, |
| 81 | (srtp_auth_start_func) external_hmac_start, |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 82 | const_cast<char*>(kExternalHmacDescription), |
| 83 | 0, // Instance count. |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 84 | const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0), |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 85 | NULL, // No debugging module. |
| 86 | EXTERNAL_HMAC_SHA1 |
| 87 | }; |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 88 | #else |
| 89 | static const srtp_auth_type_t external_hmac = { |
| 90 | external_hmac_alloc, |
| 91 | external_hmac_dealloc, |
| 92 | (srtp_auth_init_func) external_hmac_init, |
| 93 | (srtp_auth_compute_func) external_hmac_compute, |
| 94 | (srtp_auth_update_func) external_hmac_update, |
| 95 | (srtp_auth_start_func) external_hmac_start, |
| 96 | const_cast<char*>(kExternalHmacDescription), |
| 97 | const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0), |
| 98 | EXTERNAL_HMAC_SHA1 |
| 99 | }; |
| 100 | #endif // COMPILING_AGAINST_LIBSRTP1 |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 101 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 102 | srtp_err_status_t external_hmac_alloc(srtp_auth_t** a, |
| 103 | int key_len, |
| 104 | int out_len) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 105 | uint8_t* pointer; |
| 106 | |
| 107 | // Check key length - note that we don't support keys larger |
| 108 | // than 20 bytes yet |
| 109 | if (key_len > 20) |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 110 | return srtp_err_status_bad_param; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 111 | |
| 112 | // Check output length - should be less than 20 bytes/ |
| 113 | if (out_len > 20) |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 114 | return srtp_err_status_bad_param; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 115 | |
| 116 | // Allocate memory for auth and hmac_ctx_t structures. |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 117 | pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))]; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 118 | if (pointer == NULL) |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 119 | return srtp_err_status_alloc_fail; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 120 | |
| 121 | // Set pointers |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 122 | *a = (srtp_auth_t *)pointer; |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 123 | // |external_hmac| is const and libsrtp expects |type| to be non-const. |
| 124 | // const conversion is required. |external_hmac| is constant because we don't |
| 125 | // want to increase global count in Chrome. |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 126 | (*a)->type = const_cast<srtp_auth_type_t*>(&external_hmac); |
| 127 | (*a)->state = pointer + sizeof(srtp_auth_t); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 128 | (*a)->out_len = out_len; |
| 129 | (*a)->key_len = key_len; |
| 130 | (*a)->prefix_len = 0; |
| 131 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 132 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 133 | } |
| 134 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 135 | srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 136 | // Zeroize entire state |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 137 | memset((uint8_t *)a, 0, sizeof(ExternalHmacContext) + sizeof(srtp_auth_t)); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 138 | |
| 139 | // Free memory |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 140 | delete[] a; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 141 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 142 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 143 | } |
| 144 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 145 | srtp_err_status_t external_hmac_init(ExternalHmacContext* state, |
| 146 | const uint8_t* key, |
| 147 | int key_len) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 148 | if (key_len > HMAC_KEY_LENGTH) |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 149 | return srtp_err_status_bad_param; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 150 | |
| 151 | memset(state->key, 0, key_len); |
| 152 | memcpy(state->key, key, key_len); |
| 153 | state->key_length = key_len; |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 154 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 155 | } |
| 156 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 157 | srtp_err_status_t external_hmac_start(ExternalHmacContext* state) { |
| 158 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 159 | } |
| 160 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 161 | srtp_err_status_t external_hmac_update(ExternalHmacContext* state, |
| 162 | const uint8_t* message, |
| 163 | int msg_octets) { |
| 164 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 165 | } |
| 166 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 167 | srtp_err_status_t external_hmac_compute(ExternalHmacContext* state, |
| 168 | const void* message, |
| 169 | int msg_octets, |
| 170 | int tag_len, |
| 171 | uint8_t* result) { |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 172 | memcpy(result, kExternalHmacFakeTag, tag_len); |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 173 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 174 | } |
| 175 | |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 176 | srtp_err_status_t external_crypto_init() { |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 | [diff] [blame] | 177 | // |external_hmac| is const. const_cast is required as libsrtp expects |
| 178 | // non-const. |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 179 | srtp_err_status_t status = srtp_replace_auth_type( |
| 180 | const_cast<srtp_auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 181 | if (status) { |
| 182 | LOG(LS_ERROR) << "Error in replacing default auth module, error: " |
| 183 | << status; |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 184 | return srtp_err_status_fail; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 185 | } |
mattdr | 8cab52d | 2016-10-10 22:33:37 | [diff] [blame^] | 186 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | #endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH) |