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