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

File DefaultKeyFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart1.png
82% of files have more coverage

Code metrics

20
34
8
1
150
100
20
0.59
4.25
8
2.5

Classes

Class Line # Actions
DefaultKeyFactory 54 34 0% 20 60
0.0322580643.2%
 

Contributing tests

This file is covered by 7 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.internal.asymmetric.keyfactory;
21   
22    import java.io.IOException;
23    import java.security.PrivateKey;
24    import java.security.PublicKey;
25   
26    import javax.inject.Inject;
27    import javax.inject.Singleton;
28   
29    import org.bouncycastle.asn1.ASN1Object;
30    import org.bouncycastle.asn1.ASN1ObjectIdentifier;
31    import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
32    import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
33    import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
34    import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
35    import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
36    import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
37    import org.bouncycastle.jcajce.provider.asymmetric.dsa.DSAUtil;
38    import org.bouncycastle.jcajce.provider.asymmetric.rsa.RSAUtil;
39    import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
40    import org.xwiki.component.annotation.Component;
41    import org.xwiki.component.manager.ComponentLookupException;
42    import org.xwiki.component.manager.ComponentManager;
43    import org.xwiki.crypto.AsymmetricKeyFactory;
44    import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters;
45   
46    /**
47    * Default asymmetric key factory.
48    *
49    * @version $Id: 226d2bc382bb504323b1baf11e9e0971052bda2c $
50    * @since 5.4M1
51    */
52    @Component
53    @Singleton
 
54    public class DefaultKeyFactory extends AbstractBcKeyFactory
55    {
56    @Inject
57    private ComponentManager manager;
58   
 
59  0 toggle @Override
60    protected AsymmetricKeyInfoConverter getKeyInfoConverter()
61    {
62  0 throw new UnsupportedOperationException("Unexpected illegal internal call");
63    }
64   
 
65  14 toggle @Override
66    protected String checkKeyType(BcAsymmetricKeyParameters key)
67    {
68    // Not key type check for this generic factory.
69  14 return null;
70    }
71   
 
72  0 toggle private AsymmetricKeyFactory getKeyFactory(ASN1Object keyInfo)
73    {
74  0 return getKeyFactory(getKeyFactoryHint(keyInfo));
75    }
76   
 
77  0 toggle private String getKeyFactoryHint(ASN1Object keyInfo)
78    {
79  0 ASN1ObjectIdentifier algId = getAlgorithmId(keyInfo);
80   
81  0 String hint = null;
82   
83  0 if (RSAUtil.isRsaOid(algId)) {
84  0 hint = "RSA";
85  0 } else if (DSAUtil.isDsaOid(algId)) {
86  0 hint = "DSA";
87  0 } else if (algId.equals(PKCSObjectIdentifiers.dhKeyAgreement)
88    || algId.equals(X9ObjectIdentifiers.dhpublicnumber)) {
89  0 hint = "DH";
90  0 } else if (algId.equals(OIWObjectIdentifiers.elGamalAlgorithm)) {
91  0 hint = "ElGamal";
92  0 } else if (algId.equals(CryptoProObjectIdentifiers.gostR3410_94)) {
93  0 hint = "GOST3410";
94    }
95   
96  0 if (hint == null) {
97  0 throw new UnsupportedOperationException("Asymmetric key algorithm not supported: " + algId.getId());
98    }
99   
100  0 return hint;
101    }
102   
 
103  0 toggle private ASN1ObjectIdentifier getAlgorithmId(ASN1Object keyInfo)
104    {
105  0 if (keyInfo instanceof PrivateKeyInfo) {
106  0 return ((PrivateKeyInfo) keyInfo).getPrivateKeyAlgorithm().getAlgorithm();
107  0 } else if (keyInfo instanceof SubjectPublicKeyInfo) {
108  0 return ((SubjectPublicKeyInfo) keyInfo).getAlgorithm().getAlgorithm();
109    } else {
110  0 throw new IllegalArgumentException("Asymmetric key expected but received: " + keyInfo.getClass().getName());
111    }
112    }
113   
 
114  0 toggle private AsymmetricKeyFactory getKeyFactory(String hint)
115    {
116  0 try {
117  0 return this.manager.getInstance(AsymmetricKeyFactory.class, hint);
118    } catch (ComponentLookupException e) {
119  0 throw new UnsupportedOperationException("Asymmetric key algorithm not found.", e);
120    }
121    }
122   
123    //
124    // AsymmetricKeyInfoConverter
125    //
126   
 
127  0 toggle @Override
128    public PrivateKey generatePrivate(PrivateKeyInfo privateKeyInfo) throws IOException
129    {
130  0 AsymmetricKeyFactory factory = getKeyFactory(privateKeyInfo);
131   
132  0 if (factory instanceof AsymmetricKeyInfoConverter) {
133  0 return ((AsymmetricKeyInfoConverter) factory).generatePrivate(privateKeyInfo);
134    }
135   
136  0 return factory.toKey(fromPKCS8(privateKeyInfo.getEncoded()));
137    }
138   
 
139  0 toggle @Override
140    public PublicKey generatePublic(SubjectPublicKeyInfo publicKeyInfo) throws IOException
141    {
142  0 AsymmetricKeyFactory factory = getKeyFactory(publicKeyInfo);
143   
144  0 if (factory instanceof AsymmetricKeyInfoConverter) {
145  0 return ((AsymmetricKeyInfoConverter) factory).generatePublic(publicKeyInfo);
146    }
147   
148  0 return factory.toKey(fromX509(publicKeyInfo.getEncoded()));
149    }
150    }