@Override boolean verify(char[] guess) throws InvalidKeyException { // The OTP SASL mechanism handles this (this involves updating the stored password) throw new InvalidKeyException(); }
private InvalidKeyException invalidKey() throws InvalidKeyException { throw new InvalidKeyException("Decrypted data does not represent valid PKCS#8 PrivateKeyInfo"); }
boolean verify(final char[] guess) throws InvalidKeyException { try { return Arrays.equals(guess, unmask(algorithm, initialKeyMaterial, iterationCount, salt, maskedPasswordBytes)); } catch (InvalidKeySpecException e) { throw new InvalidKeyException(e); } }
/** * Gets the private key from bytes. * * @param keyBytes the key bytes * @return the private * @throws InvalidKeyException invalid key exception */ public static PrivateKey getPrivate(byte[] keyBytes) throws InvalidKeyException { try { PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance(RSA); return kf.generatePrivate(spec); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new InvalidKeyException(ex); } }
/** * Gets the public key from bytes. * * @param keyBytes the key bytes * @return the public * @throws InvalidKeyException invalid key exception */ public static PublicKey getPublic(byte[] keyBytes) throws InvalidKeyException { try { X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance(RSA); return kf.generatePublic(spec); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new InvalidKeyException(ex); } }
public synchronized void setBranch(String branch) throws InvalidKeyException, JSONException, IOException { if(mBranches==null) fetchBranches(); for (int i = 0; i < mBranches.length(); i++) { if((mBranches.getJSONObject(i)).getString("name").equals(branch)) { mBranch = (mBranches.getJSONObject(i)); mLastCommit = mBranch.getJSONObject("commit"); return; } } throw new InvalidKeyException("branch '" + branch + "' not found"); }
private static String getJcaSignatureAlgorithm( PublicKey publicKey, DigestAlgorithm digestAlgorithm) throws InvalidKeyException { String keyAlgorithm = publicKey.getAlgorithm(); String digestPrefixForSigAlg; switch (digestAlgorithm) { case SHA1: digestPrefixForSigAlg = "SHA1"; break; case SHA256: digestPrefixForSigAlg = "SHA256"; break; default: throw new IllegalArgumentException( "Unexpected digest algorithm: " + digestAlgorithm); } if ("RSA".equalsIgnoreCase(keyAlgorithm)) { return digestPrefixForSigAlg + "withRSA"; } else if ("DSA".equalsIgnoreCase(keyAlgorithm)) { return digestPrefixForSigAlg + "withDSA"; } else if ("EC".equalsIgnoreCase(keyAlgorithm)) { return digestPrefixForSigAlg + "withECDSA"; } else { throw new InvalidKeyException("Unsupported key algorithm: " + keyAlgorithm); } } }
private static byte[] encodePublicKey(PublicKey publicKey) throws InvalidKeyException { byte[] encodedPublicKey = null; if ("X.509".equals(publicKey.getFormat())) { encodedPublicKey = publicKey.getEncoded(); } if (encodedPublicKey == null) { try { encodedPublicKey = KeyFactory.getInstance(publicKey.getAlgorithm()) .getKeySpec(publicKey, X509EncodedKeySpec.class) .getEncoded(); } catch (NoSuchAlgorithmException e) { throw new InvalidKeyException( "Failed to obtain X.509 encoded form of public key " + publicKey + " of class " + publicKey.getClass().getName(), e); } catch (InvalidKeySpecException e) { throw new InvalidKeyException( "Failed to obtain X.509 encoded form of public key " + publicKey + " of class " + publicKey.getClass().getName(), e); } } if ((encodedPublicKey == null) || (encodedPublicKey.length == 0)) { throw new InvalidKeyException( "Failed to obtain X.509 encoded form of public key " + publicKey + " of class " + publicKey.getClass().getName()); } return encodedPublicKey; }
@Override protected Password engineTransform(final String algorithm, final Password password, final AlgorithmParameterSpec parameterSpec) throws InvalidKeyException, InvalidAlgorithmParameterException { if (password instanceof AbstractPasswordImpl) { final AbstractPasswordImpl abstractPassword = (AbstractPasswordImpl) password; if (algorithm.equals(abstractPassword.getAlgorithm())) { return abstractPassword.translate(parameterSpec); } } throw new InvalidKeyException(); } }
@Override protected boolean engineVerify(final String algorithm, final Password password, final char[] guess) throws InvalidKeyException { if (password instanceof AbstractPasswordImpl) { final AbstractPasswordImpl abstractPassword = (AbstractPasswordImpl) password; if (algorithm.equals(abstractPassword.getAlgorithm())) { return abstractPassword.verify(guess); } } throw new InvalidKeyException(); }
private SSLEngineConfigurator buildSslEngineConfigurator(Path certFile, Path keyFile, String keyPassword) throws GeneralSecurityException, IOException { if (keyFile == null || !Files.isRegularFile(keyFile) || !Files.isReadable(keyFile)) { throw new InvalidKeyException("Unreadable or missing private key: " + keyFile); } if (certFile == null || !Files.isRegularFile(certFile) || !Files.isReadable(certFile)) { throw new CertificateException("Unreadable or missing X.509 certificate: " + certFile); } final SSLContextConfigurator sslContextConfigurator = new SSLContextConfigurator(); final char[] password = firstNonNull(keyPassword, "").toCharArray(); final KeyStore keyStore = PemKeyStore.buildKeyStore(certFile, keyFile, password); sslContextConfigurator.setKeyStorePass(password); sslContextConfigurator.setKeyStoreBytes(KeyStoreUtils.getBytes(keyStore, password)); final SSLContext sslContext = sslContextConfigurator.createSSLContext(true); return new SSLEngineConfigurator(sslContext, false, false, false); }
@Override public final InvalidKeyException invalidKeyNoSuchMessageDigestAlgorithm(final String algorithm) { final InvalidKeyException result = new InvalidKeyException(String.format(getLoggingLocale(), invalidKeyNoSuchMessageDigestAlgorithm$str(), algorithm)); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String invalidKeyCannotVerifyPassword = "ELY08015: Cannot verify password";
@Override public final InvalidKeyException invalidKeyCannotVerifyPassword(final Throwable cause) { final InvalidKeyException result = new InvalidKeyException(String.format(getLoggingLocale(), invalidKeyCannotVerifyPassword$str()), cause); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String invalidKeyDesCryptPasswordHashMustBeBytes = "ELY08017: DES crypt password hash must be %d bytes";
@Override public final InvalidKeyException invalidKeyBsdDesCryptPasswordHashMustBeBytes(final int bytes) { final InvalidKeyException result = new InvalidKeyException(String.format(getLoggingLocale(), invalidKeyBsdDesCryptPasswordHashMustBeBytes$str(), bytes)); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String invalidKeySpecExpectedSpecGotSpec = "ELY08025: Expected to get a \"%s\" as spec, got \"%s\"";
@Override public final InvalidKeyException invalidKeyUnknownUnknownPasswordTypeOrAlgorithm() { final InvalidKeyException result = new InvalidKeyException(String.format(getLoggingLocale(), invalidKeyUnknownUnknownPasswordTypeOrAlgorithm$str())); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String noSuchAlgorithmInvalidAlgorithm = "ELY08028: Invalid algorithm \"%s\"";
@Override public final InvalidKeyException invalidKeyDesCryptPasswordHashMustBeBytes(final int bytes) { final InvalidKeyException result = new InvalidKeyException(String.format(getLoggingLocale(), invalidKeyDesCryptPasswordHashMustBeBytes$str(), bytes)); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String invalidParameterSpecSaltMustBeBytesBits = "ELY08018: Salt must be %d bytes (%d bits)";
public static byte[] calculateSignature(ECPrivateKey signingKey, byte[] message) throws InvalidKeyException { if (signingKey == null || message == null) { throw new InvalidKeyException("Values must not be null"); } if (signingKey.getType() == DJB_TYPE) { return Curve25519.getInstance(BEST) .calculateSignature(((DjbECPrivateKey) signingKey).getPrivateKey(), message); } else { throw new InvalidKeyException("Unknown type: " + signingKey.getType()); } }
private static byte[] generateApkSignatureSchemeV2Block( List<SignerConfig> signerConfigs, Map<ContentDigestAlgorithm, byte[]> contentDigests) throws InvalidKeyException, SignatureException { // FORMAT: // * length-prefixed sequence of length-prefixed signer blocks. List<byte[]> signerBlocks = new ArrayList<>(signerConfigs.size()); int signerNumber = 0; for (SignerConfig signerConfig : signerConfigs) { signerNumber++; byte[] signerBlock; try { signerBlock = generateSignerBlock(signerConfig, contentDigests); } catch (InvalidKeyException e) { throw new InvalidKeyException("Signer #" + signerNumber + " failed", e); } catch (SignatureException e) { throw new SignatureException("Signer #" + signerNumber + " failed", e); } signerBlocks.add(signerBlock); } return encodeAsSequenceOfLengthPrefixedElements( new byte[][] { encodeAsSequenceOfLengthPrefixedElements(signerBlocks), }); }
@Override boolean verify(char[] guess) throws InvalidKeyException { if (guess.length == 0) return false; try { byte[] output = scramDigest(this.getAlgorithm(), getNormalizedPasswordBytes(guess), this.getSalt(), this.getIterationCount()); return Arrays.equals(this.digest, output); } catch (NoSuchAlgorithmException nsae) { throw new InvalidKeyException(nsae); } }
public static boolean verifySignature(ECPublicKey signingKey, byte[] message, byte[] signature) throws InvalidKeyException { if (signingKey.getType() == DJB_TYPE) { return Curve25519.getInstance(BEST) .verifySignature(((DjbECPublicKey) signingKey).getPublicKey(), message, signature); } else { throw new InvalidKeyException("Unknown type: " + signingKey.getType()); } }