Search in sources :

Example 6 with Attribute

use of org.jdom2.Attribute in project android_frameworks_base by ResurrectionRemix.

the class ESTHandler method buildCSR.

private byte[] buildCSR(ByteBuffer octetBuffer, OMADMAdapter omadmAdapter, HTTPHandler httpHandler) throws IOException, GeneralSecurityException {
    //Security.addProvider(new BouncyCastleProvider());
    Log.d(TAG, "/csrattrs:");
    /*
        byte[] octets = new byte[octetBuffer.remaining()];
        octetBuffer.duplicate().get(octets);
        for (byte b : octets) {
            System.out.printf("%02x ", b & 0xff);
        }
        */
    Collection<Asn1Object> csrs = Asn1Decoder.decode(octetBuffer);
    for (Asn1Object asn1Object : csrs) {
        Log.d(TAG, asn1Object.toString());
    }
    if (csrs.size() != 1) {
        throw new IOException("Unexpected object count in CSR attributes response: " + csrs.size());
    }
    Asn1Object sequence = csrs.iterator().next();
    if (sequence.getClass() != Asn1Constructed.class) {
        throw new IOException("Unexpected CSR attribute container: " + sequence);
    }
    String keyAlgo = null;
    Asn1Oid keyAlgoOID = null;
    String sigAlgo = null;
    String curveName = null;
    Asn1Oid pubCrypto = null;
    int keySize = -1;
    Map<Asn1Oid, ASN1Encodable> idAttributes = new HashMap<>();
    for (Asn1Object child : sequence.getChildren()) {
        if (child.getTag() == Asn1Decoder.TAG_OID) {
            Asn1Oid oid = (Asn1Oid) child;
            OidMappings.SigEntry sigEntry = OidMappings.getSigEntry(oid);
            if (sigEntry != null) {
                sigAlgo = sigEntry.getSigAlgo();
                keyAlgoOID = sigEntry.getKeyAlgo();
                keyAlgo = OidMappings.getJCEName(keyAlgoOID);
            } else if (oid.equals(OidMappings.sPkcs9AtChallengePassword)) {
                byte[] tlsUnique = httpHandler.getTLSUnique();
                if (tlsUnique != null) {
                    idAttributes.put(oid, new DERPrintableString(Base64.encodeToString(tlsUnique, Base64.DEFAULT)));
                } else {
                    Log.w(TAG, "Cannot retrieve TLS unique channel binding");
                }
            }
        } else if (child.getTag() == Asn1Decoder.TAG_SEQ) {
            Asn1Oid oid = null;
            Set<Asn1Oid> oidValues = new HashSet<>();
            List<Asn1Object> values = new ArrayList<>();
            for (Asn1Object attributeSeq : child.getChildren()) {
                if (attributeSeq.getTag() == Asn1Decoder.TAG_OID) {
                    oid = (Asn1Oid) attributeSeq;
                } else if (attributeSeq.getTag() == Asn1Decoder.TAG_SET) {
                    for (Asn1Object value : attributeSeq.getChildren()) {
                        if (value.getTag() == Asn1Decoder.TAG_OID) {
                            oidValues.add((Asn1Oid) value);
                        } else {
                            values.add(value);
                        }
                    }
                }
            }
            if (oid == null) {
                throw new IOException("Invalid attribute, no OID");
            }
            if (oid.equals(OidMappings.sExtensionRequest)) {
                for (Asn1Oid subOid : oidValues) {
                    if (OidMappings.isIDAttribute(subOid)) {
                        if (subOid.equals(OidMappings.sMAC)) {
                            idAttributes.put(subOid, new DERIA5String(omadmAdapter.getMAC()));
                        } else if (subOid.equals(OidMappings.sIMEI)) {
                            idAttributes.put(subOid, new DERIA5String(omadmAdapter.getImei()));
                        } else if (subOid.equals(OidMappings.sMEID)) {
                            idAttributes.put(subOid, new DERBitString(omadmAdapter.getMeid()));
                        } else if (subOid.equals(OidMappings.sDevID)) {
                            idAttributes.put(subOid, new DERPrintableString(omadmAdapter.getDevID()));
                        }
                    }
                }
            } else if (OidMappings.getCryptoID(oid) != null) {
                pubCrypto = oid;
                if (!values.isEmpty()) {
                    for (Asn1Object value : values) {
                        if (value.getTag() == Asn1Decoder.TAG_INTEGER) {
                            keySize = (int) ((Asn1Integer) value).getValue();
                        }
                    }
                }
                if (oid.equals(OidMappings.sAlgo_EC)) {
                    if (oidValues.isEmpty()) {
                        throw new IOException("No ECC curve name provided");
                    }
                    for (Asn1Oid value : oidValues) {
                        curveName = OidMappings.getJCEName(value);
                        if (curveName != null) {
                            break;
                        }
                    }
                    if (curveName == null) {
                        throw new IOException("Found no ECC curve for " + oidValues);
                    }
                }
            }
        }
    }
    if (keyAlgoOID == null) {
        throw new IOException("No public key algorithm specified");
    }
    if (pubCrypto != null && !pubCrypto.equals(keyAlgoOID)) {
        throw new IOException("Mismatching key algorithms");
    }
    if (keyAlgoOID.equals(OidMappings.sAlgo_RSA)) {
        if (keySize < MinRSAKeySize) {
            if (keySize >= 0) {
                Log.i(TAG, "Upgrading suggested RSA key size from " + keySize + " to " + MinRSAKeySize);
            }
            keySize = MinRSAKeySize;
        }
    }
    Log.d(TAG, String.format("pub key '%s', signature '%s', ECC curve '%s', id-atts %s", keyAlgo, sigAlgo, curveName, idAttributes));
    /*
          Ruckus:
            SEQUENCE:
              OID=1.2.840.113549.1.1.11 (algo_id_sha256WithRSAEncryption)

          RFC-7030:
            SEQUENCE:
              OID=1.2.840.113549.1.9.7 (challengePassword)
              SEQUENCE:
                OID=1.2.840.10045.2.1 (algo_id_ecPublicKey)
                SET:
                  OID=1.3.132.0.34 (secp384r1)
              SEQUENCE:
                OID=1.2.840.113549.1.9.14 (extensionRequest)
                SET:
                  OID=1.3.6.1.1.1.1.22 (mac-address)
              OID=1.2.840.10045.4.3.3 (eccdaWithSHA384)

              1L, 3L, 6L, 1L, 1L, 1L, 1L, 22
         */
    // ECC Does not appear to be supported currently
    KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo);
    if (curveName != null) {
        AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(keyAlgo);
        algorithmParameters.init(new ECNamedCurveGenParameterSpec(curveName));
        kpg.initialize(algorithmParameters.getParameterSpec(ECNamedCurveGenParameterSpec.class));
    } else {
        kpg.initialize(keySize);
    }
    KeyPair kp = kpg.generateKeyPair();
    X500Principal subject = new X500Principal("CN=Android, O=Google, C=US");
    mClientKey = kp.getPrivate();
    // !!! Map the idAttributes into an ASN1Set of values to pass to
    // the PKCS10CertificationRequest - this code is using outdated BC classes and
    // has *not* been tested.
    ASN1Set attributes;
    if (!idAttributes.isEmpty()) {
        ASN1EncodableVector payload = new DEREncodableVector();
        for (Map.Entry<Asn1Oid, ASN1Encodable> entry : idAttributes.entrySet()) {
            DERObjectIdentifier type = new DERObjectIdentifier(entry.getKey().toOIDString());
            ASN1Set values = new DERSet(entry.getValue());
            Attribute attribute = new Attribute(type, values);
            payload.add(attribute);
        }
        attributes = new DERSet(payload);
    } else {
        attributes = null;
    }
    return new PKCS10CertificationRequest(sigAlgo, subject, kp.getPublic(), attributes, mClientKey).getEncoded();
}
Also used : DERSet(com.android.org.bouncycastle.asn1.DERSet) ASN1Set(com.android.org.bouncycastle.asn1.ASN1Set) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Attribute(com.android.org.bouncycastle.asn1.x509.Attribute) DERBitString(com.android.org.bouncycastle.asn1.DERBitString) DERPrintableString(com.android.org.bouncycastle.asn1.DERPrintableString) DERIA5String(com.android.org.bouncycastle.asn1.DERIA5String) DERSet(com.android.org.bouncycastle.asn1.DERSet) DERIA5String(com.android.org.bouncycastle.asn1.DERIA5String) Asn1Integer(com.android.hotspot2.asn1.Asn1Integer) DERPrintableString(com.android.org.bouncycastle.asn1.DERPrintableString) ASN1EncodableVector(com.android.org.bouncycastle.asn1.ASN1EncodableVector) List(java.util.List) ArrayList(java.util.ArrayList) ASN1Encodable(com.android.org.bouncycastle.asn1.ASN1Encodable) PKCS10CertificationRequest(com.android.org.bouncycastle.jce.PKCS10CertificationRequest) Asn1Oid(com.android.hotspot2.asn1.Asn1Oid) KeyPair(java.security.KeyPair) ECNamedCurveGenParameterSpec(com.android.org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec) DEREncodableVector(com.android.org.bouncycastle.asn1.DEREncodableVector) DERBitString(com.android.org.bouncycastle.asn1.DERBitString) IOException(java.io.IOException) KeyPairGenerator(java.security.KeyPairGenerator) DERObjectIdentifier(com.android.org.bouncycastle.asn1.DERObjectIdentifier) Asn1Object(com.android.hotspot2.asn1.Asn1Object) OidMappings(com.android.hotspot2.asn1.OidMappings) ASN1Set(com.android.org.bouncycastle.asn1.ASN1Set) X500Principal(javax.security.auth.x500.X500Principal) Map(java.util.Map) HashMap(java.util.HashMap) AlgorithmParameters(java.security.AlgorithmParameters)

Example 7 with Attribute

use of org.jdom2.Attribute in project pcgen by PCGen.

the class Localized method update.

/**
	 * 
	 * @param e
	 * @param attribute if {@code null}, use the trimmed text.
	 */
private void update(Element e, String attribute) {
    List<?> children = e.getChildren(ELEMENT_LOC);
    for (Object object : children) {
        if (object instanceof Element) {
            Element child = (Element) object;
            String lang = child.getAttributeValue(ATTRIBUTE_LANGUAGE);
            String name;
            if (attribute == null)
                name = child.getTextTrim();
            else
                name = child.getAttributeValue(attribute);
            if (lang != null && !lang.isEmpty())
                addName(lang, name);
        }
    }
}
Also used : Element(org.jdom2.Element)

Example 8 with Attribute

use of org.jdom2.Attribute in project JMRI by JMRI.

the class BlockManagerXml method loadPath.

/**
     * Load path into an existing Block from XML.
     *
     * @param block   Block to receive path
     * @param element Element containing path information
     * @return true if path added to block; false otherwise
     * @throws jmri.configurexml.JmriConfigureXmlException if element contains
     *                                                     malformed or
     *                                                     schematically invalid
     *                                                     XMl
     */
public boolean loadPath(Block block, Element element) throws JmriConfigureXmlException {
    // load individual path
    int toDir = 0;
    int fromDir = 0;
    try {
        toDir = element.getAttribute("todir").getIntValue();
        fromDir = element.getAttribute("fromdir").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("Could not parse path attribute");
    } catch (NullPointerException e) {
        handleException("Block Path entry in file missing required attribute", null, block.getSystemName(), block.getUserName(), null);
    }
    Block toBlock = null;
    if (element.getAttribute("block") != null) {
        String name = element.getAttribute("block").getValue();
        toBlock = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(name);
    }
    Path path = new Path(toBlock, toDir, fromDir);
    List<Element> settings = element.getChildren("beansetting");
    for (int i = 0; i < settings.size(); i++) {
        Element setting = settings.get(i);
        loadBeanSetting(path, setting);
    }
    // check if path already in block
    if (!block.hasPath(path)) {
        block.addPath(path);
        return true;
    } else {
        log.debug("Skipping load of duplicate path {}", path);
        return false;
    }
}
Also used : Path(jmri.Path) Element(org.jdom2.Element) Block(jmri.Block)

Example 9 with Attribute

use of org.jdom2.Attribute in project JMRI by JMRI.

the class BlockManagerXml method loadBlock.

/**
     * Utility method to load the individual Block objects.
     *
     * @param element Element containing one block
     * @throws jmri.configurexml.JmriConfigureXmlException if element contains
     *                                                     malformed or
     *                                                     schematically invalid
     *                                                     XMl
     */
public void loadBlock(Element element) throws JmriConfigureXmlException {
    String sysName = getSystemName(element);
    String userName = getUserName(element);
    if (log.isDebugEnabled()) {
        log.debug("defined Block: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
    }
    Block block = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(sysName);
    if (block == null) {
        // create it if doesn't exist
        InstanceManager.getDefault(jmri.BlockManager.class).createNewBlock(sysName, userName);
        block = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(sysName);
    }
    if (block == null) {
        log.error("Unable to load block with system name " + sysName + " and username of " + (userName == null ? "<null>" : userName));
        return;
    }
    if (userName != null) {
        block.setUserName(userName);
    }
    if (element.getAttribute("length") != null) {
        // load length in millimeters
        block.setLength(Float.parseFloat(element.getAttribute("length").getValue()));
    }
    if (element.getAttribute("curve") != null) {
        // load curve attribute
        block.setCurvature(Integer.parseInt((element.getAttribute("curve")).getValue()));
    }
    try {
        block.setBlockSpeed("Global");
        if (element.getChild("speed") != null) {
            String speed = element.getChild("speed").getText();
            if (speed != null && !speed.equals("") && !speed.contains("Global")) {
                block.setBlockSpeed(speed);
            }
        }
    } catch (jmri.JmriException ex) {
        log.error(ex.toString());
    }
    if (element.getChild("permissive") != null) {
        boolean permissive = false;
        if (element.getChild("permissive").getText().equals("yes")) {
            permissive = true;
        }
        block.setPermissiveWorking(permissive);
    }
    Element deniedBlocks = element.getChild("deniedBlocks");
    if (deniedBlocks != null) {
        List<Element> denyBlock = deniedBlocks.getChildren("block");
        for (Element deny : denyBlock) {
            block.addBlockDenyList(deny.getText());
        }
    }
    // load common parts
    loadCommon(block, element);
    // load sensor if present
    List<Element> sensors = element.getChildren("sensor");
    if (sensors.size() > 1) {
        log.error("More than one sensor present: " + sensors.size());
    }
    if (sensors.size() == 1) {
        //Old method of saving sensors
        if (sensors.get(0).getAttribute("systemName") != null) {
            String name = sensors.get(0).getAttribute("systemName").getValue();
            if (!name.equals("")) {
                block.setSensor(name);
            }
        }
    }
    if (element.getChild("occupancysensor") != null) {
        String name = element.getChild("occupancysensor").getText();
        if (!name.equals("")) {
            block.setSensor(name);
        }
    }
    // load Reporter if present
    List<Element> reporters = element.getChildren("reporter");
    if (reporters.size() > 1) {
        log.error("More than one reporter present: " + reporters.size());
    }
    if (reporters.size() == 1) {
        // Reporter
        String name = reporters.get(0).getAttribute("systemName").getValue();
        try {
            Reporter reporter = InstanceManager.getDefault(jmri.ReporterManager.class).provideReporter(name);
            block.setReporter(reporter);
            block.setReportingCurrent(reporters.get(0).getAttribute("useCurrent").getValue().equals("yes"));
        } catch (IllegalArgumentException ex) {
            log.warn("failed to create Reporter \"{}\" during Block load", name);
        }
    }
    // load paths if present
    List<Element> paths = element.getChildren("path");
    int startSize = block.getPaths().size();
    int loadCount = 0;
    for (int i = 0; i < paths.size(); i++) {
        Element path = paths.get(i);
        if (loadPath(block, path)) {
            loadCount++;
        }
    }
    if (startSize > 0 && loadCount > 0) {
        log.warn("Added " + loadCount++ + " paths to block " + sysName + " that already had " + startSize + " blocks.");
    }
    if (startSize + loadCount != block.getPaths().size()) {
        log.error("Started with " + startSize + " paths in block " + sysName + ", added " + loadCount + " but final count is " + block.getPaths().size() + "; something not right.");
    }
}
Also used : BlockManager(jmri.BlockManager) Element(org.jdom2.Element) Reporter(jmri.Reporter) Block(jmri.Block)

Example 10 with Attribute

use of org.jdom2.Attribute in project JMRI by JMRI.

the class BlockManagerXml method loadBeanSetting.

/**
     * Load BeanSetting into an existing Path.
     *
     * @param path    Path to receive BeanSetting
     * @param element Element containing beansetting information
     */
public void loadBeanSetting(Path path, Element element) {
    int setting = 0;
    try {
        setting = element.getAttribute("setting").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("Could not parse beansetting attribute");
    }
    List<Element> turnouts = element.getChildren("turnout");
    if (turnouts.size() != 1) {
        log.error("invalid number of turnout element children");
    }
    String name = turnouts.get(0).getAttribute("systemName").getValue();
    try {
        Turnout t = InstanceManager.turnoutManagerInstance().provideTurnout(name);
        BeanSetting bs = new BeanSetting(t, name, setting);
        path.addSetting(bs);
    } catch (IllegalArgumentException ex) {
        log.warn("failed to create Turnout \"{}\" during Block load", name);
    }
}
Also used : BeanSetting(jmri.BeanSetting) Element(org.jdom2.Element) Turnout(jmri.Turnout)

Aggregations

Attribute (org.jdom2.Attribute)104 Element (org.jdom2.Element)96 DataConversionException (org.jdom2.DataConversionException)17 Editor (jmri.jmrit.display.Editor)15 ArrayList (java.util.ArrayList)13 NamedIcon (jmri.jmrit.catalog.NamedIcon)13 IOException (java.io.IOException)12 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)10 File (java.io.File)8 Color (java.awt.Color)7 List (java.util.List)7 HashMap (java.util.HashMap)6 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5 Asn1Object (com.android.hotspot2.asn1.Asn1Object)5 Asn1Oid (com.android.hotspot2.asn1.Asn1Oid)5 OidMappings (com.android.hotspot2.asn1.OidMappings)5 ASN1Encodable (com.android.org.bouncycastle.asn1.ASN1Encodable)5 ASN1EncodableVector (com.android.org.bouncycastle.asn1.ASN1EncodableVector)5 ASN1Set (com.android.org.bouncycastle.asn1.ASN1Set)5 DERBitString (com.android.org.bouncycastle.asn1.DERBitString)5