Skip to content

Commit d1643d4

Browse files
Incorporate Keyrings into AwsCrypto and deprecate MasterKeyProviders.
1 parent a6893b6 commit d1643d4

27 files changed

+1742
-183
lines changed

src/main/java/com/amazonaws/encryptionsdk/AwsCrypto.java

Lines changed: 403 additions & 59 deletions
Large diffs are not rendered by default.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright 2020 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.encryptionsdk;
15+
16+
import com.amazonaws.encryptionsdk.caching.CachingCryptoMaterialsManager;
17+
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
18+
import com.amazonaws.encryptionsdk.internal.MessageCryptoHandler;
19+
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
23+
/**
24+
* An AwsCryptoInputStream is a subclass of java.io.InputStream. It performs cryptographic
25+
* transformation of the bytes passing through it.
26+
*
27+
* <p>
28+
* The AwsCryptoInputStream wraps a provided InputStream object and performs cryptographic
29+
* transformation of the bytes read from the wrapped InputStream. It uses the cryptography handler
30+
* provided during construction to invoke methods that perform the cryptographic transformations.
31+
*
32+
* <p>
33+
* In short, reading from the AwsCryptoInputStream returns bytes that are the cryptographic
34+
* transformations of the bytes read from the wrapped InputStream.
35+
*
36+
* <p>
37+
* For example, if the cryptography handler provides methods for decryption, the AwsCryptoInputStream
38+
* will read ciphertext bytes from the wrapped InputStream, decrypt, and return them as plaintext
39+
* bytes.
40+
*
41+
* <p>
42+
* This class adheres strictly to the semantics, especially the failure semantics, of its ancestor
43+
* class java.io.InputStream. This class overrides all the methods specified in its ancestor class.
44+
*
45+
* <p>
46+
* To instantiate an instance of this class, please see {@link AwsCrypto}.
47+
*
48+
*/
49+
public class AwsCryptoInputStream extends InputStream {
50+
51+
private final CryptoInputStream<?> cryptoInputStream;
52+
53+
/**
54+
* Constructs an AwsCryptoInputStream that wraps the provided InputStream object. It performs
55+
* cryptographic transformation of the bytes read from the wrapped InputStream using the methods
56+
* provided in the provided CryptoHandler implementation.
57+
*
58+
* @param inputStream
59+
* the inputStream object to be wrapped.
60+
* @param cryptoHandler
61+
* the cryptoHandler implementation that provides the methods to use in performing
62+
* cryptographic transformation of the bytes read from the inputStream.
63+
*/
64+
AwsCryptoInputStream(final InputStream inputStream, final MessageCryptoHandler cryptoHandler) {
65+
cryptoInputStream = new CryptoInputStream<>(inputStream, cryptoHandler);
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
*
71+
* @throws BadCiphertextException
72+
* This is thrown only during decryption if b contains invalid or corrupt
73+
* ciphertext.
74+
*/
75+
@Override
76+
public int read(final byte[] b, final int off, final int len) throws IllegalArgumentException, IOException,
77+
BadCiphertextException {
78+
return cryptoInputStream.read(b, off, len);
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*
84+
* @throws BadCiphertextException
85+
* This is thrown only during decryption if b contains invalid or corrupt
86+
* ciphertext.
87+
*/
88+
@Override
89+
public int read(final byte[] b) throws IllegalArgumentException, IOException, BadCiphertextException {
90+
return cryptoInputStream.read(b);
91+
}
92+
93+
/**
94+
* {@inheritDoc}
95+
*
96+
* @throws BadCiphertextException
97+
* if b contains invalid or corrupt ciphertext. This is thrown only during
98+
* decryption.
99+
*/
100+
@Override
101+
public int read() throws IOException, BadCiphertextException {
102+
return cryptoInputStream.read();
103+
}
104+
105+
@Override
106+
public void close() throws IOException {
107+
cryptoInputStream.close();
108+
}
109+
110+
/**
111+
* Returns metadata associated with the performed cryptographic operation.
112+
*/
113+
@Override
114+
public int available() throws IOException {
115+
return cryptoInputStream.available();
116+
}
117+
118+
/**
119+
* Sets an upper bound on the size of the input data. This method should be called before reading any data from the
120+
* stream. If this method is not called prior to reading any data, performance may be reduced (notably, it will not
121+
* be possible to cache data keys when encrypting).
122+
*
123+
* Among other things, this size is used to enforce limits configured on the {@link CachingCryptoMaterialsManager}.
124+
*
125+
* If the input size set here is exceeded, an exception will be thrown, and the encryption or decryption will fail.
126+
*
127+
* @param size Maximum input size.
128+
*/
129+
public void setMaxInputLength(long size) {
130+
cryptoInputStream.setMaxInputLength(size);
131+
}
132+
133+
/**
134+
* Gets the {@link AwsCryptoResult}.
135+
*
136+
* @return The {@link AwsCryptoResult}
137+
* @throws IOException if an input/output exception occurs while processing the result
138+
*/
139+
public AwsCryptoResult<AwsCryptoInputStream> getAwsCryptoResult() throws IOException {
140+
return cryptoInputStream.getAwsCryptoResult(this);
141+
}
142+
143+
CryptoInputStream<?> toCryptoInputStream() {
144+
return cryptoInputStream;
145+
}
146+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2020 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.encryptionsdk;
15+
16+
import com.amazonaws.encryptionsdk.caching.CachingCryptoMaterialsManager;
17+
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
18+
import com.amazonaws.encryptionsdk.internal.MessageCryptoHandler;
19+
20+
import java.io.IOException;
21+
import java.io.OutputStream;
22+
23+
/**
24+
* An AwsCryptoOutputStream is a subclass of java.io.OutputStream. It performs cryptographic
25+
* transformation of the bytes passing through it.
26+
*
27+
* <p>
28+
* The AwsCryptoOutputStream wraps a provided OutputStream object and performs cryptographic
29+
* transformation of the bytes written to it. The transformed bytes are then written to the wrapped
30+
* OutputStream. It uses the cryptography handler provided during construction to invoke methods
31+
* that perform the cryptographic transformations.
32+
*
33+
* <p>
34+
* In short, writing to the AwsCryptoOutputStream results in those bytes being cryptographically
35+
* transformed and written to the wrapped OutputStream.
36+
*
37+
* <p>
38+
* For example, if the crypto handler provides methods for decryption, the AwsCryptoOutputStream will
39+
* decrypt the provided ciphertext bytes and write the plaintext bytes to the wrapped OutputStream.
40+
*
41+
* <p>
42+
* This class adheres strictly to the semantics, especially the failure semantics, of its ancestor
43+
* class java.io.OutputStream. This class overrides all the methods specified in its ancestor class.
44+
*
45+
* <p>
46+
* To instantiate an instance of this class, please see {@link AwsCrypto}.
47+
*
48+
*/
49+
public class AwsCryptoOutputStream extends OutputStream {
50+
51+
private final CryptoOutputStream<?> cryptoOutputStream;
52+
53+
/**
54+
* Constructs an AwsCryptoOutputStream that wraps the provided OutputStream object. It performs
55+
* cryptographic transformation of the bytes written to it using the methods provided in the
56+
* provided CryptoHandler implementation. The transformed bytes are then written to the wrapped
57+
* OutputStream.
58+
*
59+
* @param outputStream
60+
* the outputStream object to be wrapped.
61+
* @param cryptoHandler
62+
* the cryptoHandler implementation that provides the methods to use in performing
63+
* cryptographic transformation of the bytes written to this stream.
64+
*/
65+
AwsCryptoOutputStream(final OutputStream outputStream, final MessageCryptoHandler cryptoHandler) {
66+
cryptoOutputStream = new CryptoOutputStream<>(outputStream, cryptoHandler);
67+
}
68+
69+
/**
70+
* {@inheritDoc}
71+
*
72+
* @throws BadCiphertextException
73+
* This is thrown only during decryption if b contains invalid or corrupt
74+
* ciphertext.
75+
*/
76+
@Override
77+
public void write(final byte[] b) throws IllegalArgumentException, IOException, BadCiphertextException {
78+
cryptoOutputStream.write(b);
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*
84+
* @throws BadCiphertextException
85+
* This is thrown only during decryption if b contains invalid or corrupt
86+
* ciphertext.
87+
*/
88+
@Override
89+
public void write(final byte[] b, final int off, final int len) throws IllegalArgumentException, IOException,
90+
BadCiphertextException {
91+
cryptoOutputStream.write(b, off, len);
92+
}
93+
94+
/**
95+
* {@inheritDoc}
96+
*
97+
* @throws BadCiphertextException
98+
* This is thrown only during decryption if b contains invalid or corrupt
99+
* ciphertext.
100+
*/
101+
@Override
102+
public void write(int b) throws IOException, BadCiphertextException {
103+
cryptoOutputStream.write(b);
104+
}
105+
106+
/**
107+
* Closes this output stream and releases any system resources associated
108+
* with this stream.
109+
*
110+
* <p>
111+
* This method writes any final bytes to the underlying stream that complete
112+
* the cryptographic transformation of the written bytes. It also calls close
113+
* on the wrapped OutputStream.
114+
*
115+
* @throws IOException
116+
* if an I/O error occurs.
117+
* @throws BadCiphertextException
118+
* This is thrown only during decryption if b contains invalid
119+
* or corrupt ciphertext.
120+
*/
121+
@Override
122+
public void close() throws IOException, BadCiphertextException {
123+
cryptoOutputStream.close();
124+
}
125+
126+
/**
127+
* Sets an upper bound on the size of the input data. This method should be called before writing any data to the
128+
* stream. If this method is not called prior to writing data, performance may be reduced (notably, it will not
129+
* be possible to cache data keys when encrypting).
130+
*
131+
* Among other things, this size is used to enforce limits configured on the {@link CachingCryptoMaterialsManager}.
132+
*
133+
* If the size set here is exceeded, an exception will be thrown, and the encryption or decryption will fail.
134+
*
135+
* @param size Maximum input size.
136+
*/
137+
public void setMaxInputLength(long size) {
138+
cryptoOutputStream.setMaxInputLength(size);
139+
}
140+
141+
/**
142+
* Gets the {@link AwsCryptoResult}.
143+
*
144+
* @return The {@link AwsCryptoResult}
145+
*/
146+
public AwsCryptoResult<AwsCryptoOutputStream> getAwsCryptoResult() {
147+
return cryptoOutputStream.getAwsCryptoResult(this);
148+
}
149+
150+
CryptoOutputStream<?> toCryptoOutputStream() {
151+
return cryptoOutputStream;
152+
}
153+
}

0 commit comments

Comments
 (0)