Example usage for org.bouncycastle.util.encoders Base64 encode

List of usage examples for org.bouncycastle.util.encoders Base64 encode

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders Base64 encode.

Prototype

public static byte[] encode(byte[] data) 

Source Link

Document

encode the input data producing a base 64 encoded byte array.

Usage

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test
public void testDeleteTrustedCert() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(crtFile);
    byte[] crtBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addTrustedCertificate("asdf", "changeit", "", new String(Base64.encode(crtBytes)),
            KeystoreEditor.PEM_TYPE, crtFile.toString());
    List<Map<String, Object>> truststore = keystoreEditor.getTruststore();
    Assert.assertThat(truststore.size(), Is.is(1));

    List<Map<String, Object>> keystore = keystoreEditor.getKeystore();
    Assert.assertThat(keystore.size(), Is.is(0));

    keystoreEditor.deleteTrustedCertificate("asdf");
    truststore = keystoreEditor.getTruststore();
    Assert.assertThat(truststore.size(), Is.is(0));

    keystore = keystoreEditor.getKeystore();
    Assert.assertThat(keystore.size(), Is.is(0));
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test
public void testEncryptedData() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(p7bFile);
    byte[] crtBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addTrustedCertificate("asdf", "changeit", "", new String(Base64.encode(crtBytes)),
            KeystoreEditor.PEM_TYPE, p7bFile.toString());
    List<Map<String, Object>> truststore = keystoreEditor.getTruststore();
    Assert.assertThat(truststore.size(), Is.is(1));
    Assert.assertThat((String) truststore.get(0).get("alias"), Is.is("asdf"));

    List<Map<String, Object>> keystore = keystoreEditor.getKeystore();
    Assert.assertThat(keystore.size(), Is.is(0));
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test(expected = KeystoreEditor.KeystoreEditorException.class)
public void testBadKeyPassword() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(jksFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("asdf", "blah", "changeit", new String(Base64.encode(keyBytes)), "",
            jksFile.toString());/*from w w w. j  a  v  a 2 s.c o  m*/
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test(expected = KeystoreEditor.KeystoreEditorException.class)
public void testBadKeyPasswordP12() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(pkcs12StoreFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("asdf", "blah", "changeit", new String(Base64.encode(keyBytes)), "",
            pkcs12StoreFile.toString());
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test(expected = KeystoreEditor.KeystoreEditorException.class)
public void testBadStorePassword() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(jksFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("asdf", "changeit", "blah", new String(Base64.encode(keyBytes)), "",
            jksFile.toString());//w  w w.j  av  a  2s.  c o m
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test(expected = KeystoreEditor.KeystoreEditorException.class)
public void testBadStorePasswordP12() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(pkcs12StoreFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("asdf", "changeit", "blah", new String(Base64.encode(keyBytes)), "",
            pkcs12StoreFile.toString());
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test(expected = KeystoreEditor.KeystoreEditorException.class)
public void testNullAlias() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(pkcs12StoreFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey(null, "changeit", "blah", new String(Base64.encode(keyBytes)), "",
            pkcs12StoreFile.toString());
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test(expected = KeystoreEditor.KeystoreEditorException.class)
public void testBlankAlias() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(pkcs12StoreFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("", "changeit", "blah", new String(Base64.encode(keyBytes)), "",
            pkcs12StoreFile.toString());
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test(expected = KeystoreEditor.KeystoreEditorException.class)
public void testBadFile() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(badFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("", "changeit", "blah", new String(Base64.encode(keyBytes)), "",
            badFile.toString());/*from w w w  .  jav  a  2s. c  o  m*/
}

From source file:org.codice.ddf.security.saml.assertion.validator.impl.SamlAssertionValidatorImplTest.java

License:Open Source License

private Assertion createHolderOfKeyAssertion() throws Exception {
    Assertion assertion = new AssertionBuilder().buildObject();
    assertion.setID(UUID.randomUUID().toString());
    assertion.setIssueInstant(new DateTime());

    Issuer issuer = new IssuerBuilder().buildObject();
    issuer.setValue(ISSUER);/*from www .j  ava2s . c o  m*/
    assertion.setIssuer(issuer);

    NameID nameID = new NameIDBuilder().buildObject();
    nameID.setFormat("urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified");
    nameID.setNameQualifier("http://cxf.apache.org/sts");
    nameID.setValue("admin");

    X509SubjectName x509SubjectName = new X509SubjectNameBuilder().buildObject();
    x509SubjectName.setValue("EMAILADDRESS=localhost@example.org, CN=localhost, OU=Dev, O=DDF, ST=AZ, C=US");

    org.opensaml.xmlsec.signature.X509Certificate x509Certificate = new X509CertificateBuilder().buildObject();
    byte[] certBytes = certificate.getEncoded();
    String certString = new String(Base64.encode(certBytes));
    x509Certificate.setValue(certString);

    X509Data x509Data = new X509DataBuilder().buildObject();
    x509Data.getX509SubjectNames().add(x509SubjectName);
    x509Data.getX509Certificates().add(x509Certificate);

    KeyInfo keyInfo = new KeyInfoBuilder().buildObject();
    keyInfo.getX509Datas().add(x509Data);

    KeyInfoConfirmationDataType keyInfoConfirmationDataType = new KeyInfoConfirmationDataTypeBuilder()
            .buildObject();
    keyInfoConfirmationDataType.getKeyInfos().add(keyInfo);

    SubjectConfirmation subjectConfirmation = new SubjectConfirmationBuilder().buildObject();
    subjectConfirmation.setMethod("urn:oasis:names:tc:SAML:2.0:cm:holder-of-key");
    subjectConfirmation.setSubjectConfirmationData(keyInfoConfirmationDataType);

    Subject subject = new SubjectBuilder().buildObject();
    subject.setNameID(nameID);
    subject.getSubjectConfirmations().add(subjectConfirmation);
    assertion.setSubject(subject);

    Conditions conditions = new ConditionsBuilder().buildObject();
    conditions.setNotBefore(new DateTime().minusDays(3));
    conditions.setNotOnOrAfter(new DateTime().plusDays(3));
    assertion.setConditions(conditions);

    AuthnStatement authnStatement = new AuthnStatementBuilder().buildObject();
    authnStatement.setAuthnInstant(new DateTime());
    AuthnContext authnContext = new AuthnContextBuilder().buildObject();
    AuthnContextClassRef authnContextClassRef = new AuthnContextClassRefBuilder().buildObject();
    authnContextClassRef.setAuthnContextClassRef("urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified");
    authnContext.setAuthnContextClassRef(authnContextClassRef);
    authnStatement.setAuthnContext(authnContext);
    assertion.getAuthnStatements().add(authnStatement);

    AttributeStatement attributeStatement = new AttributeStatementBuilder().buildObject();
    Attribute attribute = new AttributeBuilder().buildObject();
    AttributeValueType attributeValue = new AttributeValueTypeImplBuilder().buildObject();
    attributeValue.setValue("admin");
    attribute.setName("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role");
    attribute.setNameFormat("urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
    attribute.getAttributeValues().add(attributeValue);
    attributeStatement.getAttributes().add(attribute);
    assertion.getAttributeStatements().add(attributeStatement);

    Signature signature = OpenSAMLUtil.buildSignature();
    signature.setCanonicalizationAlgorithm(SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
    signature.setSignatureAlgorithm(WSS4JConstants.RSA);

    BasicX509Credential signingCredential;

    signingCredential = new BasicX509Credential(certificate);
    signingCredential.setPrivateKey(privateKey);
    signature.setSigningCredential(signingCredential);

    X509KeyInfoGeneratorFactory x509KeyInfoGeneratorFactory = new X509KeyInfoGeneratorFactory();
    x509KeyInfoGeneratorFactory.setEmitEntityCertificate(true);

    KeyInfo signatureKeyInfo = x509KeyInfoGeneratorFactory.newInstance().generate(signingCredential);
    signature.setKeyInfo(signatureKeyInfo);

    assertion.setSignature(signature);

    return assertion;
}