Search in sources :

Example 86 with DerOutputStream

use of org.mozilla.jss.netscape.security.util.DerOutputStream in project jss by dogtagpki.

the class CertificateSubjectName method encode.

/**
 * Encode the name in DER form to the stream.
 *
 * @param out the DerOutputStream to marshal the contents to.
 * @exception IOException on errors.
 */
@Override
public void encode(OutputStream out) throws IOException {
    DerOutputStream tmp = new DerOutputStream();
    dnName.encode(tmp);
    out.write(tmp.toByteArray());
}
Also used : DerOutputStream(org.mozilla.jss.netscape.security.util.DerOutputStream)

Example 87 with DerOutputStream

use of org.mozilla.jss.netscape.security.util.DerOutputStream in project jss by dogtagpki.

the class CertificateSubjectUniqueIdentity method encode.

/**
 * Encode the identity in DER form to the stream.
 *
 * @param out the DerOutputStream to marshal the contents to.
 * @exception IOException on errors.
 */
@Override
public void encode(OutputStream out) throws IOException {
    DerOutputStream tmp = new DerOutputStream();
    id.encode(tmp, DerValue.createTag(DerValue.TAG_CONTEXT, false, (byte) 2));
    out.write(tmp.toByteArray());
}
Also used : DerOutputStream(org.mozilla.jss.netscape.security.util.DerOutputStream)

Example 88 with DerOutputStream

use of org.mozilla.jss.netscape.security.util.DerOutputStream in project jss by dogtagpki.

the class DeltaCRLIndicatorExtension method encode.

/**
 * Write the extension to the DerOutputStream.
 *
 * @param out the DerOutputStream to write the extension to.
 * @exception IOException on encoding errors.
 */
@Override
public void encode(OutputStream out) throws IOException {
    DerOutputStream tmp = new DerOutputStream();
    if (this.extensionValue == null) {
        this.extensionId = PKIXExtensions.DeltaCRLIndicator_Id;
        this.critical = true;
        encodeThis();
    }
    super.encode(tmp);
    out.write(tmp.toByteArray());
}
Also used : DerOutputStream(org.mozilla.jss.netscape.security.util.DerOutputStream)

Example 89 with DerOutputStream

use of org.mozilla.jss.netscape.security.util.DerOutputStream in project jss by dogtagpki.

the class DeltaCRLIndicatorExtension method encodeThis.

// Encode this extension value
private void encodeThis() throws IOException {
    if (baseCRLNumber == null)
        throw new IOException("Unintialized delta CRL indicator extension");
    try (DerOutputStream os = new DerOutputStream()) {
        os.putInteger(this.baseCRLNumber);
        this.extensionValue = os.toByteArray();
    }
}
Also used : DerOutputStream(org.mozilla.jss.netscape.security.util.DerOutputStream) IOException(java.io.IOException)

Example 90 with DerOutputStream

use of org.mozilla.jss.netscape.security.util.DerOutputStream in project jss by dogtagpki.

the class PKCS8Key method buildPKCS8Key.

/*
     * Factory interface, building the kind of key associated with this
     * specific algorithm ID or else returning this generic base class.
     * See the description above.
     */
public static PKCS8Key buildPKCS8Key(AlgorithmId algid, byte[] key) throws IOException, InvalidKeyException {
    /*
         * Use the algid and key parameters to produce the ASN.1 encoding
         * of the key, which will then be used as the input to the
         * key factory.
         */
    DerOutputStream pkcs8EncodedKeyStream = new DerOutputStream();
    encode(pkcs8EncodedKeyStream, algid, key);
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(pkcs8EncodedKeyStream.toByteArray());
    try {
        // Instantiate the key factory of the appropriate algorithm
        KeyFactory keyFac = KeyFactory.getInstance(algid.getName());
        // Generate the private key
        PrivateKey privKey = keyFac.generatePrivate(pkcs8KeySpec);
        if (privKey instanceof PKCS8Key) {
            /*
                 * Return specialized PKCS8Key, where the structure within the
                 * key has been parsed
                 */
            return (PKCS8Key) privKey;
        }
    } catch (NoSuchAlgorithmException e) {
    // Return generic PKCS8Key with opaque key data (see below)
    } catch (InvalidKeySpecException e) {
    // Return generic PKCS8Key with opaque key data (see below)
    }
    /*
         * Try again using JDK1.1-style for backwards compatibility.
         */
    String classname = "";
    try {
        Provider sunProvider;
        sunProvider = Security.getProvider("SUN");
        if (sunProvider == null)
            throw new InstantiationException();
        classname = sunProvider.getProperty("PrivateKey.PKCS#8." + algid.getName());
        if (classname == null) {
            throw new InstantiationException();
        }
        Class<?> keyClass = Class.forName(classname);
        Object inst;
        PKCS8Key result;
        inst = keyClass.getConstructor().newInstance();
        if (inst instanceof PKCS8Key) {
            result = (PKCS8Key) inst;
            result.algid = algid;
            result.key = key;
            result.parseKeyBits();
            return result;
        }
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
        throw new IOException("IllegalAccessException : " + e.getMessage(), e);
    } catch (NoSuchMethodException e) {
    } catch (InvocationTargetException e) {
        throw new IOException("InvocationTargetException : " + e.getMessage(), e);
    }
    PKCS8Key result = new PKCS8Key();
    result.algid = algid;
    result.key = key;
    return result;
}
Also used : PrivateKey(java.security.PrivateKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Provider(java.security.Provider) DerOutputStream(org.mozilla.jss.netscape.security.util.DerOutputStream) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Aggregations

DerOutputStream (org.mozilla.jss.netscape.security.util.DerOutputStream)141 IOException (java.io.IOException)37 BigInt (org.mozilla.jss.netscape.security.util.BigInt)13 DerValue (org.mozilla.jss.netscape.security.util.DerValue)8 ObjectIdentifier (org.mozilla.jss.netscape.security.util.ObjectIdentifier)8 CRLException (java.security.cert.CRLException)7 CertificateException (java.security.cert.CertificateException)7 InvalidKeyException (java.security.InvalidKeyException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 ANY (org.mozilla.jss.asn1.ANY)5 InvalidBERException (org.mozilla.jss.asn1.InvalidBERException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 KeyFactory (java.security.KeyFactory)3 SignatureException (java.security.SignatureException)3 CertificateEncodingException (java.security.cert.CertificateEncodingException)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3 NoSuchProviderException (java.security.NoSuchProviderException)2 Provider (java.security.Provider)2 PublicKey (java.security.PublicKey)2