Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading Conscrypt to 2.5.1 #7342

Merged
merged 2 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 44 additions & 55 deletions alts/src/main/java/io/grpc/alts/internal/AltsChannelCrypter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package io.grpc.alts.internal;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Verify.verify;

import com.google.common.annotations.VisibleForTesting;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.util.List;
Expand Down Expand Up @@ -56,72 +56,61 @@ static int getCounterLength() {

@Override
public void encrypt(ByteBuf outBuf, List<ByteBuf> plainBufs) throws GeneralSecurityException {
byte[] tempArr = new byte[outBuf.writableBytes()];

// Copy plaintext into tempArr.
{
ByteBuf tempBuf = Unpooled.wrappedBuffer(tempArr, 0, tempArr.length - TAG_LENGTH);
tempBuf.resetWriterIndex();
for (ByteBuf plainBuf : plainBufs) {
tempBuf.writeBytes(plainBuf);
}
checkArgument(outBuf.nioBufferCount() == 1);
// Copy plaintext buffers into outBuf for in-place encryption on single direct buffer.
ByteBuf plainBuf = outBuf.slice(outBuf.writerIndex(), outBuf.writableBytes());
plainBuf.writerIndex(0);
for (ByteBuf inBuf : plainBufs) {
plainBuf.writeBytes(inBuf);
}

// Encrypt into tempArr.
{
ByteBuffer out = ByteBuffer.wrap(tempArr);
ByteBuffer plain = ByteBuffer.wrap(tempArr, 0, tempArr.length - TAG_LENGTH);

byte[] counter = incrementOutCounter();
aeadCrypter.encrypt(out, plain, counter);
}
outBuf.writeBytes(tempArr);
verify(outBuf.writableBytes() == plainBuf.readableBytes() + TAG_LENGTH);
ByteBuffer out = outBuf.internalNioBuffer(outBuf.writerIndex(), outBuf.writableBytes());
ByteBuffer plain = out.duplicate();
plain.limit(out.limit() - TAG_LENGTH);

byte[] counter = incrementOutCounter();
int outPosition = out.position();
aeadCrypter.encrypt(out, plain, counter);
int bytesWritten = out.position() - outPosition;
outBuf.writerIndex(outBuf.writerIndex() + bytesWritten);
verify(!outBuf.isWritable());
}

@Override
public void decrypt(ByteBuf outBuf, ByteBuf tagBuf, List<ByteBuf> ciphertextBufs)
public void decrypt(ByteBuf out, ByteBuf tag, List<ByteBuf> ciphertextBufs)
throws GeneralSecurityException {
// There is enough space for the ciphertext including the tag in outBuf.
byte[] tempArr = new byte[outBuf.writableBytes()];

// Copy ciphertext and tag into tempArr.
{
ByteBuf tempBuf = Unpooled.wrappedBuffer(tempArr);
tempBuf.resetWriterIndex();
for (ByteBuf ciphertextBuf : ciphertextBufs) {
tempBuf.writeBytes(ciphertextBuf);
}
tempBuf.writeBytes(tagBuf);
}

decryptInternal(outBuf, tempArr);
}
ByteBuf cipherTextAndTag = out.slice(out.writerIndex(), out.writableBytes());
cipherTextAndTag.writerIndex(0);

@Override
public void decrypt(
ByteBuf outBuf, ByteBuf ciphertextAndTagDirect) throws GeneralSecurityException {
byte[] tempArr = new byte[ciphertextAndTagDirect.readableBytes()];

// Copy ciphertext and tag into tempArr.
{
ByteBuf tempBuf = Unpooled.wrappedBuffer(tempArr);
tempBuf.resetWriterIndex();
tempBuf.writeBytes(ciphertextAndTagDirect);
for (ByteBuf inBuf : ciphertextBufs) {
cipherTextAndTag.writeBytes(inBuf);
}
cipherTextAndTag.writeBytes(tag);

decryptInternal(outBuf, tempArr);
decrypt(out, cipherTextAndTag);
}

private void decryptInternal(ByteBuf outBuf, byte[] tempArr) throws GeneralSecurityException {
// Perform in-place decryption on tempArr.
{
ByteBuffer ciphertextAndTag = ByteBuffer.wrap(tempArr);
ByteBuffer out = ByteBuffer.wrap(tempArr);
byte[] counter = incrementInCounter();
aeadCrypter.decrypt(out, ciphertextAndTag, counter);
}

outBuf.writeBytes(tempArr, 0, tempArr.length - TAG_LENGTH);
@Override
public void decrypt(ByteBuf out, ByteBuf ciphertextAndTag) throws GeneralSecurityException {
int bytesRead = ciphertextAndTag.readableBytes();
checkArgument(bytesRead == out.writableBytes());

checkArgument(out.nioBufferCount() == 1);
ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());

checkArgument(ciphertextAndTag.nioBufferCount() == 1);
ByteBuffer ciphertextAndTagBuffer =
ciphertextAndTag.nioBuffer(ciphertextAndTag.readerIndex(), bytesRead);

byte[] counter = incrementInCounter();
int outPosition = outBuffer.position();
aeadCrypter.decrypt(outBuffer, ciphertextAndTagBuffer, counter);
int bytesWritten = outBuffer.position() - outPosition;
out.writerIndex(out.writerIndex() + bytesWritten);
ciphertextAndTag.readerIndex(out.readerIndex() + bytesRead);
verify(out.writableBytes() == TAG_LENGTH);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ subprojects {
// examples/example-tls/pom.xml
netty_tcnative: 'io.netty:netty-tcnative-boringssl-static:2.0.30.Final',

conscrypt: 'org.conscrypt:conscrypt-openjdk-uber:2.2.1',
conscrypt: 'org.conscrypt:conscrypt-openjdk-uber:2.5.1',
re2j: 'com.google.re2j:re2j:1.2',

bouncycastle: 'org.bouncycastle:bcpkix-jdk15on:1.61',
Expand Down