Example usage for org.bouncycastle.asn1 DERSequence getObjectAt

List of usage examples for org.bouncycastle.asn1 DERSequence getObjectAt

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 DERSequence getObjectAt.

Prototype

public ASN1Encodable getObjectAt(int index) 

Source Link

Document

Return the object at the sequence position indicated by index.

Usage

From source file:org.jruby.ext.openssl.x509store.BouncyCastleASN1FormatHandler.java

License:LGPL

@Override
public KeyPair readDSAPrivateKey(String input) throws IOException, GeneralSecurityException {
    KeyFactory fact = KeyFactory.getInstance("DSA");
    DERSequence seq = (DERSequence) (new ASN1InputStream(ByteList.plain(input)).readObject());
    if (seq.size() == 6) {
        BigInteger p = ((DERInteger) seq.getObjectAt(1)).getValue();
        BigInteger q = ((DERInteger) seq.getObjectAt(2)).getValue();
        BigInteger g = ((DERInteger) seq.getObjectAt(3)).getValue();
        BigInteger y = ((DERInteger) seq.getObjectAt(4)).getValue();
        BigInteger x = ((DERInteger) seq.getObjectAt(5)).getValue();
        PrivateKey priv = fact.generatePrivate(new DSAPrivateKeySpec(x, p, q, g));
        PublicKey pub = fact.generatePublic(new DSAPublicKeySpec(y, p, q, g));
        return new KeyPair(pub, priv);
    } else {//  w ww  . jav a2 s .c  o  m
        return null;
    }
}

From source file:org.jruby.ext.openssl.x509store.BouncyCastleASN1FormatHandler.java

License:LGPL

@Override
public PublicKey readDSAPublicKey(String input) throws IOException, GeneralSecurityException {
    KeyFactory fact = KeyFactory.getInstance("RSA");
    DERSequence seq = (DERSequence) (new ASN1InputStream(ByteList.plain(input)).readObject());
    if (seq.size() == 4) {
        BigInteger y = ((DERInteger) seq.getObjectAt(0)).getValue();
        BigInteger p = ((DERInteger) seq.getObjectAt(1)).getValue();
        BigInteger q = ((DERInteger) seq.getObjectAt(2)).getValue();
        BigInteger g = ((DERInteger) seq.getObjectAt(3)).getValue();
        return fact.generatePublic(new DSAPublicKeySpec(y, p, q, g));
    } else {/*from w w  w.  j  a  v  a  2  s.  co  m*/
        return null;
    }
}

From source file:org.opensaml.xml.security.x509.X509Util.java

License:Apache License

/**
 * Gets the commons names that appear within the given distinguished name. The returned list provides the names in
 * the order they appeared in the DN./*from   w  w w . jav  a  2 s.  c  o  m*/
 * 
 * @param dn the DN to extract the common names from
 * 
 * @return the common names that appear in the DN in the order they appear or null if the given DN is null
 */
public static List<String> getCommonNames(X500Principal dn) {
    if (dn == null) {
        return null;
    }

    log.debug("Extracting CNs from the following DN: {}", dn.toString());
    List<String> commonNames = new LinkedList<String>();
    try {
        ASN1InputStream asn1Stream = new ASN1InputStream(dn.getEncoded());
        DERObject parent = asn1Stream.readObject();

        String cn = null;
        DERObject dnComponent;
        DERSequence grandChild;
        DERObjectIdentifier componentId;
        for (int i = 0; i < ((DERSequence) parent).size(); i++) {
            dnComponent = ((DERSequence) parent).getObjectAt(i).getDERObject();
            if (!(dnComponent instanceof DERSet)) {
                log.debug("No DN components.");
                continue;
            }

            // Each DN component is a set
            for (int j = 0; j < ((DERSet) dnComponent).size(); j++) {
                grandChild = (DERSequence) ((DERSet) dnComponent).getObjectAt(j).getDERObject();

                if (grandChild.getObjectAt(0) != null
                        && grandChild.getObjectAt(0).getDERObject() instanceof DERObjectIdentifier) {
                    componentId = (DERObjectIdentifier) grandChild.getObjectAt(0).getDERObject();

                    if (CN_OID.equals(componentId.getId())) {
                        // OK, this dn component is actually a cn attribute
                        if (grandChild.getObjectAt(1) != null
                                && grandChild.getObjectAt(1).getDERObject() instanceof DERString) {
                            cn = ((DERString) grandChild.getObjectAt(1).getDERObject()).getString();
                            commonNames.add(cn);
                        }
                    }
                }
            }
        }

        asn1Stream.close();

        return commonNames;

    } catch (IOException e) {
        log.error("Unable to extract common names from DN: ASN.1 parsing failed: " + e);
        return null;
    }
}

From source file:passwdmanager.hig.no.lds.DG_SOD.java

/**
 * Constructs a Security Object data structure.
 * //from  w w  w  .ja  v  a2 s.  c  o m
 * @param in
 *            some inputstream
 * @throws IOException
 *             if something goes wrong
 */
public DG_SOD(InputStream in) throws IOException {
    BERTLVInputStream tlvIn = new BERTLVInputStream(in);
    if (tlvIn.readTag() != EF_SOD_TAG)
        throw new IOException("Wrong tag");
    tlvIn.readLength();
    ASN1InputStream asn1in = new ASN1InputStream(in);
    DERSequence seq = (DERSequence) asn1in.readObject();
    DERObjectIdentifier objectIdentifier = (DERObjectIdentifier) seq.getObjectAt(0);
    if (!objectIdentifier.equals(SIGNED_DATA_OID)) {
        throw new IOException("Wrong OID: " + objectIdentifier.getId());
    }
    DERTaggedObject o = (DERTaggedObject) seq.getObjectAt(1);
    /* TODO: where is this tagNo specified? */
    // int tagNo = o.getTagNo();
    DERSequence s2 = (DERSequence) o.getObject();
    this.signedData = new SignedData(s2);

}

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

License:Open Source License

private String handleDERSequence(String identifier, DERSequence seq) {
    String value = null;/*from   www. ja v a  2s  .  c  o  m*/
    for (int a = 0; a < seq.size() && value == null; a++) {
        DERObject obj = seq.getObjectAt(a).getDERObject();
        if (obj instanceof ASN1ObjectIdentifier) {
            ASN1ObjectIdentifier objectIdentifier = (ASN1ObjectIdentifier) obj;
            if (objectIdentifier.getId().equals(identifier)) {
                value = seq.getObjectAt(a + 1).toString();
                break;
            }
        } else {
            value = findInCertificate(identifier, obj);
        }
    }
    return value;
}