/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2009 Sun Microsystems, Inc.
* Portions Copyright 2014-2015 ForgeRock AS
*/
/**
* Utility class used to decode LDAP messages from an ASN1Reader.
*/
public class LDAPReader
{
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP message.
*
* @param reader The ASN.1 reader.
*
* @return The decoded LDAP message.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* LDAP message.
*/
throws LDAPException
{
try
{
}
catch(Exception e)
{
}
int messageID;
try
{
}
catch(Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch(Exception e)
{
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP
* protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The LDAP protocol op decoded from the provided ASN.1 element.
*
* @throws LDAPException If a problem occurs while trying to decode the
* provided ASN.1 elements as an LDAP protocol op.
*/
throws LDAPException
{
byte type;
try
{
}
catch(Exception e)
{
}
switch(type)
{
case OP_TYPE_UNBIND_REQUEST: // 0x42
return readUnbindRequest(reader);
case 0x43: // 0x43
case 0x44: // 0x44
case 0x45: // 0x45
case 0x46: // 0x46
case 0x47: // 0x47
case 0x48: // 0x48
case 0x49: // 0x49
case OP_TYPE_DELETE_REQUEST: // 0x4A
return readDeleteRequest(reader);
case 0x4B: // 0x4B
case 0x4C: // 0x4C
case 0x4D: // 0x4D
case 0x4E: // 0x4E
case 0x4F: // 0x4F
message =
case OP_TYPE_ABANDON_REQUEST: // 0x50
return readAbandonRequest(reader);
case 0x51: // 0x51
case 0x52: // 0x52
case 0x53: // 0x53
case 0x54: // 0x54
case 0x55: // 0x55
case 0x56: // 0x56
case 0x57: // 0x57
case 0x58: // 0x58
case 0x59: // 0x59
case 0x5A: // 0x5A
case 0x5B: // 0x5B
case 0x5C: // 0x5C
case 0x5D: // 0x5D
case 0x5E: // 0x5E
case 0x5F: // 0x5F
message =
case OP_TYPE_BIND_REQUEST: // 0x60
return readBindRequest(reader);
case OP_TYPE_BIND_RESPONSE: // 0x61
return readBindResponse(reader);
case 0x62: // 0x62
message =
case OP_TYPE_SEARCH_REQUEST: // 0x63
return readSearchRequest(reader);
case OP_TYPE_SEARCH_RESULT_ENTRY: // 0x64
return readSearchEntry(reader);
case OP_TYPE_SEARCH_RESULT_DONE: // 0x65
return readSearchDone(reader);
case OP_TYPE_MODIFY_REQUEST: // 0x66
return readModifyRequest(reader);
case OP_TYPE_MODIFY_RESPONSE: // 0x67
return readModifyResponse(reader);
case OP_TYPE_ADD_REQUEST: // 0x68
return readAddRequest(reader);
case OP_TYPE_ADD_RESPONSE: // 0x69
return readAddResponse(reader);
case 0x6A: // 0x6A
message =
case OP_TYPE_DELETE_RESPONSE: // 0x6B
return readDeleteResponse(reader);
case OP_TYPE_MODIFY_DN_REQUEST: // 0x6C
return readModifyDNRequest(reader);
case OP_TYPE_MODIFY_DN_RESPONSE: // 0x6D
return readModifyDNResponse(reader);
case OP_TYPE_COMPARE_REQUEST: // 0x6E
return readCompareRequest(reader);
case OP_TYPE_COMPARE_RESPONSE: // 0x6F
return readCompareResponse(reader);
case 0x70: // 0x70
case 0x71: // 0x71
case 0x72: // 0x72
message =
case OP_TYPE_SEARCH_RESULT_REFERENCE: // 0x73
return readSearchReference(reader);
case 0x74: // 0x74
case 0x75: // 0x75
case 0x76: // 0x76
message =
case OP_TYPE_EXTENDED_REQUEST: // 0x77
return readExtendedRequest(reader);
case OP_TYPE_EXTENDED_RESPONSE: // 0x78
return readExtendedResponse(reader);
case OP_TYPE_INTERMEDIATE_RESPONSE: // 0x79
return
default:
message =
}
}
/**
* Decodes the elements from the provided ASN.1 read as an LDAP
* abandon request protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded abandon request protocol op.
*
* @throws LDAPException If the provided ASN.1 element cannot be decoded as
* an abandon request protocol op.
*/
throws LDAPException
{
long idToAbandon;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
return new AbandonRequestProtocolOp((int)idToAbandon);
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP
* add request protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded add request protocol op.
*
* @throws LDAPException If a problem occurs while decoding the provided
* ASN.1 element as an LDAP add request protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
attributes = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an
* add response protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded add response protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element to a protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int resultCode;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
}
else
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
errorMessage = null;
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if (reader.hasNextElement())
{
referralURLs = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 read as an LDAP bind
* request protocol op.
*
* @param reader The ASN.1 reader
*
* @return The decoded LDAP bind request protocol op.
*
* @throws LDAPException If a problem occurs while trying to decode the
* provided ASN.1 element as an LDAP bind request.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int protocolVersion;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
byte type;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
switch (type)
{
try
{
}
catch (Exception e)
{
}
break;
case TYPE_AUTHENTICATION_SASL:
try
{
if (reader.hasNextElement())
{
}
}
catch (Exception e)
{
}
break;
default:
type);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
if(type == TYPE_AUTHENTICATION_SIMPLE)
{
}
else
{
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a bind
* response protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded bind response protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element to a protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int resultCode;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
}
else
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
errorMessage = null;
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if(reader.hasNextElement() &&
{
try
{
referralURLs = new ArrayList<>();
// Should have at least 1.
do
{
}
while(reader.hasNextElement());
}
catch (Exception e)
{
logger.traceException(e);
}
}
if(reader.hasNextElement() &&
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
}
catch(IOException e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP
* compare request protocol op.
*
* @param reader The ASN.1 reader
*
* @return The decoded LDAP compare request protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element as a compare request protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a
* compare response protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded compare response protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element to a protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int resultCode;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
}
else
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
errorMessage = null;
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if (reader.hasNextElement())
{
referralURLs = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP delete
* request protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded delete request protocol op.
*
* @throws LDAPException If the provided ASN.1 element cannot be decoded as
* an unbind request protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the provided ASN.1 element as a delete response protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded delete response protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element to a protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int resultCode;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
}
else
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
errorMessage = null;
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if (reader.hasNextElement())
{
referralURLs = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an
* LDAP extended request protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded extended request protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* provided ASN.1 element as an LDAP extended request
* protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a
* extended response protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded extended response protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element to a protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int resultCode;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
}
else
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
errorMessage = null;
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if(reader.hasNextElement() &&
{
try
{
referralURLs = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
}
if(reader.hasNextElement() &&
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
if(reader.hasNextElement() &&
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
}
catch(IOException e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP
* intermediate response protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded intermediate response protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* provided ASN.1 element as an LDAP intermediate
* response protocol op.
*/
private static IntermediateResponseProtocolOp
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if(reader.hasNextElement() &&
{
try
{
if(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
e.getMessage());
}
}
if(reader.hasNextElement() &&
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
get(e.getMessage());
}
}
}
catch(IOException e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a
* modify DN request protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded modify DN request protocol op.
*
* @throws LDAPException If a problem occurs while trying to decode the
* provided ASN.1 element as an LDAP modify DN request
* protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
boolean deleteOldRDN;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a
* modify DN response protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded modify DN response protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element to a protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int resultCode;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
}
else
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
errorMessage = null;
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if (reader.hasNextElement())
{
referralURLs = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP
* modify request protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded modify request protocol op.
*
* @throws LDAPException If a problem occurs while decoding the provided
* ASN.1 element as an LDAP modify request protocol
* op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
modifications = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a modify
* response protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded modify response protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element to a protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int resultCode;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
}
else
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
errorMessage = null;
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if (reader.hasNextElement())
{
referralURLs = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP search
* request protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded LDAP search request protocol op.
*
* @throws LDAPException If a problem occurs while decoding the provided
* ASN.1 element as an LDAP search request protocol
* op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
switch (scopeValue)
{
case SCOPE_BASE_OBJECT:
break;
case SCOPE_SINGLE_LEVEL:
break;
case SCOPE_WHOLE_SUBTREE:
break;
break;
default:
}
}
catch (LDAPException le)
{
throw le;
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
switch (derefValue)
{
case DEREF_NEVER:
case DEREF_IN_SEARCHING:
case DEREF_FINDING_BASE:
case DEREF_ALWAYS:
break;
default:
}
}
catch (LDAPException le)
{
throw le;
}
catch (Exception e)
{
logger.traceException(e);
}
int sizeLimit;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int timeLimit;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
boolean typesOnly;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
attributes = new LinkedHashSet<>();
while(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a search
* result done protocol op.
*
* @param reader The ASN.1 reader
*
* @return The decoded search result done protocol op.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* ASN.1 element to a protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
int resultCode;
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
}
else
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
{
errorMessage = null;
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
if (reader.hasNextElement())
{
referralURLs = new ArrayList<>();
while(reader.hasNextElement())
{
}
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP search
* result entry protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded search result entry protocol op.
*
* @throws LDAPException If a problem occurs while decoding the provided
* ASN.1 element as an LDAP search result entry
* protocol op.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
attributes = new LinkedList<>();
while(reader.hasNextElement())
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a search
* result reference protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded search result reference protocol op.
*
* @throws LDAPException If a problem occurs while decoding the provided
* ASN.1 element as an LDAP search result reference
* protocol op.
*/
private static SearchResultReferenceProtocolOp
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
// Should have atleast 1 URL.
do
{
}
while(reader.hasNextElement());
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
return new SearchResultReferenceProtocolOp(referralURLs);
}
/**
* Decodes the elements from the provided ASN.1 read as an LDAP unbind
* request protocol op.
*
* @param reader The ASN.1 reader.
*
* @return The decoded LDAP unbind request protocol op.
*
* @throws LDAPException If the provided ASN.1 element cannot be decoded as
* an unbind request protocol op.
*/
throws LDAPException
{
try
{
return new UnbindRequestProtocolOp();
}
catch (Exception e)
{
logger.traceException(e);
}
}
/**
* Decodes the elements from the provided ASN.1 reader as a set of controls.
*
* @param reader The ASN.1 reader.
*
* @return The decoded set of controls.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* controls.
*/
throws LDAPException
{
try
{
while(reader.hasNextElement())
{
}
return controls;
}
catch (Exception e)
{
}
}
/**
* Decodes the elements from the provided ASN.1 reader as an LDAP control.
*
* @param reader The ASN.1 reader.
*
* @return The decoded LDAP control.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* provided ASN.1 element as an LDAP control.
*/
throws LDAPException
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
boolean isCritical = false;
try
{
if(reader.hasNextElement() &&
{
try
{
}
{
}
}
if(reader.hasNextElement() &&
{
try
{
}
{
}
}
}
catch(IOException e)
{
logger.traceException(e);
}
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
}