This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make `Cipher` (plexus, not Java) pluggable components, `PlexusCipher` component is just "shell" gathering then and having some extra functions like "decorating". Dropped insecure AES/CBC/PKCS5Padding and introduced AES/GCM/NoPadding instead.
- Loading branch information
Showing
9 changed files
with
292 additions
and
363 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
107 changes: 107 additions & 0 deletions
107
src/main/java/org/codehaus/plexus/components/cipher/internal/AESGCMNoPadding.java
This file contains 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,107 @@ | ||
/* | ||
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.codehaus.plexus.components.cipher.internal; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.GCMParameterSpec; | ||
import javax.crypto.spec.PBEKeySpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.KeySpec; | ||
import java.util.Base64; | ||
|
||
import org.codehaus.plexus.components.cipher.PlexusCipherException; | ||
|
||
@Singleton | ||
@Named(AESGCMNoPadding.CIPHER_ALG) | ||
public class AESGCMNoPadding implements org.codehaus.plexus.components.cipher.internal.Cipher { | ||
public static final String CIPHER_ALG = "AES/GCM/NoPadding"; | ||
|
||
private static final int TAG_LENGTH_BIT = 128; | ||
private static final int IV_LENGTH_BYTE = 12; | ||
private static final int SALT_LENGTH_BYTE = 16; | ||
private static final int PBE_ITERATIONS = 310000; | ||
private static final int PBE_KEY_SIZE = SALT_LENGTH_BYTE * 16; | ||
private static final String KEY_FACTORY = "PBKDF2WithHmacSHA512"; | ||
private static final String KEY_ALGORITHM = "AES"; | ||
|
||
@Override | ||
public String encrypt(String clearText, String password) throws PlexusCipherException { | ||
try { | ||
byte[] salt = getRandomNonce(SALT_LENGTH_BYTE); | ||
byte[] iv = getRandomNonce(IV_LENGTH_BYTE); | ||
SecretKey secretKey = getAESKeyFromPassword(password.toCharArray(), salt); | ||
Cipher cipher = Cipher.getInstance(CIPHER_ALG); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new GCMParameterSpec(TAG_LENGTH_BIT, iv)); | ||
byte[] cipherText = cipher.doFinal(clearText.getBytes(StandardCharsets.UTF_8)); | ||
byte[] cipherTextWithIvSalt = ByteBuffer.allocate(iv.length + salt.length + cipherText.length) | ||
.put(iv) | ||
.put(salt) | ||
.put(cipherText) | ||
.array(); | ||
return Base64.getEncoder().encodeToString(cipherTextWithIvSalt); | ||
} catch (Exception e) { | ||
throw new PlexusCipherException("Failed encrypting", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String decrypt(String encryptedText, String password) throws PlexusCipherException { | ||
try { | ||
byte[] material = Base64.getDecoder().decode(encryptedText.getBytes(StandardCharsets.UTF_8)); | ||
ByteBuffer buffer = ByteBuffer.wrap(material); | ||
byte[] iv = new byte[IV_LENGTH_BYTE]; | ||
buffer.get(iv); | ||
byte[] salt = new byte[SALT_LENGTH_BYTE]; | ||
buffer.get(salt); | ||
byte[] cipherText = new byte[buffer.remaining()]; | ||
buffer.get(cipherText); | ||
SecretKey secretKey = getAESKeyFromPassword(password.toCharArray(), salt); | ||
Cipher cipher = Cipher.getInstance(CIPHER_ALG); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey, new GCMParameterSpec(TAG_LENGTH_BIT, iv)); | ||
byte[] plainText = cipher.doFinal(cipherText); | ||
return new String(plainText, StandardCharsets.UTF_8); | ||
} catch (Exception e) { | ||
throw new PlexusCipherException("Failed decrypting", e); | ||
} | ||
} | ||
|
||
private static byte[] getRandomNonce(int numBytes) throws NoSuchAlgorithmException { | ||
byte[] nonce = new byte[numBytes]; | ||
SecureRandom.getInstanceStrong().nextBytes(nonce); | ||
return nonce; | ||
} | ||
|
||
private static SecretKey getAESKeyFromPassword(char[] password, byte[] salt) | ||
throws NoSuchAlgorithmException, InvalidKeySpecException { | ||
SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_FACTORY); | ||
KeySpec spec = new PBEKeySpec(password, salt, PBE_ITERATIONS, PBE_KEY_SIZE); | ||
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), KEY_ALGORITHM); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/codehaus/plexus/components/cipher/internal/Cipher.java
This file contains 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,37 @@ | ||
/* | ||
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.codehaus.plexus.components.cipher.internal; | ||
|
||
import org.codehaus.plexus.components.cipher.PlexusCipherException; | ||
|
||
/** | ||
* Cipher interface. | ||
*/ | ||
public interface Cipher { | ||
/** | ||
* Encrypts the clear text data with password and returns result. | ||
*/ | ||
String encrypt(final String clearText, final String password) throws PlexusCipherException; | ||
|
||
/** | ||
* Decrypts the encrypted text with password and returns clear text result. | ||
*/ | ||
String decrypt(final String encryptedText, final String password) throws PlexusCipherException; | ||
} |
Oops, something went wrong.