Search in sources :

Example 1 with NativeProxy

use of org.mozilla.jss.util.NativeProxy in project jss by dogtagpki.

the class PK11KeyWrapper method wrap.

@Override
public byte[] wrap(SymmetricKey toBeWrapped) throws InvalidKeyException, IllegalStateException, TokenException {
    if (state != WRAP) {
        throw new IllegalStateException();
    }
    if (algorithm == KeyWrapAlgorithm.PLAINTEXT) {
        throw new InvalidKeyException("plaintext wrapping not supported");
    }
    checkWrappee(toBeWrapped);
    if (symKey != null) {
        assert (privKey == null && pubKey == null);
        return nativeWrapSymWithSym(token, toBeWrapped, symKey, algorithm, IV);
    }
    assert (pubKey != null && privKey == null && symKey == null);
    NativeProxy params = null;
    long params_size = 0;
    if (parameters != null) {
        try {
            ((NativeEnclosure) parameters).open();
            params = ((NativeEnclosure) parameters).mPointer;
            params_size = ((NativeEnclosure) parameters).mPointerSize;
        } catch (Exception e) {
            throw new TokenException(e.getMessage(), e);
        }
    }
    try {
        return nativeWrapSymWithPub(token, toBeWrapped, pubKey, algorithm, params, params_size);
    } finally {
        if (parameters != null) {
            try {
                ((NativeEnclosure) parameters).close();
            } catch (Exception e) {
                throw new TokenException(e.getMessage(), e);
            }
        }
    }
}
Also used : NativeProxy(org.mozilla.jss.util.NativeProxy) NativeEnclosure(org.mozilla.jss.util.NativeEnclosure) TokenException(org.mozilla.jss.crypto.TokenException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TokenException(org.mozilla.jss.crypto.TokenException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with NativeProxy

use of org.mozilla.jss.util.NativeProxy in project jss by dogtagpki.

the class PK11KeyWrapper method unwrapSymmetric.

private SymmetricKey unwrapSymmetric(byte[] wrapped, SymmetricKey.Type type, int usageEnum, int keyLen) throws TokenException, IllegalStateException, InvalidAlgorithmParameterException {
    if (state != UNWRAP) {
        throw new IllegalStateException();
    }
    if ((!algorithm.isPadded()) && (type == SymmetricKey.RC4)) {
        if (keyLen <= 0) {
            throw new InvalidAlgorithmParameterException("RC4 keys wrapped in unpadded algorithms need key length" + " specified when unwrapping");
        }
    } else {
    // Don't use the key length
    // keyLen = 0;
    }
    /* Since we DONT want permanent,make the temporary arg true */
    boolean temporary = true;
    if (algorithm == KeyWrapAlgorithm.PLAINTEXT) {
        return nativeUnwrapSymPlaintext(token, wrapped, algFromType(type), usageEnum, temporary);
    }
    if (symKey != null) {
        assert (pubKey == null && privKey == null);
        return nativeUnwrapSymWithSym(token, symKey, wrapped, algorithm, algFromType(type), keyLen, IV, usageEnum, temporary);
    }
    assert (privKey != null && pubKey == null && symKey == null);
    NativeProxy params = null;
    long params_size = 0;
    if (parameters != null) {
        try {
            ((NativeEnclosure) parameters).open();
            params = ((NativeEnclosure) parameters).mPointer;
            params_size = ((NativeEnclosure) parameters).mPointerSize;
        } catch (Exception e) {
            throw new TokenException(e.getMessage(), e);
        }
    }
    try {
        return nativeUnwrapSymWithPriv(token, privKey, wrapped, algorithm, algFromType(type), keyLen, params, params_size, usageEnum);
    } finally {
        if (parameters != null) {
            try {
                ((NativeEnclosure) parameters).close();
            } catch (Exception e) {
                throw new TokenException(e.getMessage(), e);
            }
        }
    }
}
Also used : NativeProxy(org.mozilla.jss.util.NativeProxy) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NativeEnclosure(org.mozilla.jss.util.NativeEnclosure) TokenException(org.mozilla.jss.crypto.TokenException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TokenException(org.mozilla.jss.crypto.TokenException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 TokenException (org.mozilla.jss.crypto.TokenException)2 NativeEnclosure (org.mozilla.jss.util.NativeEnclosure)2 NativeProxy (org.mozilla.jss.util.NativeProxy)2