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:it.scoppelletti.spaceship.security.FakeCertificateFactory.java

License:Apache License

@SuppressWarnings({ "deprecation", "TryFinallyCanBeTryWithResources" })
public static X509Certificate create(PublicKey publicKey, FakeKeyPairGeneratorSpec spec)
        throws IOException, CertificateParsingException {
    ASN1ObjectIdentifier sigAlgOid;//from   w w  w . j a  v a  2  s . c  om
    AlgorithmIdentifier sigAlgId;
    org.bouncycastle.jce.X509Principal subject;
    ASN1EncodableVector result;
    Certificate cert;
    org.bouncycastle.jce.provider.X509CertificateObject x509Cert;
    TBSCertificate tbsCertificate;
    ASN1InputStream publicKeyInfoIn = null;
    V3TBSCertificateGenerator tbsGenerator;
    byte[] signature;

    sigAlgOid = PKCSObjectIdentifiers.sha256WithRSAEncryption;
    sigAlgId = new AlgorithmIdentifier(sigAlgOid, DERNull.INSTANCE);
    signature = new byte[1];

    tbsGenerator = new V3TBSCertificateGenerator();
    try {
        publicKeyInfoIn = new ASN1InputStream(publicKey.getEncoded());
        tbsGenerator.setSubjectPublicKeyInfo(SubjectPublicKeyInfo.getInstance(publicKeyInfoIn.readObject()));
    } finally {
        if (publicKeyInfoIn != null) {
            publicKeyInfoIn.close();
        }
    }

    subject = new org.bouncycastle.jce.X509Principal(spec.getSubject().getEncoded());

    tbsGenerator.setSerialNumber(new ASN1Integer(spec.getSerialNumber()));
    tbsGenerator.setSubject(subject);
    tbsGenerator.setIssuer(subject);
    tbsGenerator.setStartDate(new Time(spec.getStartDate()));
    tbsGenerator.setEndDate(new Time(spec.getEndDate()));
    tbsGenerator.setSignature(sigAlgId);

    tbsCertificate = tbsGenerator.generateTBSCertificate();

    result = new ASN1EncodableVector();
    result.add(tbsCertificate);
    result.add(sigAlgId);
    result.add(new DERBitString(signature));

    cert = Certificate.getInstance(new DERSequence(result));
    x509Cert = new org.bouncycastle.jce.provider.X509CertificateObject(cert);
    return x509Cert;
}

From source file:jcifs.spnego.NegTokenInit.java

License:Open Source License

@Override
public byte[] toByteArray() {
    try {/*w  w w. j  av a2  s .co  m*/
        ASN1EncodableVector fields = new ASN1EncodableVector();
        Oid[] mechs = getMechanisms();
        if (mechs != null) {
            ASN1EncodableVector vector = new ASN1EncodableVector();
            for (int i = 0; i < mechs.length; i++) {
                vector.add(ASN1ObjectIdentifier.getInstance(mechs[i].getDER()));
            }
            fields.add(new DERTaggedObject(true, 0, new DERSequence(vector)));
        }
        int ctxFlags = getContextFlags();
        if (ctxFlags != 0) {
            fields.add(new DERTaggedObject(true, 1, new DERBitString(ctxFlags)));
        }
        byte[] mechanismToken = getMechanismToken();
        if (mechanismToken != null) {
            fields.add(new DERTaggedObject(true, 2, new DEROctetString(mechanismToken)));
        }
        byte[] mechanismListMIC = getMechanismListMIC();
        if (mechanismListMIC != null) {
            fields.add(new DERTaggedObject(true, 3, new DEROctetString(mechanismListMIC)));
        }

        ASN1EncodableVector ev = new ASN1EncodableVector();
        ev.add(SPNEGO_OID);
        ev.add(new DERTaggedObject(true, 0, new DERSequence(fields)));
        ByteArrayOutputStream collector = new ByteArrayOutputStream();
        DEROutputStream der = new DEROutputStream(collector);
        DERApplicationSpecific derApplicationSpecific = new DERApplicationSpecific(0, ev);
        der.writeObject(derApplicationSpecific);
        return collector.toByteArray();
    } catch (IOException | GSSException ex) {
        throw new IllegalStateException(ex.getMessage());
    }
}

From source file:jcifs.spnego.NegTokenTarg.java

License:Open Source License

@Override
public byte[] toByteArray() {
    try {/*from  w w w  . java 2 s . com*/
        ByteArrayOutputStream collector = new ByteArrayOutputStream();
        DEROutputStream der = new DEROutputStream(collector);
        ASN1EncodableVector fields = new ASN1EncodableVector();
        int res = getResult();
        if (res != UNSPECIFIED_RESULT) {
            fields.add(new DERTaggedObject(true, 0, ASN1Enumerated.getInstance(res)));
        }
        Oid mech = getMechanism();
        if (mech != null) {
            fields.add(new DERTaggedObject(true, 1, ASN1ObjectIdentifier.getInstance(mech.getDER())));
        }
        byte[] mechanismToken = getMechanismToken();
        if (mechanismToken != null) {
            fields.add(new DERTaggedObject(true, 2, new DEROctetString(mechanismToken)));
        }
        byte[] mechanismListMIC = getMechanismListMIC();
        if (mechanismListMIC != null) {
            fields.add(new DERTaggedObject(true, 3, new DEROctetString(mechanismListMIC)));
        }
        der.writeObject(new DERTaggedObject(true, 1, new DERSequence(fields)));
        return collector.toByteArray();
    } catch (IOException | GSSException ex) {
        throw new IllegalStateException(ex.getMessage());
    }
}

From source file:mitm.common.security.certificate.AltNamesBuilder.java

License:Open Source License

/**
 * Builds a GeneralNames instance with the provided altNames 
 * @return/*ww w . ja va  2  s  .  c o  m*/
 */
public GeneralNames buildAltNames() {
    ASN1EncodableVector listOfNames = new ASN1EncodableVector();

    addGeneralNames(rfc822Names, AltNameType.RFC822NAME, listOfNames);
    addGeneralNames(dnsNames, AltNameType.DNSNAME, listOfNames);

    DERSequence derEncodedNames = new DERSequence(listOfNames);

    GeneralNames altNames = GeneralNames.getInstance(derEncodedNames);

    return altNames;
}

From source file:mitm.common.security.crl.CRLDistributionPointsBuilder.java

License:Open Source License

public CRLDistPoint buildCRLDistPoint() {
    if (uris.size() == 0) {
        return null;
    }//  w  ww  .  ja v  a 2  s . c o  m

    CRLDistPoint crlDistPoint;

    ASN1EncodableVector names = new ASN1EncodableVector();

    for (String uri : uris) {
        GeneralName gn = new GeneralName(GeneralName.uniformResourceIdentifier, new DERIA5String(uri));

        names.add(gn);
    }

    GeneralNames gns = GeneralNames.getInstance(new DERSequence(names));

    DistributionPointName dpn = new DistributionPointName(DistributionPointName.FULL_NAME, gns);
    DistributionPoint distp = new DistributionPoint(dpn, null, null);

    crlDistPoint = CRLDistPoint.getInstance(new DERSequence(distp));

    return crlDistPoint;
}

From source file:mitm.common.security.crl.PKIXRevocationChecker.java

License:Open Source License

private X500Name getFullName(X500Principal issuer, DistributionPointName distributionPointName)
        throws IOException {
    ASN1Encodable rdn = distributionPointName.getName();

    X500Name name = null;// w ww.  j  a  v a  2  s  .co m

    if (rdn != null) {
        ASN1EncodableVector v = ASN1Utils.toASN1EncodableVector(issuer);

        v.add(rdn);

        name = X500Name.getInstance(new DERSequence(v).getEncoded(ASN1Encoding.DER));
    }

    return name;
}

From source file:net.jsign.asn1.authenticode.AuthenticodeTimeStampRequest.java

License:Apache License

@Override
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(AuthenticodeObjectIdentifiers.SPC_TIME_STAMP_REQUEST_OBJID);
    v.add(contenInfo);//from   ww w. jav a  2 s.c  o  m
    return new DERSequence(v);
}

From source file:net.jsign.asn1.authenticode.SpcSerializedObject.java

License:Apache License

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

    v.add(classId);/*w  w w .jav a  2 s.c o m*/
    v.add(serializedData);

    return new DERSequence(v);
}

From source file:net.jsign.asn1.authenticode.SpcStatementType.java

License:Apache License

@Override
public ASN1Primitive toASN1Primitive() {
    return new DERSequence(new ASN1Encodable[] { identifier });
}

From source file:net.lightbody.bmp.proxy.selenium.CertificateCreator.java

License:Open Source License

/**
 * Utility method for generating a "standard" server certificate. Recognized by most
 * browsers as valid for SSL/TLS.  These certificates are generated de novo, not from
 * a template, so they will not retain the structure of the original certificate and may
 * not be suitable for applications that require Extended Validation/High Assurance SSL
 * or other distinct extensions or EKU.//from w  ww. j  ava  2s .  c o  m
 *
 * @param newPubKey
 * @param caCert
 * @param caPrivateKey
 * @param hostname
 * @return
 * @throws CertificateParsingException
 * @throws SignatureException
 * @throws InvalidKeyException
 * @throws CertificateExpiredException
 * @throws CertificateNotYetValidException
 * @throws CertificateException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
@SuppressWarnings({ "deprecation", "unused" })
public static X509Certificate generateStdSSLServerCertificate(final PublicKey newPubKey,
        final X509Certificate caCert, final PrivateKey caPrivateKey, final String subject)
        throws CertificateParsingException, SignatureException, InvalidKeyException,
        CertificateExpiredException, CertificateNotYetValidException, CertificateException,
        NoSuchAlgorithmException, NoSuchProviderException {
    X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();

    v3CertGen.setSubjectDN(new X500Principal(subject));
    v3CertGen.setSignatureAlgorithm(CertificateCreator.SIGN_ALGO);
    v3CertGen.setPublicKey(newPubKey);
    v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + 30L * 60 * 60 * 24 * 30 * 12));
    v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30 * 12));
    v3CertGen.setIssuerDN(caCert.getSubjectX500Principal());

    // Firefox actually tracks serial numbers within a CA and refuses to validate if it sees duplicates
    // This is not a secure serial number generator, (duh!) but it's good enough for our purposes.
    v3CertGen.setSerialNumber(new BigInteger(Long.toString(System.currentTimeMillis())));

    v3CertGen.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(false));

    v3CertGen.addExtension(X509Extensions.SubjectKeyIdentifier, false,
            new SubjectKeyIdentifierStructure(newPubKey));

    v3CertGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false,
            new AuthorityKeyIdentifierStructure(caCert.getPublicKey()));

    //       Firefox 2 disallows these extensions in an SSL server cert.  IE7 doesn't care.
    //      v3CertGen.addExtension(
    //            X509Extensions.KeyUsage,
    //            false,
    //            new KeyUsage(KeyUsage.dataEncipherment | KeyUsage.digitalSignature ) );

    DEREncodableVector typicalSSLServerExtendedKeyUsages = new DEREncodableVector();

    typicalSSLServerExtendedKeyUsages.add(new DERObjectIdentifier(ExtendedKeyUsageConstants.serverAuth));
    typicalSSLServerExtendedKeyUsages.add(new DERObjectIdentifier(ExtendedKeyUsageConstants.clientAuth));
    typicalSSLServerExtendedKeyUsages
            .add(new DERObjectIdentifier(ExtendedKeyUsageConstants.netscapeServerGatedCrypto));
    typicalSSLServerExtendedKeyUsages
            .add(new DERObjectIdentifier(ExtendedKeyUsageConstants.msServerGatedCrypto));

    v3CertGen.addExtension(X509Extensions.ExtendedKeyUsage, false,
            new DERSequence(typicalSSLServerExtendedKeyUsages));

    //  Disabled by default.  Left in comments in case this is desired.
    //
    //      v3CertGen.addExtension(
    //            X509Extensions.AuthorityInfoAccess,
    //            false,
    //            new AuthorityInformationAccess(new DERObjectIdentifier(OID_ID_AD_CAISSUERS),
    //                  new GeneralName(GeneralName.uniformResourceIdentifier, "http://" + subject + "/aia")));

    //      v3CertGen.addExtension(
    //            X509Extensions.CRLDistributionPoints,
    //            false,
    //            new CRLDistPoint(new DistributionPoint[] {}));

    X509Certificate cert = v3CertGen.generate(caPrivateKey, "BC");

    return cert;
}