Search in sources :

Example 41 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project xipki by xipki.

the class Asn1GenDSAKeypairParams method toASN1Primitive.

@Override
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector vector = new ASN1EncodableVector();
    vector.add(new Asn1P11SlotIdentifier(slotId));
    vector.add(new DERUTF8String(label));
    vector.add(new Asn1NewKeyControl(control));
    vector.add(new ASN1Integer(p));
    vector.add(new ASN1Integer(q));
    vector.add(new ASN1Integer(g));
    return new DERSequence(vector);
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Example 42 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project keystore-explorer by kaikramer.

the class Pkcs10Util method generateCsr.

/**
 * Create a PKCS #10 certificate signing request (CSR) using the supplied
 * certificate, private key and signature algorithm.
 *
 * @param subjectDN        Distinguished name for CSR
 * @param publicKey        Public key for CSR
 * @param privateKey       Private key for CSR
 * @param signatureType    Signature algorithm
 * @param challenge        Challenge, optional, pass null if not required
 * @param unstructuredName An optional company name, pass null if not required
 * @param extensions       Optional extensions from cert for extensionRequest attribute, pass null if not required
 * @param provider         Optional provider (for example for PKCS11)
 * @return The CSR
 * @throws CryptoException If there was a problem generating the CSR
 */
public static PKCS10CertificationRequest generateCsr(X500Principal subjectDN, PublicKey publicKey, PrivateKey privateKey, SignatureType signatureType, String challenge, String unstructuredName, Extensions extensions, Provider provider) throws CryptoException {
    try {
        JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(subjectDN, publicKey);
        // add challenge attribute
        if (challenge != null) {
            // PKCS#9 2.0: SHOULD use UTF8String encoding
            csrBuilder.addAttribute(pkcs_9_at_challengePassword, new DERUTF8String(challenge));
        }
        if (unstructuredName != null) {
            csrBuilder.addAttribute(pkcs_9_at_unstructuredName, new DERUTF8String(unstructuredName));
        }
        if (extensions != null) {
            csrBuilder.addAttribute(pkcs_9_at_extensionRequest, extensions.toASN1Primitive());
        }
        // fall back to bouncy castle provider if given provider does not support the requested algorithm
        if (provider != null && provider.getService("Signature", signatureType.jce()) == null) {
            provider = new BouncyCastleProvider();
        }
        ContentSigner contentSigner = null;
        if (provider == null) {
            contentSigner = new JcaContentSignerBuilder(signatureType.jce()).build(privateKey);
        } else {
            contentSigner = new JcaContentSignerBuilder(signatureType.jce()).setProvider(provider).build(privateKey);
        }
        PKCS10CertificationRequest csr = csrBuilder.build(contentSigner);
        if (!verifyCsr(csr)) {
            throw new CryptoException(res.getString("NoVerifyGenPkcs10Csr.exception.message"));
        }
        return csr;
    } catch (OperatorCreationException e) {
        throw new CryptoException(res.getString("NoGeneratePkcs10Csr.exception.message"), e);
    }
}
Also used : PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) JcaPKCS10CertificationRequest(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) JcaPKCS10CertificationRequestBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) ContentSigner(org.bouncycastle.operator.ContentSigner) CryptoException(org.kse.crypto.CryptoException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 43 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project keystore-explorer by kaikramer.

the class DialogHelper method populateTextField.

private static void populateTextField(Attribute[] attrs, JTextField textField, ASN1ObjectIdentifier pkcs9Attr) {
    if (attrs != null) {
        for (Attribute attribute : attrs) {
            ASN1ObjectIdentifier attributeOid = attribute.getAttrType();
            if (attributeOid.equals(pkcs9Attr)) {
                ASN1Encodable challenge = attribute.getAttributeValues()[0];
                // data type can be one of IA5String or UTF8String
                if (challenge instanceof DERPrintableString) {
                    textField.setText(((DERPrintableString) challenge).getString());
                } else if (challenge instanceof DERUTF8String) {
                    textField.setText(((DERUTF8String) challenge).getString());
                }
                textField.setCaretPosition(0);
            }
        }
    }
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String) Attribute(org.bouncycastle.asn1.pkcs.Attribute) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 44 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project xipki by xipki.

the class DemoCertprofile method getExtraExtensions.

// method initExtraExtension
@Override
public ExtensionValues getExtraExtensions(Map<ASN1ObjectIdentifier, ExtensionControl> extensionOccurences, X500Name requestedSubject, X500Name grantedSubject, Map<ASN1ObjectIdentifier, Extension> requestedExtensions, Date notBefore, Date notAfter, PublicCaInfo caInfo) throws CertprofileException, BadCertTemplateException {
    ExtensionValues extnValues = new ExtensionValues();
    if (addExtraWithoutConf) {
        ASN1ObjectIdentifier type = id_demo_without_conf;
        ExtensionControl extnControl = extensionOccurences.get(type);
        if (extnControl != null) {
            ConfPairs caExtraControl = caInfo.getExtraControl();
            String name = "name-a";
            String value = null;
            if (caExtraControl != null) {
                value = caExtraControl.value(name);
            }
            if (value == null) {
                value = "UNDEF";
            }
            ExtensionValue extnValue = new ExtensionValue(extnControl.isCritical(), new DERUTF8String(name + ": " + value));
            extnValues.addExtension(type, extnValue);
        }
    }
    if (addExtraWithConf) {
        ASN1ObjectIdentifier type = id_demo_with_conf;
        ExtensionControl extnControl = extensionOccurences.get(type);
        if (extnControl != null) {
            if (sequence == null) {
                throw new IllegalStateException("Certprofile is not initialized");
            }
            ExtensionValue extnValue = new ExtensionValue(extnControl.isCritical(), sequence);
            extnValues.addExtension(type, extnValue);
        }
    }
    return extnValues.size() == 0 ? null : extnValues;
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String) ExtensionValue(org.xipki.ca.api.profile.ExtensionValue) ConfPairs(org.xipki.util.ConfPairs) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) ExtensionValues(org.xipki.ca.api.profile.ExtensionValues) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 45 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project signer by demoiselle.

the class PolicyIssuerName method parse.

@Override
public void parse(ASN1Primitive primitive) {
    if (primitive instanceof DLSequence) {
        DLSequence sequence = (DLSequence) primitive;
        ASN1Encodable asn1Encodable = sequence.getObjectAt(0);
        if (asn1Encodable instanceof DERTaggedObject) {
            DERTaggedObject derTaggedObject = (DERTaggedObject) asn1Encodable;
            ASN1Primitive object = derTaggedObject.getObject();
            if (object instanceof DEROctetString) {
                OctetString octetString = new OctetString();
                octetString.parse(object);
                this.issuerName = octetString.getValueUTF8();
            } else if (object instanceof DERSequence) {
                DERSequence sequence2 = (DERSequence) object;
                for (int i = 0; i < sequence2.size(); i++) {
                    ASN1Encodable obj = sequence2.getObjectAt(i);
                    if (obj instanceof DERSet) {
                        DERSet set = (DERSet) obj;
                        ASN1Encodable object2 = set.getObjectAt(0);
                        if (object2 instanceof DERSequence) {
                            DERSequence sequence3 = (DERSequence) object2;
                            ObjectIdentifier objectIdendifier = new ObjectIdentifier();
                            objectIdendifier.parse(sequence3.getObjectAt(0).toASN1Primitive());
                            String name = null;
                            ASN1Encodable object3 = sequence3.getObjectAt(1);
                            if (object3 instanceof DERPrintableString) {
                                name = ((DERPrintableString) object3).getString();
                            } else if (object3 instanceof DERUTF8String) {
                                name = ((DERUTF8String) object3).getString();
                            } else {
                                System.out.println(policyMessagesBundle.getString("error.not.recognized.object", object3.getClass(), object3.toString()));
                            }
                            if (this.issuerNames == null) {
                                this.issuerNames = new HashMap<ObjectIdentifier, String>();
                            }
                            this.issuerNames.put(objectIdendifier, name);
                        }
                    }
                }
            }
        }
    }
}
Also used : DEROctetString(org.bouncycastle.asn1.DEROctetString) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) HashMap(java.util.HashMap) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) DERSet(org.bouncycastle.asn1.DERSet) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERSequence(org.bouncycastle.asn1.DERSequence) DLSequence(org.bouncycastle.asn1.DLSequence) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Aggregations

DERUTF8String (org.bouncycastle.asn1.DERUTF8String)52 DERSequence (org.bouncycastle.asn1.DERSequence)28 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)20 DEROctetString (org.bouncycastle.asn1.DEROctetString)19 IOException (java.io.IOException)17 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)17 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)17 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)15 DERIA5String (org.bouncycastle.asn1.DERIA5String)15 DERPrintableString (org.bouncycastle.asn1.DERPrintableString)12 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)11 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)11 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)10 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)10 DLSequence (org.bouncycastle.asn1.DLSequence)9 X500Name (org.bouncycastle.asn1.x500.X500Name)8 X509Certificate (java.security.cert.X509Certificate)7 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)7 Pair (android.util.Pair)5 Date (java.util.Date)5