Example usage for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider

List of usage examples for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider

Introduction

In this page you can find the example usage for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider.

Prototype

public BouncyCastleProvider() 

Source Link

Document

Construct a new provider.

Usage

From source file:ca.trustpoint.m2m.M2mCertPathTest.java

License:Apache License

@BeforeClass
public static void initializeTests() throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    KeyAlgorithmDefinition caAlgorithm = new KeyAlgorithmDefinition();
    caAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA512_SECP521R1);

    EntityName issuer = new EntityName();
    issuer.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "blueline"));

    Date validFrom = new Date((new BigInteger(Hex.decode("5797C511"))).longValue() * 1000);
    int validDuration = (new BigInteger(Hex.decode("01E13380"))).intValue();

    EntityName subject = new EntityName();
    subject.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "blueline"));

    KeyAlgorithmDefinition pkAlgorithm = new KeyAlgorithmDefinition();
    pkAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA512_SECP521R1);

    X962Parameters keyParams = new X962Parameters(SECObjectIdentifiers.secp521r1);
    AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey,
            keyParams.toASN1Primitive());
    SubjectPublicKeyInfo publicKeyInfo = new SubjectPublicKeyInfo(algId,
            Hex.decode("040078EF059D605AB85B6A25A6EF31A1A73A632D3CB04DC606A8CA0B5823966168CFAF6131D8D9B5"
                    + "3F6BDF6B62946EC4B41D618FA3FF7F8BBFACBFD4F64FE3C33DA9D200A47AE528DC50B6F3876D7F5B"
                    + "A3C082D9927751E1A8C4F934D90942B35C57DFE311B2663E8D0187AD4EDE31BF9CD2AD8317107360"
                    + "522FDB6975AB2CD66DC029981F"));
    PublicKey publicKey = BouncyCastleProvider.getPublicKey(publicKeyInfo);

    byte[] signature = Hex
            .decode("3081880242014F15CAF8EF38626B2C7CFA85B9544E028668290CADB45F62E2153EAAF5A9D51AF5BF0D02"
                    + "F2C057D3856B5CBFB3529C25B8481405924039FA612D422AE9A1A85591024201868D3DFE5FC2BEDD2F74"
                    + "68B0B17ED2708E76CD0D37C44F4D0BB88693752046FCFC56D9818B32533B8992923C2C81499400AC44FB"
                    + "BECD6324D8AE1DD41EC73A0B2A");

    rootCertificate.setSerialNumber(new byte[] { 0x02 });
    rootCertificate.setCaKeyDefinition(caAlgorithm);
    rootCertificate.setIssuer(issuer);/*  w w w .  j  av  a  2s . c  om*/
    rootCertificate.setValidFrom(validFrom);
    rootCertificate.setValidDuration(validDuration);
    rootCertificate.setSubject(subject);
    rootCertificate.setPublicKeyDefinition(pkAlgorithm);
    rootCertificate.setPublicKey(publicKey);
    rootCertificate.setCaCalcValue(signature);

    caAlgorithm = new KeyAlgorithmDefinition();
    caAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA512_SECP521R1);

    issuer = new EntityName();
    issuer.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "blueline"));

    validFrom = new Date((new BigInteger(Hex.decode("57990E5F"))).longValue() * 1000);
    validDuration = (new BigInteger(Hex.decode("03C26700"))).intValue();

    subject = new EntityName();
    subject.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "MyIssuer"));

    pkAlgorithm = new KeyAlgorithmDefinition();
    pkAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA256_SECP256R1);

    keyParams = new X962Parameters(SECObjectIdentifiers.secp256r1);
    algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, keyParams.toASN1Primitive());
    publicKeyInfo = new SubjectPublicKeyInfo(algId,
            Hex.decode("0461591E779EE482541CF63EF2A0709D3D04CEBE1F621D4764EFECC4FF374864305E3742DAB2690E"
                    + "889B84906A7D2EAB444B9E03B546393BFCF9B2B3B87658C6FA"));
    publicKey = BouncyCastleProvider.getPublicKey(publicKeyInfo);

    signature = Hex
            .decode("3081870242016A8F50899193BD85FF36965129F86F64290B64FAD40E755CA367D31B3484F2A5552DDAB0"
                    + "5B1246304CFC4164E29950D56DEA04BB4D9A3D489E07106D1D3F34669D0241631ED08CD7EEAFE6114189"
                    + "53C64F1A6097B45D1ABB5FB9390A3CEAEDAB3C47FF3E7A1A754E1E0D53B2C2FEE90EB14EBDA0B4F15260"
                    + "C375FFC1868A7569B505FF08");

    issuerCertificate.setSerialNumber(new byte[] { 0x65 });
    issuerCertificate.setCaKeyDefinition(caAlgorithm);
    issuerCertificate.setIssuer(issuer);
    issuerCertificate.setValidFrom(validFrom);
    issuerCertificate.setValidDuration(validDuration);
    issuerCertificate.setSubject(subject);
    issuerCertificate.setPublicKeyDefinition(pkAlgorithm);
    issuerCertificate.setPublicKey(publicKey);
    issuerCertificate.setCaCalcValue(signature);

    caAlgorithm = new KeyAlgorithmDefinition();
    caAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA256_SECP256R1);

    issuer = new EntityName();
    issuer.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "MyIssuer"));

    validFrom = new Date((new BigInteger(Hex.decode("57A26BCC"))).longValue() * 1000);
    validDuration = (new BigInteger(Hex.decode("03C26700"))).intValue();

    subject = new EntityName();
    subject.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "MySigner"));

    keyParams = new X962Parameters(SECObjectIdentifiers.secp256r1);
    algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, keyParams.toASN1Primitive());
    publicKeyInfo = new SubjectPublicKeyInfo(algId,
            Hex.decode("0463C779CFF44EB3C97D7CDF9AB3AD9A6ED0DCB6F3F1A3155DF674109A3AAD0A757FCAF2F01E53CD"
                    + "ED25707ADC38C2271E90BB554DB4ED47B65B25BB478E9E3BF8"));
    publicKey = BouncyCastleProvider.getPublicKey(publicKeyInfo);

    signature = Hex
            .decode("3045022100CBD969EEEB637A03D60B3271BD7320E7A3DDA1B1EF014E641F6C32BF897EEAC6022030FF7F"
                    + "FD3A59C9B16F2F335716B47402A3CFF3EE667767A89017D218203CD66E");

    signerCertificate.setSerialNumber(new byte[] { 0x68 });
    signerCertificate.setCaKeyDefinition(caAlgorithm);
    signerCertificate.setIssuer(issuer);
    signerCertificate.setValidFrom(validFrom);
    signerCertificate.setValidDuration(validDuration);
    signerCertificate.setSubject(subject);
    signerCertificate.setPublicKey(publicKey);
    signerCertificate.setCaCalcValue(signature);
}

From source file:ca.trustpoint.m2m.M2mCertPathValidatorTest.java

License:Apache License

@BeforeClass
public static void initializeTests() throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    KeyAlgorithmDefinition caAlgorithm = new KeyAlgorithmDefinition();
    caAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA512_SECP521R1);

    EntityName issuer = new EntityName();
    issuer.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "blueline"));

    Date validFrom = new Date((new BigInteger(Hex.decode("5797C511"))).longValue() * 1000);
    int validDuration = (new BigInteger(Hex.decode("01E13380"))).intValue();

    EntityName subject = new EntityName();
    subject.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "blueline"));

    KeyAlgorithmDefinition pkAlgorithm = new KeyAlgorithmDefinition();
    pkAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA512_SECP521R1);

    X962Parameters keyParams = new X962Parameters(SECObjectIdentifiers.secp521r1);
    AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey,
            keyParams.toASN1Primitive());
    SubjectPublicKeyInfo publicKeyInfo = new SubjectPublicKeyInfo(algId,
            Hex.decode("040078EF059D605AB85B6A25A6EF31A1A73A632D3CB04DC606A8CA0B5823966168CFAF6131D8D9B5"
                    + "3F6BDF6B62946EC4B41D618FA3FF7F8BBFACBFD4F64FE3C33DA9D200A47AE528DC50B6F3876D7F5B"
                    + "A3C082D9927751E1A8C4F934D90942B35C57DFE311B2663E8D0187AD4EDE31BF9CD2AD8317107360"
                    + "522FDB6975AB2CD66DC029981F"));
    PublicKey publicKey = BouncyCastleProvider.getPublicKey(publicKeyInfo);

    byte[] signature = Hex
            .decode("3081880242014F15CAF8EF38626B2C7CFA85B9544E028668290CADB45F62E2153EAAF5A9D51AF5BF0D02"
                    + "F2C057D3856B5CBFB3529C25B8481405924039FA612D422AE9A1A85591024201868D3DFE5FC2BEDD2F74"
                    + "68B0B17ED2708E76CD0D37C44F4D0BB88693752046FCFC56D9818B32533B8992923C2C81499400AC44FB"
                    + "BECD6324D8AE1DD41EC73A0B2A");

    rootCertificate.setSerialNumber(new byte[] { 0x02 });
    rootCertificate.setCaKeyDefinition(caAlgorithm);
    rootCertificate.setIssuer(issuer);/*from   www. j  a  v a2 s.  c  om*/
    rootCertificate.setValidFrom(validFrom);
    rootCertificate.setValidDuration(validDuration);
    rootCertificate.setSubject(subject);
    rootCertificate.setPublicKeyDefinition(pkAlgorithm);
    rootCertificate.setPublicKey(publicKey);
    rootCertificate.setCaCalcValue(signature);

    caAlgorithm = new KeyAlgorithmDefinition();
    caAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA512_SECP521R1);

    issuer = new EntityName();
    issuer.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "blueline"));

    validFrom = new Date((new BigInteger(Hex.decode("57990E5F"))).longValue() * 1000);
    validDuration = (new BigInteger(Hex.decode("03C26700"))).intValue();

    subject = new EntityName();
    subject.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "MyIssuer"));

    pkAlgorithm = new KeyAlgorithmDefinition();
    pkAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA256_SECP256R1);

    keyParams = new X962Parameters(SECObjectIdentifiers.secp256r1);
    algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, keyParams.toASN1Primitive());
    publicKeyInfo = new SubjectPublicKeyInfo(algId,
            Hex.decode("0461591E779EE482541CF63EF2A0709D3D04CEBE1F621D4764EFECC4FF374864305E3742DAB2690E"
                    + "889B84906A7D2EAB444B9E03B546393BFCF9B2B3B87658C6FA"));
    publicKey = BouncyCastleProvider.getPublicKey(publicKeyInfo);

    signature = Hex
            .decode("3081870242016A8F50899193BD85FF36965129F86F64290B64FAD40E755CA367D31B3484F2A5552DDAB0"
                    + "5B1246304CFC4164E29950D56DEA04BB4D9A3D489E07106D1D3F34669D0241631ED08CD7EEAFE6114189"
                    + "53C64F1A6097B45D1ABB5FB9390A3CEAEDAB3C47FF3E7A1A754E1E0D53B2C2FEE90EB14EBDA0B4F15260"
                    + "C375FFC1868A7569B505FF08");

    issuerCertificate.setSerialNumber(new byte[] { 0x65 });
    issuerCertificate.setCaKeyDefinition(caAlgorithm);
    issuerCertificate.setIssuer(issuer);
    issuerCertificate.setValidFrom(validFrom);
    issuerCertificate.setValidDuration(validDuration);
    issuerCertificate.setSubject(subject);
    issuerCertificate.setPublicKeyDefinition(pkAlgorithm);
    issuerCertificate.setPublicKey(publicKey);
    issuerCertificate.setCaCalcValue(signature);

    caAlgorithm = new KeyAlgorithmDefinition();
    caAlgorithm.setAlgorithm(M2mSignatureAlgorithmOids.ECDSA_SHA256_SECP256R1);

    issuer = new EntityName();
    issuer.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "MyIssuer"));

    validFrom = new Date((new BigInteger(Hex.decode("57A26BCC"))).longValue() * 1000);
    validDuration = (new BigInteger(Hex.decode("03C26700"))).intValue();

    subject = new EntityName();
    subject.addAttribute(new EntityNameAttribute(EntityNameAttributeId.CommonName, "MySigner"));

    keyParams = new X962Parameters(SECObjectIdentifiers.secp256r1);
    algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, keyParams.toASN1Primitive());
    publicKeyInfo = new SubjectPublicKeyInfo(algId,
            Hex.decode("0463C779CFF44EB3C97D7CDF9AB3AD9A6ED0DCB6F3F1A3155DF674109A3AAD0A757FCAF2F01E53CD"
                    + "ED25707ADC38C2271E90BB554DB4ED47B65B25BB478E9E3BF8"));
    publicKey = BouncyCastleProvider.getPublicKey(publicKeyInfo);

    signature = Hex
            .decode("3045022100CBD969EEEB637A03D60B3271BD7320E7A3DDA1B1EF014E641F6C32BF897EEAC6022030FF7F"
                    + "FD3A59C9B16F2F335716B47402A3CFF3EE667767A89017D218203CD66E");

    signerCertificate.setSerialNumber(new byte[] { 0x68 });
    signerCertificate.setCaKeyDefinition(caAlgorithm);
    signerCertificate.setIssuer(issuer);
    signerCertificate.setValidFrom(validFrom);
    signerCertificate.setValidDuration(validDuration);
    signerCertificate.setSubject(subject);
    signerCertificate.setPublicKey(publicKey);
    signerCertificate.setCaCalcValue(signature);

    caAlgorithm = new KeyAlgorithmDefinition();
    caAlgorithm.setAlgorithm(NfcSignatureAlgorithmOids.RSA_SHA256_RSA);

    issuer = new EntityName();
    issuer.addAttribute(new EntityNameAttribute(EntityNameAttributeId.Country, "US"));
    issuer.addAttribute(new EntityNameAttribute(EntityNameAttributeId.Organization, "NFC Forum Test RSA CA"));

    validFrom = new Date((new BigInteger(Hex.decode("5418AEDA"))).longValue() * 1000);
    validDuration = (new BigInteger(Hex.decode("05A497A0"))).intValue();

    subject = new EntityName();
    subject.addAttribute(new EntityNameAttribute(EntityNameAttributeId.Country, "US"));
    subject.addAttribute(new EntityNameAttribute(EntityNameAttributeId.StateOrProvince, "UT"));
    subject.addAttribute(
            new EntityNameAttribute(EntityNameAttributeId.Organization, "NFC Forum RSA Test M2M EE 1"));

    algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERSet(DERNull.INSTANCE));
    publicKeyInfo = new SubjectPublicKeyInfo(algId,
            Hex.decode("3082010A0282010100E93D3E174F587784C53A4B01C05D2F73CFEC22CCCD1FBCF1B1C5B49A118CE6"
                    + "B323640F28DFE1D5882FAFAEFDE9BB9A20347C44347D69F431AEEF5788D2EAE2131E49E3B9FD6A94"
                    + "BCE34AFCF88C603BAA8EADBC5E6BC558D1459064F9FF6F6157C472739E90B9A312A5DE67176A03FB"
                    + "A77981A6F89F1CA9C0733C67797ED6DB766FC99ABEE0C8D3641D69A9C6FD1E6F33CEE29344374146"
                    + "E9A8E3CB141163798FDD9217CF58D93E836EA735D5A7F642F203DE097C1623EB855AB72D81330014"
                    + "26163E671C747DB54629C0EAF37342CF16923FCAD53B5CAF2CECCB3876853CE003C3753FA72C1F39"
                    + "9A9B5FA7A232792FBE38C995B55B9D105F3C0AC536D841068B0203010001"));
    publicKey = BouncyCastleProvider.getPublicKey(publicKeyInfo);

    signature = Hex.decode("B6A683AF9B20715210CA38D0DAA647F48270DBF67EDF3E043BFBD02265A035540D50540F877179D6"
            + "1349B9F872AFA41646835F8353CF90049551941B89D79B3FC61B1AADE00E8BA474A4342BDAACA5CD"
            + "28AFC9DD7C505127857224D0278A6E5C9AC4344B3FA36B7FD6E5E54D4D92FBCD717AD4D2FE73C2E6"
            + "2219D6A097970BB4F956AAA948501E4083137992EEBCFA41308687F36DBE8CEC54579C76DE4DE54A"
            + "1D6E007AD22F83BEE86CDEF39A37B4BCCD71D5B0A364C258B94D0B953DC3DA5637874157C3AD7CEC"
            + "3367F3075FA1D8939B27F4062DFBE436F871AECDC6D2A3098793A1212ED192F6B128648FFE764C4D"
            + "3176D64E6594DB295400465395781A37");

    rsaTestCertificate.setSerialNumber(Hex.decode("034F3F184941B948A47F3D59EE625F09"));
    rsaTestCertificate.setCaKeyDefinition(caAlgorithm);
    rsaTestCertificate.setIssuer(issuer);
    rsaTestCertificate.setValidFrom(validFrom);
    rsaTestCertificate.setValidDuration(validDuration);
    rsaTestCertificate.setSubject(subject);
    rsaTestCertificate.setPublicKey(publicKey);
    rsaTestCertificate.setKeyUsage(new KeyUsage((byte) 0xA0));
    rsaTestCertificate.setExtendedKeyUsage("2.16.840.1.114513.29.37");
    rsaTestCertificate.setCrlDistributionPointUri(new URI("http://crl.nfctest.example.com/nfctestrsaca.crl"));
    rsaTestCertificate.setCaCalcValue(signature);

    CertificateFactory x509Factory = CertificateFactory.getInstance("X.509",
            BouncyCastleProvider.PROVIDER_NAME);

    FileInputStream fileInput = new FileInputStream("testdata/digicert_batch_2/NFC Forum Test RSA CA.cer");
    x509Ca = (X509Certificate) x509Factory.generateCertificate(fileInput);
    fileInput.close();
}

From source file:ca.trustpoint.m2m.M2mProvider.java

License:Apache License

/**
 * To add the provider at runtime use://from  w  w  w .  j  a v  a 2  s.  c o m
 * <pre>
 * import java.security.Security;
 * import com.trustpoint.m2m.M2MProvider;
 *
 * Security.addProvider(new M2MProvider());
 * </pre>
 * The provider can also be configured as part of your environment via static registration by
 * adding an entry to the java.security properties file (found in
 * $JAVA_HOME/jre/lib/security/java.security, where $JAVA_HOME is the location of your JDK/JRE
 * distribution). You'll find detailed instructions in the file but basically it comes down to
 * adding a line:
 * <pre>
 * <code>
 *    security.provider.&lt;n&gt;=com.trustpoint.m2m.M2MProvider
 * </code>
 * </pre>
 * Where &lt;n&gt; is the preference you want the provider at (1 being the most preferred).
 * <p>Note: JCE algorithm names should be upper-case only so the case insensitive test for
 * getInstance() works.
 */
public M2mProvider() {
    super(PROVIDER_NAME, VERSION, "M2M Certificate Handling Provider v1.0.");

    put("CertPathValidator.M2M", M2mCertPathValidator.class.getName());
    put("CertificateFactory.M2M", M2mCertificateFactory.class.getName());

    // Since this library is built on top of Bouncy Castle, make sure the Bouncy Castle provider is
    // registered.
    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.addProvider(new BouncyCastleProvider());
    }
}

From source file:ca.trustpoint.m2m.util.KeyConversionUtils.java

License:Apache License

/**
 * Constructs a PublicKey object from raw EC public key data.
 *
 * @param rawKey Raw EC public key data.
 * @return A PublicKey object constructed from the raw EC public key data.
 *
 * @throws InvalidKeyException if key type or length is unrecognized.
 * @throws IOException if raw data reading error.
 *//*from  w  w  w.  j  av  a 2  s.  com*/
public static PublicKey convertRawBytestoEcPublicKey(byte[] rawKey)
        throws IllegalArgumentException, InvalidKeyException, IOException {
    if (rawKey == null) {
        throw new IllegalArgumentException("rawKey cannot be null.");
    }

    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.addProvider(new BouncyCastleProvider());
    }

    boolean isCompressed = isCompressedEcPoint(rawKey);

    X962Parameters params = null;
    int keyCompressedLength = 0;

    // Obtain the length of the compressed public key
    if (isCompressed) {
        keyCompressedLength = rawKey.length;
    } else {
        keyCompressedLength = (rawKey.length - 1) / 2 + 1;
    }

    switch (keyCompressedLength) {
    case 25: // compressed 192 curve
        params = new X962Parameters(X9ObjectIdentifiers.prime192v1);
        break;
    case 29: // compressed 224 curve
        params = new X962Parameters(SECObjectIdentifiers.secp224r1);
        break;
    case 31: // compressed 233 curve
        params = new X962Parameters(SECObjectIdentifiers.sect233r1);
        break;
    case 33: // compressed 256 curve
        params = new X962Parameters(X9ObjectIdentifiers.prime256v1);
        break;
    case 49: // compressed 384 curve
        params = new X962Parameters(SECObjectIdentifiers.secp384r1);
        break;
    case 67: // compressed 521 curve
        params = new X962Parameters(SECObjectIdentifiers.secp521r1);
        break;
    default:
        throw new InvalidKeyException("unrecognized public key length: (" + keyCompressedLength + ")");
    }

    AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey,
            params.toASN1Primitive());
    SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(algId, rawKey);

    BCECPublicKey key = (BCECPublicKey) BouncyCastleProvider.getPublicKey(info);
    if (isCompressed) {
        key.setPointFormat("COMPRESSED");
    }

    return key;
}

From source file:ca.trustpoint.m2m.VerificationChainTimingTest.java

License:Apache License

/**
 * Stress test for certificate chain verification.
 *
 * @throws Exception//from  ww w  . ja v a 2s.  c om
 */
@Test
public void stressTest() throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    M2mCertificateFactory factory;
    InputStream inStream;

    M2mCertPathValidator validator = new M2mCertPathValidator();

    byte[] signature = Base64
            .decode("MEUCIQC73TC9nqt18hVxE28TkFRNd+yXXWI3/BodfiiPT8RPHAIgPViFgbgSWtRciiilQEpGSu6YP"
                    + "ENgpDGAGjqyWgTYUJE=");

    byte[] signerCert = Base64
            .decode("dIIBCKB4gQEJggUrgToBDaQKhghibHVlbGluZYUEV5fFEYYEAeEzgKcKhghDIChQMjU2KYgFK4E6A"
                    + "QmKQQTYEDiOt19zyoGull4uJ+V1xcFQT4SAu30fo+ALWO6SQbNI81UkajsV+vBB7HVZwDIr0Bw8Vz"
                    + "PCYkzhXCF8T/pWgYGLMIGIAkIBvuaG0c3KqZsnRE408FY3RGYkc4vbte2ZovSUyhe72e7UjsHaImo"
                    + "M8WWPzDDe/rI558l5QYM2kPh/nRhlROLQiwICQgF1iObyUR5maod8GiJz0GHxgk9U16KuOqKLYyzI"
                    + "9+rl9UVAne2RSqmSKMwWX1Rcg1pTVZKNKAFvRe5z0qNlTVvr5g==");

    byte[] rootCert = Base64
            .decode("dIIBTqCBvYEBAoIFK4E6AQ2kCoYIYmx1ZWxpbmWFBFeXxRGGBAHhM4CnCoYIYmx1ZWxpbmWIBSuBO"
                    + "gENioGFBAB47wWdYFq4W2olpu8xoac6Yy08sE3GBqjKC1gjlmFoz69hMdjZtT9r32tilG7EtB1hj6"
                    + "P/f4u/rL/U9k/jwz2p0gCkeuUo3FC284dtf1ujwILZkndR4ajE+TTZCUKzXFff4xGyZj6NAYetTt4"
                    + "xv5zSrYMXEHNgUi/baXWrLNZtwCmYH4GBizCBiAJCAU8VyvjvOGJrLHz6hblUTgKGaCkMrbRfYuIV"
                    + "Pqr1qdUa9b8NAvLAV9OFa1y/s1KcJbhIFAWSQDn6YS1CKumhqFWRAkIBho09/l/Cvt0vdGiwsX7Sc"
                    + "I52zQ03xE9NC7iGk3UgRvz8VtmBizJTO4mSkjwsgUmUAKxE+77NYyTYrh3UHsc6Cyo=");

    // Construct a list of M2M certificate
    // NOTE: engineGenerateCertificate() was tested in testEngineGenerateCertificateInputStream(),
    //       so it's okay to use it for generating certificate from certificate raw data here.
    List<M2mCertificate> certs = new ArrayList<M2mCertificate>();
    M2mCertificate cert;

    factory = new M2mCertificateFactory();

    inStream = new ByteArrayInputStream(signerCert);
    cert = (M2mCertificate) factory.engineGenerateCertificate(inStream);
    certs.add(cert);

    inStream = new ByteArrayInputStream(rootCert);
    cert = (M2mCertificate) factory.engineGenerateCertificate(inStream);
    certs.add(cert);

    M2mCertPath path = new M2mCertPath(certs);

    List<M2mTrustAnchor> m2mAnchors = new ArrayList<M2mTrustAnchor>();
    m2mAnchors.add(new M2mTrustAnchor(cert));

    Calendar validityDate = new GregorianCalendar(2016, 7, 5);
    M2mCertPathParameters params = new M2mCertPathParameters(null, validityDate.getTime(), true);
    params = new M2mCertPathParameters(m2mAnchors, validityDate.getTime(), false);

    M2mCertPathValidatorResult result;
    result = (M2mCertPathValidatorResult) validator.engineValidate(path, params);

    for (int i = 0; i < ITERATIONS; i++) {
        result = (M2mCertPathValidatorResult) validator.engineValidate(path, params);
    }
}

From source file:ca.trustpoint.m2m.VerificationSingleTimingTest.java

License:Apache License

/**
 * Simple stress test running a tight loop on M2M cert verify.
 *
 * @throws Exception/*w  w w. j a va2s.  c om*/
 */
@Test
public void stressTest() throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    M2mCertificateFactory factory;
    InputStream inStream;
    M2mCertificate cert;

    byte[] certData = Base64
            .decode("dIHKoH+BAWaCBSuBOgEJpBGGD1NlbGYgU2lnbmVkICMxMIUEVHyKLIYEA9TcAKcRhg9TZWxmIFNpZ2"
                    + "5lZCAjMTCKQQQ4mwknUz3zC/MQZF6hPNfsyz/0d/0DhGbeTJMcsCBPlE1UDggGr0XDFDltw0uqy1oF"
                    + "H9t/gQxdZ32JOVNiSQRbgUcwRQIhAK/Bxm6rOIbb5b1S7gF2F+b6K10KoS5IxxdJBAU/oVi+AiAn0z"
                    + "fK7ST5j9eL3t9IGl/sbmmqyWqAcefJ3hdrPX5IPA==");

    inStream = new ByteArrayInputStream(certData);
    factory = new M2mCertificateFactory();
    cert = (M2mCertificate) factory.engineGenerateCertificate(inStream);

    PublicKey pubKey = cert.getPublicKey();

    // Tight loop of verification
    for (int i = 0; i < ITERATIONS; i++) {
        cert.verify(pubKey);
    }
}

From source file:ca.trustpoint.m2m.VerificationTest.java

License:Apache License

@Before
public void setup() throws Exception {
    Security.addProvider(new BouncyCastleProvider());
}

From source file:ca.ualberta.physics.cssdp.file.remote.protocol.SftpConnection.java

License:Apache License

public SftpConnection(Host hostEntry) {
    super(hostEntry);
    // use bouncy castle
    Security.addProvider(new BouncyCastleProvider());
}

From source file:cc.arduino.packages.security.ClearSignedVerifier.java

License:Open Source License

/**
 * Verify a PGP clearText-signature./*from  www  .  j  a v  a  2  s.c  om*/
 *
 * @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:cc.telepath.phage.util.Crypto.java

License:GNU General Public License

public Crypto() {
    Security.addProvider(new BouncyCastleProvider());
}