Example usage for java.security.interfaces RSAPublicKey getAlgorithm

List of usage examples for java.security.interfaces RSAPublicKey getAlgorithm

Introduction

In this page you can find the example usage for java.security.interfaces RSAPublicKey getAlgorithm.

Prototype

public String getAlgorithm();

Source Link

Document

Returns the standard algorithm name for this key.

Usage

From source file:com.kuzumeji.platform.standard.SecurityServiceTest.java

@Test
public void testKeyPair() {
    // RSA???// ww  w  . j a  va  2  s .c  o m
    final KeyPair keyPair = testee.generateKeyPair();
    final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    final RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    LOG.debug("?->{}", dumpKeyPair(publicKey));
    LOG.debug("?->{}", dumpKeyPair(privateKey));
    // RSA???/
    testee.saveKeyPair("testee", keyPair);
    final KeyPair keyPair2 = testee.loadKeyPair("testee");
    assertThat(keyPair2.getPublic().getAlgorithm(), is(publicKey.getAlgorithm()));
    assertThat(keyPair2.getPublic().getFormat(), is(publicKey.getFormat()));
    assertThat(keyPair2.getPublic().getEncoded(), is(publicKey.getEncoded()));
    assertThat(keyPair2.getPrivate().getAlgorithm(), is(privateKey.getAlgorithm()));
    assertThat(keyPair2.getPrivate().getFormat(), is(privateKey.getFormat()));
    assertThat(keyPair2.getPrivate().getEncoded(), is(privateKey.getEncoded()));
    // ???(??)
    final File file = testee.savePublicKeyFile(publicKey);
    LOG.debug("? : {}", file.getPath());
}

From source file:org.ejbca.ui.web.pub.CertRequestHttpTest.java

/**
 * Tests request for a pkcs12/*from  www .j  a  v  a  2s  . co  m*/
 * 
 * @throws Exception error
 */
@Test
public void test01RequestPKCS12() throws Exception {
    log.trace(">test01RequestPKCS12()");

    // find a CA (TestCA?) create a user
    // Send certificate request for a server generated PKCS12
    setupUser(SecConst.TOKEN_SOFT_P12);
    setupUserStatus(EndEntityConstants.STATUS_NEW);

    // POST the OCSP request
    URL url = new URL(httpReqPath + '/' + resourceReq);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    // we are going to do a POST
    con.setDoOutput(true);
    con.setRequestMethod("POST");

    // POST it
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    OutputStream os = con.getOutputStream();
    os.write(("user=" + TEST_USERNAME + "&password=foo123&keylength=2048").getBytes("UTF-8"));
    os.close();
    assertEquals("Response code", 200, con.getResponseCode());
    // Some appserver (Weblogic) responds with
    // "application/x-pkcs12; charset=UTF-8"
    String contentType = con.getContentType();
    boolean contentTypeIsPkcs12 = contentType.startsWith("application/x-pkcs12");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // This works for small requests, and PKCS12 requests are small
    InputStream in = con.getInputStream();
    int b = in.read();
    while (b != -1) {
        baos.write(b);
        b = in.read();
    }
    baos.flush();
    in.close();
    byte[] respBytes = baos.toByteArray();
    assertTrue(respBytes.length > 0);
    if (!contentTypeIsPkcs12 && log.isDebugEnabled()) {
        // If the content-type isn't application/x-pkcs12 we like to know what we got back..
        log.debug(new String(respBytes));
    }
    assertTrue("contentType was " + contentType, contentTypeIsPkcs12);

    KeyStore store = KeyStore.getInstance("PKCS12", "BC");
    ByteArrayInputStream is = new ByteArrayInputStream(respBytes);
    store.load(is, "foo123".toCharArray());
    assertTrue(store.containsAlias("ReqTest"));
    X509Certificate cert = (X509Certificate) store.getCertificate("ReqTest");
    PublicKey pk = cert.getPublicKey();
    if (pk instanceof RSAPublicKey) {
        RSAPublicKey rsapk = (RSAPublicKey) pk;
        assertEquals(rsapk.getAlgorithm(), "RSA");
        assertEquals(2048, rsapk.getModulus().bitLength());
    } else {
        assertTrue("Public key is not RSA", false);
    }

    log.trace("<test01RequestPKCS12()");
}

From source file:org.wso2.carbon.identity.oauth.endpoint.jwks.JwksEndpoint.java

@GET
@Path(value = "/jwks")
@Produces(MediaType.APPLICATION_JSON)//from w w w. ja  v  a2 s  .c o  m
public String jwks() {

    String tenantDomain = null;
    int tenantId = -1;
    Object tenantObj = IdentityUtil.threadLocalProperties.get().get(OAuthConstants.TENANT_NAME_FROM_CONTEXT);
    if (tenantObj != null) {
        tenantDomain = (String) tenantObj;
    }
    if (StringUtils.isEmpty(tenantDomain)) {
        tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }

    RSAPublicKey publicKey = null;
    JSONObject jwksJson = new JSONObject();
    FileInputStream file = null;
    try {
        tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
        if (tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
            file = new FileInputStream(
                    CarbonUtils.getServerConfiguration().getFirstProperty("Security.KeyStore.Location"));
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            String password = CarbonUtils.getServerConfiguration().getInstance()
                    .getFirstProperty("Security.KeyStore.Password");
            keystore.load(file, password.toCharArray());
            String alias = CarbonUtils.getServerConfiguration().getInstance()
                    .getFirstProperty("Security.KeyStore.KeyAlias");
            // Get certificate of public key
            Certificate cert = keystore.getCertificate(alias);
            // Get public key
            publicKey = (RSAPublicKey) cert.getPublicKey();
        } else {

            if (tenantId < 1 && tenantId != -1234) {
                String errorMesage = "The tenant is not existing";
                log.error(errorMesage);
                return errorMesage;
            }
            KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(tenantId);
            KeyStore keyStore = keyStoreManager.getKeyStore(generateKSNameFromDomainName(tenantDomain));
            // Get certificate of public key
            Certificate cert = keyStore.getCertificate(tenantDomain);
            publicKey = (RSAPublicKey) cert.getPublicKey();

        }
        String modulus = base64EncodeUint(publicKey.getModulus());
        String exponent = base64EncodeUint(publicKey.getPublicExponent());
        String kty = publicKey.getAlgorithm();
        JSONArray jwksKeyArray = new JSONArray();
        JSONObject jwksKeys = new JSONObject();
        jwksKeys.put("kty", kty);
        jwksKeys.put("alg", alg);
        jwksKeys.put("use", use);
        jwksKeys.put("kid", OAuth2Util.getThumbPrint(tenantDomain, tenantId));
        jwksKeys.put("n", modulus);
        jwksKeys.put("e", exponent);
        jwksKeyArray.put(jwksKeys);
        jwksJson.put("keys", jwksKeyArray);
    } catch (Exception e) {
        String errorMesage = "Error while generating the keyset for " + tenantDomain + " tenant domain.";
        log.error(errorMesage, e);
        return errorMesage;
    } finally {
        IdentityIOStreamUtils.closeInputStream(file);
    }

    return jwksJson.toString();
}