Search in sources :

Example 46 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object in project candlepin by candlepin.

the class X509CRLStreamWriter method readAndReplaceTime.

/**
 * Replace a time in the ASN1 with the current time.
 *
 * @param out
 * @param tagNo
 * @return the time that was replaced
 * @throws IOException
 */
protected Date readAndReplaceTime(OutputStream out, int tagNo) throws IOException {
    int originalLength = readLength(crlIn, null);
    byte[] oldBytes = new byte[originalLength];
    readFullyAndTrack(crlIn, oldBytes, null);
    ASN1Object oldTime;
    ASN1Object newTime;
    if (tagNo == UTC_TIME) {
        ASN1TaggedObject t = new DERTaggedObject(UTC_TIME, new DEROctetString(oldBytes));
        oldTime = ASN1UTCTime.getInstance(t, false);
        newTime = new DERUTCTime(new Date());
    } else {
        ASN1TaggedObject t = new DERTaggedObject(GENERALIZED_TIME, new DEROctetString(oldBytes));
        oldTime = ASN1GeneralizedTime.getInstance(t, false);
        newTime = new DERGeneralizedTime(new Date());
    }
    writeNewTime(out, newTime, originalLength);
    return Time.getInstance(oldTime).getDate();
}
Also used : DERGeneralizedTime(org.bouncycastle.asn1.DERGeneralizedTime) DERUTCTime(org.bouncycastle.asn1.DERUTCTime) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) DEROctetString(org.bouncycastle.asn1.DEROctetString) Date(java.util.Date)

Example 47 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object in project xades4j by luisgoncalves.

the class CrlExtensionsUtils method getCrlNumber.

public static BigInteger getCrlNumber(X509CRL crl) throws IOException {
    byte[] crlNumEnc = crl.getExtensionValue(X509Extension.cRLNumber.getId());
    BigInteger crlNum = null;
    // XAdES 7.4.2: "The 'number' element is an optional hint ..."
    if (crlNumEnc != null) {
        ASN1Object derCrlNum = X509ExtensionUtil.fromExtensionValue(crlNumEnc);
        crlNum = CRLNumber.getInstance(derCrlNum).getCRLNumber();
    }
    return crlNum;
}
Also used : BigInteger(java.math.BigInteger) ASN1Object(org.bouncycastle.asn1.ASN1Object)

Example 48 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object in project pdfbox by apache.

the class CertInformationCollector method addTimestampCerts.

/**
 * Processes an embedded signed timestamp, that has been placed into a signature. The
 * certificates and its chain(s) will be processed the same way as the signature itself.
 *
 * @param signerInformation of the signature, to get unsigned attributes from it.
 * @throws IOException
 * @throws CertificateProccessingException
 */
private void addTimestampCerts(SignerInformation signerInformation) throws IOException, CertificateProccessingException {
    AttributeTable unsignedAttributes = signerInformation.getUnsignedAttributes();
    if (unsignedAttributes == null) {
        return;
    }
    Attribute tsAttribute = unsignedAttributes.get(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken);
    if (tsAttribute == null) {
        return;
    }
    ASN1Encodable obj0 = tsAttribute.getAttrValues().getObjectAt(0);
    if (!(obj0 instanceof ASN1Object)) {
        return;
    }
    ASN1Object tsSeq = (ASN1Object) obj0;
    try {
        CMSSignedData signedData = new CMSSignedData(tsSeq.getEncoded("DER"));
        rootCertInfo.tsaCerts = new CertSignatureInformation();
        processSignerStore(signedData, rootCertInfo.tsaCerts);
    } catch (CMSException e) {
        throw new IOException("Error parsing timestamp token", e);
    }
}
Also used : Attribute(org.bouncycastle.asn1.cms.Attribute) AttributeTable(org.bouncycastle.asn1.cms.AttributeTable) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) IOException(java.io.IOException) ASN1Object(org.bouncycastle.asn1.ASN1Object) CMSSignedData(org.bouncycastle.cms.CMSSignedData) CMSException(org.bouncycastle.cms.CMSException)

Example 49 with ASN1Object

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

the class ProxyP11Slot method getPublicKey.

private PublicKey getPublicKey(P11ObjectIdentifier objectId) throws P11TokenException {
    ASN1Object req = new SlotIdAndObjectId(asn1SlotId, new ObjectIdentifier(objectId));
    byte[] resp = module.send(P11ProxyConstants.ACTION_GET_PUBLICKEY, req);
    if (resp == null) {
        return null;
    }
    SubjectPublicKeyInfo pkInfo = SubjectPublicKeyInfo.getInstance(resp);
    try {
        return KeyUtil.generatePublicKey(pkInfo);
    } catch (InvalidKeySpecException ex) {
        throw new P11TokenException("could not generate Public Key from SubjectPublicKeyInfo:" + ex.getMessage(), ex);
    }
}
Also used : InvalidKeySpecException(java.security.spec.InvalidKeySpecException) ASN1Object(org.bouncycastle.asn1.ASN1Object) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 50 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object in project churchkey by tomitribe.

the class FooTest method test3.

@Ignore
@Test
public void test3() throws Exception {
    final Resource resource = Resource.resource(BeginPrivateKeyTest.class.getSimpleName());
    final byte[] bytes = resource.bytes("openssl-rsaprivatekey-3072.pem");
    final Pem pem = Pem.parse(bytes);
    {
        final DerParser d1 = new DerParser(pem.getData());
        final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
        {
            final DerParser d2 = new DerParser(d1o1.getValue());
            final Asn1Object d2o1 = d2.readObject().assertType(Asn1Type.INTEGER);
            final Asn1Object d2o2 = d2.readObject().assertType(Asn1Type.SEQUENCE);
            {
                final DerParser d3 = new DerParser(d2o2.getValue());
                final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
                final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.NULL);
            }
            final Asn1Object d2o3 = d2.readObject().assertType(OCTET_STRING);
            {
                final DerParser d3 = new DerParser(d2o3.getValue());
                final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.SEQUENCE);
                {
                    final DerParser d4 = new DerParser(d3o1.getValue());
                    final BigInteger version = d4.readBigInteger();
                    final RSAPrivateCrtKey privateKey = Rsa.Private.builder().modulus(d4.readBigInteger()).publicExponent(d4.readBigInteger()).privateExponent(d4.readBigInteger()).primeP(d4.readBigInteger()).primeQ(d4.readBigInteger()).primeExponentP(d4.readBigInteger()).primeExponentQ(d4.readBigInteger()).crtCoefficient(d4.readBigInteger()).build().toKey();
                    final Key key1 = new Key(privateKey, Key.Type.PRIVATE, RSA, Key.Format.PEM);
                    System.out.println(key1);
                }
            }
        }
    }
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Resource(io.churchkey.Resource) Pem(io.churchkey.util.Pem) BigInteger(java.math.BigInteger) DerParser(io.churchkey.asn1.DerParser) Key(io.churchkey.Key) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Asn1Object(io.churchkey.asn1.Asn1Object) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)37 Asn1Object (com.android.hotspot2.asn1.Asn1Object)25 ASN1Object (org.bouncycastle.asn1.ASN1Object)20 ArrayList (java.util.ArrayList)16 Asn1Constructed (com.android.hotspot2.asn1.Asn1Constructed)15 HashMap (java.util.HashMap)15 Asn1Object (io.churchkey.asn1.Asn1Object)13 DerParser (io.churchkey.asn1.DerParser)12 X509Certificate (java.security.cert.X509Certificate)12 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)10 DERBitString (com.android.org.bouncycastle.asn1.DERBitString)10 DERIA5String (com.android.org.bouncycastle.asn1.DERIA5String)10 DERPrintableString (com.android.org.bouncycastle.asn1.DERPrintableString)10 ByteBuffer (java.nio.ByteBuffer)10 Key (io.churchkey.Key)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 I18Name (com.android.anqp.I18Name)5 Asn1Oid (com.android.hotspot2.asn1.Asn1Oid)5 Asn1String (com.android.hotspot2.asn1.Asn1String)5 OidMappings (com.android.hotspot2.asn1.OidMappings)5