List of usage examples for org.bouncycastle.bcpg ArmoredInputStream close
public void close() throws IOException
From source file:cc.arduino.packages.security.ClearSignedVerifier.java
License:Open Source License
/** * Verify a PGP clearText-signature./*from w w w .j av a 2 s .co m*/ * * @param signedTextFile A File containing the clearText signature * @param pubKeyRing A public key-ring containing the public key needed for the * signature verification * @return A VerifyResult class with the clearText and the signature * verification status * @throws FileNotFoundException */ public static VerifyResult verify(File signedTextFile, PGPPublicKeyRingCollection pubKeyRing) { // Create the result object VerifyResult result = new VerifyResult(); result.clearText = null; result.verified = false; result.error = null; ArmoredInputStream in = null; try { // Extract clear text. // Dash-encoding is removed by ArmoredInputStream. in = new ArmoredInputStream(new FileInputStream(signedTextFile)); ByteArrayOutputStream temp = new ByteArrayOutputStream(in.available()); while (true) { int c = in.read(); if (c == -1) throw new IOException("Unexpected end of file"); if (!in.isClearText()) break; temp.write(c); } byte clearText[] = temp.toByteArray(); result.clearText = clearText; // Extract signature from clear-signed text PGPObjectFactory pgpFact = new PGPObjectFactory(in); PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject(); PGPSignature sig = p3.get(0); // Decode public key PGPPublicKey publicKey = pubKeyRing.getPublicKey(sig.getKeyID()); // Verify signature Security.addProvider(new BouncyCastleProvider()); sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), publicKey); // RFC 4880, section 7: http://tools.ietf.org/html/rfc4880#section-7 // The signature must be validated using clear text: // - without trailing white spaces on every line // - using CR LF line endings, no matter what the original line ending is // - without the latest line ending BufferedReader textIn = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(clearText))); while (true) { // remove trailing whitespace and line endings String line = StringUtils.rtrim(textIn.readLine()); sig.update(line.getBytes()); if (!textIn.ready()) // skip latest line ending break; // always use CR LF sig.update((byte) '\r'); sig.update((byte) '\n'); } // Prepare the result result.verified = sig.verify(); } catch (Exception e) { result.error = e; } finally { if (in != null) try { in.close(); } catch (IOException e) { // ignored } } return result; }
From source file:org.rhq.enterprise.server.plugins.rhnhosted.certificate.PublicKeyRing.java
License:Open Source License
/** * Turn the ASCII armored signature <code>asciiSig</code> into a * <code>PGPSignature</code> * @param asciiSig an ASCII armored signature * @return the signature/*from www.jav a2 s .c o m*/ */ private static PGPSignature decodeSignature(String asciiSig) { PGPSignature result; ByteArrayInputStream bais = new ByteArrayInputStream(asciiSig.getBytes()); try { ArmoredInputStream ais = new ArmoredInputStream(bais); try { InputStream in = PGPUtil.getDecoderStream(ais); try { result = ((PGPSignatureList) new PGPObjectFactory(in).nextObject()).get(0); } finally { in.close(); } } finally { ais.close(); } } catch (IOException e) { // This is so unlikely that we convert it to a runtime exception throw (IllegalArgumentException) new IllegalArgumentException( "The string asciiSig is not a valid signature").initCause(e); } return result; }