| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/crypto/aes_128_gcm_12_decrypter.h" | 5 #include "net/quic/crypto/aes_128_gcm_12_decrypter.h" |
| 6 | 6 |
| 7 #include <nss.h> | |
| 8 #include <pk11pub.h> | 7 #include <pk11pub.h> |
| 9 #include <secerr.h> | 8 #include <secerr.h> |
| 10 | 9 |
| 11 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "crypto/ghash.h" | 11 #include "crypto/ghash.h" |
| 14 #include "crypto/scoped_nss_types.h" | 12 #include "crypto/scoped_nss_types.h" |
| 15 | 13 |
| 16 #if defined(USE_NSS) | 14 #if defined(USE_NSS) |
| 17 #include <dlfcn.h> | 15 #include <dlfcn.h> |
| 18 #endif | 16 #endif |
| 19 | 17 |
| 20 using base::StringPiece; | 18 using base::StringPiece; |
| 21 | 19 |
| 22 namespace net { | 20 namespace net { |
| 23 | 21 |
| 24 namespace { | 22 namespace { |
| 25 | 23 |
| 26 // The pkcs11t.h header in NSS versions older than 3.14 does not have the CTR | 24 const size_t kKeySize = 16; |
| 27 // and GCM types, so define them here. | 25 const size_t kNoncePrefixSize = 4; |
| 28 #if !defined(CKM_AES_CTR) | |
| 29 #define CKM_AES_CTR 0x00001086 | |
| 30 #define CKM_AES_GCM 0x00001087 | |
| 31 | |
| 32 struct CK_AES_CTR_PARAMS { | |
| 33 CK_ULONG ulCounterBits; | |
| 34 CK_BYTE cb[16]; | |
| 35 }; | |
| 36 | |
| 37 struct CK_GCM_PARAMS { | |
| 38 CK_BYTE_PTR pIv; | |
| 39 CK_ULONG ulIvLen; | |
| 40 CK_BYTE_PTR pAAD; | |
| 41 CK_ULONG ulAADLen; | |
| 42 CK_ULONG ulTagBits; | |
| 43 }; | |
| 44 #endif // CKM_AES_CTR | |
| 45 | |
| 46 typedef SECStatus | |
| 47 (*PK11_DecryptFunction)( | |
| 48 PK11SymKey* symKey, CK_MECHANISM_TYPE mechanism, SECItem* param, | |
| 49 unsigned char* out, unsigned int* outLen, unsigned int maxLen, | |
| 50 const unsigned char* enc, unsigned encLen); | |
| 51 | 26 |
| 52 // On Linux, dynamically link against the system version of libnss3.so. In | 27 // On Linux, dynamically link against the system version of libnss3.so. In |
| 53 // order to continue working on systems without up-to-date versions of NSS, | 28 // order to continue working on systems without up-to-date versions of NSS, |
| 54 // lookup PK11_Decrypt with dlsym. | 29 // lookup PK11_Decrypt with dlsym. |
| 55 | 30 |
| 56 // GcmSupportChecker is a singleton which caches the results of runtime symbol | 31 // GcmSupportChecker is a singleton which caches the results of runtime symbol |
| 57 // resolution of PK11_Decrypt. | 32 // resolution of PK11_Decrypt. |
| 58 class GcmSupportChecker { | 33 class GcmSupportChecker { |
| 59 public: | 34 public: |
| 60 static PK11_DecryptFunction pk11_decrypt_func() { | 35 static PK11_DecryptFunction pk11_decrypt_func() { |
| 61 return pk11_decrypt_func_; | 36 return pk11_decrypt_func_; |
| 62 } | 37 } |
| 63 | 38 |
| 64 static CK_MECHANISM_TYPE aes_key_mechanism() { | |
| 65 return aes_key_mechanism_; | |
| 66 } | |
| 67 | |
| 68 private: | 39 private: |
| 69 friend struct base::DefaultLazyInstanceTraits<GcmSupportChecker>; | 40 friend struct base::DefaultLazyInstanceTraits<GcmSupportChecker>; |
| 70 | 41 |
| 71 GcmSupportChecker() { | 42 GcmSupportChecker() { |
| 72 #if !defined(USE_NSS) | 43 #if !defined(USE_NSS) |
| 73 // Using a bundled version of NSS that is guaranteed to have this symbol. | 44 // Using a bundled version of NSS that is guaranteed to have this symbol. |
| 74 pk11_decrypt_func_ = PK11_Decrypt; | 45 pk11_decrypt_func_ = PK11_Decrypt; |
| 75 #else | 46 #else |
| 76 // Using system NSS libraries and PCKS #11 modules, which may not have the | 47 // Using system NSS libraries and PCKS #11 modules, which may not have the |
| 77 // necessary function (PK11_Decrypt) or mechanism support (CKM_AES_GCM). | 48 // necessary function (PK11_Decrypt) or mechanism support (CKM_AES_GCM). |
| 78 | 49 |
| 79 // If PK11_Decrypt() was successfully resolved, then NSS will support | 50 // If PK11_Decrypt() was successfully resolved, then NSS will support |
| 80 // AES-GCM directly. This was introduced in NSS 3.15. | 51 // AES-GCM directly. This was introduced in NSS 3.15. |
| 81 pk11_decrypt_func_ = (PK11_DecryptFunction)dlsym(RTLD_DEFAULT, | 52 pk11_decrypt_func_ = (PK11_DecryptFunction)dlsym(RTLD_DEFAULT, |
| 82 "PK11_Decrypt"); | 53 "PK11_Decrypt"); |
| 83 if (pk11_decrypt_func_ == NULL) { | |
| 84 aes_key_mechanism_ = CKM_AES_ECB; | |
| 85 } | |
| 86 #endif | 54 #endif |
| 87 } | 55 } |
| 88 | 56 |
| 89 // |pk11_decrypt_func_| stores the runtime symbol resolution of PK11_Decrypt. | 57 // |pk11_decrypt_func_| stores the runtime symbol resolution of PK11_Decrypt. |
| 90 static PK11_DecryptFunction pk11_decrypt_func_; | 58 static PK11_DecryptFunction pk11_decrypt_func_; |
| 91 | |
| 92 // The correct value for |aes_key_mechanism_| is CKM_AES_GCM, but because of | |
| 93 // NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=853285 (to be fixed in | |
| 94 // NSS 3.15), use CKM_AES_ECB for NSS versions older than 3.15. | |
| 95 static CK_MECHANISM_TYPE aes_key_mechanism_; | |
| 96 }; | 59 }; |
| 97 | 60 |
| 98 // static | 61 // static |
| 99 PK11_DecryptFunction GcmSupportChecker::pk11_decrypt_func_ = NULL; | 62 PK11_DecryptFunction GcmSupportChecker::pk11_decrypt_func_ = NULL; |
| 100 | 63 |
| 101 // static | |
| 102 CK_MECHANISM_TYPE GcmSupportChecker::aes_key_mechanism_ = CKM_AES_GCM; | |
| 103 | |
| 104 base::LazyInstance<GcmSupportChecker>::Leaky g_gcm_support_checker = | 64 base::LazyInstance<GcmSupportChecker>::Leaky g_gcm_support_checker = |
| 105 LAZY_INSTANCE_INITIALIZER; | 65 LAZY_INSTANCE_INITIALIZER; |
| 106 | 66 |
| 107 const size_t kNoncePrefixSize = 4; | |
| 108 const size_t kAESNonceSize = 12; | |
| 109 | |
| 110 // Calls PK11_Decrypt if it's available. Otherwise, emulates CKM_AES_GCM using | 67 // Calls PK11_Decrypt if it's available. Otherwise, emulates CKM_AES_GCM using |
| 111 // CKM_AES_CTR and the GaloisHash class. | 68 // CKM_AES_CTR and the GaloisHash class. |
| 112 SECStatus My_Decrypt(PK11SymKey* key, | 69 SECStatus My_Decrypt(PK11SymKey* key, |
| 113 CK_MECHANISM_TYPE mechanism, | 70 CK_MECHANISM_TYPE mechanism, |
| 114 SECItem* param, | 71 SECItem* param, |
| 115 unsigned char* out, | 72 unsigned char* out, |
| 116 unsigned int* out_len, | 73 unsigned int* out_len, |
| 117 unsigned int max_len, | 74 unsigned int max_len, |
| 118 const unsigned char* enc, | 75 const unsigned char* enc, |
| 119 unsigned int enc_len) { | 76 unsigned int enc_len) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 PORT_SetError(SEC_ERROR_BAD_DATA); | 200 PORT_SetError(SEC_ERROR_BAD_DATA); |
| 244 return SECFailure; | 201 return SECFailure; |
| 245 } | 202 } |
| 246 | 203 |
| 247 *out_len = output_len; | 204 *out_len = output_len; |
| 248 return SECSuccess; | 205 return SECSuccess; |
| 249 } | 206 } |
| 250 | 207 |
| 251 } // namespace | 208 } // namespace |
| 252 | 209 |
| 253 Aes128Gcm12Decrypter::Aes128Gcm12Decrypter() { | 210 Aes128Gcm12Decrypter::Aes128Gcm12Decrypter() |
| 211 : AeadBaseDecrypter(CKM_AES_GCM, My_Decrypt, kKeySize, kAuthTagSize, |
| 212 kNoncePrefixSize) { |
| 213 COMPILE_ASSERT(kKeySize <= kMaxKeySize, key_size_too_big); |
| 214 COMPILE_ASSERT(kNoncePrefixSize <= kMaxNoncePrefixSize, |
| 215 nonce_prefix_size_too_big); |
| 254 ignore_result(g_gcm_support_checker.Get()); | 216 ignore_result(g_gcm_support_checker.Get()); |
| 255 } | 217 } |
| 256 | 218 |
| 257 Aes128Gcm12Decrypter::~Aes128Gcm12Decrypter() {} | 219 Aes128Gcm12Decrypter::~Aes128Gcm12Decrypter() {} |
| 258 | 220 |
| 259 // static | 221 void Aes128Gcm12Decrypter::FillAeadParams(StringPiece nonce, |
| 260 bool Aes128Gcm12Decrypter::IsSupported() { | 222 StringPiece associated_data, |
| 261 // NSS 3.15 supports CKM_AES_GCM directly. | 223 size_t auth_tag_size, |
| 262 // NSS 3.14 supports CKM_AES_CTR, which can be used to emulate CKM_AES_GCM. | 224 AeadParams* aead_params) const { |
| 263 // Versions earlier than NSS 3.14 are not supported. | 225 aead_params->len = sizeof(aead_params->data.gcm_params); |
| 264 return NSS_VersionCheck("3.14") != PR_FALSE; | 226 CK_GCM_PARAMS* gcm_params = &aead_params->data.gcm_params; |
| 265 } | 227 gcm_params->pIv = |
| 266 | |
| 267 bool Aes128Gcm12Decrypter::SetKey(StringPiece key) { | |
| 268 DCHECK_EQ(key.size(), sizeof(key_)); | |
| 269 if (key.size() != sizeof(key_)) { | |
| 270 return false; | |
| 271 } | |
| 272 memcpy(key_, key.data(), key.size()); | |
| 273 return true; | |
| 274 } | |
| 275 | |
| 276 bool Aes128Gcm12Decrypter::SetNoncePrefix(StringPiece nonce_prefix) { | |
| 277 DCHECK_EQ(nonce_prefix.size(), kNoncePrefixSize); | |
| 278 if (nonce_prefix.size() != kNoncePrefixSize) { | |
| 279 return false; | |
| 280 } | |
| 281 COMPILE_ASSERT(sizeof(nonce_prefix_) == kNoncePrefixSize, bad_nonce_length); | |
| 282 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); | |
| 283 return true; | |
| 284 } | |
| 285 | |
| 286 bool Aes128Gcm12Decrypter::Decrypt(StringPiece nonce, | |
| 287 StringPiece associated_data, | |
| 288 StringPiece ciphertext, | |
| 289 uint8* output, | |
| 290 size_t* output_length) { | |
| 291 if (ciphertext.length() < kAuthTagSize || | |
| 292 nonce.size() != kNoncePrefixSize + sizeof(QuicPacketSequenceNumber)) { | |
| 293 return false; | |
| 294 } | |
| 295 // NSS 3.14.x incorrectly requires an output buffer at least as long as | |
| 296 // the ciphertext (NSS bug | |
| 297 // https://bugzilla.mozilla.org/show_bug.cgi?id= 853674). Fortunately | |
| 298 // QuicDecrypter::Decrypt() specifies that |output| must be as long as | |
| 299 // |ciphertext| on entry. | |
| 300 size_t plaintext_size = ciphertext.length() - kAuthTagSize; | |
| 301 | |
| 302 // Import key_ into NSS. | |
| 303 SECItem key_item; | |
| 304 key_item.type = siBuffer; | |
| 305 key_item.data = key_; | |
| 306 key_item.len = sizeof(key_); | |
| 307 PK11SlotInfo* slot = PK11_GetInternalSlot(); | |
| 308 // The exact value of the |origin| argument doesn't matter to NSS as long as | |
| 309 // it's not PK11_OriginFortezzaHack, so pass PK11_OriginUnwrap as a | |
| 310 // placeholder. | |
| 311 crypto::ScopedPK11SymKey aes_key(PK11_ImportSymKey( | |
| 312 slot, GcmSupportChecker::aes_key_mechanism(), PK11_OriginUnwrap, | |
| 313 CKA_DECRYPT, &key_item, NULL)); | |
| 314 PK11_FreeSlot(slot); | |
| 315 slot = NULL; | |
| 316 if (!aes_key) { | |
| 317 DVLOG(1) << "PK11_ImportSymKey failed"; | |
| 318 return false; | |
| 319 } | |
| 320 | |
| 321 CK_GCM_PARAMS gcm_params = {0}; | |
| 322 gcm_params.pIv = | |
| 323 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data())); | 228 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data())); |
| 324 gcm_params.ulIvLen = nonce.size(); | 229 gcm_params->ulIvLen = nonce.size(); |
| 325 gcm_params.pAAD = | 230 gcm_params->pAAD = |
| 326 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data())); | 231 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data())); |
| 327 gcm_params.ulAADLen = associated_data.size(); | 232 gcm_params->ulAADLen = associated_data.size(); |
| 328 gcm_params.ulTagBits = kAuthTagSize * 8; | 233 gcm_params->ulTagBits = auth_tag_size * 8; |
| 329 | |
| 330 SECItem param; | |
| 331 param.type = siBuffer; | |
| 332 param.data = reinterpret_cast<unsigned char*>(&gcm_params); | |
| 333 param.len = sizeof(gcm_params); | |
| 334 | |
| 335 unsigned int output_len; | |
| 336 if (My_Decrypt(aes_key.get(), CKM_AES_GCM, ¶m, | |
| 337 output, &output_len, ciphertext.length(), | |
| 338 reinterpret_cast<const unsigned char*>(ciphertext.data()), | |
| 339 ciphertext.length()) != SECSuccess) { | |
| 340 return false; | |
| 341 } | |
| 342 | |
| 343 if (output_len != plaintext_size) { | |
| 344 DVLOG(1) << "Wrong output length"; | |
| 345 return false; | |
| 346 } | |
| 347 *output_length = output_len; | |
| 348 return true; | |
| 349 } | |
| 350 | |
| 351 QuicData* Aes128Gcm12Decrypter::DecryptPacket( | |
| 352 QuicPacketSequenceNumber sequence_number, | |
| 353 StringPiece associated_data, | |
| 354 StringPiece ciphertext) { | |
| 355 if (ciphertext.length() < kAuthTagSize) { | |
| 356 return NULL; | |
| 357 } | |
| 358 size_t plaintext_size; | |
| 359 scoped_ptr<char[]> plaintext(new char[ciphertext.length()]); | |
| 360 | |
| 361 uint8 nonce[kNoncePrefixSize + sizeof(sequence_number)]; | |
| 362 COMPILE_ASSERT(sizeof(nonce) == kAESNonceSize, bad_sequence_number_size); | |
| 363 memcpy(nonce, nonce_prefix_, kNoncePrefixSize); | |
| 364 memcpy(nonce + kNoncePrefixSize, &sequence_number, sizeof(sequence_number)); | |
| 365 if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce), sizeof(nonce)), | |
| 366 associated_data, ciphertext, | |
| 367 reinterpret_cast<uint8*>(plaintext.get()), | |
| 368 &plaintext_size)) { | |
| 369 return NULL; | |
| 370 } | |
| 371 return new QuicData(plaintext.release(), plaintext_size, true); | |
| 372 } | |
| 373 | |
| 374 StringPiece Aes128Gcm12Decrypter::GetKey() const { | |
| 375 return StringPiece(reinterpret_cast<const char*>(key_), sizeof(key_)); | |
| 376 } | |
| 377 | |
| 378 StringPiece Aes128Gcm12Decrypter::GetNoncePrefix() const { | |
| 379 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | |
| 380 kNoncePrefixSize); | |
| 381 } | 234 } |
| 382 | 235 |
| 383 } // namespace net | 236 } // namespace net |
| OLD | NEW |