Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(636)

Side by Side Diff: net/third_party/nss/ssl/ssl3con.c

Issue 23713003: Use the TLS 1.2 mechanisms for PKCS #11. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Re-implement ssl3_ComputeTLSFinished using CKM_TLS12_MAC, for all versions of TLS Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | net/third_party/nss/ssl/ssl3ecc.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* 2 /*
3 * SSL3 Protocol 3 * SSL3 Protocol
4 * 4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public 5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 8
9 /* TODO(ekr): Implement HelloVerifyRequest on server side. OK for now. */ 9 /* TODO(ekr): Implement HelloVerifyRequest on server side. OK for now. */
10 10
(...skipping 17 matching lines...) Expand all
28 #include "pk11func.h" 28 #include "pk11func.h"
29 #include "secmod.h" 29 #include "secmod.h"
30 #ifndef NO_PKCS11_BYPASS 30 #ifndef NO_PKCS11_BYPASS
31 #include "blapi.h" 31 #include "blapi.h"
32 #endif 32 #endif
33 33
34 /* This is a bodge to allow this code to be compiled against older NSS headers 34 /* This is a bodge to allow this code to be compiled against older NSS headers
35 * that don't contain the TLS 1.2 changes. */ 35 * that don't contain the TLS 1.2 changes. */
36 #ifndef CKM_NSS_TLS_PRF_GENERAL_SHA256 36 #ifndef CKM_NSS_TLS_PRF_GENERAL_SHA256
37 #define CKM_NSS_TLS_PRF_GENERAL_SHA256 (CKM_NSS + 21) 37 #define CKM_NSS_TLS_PRF_GENERAL_SHA256 (CKM_NSS + 21)
38 #define CKM_NSS_TLS_MASTER_KEY_DERIVE_SHA256 (CKM_NSS + 22) 38 #define CKM_TLS12_MASTER_KEY_DERIVE 0x000003E0
39 #define CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256 (CKM_NSS + 23) 39 #define CKM_TLS12_KEY_AND_MAC_DERIVE 0x000003E1
40 #define CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256 (CKM_NSS + 24) 40 #define CKM_TLS12_MASTER_KEY_DERIVE_DH 0x000003E2
41 #endif 41 #endif
42 42
43 #include <stdio.h> 43 #include <stdio.h>
44 #ifdef NSS_ENABLE_ZLIB 44 #ifdef NSS_ENABLE_ZLIB
45 #include "zlib.h" 45 #include "zlib.h"
46 #endif 46 #endif
47 #ifdef LINUX 47 #ifdef LINUX
48 #include <dlfcn.h> 48 #include <dlfcn.h>
49 #endif 49 #endif
50 50
(...skipping 3545 matching lines...) Expand 10 before | Expand all | Expand 10 after
3596 * data into a 48-byte value. 3596 * data into a 48-byte value.
3597 */ 3597 */
3598 PRBool isDH = (PRBool) ((ss->ssl3.hs.kea_def->exchKeyType == kt_dh) || 3598 PRBool isDH = (PRBool) ((ss->ssl3.hs.kea_def->exchKeyType == kt_dh) ||
3599 (ss->ssl3.hs.kea_def->exchKeyType == kt_ecdh)); 3599 (ss->ssl3.hs.kea_def->exchKeyType == kt_ecdh));
3600 SECStatus rv = SECFailure; 3600 SECStatus rv = SECFailure;
3601 CK_MECHANISM_TYPE master_derive; 3601 CK_MECHANISM_TYPE master_derive;
3602 CK_MECHANISM_TYPE key_derive; 3602 CK_MECHANISM_TYPE key_derive;
3603 SECItem params; 3603 SECItem params;
3604 CK_FLAGS keyFlags; 3604 CK_FLAGS keyFlags;
3605 CK_VERSION pms_version; 3605 CK_VERSION pms_version;
3606 CK_SSL3_MASTER_KEY_DERIVE_PARAMS master_params; 3606 /* master_params may be used as a CK_SSL3_MASTER_KEY_DERIVE_PARAMS */
3607 CK_TLS12_MASTER_KEY_DERIVE_PARAMS master_params;
3608 unsigned int master_params_len;
3607 3609
3608 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); 3610 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
3609 PORT_Assert( ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); 3611 PORT_Assert( ss->opt.noLocks || ssl_HaveSpecWriteLock(ss));
3610 PORT_Assert(ss->ssl3.prSpec == ss->ssl3.pwSpec); 3612 PORT_Assert(ss->ssl3.prSpec == ss->ssl3.pwSpec);
3611 if (isTLS12) { 3613 if (isTLS12) {
3612 » if(isDH) master_derive = CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256; 3614 » if(isDH) master_derive = CKM_TLS12_MASTER_KEY_DERIVE_DH;
3613 » else master_derive = CKM_NSS_TLS_MASTER_KEY_DERIVE_SHA256; 3615 » else master_derive = CKM_TLS12_MASTER_KEY_DERIVE;
3614 » key_derive = CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256; 3616 » key_derive = CKM_TLS12_KEY_AND_MAC_DERIVE;
3615 keyFlags = CKF_SIGN | CKF_VERIFY; 3617 keyFlags = CKF_SIGN | CKF_VERIFY;
3616 } else if (isTLS) { 3618 } else if (isTLS) {
3617 if(isDH) master_derive = CKM_TLS_MASTER_KEY_DERIVE_DH; 3619 if(isDH) master_derive = CKM_TLS_MASTER_KEY_DERIVE_DH;
3618 else master_derive = CKM_TLS_MASTER_KEY_DERIVE; 3620 else master_derive = CKM_TLS_MASTER_KEY_DERIVE;
3619 key_derive = CKM_TLS_KEY_AND_MAC_DERIVE; 3621 key_derive = CKM_TLS_KEY_AND_MAC_DERIVE;
3620 keyFlags = CKF_SIGN | CKF_VERIFY; 3622 keyFlags = CKF_SIGN | CKF_VERIFY;
3621 } else { 3623 } else {
3622 if (isDH) master_derive = CKM_SSL3_MASTER_KEY_DERIVE_DH; 3624 if (isDH) master_derive = CKM_SSL3_MASTER_KEY_DERIVE_DH;
3623 else master_derive = CKM_SSL3_MASTER_KEY_DERIVE; 3625 else master_derive = CKM_SSL3_MASTER_KEY_DERIVE;
3624 key_derive = CKM_SSL3_KEY_AND_MAC_DERIVE; 3626 key_derive = CKM_SSL3_KEY_AND_MAC_DERIVE;
3625 keyFlags = 0; 3627 keyFlags = 0;
3626 } 3628 }
3627 3629
3628 if (pms || !pwSpec->master_secret) { 3630 if (pms || !pwSpec->master_secret) {
3629 if (isDH) { 3631 if (isDH) {
3630 master_params.pVersion = NULL; 3632 master_params.pVersion = NULL;
3631 } else { 3633 } else {
3632 master_params.pVersion = &pms_version; 3634 master_params.pVersion = &pms_version;
3633 } 3635 }
3634 master_params.RandomInfo.pClientRandom = cr; 3636 master_params.RandomInfo.pClientRandom = cr;
3635 master_params.RandomInfo.ulClientRandomLen = SSL3_RANDOM_LENGTH; 3637 master_params.RandomInfo.ulClientRandomLen = SSL3_RANDOM_LENGTH;
3636 master_params.RandomInfo.pServerRandom = sr; 3638 master_params.RandomInfo.pServerRandom = sr;
3637 master_params.RandomInfo.ulServerRandomLen = SSL3_RANDOM_LENGTH; 3639 master_params.RandomInfo.ulServerRandomLen = SSL3_RANDOM_LENGTH;
3640 if (isTLS12) {
3641 master_params.prfHashMechanism = CKM_SHA256;
3642 master_params_len = sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS);
3643 } else {
3644 master_params_len = sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS);
3645 }
3638 3646
3639 params.data = (unsigned char *) &master_params; 3647 params.data = (unsigned char *) &master_params;
3640 » params.len = sizeof master_params; 3648 » params.len = master_params_len;
3641 } 3649 }
3642 3650
3643 if (pms != NULL) { 3651 if (pms != NULL) {
3644 #if defined(TRACE) 3652 #if defined(TRACE)
3645 if (ssl_trace >= 100) { 3653 if (ssl_trace >= 100) {
3646 SECStatus extractRV = PK11_ExtractKeyValue(pms); 3654 SECStatus extractRV = PK11_ExtractKeyValue(pms);
3647 if (extractRV == SECSuccess) { 3655 if (extractRV == SECSuccess) {
3648 SECItem * keyData = PK11_GetKeyData(pms); 3656 SECItem * keyData = PK11_GetKeyData(pms);
3649 if (keyData && keyData->data && keyData->len) { 3657 if (keyData && keyData->data && keyData->len) {
3650 ssl_PrintBuf(ss, "Pre-Master Secret", 3658 ssl_PrintBuf(ss, "Pre-Master Secret",
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
3760 PRBool isTLS = (PRBool)(kea_def->tls_keygen || 3768 PRBool isTLS = (PRBool)(kea_def->tls_keygen ||
3761 (pwSpec->version > SSL_LIBRARY_VERSION_3_0)); 3769 (pwSpec->version > SSL_LIBRARY_VERSION_3_0));
3762 PRBool isTLS12= 3770 PRBool isTLS12=
3763 (PRBool)(isTLS && pwSpec->version >= SSL_LIBRARY_VERSION_TLS_1_2); 3771 (PRBool)(isTLS && pwSpec->version >= SSL_LIBRARY_VERSION_TLS_1_2);
3764 /* following variables used in PKCS11 path */ 3772 /* following variables used in PKCS11 path */
3765 const ssl3BulkCipherDef *cipher_def = pwSpec->cipher_def; 3773 const ssl3BulkCipherDef *cipher_def = pwSpec->cipher_def;
3766 PK11SlotInfo * slot = NULL; 3774 PK11SlotInfo * slot = NULL;
3767 PK11SymKey * symKey = NULL; 3775 PK11SymKey * symKey = NULL;
3768 void * pwArg = ss->pkcs11PinArg; 3776 void * pwArg = ss->pkcs11PinArg;
3769 int keySize; 3777 int keySize;
3770 CK_SSL3_KEY_MAT_PARAMS key_material_params; 3778 CK_TLS12_KEY_MAT_PARAMS key_material_params; /* may be used as a
3779 » » » » » » * CK_SSL3_KEY_MAT_PARAMS */
3780 unsigned int key_material_params_len;
3771 CK_SSL3_KEY_MAT_OUT returnedKeys; 3781 CK_SSL3_KEY_MAT_OUT returnedKeys;
3772 CK_MECHANISM_TYPE key_derive; 3782 CK_MECHANISM_TYPE key_derive;
3773 CK_MECHANISM_TYPE bulk_mechanism; 3783 CK_MECHANISM_TYPE bulk_mechanism;
3774 SSLCipherAlgorithm calg; 3784 SSLCipherAlgorithm calg;
3775 SECItem params; 3785 SECItem params;
3776 PRBool skipKeysAndIVs = (PRBool)(cipher_def->calg == calg_null); 3786 PRBool skipKeysAndIVs = (PRBool)(cipher_def->calg == calg_null);
3777 3787
3778 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); 3788 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
3779 PORT_Assert( ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); 3789 PORT_Assert( ss->opt.noLocks || ssl_HaveSpecWriteLock(ss));
3780 PORT_Assert(ss->ssl3.prSpec == ss->ssl3.pwSpec); 3790 PORT_Assert(ss->ssl3.prSpec == ss->ssl3.pwSpec);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
3814 key_material_params.ulKeySizeInBits = 0; 3824 key_material_params.ulKeySizeInBits = 0;
3815 key_material_params.ulIVSizeInBits = 0; 3825 key_material_params.ulIVSizeInBits = 0;
3816 returnedKeys.pIVClient = NULL; 3826 returnedKeys.pIVClient = NULL;
3817 returnedKeys.pIVServer = NULL; 3827 returnedKeys.pIVServer = NULL;
3818 } 3828 }
3819 3829
3820 calg = cipher_def->calg; 3830 calg = cipher_def->calg;
3821 PORT_Assert( alg2Mech[calg].calg == calg); 3831 PORT_Assert( alg2Mech[calg].calg == calg);
3822 bulk_mechanism = alg2Mech[calg].cmech; 3832 bulk_mechanism = alg2Mech[calg].cmech;
3823 3833
3824 params.data = (unsigned char *)&key_material_params;
3825 params.len = sizeof(key_material_params);
3826
3827 if (isTLS12) { 3834 if (isTLS12) {
3828 » key_derive = CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256; 3835 » key_derive = CKM_TLS12_KEY_AND_MAC_DERIVE;
3836 » key_material_params.prfHashMechanism = CKM_SHA256;
3837 » key_material_params_len = sizeof(CK_TLS12_KEY_MAT_PARAMS);
3829 } else if (isTLS) { 3838 } else if (isTLS) {
3830 key_derive = CKM_TLS_KEY_AND_MAC_DERIVE; 3839 key_derive = CKM_TLS_KEY_AND_MAC_DERIVE;
3840 key_material_params_len = sizeof(CK_SSL3_KEY_MAT_PARAMS);
3831 } else { 3841 } else {
3832 key_derive = CKM_SSL3_KEY_AND_MAC_DERIVE; 3842 key_derive = CKM_SSL3_KEY_AND_MAC_DERIVE;
3843 key_material_params_len = sizeof(CK_SSL3_KEY_MAT_PARAMS);
3833 } 3844 }
3834 3845
3846 params.data = (unsigned char *)&key_material_params;
3847 params.len = key_material_params_len;
3848
3835 /* CKM_SSL3_KEY_AND_MAC_DERIVE is defined to set ENCRYPT, DECRYPT, and 3849 /* CKM_SSL3_KEY_AND_MAC_DERIVE is defined to set ENCRYPT, DECRYPT, and
3836 * DERIVE by DEFAULT */ 3850 * DERIVE by DEFAULT */
3837 symKey = PK11_Derive(pwSpec->master_secret, key_derive, &params, 3851 symKey = PK11_Derive(pwSpec->master_secret, key_derive, &params,
3838 bulk_mechanism, CKA_ENCRYPT, keySize); 3852 bulk_mechanism, CKA_ENCRYPT, keySize);
3839 if (!symKey) { 3853 if (!symKey) {
3840 ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE); 3854 ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE);
3841 return SECFailure; 3855 return SECFailure;
3842 } 3856 }
3843 /* we really should use the actual mac'ing mechanism here, but we 3857 /* we really should use the actual mac'ing mechanism here, but we
3844 * don't because these types are used to map keytype anyway and both 3858 * don't because these types are used to map keytype anyway and both
(...skipping 6245 matching lines...) Expand 10 before | Expand all | Expand 10 after
10090 10104
10091 return rv; 10105 return rv;
10092 } 10106 }
10093 10107
10094 static SECStatus 10108 static SECStatus
10095 ssl3_ComputeTLSFinished(ssl3CipherSpec *spec, 10109 ssl3_ComputeTLSFinished(ssl3CipherSpec *spec,
10096 PRBool isServer, 10110 PRBool isServer,
10097 const SSL3Hashes * hashes, 10111 const SSL3Hashes * hashes,
10098 TLSFinished * tlsFinished) 10112 TLSFinished * tlsFinished)
10099 { 10113 {
10100 const char * label; 10114 SECStatus rv;
10101 unsigned int len; 10115 CK_TLS12_MAC_PARAMS tls12_mac_params;
10102 SECStatus rv; 10116 SECItem param = {siBuffer, NULL, 0};
10117 PK11Context *prf_context;
10118 unsigned int retLen;
10103 10119
10104 label = isServer ? "server finished" : "client finished"; 10120 if (spec->version < SSL_LIBRARY_VERSION_TLS_1_2) {
10105 len = 15; 10121 » tls12_mac_params.prfHashMechanism = CKM_TLS_PRF;
10122 } else {
10123 » tls12_mac_params.prfHashMechanism = CKM_SHA256;
10124 }
10125 tls12_mac_params.ulMacLength = 12;
10126 tls12_mac_params.ulServerOrClient = isServer ? 1 : 2;
10127 param.data = (unsigned char *)&tls12_mac_params;
10128 param.len = sizeof(tls12_mac_params);
10129 prf_context = PK11_CreateContextBySymKey(CKM_TLS12_MAC, CKA_SIGN,
10130 » » » » » spec->master_secret, &param);
10131 if (!prf_context)
10132 » return SECFailure;
10106 10133
10107 rv = ssl3_TLSPRFWithMasterSecret(spec, label, len, hashes->u.raw, 10134 rv = PK11_DigestBegin(prf_context);
10108 » hashes->len, tlsFinished->verify_data, 10135 rv |= PK11_DigestOp(prf_context, hashes->u.raw, hashes->len);
10109 » sizeof tlsFinished->verify_data); 10136 rv |= PK11_DigestFinal(prf_context, tlsFinished->verify_data, &retLen,
10137 » » » sizeof tlsFinished->verify_data);
10138 PORT_Assert(rv != SECSuccess || retLen == sizeof tlsFinished->verify_data);
10139
10140 PK11_DestroyContext(prf_context, PR_TRUE);
10110 10141
10111 return rv; 10142 return rv;
10112 } 10143 }
10113 10144
10114 /* The calling function must acquire and release the appropriate 10145 /* The calling function must acquire and release the appropriate
10115 * lock (e.g., ssl_GetSpecReadLock / ssl_ReleaseSpecReadLock for 10146 * lock (e.g., ssl_GetSpecReadLock / ssl_ReleaseSpecReadLock for
10116 * ss->ssl3.crSpec). 10147 * ss->ssl3.crSpec).
10117 */ 10148 */
10118 SECStatus 10149 SECStatus
10119 ssl3_TLSPRFWithMasterSecret(ssl3CipherSpec *spec, const char *label, 10150 ssl3_TLSPRFWithMasterSecret(ssl3CipherSpec *spec, const char *label,
(...skipping 2144 matching lines...) Expand 10 before | Expand all | Expand 10 after
12264 PORT_Free(ss->ssl3.hs.recvdFragments.buf); 12295 PORT_Free(ss->ssl3.hs.recvdFragments.buf);
12265 } 12296 }
12266 } 12297 }
12267 12298
12268 ss->ssl3.initialized = PR_FALSE; 12299 ss->ssl3.initialized = PR_FALSE;
12269 12300
12270 SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE); 12301 SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE);
12271 } 12302 }
12272 12303
12273 /* End of ssl3con.c */ 12304 /* End of ssl3con.c */
OLDNEW
« no previous file with comments | « no previous file | net/third_party/nss/ssl/ssl3ecc.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698