1 /* |
1 /* |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
29 import java.util.Arrays; |
29 import java.util.Arrays; |
30 import java.util.concurrent.ConcurrentSkipListSet; |
30 import java.util.concurrent.ConcurrentSkipListSet; |
31 import java.lang.ref.*; |
31 import java.lang.ref.*; |
32 |
32 |
33 import java.math.BigInteger; |
33 import java.math.BigInteger; |
34 import java.security.InvalidKeyException; |
34 import java.security.*; |
35 import java.security.NoSuchAlgorithmException; |
35 import java.security.interfaces.*; |
36 import java.security.Key; |
36 import java.security.spec.*; |
37 import java.security.PublicKey; |
|
38 import java.security.PrivateKey; |
|
39 import java.security.KeyFactorySpi; |
|
40 import java.security.interfaces.RSAPrivateCrtKey; |
|
41 import java.security.interfaces.RSAPublicKey; |
|
42 |
|
43 import java.security.spec.InvalidKeySpecException; |
|
44 import java.security.spec.KeySpec; |
|
45 import java.security.spec.RSAPrivateCrtKeySpec; |
|
46 import java.security.spec.RSAPublicKeySpec; |
|
47 |
37 |
48 /** |
38 /** |
49 * Wrapper class for native keys needed for using ucrypto APIs. |
39 * Wrapper class for native keys needed for using ucrypto APIs. |
50 * This class currently supports native RSA private/public keys. |
40 * This class currently supports native RSA private/public keys. |
51 * |
41 * |
83 byte[] newarray = new byte[n]; |
73 byte[] newarray = new byte[n]; |
84 System.arraycopy(b, 1, newarray, 0, n); |
74 System.arraycopy(b, 1, newarray, 0, n); |
85 b = newarray; |
75 b = newarray; |
86 } |
76 } |
87 return b; |
77 return b; |
|
78 } |
|
79 |
|
80 static final class RSAPrivate extends NativeKey implements RSAPrivateKey { |
|
81 |
|
82 private static final long serialVersionUID = 1622705588904302831L; |
|
83 |
|
84 private final RSAPrivateKeySpec keySpec; |
|
85 private final long keyId; |
|
86 |
|
87 RSAPrivate(KeySpec keySpec) throws InvalidKeySpecException { |
|
88 super(2); |
|
89 long pKey = 0L; |
|
90 if (keySpec instanceof RSAPrivateKeySpec) { |
|
91 RSAPrivateKeySpec ks = (RSAPrivateKeySpec) keySpec; |
|
92 BigInteger mod = ks.getModulus(); |
|
93 BigInteger privateExp = ks.getPrivateExponent(); |
|
94 pKey = nativeInit(NativeKey.getMagnitude(mod), |
|
95 NativeKey.getMagnitude(privateExp)); |
|
96 } else { |
|
97 throw new InvalidKeySpecException("Only supports RSAPrivateKeySpec"); |
|
98 } |
|
99 if (pKey == 0L) { |
|
100 throw new UcryptoException("Error constructing RSA PrivateKey"); |
|
101 } |
|
102 // track native resource clean up |
|
103 new KeyRef(this, pKey); |
|
104 this.keySpec = (RSAPrivateKeySpec) keySpec; |
|
105 this.keyId = pKey; |
|
106 } |
|
107 |
|
108 long value() { return keyId; } |
|
109 public BigInteger getModulus() { return keySpec.getModulus(); }; |
|
110 public BigInteger getPrivateExponent() { return keySpec.getPrivateExponent(); }; |
|
111 |
|
112 private native static long nativeInit(byte[] mod, byte[] privExp); |
88 } |
113 } |
89 |
114 |
90 static final class RSAPrivateCrt extends NativeKey implements RSAPrivateCrtKey { |
115 static final class RSAPrivateCrt extends NativeKey implements RSAPrivateCrtKey { |
91 |
116 |
92 private static final long serialVersionUID = 6812507588904302831L; |
117 private static final long serialVersionUID = 6812507588904302831L; |
117 NativeKey.getMagnitude(crtCoeff)); |
142 NativeKey.getMagnitude(crtCoeff)); |
118 } else { |
143 } else { |
119 throw new InvalidKeySpecException("Only supports RSAPrivateCrtKeySpec"); |
144 throw new InvalidKeySpecException("Only supports RSAPrivateCrtKeySpec"); |
120 } |
145 } |
121 if (pKey == 0L) { |
146 if (pKey == 0L) { |
122 throw new UcryptoException("Error constructing RSA PrivateKey"); |
147 throw new UcryptoException("Error constructing RSA PrivateCrtKey"); |
123 } |
148 } |
124 // track native resource clean up |
149 // track native resource clean up |
125 new KeyRef(this, pKey); |
150 new KeyRef(this, pKey); |
126 this.keySpec = (RSAPrivateCrtKeySpec) keySpec; |
151 this.keySpec = (RSAPrivateCrtKeySpec) keySpec; |
127 this.keyId = pKey; |
152 this.keyId = pKey; |