public static EllipticCurveJsonWebKey generateJwk(ECParameterSpec spec, String provider, SecureRandom secureRandom) throws JoseException { EcKeyUtil keyUtil = new EcKeyUtil(provider, secureRandom); KeyPair keyPair = keyUtil.generateKeyPair(spec); PublicKey publicKey = keyPair.getPublic(); EllipticCurveJsonWebKey ecJwk = (EllipticCurveJsonWebKey) PublicJsonWebKey.Factory.newPublicJwk(publicKey); ecJwk.setPrivateKey(keyPair.getPrivate()); return ecJwk; } }
public static RsaJsonWebKey generateJwk(int bits, String provider, SecureRandom secureRandom) throws JoseException { RsaKeyUtil keyUtil = new RsaKeyUtil(provider, secureRandom); KeyPair keyPair = keyUtil.generateKeyPair(bits); RsaJsonWebKey rsaJwk = (RsaJsonWebKey) PublicJsonWebKey.Factory.newPublicJwk(keyPair.getPublic()); rsaJwk.setPrivateKey(keyPair.getPrivate()); return rsaJwk; } }
/** * Puts a {@link Key} into the claim. The key is serializied as JWK. * * @param key * Property key * @param publickey * {@link PublicKey} to serialize * @return {@code this} */ public JSONBuilder putKey(String key, PublicKey publickey) { Objects.requireNonNull(publickey, "publickey"); try { final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(publickey); Map<String, Object> jwkParams = jwk.toParams(JsonWebKey.OutputControlLevel.PUBLIC_ONLY); object(key).data.putAll(jwkParams); return this; } catch (JoseException ex) { throw new AcmeProtocolException("Invalid key", ex); } }
conn.setDoOutput(true); final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(keypair.getPublic()); JsonWebSignature jws = new JsonWebSignature(); jws.setPayload(claimJson);
conn.setDoOutput(true); final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(keypair.getPublic()); JsonWebSignature jws = new JsonWebSignature(); jws.setPayload(claimJson);
PublicJsonWebKey newKeyJwk = PublicJsonWebKey.Factory.newPublicJwk(newKeyPair.getPublic());
throws AcmeException { try { PublicJsonWebKey keyJwk = PublicJsonWebKey.Factory.newPublicJwk(accountKey);
throws AcmeException { try { PublicJsonWebKey keyJwk = PublicJsonWebKey.Factory.newPublicJwk(accountKey);
PublicJsonWebKey newKeyJwk = PublicJsonWebKey.Factory.newPublicJwk(newKeyPair.getPublic());
public boolean testDecryptWithJose4J(String jwe) { try { PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(recipientJwkJson); JsonWebEncryption receiverJwe = new JsonWebEncryption(); AlgorithmConstraints algConstraints = new AlgorithmConstraints(ConstraintType.WHITELIST, KeyManagementAlgorithmIdentifiers.RSA_OAEP); receiverJwe.setAlgorithmConstraints(algConstraints); AlgorithmConstraints encConstraints = new AlgorithmConstraints(ConstraintType.WHITELIST, ContentEncryptionAlgorithmIdentifiers.AES_128_GCM); receiverJwe.setContentEncryptionAlgorithmConstraints(encConstraints); receiverJwe.setKey(jwk.getPrivateKey()); receiverJwe.setCompactSerialization(jwe); final String decryptedPayload = new String(Base64Util.base64urldecode(receiverJwe.getPlaintextString())); System.out.println("Jose4j decrypt succeed: " + decryptedPayload); if (decryptedPayload.equals(PAYLOAD)) { return true; } } catch (Exception e) { System.out.println("Jose4j decrypt failed: " + e.getMessage()); e.printStackTrace(); } return false; }
/** * Puts a {@link Key} into the claim. The key is serializied as JWK. * * @param key * Property key * @param publickey * {@link PublicKey} to serialize * @return {@code this} */ public JSONBuilder putKey(String key, PublicKey publickey) { Objects.requireNonNull(publickey, "publickey"); try { final PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(publickey); Map<String, Object> jwkParams = jwk.toParams(JsonWebKey.OutputControlLevel.PUBLIC_ONLY); object(key).data.putAll(jwkParams); return this; } catch (JoseException ex) { throw new AcmeProtocolException("Invalid key", ex); } }
/** * Returns the authorization string. * <p> * The default is {@code token + '.' + base64url(jwkThumbprint)}. Subclasses may * override this method if a different algorithm is used. */ public String getAuthorization() { try { PublicKey pk = getLogin().getKeyPair().getPublic(); PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(pk); return getToken() + '.' + base64UrlEncode(jwk.calculateThumbprint("SHA-256")); } catch (JoseException ex) { throw new AcmeProtocolException("Cannot compute key thumbprint", ex); } }
/** * Returns the authorization string. * <p> * The default is {@code token + '.' + base64url(jwkThumbprint)}. Subclasses may * override this method if a different algorithm is used. */ public String getAuthorization() { try { PublicKey pk = getLogin().getKeyPair().getPublic(); PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(pk); return getToken() + '.' + base64UrlEncode(jwk.calculateThumbprint("SHA-256")); } catch (JoseException ex) { throw new AcmeProtocolException("Cannot compute key thumbprint", ex); } }
public static PublicJsonWebKey newPublicJwk(String json) throws JoseException { return newPublicJwk(json, null); }
public static PublicJsonWebKey newPublicJwk(Map<String,Object> params) throws JoseException { return newPublicJwk(params, null); }
public PublicJsonWebKey getPublicJwkHeaderValue(String name, String jcaProvider) throws JoseException { Object objectHeaderValue = getObjectHeaderValue(name); Map<String, Object> jwkParams = (Map<String, Object>) objectHeaderValue; return PublicJsonWebKey.Factory.newPublicJwk(jwkParams, jcaProvider); }
public static PublicJsonWebKey newPublicJwk(String json, String jcaProvider) throws JoseException { Map<String, Object> parsed = JsonUtil.parseJson(json); return newPublicJwk(parsed, jcaProvider); } }