162 } 163 } 164 165 private PublicKey generatePublic(BigInteger y, BigInteger p, BigInteger q, 166 BigInteger g) throws PKCS11Exception { 167 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { 168 new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY), 169 new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA), 170 new CK_ATTRIBUTE(CKA_VALUE, y), 171 new CK_ATTRIBUTE(CKA_PRIME, p), 172 new CK_ATTRIBUTE(CKA_SUBPRIME, q), 173 new CK_ATTRIBUTE(CKA_BASE, g), 174 }; 175 attributes = token.getAttributes 176 (O_IMPORT, CKO_PUBLIC_KEY, CKK_DSA, attributes); 177 Session session = null; 178 try { 179 session = token.getObjSession(); 180 long keyID = token.p11.C_CreateObject(session.id(), attributes); 181 return P11Key.publicKey 182 (session, keyID, "DSA", p.bitLength(), attributes); 183 } finally { 184 token.releaseSession(session); 185 } 186 } 187 188 private PrivateKey generatePrivate(BigInteger x, BigInteger p, 189 BigInteger q, BigInteger g) throws PKCS11Exception { 190 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { 191 new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY), 192 new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA), 193 new CK_ATTRIBUTE(CKA_VALUE, x), 194 new CK_ATTRIBUTE(CKA_PRIME, p), 195 new CK_ATTRIBUTE(CKA_SUBPRIME, q), 196 new CK_ATTRIBUTE(CKA_BASE, g), 197 }; 198 attributes = token.getAttributes 199 (O_IMPORT, CKO_PRIVATE_KEY, CKK_DSA, attributes); 200 Session session = null; 201 try { 202 session = token.getObjSession(); 203 long keyID = token.p11.C_CreateObject(session.id(), attributes); 204 return P11Key.privateKey 205 (session, keyID, "DSA", p.bitLength(), attributes); 206 } finally { 207 token.releaseSession(session); 208 } 209 } 210 211 <T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec, 212 Session[] session) throws PKCS11Exception, InvalidKeySpecException { 213 if (DSAPublicKeySpec.class.isAssignableFrom(keySpec)) { 214 session[0] = token.getObjSession(); 215 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { 216 new CK_ATTRIBUTE(CKA_VALUE), 217 new CK_ATTRIBUTE(CKA_PRIME), 218 new CK_ATTRIBUTE(CKA_SUBPRIME), 219 new CK_ATTRIBUTE(CKA_BASE), 220 }; 221 token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes); 222 KeySpec spec = new DSAPublicKeySpec( 223 attributes[0].getBigInteger(), 224 attributes[1].getBigInteger(), 225 attributes[2].getBigInteger(), 226 attributes[3].getBigInteger() 227 ); 228 return keySpec.cast(spec); 229 } else { // X.509 handled in superclass 230 throw new InvalidKeySpecException("Only DSAPublicKeySpec and " 231 + "X509EncodedKeySpec supported for DSA public keys"); 232 } 233 } 234 235 <T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec, 236 Session[] session) throws PKCS11Exception, InvalidKeySpecException { 237 if (DSAPrivateKeySpec.class.isAssignableFrom(keySpec)) { 238 session[0] = token.getObjSession(); 239 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { 240 new CK_ATTRIBUTE(CKA_VALUE), 241 new CK_ATTRIBUTE(CKA_PRIME), 242 new CK_ATTRIBUTE(CKA_SUBPRIME), 243 new CK_ATTRIBUTE(CKA_BASE), 244 }; 245 token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes); 246 KeySpec spec = new DSAPrivateKeySpec( 247 attributes[0].getBigInteger(), 248 attributes[1].getBigInteger(), 249 attributes[2].getBigInteger(), 250 attributes[3].getBigInteger() 251 ); 252 return keySpec.cast(spec); 253 } else { // PKCS#8 handled in superclass 254 throw new InvalidKeySpecException("Only DSAPrivateKeySpec " 255 + "and PKCS8EncodedKeySpec supported for DSA private keys"); 256 } 257 } 258 259 KeyFactory implGetSoftwareFactory() throws GeneralSecurityException { 260 return KeyFactory.getInstance("DSA", P11Util.getSunProvider()); 261 } 262 263 } | 162 } 163 } 164 165 private PublicKey generatePublic(BigInteger y, BigInteger p, BigInteger q, 166 BigInteger g) throws PKCS11Exception { 167 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { 168 new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY), 169 new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA), 170 new CK_ATTRIBUTE(CKA_VALUE, y), 171 new CK_ATTRIBUTE(CKA_PRIME, p), 172 new CK_ATTRIBUTE(CKA_SUBPRIME, q), 173 new CK_ATTRIBUTE(CKA_BASE, g), 174 }; 175 attributes = token.getAttributes 176 (O_IMPORT, CKO_PUBLIC_KEY, CKK_DSA, attributes); 177 Session session = null; 178 try { 179 session = token.getObjSession(); 180 long keyID = token.p11.C_CreateObject(session.id(), attributes); 181 return P11Key.publicKey 182 (session, keyID, "DSA", p.bitLength(), attributes, true); 183 } finally { 184 token.releaseSession(session); 185 } 186 } 187 188 private PrivateKey generatePrivate(BigInteger x, BigInteger p, 189 BigInteger q, BigInteger g) throws PKCS11Exception { 190 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { 191 new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY), 192 new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA), 193 new CK_ATTRIBUTE(CKA_VALUE, x), 194 new CK_ATTRIBUTE(CKA_PRIME, p), 195 new CK_ATTRIBUTE(CKA_SUBPRIME, q), 196 new CK_ATTRIBUTE(CKA_BASE, g), 197 }; 198 attributes = token.getAttributes 199 (O_IMPORT, CKO_PRIVATE_KEY, CKK_DSA, attributes); 200 Session session = null; 201 try { 202 session = token.getObjSession(); 203 long keyID = token.p11.C_CreateObject(session.id(), attributes); 204 return P11Key.privateKey 205 (session, keyID, "DSA", p.bitLength(), attributes, true); 206 } finally { 207 token.releaseSession(session); 208 } 209 } 210 211 <T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec, 212 Session[] session) throws PKCS11Exception, InvalidKeySpecException { 213 if (DSAPublicKeySpec.class.isAssignableFrom(keySpec)) { 214 session[0] = token.getObjSession(); 215 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { 216 new CK_ATTRIBUTE(CKA_VALUE), 217 new CK_ATTRIBUTE(CKA_PRIME), 218 new CK_ATTRIBUTE(CKA_SUBPRIME), 219 new CK_ATTRIBUTE(CKA_BASE), 220 }; 221 key.incNativeKeyRef(); 222 try { 223 token.p11.C_GetAttributeValue(session[0].id(), key.keyID, 224 attributes); 225 } finally { 226 key.decNativeKeyRef(); 227 } 228 KeySpec spec = new DSAPublicKeySpec( 229 attributes[0].getBigInteger(), 230 attributes[1].getBigInteger(), 231 attributes[2].getBigInteger(), 232 attributes[3].getBigInteger() 233 ); 234 return keySpec.cast(spec); 235 } else { // X.509 handled in superclass 236 throw new InvalidKeySpecException("Only DSAPublicKeySpec and " 237 + "X509EncodedKeySpec supported for DSA public keys"); 238 } 239 } 240 241 <T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec, 242 Session[] session) throws PKCS11Exception, InvalidKeySpecException { 243 if (DSAPrivateKeySpec.class.isAssignableFrom(keySpec)) { 244 session[0] = token.getObjSession(); 245 CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { 246 new CK_ATTRIBUTE(CKA_VALUE), 247 new CK_ATTRIBUTE(CKA_PRIME), 248 new CK_ATTRIBUTE(CKA_SUBPRIME), 249 new CK_ATTRIBUTE(CKA_BASE), 250 }; 251 key.incNativeKeyRef(); 252 try { 253 token.p11.C_GetAttributeValue(session[0].id(), key.keyID, 254 attributes); 255 } finally { 256 key.decNativeKeyRef(); 257 } 258 KeySpec spec = new DSAPrivateKeySpec( 259 attributes[0].getBigInteger(), 260 attributes[1].getBigInteger(), 261 attributes[2].getBigInteger(), 262 attributes[3].getBigInteger() 263 ); 264 return keySpec.cast(spec); 265 } else { // PKCS#8 handled in superclass 266 throw new InvalidKeySpecException("Only DSAPrivateKeySpec " 267 + "and PKCS8EncodedKeySpec supported for DSA private keys"); 268 } 269 } 270 271 KeyFactory implGetSoftwareFactory() throws GeneralSecurityException { 272 return KeyFactory.getInstance("DSA", P11Util.getSunProvider()); 273 } 274 275 } |