chibenwa commented on a change in pull request #342: URL: https://github.com/apache/james-project/pull/342#discussion_r602180297
########## File path: server/blob/blob-aes/src/main/java/org/apache/james/blob/aes/AESBlobStoreDAO.java ########## @@ -0,0 +1,155 @@ +/**************************************************************** + * 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.james.blob.aes; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.GeneralSecurityException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; + +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; + +import org.apache.commons.io.IOUtils; +import org.apache.james.blob.api.BlobId; +import org.apache.james.blob.api.BlobStoreDAO; +import org.apache.james.blob.api.BucketName; +import org.apache.james.blob.api.ObjectNotFoundException; +import org.apache.james.blob.api.ObjectStoreIOException; +import org.reactivestreams.Publisher; + +import com.github.fge.lambdas.Throwing; +import com.google.common.base.Preconditions; +import com.google.common.io.ByteSource; +import com.google.crypto.tink.Aead; +import com.google.crypto.tink.aead.AeadConfig; +import com.google.crypto.tink.subtle.AesGcmJce; + +import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; + +public class AESBlobStoreDAO implements BlobStoreDAO { + private static final byte[] EMPTY_ASSOCIATED_DATA = new byte[0]; + private static final int PBKDF2_ITERATIONS = 65536; + private static final int KEY_SIZE = 256; + private static final String SECRET_KEY_FACTORY_ALGORITHM = "PBKDF2WithHmacSHA256"; + + private final BlobStoreDAO underlying; + private final Aead aead; + + public AESBlobStoreDAO(BlobStoreDAO underlying, CryptoConfig cryptoConfig) { + this.underlying = underlying; + + try { + AeadConfig.register(); + + SecretKey secretKey = deriveKey(cryptoConfig); + aead = new AesGcmJce(secretKey.getEncoded()); + } catch (GeneralSecurityException e) { + throw new RuntimeException("Error while starting AESPayloadCodec", e); + } + } + + private static SecretKey deriveKey(CryptoConfig cryptoConfig) throws NoSuchAlgorithmException, InvalidKeySpecException { + byte[] saltBytes = cryptoConfig.salt(); + SecretKeyFactory skf = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_ALGORITHM); + PBEKeySpec spec = new PBEKeySpec(cryptoConfig.password(), saltBytes, PBKDF2_ITERATIONS, KEY_SIZE); + return skf.generateSecret(spec); + } + + public byte[] encrypt(byte[] input) { + try { + return aead.encrypt(input, EMPTY_ASSOCIATED_DATA); + } catch (GeneralSecurityException e) { + throw new RuntimeException("Unable to build payload for object storage, failed to encrypt", e); + } + } + + public byte[] decrypt(byte[] ciphertext) throws IOException { Review comment: Re-found it, last commit contributes an acceptable version. https://github.com/apache/james-project/pull/342/commits/d7e7ad9e95be36df7b595d417f9dca2c26da5192 My proposal just ends up writing data potentially to disk... Acceptable as we prefer having large data on disk (not to trash the memory) - and encrypted data size is unknown unless we have it in some kind of byte source - as Object Storage would require the size... -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
