001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, 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.impl;
019
020
021import java.security.PrivateKey;
022import java.security.Provider;
023import java.security.interfaces.RSAPublicKey;
024import javax.crypto.Cipher;
025import javax.crypto.IllegalBlockSizeException;
026import javax.crypto.SecretKey;
027import javax.crypto.spec.SecretKeySpec;
028
029import com.nimbusds.jose.JOSEException;
030import com.nimbusds.jose.util.ByteUtils;
031import net.jcip.annotations.ThreadSafe;
032
033
034/**
035 * RSAES-PKCS1-V1_5 methods for Content Encryption Key (CEK) encryption and
036 * decryption. This class is thread-safe.
037 *
038 * @author Vladimir Dzhuvinov
039 * @version 2021-09-24
040 */
041@ThreadSafe
042public class RSA1_5 {
043
044
045        /**
046         * Encrypts the specified Content Encryption Key (CEK).
047         *
048         * @param pub      The public RSA key. Must not be {@code null}.
049         * @param cek      The Content Encryption Key (CEK) to encrypt. Must
050         *                 not be {@code null}.
051         * @param provider The JCA provider, {@code null} to use the default.
052         *
053         * @return The encrypted Content Encryption Key (CEK).
054         *
055         * @throws JOSEException If encryption failed.
056         */
057        public static byte[] encryptCEK(final RSAPublicKey pub, final SecretKey cek, Provider provider)
058                throws JOSEException {
059
060                try {
061                        Cipher cipher = CipherHelper.getInstance("RSA/ECB/PKCS1Padding", provider);
062                        cipher.init(Cipher.ENCRYPT_MODE, pub);
063                        return cipher.doFinal(cek.getEncoded());
064                        
065                } catch (IllegalBlockSizeException e) {
066                        throw new JOSEException("RSA block size exception: The RSA key is too short, use a longer one", e);
067                } catch (Exception e) {
068                        // java.security.NoSuchAlgorithmException
069                        // java.security.InvalidKeyException
070                        throw new JOSEException("Couldn't encrypt Content Encryption Key (CEK): " + e.getMessage(), e);
071                }
072        }
073
074
075        /**
076         * Decrypts the specified encrypted Content Encryption Key (CEK).
077         *
078         * @param priv         The private RSA key. Must not be {@code null}.
079         * @param encryptedCEK The encrypted Content Encryption Key (CEK) to
080         *                     decrypt. Must not be {@code null}.
081         * @param provider     The JCA provider, {@code null} to use the
082         *                     default.
083         *
084         * @return The decrypted Content Encryption Key (CEK), {@code null} if
085         *         there was a CEK key length mismatch.
086         *
087         * @throws JOSEException If decryption failed.
088         */
089        public static SecretKey decryptCEK(final PrivateKey priv,
090                                           final byte[] encryptedCEK,
091                                           final int keyLength,
092                                           final Provider provider)
093                throws JOSEException {
094
095                try {
096                        Cipher cipher = CipherHelper.getInstance("RSA/ECB/PKCS1Padding", provider);
097                        cipher.init(Cipher.DECRYPT_MODE, priv);
098                        byte[] secretKeyBytes = cipher.doFinal(encryptedCEK);
099
100                        if (ByteUtils.safeBitLength(secretKeyBytes) != keyLength) {
101                                // CEK key length mismatch
102                                return null;
103                        }
104
105                        return new SecretKeySpec(secretKeyBytes, "AES");
106
107                } catch (Exception e) {
108
109                        // java.security.NoSuchAlgorithmException
110                        // java.security.InvalidKeyException
111                        // javax.crypto.IllegalBlockSizeException
112                        // javax.crypto.BadPaddingException
113                        throw new JOSEException("Couldn't decrypt Content Encryption Key (CEK): " + e.getMessage(), e);
114                }
115        }
116
117
118        /**
119         * Prevents public instantiation.
120         */
121        private RSA1_5() { }
122}