milleruntime commented on a change in pull request #560: Provide new Crypto 
interface & impl
URL: https://github.com/apache/accumulo/pull/560#discussion_r207269106
 
 

 ##########
 File path: 
core/src/main/java/org/apache/accumulo/core/security/crypto/impl/AESCryptoService.java
 ##########
 @@ -0,0 +1,518 @@
+/*
+ * 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.accumulo.core.security.crypto.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.SecureRandom;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.CipherOutputStream;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.GCMParameterSpec;
+import javax.crypto.spec.IvParameterSpec;
+
+import org.apache.accumulo.core.security.crypto.CryptoUtils;
+import org.apache.accumulo.core.security.crypto.streams.BlockedInputStream;
+import org.apache.accumulo.core.security.crypto.streams.BlockedOutputStream;
+import 
org.apache.accumulo.core.security.crypto.streams.DiscardCloseOutputStream;
+import 
org.apache.accumulo.core.security.crypto.streams.RFileCipherOutputStream;
+import org.apache.accumulo.core.spi.crypto.CryptoEnvironment;
+import org.apache.accumulo.core.spi.crypto.CryptoService;
+import org.apache.accumulo.core.spi.crypto.FileDecrypter;
+import org.apache.accumulo.core.spi.crypto.FileEncrypter;
+
+/**
+ * Example implementation of AES encryption for Accumulo
+ */
+public class AESCryptoService implements CryptoService {
+
+  // Hard coded NoCryptoService.VERSION - this permits the removal of 
NoCryptoService from the
+  // core jar, allowing use of only one crypto service
+  private static final String NO_CRYPTO_VERSION = "U+1F47B";
+
+  private Key encryptingKek = null;
+  private String encryptingKekId = null;
+  private String encryptingKeyManager = null;
+  // Lets just load keks for reading once
+  private static HashMap<String,Key> decryptingKeys = new 
HashMap<String,Key>();
+
+  @Override
+  public void init(Map<String,String> conf) throws CryptoException {
+    String kekId = conf.get("instance.crypto.opts.kekId");
+    String keyMgr = conf.get("instance.crypto.opts.keyManager");
+    Objects.requireNonNull(kekId, "Config property table.crypto.opts.kekId is 
required.");
+    Objects.requireNonNull(keyMgr, "Config property 
table.crypto.opts.keyManager is required.");
+    switch (keyMgr) {
+      case KeyManager.URI:
+        this.encryptingKeyManager = keyMgr;
+        this.encryptingKekId = kekId;
+        this.encryptingKek = KeyManager.loadKekFromUri(kekId);
+        break;
+      default:
+        throw new CryptoException("Unrecognized key manager");
+    }
+
+  }
+
+  @Override
+  public FileEncrypter getFileEncrypter(CryptoEnvironment environment) {
+    CryptoModule cm;
+    switch (environment.getScope()) {
+      case WAL:
+        cm = new AESCBCCryptoModule(this.encryptingKek, this.encryptingKekId,
+            this.encryptingKeyManager);
+        return cm.getEncrypter();
+
+      case RFILE:
+        cm = new AESGCMCryptoModule(this.encryptingKek, this.encryptingKekId,
+            this.encryptingKeyManager);
+        return cm.getEncrypter();
+
+      default:
+        throw new CryptoException("Unknown scope: " + environment.getScope());
+    }
+  }
+
+  @Override
+  public FileDecrypter getFileDecrypter(CryptoEnvironment environment) {
+    CryptoModule cm;
+    ParsedCryptoParameters parsed = 
parseCryptoParameters(environment.getDecryptionParams());
+    Key kek = loadDecryptionKek(parsed);
+    Key fek = KeyManager.unwrapKey(parsed.getEncFek(), kek);
+    switch (parsed.getCryptoServiceVersion()) {
+      case AESCBCCryptoModule.VERSION:
+        cm = new AESCBCCryptoModule(this.encryptingKek, this.encryptingKekId,
+            this.encryptingKeyManager);
+        return (cm.getDecrypter(fek));
+      case AESGCMCryptoModule.VERSION:
+        cm = new AESGCMCryptoModule(this.encryptingKek, this.encryptingKekId,
+            this.encryptingKeyManager);
+        return (cm.getDecrypter(fek));
+
+      case NO_CRYPTO_VERSION:
+        return new NoFileDecrypter();
+      default:
+        throw new CryptoException(
+            "Unknown crypto module version: " + 
parsed.getCryptoServiceVersion());
+    }
+  }
+
+  static class ParsedCryptoParameters {
+    String cryptoServiceName;
+    String cryptoServiceVersion;
+    String keyManagerVersion;
+    String kekId;
+    byte[] encFek;
+
+    public String getCryptoServiceName() {
+      return cryptoServiceName;
+    }
+
+    public void setCryptoServiceName(String cryptoServiceName) {
+      this.cryptoServiceName = cryptoServiceName;
+    }
+
+    public String getCryptoServiceVersion() {
+      return cryptoServiceVersion;
+    }
+
+    public void setCryptoServiceVersion(String cryptoServiceVersion) {
+      this.cryptoServiceVersion = cryptoServiceVersion;
+    }
+
+    public String getKeyManagerVersion() {
+      return keyManagerVersion;
+    }
+
+    public void setKeyManagerVersion(String keyManagerVersion) {
+      this.keyManagerVersion = keyManagerVersion;
+    }
+
+    public String getKekId() {
+      return kekId;
+    }
+
+    public void setKekId(String kekId) {
+      this.kekId = kekId;
+    }
+
+    public byte[] getEncFek() {
+      return encFek;
+    }
+
+    public void setEncFek(byte[] encFek) {
+      this.encFek = encFek;
+    }
+
+  }
+
+  private static byte[] createCryptoParameters(String version, Key 
encryptingKek,
+      String encryptingKekId, String encryptingKeyManager, Key fek) {
+
+    byte[] bytes;
+    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        DataOutputStream params = new DataOutputStream(baos)) {
+      params.writeUTF(AESCryptoService.class.getName());
+      params.writeUTF(version);
+      params.writeUTF(encryptingKeyManager);
+      params.writeUTF(encryptingKekId);
+      byte[] wrappedFek = KeyManager.wrapKey(fek, encryptingKek);
+      params.writeInt(wrappedFek.length);
+      params.write(wrappedFek);
+
+      bytes = baos.toByteArray();
+    } catch (IOException e) {
+      throw new CryptoException("Error creating crypto params", e);
+    }
+    return bytes;
+  }
+
+  private static ParsedCryptoParameters parseCryptoParameters(byte[] 
parameters) {
+    ParsedCryptoParameters parsed = new ParsedCryptoParameters();
+    try (ByteArrayInputStream bais = new ByteArrayInputStream(parameters);
+        DataInputStream params = new DataInputStream(bais)) {
+      parsed.setCryptoServiceName(params.readUTF());
 
 Review comment:
   I think you are right.  I will add a Test for this, good idea.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to