3333import org .bouncycastle .operator .jcajce .JcaAlgorithmParametersConverter ;
3434import org .bouncycastle .util .Store ;
3535import org .eclipse .angus .mail .smtp .SMTPMessage ;
36+ import org .jetbrains .annotations .NotNull ;
3637import org .jetbrains .annotations .Nullable ;
3738
3839import javax .crypto .spec .OAEPParameterSpec ;
6061 */
6162public final class SmimeUtil {
6263
63- private static final String DEFAULT_SIGNATURE_ALGORITHM_NAME = "SHA256withRSA" ;
64- private static final KeyEncapsulationAlgorithm DEFAULT_KEY_ENCAPSULATION_ALGORITHM = KeyEncapsulationAlgorithm .RSA ;
65- private static final ASN1ObjectIdentifier DEFAULT_CIPHER = CMSAlgorithm .DES_EDE3_CBC ;
64+ /**
65+ * Unfortunately, these constants are not available in the Bouncy Castle, and they have to be passed as strings.
66+ *
67+ * @see org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder for a list of supported algorithms.
68+ */
69+ public static final String DEFAULT_SIGNATURE_ALGORITHM_NAME = "SHA256withRSA" ;
70+ public static final KeyEncapsulationAlgorithm DEFAULT_KEY_ENCAPSULATION_ALGORITHM = KeyEncapsulationAlgorithm .RSA ;
71+ public static final ASN1ObjectIdentifier DEFAULT_CIPHER = CMSAlgorithm .DES_EDE3_CBC ;
6672
6773 static {
6874 if (null == Security .getProvider (BouncyCastleProvider .PROVIDER_NAME )) {
@@ -190,25 +196,12 @@ private static void copyHeaders(Enumeration<Header> headers, MimeMessage toMessa
190196 private static SMIMEEnvelopedGenerator prepareGenerator (X509Certificate certificate ,
191197 KeyEncapsulationAlgorithm keyEncapsulationAlgorithm )
192198 throws CertificateEncodingException , InvalidAlgorithmParameterException {
193- JceKeyTransRecipientInfoGenerator infoGenerator ;
199+ final JceKeyTransRecipientInfoGenerator infoGenerator ;
194200 if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA ) {
195201 infoGenerator = new JceKeyTransRecipientInfoGenerator (certificate );
196202 } else {
197- String digestName ;
198- if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA_OAEP_SHA224 ) {
199- digestName = "SHA-234" ;
200- } else if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA_OAEP_SHA256 ) {
201- digestName = "SHA-256" ;
202- } else if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA_OAEP_SHA384 ) {
203- digestName = "SHA-384" ;
204- } else if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA_OAEP_SHA512 ) {
205- digestName = "SHA-512" ;
206- } else {
207- throw new InvalidAlgorithmParameterException ("Unknown S/MIME key encapsulation algorithm: "
208- + keyEncapsulationAlgorithm .name ());
209- }
210- JcaAlgorithmParametersConverter paramsConverter = new JcaAlgorithmParametersConverter ();
211- AlgorithmIdentifier oaepParams = paramsConverter .getAlgorithmIdentifier (
203+ String digestName = determineDigestName (keyEncapsulationAlgorithm );
204+ AlgorithmIdentifier oaepParams = new JcaAlgorithmParametersConverter ().getAlgorithmIdentifier (
212205 PKCSObjectIdentifiers .id_RSAES_OAEP , new OAEPParameterSpec (
213206 digestName , "MGF1" , new MGF1ParameterSpec (digestName ), PSource .PSpecified .DEFAULT ));
214207 infoGenerator = new JceKeyTransRecipientInfoGenerator (certificate , oaepParams );
@@ -219,6 +212,22 @@ PKCSObjectIdentifiers.id_RSAES_OAEP, new OAEPParameterSpec(
219212 return generator ;
220213 }
221214
215+ @ NotNull
216+ private static String determineDigestName (KeyEncapsulationAlgorithm keyEncapsulationAlgorithm ) throws InvalidAlgorithmParameterException {
217+ if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA_OAEP_SHA224 ) {
218+ return "SHA-234" ;
219+ } else if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA_OAEP_SHA256 ) {
220+ return "SHA-256" ;
221+ } else if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA_OAEP_SHA384 ) {
222+ return "SHA-384" ;
223+ } else if (keyEncapsulationAlgorithm == KeyEncapsulationAlgorithm .RSA_OAEP_SHA512 ) {
224+ return "SHA-512" ;
225+ } else {
226+ throw new InvalidAlgorithmParameterException ("Unknown S/MIME key encapsulation algorithm: "
227+ + keyEncapsulationAlgorithm .name ());
228+ }
229+ }
230+
222231 private static OutputEncryptor prepareEncryptor (ASN1ObjectIdentifier cmsAlgorithm ) throws CMSException {
223232 return new JceCMSContentEncryptorBuilder (cmsAlgorithm ).setProvider (BouncyCastleProvider .PROVIDER_NAME ).build ();
224233 }
0 commit comments