List of usage examples for org.bouncycastle.openpgp PGPOnePassSignature getKeyID
public long getKeyID()
From source file:alpha.offsync.security.OpenPGPSecurityUtility.java
License:Apache License
@Override public void verify(OutputStream outputStream, final InputStream inputStream) { try {// ww w . j av a 2 s . com final File keyFile = this.publicKeyRing; final InputStream in = PGPUtil.getDecoderStream(inputStream); PGPObjectFactory pgpFact = new PGPObjectFactory(in); final PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject(); pgpFact = new PGPObjectFactory(c1.getDataStream()); final PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact.nextObject(); final PGPOnePassSignature ops = p1.get(0); final PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject(); final InputStream dIn = p2.getInputStream(); int ch; final PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream(new FileInputStream(keyFile))); final PGPPublicKey key = pgpRing.getPublicKey(ops.getKeyID()); ops.init(new BcPGPContentVerifierBuilderProvider(), key); while ((ch = dIn.read()) >= 0) { ops.update((byte) ch); outputStream.write(ch); } outputStream.close(); final PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject(); if (!ops.verify(p3.get(0))) { outputStream = null; } } catch (final FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (final SignatureException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (final IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (final PGPException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Check a one-pass signature/*ww w . j av a2s .c om*/ */ private boolean checkOnePassSignature(OutputStream out, PGPOnePassSignatureList p1, PGPObjectFactory pgpFact, PGPPublicKeyRingCollection pgpRing) throws PGPException { try { PGPOnePassSignature ops = null; PGPPublicKey key = null; int count = 0; while (count < p1.size()) { ops = p1.get(count); key = pgpRing.getPublicKey(ops.getKeyID()); if (key != null) { break; } count++; } if (key == null) { throw new PGPException("Corresponding public key not found"); } if (key.isRevoked()) { String keyId = Long.toHexString(key.getKeyID()).substring(8); System.out.println("Warning: Signing key (0x" + keyId + ") has been revoked"); // throw new PGPException("Signing key (0x"+keyId+") has been revoked"); } PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject(); // if (outputFilename == null) { // outputFilename = ld.getFileName(); // } // // FileOutputStream out = new FileOutputStream(outputFilename); InputStream dataIn = ld.getInputStream(); ops.initVerify(key, "BC"); int ch; while ((ch = dataIn.read()) >= 0) { ops.update((byte) ch); out.write(ch); } out.close(); PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject(); return ops.verify(p3.get(0)); } catch (PGPException e) { throw e; } catch (Exception e) { throw new PGPException("Error in verification", e); } }
From source file:com.verhas.licensor.License.java
License:Open Source License
/** * Open an encoded license from input stream and decode and load it. If the * file can not be loaded or is not signed properly then the method {@see * #isVerified()} will return false.//from w w w .j av a 2s . com * <p> * Otherwise the license will be loaded and can be used. * * @param in * @throws IOException * @throws PGPException */ public void setLicenseEncoded(InputStream in) throws IOException, PGPException { final ByteArrayInputStream keyIn = new ByteArrayInputStream(publicKeyRing); in = PGPUtil.getDecoderStream(in); PGPObjectFactory pgpFact = new PGPObjectFactory(in); final PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject(); pgpAssertNotNull(c1); pgpFact = new PGPObjectFactory(c1.getDataStream()); final PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact.nextObject(); pgpAssertNotNull(p1); final PGPOnePassSignature ops = p1.get(0); final PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject(); pgpAssertNotNull(p2); final InputStream dIn = p2.getInputStream(); pgpAssertNotNull(dIn); int ch; final PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn)); pgpAssertNotNull(ops); decodeKeyId = ops.getKeyID(); if (decodeKeyId == null) { // there is no key in the key ring that can decode the license verified = false; licenseProperties = null; } else { final PGPPublicKey decodeKey = pgpRing.getPublicKey(decodeKeyId); final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ops.initVerify(decodeKey, "BC"); while ((ch = dIn.read()) >= 0) { ops.update((byte) ch); out.write(ch); } final PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject(); if (ops.verify(p3.get(0))) { setLicense(new String(out.toByteArray())); verified = true; } else { verified = false; licenseProperties = null; } } catch (final Exception e) { verified = false; licenseProperties = null; } } }
From source file:crypttools.PGPCryptoBC.java
License:Open Source License
public boolean validateData(String data, String publicKey) throws Exception { Security.addProvider(new BouncyCastleProvider()); File fileToVerify = File.createTempFile("temp", ".privateScrap"); FileUtils.writeStringToFile(fileToVerify, data); File publicKeyFile = File.createTempFile("temp", ".publicScrap"); // Creates an exception // System.out.println(this.armoredPublicKey); // String armoredKeyString = getPublicKey(); // System.out.println(armoredKeyString); FileUtils.writeStringToFile(publicKeyFile, publicKey); //FileUtils.writeStringToFile(publicKeyFile, new String(this.armoredPublicKey, "UTF-8")); try {//from w w w . ja va2 s . com InputStream in = PGPUtil.getDecoderStream(new FileInputStream(fileToVerify)); PGPObjectFactory pgpObjFactory = new PGPObjectFactory(in); PGPCompressedData compressedData = (PGPCompressedData) pgpObjFactory.nextObject(); //Get the signature from the file pgpObjFactory = new PGPObjectFactory(compressedData.getDataStream()); PGPOnePassSignatureList onePassSignatureList = (PGPOnePassSignatureList) pgpObjFactory.nextObject(); PGPOnePassSignature onePassSignature = onePassSignatureList.get(0); //Get the literal data from the file PGPLiteralData pgpLiteralData = (PGPLiteralData) pgpObjFactory.nextObject(); InputStream literalDataStream = pgpLiteralData.getInputStream(); InputStream keyIn = new FileInputStream(publicKeyFile); PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn)); PGPPublicKey key = pgpRing.getPublicKey(onePassSignature.getKeyID()); FileOutputStream literalDataOutputStream = new FileOutputStream(pgpLiteralData.getFileName()); onePassSignature.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key); int ch; while ((ch = literalDataStream.read()) >= 0) { onePassSignature.update((byte) ch); literalDataOutputStream.write(ch); } literalDataOutputStream.close(); //Get the signature from the written out file PGPSignatureList p3 = (PGPSignatureList) pgpObjFactory.nextObject(); PGPSignature signature = p3.get(0); //Verify the two signatures boolean valid = onePassSignature.verify(signature); return valid; } catch (Exception e) { System.out.println("Got an Exception: " + e.getMessage()); return false; //do something clever with the exception } finally { fileToVerify.delete(); publicKeyFile.delete(); } }
From source file:eu.mrbussy.security.crypto.pgp.PGPDecryptor.java
License:Open Source License
public InputStream decryptFile(InputStream in) throws Exception { InputStream is = null;//from ww w . j av a 2 s . c o m byte[] bytes = null; InputStream keyIn = new FileInputStream(new File(privateKeyFilePath)); char[] passwd = password.toCharArray(); in = PGPUtil.getDecoderStream(in); PGPObjectFactory pgpF = new PGPObjectFactory(in); PGPEncryptedDataList enc; Object o = pgpF.nextObject(); // // the first object might be a PGP marker packet. // if (o instanceof PGPEncryptedDataList) { enc = (PGPEncryptedDataList) o; } else { enc = (PGPEncryptedDataList) pgpF.nextObject(); } // // find the secret key // Iterator<PGPPublicKeyEncryptedData> it = enc.getEncryptedDataObjects(); PGPPrivateKey sKey = null; PGPPublicKeyEncryptedData pbe = null; while (sKey == null && it.hasNext()) { pbe = it.next(); sKey = PGPUtils.findPrivateKey(keyIn, pbe.getKeyID(), passwd); } if (sKey == null) { throw new IllegalArgumentException("secret key for message not found."); } InputStream clear = pbe.getDataStream(sKey, "BC"); PGPObjectFactory plainFact = new PGPObjectFactory(clear); Object message = plainFact.nextObject(); PGPObjectFactory pgpFact = null; if (message instanceof PGPCompressedData) { PGPCompressedData cData = (PGPCompressedData) message; pgpFact = new PGPObjectFactory(cData.getDataStream()); message = pgpFact.nextObject(); } PGPOnePassSignature ops = null; if (message instanceof PGPOnePassSignatureList) { if (isSigned) { PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) message; ops = p1.get(0); long keyId = ops.getKeyID(); PGPPublicKey signerPublicKey = PGPUtils.readPublicKey(signingPublicKeyFilePath, keyId); ops.initVerify(signerPublicKey, "BC"); } message = pgpFact.nextObject(); } if (message instanceof PGPLiteralData) { PGPLiteralData ld = (PGPLiteralData) message; if (pbe.isIntegrityProtected()) { if (!pbe.verify()) { throw new PGPException("message failed integrity check"); } } is = ld.getInputStream(); bytes = IOUtils.toByteArray(is); if (isSigned) { ops.update(bytes); PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject(); if (!ops.verify(p3.get(0))) { throw new PGPException("Signature verification failed!"); } } } else { throw new PGPException("message is not a simple encrypted file - type unknown."); } return new ByteArrayInputStream(bytes); }
From source file:org.apache.camel.converter.crypto.PGPDataFormat.java
License:Apache License
@SuppressWarnings("resource") public Object unmarshal(Exchange exchange, InputStream encryptedStream) throws Exception { if (encryptedStream == null) { return null; }// w ww. j av a2 s . co m InputStream in = PGPUtil.getDecoderStream(encryptedStream); PGPObjectFactory pgpFactory = new PGPObjectFactory(in); Object o = pgpFactory.nextObject(); // the first object might be a PGP marker packet PGPEncryptedDataList enc; if (o instanceof PGPEncryptedDataList) { enc = (PGPEncryptedDataList) o; } else { enc = (PGPEncryptedDataList) pgpFactory.nextObject(); } PGPPublicKeyEncryptedData pbe = null; PGPPrivateKey key = null; // find encrypted data for which a private key exists in the secret key ring for (int i = 0; i < enc.size() && key == null; i++) { pbe = (PGPPublicKeyEncryptedData) enc.get(i); key = PGPDataFormatUtil.findPrivateKeyWithKeyId(exchange.getContext(), findKeyFileName(exchange), findEncryptionKeyRing(exchange), pbe.getKeyID(), findKeyPassword(exchange), getPassphraseAccessor(), getProvider()); } if (key == null) { throw new PGPException("Provided input is encrypted with unknown pair of keys."); } InputStream encData = pbe .getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider(getProvider()).build(key)); pgpFactory = new PGPObjectFactory(encData); PGPCompressedData comData = (PGPCompressedData) pgpFactory.nextObject(); pgpFactory = new PGPObjectFactory(comData.getDataStream()); Object object = pgpFactory.nextObject(); PGPOnePassSignature signature; if (object instanceof PGPOnePassSignatureList) { signature = getSignature(exchange, (PGPOnePassSignatureList) object); object = pgpFactory.nextObject(); } else { signature = null; } PGPLiteralData ld = (PGPLiteralData) object; InputStream litData = ld.getInputStream(); // enable streaming via OutputStreamCache CachedOutputStream cos; ByteArrayOutputStream bos; OutputStream os; if (exchange.getContext().getStreamCachingStrategy().isEnabled()) { cos = new CachedOutputStream(exchange); bos = null; os = cos; } else { cos = null; bos = new ByteArrayOutputStream(); os = bos; } try { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = litData.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); if (signature != null) { signature.update(buffer, 0, bytesRead); } os.flush(); } } finally { IOHelper.close(os, litData, encData, in); } if (signature != null) { PGPSignatureList sigList = (PGPSignatureList) pgpFactory.nextObject(); if (!signature.verify(getSignatureWithKeyId(signature.getKeyID(), sigList))) { throw new SignatureException("Cannot verify PGP signature"); } } if (cos != null) { return cos.newStreamCache(); } else { return bos.toByteArray(); } }
From source file:org.apache.camel.converter.crypto.PGPDataFormat.java
License:Apache License
protected PGPOnePassSignature getSignature(Exchange exchange, PGPOnePassSignatureList signatureList) throws IOException, PGPException, NoSuchProviderException { for (int i = 0; i < signatureList.size(); i++) { PGPOnePassSignature signature = signatureList.get(i); // Determine public key from signature keyId PGPPublicKey sigPublicKey = PGPDataFormatUtil.findPublicKeyWithKeyId(exchange.getContext(), findSignatureKeyFileName(exchange), findSignatureKeyRing(exchange), signature.getKeyID(), false);/*w ww.ja v a 2 s .c o m*/ if (sigPublicKey == null) { continue; } // choose that signature for which a public key exists! signature.init(new JcaPGPContentVerifierBuilderProvider().setProvider(getProvider()), sigPublicKey); return signature; } if (signatureList.isEmpty()) { return null; } else { throw new IllegalArgumentException( "No public key found fitting to the signature key Id; cannot verify the signature"); } }
From source file:org.apache.camel.converter.crypto.PGPKeyAccessDataFormat.java
License:Apache License
private void verifySignature(PGPObjectFactory pgpFactory, PGPOnePassSignature signature) throws IOException, PGPException, SignatureException { if (signature != null) { PGPSignatureList sigList = (PGPSignatureList) pgpFactory.nextObject(); if (!signature.verify(getSignatureWithKeyId(signature.getKeyID(), sigList))) { throw new SignatureException("Verification of the PGP signature with the key ID " + signature.getKeyID() + " failed. The PGP message may have been tampered."); }//from w w w . java2 s . co m } }
From source file:org.apache.camel.converter.crypto.PGPKeyAccessDataFormat.java
License:Apache License
protected PGPOnePassSignature getSignature(Exchange exchange, PGPOnePassSignatureList signatureList) throws Exception { if (SIGNATURE_VERIFICATION_OPTION_IGNORE.equals(getSignatureVerificationOption())) { return null; }//from w ww. j a v a 2 s. c o m if (SIGNATURE_VERIFICATION_OPTION_NO_SIGNATURE_ALLOWED.equals(getSignatureVerificationOption())) { throw new PGPException( "PGP message contains a signature although a signature is not expected. Either change the configuration of the PGP decryptor or send a PGP message with no signature."); } List<String> allowedUserIds = determineSignaturenUserIds(exchange); for (int i = 0; i < signatureList.size(); i++) { PGPOnePassSignature signature = signatureList.get(i); // Determine public key from signature keyId PGPPublicKey sigPublicKey = publicKeyAccessor.getPublicKey(exchange, signature.getKeyID(), allowedUserIds); if (sigPublicKey == null) { continue; } // choose that signature for which a public key exists! signature.init(new JcaPGPContentVerifierBuilderProvider().setProvider(getProvider()), sigPublicKey); return signature; } if (signatureList.isEmpty()) { return null; } else { throw new IllegalArgumentException( "Cannot verify the PGP signature: No public key found for the key ID(s) contained in the PGP signature(s). " + "Either the received PGP message contains a signature from an unexpected sender or the Public Keyring does not contain the public key of the sender."); } }
From source file:org.brownsocks.payments.gateways.enets.pgp.BCPGPProvider.java
@Override public String decryptAndVerify(String messageIn) throws IOException, SignatureVerificationException { try {//from w w w. j av a2 s . com /* Stage zero: Convert to ASCII armored format and open a decoding stream */ InputStream is = new ByteArrayInputStream(Base64.decode(messageIn)); InputStream decoderStream = PGPUtil.getDecoderStream(is); /* Stage one: Init a decrypting stream */ PGPObjectFactory pgpFactory = new PGPObjectFactory(decoderStream); PGPEncryptedDataList cryptedDataList = (PGPEncryptedDataList) pgpFactory.nextObject(); PGPPublicKeyEncryptedData cryptedData = (PGPPublicKeyEncryptedData) cryptedDataList.get(0); InputStream clearStream = cryptedData.getDataStream(getCryptingPrivateKey(), _provider); /* Stage two: Seperate the XML data from the signatures */ PGPObjectFactory plainFact = new PGPObjectFactory(clearStream); PGPOnePassSignatureList onePassSignatureList = (PGPOnePassSignatureList) plainFact.nextObject(); PGPLiteralData literalData = (PGPLiteralData) plainFact.nextObject(); String xmlMessage = IOUtils.toString(literalData.getInputStream()); PGPSignatureList signatureList = (PGPSignatureList) plainFact.nextObject(); /* Stage three: Verify signature */ PGPOnePassSignature ops = onePassSignatureList.get(0); PGPPublicKey key = _remotePublicKeyRing.getPublicKey(ops.getKeyID()); ops.initVerify(key, _provider); ops.update(xmlMessage.getBytes()); if (!ops.verify(signatureList.get(0))) { throw new SignatureVerificationException( "Failed to verify message signature. Message authenticity cannot be thrusted."); } return xmlMessage; } catch (PGPException pgpException) { throw new IOException("PGP subsystem problem.", pgpException); } catch (SignatureException signException) { throw new IOException("PGP subsystem problem.", signException); } catch (Throwable t) { throw new IOException("Unknown error occured in PGP subsystem: " + t.getMessage(), t); } }