HADOOP-11422. Check CryptoCodec is AES-CTR for Crypto input/output stream (Yi Liu via Colin P. McCabe)

(cherry picked from commit e369d98ba2)
This commit is contained in:
Colin Patrick Mccabe 2014-12-19 13:11:38 -08:00
parent 22b38cf29a
commit c60e3bdc4b
5 changed files with 72 additions and 0 deletions

View File

@ -82,6 +82,9 @@ Release 2.7.0 - UNRELEASED
HADOOP-11430. Add GenericTestUtils#disableLog, GenericTestUtils#setLogLevel HADOOP-11430. Add GenericTestUtils#disableLog, GenericTestUtils#setLogLevel
(cmccabe) (cmccabe)
HADOOP-11422. Check CryptoCodec is AES-CTR for Crypto input/output stream
(Yi Liu via Colin P. McCabe)
OPTIMIZATIONS OPTIMIZATIONS
HADOOP-11323. WritableComparator#compare keeps reference to byte array. HADOOP-11323. WritableComparator#compare keeps reference to byte array.

View File

@ -113,6 +113,7 @@ public class CryptoInputStream extends FilterInputStream implements
public CryptoInputStream(InputStream in, CryptoCodec codec, public CryptoInputStream(InputStream in, CryptoCodec codec,
int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException { int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException {
super(in); super(in);
CryptoStreamUtils.checkCodec(codec);
this.bufferSize = CryptoStreamUtils.checkBufferSize(codec, bufferSize); this.bufferSize = CryptoStreamUtils.checkBufferSize(codec, bufferSize);
this.codec = codec; this.codec = codec;
this.key = key.clone(); this.key = key.clone();

View File

@ -83,6 +83,7 @@ public class CryptoOutputStream extends FilterOutputStream implements
int bufferSize, byte[] key, byte[] iv, long streamOffset) int bufferSize, byte[] key, byte[] iv, long streamOffset)
throws IOException { throws IOException {
super(out); super(out);
CryptoStreamUtils.checkCodec(codec);
this.bufferSize = CryptoStreamUtils.checkBufferSize(codec, bufferSize); this.bufferSize = CryptoStreamUtils.checkBufferSize(codec, bufferSize);
this.codec = codec; this.codec = codec;
this.key = key.clone(); this.key = key.clone();

View File

@ -49,6 +49,13 @@ public class CryptoStreamUtils {
HADOOP_SECURITY_CRYPTO_BUFFER_SIZE_DEFAULT); HADOOP_SECURITY_CRYPTO_BUFFER_SIZE_DEFAULT);
} }
/** AES/CTR/NoPadding is required */
public static void checkCodec(CryptoCodec codec) {
if (codec.getCipherSuite() != CipherSuite.AES_CTR_NOPADDING) {
throw new UnsupportedCodecException("AES/CTR/NoPadding is required");
}
}
/** Check and floor buffer size */ /** Check and floor buffer size */
public static int checkBufferSize(CryptoCodec codec, int bufferSize) { public static int checkBufferSize(CryptoCodec codec, int bufferSize) {
Preconditions.checkArgument(bufferSize >= MIN_BUFFER_SIZE, Preconditions.checkArgument(bufferSize >= MIN_BUFFER_SIZE,

View File

@ -0,0 +1,60 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.crypto;
/**
* Thrown to indicate that the specific codec is not supported.
*/
public class UnsupportedCodecException extends RuntimeException {
/** Default constructor */
public UnsupportedCodecException() {
}
/**
* Constructs an UnsupportedCodecException with the specified
* detail message.
*
* @param message the detail message
*/
public UnsupportedCodecException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified detail message and
* cause.
*
* @param message the detail message
* @param cause the cause
*/
public UnsupportedCodecException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new exception with the specified cause.
*
* @param cause the cause
*/
public UnsupportedCodecException(Throwable cause) {
super(cause);
}
private static final long serialVersionUID = 6713920435487942224L;
}