Skip to content

Commit e6aa166

Browse files
committed
Merge pull request #105 from jenkinsci-cert/SECURITY-304-t3
[SECURITY-304] Encrypt new secrets with CBC and random IV instead of ECB
1 parent b0ed966 commit e6aa166

14 files changed

Lines changed: 517 additions & 88 deletions

File tree

core/src/main/java/hudson/model/Item.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
229229
Permission DISCOVER = new Permission(PERMISSIONS, "Discover", Messages._AbstractProject_DiscoverPermission_Description(), READ, PermissionScope.ITEM);
230230
/**
231231
* Ability to view configuration details.
232-
* If the user lacks {@link CONFIGURE} then any {@link Secret}s must be masked out, even in encrypted form.
232+
* If the user lacks {@link #CONFIGURE} then any {@link Secret}s must be masked out, even in encrypted form.
233233
* @see Secret#ENCRYPTED_VALUE_PATTERN
234234
*/
235235
Permission EXTENDED_READ = new Permission(PERMISSIONS,"ExtendedRead", Messages._AbstractProject_ExtendedReadPermission_Description(), CONFIGURE, Boolean.getBoolean("hudson.security.ExtendedReadPermission"), new PermissionScope[]{PermissionScope.ITEM});
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
5+
* Copyright (c) 2016, CloudBees Inc.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package hudson.util;
26+
27+
import com.trilead.ssh2.crypto.Base64;
28+
import hudson.Util;
29+
import jenkins.model.Jenkins;
30+
import jenkins.security.CryptoConfidentialKey;
31+
import org.kohsuke.accmod.Restricted;
32+
import org.kohsuke.accmod.restrictions.NoExternalUse;
33+
34+
import javax.crypto.Cipher;
35+
import javax.crypto.SecretKey;
36+
import java.io.IOException;
37+
import java.security.GeneralSecurityException;
38+
39+
import static java.nio.charset.StandardCharsets.UTF_8;
40+
41+
/**
42+
* Historical algorithms for decrypting {@link Secret}s.
43+
*/
44+
@Restricted(NoExternalUse.class)
45+
public class HistoricalSecrets {
46+
47+
/*package*/ static Secret decrypt(String data, CryptoConfidentialKey key) throws IOException, GeneralSecurityException {
48+
byte[] in = Base64.decode(data.toCharArray());
49+
Secret s = tryDecrypt(key.decrypt(), in);
50+
if (s!=null) return s;
51+
52+
// try our historical key for backward compatibility
53+
Cipher cipher = Secret.getCipher("AES");
54+
cipher.init(Cipher.DECRYPT_MODE, getLegacyKey());
55+
return tryDecrypt(cipher, in);
56+
}
57+
58+
/*package*/ static Secret tryDecrypt(Cipher cipher, byte[] in) {
59+
try {
60+
String plainText = new String(cipher.doFinal(in), UTF_8);
61+
if(plainText.endsWith(MAGIC))
62+
return new Secret(plainText.substring(0,plainText.length()-MAGIC.length()));
63+
return null;
64+
} catch (GeneralSecurityException e) {
65+
return null; // if the key doesn't match with the bytes, it can result in BadPaddingException
66+
}
67+
}
68+
69+
/**
70+
* Turns {@link Jenkins#getSecretKey()} into an AES key.
71+
*
72+
* @deprecated
73+
* This is no longer the key we use to encrypt new information, but we still need this
74+
* to be able to decrypt what's already persisted.
75+
*/
76+
@Deprecated
77+
/*package*/ static SecretKey getLegacyKey() throws GeneralSecurityException {
78+
String secret = Secret.SECRET;
79+
if(secret==null) return Jenkins.getInstance().getSecretKeyAsAES128();
80+
return Util.toAes128Key(secret);
81+
}
82+
83+
private static final String MAGIC = "::::MAGIC::::";
84+
}

core/src/main/java/hudson/util/Secret.java

Lines changed: 94 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* The MIT License
33
*
44
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
5+
* Copyright (c) 2016, CloudBees Inc.
56
*
67
* Permission is hereby granted, free of charge, to any person obtaining a copy
78
* of this software and associated documentation files (the "Software"), to deal
@@ -29,12 +30,12 @@
2930
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
3031
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
3132
import com.trilead.ssh2.crypto.Base64;
33+
import java.util.Arrays;
3234
import jenkins.model.Jenkins;
3335
import hudson.Util;
3436
import jenkins.security.CryptoConfidentialKey;
3537
import org.kohsuke.stapler.Stapler;
3638

37-
import javax.crypto.SecretKey;
3839
import javax.crypto.Cipher;
3940
import java.io.Serializable;
4041
import java.io.UnsupportedEncodingException;
@@ -44,6 +45,8 @@
4445
import org.kohsuke.accmod.Restricted;
4546
import org.kohsuke.accmod.restrictions.NoExternalUse;
4647

48+
import static java.nio.charset.StandardCharsets.UTF_8;
49+
4750
/**
4851
* Glorified {@link String} that uses encryption in the persisted form, to avoid accidental exposure of a secret.
4952
*
@@ -58,13 +61,20 @@
5861
* @author Kohsuke Kawaguchi
5962
*/
6063
public final class Secret implements Serializable {
64+
private static final byte PAYLOAD_V1 = 1;
6165
/**
6266
* Unencrypted secret text.
6367
*/
6468
private final String value;
69+
private byte[] iv;
70+
71+
/*package*/ Secret(String value) {
72+
this.value = value;
73+
}
6574

66-
private Secret(String value) {
75+
/*package*/ Secret(String value, byte[] iv) {
6776
this.value = value;
77+
this.iv = iv;
6878
}
6979

7080
/**
@@ -100,77 +110,102 @@ public int hashCode() {
100110
return value.hashCode();
101111
}
102112

103-
/**
104-
* Turns {@link Jenkins#getSecretKey()} into an AES key.
105-
*
106-
* @deprecated
107-
* This is no longer the key we use to encrypt new information, but we still need this
108-
* to be able to decrypt what's already persisted.
109-
*/
110-
@Deprecated
111-
/*package*/ static SecretKey getLegacyKey() throws GeneralSecurityException {
112-
String secret = SECRET;
113-
if(secret==null) return Jenkins.getInstance().getSecretKeyAsAES128();
114-
return Util.toAes128Key(secret);
115-
}
116-
117113
/**
118114
* Encrypts {@link #value} and returns it in an encoded printable form.
119115
*
120116
* @see #toString()
121117
*/
122118
public String getEncryptedValue() {
123119
try {
124-
Cipher cipher = KEY.encrypt();
125-
// add the magic suffix which works like a check sum.
126-
return new String(Base64.encode(cipher.doFinal((value+MAGIC).getBytes("UTF-8"))));
120+
synchronized (this) {
121+
if (iv == null) { //if we were created from plain text or other reason without iv
122+
iv = KEY.newIv();
123+
}
124+
}
125+
Cipher cipher = KEY.encrypt(iv);
126+
byte[] encrypted = cipher.doFinal(this.value.getBytes(UTF_8));
127+
byte[] payload = new byte[1 + 8 + iv.length + encrypted.length];
128+
int pos = 0;
129+
// For PAYLOAD_V1 we use this byte shifting model, V2 probably will need DataOutput
130+
payload[pos++] = PAYLOAD_V1;
131+
payload[pos++] = (byte)(iv.length >> 24);
132+
payload[pos++] = (byte)(iv.length >> 16);
133+
payload[pos++] = (byte)(iv.length >> 8);
134+
payload[pos++] = (byte)(iv.length);
135+
payload[pos++] = (byte)(encrypted.length >> 24);
136+
payload[pos++] = (byte)(encrypted.length >> 16);
137+
payload[pos++] = (byte)(encrypted.length >> 8);
138+
payload[pos++] = (byte)(encrypted.length);
139+
System.arraycopy(iv, 0, payload, pos, iv.length);
140+
pos+=iv.length;
141+
System.arraycopy(encrypted, 0, payload, pos, encrypted.length);
142+
return "{"+new String(Base64.encode(payload))+"}";
127143
} catch (GeneralSecurityException e) {
128144
throw new Error(e); // impossible
129-
} catch (UnsupportedEncodingException e) {
130-
throw new Error(e); // impossible
131145
}
132146
}
133147

134148
/**
135-
* Pattern matching a possible output of {@link #getEncryptedValue}.
136-
* Basically, any Base64-encoded value.
137-
* You must then call {@link #decrypt} to eliminate false positives.
149+
* Pattern matching a possible output of {@link #getEncryptedValue}
150+
* Basically, any Base64-encoded value optionally wrapped by {@code {}}.
151+
* You must then call {@link #decrypt(String)} to eliminate false positives.
152+
* @see #ENCRYPTED_VALUE_PATTERN
138153
*/
139154
@Restricted(NoExternalUse.class)
140-
public static final Pattern ENCRYPTED_VALUE_PATTERN = Pattern.compile("[A-Za-z0-9+/]+={0,2}");
155+
public static final Pattern ENCRYPTED_VALUE_PATTERN = Pattern.compile("\\{?[A-Za-z0-9+/]+={0,2}}?");
141156

142157
/**
143158
* Reverse operation of {@link #getEncryptedValue()}. Returns null
144159
* if the given cipher text was invalid.
145160
*/
146161
public static Secret decrypt(String data) {
147-
if(data==null) return null;
148-
try {
149-
byte[] in = Base64.decode(data.toCharArray());
150-
Secret s = tryDecrypt(KEY.decrypt(), in);
151-
if (s!=null) return s;
162+
if (data == null) return null;
152163

153-
// try our historical key for backward compatibility
154-
Cipher cipher = getCipher("AES");
155-
cipher.init(Cipher.DECRYPT_MODE, getLegacyKey());
156-
return tryDecrypt(cipher, in);
157-
} catch (GeneralSecurityException e) {
158-
return null;
159-
} catch (UnsupportedEncodingException e) {
160-
throw new Error(e); // impossible
161-
} catch (IOException e) {
162-
return null;
163-
}
164-
}
165-
166-
/*package*/ static Secret tryDecrypt(Cipher cipher, byte[] in) throws UnsupportedEncodingException {
167-
try {
168-
String plainText = new String(cipher.doFinal(in), "UTF-8");
169-
if(plainText.endsWith(MAGIC))
170-
return new Secret(plainText.substring(0,plainText.length()-MAGIC.length()));
171-
return null;
172-
} catch (GeneralSecurityException e) {
173-
return null; // if the key doesn't match with the bytes, it can result in BadPaddingException
164+
if (data.startsWith("{") && data.endsWith("}")) { //likely CBC encrypted/containing metadata but could be plain text
165+
byte[] payload;
166+
try {
167+
payload = Base64.decode(data.substring(1, data.length()-1).toCharArray());
168+
} catch (IOException e) {
169+
return null;
170+
}
171+
switch (payload[0]) {
172+
case PAYLOAD_V1:
173+
// For PAYLOAD_V1 we use this byte shifting model, V2 probably will need DataOutput
174+
int ivLength = ((payload[1] & 0xff) << 24)
175+
| ((payload[2] & 0xff) << 16)
176+
| ((payload[3] & 0xff) << 8)
177+
| (payload[4] & 0xff);
178+
int dataLength = ((payload[5] & 0xff) << 24)
179+
| ((payload[6] & 0xff) << 16)
180+
| ((payload[7] & 0xff) << 8)
181+
| (payload[8] & 0xff);
182+
if (payload.length != 1 + 8 + ivLength + dataLength) {
183+
// not valid v1
184+
return null;
185+
}
186+
byte[] iv = Arrays.copyOfRange(payload, 9, 9 + ivLength);
187+
byte[] code = Arrays.copyOfRange(payload, 9+ivLength, payload.length);
188+
String text;
189+
try {
190+
text = new String(KEY.decrypt(iv).doFinal(code), UTF_8);
191+
} catch (GeneralSecurityException e) {
192+
// it's v1 which cannot be historical, but not decrypting
193+
return null;
194+
}
195+
return new Secret(text, iv);
196+
default:
197+
return null;
198+
}
199+
} else {
200+
try {
201+
return HistoricalSecrets.decrypt(data, KEY);
202+
} catch (GeneralSecurityException e) {
203+
return null;
204+
} catch (UnsupportedEncodingException e) {
205+
throw new Error(e); // impossible
206+
} catch (IOException e) {
207+
return null;
208+
}
174209
}
175210
}
176211

@@ -228,8 +263,6 @@ public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingCont
228263
}
229264
}
230265

231-
private static final String MAGIC = "::::MAGIC::::";
232-
233266
/**
234267
* Workaround for JENKINS-6459 / http://java.net/jira/browse/GLASSFISH-11862
235268
* @see #getCipher(String)
@@ -246,6 +279,14 @@ public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingCont
246279
*/
247280
private static final CryptoConfidentialKey KEY = new CryptoConfidentialKey(Secret.class.getName());
248281

282+
/**
283+
* Reset the internal secret key for testing.
284+
*/
285+
@Restricted(NoExternalUse.class)
286+
/*package*/ static void resetKeyForTest() {
287+
KEY.resetForTest();
288+
}
289+
249290
private static final long serialVersionUID = 1L;
250291

251292
static {

core/src/main/java/hudson/util/SecretRewriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class SecretRewriter {
4040

4141
public SecretRewriter() throws GeneralSecurityException {
4242
cipher = Secret.getCipher("AES");
43-
key = Secret.getLegacyKey();
43+
key = HistoricalSecrets.getLegacyKey();
4444
}
4545

4646
/** @deprecated SECURITY-376: {@code backupDirectory} is ignored */
@@ -62,7 +62,7 @@ private String tryRewrite(String s) throws IOException, InvalidKeyException {
6262
return s; // not a valid base64
6363
}
6464
cipher.init(Cipher.DECRYPT_MODE, key);
65-
Secret sec = Secret.tryDecrypt(cipher, in);
65+
Secret sec = HistoricalSecrets.tryDecrypt(cipher, in);
6666
if(sec!=null) // matched
6767
return sec.getEncryptedValue(); // replace by the new encrypted value
6868
else // not encrypted with the legacy key. leave it unmodified

0 commit comments

Comments
 (0)