-
-
Notifications
You must be signed in to change notification settings - Fork 30
Encrypt in-memory secrets with AES/GCM and a fresh IV per value #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vharseko
wants to merge
2
commits into
OpenIdentityPlatform:master
Choose a base branch
from
vharseko:guarded-string-aes-gcm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...k-internal/src/main/java/org/identityconnectors/common/security/impl/AesGcmEncryptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * The contents of this file are subject to the terms of the Common Development and | ||
| * Distribution License (the License). You may not use this file except in compliance with the | ||
| * License. | ||
| * | ||
| * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the | ||
| * specific language governing permission and limitations under the License. | ||
| * | ||
| * When distributing Covered Software, include this CDDL Header Notice in each file and include | ||
| * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL | ||
| * Header, with the fields enclosed by brackets [] replaced by your own identifying | ||
| * information: "Portions copyright [year] [name of copyright owner]". | ||
| * | ||
| * Copyright 2026 3A Systems, LLC. | ||
| */ | ||
| package org.identityconnectors.common.security.impl; | ||
|
|
||
| import java.security.GeneralSecurityException; | ||
| import java.security.Key; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.security.SecureRandom; | ||
|
|
||
| import javax.crypto.Cipher; | ||
| import javax.crypto.KeyGenerator; | ||
| import javax.crypto.spec.GCMParameterSpec; | ||
|
|
||
| import org.identityconnectors.common.security.Encryptor; | ||
|
|
||
| /** | ||
| * AES-256/GCM with a key generated for this instance and a fresh random IV | ||
| * for every encryption; the IV is written in front of the ciphertext. Used | ||
| * for secrets held in memory ({@code GuardedString}, {@code GuardedByteArray}), | ||
| * which never leave the process, so there is no format to stay compatible | ||
| * with. | ||
| */ | ||
| public final class AesGcmEncryptor implements Encryptor { | ||
|
|
||
| private static final String ALGORITHM = "AES"; | ||
| private static final String TRANSFORMATION = "AES/GCM/NoPadding"; | ||
| private static final int KEY_BITS = 256; | ||
| private static final int IV_BYTES = 12; | ||
| private static final int TAG_BITS = 128; | ||
|
|
||
| private final Key key; | ||
| private final SecureRandom random = new SecureRandom(); | ||
|
|
||
| public AesGcmEncryptor() { | ||
| try { | ||
| KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM); | ||
| generator.init(KEY_BITS); | ||
| key = generator.generateKey(); | ||
| } catch (NoSuchAlgorithmException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] encrypt(byte[] bytes) { | ||
| byte[] iv = new byte[IV_BYTES]; | ||
| random.nextBytes(iv); | ||
| try { | ||
| Cipher cipher = Cipher.getInstance(TRANSFORMATION); | ||
| cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(TAG_BITS, iv)); | ||
| byte[] encrypted = cipher.doFinal(bytes); | ||
| byte[] result = new byte[IV_BYTES + encrypted.length]; | ||
| System.arraycopy(iv, 0, result, 0, IV_BYTES); | ||
| System.arraycopy(encrypted, 0, result, IV_BYTES, encrypted.length); | ||
| return result; | ||
| } catch (GeneralSecurityException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] decrypt(byte[] bytes) { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| if (bytes.length < IV_BYTES) { | ||
| throw new IllegalArgumentException("Ciphertext is shorter than its IV"); | ||
| } | ||
| try { | ||
| Cipher cipher = Cipher.getInstance(TRANSFORMATION); | ||
| cipher.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(TAG_BITS, bytes, 0, IV_BYTES)); | ||
| return cipher.doFinal(bytes, IV_BYTES, bytes.length - IV_BYTES); | ||
| } catch (GeneralSecurityException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...nternal/src/test/java/org/identityconnectors/common/security/impl/EncryptorImplTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * The contents of this file are subject to the terms of the Common Development and | ||
| * Distribution License (the License). You may not use this file except in compliance with the | ||
| * License. | ||
| * | ||
| * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the | ||
| * specific language governing permission and limitations under the License. | ||
| * | ||
| * When distributing Covered Software, include this CDDL Header Notice in each file and include | ||
| * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL | ||
| * Header, with the fields enclosed by brackets [] replaced by your own identifying | ||
| * information: "Portions copyright [year] [name of copyright owner]". | ||
| * | ||
| * Copyright 2026 3A Systems, LLC. | ||
| */ | ||
| package org.identityconnectors.common.security.impl; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertFalse; | ||
| import static org.testng.Assert.fail; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
|
|
||
| import org.identityconnectors.common.security.Encryptor; | ||
| import org.identityconnectors.common.security.EncryptorFactory; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| public class EncryptorImplTests { | ||
|
|
||
| private static final byte[] SECRET = "secret".getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| /** | ||
| * The default encryptor is the wire format of the legacy connector server | ||
| * protocol, shared with the .NET connector server: its output for a given | ||
| * input must not change. | ||
| */ | ||
| @Test | ||
| public void defaultEncryptorKeepsLegacyWireFormat() { | ||
| Encryptor encryptor = EncryptorFactory.getInstance().getDefaultEncryptor(); | ||
| assertEquals(encryptor.encrypt(SECRET), hex("66df076267e3a421575ce6fa2ec1d5c7")); | ||
| assertEquals(encryptor.decrypt(hex("66df076267e3a421575ce6fa2ec1d5c7")), SECRET); | ||
| } | ||
|
|
||
| @Test | ||
| public void randomEncryptorRoundTrips() { | ||
| Encryptor encryptor = EncryptorFactory.getInstance().newRandomEncryptor(); | ||
| assertEquals(encryptor.decrypt(encryptor.encrypt(SECRET)), SECRET); | ||
| } | ||
|
|
||
| @Test | ||
| public void randomEncryptorsDoNotShareKeys() { | ||
| Encryptor one = EncryptorFactory.getInstance().newRandomEncryptor(); | ||
| Encryptor other = EncryptorFactory.getInstance().newRandomEncryptor(); | ||
| try { | ||
| byte[] decrypted = other.decrypt(one.encrypt(SECRET)); | ||
| assertFalse(Arrays.equals(decrypted, SECRET), "decrypted with another key"); | ||
| } catch (RuntimeException expected) { | ||
| // an authenticated cipher rejects the foreign key outright | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * In-memory secrets: the same value must not encrypt to the same bytes | ||
| * twice, otherwise equal secrets can be spotted in a heap dump. | ||
| */ | ||
| @Test | ||
| public void randomEncryptorUsesFreshIvForEveryEncryption() { | ||
| Encryptor encryptor = EncryptorFactory.getInstance().newRandomEncryptor(); | ||
| assertFalse(Arrays.equals(encryptor.encrypt(SECRET), encryptor.encrypt(SECRET)), | ||
| "same ciphertext for the same input"); | ||
| } | ||
|
|
||
| @Test | ||
| public void randomEncryptorRejectsTamperedCiphertext() { | ||
| Encryptor encryptor = EncryptorFactory.getInstance().newRandomEncryptor(); | ||
| // several blocks long, so that a flipped byte in the first block | ||
| // leaves a CBC padding check intact and only an authenticated | ||
| // cipher notices | ||
| byte[] plain = "a plaintext that spans several cipher blocks".getBytes(StandardCharsets.UTF_8); | ||
| byte[] tampered = encryptor.encrypt(plain); | ||
| tampered[tampered.length - plain.length] ^= 0x01; | ||
| try { | ||
| byte[] decrypted = encryptor.decrypt(tampered); | ||
| fail("tampered ciphertext decrypted to " + Arrays.toString(decrypted)); | ||
| } catch (RuntimeException expected) { | ||
| // authentication failure | ||
| } | ||
| } | ||
|
|
||
| private static byte[] hex(String hex) { | ||
| byte[] bytes = new byte[hex.length() / 2]; | ||
| for (int i = 0; i < bytes.length; i++) { | ||
| bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); | ||
| } | ||
| return bytes; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.