Search in sources :

Example 71 with DERSequence

use of org.gudy.bouncycastle.asn1.DERSequence in project keystore-explorer by kaikramer.

the class DPolicyInformationChooser method okPressed.

private void okPressed() {
    ASN1ObjectIdentifier policyIdentifer = joiPolicyIdentifier.getObjectId();
    if (policyIdentifer == null) {
        JOptionPane.showMessageDialog(this, res.getString("DPolicyInformationChooser.PolicyIdentifierValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return;
    }
    List<PolicyQualifierInfo> policyQualifierInfo = jpqPolicyQualifiers.getPolicyQualifierInfo();
    if (policyQualifierInfo.size() > 0) {
        ASN1EncodableVector policyQualifiersVec = new ASN1EncodableVector();
        for (PolicyQualifierInfo policyQualInfo : policyQualifierInfo) {
            try {
                policyQualifiersVec.add(policyQualInfo);
            } catch (Exception ex) {
                DError dError = new DError(this, ex);
                dError.setLocationRelativeTo(this);
                dError.setVisible(true);
                return;
            }
        }
        DERSequence policyQualifiersSeq = new DERSequence(policyQualifiersVec);
        policyInformation = new PolicyInformation(policyIdentifer, policyQualifiersSeq);
    } else {
        policyInformation = new PolicyInformation(policyIdentifer);
    }
    closeDialog();
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) PolicyInformation(org.bouncycastle.asn1.x509.PolicyInformation) PolicyQualifierInfo(org.bouncycastle.asn1.x509.PolicyQualifierInfo) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) IOException(java.io.IOException) DError(org.kse.gui.error.DError)

Example 72 with DERSequence

use of org.gudy.bouncycastle.asn1.DERSequence in project keystore-explorer by kaikramer.

the class DGeneralNameChooser method okPressed.

private void okPressed() {
    try {
        GeneralName newGeneralName = null;
        if (jrbDirectoryName.isSelected()) {
            X500Name directoryName = jdnDirectoryName.getDistinguishedName();
            if (directoryName == null) {
                JOptionPane.showMessageDialog(this, res.getString("DGeneralNameChooser.DirectoryNameValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            newGeneralName = new GeneralName(GeneralName.directoryName, directoryName);
        } else if (jrbDnsName.isSelected()) {
            String dnsName = jtfDnsName.getText().trim();
            if (dnsName.length() == 0) {
                JOptionPane.showMessageDialog(this, res.getString("DGeneralNameChooser.DnsNameValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            newGeneralName = new GeneralName(GeneralName.dNSName, new DERIA5String(dnsName));
        } else if (jrbIpAddress.isSelected()) {
            String ipAddress = jtfIpAddress.getText().trim();
            if (ipAddress.length() == 0) {
                JOptionPane.showMessageDialog(this, res.getString("DGeneralNameChooser.IpAddressValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            if (!IPAddress.isValid(ipAddress)) {
                JOptionPane.showMessageDialog(this, res.getString("DGeneralNameChooser.NotAValidIP.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            newGeneralName = new GeneralName(GeneralName.iPAddress, ipAddress);
        } else if (jrbRegisteredId.isSelected()) {
            ASN1ObjectIdentifier registeredId = joiRegisteredId.getObjectId();
            if (registeredId == null) {
                JOptionPane.showMessageDialog(this, res.getString("DGeneralNameChooser.RegisteredIdValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            newGeneralName = new GeneralName(GeneralName.registeredID, registeredId);
        } else if (jrbRfc822Name.isSelected()) {
            String rfc822Name = jtfRfc822Name.getText().trim();
            if (rfc822Name.length() == 0) {
                JOptionPane.showMessageDialog(this, res.getString("DGeneralNameChooser.Rfc822NameValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            newGeneralName = new GeneralName(GeneralName.rfc822Name, new DERIA5String(rfc822Name));
        } else if (jrbUniformResourceIdentifier.isSelected()) {
            String uniformResourceIdentifier = jtfUniformResourceIdentifier.getText().trim();
            if (uniformResourceIdentifier.length() == 0) {
                JOptionPane.showMessageDialog(this, res.getString("DGeneralNameChooser.UniformResourceIdentifierValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            newGeneralName = new GeneralName(GeneralName.uniformResourceIdentifier, new DERIA5String(uniformResourceIdentifier));
        } else if (jrbPrincipalName.isSelected()) {
            String upnString = jtfPrincipalName.getText().trim();
            if (upnString.length() == 0) {
                JOptionPane.showMessageDialog(this, res.getString("DGeneralNameChooser.PrincipalNameValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            ASN1EncodableVector asn1Vector = new ASN1EncodableVector();
            asn1Vector.add(new ASN1ObjectIdentifier(GeneralNameUtil.UPN_OID));
            asn1Vector.add(new DERTaggedObject(true, 0, new DERUTF8String(upnString)));
            newGeneralName = new GeneralName(GeneralName.otherName, new DERSequence(asn1Vector));
        }
        generalName = newGeneralName;
    } catch (Exception ex) {
        DError dError = new DError(this, ex);
        dError.setLocationRelativeTo(this);
        dError.setVisible(true);
        return;
    }
    closeDialog();
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DERIA5String(org.bouncycastle.asn1.DERIA5String) DERSequence(org.bouncycastle.asn1.DERSequence) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) GeneralName(org.bouncycastle.asn1.x509.GeneralName) X500Name(org.bouncycastle.asn1.x500.X500Name) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERIA5String(org.bouncycastle.asn1.DERIA5String) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) UnknownHostException(java.net.UnknownHostException) DError(org.kse.gui.error.DError)

Example 73 with DERSequence

use of org.gudy.bouncycastle.asn1.DERSequence in project keystore-explorer by kaikramer.

the class OpenSslPvkUtil method get.

/**
 * OpenSSL encode a private key.
 *
 * @return The encoding
 * @param privateKey
 *            The private key
 * @throws CryptoException
 *             Problem encountered while getting the encoded private key
 */
public static byte[] get(PrivateKey privateKey) throws CryptoException {
    // DER encoding for each key type is a sequence
    ASN1EncodableVector vec = new ASN1EncodableVector();
    if (privateKey instanceof ECPrivateKey) {
        try {
            ECPrivateKey ecPrivKey = (ECPrivateKey) privateKey;
            org.bouncycastle.asn1.sec.ECPrivateKey keyStructure = EccUtil.convertToECPrivateKeyStructure(ecPrivKey);
            return keyStructure.toASN1Primitive().getEncoded();
        } catch (IOException e) {
            throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), e);
        }
    } else if (privateKey instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        vec.add(new ASN1Integer(VERSION));
        vec.add(new ASN1Integer(rsaPrivateKey.getModulus()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPublicExponent()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrivateExponent()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrimeP()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrimeQ()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentP()));
        vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentQ()));
        vec.add(new ASN1Integer(rsaPrivateKey.getCrtCoefficient()));
    } else {
        DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
        DSAParams dsaParams = dsaPrivateKey.getParams();
        BigInteger primeModulusP = dsaParams.getP();
        BigInteger primeQ = dsaParams.getQ();
        BigInteger generatorG = dsaParams.getG();
        BigInteger secretExponentX = dsaPrivateKey.getX();
        // Derive public key from private key parts, ie Y = G^X mod P
        BigInteger publicExponentY = generatorG.modPow(secretExponentX, primeModulusP);
        vec.add(new ASN1Integer(VERSION));
        vec.add(new ASN1Integer(primeModulusP));
        vec.add(new ASN1Integer(primeQ));
        vec.add(new ASN1Integer(generatorG));
        vec.add(new ASN1Integer(publicExponentY));
        vec.add(new ASN1Integer(secretExponentX));
    }
    DERSequence derSequence = new DERSequence(vec);
    try {
        return derSequence.getEncoded();
    } catch (IOException ex) {
        throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), ex);
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) IOException(java.io.IOException) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DSAParams(java.security.interfaces.DSAParams) DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) CryptoException(org.kse.crypto.CryptoException)

Example 74 with DERSequence

use of org.gudy.bouncycastle.asn1.DERSequence in project keystore-explorer by kaikramer.

the class Spkac method encodeRsaPublicKeyAsBitString.

private byte[] encodeRsaPublicKeyAsBitString(RSAPublicKey rsaPublicKey) throws SpkacException {
    try {
        ASN1EncodableVector vec = new ASN1EncodableVector();
        vec.add(new ASN1Integer(rsaPublicKey.getModulus()));
        vec.add(new ASN1Integer(rsaPublicKey.getPublicExponent()));
        DERSequence derSequence = new DERSequence(vec);
        return derSequence.getEncoded(ASN1Encoding.DER);
    } catch (Exception ex) {
        throw new SpkacException(res.getString("NoEncodeRsaPublicKey.exception.message"), ex);
    }
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 75 with DERSequence

use of org.gudy.bouncycastle.asn1.DERSequence in project nuls by nuls-io.

the class SM2Utils method encrypt.

public static byte[] encrypt(byte[] publicKey, byte[] data) throws IOException {
    if (publicKey == null || publicKey.length == 0) {
        return null;
    }
    if (data == null || data.length == 0) {
        return null;
    }
    byte[] source = new byte[data.length];
    System.arraycopy(data, 0, source, 0, data.length);
    Cipher cipher = new Cipher();
    SM2 sm2 = SM2.Instance();
    ECPoint userKey = sm2.ecc_curve.decodePoint(publicKey);
    ECPoint c1 = cipher.initEnc(sm2, userKey);
    cipher.encrypt(source);
    byte[] c3 = new byte[32];
    cipher.dofinal(c3);
    DERInteger x = new DERInteger(c1.getX().toBigInteger());
    DERInteger y = new DERInteger(c1.getY().toBigInteger());
    DEROctetString derDig = new DEROctetString(c3);
    DEROctetString derEnc = new DEROctetString(source);
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(x);
    v.add(y);
    v.add(derDig);
    v.add(derEnc);
    DERSequence seq = new DERSequence(v);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DEROutputStream dos = new DEROutputStream(bos);
    dos.writeObject(seq);
    return bos.toByteArray();
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ECPoint(org.bouncycastle.math.ec.ECPoint) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERInteger(org.bouncycastle.asn1.DERInteger) DEROutputStream(org.bouncycastle.asn1.DEROutputStream)

Aggregations

DERSequence (org.bouncycastle.asn1.DERSequence)225 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)196 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)48 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)41 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)41 IOException (java.io.IOException)36 DEROctetString (org.bouncycastle.asn1.DEROctetString)36 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)30 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)27 BigInteger (java.math.BigInteger)25 X509Certificate (java.security.cert.X509Certificate)24 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)22 DERBitString (org.bouncycastle.asn1.DERBitString)19 DERIA5String (org.bouncycastle.asn1.DERIA5String)19 DERSet (org.bouncycastle.asn1.DERSet)19 GeneralName (org.bouncycastle.asn1.x509.GeneralName)17 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)16 X500Name (org.bouncycastle.asn1.x500.X500Name)16 DERInteger (org.bouncycastle.asn1.DERInteger)14 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)14