1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
|
|
| 3.2% |
Uncovered Elements: 60 (62) |
Complexity: 20 |
Complexity Density: 0.59 |
|
54 |
|
public class DefaultKeyFactory extends AbstractBcKeyFactory |
55 |
|
{ |
56 |
|
@Inject |
57 |
|
private ComponentManager manager; |
58 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
59 |
0 |
@Override... |
60 |
|
protected AsymmetricKeyInfoConverter getKeyInfoConverter() |
61 |
|
{ |
62 |
0 |
throw new UnsupportedOperationException("Unexpected illegal internal call"); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
65 |
14 |
@Override... |
66 |
|
protected String checkKeyType(BcAsymmetricKeyParameters key) |
67 |
|
{ |
68 |
|
|
69 |
14 |
return null; |
70 |
|
} |
71 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
0 |
private AsymmetricKeyFactory getKeyFactory(ASN1Object keyInfo)... |
73 |
|
{ |
74 |
0 |
return getKeyFactory(getKeyFactoryHint(keyInfo)); |
75 |
|
} |
76 |
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 8 |
Complexity Density: 0.53 |
|
77 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
103 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
114 |
0 |
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 |
|
|
125 |
|
|
126 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
127 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
139 |
0 |
@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 |
|
} |