Example usage for org.bouncycastle.cms KeyTransRecipientId getSerialNumber

List of usage examples for org.bouncycastle.cms KeyTransRecipientId getSerialNumber

Introduction

In this page you can find the example usage for org.bouncycastle.cms KeyTransRecipientId getSerialNumber.

Prototype

public BigInteger getSerialNumber() 

Source Link

Usage

From source file:be.e_contract.mycarenet.etee.Unsealer.java

License:Open Source License

@SuppressWarnings("unchecked")
private byte[] decrypt(byte[] encryptedData) throws CMSException, IOException {
    CMSEnvelopedDataParser cmsEnvelopedDataParser = new CMSEnvelopedDataParser(encryptedData);
    LOG.debug("content encryption algo: "
            + cmsEnvelopedDataParser.getContentEncryptionAlgorithm().getAlgorithm().getId());

    RecipientInformationStore recipientInformationStore = cmsEnvelopedDataParser.getRecipientInfos();
    RecipientId recipientId = new JceKeyTransRecipientId(this.decryptionCertificate);
    Collection<RecipientInformation> recipients = recipientInformationStore.getRecipients(recipientId);
    LOG.debug("number of recipients for given decryption cert: " + recipients.size());
    if (0 == recipients.size()) {
        recipients = recipientInformationStore.getRecipients();
        LOG.debug("number of all recipients: " + recipients.size());
        Iterator<RecipientInformation> recipientsIterator = recipients.iterator();
        while (recipientsIterator.hasNext()) {
            RecipientInformation recipientInformation = recipientsIterator.next();
            RecipientId actualRecipientId = recipientInformation.getRID();
            LOG.debug("actual recipient id type: " + actualRecipientId.getClass().getName());
            if (actualRecipientId instanceof KeyTransRecipientId) {
                KeyTransRecipientId actualKeyTransRecipientId = (KeyTransRecipientId) actualRecipientId;
                LOG.debug("actual recipient issuer: " + actualKeyTransRecipientId.getIssuer());
                LOG.debug("actual recipient serial number: " + actualKeyTransRecipientId.getSerialNumber());
            }//w  ww  .  j av  a  2s.  c o m
        }
        throw new SecurityException("message does not seem to be addressed to you");
    }
    Iterator<RecipientInformation> recipientsIterator = recipients.iterator();
    RecipientInformation recipientInformation = recipientsIterator.next();

    AsymmetricKeyParameter privKeyParams = PrivateKeyFactory.createKey(this.decryptionPrivateKey.getEncoded());
    BcRSAKeyTransEnvelopedRecipient recipient = new BcRSAKeyTransEnvelopedRecipient(privKeyParams);
    byte[] decryptedContent = recipientInformation.getContent(recipient);
    return decryptedContent;
}

From source file:mitm.common.security.cms.CMSEnvelopedInspectorImplTest.java

License:Open Source License

public void testEnveloped(CMSEnvelopedDataAdapter cmsEnveloped)
        throws CryptoMessageSyntaxException, MessagingException, IOException {
    CMSEnvelopedInspector inspector = new CMSEnvelopedInspectorImpl(cmsEnveloped, keyStoreKeyProvider,
            securityFactory.getNonSensitiveProvider(), securityFactory.getSensitiveProvider());

    List<RecipientInfo> recipients = inspector.getRecipients();

    assertEquals(1, recipients.size());/*w  w  w .j ava  2 s  .  c  o m*/

    RecipientInfo recipientInfo = recipients.get(0);

    logger.info(
            "Encryption Algorithm: " + SMIMEEncryptionAlgorithm.fromOID(inspector.getEncryptionAlgorithmOID()));
    logger.info(recipientInfo.toString());

    KeyIdentifier recipientId = recipientInfo.getRecipientId();

    assertTrue(recipientId instanceof KeyTransRecipientId);

    KeyTransRecipientId keyTransRecipientId = (KeyTransRecipientId) recipientId;

    assertEquals(new BigInteger("115FCD741088707366E9727452C9770", 16), keyTransRecipientId.getSerialNumber());
    assertEquals(new X500Principal("EMAILADDRESS=ca@example.com, CN=MITM Test CA, L=Amsterdam, ST=NH, C=NL"),
            keyTransRecipientId.getIssuer());

    byte[] decryptedContent = inspector.getContent();

    MimeMessage decryptedMessage = MailUtils.byteArrayToMessage(decryptedContent);

    File file = new File(tempDir, "encrypted-validcertificate-decrypted.eml");

    MailUtils.writeMessage(decryptedMessage, file);

    decryptedMessage = MailUtils.loadMessage(file);

    assertTrue(decryptedMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(decryptedMessage));

    checkForEmbeddedHeaders(decryptedMessage);
}

From source file:mitm.common.security.cms.RecipientInfoImpl.java

License:Open Source License

private KeyTransRecipientId getKeyTransRecipientId(org.bouncycastle.cms.KeyTransRecipientId otherRecipientId)
        throws RecipientInfoException {
    try {/* w  w w.  j ava  2 s  . c  om*/
        KeyTransRecipientIdImpl recipientId = new KeyTransRecipientIdImpl(
                X500PrincipalUtils.fromX500Name(otherRecipientId.getIssuer()),
                otherRecipientId.getSerialNumber(), otherRecipientId.getSubjectKeyIdentifier());

        return recipientId;
    } catch (IOException e) {
        throw new RecipientInfoException(e);
    }
}

From source file:org.sejda.sambox.pdmodel.encryption.PublicKeySecurityHandler.java

License:Apache License

private void appendCertInfo(StringBuilder extraInfo, KeyTransRecipientId ktRid, X509Certificate certificate,
        X509CertificateHolder materialCert) {
    BigInteger ridSerialNumber = ktRid.getSerialNumber();
    if (ridSerialNumber != null) {
        String certSerial = "unknown";
        BigInteger certSerialNumber = certificate.getSerialNumber();
        if (certSerialNumber != null) {
            certSerial = certSerialNumber.toString(16);
        }/*w w  w.  j  av  a 2 s . c o m*/
        extraInfo.append("serial-#: rid ");
        extraInfo.append(ridSerialNumber.toString(16));
        extraInfo.append(" vs. cert ");
        extraInfo.append(certSerial);
        extraInfo.append(" issuer: rid \'");
        extraInfo.append(ktRid.getIssuer());
        extraInfo.append("\' vs. cert \'");
        extraInfo.append(materialCert == null ? "null" : materialCert.getIssuer());
        extraInfo.append("\' ");
    }
}