use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class ExtendedResult method readExtendedResultFrom.
/**
* Creates a new extended result object with the provided message ID and with
* the protocol op and controls read from the given ASN.1 stream reader.
*
* @param messageID The LDAP message ID for the LDAP message that is
* associated with this extended result.
* @param messageSequence The ASN.1 stream reader sequence used in the
* course of reading the LDAP message elements.
* @param reader The ASN.1 stream reader from which to read the
* protocol op and controls.
*
* @return The decoded extended result.
*
* @throws LDAPException If a problem occurs while reading or decoding data
* from the ASN.1 stream reader.
*/
@NotNull()
static ExtendedResult readExtendedResultFrom(final int messageID, @NotNull final ASN1StreamReaderSequence messageSequence, @NotNull final ASN1StreamReader reader) throws LDAPException {
try {
final ASN1StreamReaderSequence protocolOpSequence = reader.beginSequence();
final ResultCode resultCode = ResultCode.valueOf(reader.readEnumerated());
String matchedDN = reader.readString();
if (matchedDN.length() == 0) {
matchedDN = null;
}
String diagnosticMessage = reader.readString();
if (diagnosticMessage.length() == 0) {
diagnosticMessage = null;
}
String[] referralURLs = null;
String oid = null;
ASN1OctetString value = null;
while (protocolOpSequence.hasMoreElements()) {
final byte type = (byte) reader.peek();
switch(type) {
case TYPE_REFERRAL_URLS:
final ArrayList<String> refList = new ArrayList<>(1);
final ASN1StreamReaderSequence refSequence = reader.beginSequence();
while (refSequence.hasMoreElements()) {
refList.add(reader.readString());
}
referralURLs = new String[refList.size()];
refList.toArray(referralURLs);
break;
case TYPE_EXTENDED_RESPONSE_OID:
oid = reader.readString();
break;
case TYPE_EXTENDED_RESPONSE_VALUE:
value = new ASN1OctetString(type, reader.readBytes());
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_EXTENDED_RESULT_INVALID_ELEMENT.get(StaticUtils.toHex(type)));
}
}
Control[] controls = NO_CONTROLS;
if (messageSequence.hasMoreElements()) {
final ArrayList<Control> controlList = new ArrayList<>(1);
final ASN1StreamReaderSequence controlSequence = reader.beginSequence();
while (controlSequence.hasMoreElements()) {
controlList.add(Control.readFrom(reader));
}
controls = new Control[controlList.size()];
controlList.toArray(controls);
}
return new ExtendedResult(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs, oid, value, controls);
} catch (final LDAPException le) {
Debug.debugException(le);
throw le;
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_EXTENDED_RESULT_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class Filter method readFrom.
/**
* Reads and decodes a search filter from the provided ASN.1 stream reader.
*
* @param reader The ASN.1 stream reader from which to read the filter.
*
* @return The decoded search filter.
*
* @throws LDAPException If an error occurs while reading or parsing the
* search filter.
*/
@NotNull()
public static Filter readFrom(@NotNull final ASN1StreamReader reader) throws LDAPException {
try {
final Filter[] filterComps;
final Filter notComp;
final String attrName;
final ASN1OctetString assertionValue;
final ASN1OctetString subInitial;
final ASN1OctetString[] subAny;
final ASN1OctetString subFinal;
final String matchingRuleID;
final boolean dnAttributes;
final byte filterType = (byte) reader.peek();
switch(filterType) {
case FILTER_TYPE_AND:
case FILTER_TYPE_OR:
final ArrayList<Filter> comps = new ArrayList<>(5);
final ASN1StreamReaderSet elementSet = reader.beginSet();
while (elementSet.hasMoreElements()) {
comps.add(readFrom(reader));
}
filterComps = new Filter[comps.size()];
comps.toArray(filterComps);
notComp = null;
attrName = null;
assertionValue = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_NOT:
final ASN1Element notFilterElement;
try {
final ASN1Element e = reader.readElement();
notFilterElement = ASN1Element.decode(e.getValue());
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_CANNOT_DECODE_NOT_COMP.get(StaticUtils.getExceptionMessage(ae)), ae);
}
notComp = decode(notFilterElement);
filterComps = NO_FILTERS;
attrName = null;
assertionValue = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_EQUALITY:
case FILTER_TYPE_GREATER_OR_EQUAL:
case FILTER_TYPE_LESS_OR_EQUAL:
case FILTER_TYPE_APPROXIMATE_MATCH:
reader.beginSequence();
attrName = reader.readString();
assertionValue = new ASN1OctetString(reader.readBytes());
filterComps = NO_FILTERS;
notComp = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_SUBSTRING:
reader.beginSequence();
attrName = reader.readString();
ASN1OctetString tempSubInitial = null;
ASN1OctetString tempSubFinal = null;
final ArrayList<ASN1OctetString> subAnyList = new ArrayList<>(1);
final ASN1StreamReaderSequence subSequence = reader.beginSequence();
while (subSequence.hasMoreElements()) {
final byte type = (byte) reader.peek();
final ASN1OctetString s = new ASN1OctetString(type, reader.readBytes());
switch(type) {
case SUBSTRING_TYPE_SUBINITIAL:
tempSubInitial = s;
break;
case SUBSTRING_TYPE_SUBANY:
subAnyList.add(s);
break;
case SUBSTRING_TYPE_SUBFINAL:
tempSubFinal = s;
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_INVALID_SUBSTR_TYPE.get(StaticUtils.toHex(type)));
}
}
subInitial = tempSubInitial;
subFinal = tempSubFinal;
subAny = new ASN1OctetString[subAnyList.size()];
subAnyList.toArray(subAny);
filterComps = NO_FILTERS;
notComp = null;
assertionValue = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_PRESENCE:
attrName = reader.readString();
filterComps = NO_FILTERS;
notComp = null;
assertionValue = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_EXTENSIBLE_MATCH:
String tempAttrName = null;
ASN1OctetString tempAssertionValue = null;
String tempMatchingRuleID = null;
boolean tempDNAttributes = false;
final ASN1StreamReaderSequence emSequence = reader.beginSequence();
while (emSequence.hasMoreElements()) {
final byte type = (byte) reader.peek();
switch(type) {
case EXTENSIBLE_TYPE_ATTRIBUTE_NAME:
tempAttrName = reader.readString();
break;
case EXTENSIBLE_TYPE_MATCHING_RULE_ID:
tempMatchingRuleID = reader.readString();
break;
case EXTENSIBLE_TYPE_MATCH_VALUE:
tempAssertionValue = new ASN1OctetString(type, reader.readBytes());
break;
case EXTENSIBLE_TYPE_DN_ATTRIBUTES:
tempDNAttributes = reader.readBoolean();
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_EXTMATCH_INVALID_TYPE.get(StaticUtils.toHex(type)));
}
}
if ((tempAttrName == null) && (tempMatchingRuleID == null)) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_EXTMATCH_NO_ATTR_OR_MRID.get());
}
if (tempAssertionValue == null) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_EXTMATCH_NO_VALUE.get());
}
attrName = tempAttrName;
assertionValue = tempAssertionValue;
matchingRuleID = tempMatchingRuleID;
dnAttributes = tempDNAttributes;
filterComps = NO_FILTERS;
notComp = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_ELEMENT_INVALID_TYPE.get(StaticUtils.toHex(filterType)));
}
return new Filter(null, filterType, filterComps, notComp, attrName, assertionValue, subInitial, subAny, subFinal, matchingRuleID, dnAttributes);
} catch (final LDAPException le) {
Debug.debugException(le);
throw le;
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class IntermediateResponse method readFrom.
/**
* Creates a new intermediate response object with the provided message ID and
* with the protocol op and controls read from the given ASN.1 stream reader.
*
* @param messageID The LDAP message ID for the LDAP message that is
* associated with this intermediate response.
* @param messageSequence The ASN.1 stream reader sequence used in the
* course of reading the LDAP message elements.
* @param reader The ASN.1 stream reader from which to read the
* protocol op and controls.
*
* @return The decoded intermediate response.
*
* @throws LDAPException If a problem occurs while reading or decoding data
* from the ASN.1 stream reader.
*/
@NotNull()
static IntermediateResponse readFrom(final int messageID, @NotNull final ASN1StreamReaderSequence messageSequence, @NotNull final ASN1StreamReader reader) throws LDAPException {
try {
String oid = null;
ASN1OctetString value = null;
final ASN1StreamReaderSequence opSequence = reader.beginSequence();
while (opSequence.hasMoreElements()) {
final byte type = (byte) reader.peek();
switch(type) {
case TYPE_INTERMEDIATE_RESPONSE_OID:
oid = reader.readString();
break;
case TYPE_INTERMEDIATE_RESPONSE_VALUE:
value = new ASN1OctetString(type, reader.readBytes());
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_INTERMEDIATE_RESPONSE_INVALID_ELEMENT.get(StaticUtils.toHex(type)));
}
}
final Control[] controls;
if (messageSequence.hasMoreElements()) {
final ArrayList<Control> controlList = new ArrayList<>(1);
final ASN1StreamReaderSequence controlSequence = reader.beginSequence();
while (controlSequence.hasMoreElements()) {
controlList.add(Control.readFrom(reader));
}
controls = new Control[controlList.size()];
controlList.toArray(controls);
} else {
controls = NO_CONTROLS;
}
return new IntermediateResponse(messageID, oid, value, controls);
} catch (final LDAPException le) {
Debug.debugException(le);
throw le;
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_INTERMEDIATE_RESPONSE_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class Control method readFrom.
/**
* Reads an LDAP control from the provided ASN.1 stream reader.
*
* @param reader The ASN.1 stream reader from which to read the control.
*
* @return The decoded control.
*
* @throws LDAPException If a problem occurs while attempting to read or
* parse the control.
*/
@NotNull()
public static Control readFrom(@NotNull final ASN1StreamReader reader) throws LDAPException {
try {
final ASN1StreamReaderSequence controlSequence = reader.beginSequence();
final String oid = reader.readString();
boolean isCritical = false;
ASN1OctetString value = null;
while (controlSequence.hasMoreElements()) {
final byte type = (byte) reader.peek();
switch(type) {
case ASN1Constants.UNIVERSAL_BOOLEAN_TYPE:
isCritical = reader.readBoolean();
break;
case ASN1Constants.UNIVERSAL_OCTET_STRING_TYPE:
value = new ASN1OctetString(reader.readBytes());
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_CONTROL_INVALID_TYPE.get(StaticUtils.toHex(type)));
}
}
return decode(oid, isCritical, value);
} catch (final LDAPException le) {
Debug.debugException(le);
throw le;
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_CONTROL_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class LDAPConnectionReader method doStartTLS.
/**
* Converts this clear-text connection to one that uses TLS.
*
* @param sslSocketFactory The SSL socket factory to use to convert an
* insecure connection into a secure connection. It
* must not be {@code null}.
*
* @return The TLS-enabled output stream that may be used to send encrypted
* requests to the server.
*
* @throws LDAPException If a problem occurs while attempting to convert the
* connection to use TLS security.
*/
@NotNull()
OutputStream doStartTLS(@NotNull final SSLSocketFactory sslSocketFactory) throws LDAPException {
final LDAPConnectionOptions connectionOptions = connection.getConnectionOptions();
if (connection.synchronousMode()) {
try {
final int connectTimeout = connectionOptions.getConnectTimeoutMillis();
if (connectTimeout > 0) {
InternalSDKHelper.setSoTimeout(connection, connectTimeout);
} else {
InternalSDKHelper.setSoTimeout(connection, 0);
}
final SSLSocket sslSocket;
synchronized (sslSocketFactory) {
sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, connection.getConnectedAddress(), socket.getPort(), true);
sslSocket.startHandshake();
}
connectionOptions.getSSLSocketVerifier().verifySSLSocket(connection.getConnectedAddress(), socket.getPort(), sslSocket);
inputStream = new BufferedInputStream(sslSocket.getInputStream(), DEFAULT_INPUT_BUFFER_SIZE);
asn1StreamReader = new ASN1StreamReader(inputStream, connectionOptions.getMaxMessageSize());
startTLSOutputStream = sslSocket.getOutputStream();
socket = sslSocket;
connection.getConnectionInternals(true).setSocket(sslSocket);
final OutputStream outputStream = startTLSOutputStream;
startTLSOutputStream = null;
return outputStream;
} catch (final Exception e) {
Debug.debugException(e);
connection.setDisconnectInfo(DisconnectType.SECURITY_PROBLEM, StaticUtils.getExceptionMessage(e), e);
startTLSException = e;
closeRequested = true;
closeInternal(true, StaticUtils.getExceptionMessage(e));
throw new LDAPException(ResultCode.SERVER_DOWN, ERR_CONNREADER_STARTTLS_FAILED.get(StaticUtils.getExceptionMessage(e)), e);
}
} else {
this.sslSocketFactory = sslSocketFactory;
// Since the connection isn't operating in synchronous mode, we'll want to
// use a relatively small SO_TIMEOUT for the connection during this
// process so that it'll be more responsive. The original SO_TIMEOUT will
// be restored after the TLS negotiation.
final int originalSOTimeout = InternalSDKHelper.getSoTimeout(connection);
try {
InternalSDKHelper.setSoTimeout(connection, 50);
while (true) {
if (startTLSOutputStream != null) {
final OutputStream outputStream = startTLSOutputStream;
startTLSOutputStream = null;
return outputStream;
} else if (thread == null) {
if (startTLSException == null) {
throw new LDAPException(ResultCode.LOCAL_ERROR, ERR_CONNREADER_STARTTLS_FAILED_NO_EXCEPTION.get());
} else {
final Exception e = startTLSException;
startTLSException = null;
throw new LDAPException(ResultCode.LOCAL_ERROR, ERR_CONNREADER_STARTTLS_FAILED.get(StaticUtils.getExceptionMessage(e)), e);
}
}
startTLSSleeper.sleep(10);
}
} finally {
InternalSDKHelper.setSoTimeout(connection, originalSOTimeout);
}
}
}
Aggregations