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