- * In Hudson, we need {@link jenkins.model.Jenkins} instance to perform remember-me service, - * because it relies on {@link jenkins.model.Jenkins#getSecretKey()}. However, security - * filters can be initialized before Hudson is initialized. + * In Jenkins, we need {@link Jenkins} instance to perform remember-me service, + * because it relies on {@link ConfidentialStore}. However, security + * filters can be initialized before Jenkins is initialized. * (See #1210 for example.) * *
diff --git a/core/src/main/java/hudson/security/SecurityRealm.java b/core/src/main/java/hudson/security/SecurityRealm.java
index 9721fd252e..910f7f390e 100644
--- a/core/src/main/java/hudson/security/SecurityRealm.java
+++ b/core/src/main/java/hudson/security/SecurityRealm.java
@@ -516,10 +516,21 @@ public abstract class SecurityRealm extends AbstractDescribableImpl
- * Note that since the cryptography relies on {@link jenkins.model.Jenkins#getSecretKey()}, this is not meant as a protection
- * against code running in the same VM, nor against an attacker who has local file system access.
+ * This is not meant as a protection against code running in the same VM, nor against an attacker
+ * who has local file system access on Jenkins master.
*
*
* {@link Secret}s can correctly read-in plain text password, so this allows the existing
@@ -96,9 +99,13 @@ public final class Secret implements Serializable {
}
/**
- * Turns {@link jenkins.model.Jenkins#getSecretKey()} into an AES key.
+ * Turns {@link Jenkins#getSecretKey()} into an AES key.
+ *
+ * @deprecated
+ * This is no longer the key we use to encrypt new information, but we still need this
+ * to be able to decrypt what's already persisted.
*/
- private static SecretKey getKey() throws UnsupportedEncodingException, GeneralSecurityException {
+ /*package*/ static SecretKey getLegacyKey() throws UnsupportedEncodingException, GeneralSecurityException {
String secret = SECRET;
if(secret==null) return Jenkins.getInstance().getSecretKeyAsAES128();
return Util.toAes128Key(secret);
@@ -111,8 +118,7 @@ public final class Secret implements Serializable {
*/
public String getEncryptedValue() {
try {
- Cipher cipher = getCipher("AES");
- cipher.init(Cipher.ENCRYPT_MODE, getKey());
+ Cipher cipher = KEY.encrypt();
// add the magic suffix which works like a check sum.
return new String(Base64.encode(cipher.doFinal((value+MAGIC).getBytes("UTF-8"))));
} catch (GeneralSecurityException e) {
@@ -129,12 +135,14 @@ public final class Secret implements Serializable {
public static Secret decrypt(String data) {
if(data==null) return null;
try {
+ byte[] in = Base64.decode(data.toCharArray());
+ Secret s = tryDecrypt(KEY.decrypt(), in);
+ if (s!=null) return s;
+
+ // try our historical key for backward compatibility
Cipher cipher = getCipher("AES");
- cipher.init(Cipher.DECRYPT_MODE, getKey());
- String plainText = new String(cipher.doFinal(Base64.decode(data.toCharArray())), "UTF-8");
- if(plainText.endsWith(MAGIC))
- return new Secret(plainText.substring(0,plainText.length()-MAGIC.length()));
- return null;
+ cipher.init(Cipher.DECRYPT_MODE, getLegacyKey());
+ return tryDecrypt(cipher, in);
} catch (GeneralSecurityException e) {
return null;
} catch (UnsupportedEncodingException e) {
@@ -144,6 +152,17 @@ public final class Secret implements Serializable {
}
}
+ private static Secret tryDecrypt(Cipher cipher, byte[] in) throws UnsupportedEncodingException {
+ try {
+ String plainText = new String(cipher.doFinal(in), "UTF-8");
+ if(plainText.endsWith(MAGIC))
+ return new Secret(plainText.substring(0,plainText.length()-MAGIC.length()));
+ return null;
+ } catch (GeneralSecurityException e) {
+ return null;
+ }
+ }
+
/**
* Workaround for JENKINS-6459 / http://java.net/jira/browse/GLASSFISH-11862
* This method uses specific provider selected via hudson.util.Secret.provider system property
@@ -207,10 +226,15 @@ public final class Secret implements Serializable {
private static final String PROVIDER = System.getProperty(Secret.class.getName()+".provider");
/**
- * For testing only. Override the secret key so that we can test this class without {@link jenkins.model.Jenkins}.
+ * For testing only. Override the secret key so that we can test this class without {@link Jenkins}.
*/
/*package*/ static String SECRET = null;
+ /**
+ * The key that encrypts the data on disk.
+ */
+ private static final CryptoConfidentialKey KEY = new CryptoConfidentialKey(Secret.class.getName());
+
private static final long serialVersionUID = 1L;
static {
diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java
index 116d98646d..bb9b8efc5e 100755
--- a/core/src/main/java/jenkins/model/Jenkins.java
+++ b/core/src/main/java/jenkins/model/Jenkins.java
@@ -196,6 +196,8 @@ import jenkins.ExtensionComponentSet;
import jenkins.ExtensionRefreshException;
import jenkins.InitReactorRunner;
import jenkins.model.ProjectNamingStrategy.DefaultProjectNamingStrategy;
+import jenkins.security.ConfidentialKey;
+import jenkins.security.ConfidentialStore;
import net.sf.json.JSONObject;
import org.acegisecurity.AccessDeniedException;
import org.acegisecurity.AcegiSecurityException;
@@ -994,6 +996,10 @@ public class Jenkins extends AbstractCIBase implements ModifiableTopLevelItemGro
* Returns a secret key that survives across container start/stop.
*
* This value is useful for implementing some of the security features.
+ *
+ * @deprecated
+ * Due to the past security advisory, this value should not be used any more to protect sensitive information.
+ * See {@link ConfidentialStore} and {@link ConfidentialKey} for how to store secrets.
*/
public String getSecretKey() {
return secretKey;
@@ -1002,11 +1008,29 @@ public class Jenkins extends AbstractCIBase implements ModifiableTopLevelItemGro
/**
* Gets {@linkplain #getSecretKey() the secret key} as a key for AES-128.
* @since 1.308
+ * @deprecated
+ * See {@link #getSecretKey()}.
*/
public SecretKey getSecretKeyAsAES128() {
return Util.toAes128Key(secretKey);
}
+ /**
+ * Returns the unique identifier of this Jenkins that has been historically used to identify
+ * this Jenkins to the outside world.
+ *
+ *
+ * This form of identifier is weak in that it can be impersonated by others. See
+ * https://wiki.jenkins-ci.org/display/JENKINS/Instance+Identity for more modern form of instance ID
+ * that can be challenged and verified.
+ *
+ * @since 1.498
+ */
+ @SuppressWarnings("deprecation")
+ public String getLegacyInstanceId() {
+ return Util.getDigestOf(getSecretKey());
+ }
+
/**
* Gets the SCM descriptor by name. Primarily used for making them web-visible.
*/
diff --git a/core/src/main/java/jenkins/security/ApiTokenProperty.java b/core/src/main/java/jenkins/security/ApiTokenProperty.java
index 94c10f1173..f1e06efa26 100644
--- a/core/src/main/java/jenkins/security/ApiTokenProperty.java
+++ b/core/src/main/java/jenkins/security/ApiTokenProperty.java
@@ -31,7 +31,6 @@ import hudson.model.UserProperty;
import hudson.model.UserPropertyDescriptor;
import hudson.util.HttpResponses;
import hudson.util.Secret;
-import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
@@ -103,10 +102,10 @@ public class ApiTokenProperty extends UserProperty {
* because there's no guarantee that the property is saved.
*
* But we also need to make sure that an attacker won't be able to guess
- * the initial API token value. So we take the seed by hasing the instance secret key + user ID.
+ * the initial API token value. So we take the seed by hashing the secret + user ID.
*/
public ApiTokenProperty newInstance(User user) {
- return new ApiTokenProperty(Util.getDigestOf(Jenkins.getInstance().getSecretKey() + ":" + user.getId()));
+ return new ApiTokenProperty(API_KEY_SEED.mac(user.getId()));
}
public HttpResponse doChangeToken(@AncestorInPath User u, StaplerResponse rsp) throws IOException {
@@ -123,4 +122,9 @@ public class ApiTokenProperty extends UserProperty {
}
private static final SecureRandom RANDOM = new SecureRandom();
+
+ /**
+ * We don't want an API key that's too long, so cut the length to 16 (which produces 32-letter MAC code in hexdump)
+ */
+ private static final HMACConfidentialKey API_KEY_SEED = new HMACConfidentialKey(ApiTokenProperty.class,"seed",16);
}
diff --git a/core/src/main/java/jenkins/security/ConfidentialKey.java b/core/src/main/java/jenkins/security/ConfidentialKey.java
new file mode 100644
index 0000000000..a0167f478d
--- /dev/null
+++ b/core/src/main/java/jenkins/security/ConfidentialKey.java
@@ -0,0 +1,57 @@
+package jenkins.security;
+
+import hudson.scm.SCM;
+import hudson.tasks.Builder;
+import hudson.util.Secret;
+import jenkins.slaves.JnlpSlaveAgentProtocol;
+
+import javax.annotation.CheckForNull;
+import java.io.IOException;
+
+/**
+ * Confidential information that gets stored as a singleton in Jenkins, mostly some random token value.
+ *
+ *
+ * The actual value is persisted via {@link ConfidentialStore}, but each use case that requires
+ * a secret like this should use a separate {@link ConfidentialKey} instance so that one compromised
+ * {@link ConfidentialKey} (say through incorrect usage and failure to protect it) shouldn't compromise
+ * all the others.
+ *
+ *
+ * {@link ConfidentialKey} is ultimately a sequence of bytes,
+ * but for convenience, a family of subclasses are provided to represent the secret in different formats.
+ * See {@link HexStringConfidentialKey} and {@link HMACConfidentialKey} for example. In addition to the programming
+ * ease, these use case specific subtypes make it harder for vulnerability to creep in by making it harder
+ * for the secret to leak.
+ *
+ *
+ * The {@link ConfidentialKey} subtypes are expected to be used as a singleton, like {@link JnlpSlaveAgentProtocol#SLAVE_SECRET}.
+ * For code that relies on XStream for persistence (such as {@link Builder}s, {@link SCM}s, and other fragment objects
+ * around builds and jobs), {@link Secret} provides more convenient way of storing secrets.
+ *
+ * @author Kohsuke Kawaguchi
+ * @see Secret
+ * @since 1.498
+ */
+public abstract class ConfidentialKey {
+ /**
+ * Name of the key. This is used as the file name.
+ */
+ private final String id;
+
+ protected ConfidentialKey(String id) {
+ this.id = id;
+ }
+
+ protected @CheckForNull byte[] load() throws IOException {
+ return ConfidentialStore.get().load(this);
+ }
+
+ protected void store(byte[] payload) throws IOException {
+ ConfidentialStore.get().store(this,payload);
+ }
+
+ public String getId() {
+ return id;
+ }
+}
diff --git a/core/src/main/java/jenkins/security/ConfidentialStore.java b/core/src/main/java/jenkins/security/ConfidentialStore.java
new file mode 100644
index 0000000000..0c768f0af7
--- /dev/null
+++ b/core/src/main/java/jenkins/security/ConfidentialStore.java
@@ -0,0 +1,61 @@
+package jenkins.security;
+
+import hudson.Extension;
+import jenkins.model.Jenkins;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+/**
+ * The actual storage for the data held by {@link ConfidentialKey}s, and the holder
+ * of the master secret.
+ *
+ *
+ * This class is only relevant for the implementers of {@link ConfidentialKey}s.
+ * Most plugin code should interact with {@link ConfidentialKey}s.
+ *
+ *
+ * OEM distributions of Jenkins can provide a custom {@link ConfidentialStore} implementation
+ * by writing a subclass, mark it with {@link Extension} annotation, package it as a Jenkins module,
+ * and bundling it with the war file.
+ *
+ * @author Kohsuke Kawaguchi
+ * @since 1.498
+ */
+public abstract class ConfidentialStore {
+ /**
+ * Persists the payload of {@link ConfidentialKey} to a persisted storage (such as disk.)
+ * The expectation is that the persisted form is secure.
+ */
+ protected abstract void store(ConfidentialKey key, byte[] payload) throws IOException;
+
+ /**
+ * Reverse operation of {@link #store(ConfidentialKey, byte[])}
+ *
+ * @return
+ * null the data has not been previously persisted, or if the data was tampered.
+ */
+ protected abstract @CheckForNull byte[] load(ConfidentialKey key) throws IOException;
+
+ /**
+ * Works like {@link SecureRandom#nextBytes(byte[])}.
+ *
+ * This enables implementations to consult other entropy sources, if it's available.
+ */
+ public abstract byte[] randomBytes(int size);
+
+ /**
+ * Retrieves the currently active singleton instance of {@link ConfidentialStore}.
+ */
+ public static @Nonnull ConfidentialStore get() {
+ if (TEST!=null) return TEST.get();
+ return Jenkins.getInstance().getExtensionList(ConfidentialStore.class).get(0);
+ }
+
+ /**
+ * Testing only. Used for testing {@link ConfidentialKey} without {@link Jenkins}
+ */
+ /*package*/ static ThreadLocal
+ * This provides more secure version of it by using HMAC.
+ * See http://rdist.root.org/2009/10/29/stop-using-unsafe-keyed-hashes-use-hmac/ for background.
+ * This implementation also never leaks the secret value to outside, so it makes it impossible
+ * for the careless caller to misuse the key (thus protecting ourselves from our own stupidity!)
+ *
+ * @author Kohsuke Kawaguchi
+ * @since 1.498
+ */
+public class HMACConfidentialKey extends ConfidentialKey {
+ private volatile SecretKey key;
+ private final int length;
+
+ /**
+ * @param length
+ * Byte length of the HMAC code.
+ * By default we use HMAC-SHA256, which produces 256bit (=32bytes) HMAC,
+ * but if different use cases requires a shorter HMAC, specify the desired length here.
+ * Note that when using {@link #mac(String)}, string encoding causes the length to double.
+ * So if you want to get 16-letter HMAC, you specify 8 here.
+ */
+ public HMACConfidentialKey(String id, int length) {
+ super(id);
+ this.length = length;
+ }
+
+ /**
+ * Calls into {@link #HMACConfidentialKey(String, int)} with the longest possible HMAC length.
+ */
+ public HMACConfidentialKey(String id) {
+ this(id,Integer.MAX_VALUE);
+ }
+
+ /**
+ * Calls into {@link #HMACConfidentialKey(String, int)} by combining the class name and the shortName
+ * as the ID.
+ */
+ public HMACConfidentialKey(Class owner, String shortName, int length) {
+ this(owner.getName()+'.'+shortName,length);
+ }
+
+ public HMACConfidentialKey(Class owner, String shortName) {
+ this(owner,shortName,Integer.MAX_VALUE);
+ }
+
+
+ /**
+ * Computes the message authentication code for the specified byte sequence.
+ */
+ public byte[] mac(byte[] message) {
+ return chop(createMac().doFinal(message));
+ }
+
+ /**
+ * Convenience method for verifying the MAC code.
+ */
+ public boolean checkMac(byte[] message, byte[] mac) {
+ return Arrays.equals(mac(message),mac);
+ }
+
+ /**
+ * Computes the message authentication code and return it as a string.
+ * While redundant, often convenient.
+ */
+ public String mac(String message) {
+ try {
+ return Util.toHexString(mac(message.getBytes("UTF-8")));
+ } catch (UnsupportedEncodingException e) {
+ throw new AssertionError(e);
+ }
+ }
+
+ /**
+ * Verifies MAC constructed from {@link #mac(String)}
+ */
+ public boolean checkMac(String message, String mac) {
+ return mac(message).equals(mac);
+ }
+
+ private byte[] chop(byte[] mac) {
+ if (mac.length<=length) return mac; // already too short
+
+ byte[] b = new byte[length];
+ System.arraycopy(mac,0,b,0,b.length);
+ return b;
+ }
+
+ /**
+ * Creates a new {@link Mac} object.
+ */
+ public Mac createMac() {
+ try {
+ Mac mac = Mac.getInstance(ALGORITHM);
+ mac.init(getKey());
+ return mac;
+ } catch (GeneralSecurityException e) {
+ // Javadoc says HmacSHA256 must be supported by every Java implementation.
+ throw new Error(ALGORITHM+" not supported?",e);
+ }
+ }
+
+ private SecretKey getKey() {
+ if (key==null) {
+ synchronized (this) {
+ if (key==null) {
+ try {
+ byte[] encoded = load();
+ if (encoded==null) {
+ KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
+ SecretKey key = kg.generateKey();
+ store(encoded=key.getEncoded());
+ }
+ key = new SecretKeySpec(encoded,ALGORITHM);
+ } catch (IOException e) {
+ throw new Error("Failed to load the key: "+getId(),e);
+ } catch (NoSuchAlgorithmException e) {
+ throw new Error("Failed to load the key: "+getId(),e);
+ }
+ }
+ }
+ }
+ return key;
+ }
+
+ private static final String ALGORITHM = "HmacSHA256";
+}
diff --git a/core/src/main/java/jenkins/security/HexStringConfidentialKey.java b/core/src/main/java/jenkins/security/HexStringConfidentialKey.java
new file mode 100644
index 0000000000..75fb259bd8
--- /dev/null
+++ b/core/src/main/java/jenkins/security/HexStringConfidentialKey.java
@@ -0,0 +1,65 @@
+package jenkins.security;
+
+import hudson.Util;
+
+import java.io.IOException;
+
+/**
+ * {@link ConfidentialKey} that is the random hexadecimal string of length N.
+ *
+ *
+ * This is typically used as a unique ID, as a hex dump is suitable for printing, copy-pasting,
+ * as well as use as an identifier.
+ *
+ * @author Kohsuke Kawaguchi
+ * @since 1.498
+ */
+public class HexStringConfidentialKey extends ConfidentialKey {
+ private final int length;
+
+ private volatile String secret;
+
+ /**
+ * @param length
+ * Length of the hexadecimal string.
+ */
+ public HexStringConfidentialKey(String id, int length) {
+ super(id);
+ if (length%2!=0)
+ throw new IllegalArgumentException("length must be even: "+length);
+ this.length = length;
+ }
+
+ public HexStringConfidentialKey(Class owner, String shortName, int length) {
+ this(owner.getName()+'.'+shortName,length);
+ }
+
+ /**
+ * Returns the persisted hex string value.
+ *
+ * If the value isn't persisted, a new random value is created.
+ *
+ * @throws Error
+ * If the secret fails to load. Not throwing a checked exception is for the convenience
+ * of the caller.
+ */
+ public String get() {
+ try {
+ if (secret==null) {
+ synchronized (this) {
+ if (secret==null) {
+ byte[] payload = load();
+ if (payload==null) {
+ payload = ConfidentialStore.get().randomBytes(length/2);
+ store(payload);
+ }
+ secret = Util.toHexString(payload).substring(0,length);
+ }
+ }
+ }
+ return secret;
+ } catch (IOException e) {
+ throw new Error("Failed to load the key: "+getId(),e);
+ }
+ }
+}
diff --git a/core/src/main/java/jenkins/slaves/JnlpSlaveAgentProtocol.java b/core/src/main/java/jenkins/slaves/JnlpSlaveAgentProtocol.java
index 2050ce1ecd..664f5e3822 100644
--- a/core/src/main/java/jenkins/slaves/JnlpSlaveAgentProtocol.java
+++ b/core/src/main/java/jenkins/slaves/JnlpSlaveAgentProtocol.java
@@ -8,6 +8,7 @@ import hudson.remoting.Engine;
import hudson.slaves.SlaveComputer;
import jenkins.AgentProtocol;
import jenkins.model.Jenkins;
+import jenkins.security.HMACConfidentialKey;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@@ -31,10 +32,10 @@ import java.util.logging.Logger;
* unauthorized remote slaves.
*
*
- * The approach here is to have {@link Jenkins#getSecretKey() a secret key} on the master.
- * This key is sent to the slave inside the .jnlp file
+ * We do this by computing HMAC of the slave name.
+ * This code is sent to the slave inside the .jnlp file
* (this file itself is protected by HTTP form-based authentication that
- * we use everywhere else in Hudson), and the slave sends this
+ * we use everywhere else in Jenkins), and the slave sends this
* token back when it connects to the master.
* Unauthorized slaves can't access the protected .jnlp file,
* so it can't impersonate a valid slave.
@@ -84,12 +85,15 @@ public class JnlpSlaveAgentProtocol extends AgentProtocol {
}
protected void run() throws IOException, InterruptedException {
- if(!getSecretKey().equals(in.readUTF())) {
+ final String secret = in.readUTF();
+ final String nodeName = in.readUTF();
+
+ if(!SLAVE_SECRET.mac(nodeName).equals(secret)) {
error(out, "Unauthorized access");
return;
}
- final String nodeName = in.readUTF();
+
SlaveComputer computer = (SlaveComputer) Jenkins.getInstance().getComputer(nodeName);
if(computer==null) {
error(out, "No such slave: "+nodeName);
@@ -138,10 +142,6 @@ public class JnlpSlaveAgentProtocol extends AgentProtocol {
}
}
- protected String getSecretKey() {
- return Jenkins.getInstance().getSecretKey();
- }
-
protected void error(PrintWriter out, String msg) throws IOException {
out.println(msg);
LOGGER.log(Level.WARNING,Thread.currentThread().getName()+" is aborted: "+msg);
@@ -150,4 +150,9 @@ public class JnlpSlaveAgentProtocol extends AgentProtocol {
}
private static final Logger LOGGER = Logger.getLogger(JnlpSlaveAgentProtocol.class.getName());
+
+ /**
+ * This secret value is used as a seed for slaves.
+ */
+ public static final HMACConfidentialKey SLAVE_SECRET = new HMACConfidentialKey(JnlpSlaveAgentProtocol.class,"secret");
}
diff --git a/core/src/main/java/jenkins/slaves/JnlpSlaveAgentProtocol2.java b/core/src/main/java/jenkins/slaves/JnlpSlaveAgentProtocol2.java
index 413486e38d..86da2de8ae 100644
--- a/core/src/main/java/jenkins/slaves/JnlpSlaveAgentProtocol2.java
+++ b/core/src/main/java/jenkins/slaves/JnlpSlaveAgentProtocol2.java
@@ -57,12 +57,13 @@ public class JnlpSlaveAgentProtocol2 extends JnlpSlaveAgentProtocol {
Properties request = new Properties();
request.load(new ByteArrayInputStream(in.readUTF().getBytes("UTF-8")));
- if(!getSecretKey().equals(request.getProperty("Secret-Key"))) {
+ final String nodeName = request.getProperty("Node-Name");
+
+ if(!SLAVE_SECRET.mac(nodeName).equals(request.getProperty("Secret-Key"))) {
error(out, "Unauthorized access");
return;
}
- final String nodeName = request.getProperty("Node-Name");
SlaveComputer computer = (SlaveComputer) Jenkins.getInstance().getComputer(nodeName);
if(computer==null) {
error(out, "No such slave: "+nodeName);
diff --git a/core/src/main/resources/hudson/slaves/SlaveComputer/slave-agent.jnlp.jelly b/core/src/main/resources/hudson/slaves/SlaveComputer/slave-agent.jnlp.jelly
index 32962d7b31..317743245c 100644
--- a/core/src/main/resources/hudson/slaves/SlaveComputer/slave-agent.jnlp.jelly
+++ b/core/src/main/resources/hudson/slaves/SlaveComputer/slave-agent.jnlp.jelly
@@ -31,7 +31,9 @@ THE SOFTWARE.
-