1 // Transport Security Layer (TLS) 2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez 3 // Copyright (C) 2006 Novell, Inc (http://www.novell.com) 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining 6 // a copy of this software and associated documentation files (the 7 // "Software"), to deal in the Software without restriction, including 8 // without limitation the rights to use, copy, modify, merge, publish, 9 // distribute, sublicense, and/or sell copies of the Software, and to 10 // permit persons to whom the Software is furnished to do so, subject to 11 // the following conditions: 12 // 13 // The above copyright notice and this permission notice shall be 14 // included in all copies or substantial portions of the Software. 15 // 16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 // 24 25 using System; 26 using System.IO; 27 using System.Security.Cryptography; 28 29 namespace Mono.Security.Protocol.Tls 30 { 31 internal class TlsCipherSuite : CipherSuite 32 { 33 private const int MacHeaderLength = 13; 34 private byte[] header; 35 private object headerLock = new object (); 36 37 #region Constructors 38 TlsCipherSuite( short code, string name, CipherAlgorithmType cipherAlgorithmType, HashAlgorithmType hashAlgorithmType, ExchangeAlgorithmType exchangeAlgorithmType, bool exportable, bool blockMode, byte keyMaterialSize, byte expandedKeyMaterialSize, short effectiveKeyBytes, byte ivSize, byte blockSize)39 public TlsCipherSuite( 40 short code, string name, CipherAlgorithmType cipherAlgorithmType, 41 HashAlgorithmType hashAlgorithmType, ExchangeAlgorithmType exchangeAlgorithmType, 42 bool exportable, bool blockMode, byte keyMaterialSize, 43 byte expandedKeyMaterialSize, short effectiveKeyBytes, 44 byte ivSize, byte blockSize) 45 :base(code, name, cipherAlgorithmType, hashAlgorithmType, 46 exchangeAlgorithmType, exportable, blockMode, keyMaterialSize, 47 expandedKeyMaterialSize, effectiveKeyBytes, ivSize, blockSize) 48 { 49 } 50 51 #endregion 52 53 #region MAC Generation Methods 54 ComputeServerRecordMAC(ContentType contentType, byte[] fragment)55 public override byte[] ComputeServerRecordMAC(ContentType contentType, byte[] fragment) 56 { 57 lock (headerLock) { 58 if (header == null) 59 header = new byte [MacHeaderLength]; 60 61 ulong seqnum = (Context is ClientContext) ? Context.ReadSequenceNumber : Context.WriteSequenceNumber; 62 Write (header, 0, seqnum); 63 header [8] = (byte)contentType; 64 Write (header, 9, this.Context.Protocol); 65 Write (header, 11, (short)fragment.Length); 66 67 HashAlgorithm mac = this.ServerHMAC; 68 mac.TransformBlock (header, 0, header.Length, header, 0); 69 mac.TransformBlock (fragment, 0, fragment.Length, fragment, 0); 70 // hack, else the method will allocate a new buffer of the same length (negative half the optimization) 71 mac.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0); 72 return mac.Hash; 73 } 74 } 75 ComputeClientRecordMAC(ContentType contentType, byte[] fragment)76 public override byte[] ComputeClientRecordMAC(ContentType contentType, byte[] fragment) 77 { 78 lock (headerLock) { 79 if (header == null) 80 header = new byte [MacHeaderLength]; 81 82 ulong seqnum = (Context is ClientContext) ? Context.WriteSequenceNumber : Context.ReadSequenceNumber; 83 Write (header, 0, seqnum); 84 header [8] = (byte)contentType; 85 Write (header, 9, this.Context.Protocol); 86 Write (header, 11, (short)fragment.Length); 87 88 HashAlgorithm mac = this.ClientHMAC; 89 mac.TransformBlock (header, 0, header.Length, header, 0); 90 mac.TransformBlock (fragment, 0, fragment.Length, fragment, 0); 91 // hack, else the method will allocate a new buffer of the same length (negative half the optimization) 92 mac.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0); 93 return mac.Hash; 94 } 95 } 96 97 #endregion 98 99 #region Key Generation Methods 100 ComputeMasterSecret(byte[] preMasterSecret)101 public override void ComputeMasterSecret(byte[] preMasterSecret) 102 { 103 // Create master secret 104 this.Context.MasterSecret = new byte[preMasterSecret.Length]; 105 this.Context.MasterSecret = this.PRF( 106 preMasterSecret, "master secret", this.Context.RandomCS, 48); 107 108 DebugHelper.WriteLine(">>>> MasterSecret", this.Context.MasterSecret); 109 } 110 ComputeKeys()111 public override void ComputeKeys() 112 { 113 // Create keyblock 114 TlsStream keyBlock = new TlsStream( 115 this.PRF( 116 this.Context.MasterSecret, 117 "key expansion", 118 this.Context.RandomSC, 119 this.KeyBlockSize)); 120 121 this.Context.Negotiating.ClientWriteMAC = keyBlock.ReadBytes(this.HashSize); 122 this.Context.Negotiating.ServerWriteMAC = keyBlock.ReadBytes(this.HashSize); 123 this.Context.ClientWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize); 124 this.Context.ServerWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize); 125 126 if (!this.IsExportable) 127 { 128 if (this.IvSize != 0) 129 { 130 this.Context.ClientWriteIV = keyBlock.ReadBytes(this.IvSize); 131 this.Context.ServerWriteIV = keyBlock.ReadBytes(this.IvSize); 132 } 133 else 134 { 135 this.Context.ClientWriteIV = CipherSuite.EmptyArray; 136 this.Context.ServerWriteIV = CipherSuite.EmptyArray; 137 } 138 } 139 else 140 { 141 // Generate final write keys 142 byte[] finalClientWriteKey = PRF(this.Context.ClientWriteKey, "client write key", this.Context.RandomCS, this.ExpandedKeyMaterialSize); 143 byte[] finalServerWriteKey = PRF(this.Context.ServerWriteKey, "server write key", this.Context.RandomCS, this.ExpandedKeyMaterialSize); 144 145 this.Context.ClientWriteKey = finalClientWriteKey; 146 this.Context.ServerWriteKey = finalServerWriteKey; 147 148 if (this.IvSize > 0) 149 { 150 // Generate IV block 151 byte[] ivBlock = PRF(CipherSuite.EmptyArray, "IV block", this.Context.RandomCS, this.IvSize*2); 152 153 // Generate IV keys 154 this.Context.ClientWriteIV = new byte[this.IvSize]; 155 Buffer.BlockCopy(ivBlock, 0, this.Context.ClientWriteIV, 0, this.Context.ClientWriteIV.Length); 156 157 this.Context.ServerWriteIV = new byte[this.IvSize]; 158 Buffer.BlockCopy(ivBlock, this.IvSize, this.Context.ServerWriteIV, 0, this.Context.ServerWriteIV.Length); 159 } 160 else 161 { 162 this.Context.ClientWriteIV = CipherSuite.EmptyArray; 163 this.Context.ServerWriteIV = CipherSuite.EmptyArray; 164 } 165 } 166 167 DebugHelper.WriteLine(">>>> KeyBlock", keyBlock.ToArray()); 168 DebugHelper.WriteLine(">>>> ClientWriteKey", this.Context.ClientWriteKey); 169 DebugHelper.WriteLine(">>>> ClientWriteIV", this.Context.ClientWriteIV); 170 DebugHelper.WriteLine(">>>> ClientWriteMAC", this.Context.Negotiating.ClientWriteMAC); 171 DebugHelper.WriteLine(">>>> ServerWriteKey", this.Context.ServerWriteKey); 172 DebugHelper.WriteLine(">>>> ServerWriteIV", this.Context.ServerWriteIV); 173 DebugHelper.WriteLine(">>>> ServerWriteMAC", this.Context.Negotiating.ServerWriteMAC); 174 175 ClientSessionCache.SetContextInCache (this.Context); 176 // Clear no more needed data 177 keyBlock.Reset(); 178 } 179 180 #endregion 181 } 182 } 183