Search in sources :

Example 6 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project gdmatrix by gdmatrix.

the class CMSUtils method recoverTSTInfo.

public static TSTInfo recoverTSTInfo(ContentInfo contentInfo) throws IOException {
    SignedData sd = SignedData.getInstance(contentInfo.getContent());
    ASN1Encodable content = sd.getEncapContentInfo().getContent();
    // TSTInfo tstInfo = new TSTInfo((ASN1Sequence)
    // new ASN1InputStream(((DEROctetString)content).getOctets()).readObject());
    TSTInfo tstInfo = TSTInfo.getInstance(((ASN1OctetString) content).getOctets());
    return tstInfo;
}
Also used : TSTInfo(org.bouncycastle.asn1.tsp.TSTInfo) SignedData(org.bouncycastle.asn1.cms.SignedData) CMSSignedData(org.bouncycastle.cms.CMSSignedData) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable)

Example 7 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project jiguang-java-client-common by jpush.

the class BCECUtil method convertECPrivateKeyToSEC1.

/**
 * 将ECC私钥转换为SEC1标准的字节流
 * openssl d2i_ECPrivateKey函数要求的DER编码的私钥也是SEC1标准的,
 * 这个工具函数的主要目的就是为了能生成一个openssl可以直接“识别”的ECC私钥.
 * 相对RSA私钥的PKCS1标准,ECC私钥的标准为SEC1
 *
 * @param priKey
 * @param pubKey
 * @return
 * @throws IOException
 */
public static byte[] convertECPrivateKeyToSEC1(ECPrivateKeyParameters priKey, ECPublicKeyParameters pubKey) throws IOException {
    byte[] pkcs8Bytes = convertECPrivateKeyToPKCS8(priKey, pubKey);
    PrivateKeyInfo pki = PrivateKeyInfo.getInstance(pkcs8Bytes);
    ASN1Encodable encodable = pki.parsePrivateKey();
    ASN1Primitive primitive = encodable.toASN1Primitive();
    byte[] sec1Bytes = primitive.getEncoded();
    return sec1Bytes;
}
Also used : ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 8 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project hotmoka by Hotmoka.

the class ED25519 method encodingOf.

@Override
public byte[] encodingOf(PrivateKey privateKey) {
    try {
        PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(new PKCS8EncodedKeySpec(privateKey.getEncoded()).getEncoded()));
        ASN1Encodable privateKey2 = privateKeyInfo.parsePrivateKey();
        Ed25519PrivateKeyParameters privateKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey2).getOctets(), 0);
        return privateKeyParams.getEncoded();
    } catch (IOException e) {
        throw InternalFailureException.of("cannot encode the private key", e);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) IOException(java.io.IOException) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 9 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project hotmoka by Hotmoka.

the class ED25519DET method encodingOf.

@Override
public byte[] encodingOf(PrivateKey privateKey) {
    try {
        PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(new PKCS8EncodedKeySpec(privateKey.getEncoded()).getEncoded()));
        ASN1Encodable privateKey2 = privateKeyInfo.parsePrivateKey();
        Ed25519PrivateKeyParameters privateKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey2).getOctets(), 0);
        return privateKeyParams.getEncoded();
    } catch (IOException e) {
        throw InternalFailureException.of("cannot encode the private key", e);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) IOException(java.io.IOException) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 10 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project ca3sCore by kuehne-trustable-de.

the class CaCmpConnector method buildCertRequest.

/**
 * @param certReqId
 * @param csr
 * @param hmacSecret
 * @return PKIMessage
 * @throws GeneralSecurityException
 */
public PKIMessage buildCertRequest(long certReqId, final CSR csr, final String hmacSecret) throws GeneralSecurityException {
    // read the pem csr and verify the signature
    PKCS10CertificationRequest p10Req;
    try {
        p10Req = cryptoUtil.parseCertificateRequest(csr.getCsrBase64()).getP10Req();
    } catch (IOException e) {
        LOGGER.error("parsing csr", e);
        throw new GeneralSecurityException(e.getMessage());
    }
    List<RDN> rdnList = new ArrayList<>();
    for (de.trustable.ca3s.core.domain.RDN rdnDao : csr.getRdns()) {
        LOGGER.debug("rdnDao : " + rdnDao.getRdnAttributes());
        List<AttributeTypeAndValue> attrTVList = new ArrayList<AttributeTypeAndValue>();
        if (rdnDao != null && rdnDao.getRdnAttributes() != null) {
            for (RDNAttribute rdnAttr : rdnDao.getRdnAttributes()) {
                ASN1ObjectIdentifier aoi = new ASN1ObjectIdentifier(rdnAttr.getAttributeType());
                ASN1Encodable ae = new DERUTF8String(rdnAttr.getAttributeValue());
                AttributeTypeAndValue attrTV = new AttributeTypeAndValue(aoi, ae);
                attrTVList.add(attrTV);
            }
        }
        RDN rdn = new RDN(attrTVList.toArray(new AttributeTypeAndValue[attrTVList.size()]));
        LOGGER.debug("rdn : " + rdn.size() + " elements");
        rdnList.add(rdn);
    }
    X500Name subjectDN = new X500Name(rdnList.toArray(new RDN[rdnList.size()]));
    LOGGER.debug("subjectDN : " + subjectDN);
    Collection<Extension> certExtList = new ArrayList<>();
    // copy CSR attributes to Extension list
    for (Attribute attribute : p10Req.getAttributes()) {
        for (ASN1Encodable asn1Encodable : attribute.getAttributeValues()) {
            if (asn1Encodable != null) {
                try {
                    Extensions extensions = Extensions.getInstance(asn1Encodable);
                    for (ASN1ObjectIdentifier oid : extensions.getExtensionOIDs()) {
                        LOGGER.debug("copying oid '" + oid.toString() + "' from csr to PKIMessage");
                        certExtList.add(extensions.getExtension(oid));
                    }
                } catch (IllegalArgumentException iae) {
                    LOGGER.debug("processing asn1 value  '" + asn1Encodable + "' caused exception", iae);
                }
            }
        }
    }
    final SubjectPublicKeyInfo keyInfo = p10Req.getSubjectPublicKeyInfo();
    return cryptoUtil.buildCertRequest(certReqId, subjectDN, certExtList, keyInfo, hmacSecret);
}
Also used : PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) RDNAttribute(de.trustable.ca3s.core.domain.RDNAttribute) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) RDNAttribute(de.trustable.ca3s.core.domain.RDNAttribute) CsrAttribute(de.trustable.ca3s.core.domain.CsrAttribute) Attribute(org.bouncycastle.asn1.pkcs.Attribute) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) X500Name(org.bouncycastle.asn1.x500.X500Name) Extensions(org.bouncycastle.asn1.x509.Extensions) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) AttributeTypeAndValue(org.bouncycastle.asn1.x500.AttributeTypeAndValue) Extension(org.bouncycastle.asn1.x509.Extension) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) RDN(org.bouncycastle.asn1.x500.RDN) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Aggregations

ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)209 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)89 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)76 IOException (java.io.IOException)72 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)58 ArrayList (java.util.ArrayList)45 DEROctetString (org.bouncycastle.asn1.DEROctetString)43 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)42 DERSequence (org.bouncycastle.asn1.DERSequence)35 BigInteger (java.math.BigInteger)31 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)30 DERIA5String (org.bouncycastle.asn1.DERIA5String)30 X509Certificate (java.security.cert.X509Certificate)29 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)29 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)29 GeneralName (org.bouncycastle.asn1.x509.GeneralName)26 List (java.util.List)25 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)24 HashSet (java.util.HashSet)24 ASN1TaggedObject (org.bouncycastle.asn1.ASN1TaggedObject)23