|
| 1 | +/* |
| 2 | + * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except |
| 5 | + * in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://aws.amazon.com/apache2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.amazonaws.crypto.examples; |
| 15 | + |
| 16 | +import java.io.FileInputStream; |
| 17 | +import java.io.FileOutputStream; |
| 18 | +import java.security.GeneralSecurityException; |
| 19 | +import java.security.KeyPair; |
| 20 | +import java.security.KeyPairGenerator; |
| 21 | +import java.security.PrivateKey; |
| 22 | +import java.security.PublicKey; |
| 23 | + |
| 24 | +import com.amazonaws.encryptionsdk.AwsCrypto; |
| 25 | +import com.amazonaws.encryptionsdk.CryptoOutputStream; |
| 26 | +import com.amazonaws.encryptionsdk.MasterKeyProvider; |
| 27 | +import com.amazonaws.encryptionsdk.jce.JceMasterKey; |
| 28 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider; |
| 29 | +import com.amazonaws.encryptionsdk.multi.MultipleProviderFactory; |
| 30 | +import com.amazonaws.util.IOUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * <p> |
| 34 | + * Encrypts a file using both KMS and an asymmetric key pair. |
| 35 | + * |
| 36 | + * <p> |
| 37 | + * Arguments: |
| 38 | + * <ol> |
| 39 | + * <li>KMS KeyArn |
| 40 | + * <li>File Name |
| 41 | + * </ol> |
| 42 | + * |
| 43 | + * Some organizations want the ability to decrypt their data even if KMS is unavailable. This |
| 44 | + * program demonstrates one possible way of accomplishing this by generating an "Escrow" RSA |
| 45 | + * key-pair and using that in addition to the KMS key for encryption. The organization would keep |
| 46 | + * the RSA private key someplace secure (such as an offline HSM) and distribute the public key their |
| 47 | + * developers. This way all standard use would use KMS for decryption, however the organization |
| 48 | + * maintains the ability to decrypt all ciphertexts in a completely offline manner. |
| 49 | + */ |
| 50 | +public class EscrowedEncryptExample { |
| 51 | + private static PublicKey publicEscrowKey; |
| 52 | + private static PrivateKey privateEscrowKey; |
| 53 | + |
| 54 | + public static void main(final String[] args) throws Exception { |
| 55 | + // In the real world, the public key would be distributed by the organization. |
| 56 | + // For this demo, we'll just generate a new random one each time. |
| 57 | + generateEscrowKeyPair(); |
| 58 | + |
| 59 | + final String kmsArn = args[0]; |
| 60 | + final String fileName = args[1]; |
| 61 | + |
| 62 | + standardEncrypt(kmsArn, fileName); |
| 63 | + standardDecrypt(kmsArn, fileName); |
| 64 | + |
| 65 | + escrowDecrypt(fileName); |
| 66 | + } |
| 67 | + |
| 68 | + private static void standardEncrypt(final String kmsArn, final String fileName) throws Exception { |
| 69 | + // Standard user encrypting to both KMS and the escrow public key |
| 70 | + // 1. Instantiate the SDK |
| 71 | + final AwsCrypto crypto = new AwsCrypto(); |
| 72 | + |
| 73 | + // 2. Instantiate the providers |
| 74 | + final KmsMasterKeyProvider kms = new KmsMasterKeyProvider(kmsArn); |
| 75 | + // Note that the standard user does not have access to the private escrow |
| 76 | + // key and so simply passes in "null" |
| 77 | + final JceMasterKey escrowPub = JceMasterKey.getInstance(publicEscrowKey, null, "Escrow", "Escrow", |
| 78 | + "RSA/ECB/OAEPWithSHA-512AndMGF1Padding"); |
| 79 | + |
| 80 | + // 3. Combine the providers into a single one |
| 81 | + final MasterKeyProvider<?> provider = MultipleProviderFactory.buildMultiProvider(kms, escrowPub); |
| 82 | + |
| 83 | + // 4. Encrypt the file |
| 84 | + // To simplify the code, we'll be omitted Encryption Context this time. Production code |
| 85 | + // should always use Encryption Context. Please see the other examples for more information. |
| 86 | + final FileInputStream in = new FileInputStream(fileName); |
| 87 | + final FileOutputStream out = new FileOutputStream(fileName + ".encrypted"); |
| 88 | + final CryptoOutputStream<?> encryptingStream = crypto.createEncryptingStream(provider, out); |
| 89 | + |
| 90 | + IOUtils.copy(in, encryptingStream); |
| 91 | + in.close(); |
| 92 | + encryptingStream.close(); |
| 93 | + } |
| 94 | + |
| 95 | + private static void standardDecrypt(final String kmsArn, final String fileName) throws Exception { |
| 96 | + // A standard user decrypts the file. They can just use the same provider from before |
| 97 | + // or could use a provider just referring to the KMS key. It doesn't matter. |
| 98 | + |
| 99 | + // 1. Instantiate the SDK |
| 100 | + final AwsCrypto crypto = new AwsCrypto(); |
| 101 | + |
| 102 | + // 2. Instantiate the providers |
| 103 | + final KmsMasterKeyProvider kms = new KmsMasterKeyProvider(kmsArn); |
| 104 | + // Note that the standard user does not have access to the private escrow |
| 105 | + // key and so simply passes in "null" |
| 106 | + final JceMasterKey escrowPub = JceMasterKey.getInstance(publicEscrowKey, null, "Escrow", "Escrow", |
| 107 | + "RSA/ECB/OAEPWithSHA-512AndMGF1Padding"); |
| 108 | + |
| 109 | + // 3. Combine the providers into a single one |
| 110 | + final MasterKeyProvider<?> provider = MultipleProviderFactory.buildMultiProvider(kms, escrowPub); |
| 111 | + |
| 112 | + // 4. Decrypt the file |
| 113 | + // To simplify the code, we'll be omitted Encryption Context this time. Production code |
| 114 | + // should always use Encryption Context. Please see the other examples for more information. |
| 115 | + final FileInputStream in = new FileInputStream(fileName + ".encrypted"); |
| 116 | + final FileOutputStream out = new FileOutputStream(fileName + ".decrypted"); |
| 117 | + final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(provider, out); |
| 118 | + IOUtils.copy(in, decryptingStream); |
| 119 | + in.close(); |
| 120 | + decryptingStream.close(); |
| 121 | + } |
| 122 | + |
| 123 | + private static void escrowDecrypt(final String fileName) throws Exception { |
| 124 | + // The organization can decrypt using just the private escrow key with no calls to KMS |
| 125 | + |
| 126 | + // 1. Instantiate the SDK |
| 127 | + final AwsCrypto crypto = new AwsCrypto(); |
| 128 | + |
| 129 | + // 2. Instantiate the provider |
| 130 | + // Note that the organization does have access to the private escrow key and can use it. |
| 131 | + final JceMasterKey escrowPriv = JceMasterKey.getInstance(publicEscrowKey, privateEscrowKey, "Escrow", "Escrow", |
| 132 | + "RSA/ECB/OAEPWithSHA-512AndMGF1Padding"); |
| 133 | + |
| 134 | + // 3. Decrypt the file |
| 135 | + // To simplify the code, we'll be omitted Encryption Context this time. Production code |
| 136 | + // should always use Encryption Context. Please see the other examples for more information. |
| 137 | + final FileInputStream in = new FileInputStream(fileName + ".encrypted"); |
| 138 | + final FileOutputStream out = new FileOutputStream(fileName + ".deescrowed"); |
| 139 | + final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(escrowPriv, out); |
| 140 | + IOUtils.copy(in, decryptingStream); |
| 141 | + in.close(); |
| 142 | + decryptingStream.close(); |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + private static void generateEscrowKeyPair() throws GeneralSecurityException { |
| 147 | + final KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA"); |
| 148 | + kg.initialize(4096); // Escrow keys should be very strong |
| 149 | + final KeyPair keyPair = kg.generateKeyPair(); |
| 150 | + publicEscrowKey = keyPair.getPublic(); |
| 151 | + privateEscrowKey = keyPair.getPrivate(); |
| 152 | + |
| 153 | + } |
| 154 | +} |
0 commit comments