Skip to content

Commit

Permalink
JENKINS-59633 delete any new line or space characters in encrypted wi…
Browse files Browse the repository at this point in the history
…ndows password (#418)
  • Loading branch information
Raphael Pionke authored and Raihaan Shouhell committed Dec 4, 2019
1 parent 840995a commit 395bfc8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/hudson/plugins/ec2/EC2PrivateKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import jenkins.bouncycastle.api.PEMEncodable;
import javax.crypto.Cipher;
import java.nio.charset.Charset;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand Down Expand Up @@ -133,7 +135,7 @@ public String decryptWindowsPassword(String encodedPassword) throws AmazonClient
try {
Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, PEMEncodable.decode(privateKey.getPlainText()).toPrivateKey());
byte[] cipherText = Base64.getDecoder().decode(encodedPassword);
byte[] cipherText = Base64.getDecoder().decode(StringUtils.deleteWhitespace(encodedPassword));
byte[] plainText = cipher.doFinal(cipherText);
return new String(plainText, Charset.forName("ASCII"));
} catch (Exception e) {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/hudson/plugins/ec2/EC2PrivateKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public void testDecryptPassword() throws AmazonClientException {
assertEquals("$32s7y%zAZ#xWHu3xm0TT0PH%5kKgmHO", password);
}

@Test
public void testDecryptPasswordwNewLines() throws AmazonClientException {
EC2PrivateKey k = getPrivateKey();
final String password = k.decryptWindowsPassword("\r\nLXOVLWjPem85Gy3B/AbIQ2/aQKIT5PZuq+Egg97EJSLKXVkdxDSdP7XbfpkKSNSZdhLV8XAE/vfAvU7MU2FfKArxZ6vvN2Gy8ukSoQW2UC2p1xm8ygI4sr40+Op2Hva/Svcjka4sVLYJy/ySTZCEedFkEzxhPV7FM6KHhZZ9L56hxSJe1P/7E1dY2pxbJbj/QeAKu8ps/RYTHPgqvTw0oeq1/Sal362nNPPAG+aznocazp8g0gNhySg58/9i+xjtyqm4mjWsN0p8s4JhRKPZ/iHJSqu+ZfaJqURXd9OHLdnzkvHpyzKCbU3URmnY3dha4B8dlPGAX0z3hSEtaI0LJA==\r\n");
assertEquals("$32s7y%zAZ#xWHu3xm0TT0PH%5kKgmHO", password);
}

@Issue("JENKINS-41824")
@Test(expected = IOException.class)
public void testEmptyPrivateKey() throws Exception {
Expand Down

0 comments on commit 395bfc8

Please sign in to comment.