Example usage for org.bouncycastle.asn1 DERSet getObjects

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

Introduction

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

Prototype

public Enumeration getObjects() 

Source Link

Usage

From source file:ch.cyberduck.core.aquaticprime.Receipt.java

License:Open Source License

/**
 * Verifies the App Store Receipt// w w w .  j  a v a  2 s.c  o  m
 *
 * @return False if receipt validation failed.
 */
@Override
public boolean verify() {
    try {
        Security.addProvider(new BouncyCastleProvider());
        PKCS7SignedData signature = new PKCS7SignedData(
                IOUtils.toByteArray(new FileInputStream(this.getFile().getAbsolute())));

        signature.verify();
        // For additional security, you may verify the fingerprint of the root CA and the OIDs of the
        // intermediate CA and signing certificate. The OID in the Certificate Policies Extension of the
        // intermediate CA is (1 2 840 113635 100 5 6 1), and the Marker OID of the signing certificate
        // is (1 2 840 113635 100 6 11 1).

        // Extract the receipt attributes
        CMSSignedData s = new CMSSignedData(new FileInputStream(this.getFile().getAbsolute()));
        CMSProcessable signedContent = s.getSignedContent();
        byte[] originalContent = (byte[]) signedContent.getContent();
        ASN1Object asn = ASN1Object.fromByteArray(originalContent);

        byte[] opaque = null;
        String bundleIdentifier = null;
        String bundleVersion = null;
        byte[] hash = null;

        if (asn instanceof DERSet) {
            // 2 Bundle identifier      Interpret as an ASN.1 UTF8STRING.
            // 3 Application version    Interpret as an ASN.1 UTF8STRING.
            // 4 Opaque value           Interpret as a series of bytes.
            // 5 SHA-1 hash             Interpret as a 20-byte SHA-1 digest value.
            DERSet set = (DERSet) asn;
            Enumeration enumeration = set.getObjects();
            while (enumeration.hasMoreElements()) {
                Object next = enumeration.nextElement();
                if (next instanceof DERSequence) {
                    DERSequence sequence = (DERSequence) next;
                    DEREncodable type = sequence.getObjectAt(0);
                    if (type instanceof DERInteger) {
                        if (((DERInteger) type).getValue().intValue() == 2) {
                            DEREncodable value = sequence.getObjectAt(2);
                            if (value instanceof DEROctetString) {
                                bundleIdentifier = new String(((DEROctetString) value).getOctets(), "utf-8");
                            }
                        } else if (((DERInteger) type).getValue().intValue() == 3) {
                            DEREncodable value = sequence.getObjectAt(2);
                            if (value instanceof DEROctetString) {
                                bundleVersion = new String(((DEROctetString) value).getOctets(), "utf-8");
                            }
                        } else if (((DERInteger) type).getValue().intValue() == 4) {
                            DEREncodable value = sequence.getObjectAt(2);
                            if (value instanceof DEROctetString) {
                                opaque = ((DEROctetString) value).getOctets();
                            }
                        } else if (((DERInteger) type).getValue().intValue() == 5) {
                            DEREncodable value = sequence.getObjectAt(2);
                            if (value instanceof DEROctetString) {
                                hash = ((DEROctetString) value).getOctets();
                            }
                        }
                    }
                }
            }
        } else {
            log.error(String.format("Expected set of attributes for %s", asn));
            return false;
        }
        if (!StringUtils.equals("ch.sudo.cyberduck", StringUtils.trim(bundleIdentifier))) {
            log.error("Bundle identifier in ASN set does not match");
            return false;
        }
        if (!StringUtils.equals(Preferences.instance().getDefault("CFBundleShortVersionString"),
                StringUtils.trim(bundleVersion))) {
            log.warn("Bundle version in ASN set does not match");
        }

        NetworkInterface en0 = NetworkInterface.getByName("en0");
        if (null == en0) {
            // Interface is not found when link is down #fail
            log.warn("No network interface en0");
        } else {
            byte[] mac = en0.getHardwareAddress();
            if (null == mac) {
                log.error("Cannot determine MAC address");
                // Continue without validation
                return true;
            }
            final String hex = Hex.encodeHexString(mac);
            if (log.isDebugEnabled()) {
                log.debug("Interface en0:" + hex);
            }
            // Compute the hash of the GUID
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(mac);
            digest.update(opaque);
            digest.update(bundleIdentifier.getBytes(Charset.forName("utf-8")));
            byte[] result = digest.digest();
            if (Arrays.equals(result, hash)) {
                if (log.isInfoEnabled()) {
                    log.info(String.format("Valid receipt for GUID %s", hex));
                }
                this.name = hex;
            } else {
                log.error(String.format("Failed verification. Hash with GUID %s does not match hash in receipt",
                        hex));
                return false;
            }
        }
    } catch (Exception e) {
        log.error("Unknown receipt validation error", e);
        // Shutdown if receipt is not valid
        return false;
    }
    // Always return true to dismiss donation prompt.
    return true;
}

From source file:org.glite.voms.ac.AttributeCertificate.java

License:eu-egee.org license

/**
 * Returns a list of the attributes matching the provided OID.
 * @param oid Object Identifier, on the form "1.2.3.4"
 * @return List of ASN.1 objects representing the OID type in question
 *//* w ww  . ja  v  a 2  s .co  m*/
public List getAttributes(String oid) {
    if (oid == null) {
        return Collections.EMPTY_LIST;
    }

    ASN1Sequence seq = getAttributes();

    if ((seq == null) || (seq.size() == 0)) {
        return Collections.EMPTY_LIST;
    }

    Vector v = new Vector();

    for (Enumeration e = seq.getObjects(); e.hasMoreElements();) {
        ASN1Sequence attribute = (ASN1Sequence) e.nextElement();

        if (oid.equals(((DERObjectIdentifier) attribute.getObjectAt(0)).getId())) {
            DERSet set = (DERSet) attribute.getObjectAt(1);

            for (Enumeration s = set.getObjects(); s.hasMoreElements();) {
                v.add(s.nextElement());
            }
        }
    }

    return v;
}