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

The following examples show how to use sun.security.util.DerOutputStream#putOID() . 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: NamedCurve.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
NamedCurve(String name, String oid, EllipticCurve curve,
        ECPoint g, BigInteger n, int h) {
    super(curve, g, n, h);
    this.name = name;
    this.oid = oid;

    DerOutputStream out = new DerOutputStream();

    try {
        out.putOID(new ObjectIdentifier(oid));
    } catch (IOException e) {
        throw new RuntimeException("Internal error", e);
    }

    encoded = out.toByteArray();
}
 
Example 2
Source File: NamedCurve.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
NamedCurve(String name, String oid, EllipticCurve curve,
        ECPoint g, BigInteger n, int h) {
    super(curve, g, n, h);
    this.name = name;
    this.oid = oid;

    DerOutputStream out = new DerOutputStream();

    try {
        out.putOID(new ObjectIdentifier(oid));
    } catch (IOException e) {
        throw new RuntimeException("Internal error", e);
    }

    encoded = out.toByteArray();
}
 
Example 3
Source File: NamedCurve.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
NamedCurve(String name, String oid, EllipticCurve curve,
        ECPoint g, BigInteger n, int h) {
    super(curve, g, n, h);
    this.name = name;
    this.oid = oid;

    DerOutputStream out = new DerOutputStream();

    try {
        out.putOID(new ObjectIdentifier(oid));
    } catch (IOException e) {
        throw new RuntimeException("Internal error", e);
    }

    encoded = out.toByteArray();
}
 
Example 4
Source File: NamedCurve.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
NamedCurve(String name, String oid, EllipticCurve curve,
        ECPoint g, BigInteger n, int h) {
    super(curve, g, n, h);
    this.name = name;
    this.oid = oid;

    DerOutputStream out = new DerOutputStream();

    try {
        out.putOID(new ObjectIdentifier(oid));
    } catch (IOException e) {
        throw new RuntimeException("Internal error", e);
    }

    encoded = out.toByteArray();
}
 
Example 5
Source File: NamedCurve.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
NamedCurve(String name, String oid, EllipticCurve curve,
        ECPoint g, BigInteger n, int h) {
    super(curve, g, n, h);
    this.name = name;
    this.oid = oid;

    DerOutputStream out = new DerOutputStream();

    try {
        out.putOID(new ObjectIdentifier(oid));
    } catch (IOException e) {
        throw new RuntimeException("Internal error", e);
    }

    encoded = out.toByteArray();
}
 
Example 6
Source File: ExtendedKeyUsageExtension.java    From jdk8u_jdk 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: ExtendedKeyUsageExtension.java    From openjdk-jdk8u 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 8
Source File: SM2NamedCurve.java    From julongchain with Apache License 2.0 5 votes vote down vote up
private SM2NamedCurve(String name, String oid, EllipticCurve ellipticCurve, ECPoint ecPoint, BigInteger n, int h) {
    super(ellipticCurve, ecPoint, n, h);
    this.name = name;
    this.oid = oid;
    DerOutputStream der = new DerOutputStream();

    try {
        der.putOID(new ObjectIdentifier(oid));
    } catch (IOException e) {
        throw new RuntimeException("Internal error", e);
    }

    this.encoded = der.toByteArray();
}
 
Example 9
Source File: Oid.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the full ASN.1 DER encoding for this oid object, which
 * includes the tag and length.
 *
 * @return byte array containing the DER encoding of this oid object.
 * @exception GSSException may be thrown when the oid can't be encoded
 */
public byte[] getDER() throws GSSException {

    if (derEncoding == null) {
        DerOutputStream dout = new DerOutputStream();
        try {
            dout.putOID(oid);
        } catch (IOException e) {
            throw new GSSException(GSSException.FAILURE, e.getMessage());
        }
        derEncoding = dout.toByteArray();
    }

    return derEncoding.clone();
}
 
Example 10
Source File: ExtendedKeyUsageExtension.java    From jdk8u-jdk 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 11
Source File: Oid.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the full ASN.1 DER encoding for this oid object, which
 * includes the tag and length.
 *
 * @return byte array containing the DER encoding of this oid object.
 * @exception GSSException may be thrown when the oid can't be encoded
 */
public byte[] getDER() throws GSSException {

    if (derEncoding == null) {
        DerOutputStream dout = new DerOutputStream();
        try {
            dout.putOID(oid);
        } catch (IOException e) {
            throw new GSSException(GSSException.FAILURE, e.getMessage());
        }
        derEncoding = dout.toByteArray();
    }

    return derEncoding.clone();
}
 
Example 12
Source File: TSRequest.java    From hottub 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 13
Source File: Oid.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the full ASN.1 DER encoding for this oid object, which
 * includes the tag and length.
 *
 * @return byte array containing the DER encoding of this oid object.
 * @exception GSSException may be thrown when the oid can't be encoded
 */
public byte[] getDER() throws GSSException {

    if (derEncoding == null) {
        DerOutputStream dout = new DerOutputStream();
        try {
            dout.putOID(oid);
        } catch (IOException e) {
            throw new GSSException(GSSException.FAILURE, e.getMessage());
        }
        derEncoding = dout.toByteArray();
    }

    return derEncoding.clone();
}
 
Example 14
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 15
Source File: PKCS12KeyStore.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
 
Example 16
Source File: PKCS12KeyStore.java    From jdk8u-jdk with GNU General Public License v2.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();
}
 
Example 17
Source File: PKCS12KeyStore.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
 
Example 18
Source File: GSSNameElement.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
GSSNameElement(byte[] nameBytes, Oid nameType, GSSLibStub stub)
    throws GSSException {
    assert(stub != null);
    if (nameBytes == null) {
        throw new GSSException(GSSException.BAD_NAME);
    }
    cStub = stub;
    byte[] name = nameBytes;

    if (nameType != null) {
        // Special handling the specified name type if
        // necessary
        nameType = getNativeNameType(nameType, stub);

        if (GSSName.NT_EXPORT_NAME.equals(nameType)) {
            // Need to add back the mech Oid portion (stripped
            // off by GSSNameImpl class prior to calling this
            // method) for "NT_EXPORT_NAME"
            byte[] mechBytes = null;
            DerOutputStream dout = new DerOutputStream();
            Oid mech = cStub.getMech();
            try {
                dout.putOID(new ObjectIdentifier(mech.toString()));
            } catch (IOException e) {
                throw new GSSExceptionImpl(GSSException.FAILURE, e);
            }
            mechBytes = dout.toByteArray();
            name = new byte[2 + 2 + mechBytes.length + 4 + nameBytes.length];
            int pos = 0;
            name[pos++] = 0x04;
            name[pos++] = 0x01;
            name[pos++] = (byte) (mechBytes.length>>>8);
            name[pos++] = (byte) mechBytes.length;
            System.arraycopy(mechBytes, 0, name, pos, mechBytes.length);
            pos += mechBytes.length;
            name[pos++] = (byte) (nameBytes.length>>>24);
            name[pos++] = (byte) (nameBytes.length>>>16);
            name[pos++] = (byte) (nameBytes.length>>>8);
            name[pos++] = (byte) nameBytes.length;
            System.arraycopy(nameBytes, 0, name, pos, nameBytes.length);
        }
    }
    pName = cStub.importName(name, nameType);
    setPrintables();

    SecurityManager sm = System.getSecurityManager();
    if (sm != null && !Realm.AUTODEDUCEREALM) {
        String krbName = getKrbName();
        int atPos = krbName.lastIndexOf('@');
        if (atPos != -1) {
            String atRealm = krbName.substring(atPos);
            // getNativeNameType() can modify NT_GSS_KRB5_PRINCIPAL to null
            if ((nameType == null
                        || nameType.equals(GSSUtil.NT_GSS_KRB5_PRINCIPAL))
                    && new String(nameBytes).endsWith(atRealm)) {
                // Created from Kerberos name with realm, no need to check
            } else {
                try {
                    sm.checkPermission(new ServicePermission(atRealm, "-"));
                } catch (SecurityException se) {
                    // Do not chain the actual exception to hide info
                    throw new GSSException(GSSException.FAILURE);
                }
            }
        }
    }

    SunNativeProvider.debug("Imported " + printableName + " w/ type " +
                            printableType);
}
 
Example 19
Source File: GSSNameElement.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
GSSNameElement(byte[] nameBytes, Oid nameType, GSSLibStub stub)
    throws GSSException {
    assert(stub != null);
    if (nameBytes == null) {
        throw new GSSException(GSSException.BAD_NAME);
    }
    cStub = stub;
    byte[] name = nameBytes;

    if (nameType != null) {
        // Special handling the specified name type if
        // necessary
        nameType = getNativeNameType(nameType, stub);

        if (GSSName.NT_EXPORT_NAME.equals(nameType)) {
            // Need to add back the mech Oid portion (stripped
            // off by GSSNameImpl class prior to calling this
            // method) for "NT_EXPORT_NAME"
            byte[] mechBytes = null;
            DerOutputStream dout = new DerOutputStream();
            Oid mech = cStub.getMech();
            try {
                dout.putOID(new ObjectIdentifier(mech.toString()));
            } catch (IOException e) {
                throw new GSSExceptionImpl(GSSException.FAILURE, e);
            }
            mechBytes = dout.toByteArray();
            name = new byte[2 + 2 + mechBytes.length + 4 + nameBytes.length];
            int pos = 0;
            name[pos++] = 0x04;
            name[pos++] = 0x01;
            name[pos++] = (byte) (mechBytes.length>>>8);
            name[pos++] = (byte) mechBytes.length;
            System.arraycopy(mechBytes, 0, name, pos, mechBytes.length);
            pos += mechBytes.length;
            name[pos++] = (byte) (nameBytes.length>>>24);
            name[pos++] = (byte) (nameBytes.length>>>16);
            name[pos++] = (byte) (nameBytes.length>>>8);
            name[pos++] = (byte) nameBytes.length;
            System.arraycopy(nameBytes, 0, name, pos, nameBytes.length);
        }
    }
    pName = cStub.importName(name, nameType);
    setPrintables();

    SecurityManager sm = System.getSecurityManager();
    if (sm != null && !Realm.AUTODEDUCEREALM) {
        String krbName = getKrbName();
        int atPos = krbName.lastIndexOf('@');
        if (atPos != -1) {
            String atRealm = krbName.substring(atPos);
            if (nameType.equals(GSSUtil.NT_GSS_KRB5_PRINCIPAL)
                    && new String(nameBytes).endsWith(atRealm)) {
                // Created from Kerberos name with realm, no need to check
            } else {
                try {
                    sm.checkPermission(new ServicePermission(atRealm, "-"));
                } catch (SecurityException se) {
                    // Do not chain the actual exception to hide info
                    throw new GSSException(GSSException.FAILURE);
                }
            }
        }
    }

    SunNativeProvider.debug("Imported " + printableName + " w/ type " +
                            printableType);
}
 
Example 20
Source File: PKCS12KeyStore.java    From jdk8u60 with GNU General Public License v2.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();
}