Session getOpSession() throws PKCS11Exception { Session session = opSessions.poll(); if (session != null) { return ensureValid(session); } // create a new session rather than re-using an obj session // that avoids potential expensive cancels() for Signatures & RSACipher if (maxSessions == Integer.MAX_VALUE || activeSessions.get() < maxSessions) { session = openSession(); return ensureValid(session); } session = objSessions.poll(); if (session != null) { return ensureValid(session); } throw new ProviderException("Could not obtain session"); }
private Session openSession() throws PKCS11Exception { if ((maxSessions != Integer.MAX_VALUE) && (activeSessions.get() >= maxSessions)) { throw new ProviderException("No more sessions available"); } long id = token.p11.C_OpenSession (token.provider.slotID, openSessionFlags, null, null); Session session = new Session(token, id); activeSessions.incrementAndGet(); if (debug != null) { synchronized(this) { if (activeSessions.get() > maxActiveSessions) { maxActiveSessions = activeSessions.get(); if (maxActiveSessions % 10 == 0) { System.out.println("Open sessions: " + maxActiveSessions); } } } } return session; }
private Session openSession() throws PKCS11Exception { if ((maxSessions != Integer.MAX_VALUE) && (activeSessions.get() >= maxSessions)) { throw new ProviderException("No more sessions available"); } long id = token.p11.C_OpenSession (token.provider.slotID, openSessionFlags, null, null); Session session = new Session(token, id); activeSessions.incrementAndGet(); if (debug != null) { synchronized(maxActiveSessionsLock) { if (activeSessions.get() > maxActiveSessions) { maxActiveSessions = activeSessions.get(); if (maxActiveSessions % 10 == 0) { System.out.println("Open sessions: " + maxActiveSessions); } } } } return session; }
/** * Performs encryption operation. * * <p>The input plain text <code>plain</code>, starting at * <code>plainOffset</code> and ending at * <code>(plainOffset + plainLen - 1)</code>, is encrypted. * The result is stored in <code>cipher</code>, starting at * <code>cipherOffset</code>. * * @param plain the buffer with the input data to be encrypted * @param plainOffset the offset in <code>plain</code> * @param plainLen the length of the input data * @param cipher the buffer for the result * @param cipherOffset the offset in <code>cipher</code> * @exception ProviderException if <code>plainLen</code> is not * a multiple of the <code>numBytes</code> * @return the length of the encrypted data */ int encrypt(byte[] plain, int plainOffset, int plainLen, byte[] cipher, int cipherOffset) { if ((plainLen % numBytes) != 0) { throw new ProviderException("Internal error in input buffering"); } int nShift = blockSize - numBytes; int loopCount = plainLen / numBytes; for (; loopCount > 0; plainOffset += numBytes, cipherOffset += numBytes, loopCount--) { embeddedCipher.encryptBlock(register, 0, k, 0); for (int i = 0; i < numBytes; i++) { cipher[i + cipherOffset] = (byte)(k[i] ^ plain[i + plainOffset]); } if (nShift != 0) { System.arraycopy(register, numBytes, register, 0, nShift); } System.arraycopy(k, 0, register, nShift, numBytes); } return plainLen; }
/** * Performs encryption operation. * * <p>The input plain text <code>plain</code>, starting at * <code>plainOffset</code> and ending at * <code>(plainOffset + plainLen - 1)</code>, is encrypted. * The result is stored in <code>cipher</code>, starting at * <code>cipherOffset</code>. * * @param plain the buffer with the input data to be encrypted * @param plainOffset the offset in <code>plain</code> * @param plainLen the length of the input data * @param cipher the buffer for the result * @param cipherOffset the offset in <code>cipher</code> * @exception ProviderException if <code>plainLen</code> is not * a multiple of the block size * @return the length of the encrypted data */ int encrypt(byte[] plain, int plainOffset, int plainLen, byte[] cipher, int cipherOffset) { if ((plainLen % blockSize) != 0) { throw new ProviderException("Internal error in input buffering"); } int i; int endIndex = plainOffset + plainLen; for (; plainOffset < endIndex; plainOffset += blockSize, cipherOffset += blockSize) { for (i = 0; i < blockSize; i++) { k[i] ^= plain[i + plainOffset]; } embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset); for (i = 0; i < blockSize; i++) { k[i] = (byte)(plain[i + plainOffset] ^ cipher[i + cipherOffset]); } } return plainLen; }
/** * Performs decryption operation. * * <p>The input cipher text <code>cipher</code>, starting at * <code>cipherOffset</code> and ending at * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted. * The result is stored in <code>plain</code>, starting at * <code>plainOffset</code>. * * @param cipher the buffer with the input data to be decrypted * @param cipherOffset the offset in <code>cipherOffset</code> * @param cipherLen the length of the input data * @param plain the buffer for the result * @param plainOffset the offset in <code>plain</code> * @exception ProviderException if <code>cipherLen</code> is not * a multiple of the block size * @return the length of the decrypted data */ int decrypt(byte[] cipher, int cipherOffset, int cipherLen, byte[] plain, int plainOffset) { if ((cipherLen % blockSize) != 0) { throw new ProviderException("Internal error in input buffering"); } int i; int endIndex = cipherOffset + cipherLen; for (; cipherOffset < endIndex; plainOffset += blockSize, cipherOffset += blockSize) { embeddedCipher.decryptBlock(cipher, cipherOffset, plain, plainOffset); for (i = 0; i < blockSize; i++) { plain[i + plainOffset] ^= k[i]; } for (i = 0; i < blockSize; i++) { k[i] = (byte)(plain[i + plainOffset] ^ cipher[i + cipherOffset]); } } return cipherLen; }
/** * Performs encryption operation. * * <p>The input plain text <code>plain</code>, starting at * <code>plainOffset</code> and ending at * <code>(plainOffset + plainLen - 1)</code>, is encrypted. * The result is stored in <code>cipher</code>, starting at * <code>cipherOffset</code>. * * @param plain the buffer with the input data to be encrypted * @param plainOffset the offset in <code>plain</code> * @param plainLen the length of the input data * @param cipher the buffer for the result * @param cipherOffset the offset in <code>cipher</code> * @exception ProviderException if <code>plainLen</code> is not * a multiple of the <code>numBytes</code> * @return the length of the encrypted data */ int encrypt(byte[] plain, int plainOffset, int plainLen, byte[] cipher, int cipherOffset) { if ((plainLen % numBytes) != 0) { throw new ProviderException("Internal error in input buffering"); } int nShift = blockSize - numBytes; int loopCount = plainLen / numBytes; for (; loopCount > 0 ; plainOffset += numBytes, cipherOffset += numBytes, loopCount--) { embeddedCipher.encryptBlock(register, 0, k, 0); if (nShift != 0) { System.arraycopy(register, numBytes, register, 0, nShift); } for (int i = 0; i < numBytes; i++) { register[nShift + i] = cipher[i + cipherOffset] = (byte)(k[i] ^ plain[i + plainOffset]); } } return plainLen; }
/** * Performs decryption operation. * * <p>The input cipher text <code>cipher</code>, starting at * <code>cipherOffset</code> and ending at * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted. * The result is stored in <code>plain</code>, starting at * <code>plainOffset</code>. * * @param cipher the buffer with the input data to be decrypted * @param cipherOffset the offset in <code>cipherOffset</code> * @param cipherLen the length of the input data * @param plain the buffer for the result * @param plainOffset the offset in <code>plain</code> * @exception ProviderException if <code>cipherLen</code> is not * a multiple of the <code>numBytes</code> * @return the length of the decrypted data */ int decrypt(byte[] cipher, int cipherOffset, int cipherLen, byte[] plain, int plainOffset) { if ((cipherLen % numBytes) != 0) { throw new ProviderException("Internal error in input buffering"); } int nShift = blockSize - numBytes; int loopCount = cipherLen / numBytes; for (; loopCount > 0; plainOffset += numBytes, cipherOffset += numBytes, loopCount--) { embeddedCipher.encryptBlock(register, 0, k, 0); if (nShift != 0) { System.arraycopy(register, numBytes, register, 0, nShift); } for (int i = 0; i < numBytes; i++) { register[i + nShift] = cipher[i + cipherOffset]; plain[i + plainOffset] = (byte)(cipher[i + cipherOffset] ^ k[i]); } } return cipherLen; }
/** * Performs encryption operation. * * <p>The input plain text <code>plain</code>, starting at * <code>plainOffset</code> and ending at * <code>(plainOffset + plainLen - 1)</code>, is encrypted. * The result is stored in <code>cipher</code>, starting at * <code>cipherOffset</code>. * * @param plain the buffer with the input data to be encrypted * @param plainOffset the offset in <code>plain</code> * @param plainLen the length of the input data * @param cipher the buffer for the result * @param cipherOffset the offset in <code>cipher</code> * @exception ProviderException if <code>len</code> is not * a multiple of the block size * @return the length of the encrypted data */ int encrypt(byte[] plain, int plainOffset, int plainLen, byte[] cipher, int cipherOffset) { if ((plainLen % blockSize) != 0) { throw new ProviderException("Internal error in input buffering"); } int endIndex = plainOffset + plainLen; for (; plainOffset < endIndex; plainOffset+=blockSize, cipherOffset += blockSize) { for (int i = 0; i < blockSize; i++) { k[i] = (byte)(plain[i + plainOffset] ^ r[i]); } embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset); System.arraycopy(cipher, cipherOffset, r, 0, blockSize); } return plainLen; }
static void test(String algo, Provider provider, boolean priv, Consumer<Key> method) throws Exception { KeyPairGenerator generator; try { generator = KeyPairGenerator.getInstance(algo, provider); } catch (NoSuchAlgorithmException nsae) { return; } System.out.println("Checking " + provider.getName() + ", " + algo); KeyPair pair = generator.generateKeyPair(); Key key = priv ? pair.getPrivate() : pair.getPublic(); pair = null; for (int i = 0; i < 32; ++i) { System.gc(); } try { method.accept(key); } catch (ProviderException pe) { failures++; } }
@Override public Object newInstance(Object ctrParamObj) throws NoSuchAlgorithmException { String type = getType(); if (ctrParamObj != null) { throw new InvalidParameterException ("constructorParameter not used with " + type + " engines"); } String algo = getAlgorithm(); try { if (type.equals("GssApiMechanism")) { if (algo.equals("1.2.840.113554.1.2.2")) { return new Krb5MechFactory(); } else if (algo.equals("1.3.6.1.5.5.2")) { return new SpNegoMechFactory(); } } } catch (Exception ex) { throw new NoSuchAlgorithmException ("Error constructing " + type + " for " + algo + " using SunJGSS", ex); } throw new ProviderException("No impl for " + algo + " " + type); }
@Override public Object newInstance(Object ctrParamObj) throws NoSuchAlgorithmException { String type = getType(); if (ctrParamObj != null) { throw new InvalidParameterException ("constructorParameter not used with " + type + " engines"); } String algo = getAlgorithm(); // GSSAPI uses same impl class for client and server try { if (algo.equals("GSSAPI")) { return new com.sun.security.sasl.gsskerb.FactoryImpl(); } } catch (Exception ex) { throw new NoSuchAlgorithmException("Error constructing " + type + " for " + algo + " using JdkSASL", ex); } throw new ProviderException("No impl for " + algo + " " + type); }
/** {@inheritDoc} */ @Override public Key engineGetKey(final String alias, final char[] password) { if (!engineContainsAlias(alias)) { return null; } // No permitimos PIN nulo, si llega nulo pedimos por dialogo grafico if (password != null) { this.cryptoCard.setPasswordCallback( new CachePasswordCallback(password) ); } final PrivateKeyReference pkRef = this.cryptoCard.getPrivateKey(alias); if (!(pkRef instanceof CeresPrivateKeyReference)) { throw new ProviderException("La clave obtenida de la tarjeta no es del tipo esperado, se ha obtenido: " + pkRef.getClass().getName()); //$NON-NLS-1$ } return new CeresPrivateKey( (CeresPrivateKeyReference) pkRef, this.cryptoCard, ((RSAPublicKey)engineGetCertificate(alias).getPublicKey()).getModulus() ); }
/** {@inheritDoc} */ @Override public Key engineGetKey(final String alias, final char[] password) { if (!engineContainsAlias(alias)) { return null; } if (password != null) { // Establecemos el PasswordCallback final PasswordCallback pwc = new CachePasswordCallback(password); this.cryptoCard.setPasswordCallback(pwc); } final PrivateKeyReference pkRef = this.cryptoCard.getPrivateKey(alias); if (!(pkRef instanceof DniePrivateKeyReference)) { throw new ProviderException( "La clave obtenida de la tarjeta no es del tipo esperado, se ha obtenido: " + pkRef.getClass().getName() //$NON-NLS-1$ ); } return new DniePrivateKey((DniePrivateKeyReference) pkRef); }
/** * Performs encryption operation. * * <p>The input plain text <code>plain</code>, starting at * <code>plainOffset</code> and ending at * <code>(plainOffset + plainLen - 1)</code>, is encrypted. * The result is stored in <code>cipher</code>, starting at * <code>cipherOffset</code>. * * @param plain the buffer with the input data to be encrypted * @param plainOffset the offset in <code>plain</code> * @param plainLen the length of the input data * @param cipher the buffer for the result * @param cipherOffset the offset in <code>cipher</code> * @exception ProviderException if <code>plainLen</code> is not * a multiple of the <code>numBytes</code> * @return the length of the encrypted data */ int encrypt(byte[] plain, int plainOffset, int plainLen, byte[] cipher, int cipherOffset) { if ((plainLen % numBytes) != 0) { throw new ProviderException("Internal error in input buffering"); } int nShift = blockSize - numBytes; int loopCount = plainLen / numBytes; for (; loopCount > 0; plainOffset += numBytes, cipherOffset += numBytes, loopCount--) { embeddedCipher.encryptBlock(register, 0, k, 0); for (int i = 0; i < numBytes; i++) { cipher[i + cipherOffset] = (byte)(k[i] ^ plain[i + plainOffset]); if (nShift != 0) { System.arraycopy(register, numBytes, register, 0, nShift); } System.arraycopy(k, 0, register, nShift, numBytes); } } return plainLen; }
/** * Returns the public exponent. */ public BigInteger getPublicExponent() { if (exponent == null) { try { publicKeyBlob = getPublicKeyBlob(hCryptKey); exponent = new BigInteger(1, getExponent(publicKeyBlob)); } catch (KeyException e) { throw new ProviderException(e); } } return exponent; }
/** * Returns the modulus. */ public BigInteger getModulus() { if (modulus == null) { try { publicKeyBlob = getPublicKeyBlob(hCryptKey); modulus = new BigInteger(1, getModulus(publicKeyBlob)); } catch (KeyException e) { throw new ProviderException(e); } } return modulus; }
protected static LdapClient getTestInstance(String applicationQualifier, LdapInstance ldapInstance, DirContextPool contextPool, Properties properties) { try { if (!applicationQualifier.matches(LdapClient.PATTERN_APPLICATION_QUALIFIER)) { throw new IllegalArgumentException("applicationQualifier (" + applicationQualifier + ") does not match pattern " + LdapClient.PATTERN_APPLICATION_QUALIFIER); } EnumDeclaredProperties baseProps = new EnumDeclaredProperties(properties); List<DirContextPool> contextPools = new ArrayList<DirContextPool>(); contextPools.add(contextPool); return new LdapClient(baseProps, contextPools, LDAP_DESCRIPTORS.get(ldapInstance)); } catch (Exception e) { throw new ProviderException(e); } }
/** * ctor given the prefactor and the basis of the root. * This creates an object of value a*sqrt(b). * * @param a * the prefactor. * @param b * the discriminant. * @since 2011-02-12 */ public BigSurd(Rational a, Rational b) { pref = a; /* * reject attempts to use a negative b */ if (b.signum() < 0) { throw new ProviderException("Not implemented: imaginary surds"); } disc = b; try { normalize(); normalizeG(); } catch (final Error e) { e.printStackTrace(); } }