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.OctetKeyPair; 025import com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator; 026import com.nimbusds.jose.util.Pair; 027import net.jcip.annotations.ThreadSafe; 028 029import javax.crypto.SecretKey; 030import java.util.*; 031 032 033/** 034 * Elliptic Curve Diffie-Hellman Multi-recipient encrypter of 035 * {@link JWEObjectJSON JWE objects} for curves using EC JWK keys. 036 * Expects a public EC key (with a P-256, P-384, or P-521 curve). 037 * 038 * <p>Public Key Authenticated Encryption for JOSE 039 * <a href="https://datatracker.ietf.org/doc/html/draft-madden-jose-ecdh-1pu-04">ECDH-1PU</a> 040 * for more information. 041 * 042 * <p>This class is thread-safe. 043 * 044 * <p>Supports the following key management algorithms: 045 * 046 * <ul> 047 * <li>{@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES} 048 * <li>{@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A128KW} 049 * <li>{@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A192KW} 050 * <li>{@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A256KW} 051 * </ul> 052 * 053 * <p>Supports the following elliptic curve: 054 * 055 * <ul> 056 * <li>{@link com.nimbusds.jose.jwk.Curve#X25519} (Curve25519) 057 * </ul> 058 * 059 * <p>Supports the following content encryption algorithms: 060 * 061 * <ul> 062 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256} 063 * <li>{@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384} 064 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512} 065 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128GCM} 066 * <li>{@link com.nimbusds.jose.EncryptionMethod#A192GCM} 067 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256GCM} 068 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED} 069 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED} 070 * <li>{@link com.nimbusds.jose.EncryptionMethod#XC20P} 071 * </ul> 072 * 073 * @author Alexander Martynov 074 * @version 2021-08-18 075 */ 076@ThreadSafe 077public class X25519EncrypterMulti extends ECDHCryptoProvider implements JWEEncrypterMulti { 078 079 /** 080 * The supported EC JWK curves by the ECDH crypto provider class. 081 */ 082 public static final Set<Curve> SUPPORTED_ELLIPTIC_CURVES; 083 084 085 static { 086 Set<Curve> curves = new LinkedHashSet<>(); 087 curves.add(Curve.X25519); 088 SUPPORTED_ELLIPTIC_CURVES = Collections.unmodifiableSet(curves); 089 } 090 091 /** 092 * The list of public recipient's keys. 093 */ 094 private final List<Pair<UnprotectedHeader, OctetKeyPair>> recipients; 095 096 /** 097 * Creates a curve x25519 Elliptic Curve Diffie-Hellman Multi-recipient encryptor. 098 * 099 * @param recipients The list of public recipient's keys. 100 * 101 * @throws JOSEException If the key subtype is not supported. 102 */ 103 public X25519EncrypterMulti(final List<Pair<UnprotectedHeader, OctetKeyPair>> recipients) 104 throws JOSEException { 105 106 super(recipients.get(0).getRight().getCurve()); 107 108 this.recipients = recipients; 109 } 110 111 @Override 112 public Set<Curve> supportedEllipticCurves() { 113 return SUPPORTED_ELLIPTIC_CURVES; 114 } 115 116 @Override 117 public JWECryptoParts encrypt(JWEHeader header, byte[] clearText) throws JOSEException { 118 // Generate ephemeral OctetKey key pair on the same curve as the consumer's public key 119 OctetKeyPair ephemeralPrivateKey = new OctetKeyPairGenerator(getCurve()).generate(); 120 OctetKeyPair ephemeralPublicKey = ephemeralPrivateKey.toPublicJWK(); 121 122 // Add the ephemeral public OctetKey key to the header 123 JWEHeader updatedHeader = new JWEHeader.Builder(header) 124 .ephemeralPublicKey(ephemeralPublicKey) 125 .build(); 126 127 List<Pair<UnprotectedHeader, SecretKey>> sharedKeys = new ArrayList<>(); 128 129 for (Pair<UnprotectedHeader, OctetKeyPair> recipient : recipients) { 130 SecretKey Z = ECDH.deriveSharedSecret( 131 recipient.getRight().toPublicJWK(), 132 ephemeralPrivateKey 133 ); 134 135 sharedKeys.add(Pair.of(recipient.getLeft(), Z)); 136 } 137 138 return encryptMulti(updatedHeader, sharedKeys, clearText); 139 } 140}