@Override protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException { if (input != null) { engineUpdate(input, inputOffset, inputLen); } if (inputTooLarge) { throw new IllegalBlockSizeException("input must be under " + buffer.length + " bytes"); } final byte[] tmpBuf; if (bufferOffset != buffer.length) { if (padding == NativeCrypto.RSA_NO_PADDING) { tmpBuf = new byte[buffer.length]; System.arraycopy(buffer, 0, tmpBuf, buffer.length - bufferOffset, bufferOffset); } else { tmpBuf = Arrays.copyOf(buffer, bufferOffset); } } else { tmpBuf = buffer; } byte[] output = new byte[buffer.length]; int resultSize; if (encrypting) { if (usingPrivateKey) { resultSize = NativeCrypto.RSA_private_encrypt( tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding); } else { resultSize = NativeCrypto.RSA_public_encrypt( tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding); } } else { try { if (usingPrivateKey) { resultSize = NativeCrypto.RSA_private_decrypt( tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding); } else { resultSize = NativeCrypto.RSA_public_decrypt( tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding); } } catch (SignatureException e) { IllegalBlockSizeException newE = new IllegalBlockSizeException(); newE.initCause(e); throw newE; } } if (!encrypting && resultSize != output.length) { output = Arrays.copyOf(output, resultSize); } bufferOffset = 0; return output; }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPublicKey) { OpenSSLECPublicKey other = (OpenSSLECPublicKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLRSAPublicKey) { OpenSSLRSAPublicKey other = (OpenSSLRSAPublicKey) o; /* * We can shortcut the true case, but it still may be equivalent but * different copies. */ if (key.equals(other.getOpenSSLKey())) { return true; } } if (!(o instanceof RSAPublicKey)) { return false; } ensureReadParams(); RSAPublicKey other = (RSAPublicKey) o; return modulus.equals(other.getModulus()) && publicExponent.equals(other.getPublicExponent()); }
private ECPoint getPublicKey() { final OpenSSLECPointContext pubKey = new OpenSSLECPointContext( group, new NativeRef.EC_POINT(NativeCrypto.EC_KEY_get_public_key(key.getNativeRef()))); return pubKey.getECPoint(); }
private void writeObject(ObjectOutputStream stream) throws IOException { if (key.isEngineBased()) { throw new NotSerializableException("engine-based keys can not be serialized"); } stream.defaultWriteObject(); stream.writeObject(getEncoded()); }
@Override public BigInteger getS() { if (key.isEngineBased()) { throw new UnsupportedOperationException("private key value S cannot be extracted"); } return getPrivateKey(); }
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); byte[] encoded = (byte[]) stream.readObject(); key = new OpenSSLKey(NativeCrypto.d2i_PUBKEY(encoded)); group = new OpenSSLECGroupContext( new NativeRef.EC_GROUP(NativeCrypto.EC_KEY_get1_group(key.getNativeRef()))); }
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); byte[] encoded = (byte[]) stream.readObject(); key = new OpenSSLKey(NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(encoded)); final long origGroup = NativeCrypto.EC_KEY_get0_group(key.getPkeyContext()); group = new OpenSSLECGroupContext(NativeCrypto.EC_GROUP_dup(origGroup)); }
private void ensureReadParams() { if (fetchedParams) { return; } byte[][] params = NativeCrypto.get_RSA_public_params(key.getPkeyContext()); modulus = new BigInteger(params[0]); publicExponent = new BigInteger(params[1]); fetchedParams = true; }
/** * Returns strength of the private/public key in bits. * * @return strength of the key in bits. Returns -1 if unable to determine it. */ public int getStrength(String password) throws CredentialException { if (opensslKey == null) { return -1; } if (this.opensslKey.isEncrypted()) { if (password == null) { throw new CredentialException("Key encrypted, password required"); } else { try { this.opensslKey.decrypt(password); } catch (GeneralSecurityException exp) { throw new CredentialException(exp.getMessage(), exp); } } } return ((RSAPrivateKey) opensslKey.getPrivateKey()).getModulus().bitLength(); }
private final synchronized void ensureReadParams() { if (fetchedParams) { return; } byte[][] params = NativeCrypto.get_DSA_params(key.getPkeyContext()); if (params[0] != null) { g = new BigInteger(params[0]); } if (params[1] != null) { p = new BigInteger(params[1]); } if (params[2] != null) { q = new BigInteger(params[2]); } if (params[3] != null) { y = new BigInteger(params[3]); } if (params[4] != null) { x = new BigInteger(params[4]); } fetchedParams = true; }
@Override public byte[] getEncoded() { return NativeCrypto.i2d_PUBKEY(key.getNativeRef()); }
public OpenSSLECPublicKey(OpenSSLKey key) { this.group = new OpenSSLECGroupContext( new NativeRef.EC_GROUP(NativeCrypto.EC_KEY_get1_group(key.getNativeRef()))); this.key = key; }
@Override public String toString() { return NativeCrypto.EVP_PKEY_print_public(key.getNativeRef()); }
@Override public byte[] getEncoded() { return NativeCrypto.i2d_PUBKEY(key.getPkeyContext()); }
@Override public byte[] getEncoded() { return NativeCrypto.i2d_PKCS8_PRIV_KEY_INFO(key.getPkeyContext()); }
public OpenSSLECPrivateKey(OpenSSLKey key) { final long origGroup = NativeCrypto.EC_KEY_get0_group(key.getPkeyContext()); this.group = new OpenSSLECGroupContext(NativeCrypto.EC_GROUP_dup(origGroup)); this.key = key; }
@Override public int hashCode() { return Arrays.hashCode(NativeCrypto.i2d_PKCS8_PRIV_KEY_INFO(key.getPkeyContext())); }
@Override public int hashCode() { return Arrays.hashCode(NativeCrypto.i2d_PUBKEY(key.getNativeRef())); }
private BigInteger getPrivateKey() { return new BigInteger(NativeCrypto.EC_KEY_get_private_key(key.getPkeyContext())); }
@Override public String toString() { return NativeCrypto.EVP_PKEY_print_private(key.getPkeyContext()); }