Search in sources :

Example 6 with Asn1Exception

use of sun.security.krb5.Asn1Exception in project jdk8u_jdk by JetBrains.

the class PrincipalName method asn1Encode.

/**
     * Encodes a <code>PrincipalName</code> object. Note that only the type and
     * names are encoded. To encode the realm, call getRealm().asn1Encode().
     * @return the byte array of the encoded PrncipalName object.
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
     * @exception IOException if an I/O error occurs while reading encoded data.
     *
     */
public byte[] asn1Encode() throws Asn1Exception, IOException {
    DerOutputStream bytes = new DerOutputStream();
    DerOutputStream temp = new DerOutputStream();
    BigInteger bint = BigInteger.valueOf(this.nameType);
    temp.putInteger(bint);
    bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);
    temp = new DerOutputStream();
    DerValue[] der = new DerValue[nameStrings.length];
    for (int i = 0; i < nameStrings.length; i++) {
        der[i] = new KerberosString(nameStrings[i]).toDerValue();
    }
    temp.putSequence(der);
    bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);
    temp = new DerOutputStream();
    temp.write(DerValue.tag_Sequence, bytes);
    return temp.toByteArray();
}
Also used : BigInteger(java.math.BigInteger) KerberosString(sun.security.krb5.internal.util.KerberosString)

Example 7 with Asn1Exception

use of sun.security.krb5.Asn1Exception in project jdk8u_jdk by JetBrains.

the class KeyImpl method readObject.

private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
    try {
        EncryptionKey encKey = new EncryptionKey(new DerValue((byte[]) ois.readObject()));
        keyType = encKey.getEType();
        keyBytes = encKey.getBytes();
    } catch (Asn1Exception ae) {
        throw new IOException(ae.getMessage());
    }
}
Also used : DerValue(sun.security.util.DerValue) EncryptionKey(sun.security.krb5.EncryptionKey) Asn1Exception(sun.security.krb5.Asn1Exception)

Example 8 with Asn1Exception

use of sun.security.krb5.Asn1Exception in project core by jcryptool.

the class ImportExportManager method exportKeyPair.

public void exportKeyPair(IPath path, PrivateKey key, Certificate[] chain, char[] password) {
    PFX pfx;
    X509Certificate[] x509Chain = convert(chain);
    try {
        if (x509Chain.length > 1) {
            X509Certificate[] shortChain = new X509Certificate[x509Chain.length - 1];
            for (int i = 1; i < chain.length; i++) {
                shortChain[i - 1] = x509Chain[i];
            }
            pfx = new PFX(key, x509Chain[0], shortChain, password, null, null);
        } else {
            pfx = new PFX(key, x509Chain[0], null, password, null, null);
        }
        IFileStore fileStore = EFS.getStore(URIUtil.toURI(path));
        OutputStream os = new BufferedOutputStream(fileStore.openOutputStream(EFS.APPEND, null));
        DEREncoder encoder = new DEREncoder(os);
        pfx.encode(encoder);
        encoder.close();
        os.close();
    } catch (CertificateEncodingException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "CertificateEncodingException while creating a PFX", e, true);
    } catch (GeneralSecurityException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "GeneralSecurityException while creating a PFX", e, true);
    } catch (ASN1Exception e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "ASN1Exception while creating a PFX", e, true);
    } catch (IOException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "IOException while creating a PFX", e, true);
    } catch (CoreException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "CoreException while creating a PFX", e, true);
    }
}
Also used : PFX(codec.pkcs12.PFX) ASN1Exception(codec.asn1.ASN1Exception) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) CoreException(org.eclipse.core.runtime.CoreException) DEREncoder(codec.asn1.DEREncoder) IFileStore(org.eclipse.core.filesystem.IFileStore) BufferedOutputStream(java.io.BufferedOutputStream)

Example 9 with Asn1Exception

use of sun.security.krb5.Asn1Exception in project core by jcryptool.

the class AbstractImportKeyStoreEntryHandler method performImportAction.

protected void performImportAction(IImportDescriptor descriptor, Object importedObject) throws IllegalArgumentException {
    if (descriptor.getKeyStoreEntryType().equals(KeyType.SECRETKEY)) {
        if (importedObject instanceof SecretKey) {
            // $NON-NLS-1$
            LogUtil.logInfo("importing secret key");
            addSecretKey(descriptor, (SecretKey) importedObject);
        } else {
            throw new IllegalArgumentException("Parameter is not as expected an instance of SecretKey");
        }
    } else if (descriptor.getKeyStoreEntryType().equals(KeyType.KEYPAIR)) {
        if (importedObject instanceof PFX) {
            // $NON-NLS-1$
            LogUtil.logInfo("importing pfx");
            PFX pfx = (PFX) importedObject;
            try {
                char[] password = promptPassword();
                if (password == null)
                    return;
                SafeBag safeBag = pfx.getAuthSafe().getSafeContents(0).getSafeBag(0);
                PKCS8ShroudedKeyBag kBag = (PKCS8ShroudedKeyBag) safeBag.getBagValue();
                PrivateKey privKey = kBag.getPrivateKey(password);
                SafeBag certBag = pfx.getAuthSafe().getSafeContents(1, password).getSafeBag(0);
                CertBag cBag = (CertBag) certBag.getBagValue();
                PublicKey pubKey = cBag.getCertificate().getPublicKey();
                int keySize = -1;
                if (pubKey instanceof RSAPublicKey)
                    keySize = ((RSAPublicKey) pubKey).getN().bitLength();
                else if (pubKey instanceof DSAPublicKey)
                    keySize = ((DSAPublicKey) pubKey).getParameters().getP().bitLength();
                // TODO: Add keySize calculation for the remaining
                // algorithms.
                ImportDescriptor newDescriptor = new ImportDescriptor(descriptor.getContactName(), privKey.getAlgorithm(), KeyType.KEYPAIR, descriptor.getFileName(), descriptor.getPassword(), descriptor.getProvider(), keySize);
                addKeyPair(newDescriptor, privKey, pubKey);
            } catch (ASN1Exception e) {
                LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "error while importing key pair", e, true);
            } catch (IOException e) {
                LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "error while importing key pair", e, false);
            } catch (GeneralSecurityException e) {
                LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "error while importing key pair", e, true);
            }
        } else {
            throw new IllegalArgumentException("Parameter is not an instance of PFX, as expected");
        }
    } else if (descriptor.getKeyStoreEntryType().equals(KeyType.PUBLICKEY)) {
        if (importedObject instanceof Certificate) {
            // $NON-NLS-1$
            LogUtil.logInfo("importing certificate");
            addCertificate(descriptor, (Certificate) importedObject);
        } else {
            throw new IllegalArgumentException("Parameter is not an instance of Certificate, as expected");
        }
    }
}
Also used : PKCS8ShroudedKeyBag(codec.pkcs12.PKCS8ShroudedKeyBag) PFX(codec.pkcs12.PFX) PrivateKey(java.security.PrivateKey) RSAPublicKey(de.flexiprovider.core.rsa.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(de.flexiprovider.core.dsa.interfaces.DSAPublicKey) ASN1Exception(codec.asn1.ASN1Exception) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) SafeBag(codec.pkcs12.SafeBag) DSAPublicKey(de.flexiprovider.core.dsa.interfaces.DSAPublicKey) SecretKey(javax.crypto.SecretKey) CertBag(codec.pkcs12.CertBag) RSAPublicKey(de.flexiprovider.core.rsa.interfaces.RSAPublicKey) IImportDescriptor(org.jcryptool.crypto.keystore.descriptors.interfaces.IImportDescriptor) ImportDescriptor(org.jcryptool.crypto.keystore.descriptors.ImportDescriptor) Certificate(java.security.cert.Certificate)

Example 10 with Asn1Exception

use of sun.security.krb5.Asn1Exception in project core by jcryptool.

the class ImportManager method importPFX.

public PFX importPFX(IPath path) {
    BufferedInputStream is;
    try {
        IFileStore fileStore = EFS.getStore(URIUtil.toURI(path));
        is = new BufferedInputStream(fileStore.openInputStream(EFS.NONE, null));
        PFX pfx = new PFX();
        DERDecoder decoder = new DERDecoder(is);
        pfx.decode(decoder);
        decoder.close();
        return pfx;
    } catch (CoreException e) {
        LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "CoreException while accessing a file store", e, true);
    } catch (ASN1Exception e) {
        LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "ASN1Exception while decoding a pfx", e, true);
    } catch (IOException e) {
        LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "IOException while decoding a pfx", e, false);
    }
    return null;
}
Also used : PFX(codec.pkcs12.PFX) CoreException(org.eclipse.core.runtime.CoreException) BufferedInputStream(java.io.BufferedInputStream) ASN1Exception(codec.asn1.ASN1Exception) IFileStore(org.eclipse.core.filesystem.IFileStore) IOException(java.io.IOException) DERDecoder(codec.asn1.DERDecoder)

Aggregations

IOException (java.io.IOException)13 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)10 Asn1Exception (sun.security.krb5.Asn1Exception)9 TlvException (es.gob.jmulticard.asn1.TlvException)8 DError (org.kse.gui.error.DError)6 Asn1Exception (org.kse.utilities.asn1.Asn1Exception)6 DecoderObject (es.gob.jmulticard.asn1.DecoderObject)4 Tlv (es.gob.jmulticard.asn1.Tlv)4 X509Certificate (java.security.cert.X509Certificate)4 ASN1Exception (codec.asn1.ASN1Exception)3 PFX (codec.pkcs12.PFX)3 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)3 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)3 FileNotFoundException (es.gob.jmulticard.card.iso7816four.FileNotFoundException)3 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)3 BigInteger (java.math.BigInteger)3 KerberosString (sun.security.krb5.internal.util.KerberosString)3 Cdf (es.gob.jmulticard.asn1.der.pkcs15.Cdf)2 Odf (es.gob.jmulticard.asn1.der.pkcs15.Odf)2 Path (es.gob.jmulticard.asn1.der.pkcs15.Path)2