Search in sources :

Example 46 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project ldapsdk by pingidentity.

the class VirtualListViewRequestControlTestCase method testConstructor9ValueSequenceInvalid.

/**
 * Tests the ninth constructor with a generic control with a value sequence
 * whose third element contains an invalid type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testConstructor9ValueSequenceInvalid() throws Exception {
    ASN1Element[] elements = { new ASN1Integer(0), new ASN1Integer(0), new ASN1OctetString("foo") };
    Control c = new Control(VirtualListViewRequestControl.VIRTUAL_LIST_VIEW_REQUEST_OID, true, new ASN1OctetString(new ASN1Sequence(elements).encode()));
    c = new VirtualListViewRequestControl(c);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Control(com.unboundid.ldap.sdk.Control) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ASN1Integer(com.unboundid.asn1.ASN1Integer) Test(org.testng.annotations.Test)

Example 47 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project ldapsdk by pingidentity.

the class AssuredReplicationServerResultTestCase method testDecodeValueSequenceInvalidResultCode.

/**
 * Tests the behavior when trying to decode an ASN.1 element that is a
 * sequence containing an unrecognized result code.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeValueSequenceInvalidResultCode() throws Exception {
    final ASN1Sequence resultSequence = new ASN1Sequence(new ASN1Enumerated((byte) 0x80, 1234), new ASN1Integer((byte) 0x81, 5678));
    AssuredReplicationServerResult.decode(resultSequence);
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Enumerated(com.unboundid.asn1.ASN1Enumerated) ASN1Integer(com.unboundid.asn1.ASN1Integer) Test(org.testng.annotations.Test)

Example 48 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project ldapsdk by pingidentity.

the class AssuredReplicationServerResultTestCase method testDecodeValueSequenceMissingResultCode.

/**
 * Tests the behavior when trying to decode an ASN.1 element that is a
 * sequence that does not have a result code.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeValueSequenceMissingResultCode() throws Exception {
    final ASN1Sequence resultSequence = new ASN1Sequence(new ASN1Integer((byte) 0x81, 1234));
    AssuredReplicationServerResult.decode(resultSequence);
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Integer(com.unboundid.asn1.ASN1Integer) Test(org.testng.annotations.Test)

Example 49 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project gdmatrix by gdmatrix.

the class CMSUtils method createTimeStampRequest.

public static TimeStampReq createTimeStampRequest(byte[] message, String nonce, boolean requireCert, Extensions extensions, String digestAlgorithm, String timestampPolicy) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    byte[] hashedMsg = md.digest(message);
    ASN1ObjectIdentifier identifier = new ASN1ObjectIdentifier(digestAlgorithm);
    org.bouncycastle.asn1.tsp.MessageImprint imprint = new org.bouncycastle.asn1.tsp.MessageImprint(new AlgorithmIdentifier(identifier), hashedMsg);
    TimeStampReq request = new TimeStampReq(imprint, timestampPolicy != null ? new ASN1ObjectIdentifier(timestampPolicy) : null, nonce != null ? new ASN1Integer(nonce.getBytes()) : null, ASN1Boolean.getInstance(requireCert), extensions);
    return request;
}
Also used : TimeStampReq(org.bouncycastle.asn1.tsp.TimeStampReq) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) MessageDigest(java.security.MessageDigest) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 50 with ASN1Integer

use of com.mindbright.asn1.ASN1Integer in project identity-credential by google.

the class Util method signatureDerToCose.

/*
     * From RFC 8152 section 8.1 ECDSA:
     *
     * The signature algorithm results in a pair of integers (R, S).  These
     * integers will be the same length as the length of the key used for
     * the signature process.  The signature is encoded by converting the
     * integers into byte strings of the same length as the key size.  The
     * length is rounded up to the nearest byte and is left padded with zero
     * bits to get to the correct length.  The two integers are then
     * concatenated together to form a byte string that is the resulting
     * signature.
     */
private static byte[] signatureDerToCose(byte[] signature, int keySize) {
    ASN1Primitive asn1;
    try {
        asn1 = new ASN1InputStream(new ByteArrayInputStream(signature)).readObject();
    } catch (IOException e) {
        throw new IllegalArgumentException("Error decoding DER signature", e);
    }
    if (!(asn1 instanceof ASN1Sequence)) {
        throw new IllegalArgumentException("Not a ASN1 sequence");
    }
    ASN1Encodable[] asn1Encodables = ((ASN1Sequence) asn1).toArray();
    if (asn1Encodables.length != 2) {
        throw new IllegalArgumentException("Expected two items in sequence");
    }
    if (!(asn1Encodables[0].toASN1Primitive() instanceof ASN1Integer)) {
        throw new IllegalArgumentException("First item is not an integer");
    }
    BigInteger r = ((ASN1Integer) asn1Encodables[0].toASN1Primitive()).getValue();
    if (!(asn1Encodables[1].toASN1Primitive() instanceof ASN1Integer)) {
        throw new IllegalArgumentException("Second item is not an integer");
    }
    BigInteger s = ((ASN1Integer) asn1Encodables[1].toASN1Primitive()).getValue();
    byte[] rBytes = stripLeadingZeroes(r.toByteArray());
    byte[] sBytes = stripLeadingZeroes(s.toByteArray());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        for (int n = 0; n < keySize - rBytes.length; n++) {
            baos.write(0x00);
        }
        baos.write(rBytes);
        for (int n = 0; n < keySize - sBytes.length; n++) {
            baos.write(0x00);
        }
        baos.write(sBytes);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return baos.toByteArray();
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) IOException(java.io.IOException) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ECPoint(java.security.spec.ECPoint) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Aggregations

ASN1Integer (org.bouncycastle.asn1.ASN1Integer)213 ASN1Integer (com.unboundid.asn1.ASN1Integer)96 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)94 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)91 IOException (java.io.IOException)89 DERSequence (org.bouncycastle.asn1.DERSequence)89 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)86 BigInteger (java.math.BigInteger)86 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)80 ASN1Element (com.unboundid.asn1.ASN1Element)69 Test (org.testng.annotations.Test)63 ArrayList (java.util.ArrayList)50 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)49 DERSequence (com.github.zhenwei.core.asn1.DERSequence)47 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)47 DEROctetString (org.bouncycastle.asn1.DEROctetString)38 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)35 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)28 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)27 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)27