< prev index next >

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11DSAKeyFactory.java

Print this page




 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, attributes);
 224             } finally {
 225                 key.decNativeKeyRef();
 226             }
 227             KeySpec spec = new DSAPublicKeySpec(
 228                 attributes[0].getBigInteger(),
 229                 attributes[1].getBigInteger(),
 230                 attributes[2].getBigInteger(),
 231                 attributes[3].getBigInteger()
 232             );
 233             return keySpec.cast(spec);
 234         } else { // X.509 handled in superclass
 235             throw new InvalidKeySpecException("Only DSAPublicKeySpec and "
 236                 + "X509EncodedKeySpec supported for DSA public keys");
 237         }
 238     }
 239 
 240     <T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
 241             Session[] session) throws PKCS11Exception, InvalidKeySpecException {
 242         if (DSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
 243             session[0] = token.getObjSession();
 244             CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
 245                 new CK_ATTRIBUTE(CKA_VALUE),
 246                 new CK_ATTRIBUTE(CKA_PRIME),
 247                 new CK_ATTRIBUTE(CKA_SUBPRIME),
 248                 new CK_ATTRIBUTE(CKA_BASE),
 249             };
 250             key.incNativeKeyRef();
 251             try {
 252                 token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
 253             } finally {
 254                 key.decNativeKeyRef();
 255             }
 256             KeySpec spec = new DSAPrivateKeySpec(
 257                 attributes[0].getBigInteger(),
 258                 attributes[1].getBigInteger(),
 259                 attributes[2].getBigInteger(),
 260                 attributes[3].getBigInteger()
 261             );
 262             return keySpec.cast(spec);
 263         } else { // PKCS#8 handled in superclass
 264             throw new InvalidKeySpecException("Only DSAPrivateKeySpec "
 265                 + "and PKCS8EncodedKeySpec supported for DSA private keys");
 266         }
 267     }
 268 
 269     KeyFactory implGetSoftwareFactory() throws GeneralSecurityException {
 270         return KeyFactory.getInstance("DSA", P11Util.getSunProvider());
 271     }
 272 
 273 }
< prev index next >