-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ingest plexus-cipher, add improvements to "detection" of encrypted strings.
- Loading branch information
Showing
12 changed files
with
429 additions
and
119 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
src/main/java/org/codehaus/plexus/components/secdispatcher/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,44 @@ | ||
/* | ||
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.secdispatcher; | ||
|
||
/** | ||
* Cipher interface. | ||
* | ||
* @since 4.0.1 | ||
*/ | ||
public interface Cipher { | ||
/** | ||
* Encrypts the clear text data with password and returns result. No argument is allowed to be {@code null}. | ||
* | ||
* @throws CipherException if encryption failed (is unexpected to happen, as it would mean that Java Runtime | ||
* lacks some Crypto elements). | ||
*/ | ||
String encrypt(final String clearText, final String password) throws CipherException; | ||
|
||
/** | ||
* Decrypts the encrypted text with password and returns clear text result. No argument is allowed to be {@code null}. | ||
* | ||
* @throws CipherException if decryption failed. It may happen as with {@link #encrypt(String, String)} due Java | ||
* Runtime lacking some Crypto elements (less likely). Most likely decrypt will fail due wrong provided password | ||
* or maybe corrupted encrypted text. | ||
*/ | ||
String decrypt(final String encryptedText, final String password) throws CipherException; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/codehaus/plexus/components/secdispatcher/CipherException.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,28 @@ | ||
/* | ||
* Copyright (c) 2008 Sonatype, Inc. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package org.codehaus.plexus.components.secdispatcher; | ||
|
||
/** | ||
* Exception thrown by {@link Cipher}. | ||
* | ||
* @since 4.0.1 | ||
*/ | ||
public class CipherException extends SecDispatcherException { | ||
public CipherException(String message) { | ||
super(message); | ||
} | ||
|
||
public CipherException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
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
...in/java/org/codehaus/plexus/components/secdispatcher/internal/cipher/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.secdispatcher.internal.cipher; | ||
|
||
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.secdispatcher.CipherException; | ||
|
||
@Singleton | ||
@Named(AESGCMNoPadding.CIPHER_ALG) | ||
public class AESGCMNoPadding implements org.codehaus.plexus.components.secdispatcher.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 CipherException { | ||
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 CipherException("Failed encrypting", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String decrypt(String encryptedText, String password) throws CipherException { | ||
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 CipherException("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); | ||
} | ||
} |
Oops, something went wrong.