1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.password.internal.pbe.factory

File BcPBES2CipherFactory.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

10
25
7
1
130
89
13
0.52
3.57
7
1.86

Classes

Class Line # Actions
BcPBES2CipherFactory 48 25 0% 13 17
0.595238159.5%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.crypto.password.internal.pbe.factory;
21   
22    import javax.inject.Inject;
23    import javax.inject.Singleton;
24   
25    import org.bouncycastle.asn1.ASN1Encodable;
26    import org.bouncycastle.asn1.ASN1Sequence;
27    import org.bouncycastle.asn1.pkcs.EncryptionScheme;
28    import org.bouncycastle.asn1.pkcs.KeyDerivationFunc;
29    import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
30    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.crypto.cipher.CipherFactory;
35    import org.xwiki.crypto.password.PasswordBasedCipher;
36    import org.xwiki.crypto.password.PasswordBasedCipherFactory;
37    import org.xwiki.crypto.password.internal.kdf.PBES2Parameters;
38    import org.xwiki.crypto.password.internal.pbe.RC5CBCParameter;
39   
40    /**
41    * Implement the parsing of PBES2 encryption scheme.
42    *
43    * @version $Id: bb875fb78b703ab9c20baa226a0f7b56c55cdd8c $
44    * @since 5.4M1
45    */
46    @Component(hints = { "PBES2", "1.2.840.113549.1.5.13" })
47    @Singleton
 
48    public class BcPBES2CipherFactory extends AbstractBcPBES2CipherFactory
49    {
50    private static final RuntimeException UNSUPPORTED =
51    new UnsupportedOperationException("Unexpected internal function call.");
52   
53    @Inject
54    private ComponentManager manager;
55   
 
56  0 toggle @Override
57    protected CipherFactory getCipherFactory()
58    {
59  0 throw UNSUPPORTED;
60    }
61   
 
62  6 toggle @Override
63    public PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, byte[] encoded)
64    {
65  6 ASN1Sequence seq = ASN1Sequence.getInstance(encoded);
66  6 AlgorithmIdentifier alg = getPBES2AlgorithmIdentifier(seq);
67   
68  6 PBES2Parameters params = PBES2Parameters.getInstance(alg.getParameters());
69  6 PasswordBasedCipherFactory pbecf = getPBES2CipherFactory(params.getEncryptionScheme());
70  6 PasswordBasedCipher cipher = getBcPBES2PasswordBasedCipher(pbecf, forEncryption, password, seq);
71   
72  6 if (cipher != null) {
73  6 return cipher;
74    }
75  0 return pbecf.getInstance(forEncryption, password, encoded);
76    }
77   
 
78  7 toggle @Override
79    public PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, ASN1Encodable parameters)
80    {
81  7 return getBcPBES2PasswordBasedCipher(
82    getPBES2CipherFactory(
83    PBES2Parameters.getInstance(
84    getPBES2AlgorithmIdentifier(parameters).getParameters()).getEncryptionScheme()),
85    forEncryption, password, parameters);
86    }
87   
 
88  13 toggle private AlgorithmIdentifier getPBES2AlgorithmIdentifier(ASN1Encodable parameters)
89    {
90  13 AlgorithmIdentifier alg = AlgorithmIdentifier.getInstance(parameters);
91   
92  13 if (!alg.getAlgorithm().equals(PKCSObjectIdentifiers.id_PBES2)) {
93  0 throw new IllegalArgumentException("Illegal algorithm identifier for PBES2: " + alg.getAlgorithm().getId());
94    }
95  13 return alg;
96    }
97   
 
98  13 toggle private PasswordBasedCipher getBcPBES2PasswordBasedCipher(PasswordBasedCipherFactory pbecf, boolean forEncryption,
99    byte[] password, ASN1Encodable parameters)
100    {
101  13 if (pbecf instanceof AbstractBcPBES2CipherFactory) {
102  13 return ((AbstractBcPBES2CipherFactory) pbecf).getInstance(forEncryption, password, parameters);
103    }
104  0 return null;
105    }
106   
 
107  0 toggle @Override
108    protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams,
109    EncryptionScheme scheme)
110    {
111    // Avoid spurious issues, this one should never be called anymore since the above one is overwritten.
112  0 throw UNSUPPORTED;
113    }
114   
 
115  13 toggle private PasswordBasedCipherFactory getPBES2CipherFactory(EncryptionScheme scheme)
116    {
117  13 try {
118  13 if (scheme.getAlgorithm().equals(PKCSObjectIdentifiers.encryptionAlgorithm.branch("9"))) {
119  0 RC5CBCParameter rc5Param = RC5CBCParameter.getInstance(scheme.getParameters());
120  0 if (rc5Param.getBlockSizeInBits().intValue() > 64) {
121    // RC5-CBC-Pad with a 128bits block size
122  0 return this.manager.getInstance(PasswordBasedCipherFactory.class, "PBES2-RC5-64-CBC-Pad");
123    }
124    }
125  13 return this.manager.getInstance(PasswordBasedCipherFactory.class, scheme.getAlgorithm().getId());
126    } catch (ComponentLookupException e) {
127  0 throw new UnsupportedOperationException("Password based cipher factory not found.", e);
128    }
129    }
130    }