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

File AbstractBcKeyFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

16
42
8
1
189
123
20
0.48
5.25
8
2.5

Classes

Class Line # Actions
AbstractBcKeyFactory 45 42 0% 20 20
0.696969769.7%
 

Contributing tests

This file is covered by 15 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 org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
27    import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
28    import org.bouncycastle.crypto.util.PrivateKeyFactory;
29    import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
30    import org.bouncycastle.crypto.util.PublicKeyFactory;
31    import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
32    import org.xwiki.crypto.AsymmetricKeyFactory;
33    import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters;
34    import org.xwiki.crypto.internal.asymmetric.BcPrivateKeyParameters;
35    import org.xwiki.crypto.internal.asymmetric.BcPublicKeyParameters;
36    import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters;
37    import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters;
38   
39    /**
40    * Abstract base class for asymmetric key factory using Bouncy Castle.
41    *
42    * @version $Id: fe2afa286ed25a9bfcc67381bbffce55613e48bc $
43    * @since 5.4M1
44    */
 
45    public abstract class AbstractBcKeyFactory implements AsymmetricKeyFactory, AsymmetricKeyInfoConverter
46    {
47    private static final String PRIVATE = "private";
48   
49    private static final String PUBLIC = "public";
50   
51    private static final String CLASS_ERROR = "Expected a %s %s key, but key class is %s.";
52   
53    private static final String ALGORITHM_ERROR = "Expected a %s %s key, but key algorithm is %s.";
54   
55    /**
56    * @return the appropriate BC converter to create JCA public and private key instance from key info.
57    */
58    protected abstract AsymmetricKeyInfoConverter getKeyInfoConverter();
59   
60    /**
61    * Check the type of the key parameter against the expected type for the current factory.
62    *
63    * @param key the parameters to check
64    * @return null if the parameter is of the expected type, else a string representing the expected type.
65    */
66    protected abstract String checkKeyType(BcAsymmetricKeyParameters key);
67   
68    //
69    // AsymmetricKeyFactory
70    //
71   
 
72  10 toggle @Override
73    public PublicKeyParameters fromX509(byte[] encoded) throws IOException
74    {
75  10 BcPublicKeyParameters key = new BcPublicKeyParameters(PublicKeyFactory.createKey(encoded));
76  10 String keyType = checkKeyType(key);
77  10 if (keyType != null) {
78  0 throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PUBLIC,
79    key.getParameters().getClass().getName()));
80    }
81  10 return key;
82    }
83   
 
84  26 toggle @Override
85    public PrivateKeyParameters fromPKCS8(byte[] encoded) throws IOException
86    {
87  26 BcPrivateKeyParameters key = new BcPrivateKeyParameters(PrivateKeyFactory.createKey(encoded));
88  26 String keyType = checkKeyType(key);
89  26 if (keyType != null) {
90  0 throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PRIVATE,
91    key.getParameters().getClass().getName()));
92    }
93  26 return key;
94    }
95   
 
96  2 toggle @Override
97    public PublicKeyParameters fromKey(PublicKey publicKey)
98    {
99  2 try {
100  2 BcPublicKeyParameters key = new BcPublicKeyParameters(PublicKeyFactory.createKey(publicKey.getEncoded()));
101  2 String keyType = checkKeyType(key);
102  2 if (keyType != null) {
103  0 throw new IllegalArgumentException(String.format(ALGORITHM_ERROR, keyType, PUBLIC,
104    publicKey.getAlgorithm()));
105    }
106  2 return key;
107    } catch (IOException e) {
108  0 throw new IllegalArgumentException("Invalid public key: " + publicKey.getClass().getName());
109    }
110    }
111   
 
112  2 toggle @Override
113    public PrivateKeyParameters fromKey(PrivateKey privateKey)
114    {
115  2 try {
116  2 BcPrivateKeyParameters key =
117    new BcPrivateKeyParameters(PrivateKeyFactory.createKey(privateKey.getEncoded()));
118  2 String keyType = checkKeyType(key);
119  2 if (keyType != null) {
120  0 throw new IllegalArgumentException(String.format(ALGORITHM_ERROR, keyType, PRIVATE,
121    privateKey.getAlgorithm()));
122    }
123  2 return key;
124    } catch (IOException e) {
125  0 throw new IllegalArgumentException("Invalid private key: " + privateKey.getClass().getName());
126    }
127    }
128   
 
129  2 toggle @Override
130    public PublicKey toKey(PublicKeyParameters key)
131    {
132  2 try {
133    // Optimization
134  2 if (key instanceof BcAsymmetricKeyParameters) {
135  2 String keyType = checkKeyType((BcAsymmetricKeyParameters) key);
136  2 if (keyType != null) {
137  0 throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PUBLIC,
138    key.getClass().getName()));
139    }
140   
141  2 return generatePublic(((BcPublicKeyParameters) key).getSubjectPublicKeyInfo());
142    }
143   
144    // Fallback
145  0 return generatePublic(SubjectPublicKeyInfo.getInstance(key.getEncoded()));
146    } catch (IOException e) {
147  0 throw new IllegalArgumentException("Invalid public key parameters: " + key.getClass().getName());
148    }
149    }
150   
 
151  2 toggle @Override
152    public PrivateKey toKey(PrivateKeyParameters key)
153    {
154  2 try {
155    // Optimization
156  2 if (key instanceof BcAsymmetricKeyParameters) {
157  2 String keyType = checkKeyType((BcAsymmetricKeyParameters) key);
158  2 if (keyType != null) {
159  0 throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PRIVATE,
160    key.getClass().getName()));
161    }
162   
163  2 return generatePrivate(PrivateKeyInfoFactory.createPrivateKeyInfo(
164    ((BcAsymmetricKeyParameters) key).getParameters()));
165    }
166   
167    // Fallback
168  0 return generatePrivate(PrivateKeyInfo.getInstance(key.getEncoded()));
169    } catch (IOException e) {
170  0 throw new IllegalArgumentException("Invalid private key parameters: " + key.getClass().getName());
171    }
172    }
173   
174    //
175    // AsymmetricKeyInfoConverter
176    //
177   
 
178  2 toggle @Override
179    public PrivateKey generatePrivate(PrivateKeyInfo privateKeyInfo) throws IOException
180    {
181  2 return getKeyInfoConverter().generatePrivate(privateKeyInfo);
182    }
183   
 
184  2 toggle @Override
185    public PublicKey generatePublic(SubjectPublicKeyInfo publicKeyInfo) throws IOException
186    {
187  2 return getKeyInfoConverter().generatePublic(publicKeyInfo);
188    }
189    }