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

The following examples show how to use sun.security.util.DerOutputStream#write() . 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: EncryptedPrivateKeyInfo.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this class.
 */
public byte[] getEncoded()
    throws IOException
{
    if (this.encoded != null) return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    // encode encryption algorithm
    algid.encode(tmp);

    // encode encrypted data
    tmp.putOctetString(encryptedData);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 2
Source File: EncryptedPrivateKeyInfo.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this class.
 */
public byte[] getEncoded()
    throws IOException
{
    if (this.encoded != null) return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    // encode encryption algorithm
    algid.encode(tmp);

    // encode encrypted data
    tmp.putOctetString(encryptedData);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 3
Source File: EncryptedPrivateKeyInfo.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this class.
 */
public byte[] getEncoded()
    throws IOException
{
    if (this.encoded != null) return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    // encode encryption algorithm
    algid.encode(tmp);

    // encode encrypted data
    tmp.putOctetString(encryptedData);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 4
Source File: X509CertPath.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Encode the CertPath using PKIPATH format.
 *
 * @return a byte array containing the binary encoding of the PkiPath object
 * @exception CertificateEncodingException if an exception occurs
 */
private byte[] encodePKIPATH() throws CertificateEncodingException {

    ListIterator<X509Certificate> li = certs.listIterator(certs.size());
    try {
        DerOutputStream bytes = new DerOutputStream();
        // encode certs in reverse order (trust anchor to target)
        // according to PkiPath format
        while (li.hasPrevious()) {
            X509Certificate cert = li.previous();
            // check for duplicate cert
            if (certs.lastIndexOf(cert) != certs.indexOf(cert)) {
                throw new CertificateEncodingException
                    ("Duplicate Certificate");
            }
            // get encoded certificates
            byte[] encoded = cert.getEncoded();
            bytes.write(encoded);
        }

        // Wrap the data in a SEQUENCE
        DerOutputStream derout = new DerOutputStream();
        derout.write(DerValue.tag_SequenceOf, bytes);
        return derout.toByteArray();

    } catch (IOException ioe) {
       throw new CertificateEncodingException("IOException encoding " +
               "PkiPath data: " + ioe, ioe);
    }
}
 
Example 5
Source File: SignerInfo.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * DER encode this object onto an output stream.
 * Implements the {@code DerEncoder} interface.
 *
 * @param out
 * the output stream on which to write the DER encoding.
 *
 * @exception IOException on encoding error.
 */
public void derEncode(OutputStream out) throws IOException {
    DerOutputStream seq = new DerOutputStream();
    seq.putInteger(version);
    DerOutputStream issuerAndSerialNumber = new DerOutputStream();
    issuerName.encode(issuerAndSerialNumber);
    issuerAndSerialNumber.putInteger(certificateSerialNumber);
    seq.write(DerValue.tag_Sequence, issuerAndSerialNumber);

    digestAlgorithmId.encode(seq);

    // encode authenticated attributes if there are any
    if (authenticatedAttributes != null)
        authenticatedAttributes.encode((byte)0xA0, seq);

    digestEncryptionAlgorithmId.encode(seq);

    seq.putOctetString(encryptedDigest);

    // encode unauthenticated attributes if there are any
    if (unauthenticatedAttributes != null)
        unauthenticatedAttributes.encode((byte)0xA1, seq);

    DerOutputStream tmp = new DerOutputStream();
    tmp.write(DerValue.tag_Sequence, seq);

    out.write(tmp.toByteArray());
}
 
Example 6
Source File: ExtendedKeyUsageExtension.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void encodeThis() throws IOException {
    if (keyUsages == null || keyUsages.isEmpty()) {
        this.extensionValue = null;
        return;
    }
    DerOutputStream os = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    for (int i = 0; i < keyUsages.size(); i++) {
        tmp.putOID(keyUsages.elementAt(i));
    }

    os.write(DerValue.tag_Sequence, tmp);
    this.extensionValue = os.toByteArray();
}
 
Example 7
Source File: PolicyInformation.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write the PolicyInformation to the DerOutputStream.
 *
 * @param out the DerOutputStream to write the extension to.
 * @exception IOException on encoding errors.
 */
public void encode(DerOutputStream out) throws IOException {
    DerOutputStream tmp = new DerOutputStream();
    policyIdentifier.encode(tmp);
    if (!policyQualifiers.isEmpty()) {
        DerOutputStream tmp2 = new DerOutputStream();
        for (PolicyQualifierInfo pq : policyQualifiers) {
            tmp2.write(pq.getEncoded());
        }
        tmp.write(DerValue.tag_Sequence, tmp2);
    }
    out.write(DerValue.tag_Sequence, tmp);
}
 
Example 8
Source File: DSAParameters.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected byte[] engineGetEncoded() throws IOException {
    DerOutputStream out = new DerOutputStream();
    DerOutputStream bytes = new DerOutputStream();

    bytes.putInteger(p);
    bytes.putInteger(q);
    bytes.putInteger(g);
    out.write(DerValue.tag_Sequence, bytes);
    return out.toByteArray();
}
 
Example 9
Source File: MacData.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this object.
 * @return the ASN.1 encoding.
 * @exception IOException if error occurs when constructing its
 * ASN.1 encoding.
 */
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
{
    if (this.encoded != null)
        return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    DerOutputStream tmp2 = new DerOutputStream();
    // encode encryption algorithm
    AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
    algid.encode(tmp2);

    // encode digest data
    tmp2.putOctetString(digest);

    tmp.write(DerValue.tag_Sequence, tmp2);

    // encode salt
    tmp.putOctetString(macSalt);

    // encode iterations
    tmp.putInteger(iterations);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 10
Source File: TSRequest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public byte[] encode() throws IOException {

        DerOutputStream request = new DerOutputStream();

        // encode version
        request.putInteger(version);

        // encode messageImprint
        DerOutputStream messageImprint = new DerOutputStream();
        hashAlgorithmId.encode(messageImprint);
        messageImprint.putOctetString(hashValue);
        request.write(DerValue.tag_Sequence, messageImprint);

        // encode optional elements

        if (policyId != null) {
            request.putOID(new ObjectIdentifier(policyId));
        }
        if (nonce != null) {
            request.putInteger(nonce);
        }
        if (returnCertificate) {
            request.putBoolean(true);
        }

        DerOutputStream out = new DerOutputStream();
        out.write(DerValue.tag_Sequence, request);
        return out.toByteArray();
    }
 
Example 11
Source File: PolicyInformation.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write the PolicyInformation to the DerOutputStream.
 *
 * @param out the DerOutputStream to write the extension to.
 * @exception IOException on encoding errors.
 */
public void encode(DerOutputStream out) throws IOException {
    DerOutputStream tmp = new DerOutputStream();
    policyIdentifier.encode(tmp);
    if (!policyQualifiers.isEmpty()) {
        DerOutputStream tmp2 = new DerOutputStream();
        for (PolicyQualifierInfo pq : policyQualifiers) {
            tmp2.write(pq.getEncoded());
        }
        tmp.write(DerValue.tag_Sequence, tmp2);
    }
    out.write(DerValue.tag_Sequence, tmp);
}
 
Example 12
Source File: PKCS12KeyStore.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private byte[] calculateMac(char[] passwd, byte[] data)
    throws IOException
{
    byte[] mData = null;
    String algName = "SHA1";

    try {
        // Generate a random salt.
        byte[] salt = getSalt();

        // generate MAC (MAC key is generated within JCE)
        Mac m = Mac.getInstance("HmacPBESHA1");
        PBEParameterSpec params =
                    new PBEParameterSpec(salt, MAC_ITERATION_COUNT);
        SecretKey key = getPBEKey(passwd);
        m.init(key, params);
        m.update(data);
        byte[] macResult = m.doFinal();

        // encode as MacData
        MacData macData = new MacData(algName, macResult, salt,
                                            MAC_ITERATION_COUNT);
        DerOutputStream bytes = new DerOutputStream();
        bytes.write(macData.getEncoded());
        mData = bytes.toByteArray();
    } catch (Exception e) {
        throw new IOException("calculateMac failed: " + e, e);
    }
    return mData;
}
 
Example 13
Source File: CertificatePoliciesExtension.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void encodeThis() throws IOException {
    if (certPolicies == null || certPolicies.isEmpty()) {
        this.extensionValue = null;
    } else {
        DerOutputStream os = new DerOutputStream();
        DerOutputStream tmp = new DerOutputStream();

        for (PolicyInformation info : certPolicies) {
            info.encode(tmp);
        }

        os.write(DerValue.tag_Sequence, tmp);
        this.extensionValue = os.toByteArray();
    }
}
 
Example 14
Source File: AuthorityInfoAccessExtension.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void encodeThis() throws IOException {
    if (accessDescriptions.isEmpty()) {
        this.extensionValue = null;
    } else {
        DerOutputStream ads = new DerOutputStream();
        for (AccessDescription accessDescription : accessDescriptions) {
            accessDescription.encode(ads);
        }
        DerOutputStream seq = new DerOutputStream();
        seq.write(DerValue.tag_Sequence, ads);
        this.extensionValue = seq.toByteArray();
    }
}
 
Example 15
Source File: DSAParameters.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
protected byte[] engineGetEncoded() throws IOException {
    DerOutputStream out = new DerOutputStream();
    DerOutputStream bytes = new DerOutputStream();

    bytes.putInteger(p);
    bytes.putInteger(q);
    bytes.putInteger(g);
    out.write(DerValue.tag_Sequence, bytes);
    return out.toByteArray();
}
 
Example 16
Source File: SubjectInfoAccessExtension.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void encodeThis() throws IOException {
    if (accessDescriptions.isEmpty()) {
        this.extensionValue = null;
    } else {
        DerOutputStream ads = new DerOutputStream();
        for (AccessDescription accessDescription : accessDescriptions) {
            accessDescription.encode(ads);
        }
        DerOutputStream seq = new DerOutputStream();
        seq.write(DerValue.tag_Sequence, ads);
        this.extensionValue = seq.toByteArray();
    }
}
 
Example 17
Source File: SubjectInfoAccessExtension.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void encodeThis() throws IOException {
    if (accessDescriptions.isEmpty()) {
        this.extensionValue = null;
    } else {
        DerOutputStream ads = new DerOutputStream();
        for (AccessDescription accessDescription : accessDescriptions) {
            accessDescription.encode(ads);
        }
        DerOutputStream seq = new DerOutputStream();
        seq.write(DerValue.tag_Sequence, ads);
        this.extensionValue = seq.toByteArray();
    }
}
 
Example 18
Source File: PolicyInformation.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write the PolicyInformation to the DerOutputStream.
 *
 * @param out the DerOutputStream to write the extension to.
 * @exception IOException on encoding errors.
 */
public void encode(DerOutputStream out) throws IOException {
    DerOutputStream tmp = new DerOutputStream();
    policyIdentifier.encode(tmp);
    if (!policyQualifiers.isEmpty()) {
        DerOutputStream tmp2 = new DerOutputStream();
        for (PolicyQualifierInfo pq : policyQualifiers) {
            tmp2.write(pq.getEncoded());
        }
        tmp.write(DerValue.tag_Sequence, tmp2);
    }
    out.write(DerValue.tag_Sequence, tmp);
}
 
Example 19
Source File: MacData.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this object.
 * @return the ASN.1 encoding.
 * @exception IOException if error occurs when constructing its
 * ASN.1 encoding.
 */
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
{
    if (this.encoded != null)
        return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    DerOutputStream tmp2 = new DerOutputStream();
    // encode encryption algorithm
    AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
    algid.encode(tmp2);

    // encode digest data
    tmp2.putOctetString(digest);

    tmp.write(DerValue.tag_Sequence, tmp2);

    // encode salt
    tmp.putOctetString(macSalt);

    // encode iterations
    tmp.putInteger(iterations);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 20
Source File: PKCS12KeyStore.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private byte[] getBagAttributes(String alias, byte[] keyId,
    ObjectIdentifier[] trustedUsage,
    Set<KeyStore.Entry.Attribute> attributes) throws IOException {

    byte[] localKeyID = null;
    byte[] friendlyName = null;
    byte[] trustedKeyUsage = null;

    // return null if all three attributes are null
    if ((alias == null) && (keyId == null) && (trustedKeyUsage == null)) {
        return null;
    }

    // SafeBag Attributes
    DerOutputStream bagAttrs = new DerOutputStream();

    // Encode the friendlyname oid.
    if (alias != null) {
        DerOutputStream bagAttr1 = new DerOutputStream();
        bagAttr1.putOID(PKCS9FriendlyName_OID);
        DerOutputStream bagAttrContent1 = new DerOutputStream();
        DerOutputStream bagAttrValue1 = new DerOutputStream();
        bagAttrContent1.putBMPString(alias);
        bagAttr1.write(DerValue.tag_Set, bagAttrContent1);
        bagAttrValue1.write(DerValue.tag_Sequence, bagAttr1);
        friendlyName = bagAttrValue1.toByteArray();
    }

    // Encode the localkeyId oid.
    if (keyId != null) {
        DerOutputStream bagAttr2 = new DerOutputStream();
        bagAttr2.putOID(PKCS9LocalKeyId_OID);
        DerOutputStream bagAttrContent2 = new DerOutputStream();
        DerOutputStream bagAttrValue2 = new DerOutputStream();
        bagAttrContent2.putOctetString(keyId);
        bagAttr2.write(DerValue.tag_Set, bagAttrContent2);
        bagAttrValue2.write(DerValue.tag_Sequence, bagAttr2);
        localKeyID = bagAttrValue2.toByteArray();
    }

    // Encode the trustedKeyUsage oid.
    if (trustedUsage != null) {
        DerOutputStream bagAttr3 = new DerOutputStream();
        bagAttr3.putOID(TrustedKeyUsage_OID);
        DerOutputStream bagAttrContent3 = new DerOutputStream();
        DerOutputStream bagAttrValue3 = new DerOutputStream();
        for (ObjectIdentifier usage : trustedUsage) {
            bagAttrContent3.putOID(usage);
        }
        bagAttr3.write(DerValue.tag_Set, bagAttrContent3);
        bagAttrValue3.write(DerValue.tag_Sequence, bagAttr3);
        trustedKeyUsage = bagAttrValue3.toByteArray();
    }

    DerOutputStream attrs = new DerOutputStream();
    if (friendlyName != null) {
        attrs.write(friendlyName);
    }
    if (localKeyID != null) {
        attrs.write(localKeyID);
    }
    if (trustedKeyUsage != null) {
        attrs.write(trustedKeyUsage);
    }

    if (attributes != null) {
        for (KeyStore.Entry.Attribute attribute : attributes) {
            String attributeName = attribute.getName();
            // skip friendlyName, localKeyId and trustedKeyUsage
            if (CORE_ATTRIBUTES[0].equals(attributeName) ||
                CORE_ATTRIBUTES[1].equals(attributeName) ||
                CORE_ATTRIBUTES[2].equals(attributeName)) {
                continue;
            }
            attrs.write(((PKCS12Attribute) attribute).getEncoded());
        }
    }

    bagAttrs.write(DerValue.tag_Set, attrs);
    return bagAttrs.toByteArray();
}