Search in sources :

Example 1 with JSONObjectReader

use of org.webpki.json.JSONObjectReader in project openkeystore by cyberphone.

the class CborEncryption method readJwk.

static KeyPair readJwk(String keyType) throws Exception {
    JSONObjectReader jwkPlus = JSONParser.parse(ArrayUtil.readFile(baseKey + keyType + "privatekey.jwk"));
    // Note: The built-in JWK decoder does not accept "kid" since it doesn't have a meaning in JSF or JEF.
    keyId = new CBORTextString(jwkPlus.getString("kid"));
    jwkPlus.removeProperty("kid");
    return jwkPlus.getKeyPair();
}
Also used : JSONObjectReader(org.webpki.json.JSONObjectReader) CBORTextString(org.webpki.cbor.CBORTextString)

Example 2 with JSONObjectReader

use of org.webpki.json.JSONObjectReader in project openkeystore by cyberphone.

the class JsonEncryption method optionalUpdate.

static void optionalUpdate(String baseName, byte[] encryptedData, LocalDecrypt decrypter) throws Exception {
    String fileName = baseEncryption + baseName;
    JSONObjectReader newEncryptedData = JSONParser.parse(encryptedData);
    if (!ArrayUtil.compare(decrypter.decrypt(newEncryptedData), dataToBeEncrypted)) {
        throw new IOException("Decrypt err:" + baseName);
    }
    boolean changed = true;
    try {
        JSONObjectReader oldEncryptedData = JSONParser.parse(ArrayUtil.readFile(fileName));
        try {
            if (ArrayUtil.compare(decrypter.decrypt(oldEncryptedData), dataToBeEncrypted)) {
                // All good but are the new and old effectively the same?
                if (cleanEncryption(newEncryptedData).equals(cleanEncryption(oldEncryptedData))) {
                    // Yes, don't rewrite.
                    return;
                }
            }
        } catch (Exception e) {
        }
    } catch (Exception e) {
        // New I guess
        changed = false;
    }
    if (changed) {
        System.out.println("UPDATED: " + baseName);
    }
    ArrayUtil.writeFile(fileName, encryptedData);
}
Also used : JSONObjectReader(org.webpki.json.JSONObjectReader) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 3 with JSONObjectReader

use of org.webpki.json.JSONObjectReader in project openkeystore by cyberphone.

the class JsonEncryption method coreAsymEnc.

static void coreAsymEnc(String keyType, String fileSuffix, ContentEncryptionAlgorithms contentEncryptionAlgorithm, boolean wantKeyId, boolean wantPublicKey, JSONCryptoHelper.ExtensionHolder extensionHolder, JSONObjectWriter extensions) throws Exception {
    KeyPair keyPair = readJwk(keyType);
    KeyEncryptionAlgorithms keyEncryptionAlgorithm = KeyEncryptionAlgorithms.RSA_OAEP_256;
    if (!(keyPair.getPublic() instanceof RSAKey)) {
        switch(contentEncryptionAlgorithm.getKeyLength()) {
            case 16:
                keyEncryptionAlgorithm = KeyEncryptionAlgorithms.ECDH_ES_A128KW;
                break;
            case 32:
                keyEncryptionAlgorithm = KeyEncryptionAlgorithms.ECDH_ES_A256KW;
                break;
            default:
                keyEncryptionAlgorithm = KeyEncryptionAlgorithms.ECDH_ES;
                break;
        }
    }
    if (keyEncryptionAlgorithm == KeyEncryptionAlgorithms.RSA_OAEP_256 && contentEncryptionAlgorithm == ContentEncryptionAlgorithms.A128GCM) {
        keyEncryptionAlgorithm = KeyEncryptionAlgorithms.RSA_OAEP;
    }
    JSONAsymKeyEncrypter encrypter = new JSONAsymKeyEncrypter(keyPair.getPublic(), keyEncryptionAlgorithm);
    JSONCryptoHelper.Options options = new JSONCryptoHelper.Options();
    if (extensionHolder != null) {
        options.setPermittedExtensions(extensionHolder);
        encrypter.setExtensions(extensions);
    }
    encrypter.setOutputPublicKeyInfo(wantPublicKey);
    if (!wantPublicKey) {
        options.setPublicKeyOption(JSONCryptoHelper.PUBLIC_KEY_OPTIONS.FORBIDDEN);
    }
    if (wantKeyId) {
        encrypter.setKeyId(keyId);
        options.setKeyIdOption(JSONCryptoHelper.KEY_ID_OPTIONS.REQUIRED);
    }
    byte[] encryptedData = JSONObjectWriter.createEncryptionObject(dataToBeEncrypted, contentEncryptionAlgorithm, encrypter).serializeToBytes(JSONOutputFormats.PRETTY_PRINT);
    optionalUpdate(keyType + "#" + keyEncryptionAlgorithm.getJoseAlgorithmId().toLowerCase() + "@" + contentEncryptionAlgorithm.getJoseAlgorithmId().toLowerCase() + "@" + fileSuffix, encryptedData, new LocalDecrypt() {

        @Override
        public byte[] decrypt(JSONObjectReader reader) throws Exception {
            return reader.getEncryptionObject(options).getDecryptedData(keyPair.getPrivate());
        }
    });
}
Also used : KeyPair(java.security.KeyPair) RSAKey(java.security.interfaces.RSAKey) JSONAsymKeyEncrypter(org.webpki.json.JSONAsymKeyEncrypter) JSONCryptoHelper(org.webpki.json.JSONCryptoHelper) JSONObjectReader(org.webpki.json.JSONObjectReader) KeyEncryptionAlgorithms(org.webpki.crypto.KeyEncryptionAlgorithms) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 4 with JSONObjectReader

use of org.webpki.json.JSONObjectReader in project openkeystore by cyberphone.

the class AuthenticationRequestDecoder method readServerRequest.

// ///////////////////////////////////////////////////////////////////////////////////////////
// JSON Reader
// ///////////////////////////////////////////////////////////////////////////////////////////
@Override
void readServerRequest(JSONObjectReader rd) throws IOException {
    // ///////////////////////////////////////////////////////////////////////////////////////
    // Read the top level properties
    // ///////////////////////////////////////////////////////////////////////////////////////
    id = InputValidator.getID(rd, ID_JSON);
    serverTime = rd.getDateTime(SERVER_TIME_JSON, ISODateTime.UTC_NO_SUBSECONDS);
    languages = InputValidator.getListConditional(rd, PREFERRED_LANGUAGES_JSON);
    keyContainerList = KeyContainerTypes.getOptionalKeyContainerSet(InputValidator.getListConditional(rd, KeyContainerTypes.KCT_TARGET_KEY_CONTAINERS));
    extendedCertPath = rd.getBooleanConditional(EXTENDED_CERT_PATH_JSON);
    // Default: no timeout and associated GUI
    expires = rd.hasProperty(EXPIRES_JSON) ? rd.getInt(EXPIRES_JSON) : -1;
    // ///////////////////////////////////////////////////////////////////////////////////////
    // Optional client features [0..1]
    // ///////////////////////////////////////////////////////////////////////////////////////
    String[] features = InputValidator.getURIListConditional(rd, CLIENT_FEATURES_JSON);
    if (features != null)
        for (String feature : features) {
            if (!clientFeatures.add(feature)) {
                bad("Duplicate \"" + CLIENT_FEATURES_JSON + "\"  :" + feature);
            }
        }
    // ///////////////////////////////////////////////////////////////////////////////////////
    for (String sig_alg_string : InputValidator.getNonEmptyList(rd, SIGNATURE_ALGORITHMS_JSON)) {
        AsymSignatureAlgorithms sig_alg = AsymSignatureAlgorithms.getAlgorithmFromId(sig_alg_string, AlgorithmPreferences.JOSE_ACCEPT_PREFER);
        if (!algorithms.add(sig_alg)) {
            bad("Duplicate \"" + SIGNATURE_ALGORITHMS_JSON + "\" : " + sig_alg_string);
        }
        if (sig_alg.getDigestAlgorithm() == null) {
            bad("Not a proper signature algorithm: " + sig_alg_string);
        }
    }
    // ///////////////////////////////////////////////////////////////////////////////////////
    for (JSONObjectReader cf : InputValidator.getObjectArrayConditional(rd, CERTIFICATE_FILTERS_JSON)) {
        certificateFilters.add(CertificateFilterReader.read(cf));
    }
}
Also used : JSONObjectReader(org.webpki.json.JSONObjectReader) AsymSignatureAlgorithms(org.webpki.crypto.AsymSignatureAlgorithms)

Example 5 with JSONObjectReader

use of org.webpki.json.JSONObjectReader in project openkeystore by cyberphone.

the class CBORTest method readJwk.

static KeyPair readJwk(String keyType) throws Exception {
    JSONObjectReader jwkPlus = JSONParser.parse(ArrayUtil.readFile(baseKey + keyType + "privatekey.jwk"));
    // Note: The built-in JWK decoder does not accept "kid" since it doesn't have a meaning in JSF or JEF.
    keyId = new CBORTextString(jwkPlus.getString("kid"));
    jwkPlus.removeProperty("kid");
    return jwkPlus.getKeyPair();
}
Also used : JSONObjectReader(org.webpki.json.JSONObjectReader)

Aggregations

JSONObjectReader (org.webpki.json.JSONObjectReader)29 IOException (java.io.IOException)13 KeyPair (java.security.KeyPair)9 GeneralSecurityException (java.security.GeneralSecurityException)7 JSONCryptoHelper (org.webpki.json.JSONCryptoHelper)7 JSONObjectWriter (org.webpki.json.JSONObjectWriter)5 RSAKey (java.security.interfaces.RSAKey)4 ArrayList (java.util.ArrayList)4 ServletException (javax.servlet.ServletException)4 JSONArrayReader (org.webpki.json.JSONArrayReader)4 KeyEncryptionAlgorithms (org.webpki.crypto.KeyEncryptionAlgorithms)3 JSONAsymKeySigner (org.webpki.json.JSONAsymKeySigner)3 JSONSignatureDecoder (org.webpki.json.JSONSignatureDecoder)3 PublicKey (java.security.PublicKey)2 CBORTextString (org.webpki.cbor.CBORTextString)2 AsymSignatureAlgorithms (org.webpki.crypto.AsymSignatureAlgorithms)2 JSONAsymKeyEncrypter (org.webpki.json.JSONAsymKeyEncrypter)2 JSONAsymKeyVerifier (org.webpki.json.JSONAsymKeyVerifier)2 JSONSigner (org.webpki.json.JSONSigner)2 KeyPairGenerator (java.security.KeyPairGenerator)1