Search in sources :

Example 66 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class ManageCertificates method getKeystorePath.

/**
 * Retrieves the path to the target key store file.
 *
 * @param  keystoreArgumentName  The name of the argument used to specify the
 *                               path to the target key store.
 *
 * @return  The path to the target keystore file, or {@code null} if no
 *          keystore path was configured.
 */
@Nullable()
private File getKeystorePath(@NotNull final String keystoreArgumentName) {
    final FileArgument keystoreArgument = subCommandParser.getFileArgument(keystoreArgumentName);
    if ((keystoreArgument != null) && keystoreArgument.isPresent()) {
        return keystoreArgument.getValue();
    }
    final BooleanArgument useJVMDefaultTrustStoreArgument = subCommandParser.getBooleanArgument("useJVMDefaultTrustStore");
    if ((useJVMDefaultTrustStoreArgument != null) && useJVMDefaultTrustStoreArgument.isPresent()) {
        return JVM_DEFAULT_CACERTS_FILE;
    }
    return null;
}
Also used : BooleanArgument(com.unboundid.util.args.BooleanArgument) FileArgument(com.unboundid.util.args.FileArgument) Nullable(com.unboundid.util.Nullable)

Example 67 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class ManageCertificates method getIssuerCertificate.

/**
 * Attempts to retrieve the issuer certificate for the provided certificate
 * from the given keystore or the JVM-default trust store.
 *
 * @param  certificate              The certificate for which to retrieve the
 *                                  issuer certificate.
 * @param  keystore                 The keystore in which to look for the
 *                                  issuer certificate.
 * @param  jvmDefaultTrustStoreRef  A reference that will be used to hold the
 *                                  JVM-default trust store if it is obtained
 *                                  in the process of retrieving the issuer
 *                                  certificate.
 * @param  missingIssuerRef         A reference that will be updated with the
 *                                  DN of a missing issuer certificate, if any
 *                                  certificate in the chain cannot be
 *                                  located.  This must not be {@code null}.
 *
 * @return  The issuer certificate for the provided certificate, or
 *          {@code null} if the issuer certificate could not be retrieved.
 *
 * @throws  Exception   If a problem is encountered while trying to retrieve
 *                      the issuer certificate.
 */
@Nullable()
private static X509Certificate getIssuerCertificate(@NotNull final X509Certificate certificate, @NotNull final KeyStore keystore, @NotNull final AtomicReference<KeyStore> jvmDefaultTrustStoreRef, @NotNull final AtomicReference<DN> missingIssuerRef) throws Exception {
    final DN subjectDN = certificate.getSubjectDN();
    final DN issuerDN = certificate.getIssuerDN();
    if (subjectDN.equals(issuerDN)) {
        // This means that the certificate is self-signed, so there is no issuer.
        return null;
    }
    // See if we can find the issuer certificate in the provided keystore.
    X509Certificate issuerCertificate = getIssuerCertificate(certificate, keystore);
    if (issuerCertificate != null) {
        return issuerCertificate;
    }
    // See if we can get the JVM-default trust store.
    KeyStore jvmDefaultTrustStore = jvmDefaultTrustStoreRef.get();
    if (jvmDefaultTrustStore == null) {
        if (JVM_DEFAULT_CACERTS_FILE == null) {
            missingIssuerRef.set(issuerDN);
            return null;
        }
        final String[] keystoreTypes = { CryptoHelper.KEY_STORE_TYPE_JKS, CryptoHelper.KEY_STORE_TYPE_PKCS_12, BouncyCastleFIPSHelper.FIPS_KEY_STORE_TYPE };
        for (final String keystoreType : keystoreTypes) {
            final KeyStore ks = CryptoHelper.getKeyStore(keystoreType);
            try (FileInputStream inputStream = new FileInputStream(JVM_DEFAULT_CACERTS_FILE)) {
                ks.load(inputStream, null);
                jvmDefaultTrustStore = ks;
                jvmDefaultTrustStoreRef.set(jvmDefaultTrustStore);
                break;
            } catch (final Exception e) {
                Debug.debugException(e);
            }
        }
    }
    if (jvmDefaultTrustStore != null) {
        issuerCertificate = getIssuerCertificate(certificate, jvmDefaultTrustStore);
    }
    if (issuerCertificate == null) {
        missingIssuerRef.set(issuerDN);
    }
    return issuerCertificate;
}
Also used : DN(com.unboundid.ldap.sdk.DN) ASN1BitString(com.unboundid.asn1.ASN1BitString) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) ArgumentException(com.unboundid.util.args.ArgumentException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) Nullable(com.unboundid.util.Nullable)

Example 68 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class ModifyDNRequest method processAsync.

/**
 * Sends this modify DN request to the directory server over the provided
 * connection and returns the message ID for the request.
 *
 * @param  connection      The connection to use to communicate with the
 *                         directory server.
 * @param  resultListener  The async result listener that is to be notified
 *                         when the response is received.  It may be
 *                         {@code null} only if the result is to be processed
 *                         by this class.
 *
 * @return  The async request ID created for the operation, or {@code null} if
 *          the provided {@code resultListener} is {@code null} and the
 *          operation will not actually be processed asynchronously.
 *
 * @throws  LDAPException  If a problem occurs while sending the request.
 */
@Nullable()
AsyncRequestID processAsync(@NotNull final LDAPConnection connection, @Nullable final AsyncResultListener resultListener) throws LDAPException {
    // Create the LDAP message.
    messageID = connection.nextMessageID();
    final LDAPMessage message = new LDAPMessage(messageID, this, getControls());
    // If the provided async result listener is {@code null}, then we'll use
    // this class as the message acceptor.  Otherwise, create an async helper
    // and use it as the message acceptor.
    final AsyncRequestID asyncRequestID;
    final long timeout = getResponseTimeoutMillis(connection);
    if (resultListener == null) {
        asyncRequestID = null;
        connection.registerResponseAcceptor(messageID, this);
    } else {
        final AsyncHelper helper = new AsyncHelper(connection, OperationType.MODIFY_DN, messageID, resultListener, getIntermediateResponseListener());
        connection.registerResponseAcceptor(messageID, helper);
        asyncRequestID = helper.getAsyncRequestID();
        if (timeout > 0L) {
            final Timer timer = connection.getTimer();
            final AsyncTimeoutTimerTask timerTask = new AsyncTimeoutTimerTask(helper);
            timer.schedule(timerTask, timeout);
            asyncRequestID.setTimerTask(timerTask);
        }
    }
    // Send the request to the server.
    try {
        Debug.debugLDAPRequest(Level.INFO, this, messageID, connection);
        final LDAPConnectionLogger logger = connection.getConnectionOptions().getConnectionLogger();
        if (logger != null) {
            logger.logModifyDNRequest(connection, messageID, this);
        }
        connection.getConnectionStatistics().incrementNumModifyDNRequests();
        connection.sendMessage(message, timeout);
        return asyncRequestID;
    } catch (final LDAPException le) {
        Debug.debugException(le);
        connection.deregisterResponseAcceptor(messageID);
        throw le;
    }
}
Also used : Timer(java.util.Timer) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) Nullable(com.unboundid.util.Nullable)

Example 69 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class ModifyRequest method processAsync.

/**
 * Sends this modify request to the directory server over the provided
 * connection and returns the message ID for the request.
 *
 * @param  connection      The connection to use to communicate with the
 *                         directory server.
 * @param  resultListener  The async result listener that is to be notified
 *                         when the response is received.  It may be
 *                         {@code null} only if the result is to be processed
 *                         by this class.
 *
 * @return  The async request ID created for the operation, or {@code null} if
 *          the provided {@code resultListener} is {@code null} and the
 *          operation will not actually be processed asynchronously.
 *
 * @throws  LDAPException  If a problem occurs while sending the request.
 */
@Nullable()
AsyncRequestID processAsync(@NotNull final LDAPConnection connection, @Nullable final AsyncResultListener resultListener) throws LDAPException {
    // Create the LDAP message.
    messageID = connection.nextMessageID();
    final LDAPMessage message = new LDAPMessage(messageID, this, getControls());
    // If the provided async result listener is {@code null}, then we'll use
    // this class as the message acceptor.  Otherwise, create an async helper
    // and use it as the message acceptor.
    final AsyncRequestID asyncRequestID;
    final long timeout = getResponseTimeoutMillis(connection);
    if (resultListener == null) {
        asyncRequestID = null;
        connection.registerResponseAcceptor(messageID, this);
    } else {
        final AsyncHelper helper = new AsyncHelper(connection, OperationType.MODIFY, messageID, resultListener, getIntermediateResponseListener());
        connection.registerResponseAcceptor(messageID, helper);
        asyncRequestID = helper.getAsyncRequestID();
        if (timeout > 0L) {
            final Timer timer = connection.getTimer();
            final AsyncTimeoutTimerTask timerTask = new AsyncTimeoutTimerTask(helper);
            timer.schedule(timerTask, timeout);
            asyncRequestID.setTimerTask(timerTask);
        }
    }
    // Send the request to the server.
    try {
        Debug.debugLDAPRequest(Level.INFO, this, messageID, connection);
        final LDAPConnectionLogger logger = connection.getConnectionOptions().getConnectionLogger();
        if (logger != null) {
            logger.logModifyRequest(connection, messageID, this);
        }
        connection.getConnectionStatistics().incrementNumModifyRequests();
        connection.sendMessage(message, timeout);
        return asyncRequestID;
    } catch (final LDAPException le) {
        Debug.debugException(le);
        connection.deregisterResponseAcceptor(messageID);
        throw le;
    }
}
Also used : Timer(java.util.Timer) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) Nullable(com.unboundid.util.Nullable)

Example 70 with Nullable

use of com.unboundid.util.Nullable in project ldapsdk by pingidentity.

the class SearchRequest method processAsync.

/**
 * Sends this search request to the directory server over the provided
 * connection and returns the message ID for the request.
 *
 * @param  connection      The connection to use to communicate with the
 *                         directory server.
 * @param  resultListener  The async result listener that is to be notified
 *                         when the response is received.  It may be
 *                         {@code null} only if the result is to be processed
 *                         by this class.
 *
 * @return  The async request ID created for the operation, or {@code null} if
 *          the provided {@code resultListener} is {@code null} and the
 *          operation will not actually be processed asynchronously.
 *
 * @throws  LDAPException  If a problem occurs while sending the request.
 */
@Nullable()
AsyncRequestID processAsync(@NotNull final LDAPConnection connection, @Nullable final AsyncSearchResultListener resultListener) throws LDAPException {
    // Create the LDAP message.
    messageID = connection.nextMessageID();
    final LDAPMessage message = new LDAPMessage(messageID, this, getControls());
    // If the provided async result listener is {@code null}, then we'll use
    // this class as the message acceptor.  Otherwise, create an async helper
    // and use it as the message acceptor.
    final AsyncRequestID asyncRequestID;
    final long timeout = getResponseTimeoutMillis(connection);
    if (resultListener == null) {
        asyncRequestID = null;
        connection.registerResponseAcceptor(messageID, this);
    } else {
        final AsyncSearchHelper helper = new AsyncSearchHelper(connection, messageID, resultListener, getIntermediateResponseListener());
        connection.registerResponseAcceptor(messageID, helper);
        asyncRequestID = helper.getAsyncRequestID();
        if (timeout > 0L) {
            final Timer timer = connection.getTimer();
            final AsyncTimeoutTimerTask timerTask = new AsyncTimeoutTimerTask(helper);
            timer.schedule(timerTask, timeout);
            asyncRequestID.setTimerTask(timerTask);
        }
    }
    // Send the request to the server.
    try {
        Debug.debugLDAPRequest(Level.INFO, this, messageID, connection);
        final LDAPConnectionLogger logger = connection.getConnectionOptions().getConnectionLogger();
        if (logger != null) {
            logger.logSearchRequest(connection, messageID, this);
        }
        connection.getConnectionStatistics().incrementNumSearchRequests();
        connection.sendMessage(message, timeout);
        return asyncRequestID;
    } catch (final LDAPException le) {
        Debug.debugException(le);
        connection.deregisterResponseAcceptor(messageID);
        throw le;
    }
}
Also used : Timer(java.util.Timer) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) Nullable(com.unboundid.util.Nullable)

Aggregations

Nullable (com.unboundid.util.Nullable)149 ArrayList (java.util.ArrayList)47 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)42 Entry (com.unboundid.ldap.sdk.Entry)30 LDAPException (com.unboundid.ldap.sdk.LDAPException)30 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)21 Attribute (com.unboundid.ldap.sdk.Attribute)21 ASN1Element (com.unboundid.asn1.ASN1Element)20 Filter (com.unboundid.ldap.sdk.Filter)20 SearchResult (com.unboundid.ldap.sdk.SearchResult)18 IOException (java.io.IOException)16 ReadOnlyEntry (com.unboundid.ldap.sdk.ReadOnlyEntry)14 File (java.io.File)14 DN (com.unboundid.ldap.sdk.DN)12 ArgumentException (com.unboundid.util.args.ArgumentException)10 RDN (com.unboundid.ldap.sdk.RDN)9 LDIFException (com.unboundid.ldif.LDIFException)8 ChangeLogEntry (com.unboundid.ldap.sdk.ChangeLogEntry)7 Modification (com.unboundid.ldap.sdk.Modification)7 LDIFModifyChangeRecord (com.unboundid.ldif.LDIFModifyChangeRecord)7