Search in sources :

Example 76 with DEROctetString

use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.

the class SubjectPermissions method getInstance.

public static SubjectPermissions getInstance(Object src) {
    if (src instanceof SubjectPermissions) {
        return (SubjectPermissions) src;
    }
    ASN1TaggedObject taggedObject = ASN1TaggedObject.getInstance(src);
    int item = taggedObject.getTagNo();
    switch(item) {
        case explicit:
            return new SubjectPermissions(explicit, SequenceOfPsidSspRange.getInstance(taggedObject.getObject()));
        case all:
            return new SubjectPermissions(all, DERNull.INSTANCE);
        case extension:
            try {
                return new SubjectPermissions(extension, new DEROctetString(taggedObject.getObject().getEncoded()));
            } catch (IOException ioException) {
                throw new RuntimeException(ioException.getMessage(), ioException);
            }
    }
    return null;
}
Also used : ASN1TaggedObject(com.github.zhenwei.core.asn1.ASN1TaggedObject) IOException(java.io.IOException) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString)

Example 77 with DEROctetString

use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.

the class CertPathValidatorUtilities method findIssuerCerts.

/**
 * Find the issuer certificates of a given certificate.
 *
 * @param cert The certificate for which an issuer should be found.
 * @return A <code>Collection</code> object containing the issuer
 * <code>X509Certificate</code>s. Never <code>null</code>.
 * @throws AnnotatedException if an error occurs.
 */
static Collection findIssuerCerts(X509Certificate cert, List<CertStore> certStores, List<PKIXCertStore> pkixCertStores) throws AnnotatedException {
    X509CertSelector selector = new X509CertSelector();
    try {
        selector.setSubject(PrincipalUtils.getIssuerPrincipal(cert).getEncoded());
    } catch (Exception e) {
        throw new AnnotatedException("Subject criteria for certificate selector to find issuer certificate could not be set.", e);
    }
    try {
        byte[] akiExtensionValue = cert.getExtensionValue(AUTHORITY_KEY_IDENTIFIER);
        if (akiExtensionValue != null) {
            ASN1OctetString aki = ASN1OctetString.getInstance(akiExtensionValue);
            byte[] authorityKeyIdentifier = AuthorityKeyIdentifier.getInstance(aki.getOctets()).getKeyIdentifier();
            if (authorityKeyIdentifier != null) {
                selector.setSubjectKeyIdentifier(new DEROctetString(authorityKeyIdentifier).getEncoded());
            }
        }
    } catch (Exception e) {
    // authority key identifier could not be retrieved from target cert, just search without it
    }
    PKIXCertStoreSelector certSelect = new PKIXCertStoreSelector.Builder(selector).build();
    LinkedHashSet certs = new LinkedHashSet();
    try {
        CertPathValidatorUtilities.findCertificates(certs, certSelect, certStores);
        CertPathValidatorUtilities.findCertificates(certs, certSelect, pkixCertStores);
    } catch (AnnotatedException e) {
        throw new AnnotatedException("Issuer certificate cannot be searched.", e);
    }
    return certs;
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) LinkedHashSet(java.util.LinkedHashSet) PKIXCertStoreSelector(com.github.zhenwei.provider.jcajce.PKIXCertStoreSelector) X509CertSelector(java.security.cert.X509CertSelector) GeneralSecurityException(java.security.GeneralSecurityException) CertPathValidatorException(java.security.cert.CertPathValidatorException) ParseException(java.text.ParseException) CertStoreException(java.security.cert.CertStoreException) ExtCertPathValidatorException(com.github.zhenwei.provider.jce.exception.ExtCertPathValidatorException) CRLException(java.security.cert.CRLException) StoreException(com.github.zhenwei.core.util.StoreException) CertificateParsingException(java.security.cert.CertificateParsingException) CertPathBuilderException(java.security.cert.CertPathBuilderException) IOException(java.io.IOException) ExtCertPathBuilderException(com.github.zhenwei.provider.jce.exception.ExtCertPathBuilderException) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString)

Example 78 with DEROctetString

use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.

the class PKCS12Util method convertToDefiniteLength.

/**
 * Re-encode the PKCS#12 structure to definite length encoding at the inner layer as well,
 * recomputing the MAC accordingly.
 *
 * @param berPKCS12File - original PKCS12 file.
 * @param provider      - provider to use for MAC calculation.
 * @return a byte array representing the DER encoding of the PFX structure.
 * @throws IOException on parsing, encoding errors.
 */
public static byte[] convertToDefiniteLength(byte[] berPKCS12File, char[] passwd, String provider) throws IOException {
    Pfx pfx = Pfx.getInstance(berPKCS12File);
    ContentInfo info = pfx.getAuthSafe();
    ASN1OctetString content = ASN1OctetString.getInstance(info.getContent());
    ASN1Primitive obj = ASN1Primitive.fromByteArray(content.getOctets());
    byte[] derEncoding = obj.getEncoded(ASN1Encoding.DER);
    info = new ContentInfo(info.getContentType(), new DEROctetString(derEncoding));
    MacData mData = pfx.getMacData();
    try {
        int itCount = mData.getIterationCount().intValue();
        byte[] data = ASN1OctetString.getInstance(info.getContent()).getOctets();
        byte[] res = calculatePbeMac(mData.getMac().getAlgorithmId().getAlgorithm(), mData.getSalt(), itCount, passwd, data, provider);
        AlgorithmIdentifier algId = new AlgorithmIdentifier(mData.getMac().getAlgorithmId().getAlgorithm(), DERNull.INSTANCE);
        DigestInfo dInfo = new DigestInfo(algId, res);
        mData = new MacData(dInfo, mData.getSalt(), itCount);
    } catch (Exception e) {
        throw new IOException("error constructing MAC: " + e.toString());
    }
    pfx = new Pfx(info, mData);
    return pfx.getEncoded(ASN1Encoding.DER);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) MacData(com.github.zhenwei.core.asn1.pkcs.MacData) Pfx(com.github.zhenwei.core.asn1.pkcs.Pfx) ContentInfo(com.github.zhenwei.core.asn1.pkcs.ContentInfo) DigestInfo(com.github.zhenwei.core.asn1.x509.DigestInfo) IOException(java.io.IOException) ASN1Primitive(com.github.zhenwei.core.asn1.ASN1Primitive) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) IOException(java.io.IOException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 79 with DEROctetString

use of com.github.zhenwei.core.asn1.DEROctetString in project alpha-wallet-android by AlphaWallet.

the class TestAttestation method makeUnsignedx509Att.

private Attestation makeUnsignedx509Att() {
    Attestation att = new Attestation();
    // =v3 since counting starts from 0
    att.setVersion(2);
    att.setSerialNumber(42);
    att.setSignature(OID_SHA256ECDSA);
    att.setIssuer("CN=ALX");
    Date now = new Date();
    att.setNotValidBefore(now);
    // Valid for an hour
    att.setNotValidAfter(new Date(System.currentTimeMillis() + 3600000));
    att.setSubject("CN=0x2042424242424564648");
    att.setSubjectPublicKeyInfo(OID_SHA256ECDSA, subjectKeys.getPublic().getEncoded());
    ASN1EncodableVector extensions = new ASN1EncodableVector();
    extensions.add(new ASN1ObjectIdentifier(Attestation.OID_OCTETSTRING));
    extensions.add(ASN1Boolean.TRUE);
    extensions.add(new DEROctetString("hello world".getBytes()));
    // Double Sequence is needed to be compatible with X509V3
    att.setExtensions(new DERSequence(new DERSequence(extensions)));
    Assert.assertTrue(att.isValidX509());
    return att;
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) Date(java.util.Date) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 80 with DEROctetString

use of com.github.zhenwei.core.asn1.DEROctetString in project alpha-wallet-android by AlphaWallet.

the class TestAttestation method makeUnsignedAtt.

private Attestation makeUnsignedAtt() {
    Attestation att = new Attestation();
    // Our initial version
    att.setVersion(18);
    att.setSerialNumber(42);
    att.setSignature(OID_SHA256ECDSA);
    att.setSubject("CN=0x2042424242424564648");
    att.setSmartcontracts(Arrays.asList(42L, 1337L));
    ASN1EncodableVector dataObject = new ASN1EncodableVector();
    dataObject.add(new DEROctetString("hello world".getBytes()));
    dataObject.add(new ASN1Integer(42));
    att.setDataObject(new DERSequence(dataObject));
    Assert.assertTrue(att.isValid());
    return att;
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Aggregations

DEROctetString (org.bouncycastle.asn1.DEROctetString)139 IOException (java.io.IOException)104 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)86 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)49 DERSequence (org.bouncycastle.asn1.DERSequence)48 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)46 DERSequence (com.github.zhenwei.core.asn1.DERSequence)44 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)39 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)32 BigInteger (java.math.BigInteger)27 Extension (org.bouncycastle.asn1.x509.Extension)27 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)26 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)26 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)24 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)23 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)21 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)20 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)19 Extensions (org.bouncycastle.asn1.x509.Extensions)19 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)18