Java Code Examples for sun.security.util.DerOutputStream#putBitString()

The following examples show how to use sun.security.util.DerOutputStream#putBitString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SM2X509CertImpl.java    From julongchain with Apache License 2.0 6 votes vote down vote up
public void sm2Sign(IKey privateKey, AlgorithmId algorithmId) throws JulongChainException, CertificateException {
    if (isReadOnly()) {
        throw new CertificateEncodingException("cannot over-write existing certificate");
    }

    try {
        this.algId = algorithmId;
        DerOutputStream signedCert = new DerOutputStream();
        DerOutputStream signedData = new DerOutputStream();
        this.info.encode(signedData);
        byte[] signedBytes = signedData.toByteArray();
        this.algId.encode(signedData);

        this.signature = CspHelper.getCsp().sign(privateKey, signedBytes, new SM2SignerOpts());

        signedData.putBitString(this.signature);
        signedCert.write((byte)48, signedData);
        setSignedCert(signedCert.toByteArray());
        setReadOnly(true);
    } catch (IOException e) {
        throw new CertificateEncodingException(e.toString());
    }
}
 
Example 2
Source File: CertificateBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Encode the contents of the outer-most ASN.1 SEQUENCE:
 *
 * <PRE>
 *  Certificate  ::=  SEQUENCE  {
 *      tbsCertificate       TBSCertificate,
 *      signatureAlgorithm   AlgorithmIdentifier,
 *      signatureValue       BIT STRING  }
 * </PRE>
 *
 * @param issuerCert The certificate of the issuing authority, or
 * {@code null} if the resulting certificate is self-signed.
 * @param issuerKey The private key of the issuing authority
 * @param signAlg The signature algorithm object
 *
 * @return The DER-encoded X.509 certificate
 *
 * @throws CertificateException If an error occurs during the
 * signing process.
 * @throws IOException if an encoding error occurs.
 */
private byte[] encodeTopLevel(X509Certificate issuerCert,
        PrivateKey issuerKey, AlgorithmId signAlg)
        throws CertificateException, IOException {
    DerOutputStream outerSeq = new DerOutputStream();
    DerOutputStream topLevelItems = new DerOutputStream();

    tbsCertBytes = encodeTbsCert(issuerCert, signAlg);
    topLevelItems.write(tbsCertBytes);
    try {
        signatureBytes = signCert(issuerKey, signAlg);
    } catch (GeneralSecurityException ge) {
        throw new CertificateException(ge);
    }
    signAlg.derEncode(topLevelItems);
    topLevelItems.putBitString(signatureBytes);
    outerSeq.write(DerValue.tag_Sequence, topLevelItems);

    return outerSeq.toByteArray();
}
 
Example 3
Source File: SimpleOCSPServer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encodeBasicOcspResponse() throws IOException {
    DerOutputStream outerSeq = new DerOutputStream();
    DerOutputStream basicORItemStream = new DerOutputStream();

    // Encode the tbsResponse
    byte[] tbsResponseBytes = encodeTbsResponse();
    basicORItemStream.write(tbsResponseBytes);

    try {
        sigAlgId.derEncode(basicORItemStream);

        // Create the signature
        Signature sig = Signature.getInstance(sigAlgId.getName());
        sig.initSign(signerKey);
        sig.update(tbsResponseBytes);
        signature = sig.sign();
        basicORItemStream.putBitString(signature);
    } catch (GeneralSecurityException exc) {
        err(exc);
        throw new IOException(exc);
    }

    // Add certificates
    try {
        DerOutputStream certStream = new DerOutputStream();
        ArrayList<DerValue> certList = new ArrayList<>();
        if (signerCert != issuerCert) {
            certList.add(new DerValue(signerCert.getEncoded()));
        }
        certList.add(new DerValue(issuerCert.getEncoded()));
        DerValue[] dvals = new DerValue[certList.size()];
        certStream.putSequence(certList.toArray(dvals));
        basicORItemStream.write(DerValue.createTag(DerValue.TAG_CONTEXT,
                true, (byte)0), certStream);
    } catch (CertificateEncodingException cex) {
        err(cex);
        throw new IOException(cex);
    }

    // Commit the outermost sequence bytes
    outerSeq.write(DerValue.tag_Sequence, basicORItemStream);
    return outerSeq.toByteArray();
}