/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* This is an implementation of a Sun proprietary, exportable algorithm
* intended for use when protecting (or recovering the cleartext version of)
* sensitive keys.
* This algorithm is not intended as a general purpose cipher.
*
* This is how the algorithm works for key protection:
*
* p - user password
* s - random salt
* X - xor key
* P - to-be-protected key
* Y - protected key
* R - what gets stored in the keystore
*
* Step 1:
* Take the user's password, append a random salt (of fixed size) to it,
* and hash it: d1 = digest(p, s)
* Store d1 in X.
*
* Step 2:
* Take the user's password, append the digest result from the previous step,
* and hash it: dn = digest(p, dn-1).
* Store dn in X (append it to the previously stored digests).
* Repeat this step until the length of X matches the length of the private key
* P.
*
* Step 3:
* XOR X and P, and store the result in Y: Y = X XOR P.
*
* Step 4:
* Store s, Y, and digest(p, P) in the result buffer R:
* R = s + Y + digest(p, P), where "+" denotes concatenation.
* (NOTE: digest(p, P) is stored in the result buffer, so that when the key is
* recovered, we can check if the recovered key indeed matches the original
* key.) R is stored in the keystore.
*
* The protected key is recovered as follows:
*
* Step1 and Step2 are the same as above, except that the salt is not randomly
* generated, but taken from the result R of step 4 (the first length(s)
* bytes).
*
* Step 3 (XOR operation) yields the plaintext key.
*
* Then concatenate the password with the recovered key, and compare with the
* last length(digest(p, P)) bytes of R. If they match, the recovered key is
* indeed the same key as the original key.
*
* @author Jan Luehe
*
*
* @see java.security.KeyStore
* @see JavaKeyStore
* @see KeyTool
*
* @since 1.2
*/
final class KeyProtector {
// defined by JavaSoft
// The password used for protecting/recovering keys passed through this
// key protector. We store it as a byte array, so that we can digest it.
private byte[] passwdBytes;
/**
* Creates an instance of this class, and initializes it with the given
* password.
*
* <p>The password is expected to be in printable ASCII.
* Normal rules for good password selection apply: at least
* seven characters, mixed case, with punctuation encouraged.
* Phrases or words which are easily guessed, for example by
* being found in dictionaries, are bad.
*/
throws NoSuchAlgorithmException
{
int i, j;
throw new IllegalArgumentException("password can't be null");
}
// Convert password to byte array, so that it can be digested
passwdBytes[j++] = (byte)password[i];
}
}
/**
* Ensures that the password bytes of this key protector are
* set to zero when there are no more references to it.
*/
protected void finalize() {
if (passwdBytes != null) {
passwdBytes = null;
}
}
/*
* Protects the given plaintext key, using the password provided at
* construction time.
*/
{
int i;
int numRounds;
byte[] digest;
int xorOffset; // offset in xorKey where next digest will be stored
int encrKeyOffset = 0;
throw new IllegalArgumentException("plaintext key can't be null");
}
throw new KeyStoreException(
"Cannot get key bytes, not PKCS#8 encoded");
}
throw new KeyStoreException(
"Cannot get key bytes, encoding not supported");
}
// Determine the number of digest rounds
numRounds++;
// Create a random salt
// Set up the byte array which will be XORed with "plainKey"
// Compute the digests, and store them in "xorKey"
i < numRounds;
i++, xorOffset += DIGEST_LEN) {
// Copy the digest into "xorKey"
if (i < numRounds - 1) {
} else {
}
}
// XOR "plainKey" with "xorKey", and store the result in "tmpKey"
}
// Store salt and "tmpKey" in "encrKey"
// Append digest(password, plainKey) as an integrity check to "encrKey"
passwdBytes = null;
// wrap the protected private key in a PKCS#8-style
// EncryptedPrivateKeyInfo, and returns its encoding
try {
} catch (IOException ioe) {
}
}
/*
* Recovers the plaintext version of the given key (in protected format),
* using the password provided at construction time.
*/
throws UnrecoverableKeyException
{
int i;
byte[] digest;
int numRounds;
int xorOffset; // offset in xorKey where next digest will be stored
int encrKeyLen; // the length of the encrpyted key
// do we support the algorithm?
throw new UnrecoverableKeyException("Unsupported key protection "
+ "algorithm");
}
/*
* Get the salt associated with this key (the first SALT_LEN bytes of
* <code>protectedKey</code>)
*/
// Determine the number of digest rounds
// Get the encrypted key portion and store it in "encrKey"
byte[] encrKey = new byte[encrKeyLen];
// Set up the byte array which will be XORed with "encrKey"
// Compute the digests, and store them in "xorKey"
i < numRounds;
i++, xorOffset += DIGEST_LEN) {
// Copy the digest into "xorKey"
if (i < numRounds - 1) {
} else {
}
}
// XOR "encrKey" with "xorKey", and store the result in "plainKey"
}
/*
* Check the integrity of the recovered key by concatenating it with
* the password, digesting the concatenation, and comparing the
* result of the digest operation with the digest provided at the end
* of <code>protectedKey</code>. If the two digest values are
* different, throw an exception.
*/
passwdBytes = null;
throw new UnrecoverableKeyException("Cannot recover key");
}
}
// The parseKey() method of PKCS8Key parses the key
// algorithm and instantiates the appropriate key factory,
// which in turn parses the key material.
try {
} catch (IOException ioe) {
}
}
}