/*
* 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.
*
* 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.
*/
/**
* @test
* @bug 4900891
* @summary Test the EncryptedPrivateKeyInfo.getKeySpec(...)
* methods with scenarios where the decrypted bytes are not
* encoded correctly per PKCS#8 standard.
* @author Valerie Peng
*/
public class GetKeySpecInvalidEncoding {
static {
try {
(cipherAlg, "SunJCE");
// should never happen
GOOD_PARAMS = null;
}
}
private static String encryptedPKCS8 = "5C:BC:13:5D:40:2F:02:28:94:3C:A9:F7:98:A6:58:DC:F9:12:B7:CB:0F:40:DD:66:AB:58:6B:23:2F:8A:5A:81:9D:55:2A:EB:3F:AA:6A:CE:AE:23:8A:96:12:21:5A:09:BF:59:65:3F:B8:48:59:69:C6:D0:9C:48:B6:78:C3:C6:B4:24:F9:BE:10:00:D5:F3:52:88:53:CD:07:CA:88:93:15:3F:BA:19:4A:E9:5D:C7:44:46:49:F6:19:83:86:E0:25:51:9E:83:6D:AE:F2:14:9C:BF:02:7B:8C:64:B4:5F:F1:7B:28:2F:39:55:32:A4:F5:41:85:9E:77:F2:07:09:CD:97:90:5A:04:81:23:30";
try {
int n = s.length();
StringReader r = new StringReader(s);
while (true) {
int b1 = nextNibble(r);
if (b1 < 0) {
break;
}
int b2 = nextNibble(r);
if (b2 < 0) {
throw new RuntimeException("Invalid string " + s);
}
}
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
while (true) {
if (ch == -1) {
return -1;
return ch - '0';
}
}
}
if (GOOD_PARAMS == null) {
throw new Exception("Static parameter generation failed");
}
// generate encrypted data and EncryptedPrivateKeyInfo object
// TEST#1 getKeySpec(Cipher)
// Prepare Cipher for decryption
try {
throw new Exception("getKeySpec(Cipher): should throw IKSE");
} catch (InvalidKeySpecException ikse) {
// expected
}
// TEST#2 getKeySpec(Key)
try {
throw new Exception("getKeySpec(Key): should throw IKE");
} catch (InvalidKeyException ike) {
// expected
}
// TEST#3 getKeySpec(Key, String)
try {
throw new Exception("getKeySpec(Key, String): should throw IKE");
} catch (InvalidKeyException ike) {
// expected
}
// TEST#4 getKeySpec(Key, Provider)
try {
throw new Exception("getKeySpec(Key, Provider): should throw IKE");
} catch (InvalidKeyException ike) {
// expected
}
}
}