Example usage for org.bouncycastle.asn1 ASN1Integer ASN1Integer

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

Introduction

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

Prototype

public ASN1Integer(byte[] bytes) 

Source Link

Document

Construct an INTEGER from the passed in byte array.

Usage

From source file:com.guardtime.asn1.CertTokenRequest.java

License:Apache License

/**
 * Composes a new {@code CertTokenRequest} structure containing the given
 * history identifier and no extensions.
 *
 * @param historyId//from  w  w w. j av  a2  s .  c o  m
 *            identifier of the second for which the certification token in
 *            requested.
 * @return a new certification token request.
 */
public static CertTokenRequest compose(BigInteger historyId) {
    if (historyId == null) {
        throw new IllegalArgumentException("invalid history ID: null");
    }

    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(VERSION));
    v.add(new ASN1Integer(historyId));
    ASN1Sequence seq = new DERSequence(v);
    Asn1CertTokenRequest req = new Asn1CertTokenRequest(seq);
    return new CertTokenRequest(req);
}

From source file:com.itextpdf.signatures.PdfPKCS7.java

License:Open Source License

/**
 * Gets the bytes for the PKCS7SignedData object. Optionally the authenticatedAttributes
 * in the signerInfo can also be set, OR a time-stamp-authority client
 * may be provided.//www.  j  a  v a 2s  .com
 *
 * @param secondDigest the digest in the authenticatedAttributes
 * @param tsaClient    TSAClient - null or an optional time stamp authority client
 * @return byte[] the bytes for the PKCS7SignedData object
 */
public byte[] getEncodedPKCS7(byte[] secondDigest, ITSAClient tsaClient, byte[] ocsp,
        Collection<byte[]> crlBytes, PdfSigner.CryptoStandard sigtype) {
    try {
        if (externalDigest != null) {
            digest = externalDigest;
            if (RSAdata != null)
                RSAdata = externalRSAdata;
        } else if (externalRSAdata != null && RSAdata != null) {
            RSAdata = externalRSAdata;
            sig.update(RSAdata);
            digest = sig.sign();
        } else {
            if (RSAdata != null) {
                RSAdata = messageDigest.digest();
                sig.update(RSAdata);
            }
            digest = sig.sign();
        }

        // Create the set of Hash algorithms
        ASN1EncodableVector digestAlgorithms = new ASN1EncodableVector();
        for (Object element : digestalgos) {
            ASN1EncodableVector algos = new ASN1EncodableVector();
            algos.add(new ASN1ObjectIdentifier((String) element));
            algos.add(DERNull.INSTANCE);
            digestAlgorithms.add(new DERSequence(algos));
        }

        // Create the contentInfo.
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(new ASN1ObjectIdentifier(SecurityIDs.ID_PKCS7_DATA));
        if (RSAdata != null)
            v.add(new DERTaggedObject(0, new DEROctetString(RSAdata)));
        DERSequence contentinfo = new DERSequence(v);

        // Get all the certificates
        //
        v = new ASN1EncodableVector();
        for (Object element : certs) {
            ASN1InputStream tempstream = new ASN1InputStream(
                    new ByteArrayInputStream(((X509Certificate) element).getEncoded()));
            v.add(tempstream.readObject());
        }

        DERSet dercertificates = new DERSet(v);

        // Create signerinfo structure.
        //
        ASN1EncodableVector signerinfo = new ASN1EncodableVector();

        // Add the signerInfo version
        //
        signerinfo.add(new ASN1Integer(signerversion));

        v = new ASN1EncodableVector();
        v.add(CertificateInfo.getIssuer(signCert.getTBSCertificate()));
        v.add(new ASN1Integer(signCert.getSerialNumber()));
        signerinfo.add(new DERSequence(v));

        // Add the digestAlgorithm
        v = new ASN1EncodableVector();
        v.add(new ASN1ObjectIdentifier(digestAlgorithmOid));
        v.add(DERNull.INSTANCE);
        signerinfo.add(new DERSequence(v));

        // add the authenticated attribute if present
        if (secondDigest != null) {
            signerinfo.add(new DERTaggedObject(false, 0,
                    getAuthenticatedAttributeSet(secondDigest, ocsp, crlBytes, sigtype)));
        }
        // Add the digestEncryptionAlgorithm
        v = new ASN1EncodableVector();
        v.add(new ASN1ObjectIdentifier(digestEncryptionAlgorithmOid));
        v.add(DERNull.INSTANCE);
        signerinfo.add(new DERSequence(v));

        // Add the digest
        signerinfo.add(new DEROctetString(digest));

        // When requested, go get and add the timestamp. May throw an exception.
        // Added by Martin Brunecky, 07/12/2007 folowing Aiken Sam, 2006-11-15
        // Sam found Adobe expects time-stamped SHA1-1 of the encrypted digest
        if (tsaClient != null) {
            byte[] tsImprint = tsaClient.getMessageDigest().digest(digest);
            byte[] tsToken = tsaClient.getTimeStampToken(tsImprint);
            if (tsToken != null) {
                ASN1EncodableVector unauthAttributes = buildUnauthenticatedAttributes(tsToken);
                if (unauthAttributes != null) {
                    signerinfo.add(new DERTaggedObject(false, 1, new DERSet(unauthAttributes)));
                }
            }
        }

        // Finally build the body out of all the components above
        ASN1EncodableVector body = new ASN1EncodableVector();
        body.add(new ASN1Integer(version));
        body.add(new DERSet(digestAlgorithms));
        body.add(contentinfo);
        body.add(new DERTaggedObject(false, 0, dercertificates));

        // Only allow one signerInfo
        body.add(new DERSet(new DERSequence(signerinfo)));

        // Now we have the body, wrap it in it's PKCS7Signed shell
        // and return it
        //
        ASN1EncodableVector whole = new ASN1EncodableVector();
        whole.add(new ASN1ObjectIdentifier(SecurityIDs.ID_PKCS7_SIGNED_DATA));
        whole.add(new DERTaggedObject(0, new DERSequence(body)));

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        ASN1OutputStream dout = new ASN1OutputStream(bOut);
        dout.writeObject(new DERSequence(whole));
        dout.close();

        return bOut.toByteArray();
    } catch (Exception e) {
        throw new PdfException(e);
    }
}

From source file:com.itextpdf.text.pdf.security.PdfPKCS7.java

License:Open Source License

/**
 * Gets the bytes for the PKCS7SignedData object. Optionally the authenticatedAttributes
 * in the signerInfo can also be set, OR a time-stamp-authority client
 * may be provided.//from w w  w .  j  a  va2s  .  c  o m
 * @param secondDigest the digest in the authenticatedAttributes
 * @param signingTime the signing time in the authenticatedAttributes
 * @param tsaClient TSAClient - null or an optional time stamp authority client
 * @return byte[] the bytes for the PKCS7SignedData object
 * @since   2.1.6
 */
public byte[] getEncodedPKCS7(byte secondDigest[], Calendar signingTime, TSAClient tsaClient, byte[] ocsp,
        Collection<byte[]> crlBytes, CryptoStandard sigtype) {
    try {
        if (externalDigest != null) {
            digest = externalDigest;
            if (RSAdata != null)
                RSAdata = externalRSAdata;
        } else if (externalRSAdata != null && RSAdata != null) {
            RSAdata = externalRSAdata;
            sig.update(RSAdata);
            digest = sig.sign();
        } else {
            if (RSAdata != null) {
                RSAdata = messageDigest.digest();
                sig.update(RSAdata);
            }
            digest = sig.sign();
        }

        // Create the set of Hash algorithms
        ASN1EncodableVector digestAlgorithms = new ASN1EncodableVector();
        for (Object element : digestalgos) {
            ASN1EncodableVector algos = new ASN1EncodableVector();
            algos.add(new ASN1ObjectIdentifier((String) element));
            algos.add(DERNull.INSTANCE);
            digestAlgorithms.add(new DERSequence(algos));
        }

        // Create the contentInfo.
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(new ASN1ObjectIdentifier(SecurityIDs.ID_PKCS7_DATA));
        if (RSAdata != null)
            v.add(new DERTaggedObject(0, new DEROctetString(RSAdata)));
        DERSequence contentinfo = new DERSequence(v);

        // Get all the certificates
        //
        v = new ASN1EncodableVector();
        for (Object element : certs) {
            ASN1InputStream tempstream = new ASN1InputStream(
                    new ByteArrayInputStream(((X509Certificate) element).getEncoded()));
            v.add(tempstream.readObject());
        }

        DERSet dercertificates = new DERSet(v);

        // Create signerinfo structure.
        //
        ASN1EncodableVector signerinfo = new ASN1EncodableVector();

        // Add the signerInfo version
        //
        signerinfo.add(new ASN1Integer(signerversion));

        v = new ASN1EncodableVector();
        v.add(CertificateInfo.getIssuer(signCert.getTBSCertificate()));
        v.add(new ASN1Integer(signCert.getSerialNumber()));
        signerinfo.add(new DERSequence(v));

        // Add the digestAlgorithm
        v = new ASN1EncodableVector();
        v.add(new ASN1ObjectIdentifier(digestAlgorithmOid));
        v.add(new DERNull());
        signerinfo.add(new DERSequence(v));

        // add the authenticated attribute if present
        if (secondDigest != null && signingTime != null) {
            signerinfo.add(new DERTaggedObject(false, 0,
                    getAuthenticatedAttributeSet(secondDigest, signingTime, ocsp, crlBytes, sigtype)));
        }
        // Add the digestEncryptionAlgorithm
        v = new ASN1EncodableVector();
        v.add(new ASN1ObjectIdentifier(digestEncryptionAlgorithmOid));
        v.add(new DERNull());
        signerinfo.add(new DERSequence(v));

        // Add the digest
        signerinfo.add(new DEROctetString(digest));

        // When requested, go get and add the timestamp. May throw an exception.
        // Added by Martin Brunecky, 07/12/2007 folowing Aiken Sam, 2006-11-15
        // Sam found Adobe expects time-stamped SHA1-1 of the encrypted digest
        if (tsaClient != null) {
            byte[] tsImprint = tsaClient.getMessageDigest().digest(digest);
            byte[] tsToken = tsaClient.getTimeStampToken(tsImprint);
            if (tsToken != null) {
                ASN1EncodableVector unauthAttributes = buildUnauthenticatedAttributes(tsToken);
                if (unauthAttributes != null) {
                    signerinfo.add(new DERTaggedObject(false, 1, new DERSet(unauthAttributes)));
                }
            }
        }

        // Finally build the body out of all the components above
        ASN1EncodableVector body = new ASN1EncodableVector();
        body.add(new ASN1Integer(version));
        body.add(new DERSet(digestAlgorithms));
        body.add(contentinfo);
        body.add(new DERTaggedObject(false, 0, dercertificates));

        // Only allow one signerInfo
        body.add(new DERSet(new DERSequence(signerinfo)));

        // Now we have the body, wrap it in it's PKCS7Signed shell
        // and return it
        //
        ASN1EncodableVector whole = new ASN1EncodableVector();
        whole.add(new ASN1ObjectIdentifier(SecurityIDs.ID_PKCS7_SIGNED_DATA));
        whole.add(new DERTaggedObject(0, new DERSequence(body)));

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        ASN1OutputStream dout = new ASN1OutputStream(bOut);
        dout.writeObject(new DERSequence(whole));
        dout.close();

        return bOut.toByteArray();
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:com.raphfrk.craftproxyclient.net.protocol.p16x.P16xProtocol.java

License:Open Source License

public byte[] encodeRSAPublicKey(RSAKeyParameters key) {
    if (((RSAKeyParameters) key).isPrivate()) {
        return null;
    }/*  ww w  .ja  v a 2 s .co m*/

    RSAKeyParameters rsaKey = (RSAKeyParameters) key;

    ASN1EncodableVector encodable = new ASN1EncodableVector();
    encodable.add(new ASN1Integer(rsaKey.getModulus()));
    encodable.add(new ASN1Integer(rsaKey.getExponent()));

    return KeyUtil.getEncodedSubjectPublicKeyInfo(
            new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE),
            new DERSequence(encodable));
}

From source file:com.vmware.identity.rest.core.test.util.CertificateGenerator.java

License:Open Source License

/**
 * Generate a self-signed X.509 certificate
 *
 * @param pair the key pair to use when signing the certificate
 * @param algorithm the signing algorithm to use
 * @param dn the X.509 distinguished name for the certificate
 * @return a self-signed X.509 certificate
 * @throws NoSuchAlgorithmException/* w  w  w .  ja v  a 2  s.  c om*/
 * @throws NoSuchProviderException
 * @throws InvalidKeyException
 * @throws SignatureException
 * @throws IOException
 * @throws CertificateException
 */
public static X509Certificate generateSelfSignedCertificate(KeyPair pair, AlgorithmName algorithm, String dn)
        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException,
        IOException, CertificateException {
    if (Security.getProvider("BC") == null) {
        Security.addProvider(new BouncyCastleProvider());
    }

    AtomicLong serialNumber = new AtomicLong(System.currentTimeMillis());
    X500Name owner = new X500Name(dn);

    V1TBSCertificateGenerator generator = new V1TBSCertificateGenerator();
    long time = System.currentTimeMillis();

    generator.setSerialNumber(new ASN1Integer(serialNumber.getAndIncrement()));
    generator.setIssuer(owner);
    generator.setSubject(owner);
    generator.setStartDate(new Time(new Date(time - 5000)));
    generator.setEndDate(new Time(new Date(time + 30 * 60 * 1000)));
    generator.setSignature(ALGORITHM_IDS.get(algorithm));
    generator.setSubjectPublicKeyInfo(SubjectPublicKeyInfo.getInstance(pair.getPublic().getEncoded()));

    Signature sig = Signature.getInstance(algorithm.toString(), "BC");

    sig.initSign(pair.getPrivate());

    sig.update(generator.generateTBSCertificate().getEncoded(ASN1Encoding.DER));

    TBSCertificate tbsCert = generator.generateTBSCertificate();

    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(tbsCert);
    v.add(ALGORITHM_IDS.get(algorithm));
    v.add(new DERBitString(sig.sign()));

    return (X509Certificate) CertificateFactory.getInstance("X.509", "BC")
            .generateCertificate(new ByteArrayInputStream(new DERSequence(v).getEncoded(ASN1Encoding.DER)));
}

From source file:com.vvote.thirdparty.ximix.util.BLSKeyStore.java

License:Apache License

private X509CertificateHolder createCertificate(String keyID, int sequenceNo, PrivateKey privKey)
        throws GeneralSecurityException, OperatorCreationException, IOException {
    String name = "C=AU, O=Ximix Network Node, OU=" + "Util";

    ////  w ww  . j av a 2  s .  co  m
    // create the certificate - version 3
    //
    X509v3CertificateBuilder v3CertBuilder = new X509v3CertificateBuilder(new X500Name(name),
            BigInteger.valueOf(1), new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30),
            new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365)), new X500Name(name),
            this.fetchPublicKey(keyID));

    // we use keyUsage extension to distinguish between signing and encryption keys

    if (signingKeys.contains(keyID)) {
        v3CertBuilder.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature));
    } else {
        v3CertBuilder.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.dataEncipherment));
    }

    v3CertBuilder.addExtension(XimixObjectIdentifiers.ximixShareIdExtension, true, new ASN1Integer(sequenceNo));

    return v3CertBuilder.build(new JcaContentSignerBuilder("SHA1withECDSA").setProvider("BC").build(privKey));
}

From source file:com.vvote.thirdparty.ximix.util.PartialPublicKeyInfo.java

License:Apache License

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

    v.add(new ASN1Integer(sequenceNo));
    v.add(partialKeyInfo);/*from   w  w  w  . j a va  2s  .c  o  m*/

    return new DERSequence(v);
}

From source file:de.carne.certmgr.store.provider.bouncycastle.BouncyCastleASN1Encoder.java

License:Open Source License

@Override
public void asn1EncodeInteger(int i) {
    asn1Encode(new ASN1Integer(i));
}

From source file:de.tsenger.animamea.asn1.AmDHPublicKey.java

License:Open Source License

public AmDHPublicKey(String oidString, BigInteger p, BigInteger q, BigInteger g, BigInteger y) {
    super(oidString);
    this.p = new DERTaggedObject(false, 1, new ASN1Integer(p));
    this.q = new DERTaggedObject(false, 2, new ASN1Integer(q));
    this.g = new DERTaggedObject(false, 3, new ASN1Integer(g));
    this.y = new DERTaggedObject(false, 4, new ASN1Integer(y));
    vec.add(this.p);
    vec.add(this.q);
    vec.add(this.g);
    vec.add(this.y);
}

From source file:de.tsenger.animamea.asn1.AmDHPublicKey.java

License:Open Source License

/**
 * Konstruktor fr Ephemeral Public Key (TR-03110 V2.05 D.3.4)
 * @param oidString OID//from  ww w  . j a  v  a 2  s .co m
 * @param y public value
 */
public AmDHPublicKey(String oidString, BigInteger y) {
    super(oidString);
    this.y = new DERTaggedObject(false, 4, new ASN1Integer(y));
    vec.add(this.y);
}