|
| 1 | +package org.bouncycastle.tls.crypto.impl; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.bouncycastle.tls.AlertDescription; |
| 6 | +import org.bouncycastle.tls.ContentType; |
| 7 | +import org.bouncycastle.tls.ProtocolVersion; |
| 8 | +import org.bouncycastle.tls.SecurityParameters; |
| 9 | +import org.bouncycastle.tls.TlsFatalAlert; |
| 10 | +import org.bouncycastle.tls.TlsUtils; |
| 11 | +import org.bouncycastle.tls.crypto.TlsCipher; |
| 12 | +import org.bouncycastle.tls.crypto.TlsCryptoParameters; |
| 13 | +import org.bouncycastle.tls.crypto.TlsCryptoUtils; |
| 14 | +import org.bouncycastle.tls.crypto.TlsDecodeResult; |
| 15 | +import org.bouncycastle.tls.crypto.TlsEncodeResult; |
| 16 | +import org.bouncycastle.tls.crypto.TlsHMAC; |
| 17 | +import org.bouncycastle.tls.crypto.TlsSecret; |
| 18 | +import org.bouncycastle.util.Arrays; |
| 19 | +import org.bouncycastle.util.Bytes; |
| 20 | + |
| 21 | +/** |
| 22 | + * A generic TLS 1.3 "integrity-only" cipher. |
| 23 | + */ |
| 24 | +public final class Tls13NullCipher |
| 25 | + implements TlsCipher |
| 26 | +{ |
| 27 | + private final TlsCryptoParameters cryptoParams; |
| 28 | + |
| 29 | + private final TlsHMAC readHMAC, writeHMAC; |
| 30 | + private final byte[] readNonce, writeNonce; |
| 31 | + |
| 32 | + public Tls13NullCipher(TlsCryptoParameters cryptoParams, TlsHMAC readHMAC, TlsHMAC writeHMAC) |
| 33 | + throws IOException |
| 34 | + { |
| 35 | + final SecurityParameters securityParameters = cryptoParams.getSecurityParametersHandshake(); |
| 36 | + |
| 37 | + if (!TlsImplUtils.isTLSv13(securityParameters.getNegotiatedVersion())) |
| 38 | + { |
| 39 | + throw new TlsFatalAlert(AlertDescription.internal_error); |
| 40 | + } |
| 41 | + |
| 42 | + this.cryptoParams = cryptoParams; |
| 43 | + this.readHMAC = readHMAC; |
| 44 | + this.writeHMAC = writeHMAC; |
| 45 | + |
| 46 | + this.readNonce = new byte[readHMAC.getMacLength()]; |
| 47 | + this.writeNonce = new byte[writeHMAC.getMacLength()]; |
| 48 | + |
| 49 | + final boolean isServer = cryptoParams.isServer(); |
| 50 | + rekeyHmac(securityParameters, readHMAC, readNonce, !isServer); |
| 51 | + rekeyHmac(securityParameters, writeHMAC, writeNonce, isServer); |
| 52 | + } |
| 53 | + |
| 54 | + public int getCiphertextDecodeLimit(int plaintextLimit) |
| 55 | + { |
| 56 | + return plaintextLimit + 1 + readHMAC.getMacLength(); |
| 57 | + } |
| 58 | + |
| 59 | + public int getCiphertextEncodeLimit(int plaintextLimit) |
| 60 | + { |
| 61 | + return plaintextLimit + 1 + writeHMAC.getMacLength(); |
| 62 | + } |
| 63 | + |
| 64 | + public int getPlaintextDecodeLimit(int ciphertextLimit) |
| 65 | + { |
| 66 | + return ciphertextLimit - readHMAC.getMacLength() - 1; |
| 67 | + } |
| 68 | + |
| 69 | + public int getPlaintextEncodeLimit(int ciphertextLimit) |
| 70 | + { |
| 71 | + return ciphertextLimit - writeHMAC.getMacLength() - 1; |
| 72 | + } |
| 73 | + |
| 74 | + public TlsEncodeResult encodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion, |
| 75 | + int headerAllocation, byte[] plaintext, int plaintextOffset, int plaintextLength) throws IOException |
| 76 | + { |
| 77 | + int macLength = writeHMAC.getMacLength(); |
| 78 | + |
| 79 | + // TODO Possibly redundant if we reset after any failures (i.e. DTLS) |
| 80 | + writeHMAC.reset(); |
| 81 | + |
| 82 | + byte[] nonce = createRecordNonce(writeNonce, seqNo); |
| 83 | + writeHMAC.update(nonce, 0, nonce.length); |
| 84 | + |
| 85 | + // TODO[tls13, cid] If we support adding padding to (D)TLSInnerPlaintext, this will need review |
| 86 | + int innerPlaintextLength = plaintextLength + 1; |
| 87 | + int ciphertextLength = innerPlaintextLength + macLength; |
| 88 | + byte[] output = new byte[headerAllocation + ciphertextLength]; |
| 89 | + int outputPos = headerAllocation; |
| 90 | + |
| 91 | + short recordType = ContentType.application_data; |
| 92 | + |
| 93 | + byte[] additionalData = getAdditionalData(seqNo, recordType, recordVersion, ciphertextLength); |
| 94 | + |
| 95 | + try |
| 96 | + { |
| 97 | + System.arraycopy(plaintext, plaintextOffset, output, outputPos, plaintextLength); |
| 98 | + output[outputPos + plaintextLength] = (byte)contentType; |
| 99 | + |
| 100 | + writeHMAC.update(additionalData, 0, additionalData.length); |
| 101 | + writeHMAC.update(output, outputPos, innerPlaintextLength); |
| 102 | + writeHMAC.calculateMAC(output, outputPos + innerPlaintextLength); |
| 103 | + outputPos += innerPlaintextLength + macLength; |
| 104 | + } |
| 105 | + catch (RuntimeException e) |
| 106 | + { |
| 107 | + throw new TlsFatalAlert(AlertDescription.internal_error, e); |
| 108 | + } |
| 109 | + |
| 110 | + if (outputPos != output.length) |
| 111 | + { |
| 112 | + // NOTE: The additional data mechanism for AEAD ciphers requires exact output size prediction. |
| 113 | + throw new TlsFatalAlert(AlertDescription.internal_error); |
| 114 | + } |
| 115 | + |
| 116 | + return new TlsEncodeResult(output, 0, output.length, recordType); |
| 117 | + } |
| 118 | + |
| 119 | + public TlsDecodeResult decodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion, |
| 120 | + byte[] ciphertext, int ciphertextOffset, int ciphertextLength) throws IOException |
| 121 | + { |
| 122 | + int macLength = readHMAC.getMacLength(); |
| 123 | + |
| 124 | + int innerPlaintextLength = ciphertextLength - macLength; |
| 125 | + if (innerPlaintextLength < 1) |
| 126 | + { |
| 127 | + throw new TlsFatalAlert(AlertDescription.decode_error); |
| 128 | + } |
| 129 | + |
| 130 | + // TODO Possibly redundant if we reset after any failures (i.e. DTLS) |
| 131 | + readHMAC.reset(); |
| 132 | + |
| 133 | + byte[] nonce = createRecordNonce(readNonce, seqNo); |
| 134 | + readHMAC.update(nonce, 0, nonce.length); |
| 135 | + |
| 136 | + byte[] additionalData = getAdditionalData(seqNo, recordType, recordVersion, ciphertextLength); |
| 137 | + |
| 138 | + try |
| 139 | + { |
| 140 | + readHMAC.update(additionalData, 0, additionalData.length); |
| 141 | + readHMAC.update(ciphertext, ciphertextOffset, innerPlaintextLength); |
| 142 | + byte[] calculated = readHMAC.calculateMAC(); |
| 143 | + if (!Arrays.constantTimeAreEqual(macLength, calculated, 0, ciphertext, ciphertextOffset + innerPlaintextLength)) |
| 144 | + { |
| 145 | + throw new TlsFatalAlert(AlertDescription.bad_record_mac); |
| 146 | + } |
| 147 | + } |
| 148 | + catch (RuntimeException e) |
| 149 | + { |
| 150 | + throw new TlsFatalAlert(AlertDescription.bad_record_mac, e); |
| 151 | + } |
| 152 | + |
| 153 | + short contentType = recordType; |
| 154 | + int plaintextLength = innerPlaintextLength; |
| 155 | + |
| 156 | + // Strip padding and read true content type from TLSInnerPlaintext |
| 157 | + for (;;) |
| 158 | + { |
| 159 | + if (--plaintextLength < 0) |
| 160 | + { |
| 161 | + throw new TlsFatalAlert(AlertDescription.unexpected_message); |
| 162 | + } |
| 163 | + |
| 164 | + byte octet = ciphertext[ciphertextOffset + plaintextLength]; |
| 165 | + if (0 != octet) |
| 166 | + { |
| 167 | + contentType = (short)(octet & 0xFF); |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return new TlsDecodeResult(ciphertext, ciphertextOffset, plaintextLength, contentType); |
| 173 | + } |
| 174 | + |
| 175 | + public void rekeyDecoder() throws IOException |
| 176 | + { |
| 177 | + rekeyHmac(cryptoParams.getSecurityParametersConnection(), readHMAC, readNonce, !cryptoParams.isServer()); |
| 178 | + } |
| 179 | + |
| 180 | + public void rekeyEncoder() throws IOException |
| 181 | + { |
| 182 | + rekeyHmac(cryptoParams.getSecurityParametersConnection(), writeHMAC, writeNonce, cryptoParams.isServer()); |
| 183 | + } |
| 184 | + |
| 185 | + public boolean usesOpaqueRecordTypeDecode() |
| 186 | + { |
| 187 | + return true; |
| 188 | + } |
| 189 | + |
| 190 | + public boolean usesOpaqueRecordTypeEncode() |
| 191 | + { |
| 192 | + return true; |
| 193 | + } |
| 194 | + |
| 195 | + private void rekeyHmac(SecurityParameters securityParameters, TlsHMAC hmac, byte[] nonce, boolean serverSecret) |
| 196 | + throws IOException |
| 197 | + { |
| 198 | + TlsSecret secret = serverSecret |
| 199 | + ? securityParameters.getTrafficSecretServer() |
| 200 | + : securityParameters.getTrafficSecretClient(); |
| 201 | + |
| 202 | + // TODO[tls13] For early data, have to disable server->client |
| 203 | + if (null == secret) |
| 204 | + { |
| 205 | + throw new TlsFatalAlert(AlertDescription.internal_error); |
| 206 | + } |
| 207 | + |
| 208 | + setupHmac(hmac, nonce, secret, securityParameters.getPRFCryptoHashAlgorithm()); |
| 209 | + } |
| 210 | + |
| 211 | + private void setupHmac(TlsHMAC hmac, byte[] nonce, TlsSecret secret, int cryptoHashAlgorithm) |
| 212 | + throws IOException |
| 213 | + { |
| 214 | + int length = hmac.getMacLength(); |
| 215 | + byte[] key = hkdfExpandLabel(secret, cryptoHashAlgorithm, "key", length).extract(); |
| 216 | + byte[] iv = hkdfExpandLabel(secret, cryptoHashAlgorithm, "iv", length).extract(); |
| 217 | + |
| 218 | + hmac.setKey(key, 0, length); |
| 219 | + System.arraycopy(iv, 0, nonce, 0, length); |
| 220 | + } |
| 221 | + |
| 222 | + private static byte[] createRecordNonce(byte[] fixedNonce, long seqNo) |
| 223 | + { |
| 224 | + int nonceLength = fixedNonce.length; |
| 225 | + byte[] nonce = new byte[nonceLength]; |
| 226 | + TlsUtils.writeUint64(seqNo, nonce, nonceLength - 8); |
| 227 | + Bytes.xorTo(nonceLength, fixedNonce, nonce); |
| 228 | + return nonce; |
| 229 | + } |
| 230 | + |
| 231 | + private static byte[] getAdditionalData(long seqNo, short recordType, ProtocolVersion recordVersion, |
| 232 | + int ciphertextLength) throws IOException |
| 233 | + { |
| 234 | + /* |
| 235 | + * TLSCiphertext.opaque_type || TLSCiphertext.legacy_record_version || TLSCiphertext.length |
| 236 | + */ |
| 237 | + byte[] additional_data = new byte[5]; |
| 238 | + TlsUtils.writeUint8(recordType, additional_data, 0); |
| 239 | + TlsUtils.writeVersion(recordVersion, additional_data, 1); |
| 240 | + TlsUtils.writeUint16(ciphertextLength, additional_data, 3); |
| 241 | + return additional_data; |
| 242 | + } |
| 243 | + |
| 244 | + private static TlsSecret hkdfExpandLabel(TlsSecret secret, int cryptoHashAlgorithm, String label, int length) |
| 245 | + throws IOException |
| 246 | + { |
| 247 | + return TlsCryptoUtils.hkdfExpandLabel(secret, cryptoHashAlgorithm, label, TlsUtils.EMPTY_BYTES, length); |
| 248 | + } |
| 249 | +} |
0 commit comments