org.spongycastle.asn1.ASN1Primitive Java Examples

The following examples show how to use org.spongycastle.asn1.ASN1Primitive. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Crypto.java    From particle-android with Apache License 2.0 6 votes vote down vote up
static PublicKey buildPublicKey(byte[] rawBytes) throws CryptoException {
    try {
        ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(rawBytes));
        SubjectPublicKeyInfo info = SubjectPublicKeyInfo
                .getInstance(new ASN1InputStream(bIn.readObject().getEncoded()).readObject());
        DLSequence dlSequence = (DLSequence) ASN1Primitive.fromByteArray(info.getPublicKeyData().getBytes());
        BigInteger modulus = ((ASN1Integer) dlSequence.getObjectAt(0)).getPositiveValue();
        BigInteger exponent = ((ASN1Integer) dlSequence.getObjectAt(1)).getPositiveValue();

        RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
        KeyFactory kf = getRSAKeyFactory();
        return kf.generatePublic(spec);
    } catch (InvalidKeySpecException | IOException e) {
        throw new CryptoException(e);
    }
}
 
Example #2
Source File: Crypto.java    From spark-setup-android with Apache License 2.0 6 votes vote down vote up
static PublicKey buildPublicKey(byte[] rawBytes) throws CryptoException {
    try {
        //FIXME replacing X509EncodedKeySpec because of problem with 8.1
        //Since 8.1 Bouncycastle cryptography was replaced with implementation from Conscrypt
        //https://developer.android.com/about/versions/oreo/android-8.1.html
        //either it's a bug in Conscrypt, our public key DER structure or use of X509EncodedKeySpec changed
        //alternative needed as this adds expensive Spongycastle dependence
        ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(rawBytes));
        SubjectPublicKeyInfo info = SubjectPublicKeyInfo
                .getInstance(new ASN1InputStream(bIn.readObject().getEncoded()).readObject());
        DLSequence dlSequence = (DLSequence) ASN1Primitive.fromByteArray(info.getPublicKeyData().getBytes());
        BigInteger modulus = ((ASN1Integer) dlSequence.getObjectAt(0)).getPositiveValue();
        BigInteger exponent = ((ASN1Integer) dlSequence.getObjectAt(1)).getPositiveValue();

        RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
        KeyFactory kf = getRSAKeyFactory();
        return kf.generatePublic(spec);
    } catch (InvalidKeySpecException | IOException e) {
        throw new CryptoException(e);
    }
}
 
Example #3
Source File: PublicKeySecurityHandler.java    From PdfBox-Android with Apache License 2.0 5 votes vote down vote up
private byte[][] computeRecipientsField(byte[] seed) throws GeneralSecurityException, IOException
{
    byte[][] recipientsField = new byte[policy.getNumberOfRecipients()][];
    Iterator<PublicKeyRecipient> it = policy.getRecipientsIterator();
    int i = 0;

    while(it.hasNext())
    {
        PublicKeyRecipient recipient = it.next();
        X509Certificate certificate = recipient.getX509();
        int permission = recipient.getPermission().getPermissionBytesForPublicKey();

        byte[] pkcs7input = new byte[24];
        byte one = (byte)(permission);
        byte two = (byte)(permission >>> 8);
        byte three = (byte)(permission >>> 16);
        byte four = (byte)(permission >>> 24);

        System.arraycopy(seed, 0, pkcs7input, 0, 20); // put this seed in the pkcs7 input

        pkcs7input[20] = four;
        pkcs7input[21] = three;
        pkcs7input[22] = two;
        pkcs7input[23] = one;

        ASN1Primitive obj = createDERForRecipient(pkcs7input, certificate);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        DEROutputStream k = new DEROutputStream(baos);

        k.writeObject(obj);

        recipientsField[i] = baos.toByteArray();

        i++;
    }
    return recipientsField;
}