001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2021, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.crypto;
019
020
021import com.nimbusds.jose.*;
022import com.nimbusds.jose.crypto.impl.*;
023import com.nimbusds.jose.jwk.Curve;
024import com.nimbusds.jose.jwk.ECKey;
025import com.nimbusds.jose.jwk.gen.ECKeyGenerator;
026import com.nimbusds.jose.util.Pair;
027import net.jcip.annotations.ThreadSafe;
028
029import javax.crypto.SecretKey;
030import java.security.interfaces.ECPrivateKey;
031import java.security.interfaces.ECPublicKey;
032import java.util.*;
033
034
035/**
036 * Elliptic Curve Diffie-Hellman Multi-recipient encrypter of
037 * {@link JWEObjectJSON JWE objects} for curves using EC JWK keys.
038 * Expects a public EC key (with a P-256, P-384, or P-521 curve).
039 *
040 * <p>Public Key Authenticated Encryption for JOSE
041 * <a href="https://datatracker.ietf.org/doc/html/draft-madden-jose-ecdh-1pu-04">ECDH-1PU</a>
042 * for more information.
043 *
044 * <p>This class is thread-safe.
045 *
046 * <p>Supports the following key management algorithms:
047 *
048 * <ul>
049 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES}
050 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A128KW}
051 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A192KW}
052 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A256KW}
053 * </ul>
054 *
055 * <p>Supports the following elliptic curves:
056 *
057 * <ul>
058 *     <li>{@link com.nimbusds.jose.jwk.Curve#P_256}
059 *     <li>{@link com.nimbusds.jose.jwk.Curve#P_384}
060 *     <li>{@link com.nimbusds.jose.jwk.Curve#P_521}
061 * </ul>
062 *
063 * <p>Supports the following content encryption algorithms:
064 *
065 * <ul>
066 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256}
067 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384}
068 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512}
069 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128GCM}
070 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A192GCM}
071 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256GCM}
072 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED}
073 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED}
074 *     <li>{@link com.nimbusds.jose.EncryptionMethod#XC20P}
075 * </ul>
076 *
077 * @author Alexander Martynov
078 * @version 2021-08-18
079 */
080@ThreadSafe
081public class ECDHEncrypterMulti extends ECDHCryptoProvider implements JWEEncrypterMulti {
082
083    /**
084     * The supported EC JWK curves by the ECDH crypto provider class.
085     */
086    public static final Set<Curve> SUPPORTED_ELLIPTIC_CURVES;
087
088
089    static {
090        Set<Curve> curves = new LinkedHashSet<>();
091        curves.add(Curve.P_256);
092        curves.add(Curve.P_384);
093        curves.add(Curve.P_521);
094        SUPPORTED_ELLIPTIC_CURVES = Collections.unmodifiableSet(curves);
095    }
096
097    /**
098     * The list of public recipient's keys.
099     */
100    private final List<Pair<UnprotectedHeader, ECKey>> recipients;
101
102    /**
103     * Creates Elliptic Curve Diffie-Hellman Multi-recipient encryptor.
104     *
105     * @param recipients The list of public recipient's keys.
106     *
107     * @throws JOSEException If the key subtype is not supported.
108     */
109    public ECDHEncrypterMulti(final List<Pair<UnprotectedHeader, ECKey>> recipients)
110        throws JOSEException {
111
112        super(recipients.get(0).getRight().getCurve());
113
114        this.recipients = recipients;
115    }
116
117    @Override
118    public Set<Curve> supportedEllipticCurves() {
119        return SUPPORTED_ELLIPTIC_CURVES;
120    }
121
122    @Override
123    public JWECryptoParts encrypt(JWEHeader header, byte[] clearText) throws JOSEException {
124        // Generate ephemeral EC key pair on the same curve as the consumer's public key
125        ECKey ephemeralKeyPair = new ECKeyGenerator(getCurve()).generate();
126        ECPublicKey ephemeralPublicKey = ephemeralKeyPair.toECPublicKey();
127        ECPrivateKey ephemeralPrivateKey = ephemeralKeyPair.toECPrivateKey();
128
129        // Add the ephemeral public EC key to the header
130        JWEHeader updatedHeader = new JWEHeader.Builder(header).
131                ephemeralPublicKey(new ECKey.Builder(getCurve(), ephemeralPublicKey).build()).
132                build();
133
134        List<Pair<UnprotectedHeader, SecretKey>> sharedKeys = new ArrayList<>();
135
136        for (Pair<UnprotectedHeader, ECKey> recipient : recipients) {
137            SecretKey Z = ECDH.deriveSharedSecret(
138                    recipient.getRight().toECPublicKey(),
139                    ephemeralPrivateKey,
140                    getJCAContext().getKeyEncryptionProvider()
141            );
142
143            sharedKeys.add(Pair.of(recipient.getLeft(), Z));
144        }
145
146        return encryptMulti(updatedHeader, sharedKeys, clearText);
147    }
148
149}