protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType) throws InvalidKeyException, NoSuchAlgorithmException { byte[] encoded; try { if (wrapEngine == null) { encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length); } else { encoded = wrapEngine.unwrap(wrappedKey, 0, wrappedKey.length); } } catch (InvalidCipherTextException e) { throw new InvalidKeyException(e.getMessage()); } catch (BadPaddingException e) { throw new InvalidKeyException(e.getMessage()); } catch (IllegalBlockSizeException e2) { throw new InvalidKeyException(e2.getMessage()); } if (wrappedKeyType == Cipher.SECRET_KEY) { return new SecretKeySpec(encoded, wrappedKeyAlgorithm); } else if (wrappedKeyAlgorithm.equals("") && wrappedKeyType == Cipher.PRIVATE_KEY) { /* * The caller doesn't know the algorithm as it is part of * the encrypted data. */ try { PrivateKeyInfo in = PrivateKeyInfo.getInstance(encoded); PrivateKey privKey = BouncyCastleProvider.getPrivateKey(in); if (privKey != null) { return privKey; } else { throw new InvalidKeyException( "algorithm " + in.getPrivateKeyAlgorithm().getAlgorithm() + " not supported"); } } catch (Exception e) { throw new InvalidKeyException("Invalid key encoding."); } } else { try { KeyFactory kf = KeyFactory.getInstance(wrappedKeyAlgorithm, BouncyCastleProvider.PROVIDER_NAME); if (wrappedKeyType == Cipher.PUBLIC_KEY) { return kf.generatePublic(new X509EncodedKeySpec(encoded)); } else if (wrappedKeyType == Cipher.PRIVATE_KEY) { return kf.generatePrivate(new PKCS8EncodedKeySpec(encoded)); } } catch (NoSuchProviderException e) { throw new InvalidKeyException("Unknown key type " + e.getMessage()); } catch (InvalidKeySpecException e2) { throw new InvalidKeyException("Unknown key type " + e2.getMessage()); } throw new InvalidKeyException("Unknown key type " + wrappedKeyType); } }
private void testEncoding(ECPrivateKey privKey, ECPublicKey pubKey) throws Exception { KeyFactory kFact = KeyFactory.getInstance("ECDSA", "SC"); byte[] bytes = privKey.getEncoded(); PrivateKeyInfo sInfo = PrivateKeyInfo.getInstance(new ASN1InputStream(bytes).readObject()); if (!sInfo.getPrivateKeyAlgorithm().getParameters().equals(DERNull.INSTANCE)) { fail("private key parameters wrong"); } ECPrivateKey sKey = (ECPrivateKey) kFact.generatePrivate(new PKCS8EncodedKeySpec(bytes)); if (!sKey.equals(privKey)) { fail("private equals failed"); } if (sKey.hashCode() != privKey.hashCode()) { fail("private hashCode failed"); } bytes = pubKey.getEncoded(); SubjectPublicKeyInfo vInfo = SubjectPublicKeyInfo.getInstance(new ASN1InputStream(bytes).readObject()); if (!vInfo.getAlgorithm().getParameters().equals(DERNull.INSTANCE)) { fail("public key parameters wrong"); } ECPublicKey vKey = (ECPublicKey) kFact.generatePublic(new X509EncodedKeySpec(bytes)); if (!vKey.equals(pubKey) || vKey.hashCode() != pubKey.hashCode()) { fail("public equals/hashCode failed"); } testBCParamsAndQ(sKey, vKey); testEC5Params(sKey, vKey); testECDSA(sKey, vKey); }
protected Key engineUnwrap(byte[] paramArrayOfByte, String paramString, int paramInt) { try { if (this.aXL == null) { paramArrayOfByte = engineDoFinal(paramArrayOfByte, 0, paramArrayOfByte.length); } else { paramArrayOfByte = this.aXL.ﹳ(paramArrayOfByte, 0, paramArrayOfByte.length); } } catch (InvalidCipherTextException paramArrayOfByte) { throw new InvalidKeyException(paramArrayOfByte.getMessage()); } catch (BadPaddingException paramArrayOfByte) { throw new InvalidKeyException(paramArrayOfByte.getMessage()); } catch (IllegalBlockSizeException paramArrayOfByte) { throw new InvalidKeyException(paramArrayOfByte.getMessage()); } if (paramInt == 3) { return new SecretKeySpec(paramArrayOfByte, paramString); } if ((paramString.equals("")) && (paramInt == 2)) { try { paramArrayOfByte = PrivateKeyInfo.ﹷ(paramArrayOfByte); paramString = BouncyCastleProvider.ˏ(paramArrayOfByte); if (paramString != null) { return paramString; } throw new InvalidKeyException("algorithm " + paramArrayOfByte.fW().fK() + " not supported"); } catch (Exception paramArrayOfByte) { throw new InvalidKeyException("Invalid key encoding."); } } try { paramString = KeyFactory.getInstance(paramString, "SC"); if (paramInt == 1) { paramArrayOfByte = paramString.generatePublic(new X509EncodedKeySpec(paramArrayOfByte)); return paramArrayOfByte; } if (paramInt == 2) { paramArrayOfByte = paramString.generatePrivate(new PKCS8EncodedKeySpec(paramArrayOfByte)); return paramArrayOfByte; } } catch (NoSuchProviderException paramArrayOfByte) { throw new InvalidKeyException("Unknown key type " + paramArrayOfByte.getMessage()); } catch (InvalidKeySpecException paramArrayOfByte) { throw new InvalidKeyException("Unknown key type " + paramArrayOfByte.getMessage()); } throw new InvalidKeyException("Unknown key type " + paramInt); }