Example usage for java.security Security addProvider

List of usage examples for java.security Security addProvider

Introduction

In this page you can find the example usage for java.security Security addProvider.

Prototype

public static int addProvider(Provider provider) 

Source Link

Document

Adds a provider to the next position available.

Usage

From source file:org.bitrepository.protocol.security.PermissionStore.java

/**
 * Public constructor, initializes the store. 
 */// w  w w . j ava  2  s  . c  o  m
public PermissionStore() {
    permissionMap = new HashMap<CertificateID, CertificatePermission>();
    Provider provider = new BouncyCastleProvider();
    Security.addProvider(provider);
}

From source file:test.unit.be.fedict.trust.OnlineOcspRepositoryTest.java

@Before
public void setUp() throws Exception {
    this.servletTester = new ServletTester();
    String pathSpec = "/test.ocsp";
    this.servletTester.addServlet(OcspResponderTestServlet.class, pathSpec);
    this.servletTester.start();

    String servletUrl = this.servletTester.createSocketConnector(true);
    this.ocspUri = new URI(servletUrl + pathSpec);

    this.testedInstance = new OnlineOcspRepository();

    OcspResponderTestServlet.reset();// w  w w.java2 s. com

    this.rootKeyPair = TrustTestUtils.generateKeyPair();
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    this.rootCertificate = TrustTestUtils.generateSelfSignedCertificate(this.rootKeyPair, "CN=TestRoot",
            notBefore, notAfter);

    KeyPair keyPair = TrustTestUtils.generateKeyPair();
    this.certificate = TrustTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", notBefore, notAfter,
            this.rootCertificate, this.rootKeyPair.getPrivate());

    // required for org.bouncycastle.ocsp.CertificateID
    Security.addProvider(new BouncyCastleProvider());
}

From source file:com.telefonica.euro_iaas.sdc.util.RSASignerImpl.java

/**
 * {@inheritDoc}/*  w  ww.j  a v  a2s .  co  m*/
 */
@Override
public String unsign(String message, File pemFile) {
    try {
        Security.addProvider(new BouncyCastleProvider());
        JCERSAPublicKey publicKey = readPublicKey(pemFile, "".toCharArray());

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        // decryption:
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(cipher.doFinal(org.bouncycastle.util.encoders.Base64.decode(message.getBytes("UTF-8"))));
        return baos.toString();
    } catch (IOException e) {
        throw new SdcRuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SdcRuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new SdcRuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new SdcRuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new SdcRuntimeException(e);
    } catch (BadPaddingException e) {
        throw new SdcRuntimeException(e);
    }
}

From source file:subscription.Subscription.java

public String notify(String message, String encryptionKey) {
    Security.addProvider(new BouncyCastleProvider());
    // System.out.println(message);
    byte[] key = Base64.decode(encryptionKey);
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    byte[] data = Base64.decode(message);
    String decryptedString = "";
    try {//from  w  ww. j  a  v a2s.c  o m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decrypted = cipher.doFinal(data);
        decryptedString = new String(decrypted);
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(decryptedString);
    return decryptedString;
}

From source file:io.milton.grizzly.GrizzlyServer.java

public boolean start(int httpPort, Integer sslPort) throws IOException {

    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    ConfigurableApplicationContext ctx = initSpringApplicationContext();
    if (ctx == null) {
        log.warn("Failed to initialise spring");
        return false;
    }/*from   w  w  w  .j  a  va 2 s.c o  m*/

    Object milton = ctx.getBean("milton.http.manager");
    if (milton instanceof HttpManager) {
        this.httpManager = (HttpManager) milton;
    } else if (milton instanceof HttpManagerBuilder) {
        HttpManagerBuilder builder = (HttpManagerBuilder) milton;
        ResourceFactory rf = builder.getMainResourceFactory();
        this.httpManager = builder.buildHttpManager();
    }

    if (parent.containsBean("milton.mail.server")) {
        log.info("init mailserver...");
        Object oMailServer = parent.getBean("milton.mail.server");
        if (oMailServer instanceof MailServer) {
            mailServer = (MailServer) oMailServer;
        } else if (oMailServer instanceof MailServerBuilder) {
            MailServerBuilder builder = (MailServerBuilder) oMailServer;
            mailServer = builder.build();
        } else {
            throw new RuntimeException("Unsupported type: " + oMailServer.getClass() + " expected "
                    + MailServer.class + " or " + MailServerBuilder.class);
        }
        log.info("starting mailserver");
        mailServer.start();
    }
    log.info("Finished init");

    String host = getPropertyOrDefault("host", null);

    int port = getPropertyOrDefaultInt("port", 8080);

    int secureHttpPort = getPropertyOrDefaultInt(MiltonSNIService.SYS_SECURE_PORT,
            MiltonSNIService.SECURE_PORT);

    if (host == null) {
        httpServer = HttpServer.createSimpleServer(null, port);
    } else {
        httpServer = HttpServer.createSimpleServer(null, host, port);
    }

    { // Start the Kademi SNI SSL service
        MiltonSNICertificateStore store = null; // TODO: allow injection
        MiltonSNICertificateManager sniCerManager = new MiltonSNICertificateManager(store);
        SSLEngineConfigurator sniConfig = sniCerManager.createEngineConfigurator();
        this.kademiSNIService = new MiltonSNIService(secureHttpPort, sniConfig);

        this.kademiSNIService.startOn(httpServer);
    }

    httpServer.getServerConfiguration().addHttpHandler(new HttpHandler() {
        @Override
        public void service(Request request, Response response) throws Exception {
            log.trace("service");
            GrizzlyMiltonRequest req = new GrizzlyMiltonRequest(request);
            GrizzlyMiltonResponse resp = new GrizzlyMiltonResponse(response);
            String p = req.getAbsolutePath();
            long tm = System.currentTimeMillis();
            httpManager.process(req, resp);
            tm = System.currentTimeMillis() - tm;
            // todo
        }
    }, "/");

    httpServer.start();

    running = true;
    return true;
}

From source file:test.unit.be.agiv.security.handler.WSSecurityHandlerTest.java

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

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions.java

@Override
public void init() throws MalformedURLException, GeneralSecurityException {
    Security.addProvider(new BouncyCastleProvider());
    KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM, "BC");
    kpg.initialize(KEY_SIZE);/*from  w  w  w.jav a 2 s  .c  om*/
    caKeyPair = kpg.genKeyPair();

    X500NameBuilder subjectBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    subjectBuilder.addRDN(BCStyle.CN, "RootCA");

    try {
        sigGen = new JcaContentSignerBuilder(SIGNATURE_ALGORITHM).setProvider("BC")
                .build(caKeyPair.getPrivate());
        X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(subjectBuilder.build(),
                BigInteger.ONE, new Date(), new Date(System.currentTimeMillis() + 600000),
                subjectBuilder.build(), caKeyPair.getPublic());
        caCert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certGen.build(sigGen));

        caCert.checkValidity();
        caCert.verify(caKeyPair.getPublic());
        caCert.verify(caCert.getPublicKey());
    } catch (OperatorCreationException ex) {
        throw new GeneralSecurityException(ex);
    }
}

From source file:ch.admin.suis.msghandler.signer.SignerTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    // Fr Zeile unter siehe
    // https://golb.hplar.ch/p/JCE-policy-changes-in-Java-SE-8u151-and-8u152
    Security.setProperty("crypto.policy", "unlimited");
    Security.addProvider(new BouncyCastleProvider());

    // Erstelle die fr die Tests erforderlichen Verzeichnisse
    createDirectory(signingOutbox1);/*from   w  ww  .  j  av  a2s  .  c o  m*/
    createDirectory(signingOutbox2);
    createDirectory(signingOutbox1Processed);
}

From source file:org.apigw.commons.crypto.ApigwCrypto.java

@PostConstruct
public void init() throws Exception {
    log.debug("Initializing...");
    if (useEncryption) {
        securityProvider = new BouncyCastleProvider();
        Security.addProvider(securityProvider);
        keyStore = initKeyStore(keyStoreFile, keyStorePassword, keyStoreType);
        saltKeyStore = initKeyStore(saltKeyKeyStoreFile, saltKeyKeyStorePassword, saltKeyKeyStoreType);
        salt = initSalt();/*from  w  w w  .jav a  2 s .  c  om*/
        Key key = keyStore.getKey(alias, keyStorePassword.toCharArray());
        validateKey(key);
        String algorithm = key.getAlgorithm();
        int size = key.getEncoded().length * 8;
        log.debug("operations will be performed using {} key with size {}", algorithm, size);

    } else {
        keyStore = null;
        log.warn("No keystore file specified, will not encrypt messages");
    }
    log.debug("Finished initializing");
}