/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * 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.
*/
/** * Standard Test with Keys and the one generated through Spec.
*/ privatestaticvoid testKeySpecs(String provider, String kaAlgo,
String kpgAlgo, KeyPair kp) throws Exception {
KeyFactory kf = KeyFactory.getInstance(kpgAlgo, provider); // For each public KeySpec supported by KeyPairGenerator for (Class pubSpecType
: getCompatibleKeySpecs(kpgAlgo, KeyType.PUBLIC)) { //For each private KeySpec supported by KeyPairGenerator for (Class priSpecType
: getCompatibleKeySpecs(kpgAlgo, KeyType.PRIVATE)) { // Transform original PublicKey through KeySpec
KeySpec pubSpec = kf.getKeySpec(kp.getPublic(), pubSpecType);
PublicKey pubKey = kf.generatePublic(pubSpec); // Transform original PrivateKey through KeySpec
KeySpec priSpec = kf.getKeySpec(kp.getPrivate(), priSpecType);
PrivateKey priKey = kf.generatePrivate(priSpec);
// Test the keys are equal after transformation through KeySpec.
testKeyEquals(kp, pubKey, priKey); // Test the keys are serializable.
testSerialize(kp); // Test Parameter name. if (!kaAlgo.equals("DiffieHellman")) {
testParamName(priSpec, pubSpec);
} // Compare KeyAgreement secret generated from original keys // and by the keys after transformed through KeySpec. if (!Arrays.equals(getKeyAgreementSecret(provider, kaAlgo,
kp.getPublic(), kp.getPrivate()),
getKeyAgreementSecret(provider, kaAlgo,
pubKey, priKey))) { thrownew RuntimeException("KeyAgreement secret mismatch.");
}
}
}
}
/** * Standard Test with Keys and the one generated from encoded bytes.
*/ privatestaticvoid testEncodedKeySpecs(String provider, String kaAlgo,
String kpgAlgo, KeyPair kp) throws Exception {
X509EncodedKeySpec pubSpec
= new X509EncodedKeySpec(kp.getPublic().getEncoded());
PublicKey pubKey = kf.generatePublic(pubSpec);
// Test the keys are equal after transformation through KeySpec.
testKeyEquals(kp, pubKey, priKey); // Test the keys are serializable.
testSerialize(kp); // Test Parameter name. if (!kaAlgo.equals("DiffieHellman")) {
testParamName(priSpec, pubSpec);
} // Compare KeyAgreement secret generated from original keys // and by the keys after transformed through KeySpec. if (!Arrays.equals(getKeyAgreementSecret(provider, kaAlgo,
kp.getPublic(), kp.getPrivate()),
getKeyAgreementSecret(provider, kaAlgo, pubKey, priKey))) { thrownew RuntimeException("KeyAgreement secret mismatch.");
}
}
privateenum KeyType { PUBLIC, PRIVATE;
}
/** * Provides Compatible KeySpec Type list for KeyPairGenerator algorithm.
*/ privatestatic List<Class> getCompatibleKeySpecs(String kpgAlgo,
KeyType type) {
/** * Compare original KeyPair with transformed ones.
*/ privatestaticvoid testKeyEquals(KeyPair kp, PublicKey pubKey,
PrivateKey priKey) {
if (!kp.getPrivate().equals(priKey)
&& kp.getPrivate().hashCode() != priKey.hashCode()) { thrownew RuntimeException("PrivateKey is not equal with PrivateKey"
+ " generated through KeySpec");
} if (!kp.getPublic().equals(pubKey)
&& kp.getPublic().hashCode() != pubKey.hashCode()) { thrownew RuntimeException("PublicKey is not equal with PublicKey"
+ " generated through KeySpec");
}
}
/** * Compare the parameter names of Public/Private KeySpec from a pair.
*/ privatestaticvoid testParamName(KeySpec priSpec, KeySpec pubSpec) {
if (priSpec instanceof XECPrivateKeySpec
&& pubSpec instanceof XECPublicKeySpec) { if (((NamedParameterSpec) ((XECPrivateKeySpec) priSpec)
.getParams()).getName()
!= ((NamedParameterSpec) ((XECPublicKeySpec) pubSpec)
.getParams()).getName()) { thrownew RuntimeException("Curve name mismatch found");
}
}
}
/** * Test serialization of KeyPair and Keys it holds.
*/ privatestaticvoid testSerialize(KeyPair keyPair) throws Exception {
// Verify Serialized PrivateKey instance. if (!keyPair.getPrivate().equals(
deserializedCopy(keyPair.getPrivate(), PrivateKey.class))) { thrownew RuntimeException("PrivateKey is not equal with PrivateKey"
+ " generated through Serialization");
} // Verify Serialized PublicKey instance. if (!keyPair.getPublic().equals(
deserializedCopy(keyPair.getPublic(), PublicKey.class))) { thrownew RuntimeException("PublicKey is not equal with PublicKey"
+ " generated through Serialization");
} // Verify Serialized KeyPair instance.
KeyPair copy = deserializedCopy(keyPair, KeyPair.class); if (!keyPair.getPrivate().equals(copy.getPrivate())) { thrownew RuntimeException("PrivateKey is not equal with PrivateKey"
+ " generated through Serialized KeyPair");
} if (!keyPair.getPublic().equals(copy.getPublic())) { thrownew RuntimeException("PublicKey is not equal with PublicKey"
+ " generated through Serialized KeyPair");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.