Search in sources :

Example 1 with ASN1OctetString

use of org.webpki.asn1.ASN1OctetString in project xwiki-commons by xwiki.

the class BcPBES2AesCipherFactory method getInstance.

@Override
protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams, EncryptionScheme scheme) {
    KeyDerivationFunction kdf = getKeyDerivationFunction(kdfParams);
    // Set key size according to the encryption scheme algorithm used.
    kdf.overrideKeySize(getAESKeySize(scheme.getAlgorithm()));
    return getPasswordBasedCipher(forEncryption, kdf, new KeyWithIVParameters(kdf.derive(password).getKey(), ((ASN1OctetString) scheme.getParameters()).getOctets()));
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) KeyDerivationFunction(org.xwiki.crypto.password.KeyDerivationFunction) KeyWithIVParameters(org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters)

Example 2 with ASN1OctetString

use of org.webpki.asn1.ASN1OctetString in project eblocker by eblocker.

the class OcspCache method getOcspResponderUrl.

private String getOcspResponderUrl(X509Certificate certificate) throws IOException {
    byte[] extensionValue = certificate.getExtensionValue(Extension.authorityInfoAccess.getId());
    if (extensionValue == null) {
        return null;
    }
    ASN1OctetString octetString = ASN1OctetString.getInstance(extensionValue);
    ASN1Primitive primitive = ASN1Primitive.fromByteArray(octetString.getOctets());
    AuthorityInformationAccess authorityInformationAccessExtension = AuthorityInformationAccess.getInstance(primitive);
    for (AccessDescription i : authorityInformationAccessExtension.getAccessDescriptions()) {
        if (i.getAccessMethod().equals(OCSPObjectIdentifiers.id_pkix_ocsp)) {
            return i.getAccessLocation().getName().toString();
        }
    }
    return null;
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) AuthorityInformationAccess(org.bouncycastle.asn1.x509.AuthorityInformationAccess) AccessDescription(org.bouncycastle.asn1.x509.AccessDescription) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 3 with ASN1OctetString

use of org.webpki.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class ExtendedRequest method toCode.

/**
 * {@inheritDoc}
 */
@Override()
public void toCode(@NotNull final List<String> lineList, @NotNull final String requestID, final int indentSpaces, final boolean includeProcessing) {
    // Create the request variable.
    final ArrayList<ToCodeArgHelper> constructorArgs = new ArrayList<>(3);
    constructorArgs.add(ToCodeArgHelper.createString(oid, "Request OID"));
    constructorArgs.add(ToCodeArgHelper.createASN1OctetString(value, "Request Value"));
    final Control[] controls = getControls();
    if (controls.length > 0) {
        constructorArgs.add(ToCodeArgHelper.createControlArray(controls, "Request Controls"));
    }
    ToCodeHelper.generateMethodCall(lineList, indentSpaces, "ExtendedRequest", requestID + "Request", "new ExtendedRequest", constructorArgs);
    // Add lines for processing the request and obtaining the result.
    if (includeProcessing) {
        // Generate a string with the appropriate indent.
        final StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < indentSpaces; i++) {
            buffer.append(' ');
        }
        final String indent = buffer.toString();
        lineList.add("");
        lineList.add(indent + "try");
        lineList.add(indent + '{');
        lineList.add(indent + "  ExtendedResult " + requestID + "Result = connection.processExtendedOperation(" + requestID + "Request);");
        lineList.add(indent + "  // The extended operation was processed and " + "we have a result.");
        lineList.add(indent + "  // This does not necessarily mean that the " + "operation was successful.");
        lineList.add(indent + "  // Examine the result details for more " + "information.");
        lineList.add(indent + "  ResultCode resultCode = " + requestID + "Result.getResultCode();");
        lineList.add(indent + "  String message = " + requestID + "Result.getMessage();");
        lineList.add(indent + "  String matchedDN = " + requestID + "Result.getMatchedDN();");
        lineList.add(indent + "  String[] referralURLs = " + requestID + "Result.getReferralURLs();");
        lineList.add(indent + "  String responseOID = " + requestID + "Result.getOID();");
        lineList.add(indent + "  ASN1OctetString responseValue = " + requestID + "Result.getValue();");
        lineList.add(indent + "  Control[] responseControls = " + requestID + "Result.getResponseControls();");
        lineList.add(indent + '}');
        lineList.add(indent + "catch (LDAPException e)");
        lineList.add(indent + '{');
        lineList.add(indent + "  // A problem was encountered while attempting " + "to process the extended operation.");
        lineList.add(indent + "  // Maybe the following will help explain why.");
        lineList.add(indent + "  ResultCode resultCode = e.getResultCode();");
        lineList.add(indent + "  String message = e.getMessage();");
        lineList.add(indent + "  String matchedDN = e.getMatchedDN();");
        lineList.add(indent + "  String[] referralURLs = e.getReferralURLs();");
        lineList.add(indent + "  Control[] responseControls = " + "e.getResponseControls();");
        lineList.add(indent + '}');
    }
}
Also used : ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString)

Example 4 with ASN1OctetString

use of org.webpki.asn1.ASN1OctetString 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);
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1StreamReaderSequence(com.unboundid.asn1.ASN1StreamReaderSequence) ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) NotNull(com.unboundid.util.NotNull)

Example 5 with ASN1OctetString

use of org.webpki.asn1.ASN1OctetString 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);
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1StreamReaderSequence(com.unboundid.asn1.ASN1StreamReaderSequence) ASN1Exception(com.unboundid.asn1.ASN1Exception) ArrayList(java.util.ArrayList) ASN1StreamReaderSet(com.unboundid.asn1.ASN1StreamReaderSet) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Exception(com.unboundid.asn1.ASN1Exception) JSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter) ASN1Element(com.unboundid.asn1.ASN1Element) NotNull(com.unboundid.util.NotNull)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1173 Test (org.testng.annotations.Test)852 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)382 Control (com.unboundid.ldap.sdk.Control)310 ASN1Element (com.unboundid.asn1.ASN1Element)237 ArrayList (java.util.ArrayList)204 NotNull (com.unboundid.util.NotNull)191 LDAPException (com.unboundid.ldap.sdk.LDAPException)142 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)133 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)99 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)92 IOException (java.io.IOException)88 ASN1Integer (com.unboundid.asn1.ASN1Integer)80 ExtendedRequest (com.unboundid.ldap.sdk.ExtendedRequest)69 DN (com.unboundid.ldap.sdk.DN)65 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)64 ByteArrayInputStream (java.io.ByteArrayInputStream)56 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)53 ASN1Boolean (com.unboundid.asn1.ASN1Boolean)52 AuthorizationIdentityResponseControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl)49