org.bouncycastle.openpgp.PGPOnePassSignatureList Java Examples

The following examples show how to use org.bouncycastle.openpgp.PGPOnePassSignatureList. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PGPVerify.java    From peer-os with Apache License 2.0 5 votes vote down vote up
private static PGPOnePassSignature getOnePassSignature( PGPPublicKey publicKey, JcaPGPObjectFactory pgpFact )
        throws IOException, PGPException
{
    PGPOnePassSignatureList p1 = ( PGPOnePassSignatureList ) pgpFact.nextObject();

    PGPOnePassSignature onePassSignature = p1.get( 0 );

    onePassSignature.init( new JcaPGPContentVerifierBuilderProvider().setProvider( "BC" ), publicKey );

    return onePassSignature;
}
 
Example #2
Source File: PGPEncryptionUtil.java    From peer-os with Apache License 2.0 5 votes vote down vote up
/**
 * ***********************************************
 */
private static PGPLiteralData asLiteral( final InputStream clear ) throws IOException, PGPException
{
    final PGPObjectFactory plainFact = new PGPObjectFactory( clear, new JcaKeyFingerprintCalculator() );
    final Object message = plainFact.nextObject();
    if ( message instanceof PGPCompressedData )
    {
        final PGPCompressedData cData = ( PGPCompressedData ) message;
        final PGPObjectFactory pgpFact =
                new PGPObjectFactory( cData.getDataStream(), new JcaKeyFingerprintCalculator() );
        // Find the first PGPLiteralData object
        Object object = null;
        for ( int safety = 0; ( safety++ < 1000 ) && !( object instanceof PGPLiteralData );
              object = pgpFact.nextObject() )
        {
            //ignore
        }
        return ( PGPLiteralData ) object;
    }
    else if ( message instanceof PGPLiteralData )
    {
        return ( PGPLiteralData ) message;
    }
    else if ( message instanceof PGPOnePassSignatureList )
    {
        throw new PGPException( "encrypted message contains a signed message - not literal data." );
    }
    else
    {
        throw new PGPException(
                "message is not a simple encrypted file - type unknown: " + message.getClass().getName() );
    }
}
 
Example #3
Source File: ContentAndSignatures.java    From peer-os with Apache License 2.0 5 votes vote down vote up
public ContentAndSignatures( final byte[] decryptedContent, final PGPOnePassSignatureList onePassSignatureList,
                             final PGPSignatureList signatureList )
{

    this.decryptedContent = decryptedContent;
    this.onePassSignatureList = onePassSignatureList;
    this.signatureList = signatureList;
}
 
Example #4
Source File: GPGFileDecryptor.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Move to the next {@link InputStream} if available, otherwise set {@link #currentUnderlyingStream} to null to
 * indicate that there is no more data.
 * @throws IOException
 */
private void moveToNextInputStream() throws IOException {
  Object pgpfObject = this.pgpFact.nextObject();

  // no more data
  if (pgpfObject == null) {
    this.currentUnderlyingStream = null;
    return;
  }

  if (pgpfObject instanceof PGPCompressedData) {
    PGPCompressedData cData = (PGPCompressedData) pgpfObject;

    try {
      this.pgpFact = new JcaPGPObjectFactory(cData.getDataStream());
    } catch (PGPException e) {
      throw new IOException("Could not get the PGP data stream", e);
    }

    pgpfObject = this.pgpFact.nextObject();
  }

  if (pgpfObject instanceof PGPLiteralData) {
    this.currentUnderlyingStream = ((PGPLiteralData) pgpfObject).getInputStream();
  } else if (pgpfObject instanceof PGPOnePassSignatureList) {
    throw new IOException("encrypted message contains PGPOnePassSignatureList message - not literal data.");
  } else if (pgpfObject instanceof PGPSignatureList) {
    throw new IOException("encrypted message contains PGPSignatureList message - not literal data.");
  } else {
    throw new IOException("message is not a simple encrypted file - type unknown.");
  }
}
 
Example #5
Source File: ContentAndSignatures.java    From peer-os with Apache License 2.0 4 votes vote down vote up
public PGPOnePassSignatureList getOnePassSignatureList()
{
    return onePassSignatureList;
}
 
Example #6
Source File: BouncyCastleTest.java    From nomulus with Apache License 2.0 3 votes vote down vote up
@Test
public void testSignVerify_OnePass() throws Exception {
  // Load the keys.
  PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY);
  PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY);
  PGPPublicKey publicKey = publicKeyRing.getPublicKey();
  PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey());

  // Sign the data and write signature data to "signatureFile".
  PGPSignatureGenerator signer = new PGPSignatureGenerator(
      new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));
  signer.init(PGPSignature.BINARY_DOCUMENT, privateKey);
  addUserInfoToSignature(publicKey, signer);
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  signer.generateOnePassVersion(false).encode(output);
  signer.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
  signer.generate().encode(output);
  byte[] signatureFileData = output.toByteArray();
  logger.atInfo().log(".sig file data: %s", dumpHex(signatureFileData));

  // Load algorithm information and signature data from "signatureFileData".
  PGPSignature sig;
  PGPOnePassSignature onePass;
  try (ByteArrayInputStream input = new ByteArrayInputStream(signatureFileData)) {
    PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
    PGPOnePassSignatureList onePassList = (PGPOnePassSignatureList) pgpFact.nextObject();
    PGPSignatureList sigList = (PGPSignatureList) pgpFact.nextObject();
    assertThat(onePassList.size()).isEqualTo(1);
    assertThat(sigList.size()).isEqualTo(1);
    onePass = onePassList.get(0);
    sig = sigList.get(0);
  }

  // Use "onePass" and "sig" to verify "publicKey" signed the text.
  onePass.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
  onePass.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
  assertThat(onePass.verify(sig)).isTrue();

  // Verify that they DIDN'T sign the text "hello monster".
  onePass.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
  onePass.update("hello monster".getBytes(UTF_8));
  assertThat(onePass.verify(sig)).isFalse();
}