Example usage for org.bouncycastle.asn1 DERSequence DERSequence

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

Introduction

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

Prototype

public DERSequence(ASN1Encodable[] elements) 

Source Link

Document

Create a sequence containing an array of objects.

Usage

From source file:org.jnotary.dvcs.PathProcInput.java

License:Open Source License

@Override
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector v = new ASN1EncodableVector();

    ASN1EncodableVector seq = new ASN1EncodableVector();
    for (int i = 0; i < acceptablePolicySet.length; i++) {
        seq.add(acceptablePolicySet[i].toASN1Primitive());
    }/*  w  w w.  ja  va  2s  . c o  m*/
    v.add(new DERSequence(seq));

    v.add(inhibitPolicyMapping);
    v.add(explicitPolicyReqd);

    return new DERSequence(v);
}

From source file:org.jnotary.dvcs.TargetEtcChain.java

License:Open Source License

@Override
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(target);//w ww .java 2 s.c o  m
    if (chain != null) {
        ASN1EncodableVector seq = new ASN1EncodableVector();
        for (int i = 0; i < chain.length; i++) {
            seq.add(chain[i].toASN1Primitive());
        }
        v.add(new DERSequence(seq));
    }
    if (pathProcInput != null)
        v.add(new DERTaggedObject(true, 0, pathProcInput));

    return new DERSequence(v);
}

From source file:org.jruby.ext.openssl.impl.NetscapeCertRequest.java

License:Open Source License

public NetscapeCertRequest(final String challenge, final AlgorithmIdentifier signingAlg,
        final PublicKey publicKey) throws InvalidKeySpecException {

    this.challenge = challenge;
    this.sigAlg = signingAlg;
    this.publicKey = publicKey;

    ASN1EncodableVector contentDER = new ASN1EncodableVector();
    try {//w w  w  .ja va 2s  .  c  om
        contentDER.add(getKeySpec());
    } catch (IOException e) {
        throw new InvalidKeySpecException(e);
    }
    //content_der.add(new SubjectPublicKeyInfo(sigAlg, new RSAPublicKeyStructure(pubkey.getModulus(), pubkey.getPublicExponent()).getDERObject()));
    contentDER.add(new DERIA5String(challenge));

    try {
        this.content = new DERBitString(new DERSequence(contentDER));
    } catch (Exception e) {
        // new DERBitString throw IOExcetpion since BC 1.49
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        throw new InvalidKeySpecException("exception encoding key: " + e.toString());
    }
}

From source file:org.jruby.ext.openssl.impl.NetscapeCertRequest.java

License:Open Source License

public void sign(final PrivateKey privateKey, SecureRandom random)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, InvalidKeySpecException {
    final Signature signature = getSignature();

    if (random != null) {
        signature.initSign(privateKey, random);
    } else {/*from  w  w w .  j  av a2 s.  c  o m*/
        signature.initSign(privateKey);
    }

    ASN1EncodableVector pkac = new ASN1EncodableVector();

    try {
        pkac.add(getKeySpec());
    } catch (IOException e) {
        throw new InvalidKeySpecException(e);
    }
    pkac.add(new DERIA5String(challenge));

    try {
        signature.update(new DERSequence(pkac).getEncoded(ASN1Encoding.DER));
    } catch (IOException e) {
        throw new SignatureException(e);
    }

    signatureBits = signature.sign();
}

From source file:org.jruby.ext.openssl.impl.NetscapeCertRequest.java

License:Open Source License

public ASN1Primitive toASN1Primitive() throws IOException {
    ASN1EncodableVector spkac = new ASN1EncodableVector();
    ASN1EncodableVector pkac = new ASN1EncodableVector();

    try {//from w ww .ja  va 2s . c  om
        pkac.add(getKeySpec());
    } catch (IOException e) {
        // TODO is this really fine shouldn't it be thrown ?
    }

    pkac.add(new DERIA5String(challenge));

    spkac.add(new DERSequence(pkac));
    spkac.add(sigAlg);
    spkac.add(new DERBitString(signatureBits));

    return new DERSequence(spkac);
}

From source file:org.jruby.ext.openssl.impl.pem.MiscPEMGenerator.java

License:Open Source License

private PemObject createPemObject(Object o) throws IOException {
    String type;//  w w  w.j  a v  a 2  s  .c  o  m
    byte[] encoding;

    if (o instanceof PemObject) {
        return (PemObject) o;
    }
    if (o instanceof PemObjectGenerator) {
        return ((PemObjectGenerator) o).generate();
    }
    if (o instanceof X509CertificateHolder) {
        type = "CERTIFICATE";
        encoding = ((X509CertificateHolder) o).getEncoded();
    } else if (o instanceof X509CRLHolder) {
        type = "X509 CRL";
        encoding = ((X509CRLHolder) o).getEncoded();
    } else if (o instanceof PrivateKeyInfo) {
        PrivateKeyInfo info = (PrivateKeyInfo) o;
        ASN1ObjectIdentifier algOID = info.getPrivateKeyAlgorithm().getAlgorithm();

        if (algOID.equals(PKCSObjectIdentifiers.rsaEncryption)) {
            type = "RSA PRIVATE KEY";
            encoding = info.parsePrivateKey().toASN1Primitive().getEncoded();
        } else if (algOID.equals(dsaOids[0]) || algOID.equals(dsaOids[1])) {
            type = "DSA PRIVATE KEY";

            DSAParameter p = DSAParameter.getInstance(info.getPrivateKeyAlgorithm().getParameters());
            ASN1EncodableVector v = new ASN1EncodableVector();

            v.add(new ASN1Integer(BigInteger.ZERO));
            v.add(new ASN1Integer(p.getP()));
            v.add(new ASN1Integer(p.getQ()));
            v.add(new ASN1Integer(p.getG()));

            BigInteger x = ASN1Integer.getInstance(info.parsePrivateKey()).getValue();
            BigInteger y = p.getG().modPow(x, p.getP());

            v.add(new ASN1Integer(y));
            v.add(new ASN1Integer(x));

            encoding = new DERSequence(v).getEncoded();
        } else if (algOID.equals(X9ObjectIdentifiers.id_ecPublicKey)) {
            type = "EC PRIVATE KEY";
            encoding = info.parsePrivateKey().toASN1Primitive().getEncoded();
        } else {
            throw new IOException("Cannot identify private key");
        }
    } else if (o instanceof SubjectPublicKeyInfo) {
        type = "PUBLIC KEY";
        encoding = ((SubjectPublicKeyInfo) o).getEncoded();
    } else if (o instanceof X509AttributeCertificateHolder) {
        type = "ATTRIBUTE CERTIFICATE";
        encoding = ((X509AttributeCertificateHolder) o).getEncoded();
    } else if (o instanceof PKCS10CertificationRequest) {
        type = "CERTIFICATE REQUEST";
        encoding = ((PKCS10CertificationRequest) o).getEncoded();
    } else if (o instanceof ContentInfo) {
        type = "PKCS7";
        encoding = ((ContentInfo) o).getEncoded();
    }
    //
    // NOTE: added behaviour to provide backwards compatibility with 1.47 :
    //
    else if (o instanceof java.security.cert.X509Certificate) // 1.47 compatibility
    {
        type = "CERTIFICATE";
        try {
            encoding = ((java.security.cert.X509Certificate) o).getEncoded();
        } catch (CertificateEncodingException e) {
            throw new PemGenerationException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof java.security.cert.X509CRL) // 1.47 compatibility
    {
        type = "X509 CRL";
        try {
            encoding = ((java.security.cert.X509CRL) o).getEncoded();
        } catch (CRLException e) {
            throw new PemGenerationException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof java.security.KeyPair) // 1.47 compatibility
    {
        return createPemObject(((java.security.KeyPair) o).getPrivate());
    } else if (o instanceof java.security.PrivateKey) // 1.47 compatibility
    {
        PrivateKeyInfo info = new PrivateKeyInfo(
                (ASN1Sequence) ASN1Primitive.fromByteArray(((java.security.Key) o).getEncoded()));

        if (o instanceof java.security.interfaces.RSAPrivateKey) {
            type = "RSA PRIVATE KEY";

            encoding = info.parsePrivateKey().toASN1Primitive().getEncoded();
        } else if (o instanceof java.security.interfaces.DSAPrivateKey) {
            type = "DSA PRIVATE KEY";

            DSAParameter p = DSAParameter.getInstance(info.getPrivateKeyAlgorithm().getParameters());
            ASN1EncodableVector v = new ASN1EncodableVector();

            v.add(new DERInteger(0));
            v.add(new DERInteger(p.getP()));
            v.add(new DERInteger(p.getQ()));
            v.add(new DERInteger(p.getG()));

            BigInteger x = ((java.security.interfaces.DSAPrivateKey) o).getX();
            BigInteger y = p.getG().modPow(x, p.getP());

            v.add(new DERInteger(y));
            v.add(new DERInteger(x));

            encoding = new DERSequence(v).getEncoded();
        } else if (((java.security.PrivateKey) o).getAlgorithm().equals("ECDSA")) {
            type = "EC PRIVATE KEY";

            encoding = info.parsePrivateKey().toASN1Primitive().getEncoded();
        } else {
            throw new IOException("Cannot identify private key");
        }
    } else if (o instanceof java.security.PublicKey) // 1.47 compatibility
    {
        type = "PUBLIC KEY";

        encoding = ((java.security.PublicKey) o).getEncoded();
    } else if (o instanceof X509AttributeCertificate) // 1.47 compatibility
    {
        type = "ATTRIBUTE CERTIFICATE";
        encoding = ((X509AttributeCertificate) o).getEncoded();
    }
    //
    //
    //
    else {
        throw new PemGenerationException("unknown object passed - can't encode.");
    }

    if (encryptor != null) // NEW STUFF (NOT IN OLD)
    {
        String dekAlgName = Strings.toUpperCase(encryptor.getAlgorithm());

        // Note: For backward compatibility
        if (dekAlgName.equals("DESEDE")) {
            dekAlgName = "DES-EDE3-CBC";
        }

        byte[] iv = encryptor.getIV();
        byte[] encData = encryptor.encrypt(encoding);

        List<PemHeader> headers = new ArrayList<PemHeader>(2);

        headers.add(new PemHeader("Proc-Type", "4,ENCRYPTED"));
        headers.add(new PemHeader("DEK-Info", dekAlgName + "," + getHexEncoded(iv)));

        return new PemObject(type, headers, encData);
    }
    return new PemObject(type, encoding);
}

From source file:org.jruby.ext.openssl.PKCS10CertificationRequestExt.java

License:LGPL

public void setAttributes(DERSet attrs) {
    ASN1Sequence seq = (ASN1Sequence) this.reqInfo.toASN1Object();
    ASN1EncodableVector v1 = new ASN1EncodableVector();
    for (int i = 0; i < (seq.size() - 1); i++) {
        v1.add(seq.getObjectAt(i));// www.  j a va2  s  . c  om
    }
    v1.add(new DERTaggedObject(0, attrs));
    this.reqInfo = new CertificationRequestInfo(new DERSequence(v1));
}

From source file:org.jruby.ext.openssl.PKCS10CertificationRequestExt.java

License:LGPL

public void setVersion(int v) {
    DERInteger nVersion = new DERInteger(v);
    ASN1Sequence seq = (ASN1Sequence) this.reqInfo.toASN1Object();
    ASN1EncodableVector v1 = new ASN1EncodableVector();
    v1.add(nVersion);//from ww  w  . j  av  a  2s .co  m
    for (int i = 1; i < seq.size(); i++) {
        v1.add(seq.getObjectAt(i));
    }
    this.reqInfo = new CertificationRequestInfo(new DERSequence(v1));
}

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

License:LGPL

public void writeX509Aux(Writer _out, X509AuxCertificate obj) throws IOException {
    BufferedWriter out = makeBuffered(_out);
    byte[] encoding = null;
    try {//from   w w  w .jav  a 2s  .  c om
        if (obj.getAux() == null) {
            encoding = obj.getEncoded();
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] ymp = obj.getEncoded();
            baos.write(ymp, 0, ymp.length);

            X509Aux aux = obj.getAux();
            ASN1EncodableVector a1 = new ASN1EncodableVector();
            if (aux.trust.size() > 0) {
                ASN1EncodableVector a2 = new ASN1EncodableVector();
                for (String trust : aux.trust) {
                    a2.add(new DERObjectIdentifier(trust));
                }
                a1.add(new DERSequence(a2));
            }
            if (aux.reject.size() > 0) {
                ASN1EncodableVector a2 = new ASN1EncodableVector();
                for (String reject : aux.reject) {
                    a2.add(new DERObjectIdentifier(reject));
                }
                a1.add(new DERTaggedObject(0, new DERSequence(a2)));
            }
            if (aux.alias != null) {
                a1.add(new DERUTF8String(aux.alias));
            }
            if (aux.keyid != null) {
                a1.add(new DEROctetString(aux.keyid));
            }
            if (aux.other.size() > 0) {
                ASN1EncodableVector a2 = new ASN1EncodableVector();
                for (DERObject other : aux.other) {
                    a2.add(other);
                }
                a1.add(new DERTaggedObject(1, new DERSequence(a2)));
            }
            ymp = new DERSequence(a1).getEncoded();
            baos.write(ymp, 0, ymp.length);
            encoding = baos.toByteArray();
        }
    } catch (CertificateEncodingException e) {
        throw new IOException("problem with encoding object in write_X509_AUX");
    }
    out.write(BEF_G + PEM_STRING_X509_TRUSTED + AFT);
    out.newLine();
    writeEncoded(out, encoding);
    out.write(BEF_E + PEM_STRING_X509_TRUSTED + AFT);
    out.newLine();
    out.flush();
}

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

License:LGPL

@Override
public void writeDSAPrivateKey(Writer _out, DSAPrivateKey obj, String algo, char[] f) throws IOException {
    BufferedWriter out = makeBuffered(_out);
    ByteArrayInputStream bIn = new ByteArrayInputStream(getEncoded(obj));
    ASN1InputStream aIn = new ASN1InputStream(bIn);
    PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) aIn.readObject());
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);

    DSAParameter p = DSAParameter.getInstance(info.getAlgorithmId().getParameters());
    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(new DERInteger(0));
    v.add(new DERInteger(p.getP()));
    v.add(new DERInteger(p.getQ()));
    v.add(new DERInteger(p.getG()));

    BigInteger x = obj.getX();/* ww  w.  ja v a  2  s  .c  om*/
    BigInteger y = p.getG().modPow(x, p.getP());

    v.add(new DERInteger(y));
    v.add(new DERInteger(x));

    aOut.writeObject(new DERSequence(v));
    byte[] encoding = bOut.toByteArray();

    if (algo != null && f != null) {
        byte[] salt = new byte[8];
        byte[] encData = null;
        random.nextBytes(salt);
        OpenSSLPBEParametersGenerator pGen = new OpenSSLPBEParametersGenerator();
        pGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(f), salt);
        SecretKey secretKey = null;
        if (algo.equalsIgnoreCase("DESede/CBC/PKCS5Padding")) {
            // generate key
            int keyLength = 24;
            KeyParameter param = (KeyParameter) pGen.generateDerivedParameters(keyLength * 8);
            secretKey = new SecretKeySpec(param.getKey(), "DESede");
        } else {
            throw new IOException("unknown algorithm in write_DSAPrivateKey: " + algo);
        }

        // cipher  
        try {
            Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            c.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(salt));
            encData = c.doFinal(encoding);
        } catch (Exception e) {
            throw new IOException("exception using cipher: " + e.toString());
        }

        // write the data
        out.write(BEF_G + PEM_STRING_DSA + AFT);
        out.newLine();
        out.write("Proc-Type: 4,ENCRYPTED");
        out.newLine();
        out.write("DEK-Info: DES-EDE3-CBC,");
        writeHexEncoded(out, salt);
        out.newLine();
        out.newLine();
        writeEncoded(out, encData);
        out.write(BEF_E + PEM_STRING_DSA + AFT);
        out.flush();
    } else {
        out.write(BEF_G + PEM_STRING_DSA + AFT);
        out.newLine();
        writeEncoded(out, encoding);
        out.write(BEF_E + PEM_STRING_DSA + AFT);
        out.newLine();
        out.flush();
    }
}