Java Code Examples for java.security.AlgorithmConstraints#permits()

The following examples show how to use java.security.AlgorithmConstraints#permits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SupportedGroupsExtension.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
static NamedGroup getPreferredGroup(
        ProtocolVersion negotiatedProtocol,
        AlgorithmConstraints constraints, NamedGroupType type,
        List<NamedGroup> requestedNamedGroups) {
    for (NamedGroup namedGroup : requestedNamedGroups) {
        if ((namedGroup.type == type) &&
                namedGroup.isAvailable(negotiatedProtocol) &&
                isSupported(namedGroup) &&
                constraints.permits(
                        EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
                        namedGroup.algorithm,
                        namedGroupParams.get(namedGroup))) {
            return namedGroup;
        }
    }

    return null;
}
 
Example 2
Source File: EllipticCurvesExtension.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static EllipticCurvesExtension createExtension(
            AlgorithmConstraints constraints) {

    ArrayList<Integer> idList = new ArrayList<>(supportedCurveIds.length);
    for (int curveId : supportedCurveIds) {
        if (constraints.permits(
                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
                            "EC", idToParams.get(curveId))) {
            idList.add(curveId);
        }
    }

    if (!idList.isEmpty()) {
        int[] ids = new int[idList.size()];
        int i = 0;
        for (Integer id : idList) {
            ids[i++] = id;
        }

        return new EllipticCurvesExtension(ids);
    }

    return null;
}
 
Example 3
Source File: SupportedEllipticCurvesExtension.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static SupportedEllipticCurvesExtension createExtension(
            AlgorithmConstraints constraints) {

    ArrayList<Integer> idList = new ArrayList<>(supportedCurveIds.length);
    for (int curveId : supportedCurveIds) {
        if (constraints.permits(
                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
                            "EC", idToParams.get(curveId))) {
            idList.add(curveId);
        }
    }

    if (!idList.isEmpty()) {
        int[] ids = new int[idList.size()];
        int i = 0;
        for (Integer id : idList) {
            ids[i++] = id;
        }

        return new SupportedEllipticCurvesExtension(ids);
    }

    return null;
}
 
Example 4
Source File: ECDHKeyExchange.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
void checkConstraints(AlgorithmConstraints constraints,
        byte[] encodedPoint) throws SSLHandshakeException {
    try {

        ECParameterSpec params = publicKey.getParams();
        ECPoint point =
                ECUtil.decodePoint(encodedPoint, params.getCurve());
        ECPublicKeySpec spec = new ECPublicKeySpec(point, params);

        KeyFactory kf = KeyFactory.getInstance("EC");
        ECPublicKey pubKey = (ECPublicKey)kf.generatePublic(spec);

        // check constraints of ECPublicKey
        if (!constraints.permits(
                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), pubKey)) {
            throw new SSLHandshakeException(
                "ECPublicKey does not comply to algorithm constraints");
        }
    } catch (GeneralSecurityException | java.io.IOException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
                "Could not generate ECPublicKey").initCause(e);
    }
}
 
Example 5
Source File: ECDHKeyExchange.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
void checkConstraints(AlgorithmConstraints constraints,
        byte[] encodedPoint) throws SSLHandshakeException {
    try {

        ECParameterSpec params = publicKey.getParams();
        ECPoint point =
                JsseJce.decodePoint(encodedPoint, params.getCurve());
        ECPublicKeySpec spec = new ECPublicKeySpec(point, params);

        KeyFactory kf = JsseJce.getKeyFactory("EC");
        ECPublicKey pubKey = (ECPublicKey)kf.generatePublic(spec);

        // check constraints of ECPublicKey
        if (!constraints.permits(
                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), pubKey)) {
            throw new SSLHandshakeException(
                "ECPublicKey does not comply to algorithm constraints");
        }
    } catch (GeneralSecurityException | java.io.IOException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
                "Could not generate ECPublicKey").initCause(e);
    }
}
 
Example 6
Source File: ECDHClientKeyExchange.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
static void checkConstraints(AlgorithmConstraints constraints,
        ECPublicKey publicKey,
        byte[] encodedPoint) throws SSLHandshakeException {

    try {
        ECParameterSpec params = publicKey.getParams();
        ECPoint point =
                JsseJce.decodePoint(encodedPoint, params.getCurve());
        ECPublicKeySpec spec = new ECPublicKeySpec(point, params);

        KeyFactory kf = JsseJce.getKeyFactory("EC");
        ECPublicKey peerPublicKey =
                (ECPublicKey)kf.generatePublic(spec);

        // check constraints of ECPublicKey
        if (!constraints.permits(
                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
                peerPublicKey)) {
            throw new SSLHandshakeException(
                "ECPublicKey does not comply to algorithm constraints");
        }
    } catch (GeneralSecurityException | java.io.IOException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
                "Could not generate ECPublicKey").initCause(e);
    }
}
 
Example 7
Source File: EllipticCurvesExtension.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static EllipticCurvesExtension createExtension(
            AlgorithmConstraints constraints) {

    ArrayList<Integer> idList = new ArrayList<>(supportedCurveIds.length);
    for (int curveId : supportedCurveIds) {
        if (constraints.permits(
                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
                            "EC", idToParams.get(curveId))) {
            idList.add(curveId);
        }
    }

    if (!idList.isEmpty()) {
        int[] ids = new int[idList.size()];
        int i = 0;
        for (Integer id : idList) {
            ids[i++] = id;
        }

        return new EllipticCurvesExtension(ids);
    }

    return null;
}
 
Example 8
Source File: SignatureAndHashAlgorithm.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm> getSupportedAlgorithms(
        AlgorithmConstraints constraints,
        Collection<SignatureAndHashAlgorithm> algorithms ) {
    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : algorithms) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                            sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 9
Source File: SignatureAndHashAlgorithm.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm> getSupportedAlgorithms(
        AlgorithmConstraints constraints,
        Collection<SignatureAndHashAlgorithm> algorithms ) {
    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : algorithms) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                            sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 10
Source File: SignatureAndHashAlgorithm.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm>
        getSupportedAlgorithms(AlgorithmConstraints constraints) {

    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : priorityMap.values()) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                        sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 11
Source File: SignatureAndHashAlgorithm.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm> getSupportedAlgorithms(
        AlgorithmConstraints constraints,
        Collection<SignatureAndHashAlgorithm> algorithms ) {
    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : algorithms) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                            sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 12
Source File: SignatureAndHashAlgorithm.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm> getSupportedAlgorithms(
        AlgorithmConstraints constraints,
        Collection<SignatureAndHashAlgorithm> algorithms ) {
    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : algorithms) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                            sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 13
Source File: SignatureAndHashAlgorithm.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm> getSupportedAlgorithms(
        AlgorithmConstraints constraints,
        Collection<SignatureAndHashAlgorithm> algorithms ) {
    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : algorithms) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                            sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 14
Source File: SignatureAndHashAlgorithm.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm>
        getSupportedAlgorithms(AlgorithmConstraints constraints) {

    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : priorityMap.values()) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                        sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 15
Source File: SupportedGroupsExtension.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
static boolean isActivatable(
        AlgorithmConstraints constraints, NamedGroupType type) {

    boolean hasFFDHEGroups = false;
    for (NamedGroup namedGroup : supportedNamedGroups) {
        if (namedGroup.type == type) {
            if (constraints.permits(
                    EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
                    namedGroup.algorithm,
                    namedGroupParams.get(namedGroup))) {

                return true;
            }

            if (!hasFFDHEGroups &&
                    (type == NamedGroupType.NAMED_GROUP_FFDHE)) {
                hasFFDHEGroups = true;
            }
        }
    }

    // For compatibility, if no FFDHE groups are defined, the non-FFDHE
    // compatible mode (using DHE cipher suite without FFDHE extension)
    // is allowed.
    //
    // Note that the constraints checking on DHE parameters will be
    // performed during key exchanging in a handshake.
    return !hasFFDHEGroups && type == NamedGroupType.NAMED_GROUP_FFDHE;
}
 
Example 16
Source File: SignatureAndHashAlgorithm.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm>
        getSupportedAlgorithms(AlgorithmConstraints constraints) {

    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : priorityMap.values()) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                        sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 17
Source File: SignatureAndHashAlgorithm.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static Collection<SignatureAndHashAlgorithm>
        getSupportedAlgorithms(AlgorithmConstraints constraints) {

    Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>();
    for (SignatureAndHashAlgorithm sigAlg : priorityMap.values()) {
        if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM &&
                constraints.permits(SIGNATURE_PRIMITIVE_SET,
                        sigAlg.algorithm, null)) {
            supported.add(sigAlg);
        }
    }

    return supported;
}
 
Example 18
Source File: EllipticCurvesExtension.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static int getPreferredCurve(int[] curves,
            AlgorithmConstraints constraints) {
    for (int curveId : curves) {
        if (isSupported(curveId) && constraints.permits(
                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
                            "EC", idToParams.get(curveId))) {
            return curveId;
        }
    }

    return -1;
}
 
Example 19
Source File: HandshakeContext.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private static List<ProtocolVersion> getActiveProtocols(
        List<ProtocolVersion> enabledProtocols,
        List<CipherSuite> enabledCipherSuites,
        AlgorithmConstraints algorithmConstraints) {
    boolean enabledSSL20Hello = false;
    ArrayList<ProtocolVersion> protocols = new ArrayList<>(4);
    for (ProtocolVersion protocol : enabledProtocols) {
        if (!enabledSSL20Hello && protocol == ProtocolVersion.SSL20Hello) {
            enabledSSL20Hello = true;
            continue;
        }

        if (!algorithmConstraints.permits(
                EnumSet.of(CryptoPrimitive.KEY_AGREEMENT),
                protocol.name, null)) {
            // Ignore disabled protocol.
            continue;
        }

        boolean found = false;
        Map<NamedGroupSpec, Boolean> cachedStatus =
                new EnumMap<>(NamedGroupSpec.class);
        for (CipherSuite suite : enabledCipherSuites) {
            if (suite.isAvailable() && suite.supports(protocol)) {
                if (isActivatable(suite,
                        algorithmConstraints, cachedStatus)) {
                    protocols.add(protocol);
                    found = true;
                    break;
                }
            } else if (SSLLogger.isOn && SSLLogger.isOn("verbose")) {
                SSLLogger.fine(
                    "Ignore unsupported cipher suite: " + suite +
                         " for " + protocol);
            }
        }

        if (!found && (SSLLogger.isOn) && SSLLogger.isOn("handshake")) {
            SSLLogger.fine(
                "No available cipher suite for " + protocol);
        }
    }

    if (!protocols.isEmpty()) {
        if (enabledSSL20Hello) {
            protocols.add(ProtocolVersion.SSL20Hello);
        }
        Collections.sort(protocols);
    }

    return Collections.unmodifiableList(protocols);
}
 
Example 20
Source File: HandshakeContext.java    From openjsse with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isActivatable(CipherSuite suite,
        AlgorithmConstraints algorithmConstraints,
        Map<NamedGroupType, Boolean> cachedStatus) {

    if (algorithmConstraints.permits(
            EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), suite.name, null)) {
        if (suite.keyExchange == null) {
            // TLS 1.3, no definition of key exchange in cipher suite.
            return true;
        }

        boolean available;
        NamedGroupType groupType = suite.keyExchange.groupType;
        if (groupType != NAMED_GROUP_NONE) {
            Boolean checkedStatus = cachedStatus.get(groupType);
            if (checkedStatus == null) {
                available = SupportedGroups.isActivatable(
                        algorithmConstraints, groupType);
                cachedStatus.put(groupType, available);

                if (!available &&
                        SSLLogger.isOn && SSLLogger.isOn("verbose")) {
                    SSLLogger.fine("No activated named group");
                }
            } else {
                available = checkedStatus;
            }

            if (!available && SSLLogger.isOn && SSLLogger.isOn("verbose")) {
                SSLLogger.fine(
                    "No active named group, ignore " + suite);
            }
            return available;
        } else {
            return true;
        }
    } else if (SSLLogger.isOn && SSLLogger.isOn("verbose")) {
        SSLLogger.fine("Ignore disabled cipher suite: " + suite);
    }

    return false;
}