Search in sources :

Example 1 with PK11PubKey

use of org.mozilla.jss.pkcs11.PK11PubKey in project jss by dogtagpki.

the class JSSCipherSpi method engineGetKeySize.

@Override
public int engineGetKeySize(Key key) throws InvalidKeyException {
    if (key instanceof PK11PrivKey) {
        return ((PK11PrivKey) key).getStrength();
    } else if (key instanceof PK11PubKey) {
        try {
            byte[] encoded = ((PK11PubKey) key).getEncoded();
            SubjectPublicKeyInfo.Template spkiTemp = new SubjectPublicKeyInfo.Template();
            SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) ASN1Util.decode(spkiTemp, encoded);
            BIT_STRING pk = spki.getSubjectPublicKey();
            return pk.getBits().length - pk.getPadCount();
        } catch (InvalidBERException e) {
            throw new InvalidKeyException("Exception while decoding " + "public key: " + e.getMessage());
        }
    } else if (key instanceof SecretKeyFacade) {
        SymmetricKey symkey = ((SecretKeyFacade) key).key;
        return symkey.getLength();
    } else {
        key = importKey(key);
        SymmetricKey symkey = ((SecretKeyFacade) key).key;
        return symkey.getLength();
    }
}
Also used : PK11PubKey(org.mozilla.jss.pkcs11.PK11PubKey) InvalidBERException(org.mozilla.jss.asn1.InvalidBERException) SecretKeyFacade(org.mozilla.jss.crypto.SecretKeyFacade) SymmetricKey(org.mozilla.jss.crypto.SymmetricKey) InvalidKeyException(java.security.InvalidKeyException) PK11PrivKey(org.mozilla.jss.pkcs11.PK11PrivKey) SubjectPublicKeyInfo(org.mozilla.jss.pkix.primitive.SubjectPublicKeyInfo) BIT_STRING(org.mozilla.jss.asn1.BIT_STRING)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)1 BIT_STRING (org.mozilla.jss.asn1.BIT_STRING)1 InvalidBERException (org.mozilla.jss.asn1.InvalidBERException)1 SecretKeyFacade (org.mozilla.jss.crypto.SecretKeyFacade)1 SymmetricKey (org.mozilla.jss.crypto.SymmetricKey)1 PK11PrivKey (org.mozilla.jss.pkcs11.PK11PrivKey)1 PK11PubKey (org.mozilla.jss.pkcs11.PK11PubKey)1 SubjectPublicKeyInfo (org.mozilla.jss.pkix.primitive.SubjectPublicKeyInfo)1