public static PublicKey fromX509PEM(Reader reader) throws IOException { return fromX509PEM(PEMFormatUtil.readerToString(reader)); } }
public static void printKeys() throws Exception { String text = "eyJ0eXAiOiJKV1QiLCJraWQiOiI5azBIUEczbW9YRU5uZSIsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJodHRwczovL2FzaGlnYXJ1Lm5jc2EudWl1Yy5lZHU6OTQ0MyIsInN1YiI6ImpnYXlub3IiLCJleHAiOjE0ODQ3NjQ3NDQsImF1ZCI6Im15cHJveHk6b2E0bXAsMjAxMjovY2xpZW50X2lkLzE0NjQ5ZTJmNDY4NDUwZGFjMGMxODM0ODExZGJkNGM3IiwiaWF0IjoxNDg0NzYzODQ0LCJub25jZSI6IjBaSWktRXV4ZUNfWDhBZ0IzVmlmT29xS2lYV3N6X05sWFN6SXU3aDhyelUiLCJhdXRoX3RpbWUiOiIxNDg0NzYzODQzIn0"; String keyID = "aQEiCy2fJcVgkOft"; KeyPair keyPair = KeyUtil.generateKeyPair(); JSONWebKeys keys = new JSONWebKeys(keyID); JSONWebKey key = new JSONWebKey(); key.privateKey = keyPair.getPrivate(); key.publicKey = keyPair.getPublic(); key.algorithm = "RS256"; key.id = keyID; key.use = "sig"; key.type = "RSA"; keys.put(key); System.out.println("----- START keys"); System.out.println(KeyUtil.toX509PEM(keyPair.getPublic())); System.out.println(KeyUtil.toPKCS1PEM(keyPair.getPrivate())); System.out.println(KeyUtil.toPKCS8PEM(keyPair.getPrivate())); System.out.println("----- END keys\n"); JSONObject jsonKeys = JSONWebKeyUtil.toJSON(keys); JSONWebKeys keys2 = JSONWebKeyUtil.fromJSON(jsonKeys.toString(2)); JSONWebKey webKey = keys2.get(keyID); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(webKey.privateKey.getEncoded()); RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); System.out.println(KeyUtil.toX509PEM(webKey.publicKey)); System.out.println(KeyUtil.toPKCS1PEM(privateKey)); System.out.println(KeyUtil.toPKCS8PEM(privateKey)); }
public static PrivateKey fromPKCS8PEM(Reader reader) throws IOException { return fromPKCS8PEM(PEMFormatUtil.readerToString(reader)); }
public RSA gen_key(int keyLength, int exponent, Object callback) throws NoSuchProviderException, NoSuchAlgorithmException { RSA rsa = new RSA(); keyPair = KeyUtil.generateKeyPair(); return rsa; } public void save_key(String fullPath, String cipher, Object callback) throws IOException {
public static void firstTestB() throws Exception { String keyID = "9k0HPG3moXENne"; JSONWebKeys keys = JSONWebKeyUtil.fromJSON(new File("/home/ncsa/dev/csd/config/keys.jwk")); JSONObject payload = new JSONObject(); payload.put("name", "jeff"); payload.put("id", "sukjfhusdfsdjkfh"); payload.put("other_claim", "skjdf93489ghiovs 98sd89wehi ws"); payload.put("another_claim", "l;kfg8934789dfio9v 92w89 98wer"); JSONWebKey webKey = keys.get(keyID); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(webKey.privateKey.getEncoded()); RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); System.out.println(KeyUtil.toX509PEM(webKey.publicKey)); System.out.println(KeyUtil.toPKCS1PEM(privateKey)); System.out.println(KeyUtil.toPKCS8PEM(privateKey)); String tokken = createJWT(payload, keys.get(keyID)); System.out.println("JWT=" + tokken); System.out.println("claims=" + verifyAndReadJWT(tokken, keys)); System.out.println("-----"); // note that if the this last call // works it is because the verification works too. }
/** * Write a PEM format PKCS1 private using a writer. * * @param privateKey * @param writer * @throws IOException */ public static void toPKCS1PEM(PrivateKey privateKey, Writer writer) throws IOException { String pem = toPKCS1PEM(privateKey); writer.write(pem); writer.flush(); }
public void save_pub_key(String fullPath) throws IOException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { KeyUtil.toX509PEM(keyPair.getPublic(), new FileWriter(fullPath)); } }
public static void toPKCS8PEM(PrivateKey privateKey, Writer writer) throws IOException { writer.write(toPKCS8PEM(privateKey)); writer.flush(); }
@Override protected KeyPair createNew() { try { return KeyUtil.generateKeyPair(); } catch (Exception e) { throw new GeneralException("Error generating keypair", e); } } }
public void save_key(String fullPath, String cipher, Object callback) throws IOException { KeyUtil.toPKCS1PEM(keyPair.getPrivate(), new FileOutputStream(fullPath)); } public void save_pub_key(String fullPath) throws IOException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
public static void toX509PEM(PublicKey publicKey, Writer writer) throws IOException { writer.write(toX509PEM(publicKey)); writer.flush(); }
/** * Internal call to convert the private key. * @return */ protected String getPrivKeyString() { if (privKeyString == null) { try { privKeyString = toPKCS8PEM(getPrivateKey()); } catch (Exception e) { throw new GeneralException("Error: could not convert private key to a PKCS 8 PEM", e); } } return privKeyString; }
protected JSONWebKey createJWK(String algorithm) throws NoSuchProviderException, NoSuchAlgorithmException { byte[] byteArray = new byte[16]; random.nextBytes(byteArray); String id = DatatypeConverter.printHexBinary(byteArray); KeyPair keyPair = KeyUtil.generateKeyPair(); JSONWebKey webKey = new JSONWebKey(); webKey.publicKey = keyPair.getPublic(); webKey.privateKey = keyPair.getPrivate(); webKey.use = "sig"; webKey.id = id; webKey.algorithm = algorithm; webKey.type = "RSA"; //only one supported return webKey; } }
protected PublicKey checkPublicKey() throws IOException { String publicKeyFileName = getCfgValue(ClientXMLTags.PUBLIC_KEY); if (trivial(publicKeyFileName)) { throw new MyConfigurationException("Error: There is no public key specified."); } File publicKeyFile = new File(publicKeyFileName); if (!publicKeyFile.exists()) { throw new MyConfigurationException("Error: The specified public key file \"" + publicKeyFileName + "\" does not exist"); } if (!publicKeyFile.isFile()) { throw new MyConfigurationException("Error: The specified public key file \"" + publicKeyFileName + "\" is not actually a file"); } if (!publicKeyFile.canRead()) { throw new MyConfigurationException("Error: The specified public key file \"" + publicKeyFileName + "\" is not readable. Check the permissions."); } return KeyUtil.fromX509PEM(new FileReader(publicKeyFile)); }
public static void toPKCS1PEM(PrivateKey privateKey, OutputStream out) throws IOException { PrintStream printStream = new PrintStream(out); printStream.print(toPKCS1PEM(privateKey)); printStream.flush(); }
public static void testSigning() throws Exception { String h = "{\"typ\":\"JWT\",\"kid\":\"9k0HPG3moXENne\",\"alg\":\"RS256\"}"; String p = "{\"iss\":\"https://ashigaru.ncsa.uiuc.edu:9443\",\"sub\":\"jgaynor\",\"exp\":1484764744,\"aud\":\"myproxy:oa4mp,2012:/client_id/14649e2f468450dac0c1834811dbd4c7\",\"iat\":1484763844,\"nonce\":\"0ZIi-EuxeC_X8AgB3VifOoqKiXWsz_NlXSzIu7h8rzU\",\"auth_time\":\"1484763843\"}\n"; JSONObject header = JSONObject.fromObject(h); System.out.println("header=" + header); JSONObject payload = JSONObject.fromObject(p); System.out.println("payload=" + payload); System.out.println("base 64=" + concat(header, payload)); //String keyID = "9k0HPG3moXENne"; String keyID = "244B235F6B28E34108D101EAC7362C4E"; JSONWebKeys keys = JSONWebKeyUtil.fromJSON(new File("/home/ncsa/dev/csd/config/polo-keys.jwk")); String idTokken = createJWT(payload, keys.get(keyID)); System.out.println(idTokken); JSONObject claims = verifyAndReadJWT(idTokken, keys); System.out.println("claims = " + claims); JSONWebKey webKey = keys.get(keyID); System.out.println(KeyUtil.toX509PEM(webKey.publicKey)); }
request.setAttribute(REDIR, REDIR); request.setAttribute("redirectUrl", gtwResp.getRedirect().toString()); request.setAttribute("privateKey", KeyUtil.toPKCS8PEM(gtwResp.getPrivateKey())); request.setAttribute(ACTION_KEY, ACTION_KEY); request.setAttribute("action", ACTION_REDIRECT_VALUE);
protected PrivateKey checkPrivateKey() throws IOException { String privateKeyFileName = getCfgValue(ClientXMLTags.PRIVATE_KEY); if (trivial(privateKeyFileName)) { throw new MyConfigurationException("Error: There is no private key specified."); } File privateKeyFile = new File(privateKeyFileName); if (!privateKeyFile.exists()) { throw new MyConfigurationException("Error: The specified private key file \"" + privateKeyFileName + "\" does not exist"); } if (!privateKeyFile.isFile()) { throw new MyConfigurationException("Error: The specified private key file \"" + privateKeyFileName + "\" is not actually a file"); } if (!privateKeyFile.canRead()) { throw new MyConfigurationException("Error: The specified private key file \"" + privateKeyFileName + "\" is not readable. Check the permissions."); } return KeyUtil.fromPKCS8PEM(new FileReader(privateKeyFile)); }
/** * Command line utility to generate a keypair with a given DN. This is intended for low level * debugging, not public consumption. This is a very stupid utility but much more convenient * than using openSSL or some other such command line utility. * @param args */ public static void main(String[] args){ if(args.length != 1){ System.out.println("Usage: This accepts a single argument that is the DN for a cert request. It returns the pem encoded " + "cert request (but not the private key)"); return; } try{ KeyPair keyPair = KeyUtil.generateKeyPair(); MyPKCS10CertRequest cr = CertUtil.createCertRequest(keyPair, args[0]); System.out.println(CertUtil.fromCertReqToString(cr)); }catch(Throwable t){ t.printStackTrace(); } } }
KeyUtil.fromX509PEM(sb.toString()); askForFile = false; } catch (GeneralException gx) {