Example usage for org.bouncycastle.asn1 DERSet size

List of usage examples for org.bouncycastle.asn1 DERSet size

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 DERSet size.

Prototype

public int size() 

Source Link

Document

return the number of objects in this set.

Usage

From source file:crossbear.CVRProcessor.java

License:Open Source License

/**
 * Search a DERSet for Common Names (identified by OID 2.5.4.3) and add all of them as byte[] to a Vector of byte[]s
 * /*from www . java  2 s  .c o m*/
 * @param set The DERSet to search
 * @param cnBytes The Vector to add all found CNs to
 * @throws IOException
 */
private static void searchSetForCN(DERSet set, Vector<byte[]> cnBytes) throws IOException {

    // The DERSet we are looking for contains exactly one element: a DERSequence
    if (set.size() != 1 || !(set.getObjectAt(0) instanceof DERSequence))
        return;

    // Extract the DERSequence
    DERSequence subseq = (DERSequence) set.getObjectAt(0);

    // The DERSequence we are looking for consists of two elements: an OID and the CN
    // First: Assert type of OID
    if (!(subseq.getObjectAt(0) instanceof ASN1ObjectIdentifier))
        return;

    // Second: Check value of OID to be id-at-commonName
    ASN1ObjectIdentifier id = (ASN1ObjectIdentifier) subseq.getObjectAt(0);
    if (!id.getId().equals("2.5.4.3"))
        return;

    // Third extract the commonName
    cnBytes.add(subseq.getObjectAt(1).getDERObject().getEncoded());
}

From source file:org.deviceconnect.android.ssl.CertificateAuthority.java

License:MIT License

/**
 * ???? Subject Alternative Names (SANs) ??.
 *
 * @param request ???/*  ww  w  .jav  a 2s  .c  o  m*/
 * @return SubjectAlternativeNames? {@link GeneralNames} 
 * @throws IOException ?????
 */
private GeneralNames parseSANs(final PKCS10CertificationRequest request) throws IOException {
    List<ASN1Encodable> generalNames = new ArrayList<>();

    CertificationRequestInfo info = request.getCertificationRequestInfo();
    ASN1Set attributes = info.getAttributes();
    for (int i = 0; i < attributes.size(); i++) {
        DEREncodable extensionRequestObj = attributes.getObjectAt(i);
        if (!(extensionRequestObj instanceof DERSequence)) {
            continue;
        }
        DERSequence extensionRequest = (DERSequence) extensionRequestObj;
        if (extensionRequest.size() != 2) {
            continue;
        }
        DEREncodable idObj = extensionRequest.getObjectAt(0);
        DEREncodable contentObj = extensionRequest.getObjectAt(1);
        if (!(idObj instanceof ASN1ObjectIdentifier && contentObj instanceof DERSet)) {
            continue;
        }
        ASN1ObjectIdentifier id = (ASN1ObjectIdentifier) idObj;
        DERSet content = (DERSet) contentObj;
        if (!id.getId().equals("1.2.840.113549.1.9.14")) {
            continue;
        }
        if (content.size() < 1) {
            continue;
        }
        DEREncodable extensionsObj = content.getObjectAt(0);
        if (!(extensionsObj instanceof DERSequence)) {
            continue;
        }
        DERSequence extensions = (DERSequence) extensionsObj;

        for (int k = 0; k < extensions.size(); k++) {
            DEREncodable extensionObj = extensions.getObjectAt(k);
            if (!(extensionObj instanceof DERSequence)) {
                continue;
            }
            DERSequence extension = (DERSequence) extensionObj;
            if (extension.size() != 2) {
                continue;
            }
            DEREncodable extensionIdObj = extension.getObjectAt(0);
            DEREncodable extensionContentObj = extension.getObjectAt(1);
            if (!(extensionIdObj instanceof ASN1ObjectIdentifier)) {
                continue;
            }
            ASN1ObjectIdentifier extensionId = (ASN1ObjectIdentifier) extensionIdObj;
            if (extensionId.getId().equals("2.5.29.17")) {
                DEROctetString san = (DEROctetString) extensionContentObj;

                ASN1StreamParser sanParser = new ASN1StreamParser(san.parser().getOctetStream());
                DEREncodable namesObj = sanParser.readObject().getDERObject();
                if (namesObj instanceof DERSequence) {
                    DERSequence names = (DERSequence) namesObj;
                    for (int m = 0; m < names.size(); m++) {
                        DEREncodable nameObj = names.getObjectAt(m);
                        if (nameObj instanceof DERTaggedObject) {
                            DERTaggedObject name = (DERTaggedObject) nameObj;
                            switch (name.getTagNo()) {
                            case GeneralName.dNSName:
                                generalNames.add(new GeneralName(GeneralName.dNSName,
                                        DERIA5String.getInstance(name, false)));
                                break;
                            case GeneralName.iPAddress:
                                generalNames.add(new GeneralName(GeneralName.iPAddress,
                                        DEROctetString.getInstance(name, true)));
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    if (generalNames.size() > 0) {
        return new GeneralNames(new DERSequence(generalNames.toArray(new ASN1Encodable[generalNames.size()])));
    }
    return null;
}

From source file:se.inera.intyg.webcert.web.service.signatur.asn1.ASN1UtilImpl.java

License:Open Source License

private String handleDERSet(String identifier, DERSet set) {
    String value = null;/*from   w  w  w .j  a  v a 2 s .  c  o  m*/
    for (int a = 0; a < set.size() && value == null; a++) {
        DERObject obj = set.getObjectAt(a).getDERObject();
        value = findInCertificate(identifier, obj);
    }
    return value;
}