GithubHelp home page GithubHelp logo

rsa_pkcs's Introduction

Build Status

rsa_pkcs

Privacy-Enhanced Mail (PEM) parser, RSA, PKCS#1, PKCS#8, X.509

openssl genrsa -out rsa_private_key.pem

openssl rsa -in rsa_private_key.pem -text -noout -out rsa_private_key.txt

openssl pkcs8 -in rsa_private_key.pem -topk8 -v2 aes-256-cbc -out rsa_enc_private_key.pem

openssl pkcs8 -in rsa_private_key.pem -topk8 -v2 aes-256-cbc -v2prf hmacWithSHA256 -out rsa_enc_private_key.pem

openssl pkcs8 -in rsa_private_key.pem -nocrypt -topk8 -out rsa_pkcs8_private_key.pem
library rsa_pkcs_test;

import 'dart:io';
import 'package:test/test.dart';
import 'package:rsa_pkcs/rsa_pkcs.dart';

/// Test suite
void main() {
  test('X509 certificate', () {
    final certificateFile = File('test/resource/certificate.pem');
    final pem = certificateFile.readAsStringSync();

    final parser = RSAPKCSParser();
    final pair = parser.parsePEM(pem);

    expect(pair.private, isNull);
    expect(pair.public, isNotNull);

    expect(
        pair.public.modulus,
        BigInt.parse(
          '28033090497217768360343969660651078939521584065407386701840944857554190333345377486890608592313957403519912593868573272324833566960046565212209064828701606844042844758296252915683436083950709369588282830401703789082050482708460081183032757865928569658700963214439372520258229921591300947157909982421739994731742762665888890674063869593136209633509251161824923657193583205462354862058115540928040675840828039354822562384195703281964881672495986693976525885611588824710685812476559097665742184509878129643078068928823514354625680850083823859349966847983641526471178472606740080720731992805405488827115995037727686469357',
        ));
    expect(pair.public.publicExponent, equals(65537));
  });

  test('rsa private key PKCS#1', () {
    //openssl genrsa -out rsa_private_key.pem
    //
    final File rsaPrivateKeyFile = File('test/resource/rsa_private_key.pem');
    final String pem = rsaPrivateKeyFile.readAsStringSync();
    final RSAPKCSParser parser = RSAPKCSParser();
    final RSAKeyPair pair = parser.parsePEM(pem);
    final RSAPrivateKey privateKey = pair.private;

    expect(pair.public, equals(null));
    expect(privateKey != null, equals(true));
    expect(privateKey.version, equals(0));
    final BigInt expectedModulus = BigInt.parse(
      '00d83c3cacb3b767a1020f947ca2012010ba494d86bda1efd437357b91d5c1e61b12384cd3c01f628312a5ef15cf003f62c4f6b835bbb3ea99409f87e583fa6991',
      radix: 16,
    );
    expect(privateKey.modulus, expectedModulus);
    expect(privateKey.publicExponent, equals(65537));

    final BigInt expectedPrivateComponent = BigInt.parse(
        '00a0c22fcda992b9cd5eeddc53c85193d83bd6917791f6198a293d6ecfde1e5885fbc0a766aaca385dd8b3b16a58201baec3900b5c1636321a0167e956d5fbe001',
        radix: 16);
    expect(privateKey.privateExponent, expectedPrivateComponent);
    expect(
        privateKey.prime1,
        BigInt.parse(
            '00f3103a1bb14f88f096983ecd86cb51f9e0e9325030039be4fbb4176de1b73f91',
            radix: 16));
    expect(
        privateKey.prime2,
        BigInt.parse(
            '00e3be793b593e4ed5bce5db1ff563468eeb5cc715b12badf223d1970cd88f8a01',
            radix: 16));
    expect(
        privateKey.exponent1,
        BigInt.parse(
            '7e4e8863ab98310914b8b8aa04c9d3278e809fec9b86c49411585c74753ecc81',
            radix: 16));
    expect(
        privateKey.exponent2,
        BigInt.parse(
            '687f8272f7ecfe11569e855ff1a17ec39f3d2fe0452e0c9f794df7281eca2601',
            radix: 16));
    expect(
        privateKey.coefficient,
        BigInt.parse(
            '0092e895c1a9b7b705fb694bba2d52cb9901d4628db794eb43861b086c557d2913',
            radix: 16));
  });

  test('rsa public key PKCS#8', () {
    //openssl genrsa -out rsa_private_key.pem
    //openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
    final File rsaPrivateKeyFile = File('test/resource/rsa_public_key.pem');
    final String pem = rsaPrivateKeyFile.readAsStringSync();
    final RSAPKCSParser parser = RSAPKCSParser();
    final RSAKeyPair pair = parser.parsePEM(pem);
    final RSAPrivateKey privateKey = pair.private;
    final RSAPublicKey publicKey = pair.public;

    expect(privateKey, equals(null));
    expect(publicKey != null, equals(true));

    expect(
        publicKey.modulus,
        BigInt.parse(
            '00d83c3cacb3b767a1020f947ca2012010ba494d86bda1efd437357b91d5c1e61b12384cd3c01f628312a5ef15cf003f62c4f6b835bbb3ea99409f87e583fa6991',
            radix: 16));
    expect(publicKey.publicExponent, equals(65537));
  });

  test('rsa private key PKCS#8', () {
    //openssl genrsa -out rsa_private_key.pem
    //openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
    final File rsaPrivateKeyFile =
        File('test/resource/rsa_pkcs8_private_key.pem');
    final String pem = rsaPrivateKeyFile.readAsStringSync();
    final RSAPKCSParser parser = RSAPKCSParser();
    final RSAKeyPair pair = parser.parsePEM(pem);
    final RSAPrivateKey privateKey = pair.private;
    final RSAPublicKey publicKey = pair.public;

    expect(privateKey != null, equals(true));
    expect(publicKey, equals(null));

    expect(
        privateKey.modulus,
        BigInt.parse(
            'a2d3fcf35dc37d26edaa77de11380ef3152939b15e8967c3a643ee83da33d9869a2d1d2663306e14629c999ad13545e6e62b0f12bad737edeb1bf26f400d850c3f36adc5fb84ad8ad6459ec51360f2c1d30068fb1e1df44698cf8be30097d8d4add4acb8591093546ac41421b41348b7d5bbe447fe235366995a974166986895',
            radix: 16));
    expect(
        privateKey.privateExponent,
        BigInt.parse(
            '8db5ab66eecaad484cfdd876b74baf8f15729c98666b75984c42c0f995d51c52ce29c73dda8392ba411c837ebee6fb603a1f6d6de2985e3fbd27c475d82c2c0698aa9b66da964072c1f5d488a3f61bb201429cecd2bfd9449f965d88b4d5ce8ed76934e1a0644cc5fd195adaadfe0c3090d40b4f6cc0bdce4badc91369bc58bd',
            radix: 16));
  });
}

rsa_pkcs's People

Contributors

amaurel avatar atrope avatar augustozanellato avatar priezz avatar pulyaevskiy avatar racr0x avatar scribetw avatar shamblett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rsa_pkcs's Issues

NNBD support

Are there any plans for supporting NNBD? (that would mainly mean to bump crypto to 3.0.0 and asn1lib to 0.9.0 and change some types here and there)

Cannot parse Firebase RSA key

Hi and thanks for the work. I'm using corsac_jwt which uses rsa_pkcs from parsing RSA public keys.

I cannot manage to parse keys (.RSAPKCSParser.parsePEM) got from https://www.googleapis.com/robot/v1/metadata/x509/[email protected] which are (I think) RSA keys but I could be wrong (doc https://firebase.google.com/docs/auth/admin/verify-id-tokens#verify_id_tokens_using_a_third-party_jwt_library). It fails finding the correct header (and if i trick the headers, it fails with a Failing Expected a value of type 'ASN1Integer', but got one of type 'ASN1Sequence error).

Should this work somehow (i.e. it just a feature missing) or am I wrong in my usage?

Thanks

RangeError (index): Invalid value: Not in inclusive range 0..5: 6 when passing a X.509 version 1 certificate

-----BEGIN CERTIFICATE-----
MIICizCCAfQCCQCY8tKaMc0BMjANBgkqhkiG9w0BAQUFADCBiTELMAkGA1UEBhMC
Tk8xEjAQBgNVBAgTCVRyb25kaGVpbTEQMA4GA1UEChMHVU5JTkVUVDEOMAwGA1UE
CxMFRmVpZGUxGTAXBgNVBAMTEG9wZW5pZHAuZmVpZGUubm8xKTAnBgkqhkiG9w0B
CQEWGmFuZHJlYXMuc29sYmVyZ0B1bmluZXR0Lm5vMB4XDTA4MDUwODA5MjI0OFoX
DTM1MDkyMzA5MjI0OFowgYkxCzAJBgNVBAYTAk5PMRIwEAYDVQQIEwlUcm9uZGhl
aW0xEDAOBgNVBAoTB1VOSU5FVFQxDjAMBgNVBAsTBUZlaWRlMRkwFwYDVQQDExBv
cGVuaWRwLmZlaWRlLm5vMSkwJwYJKoZIhvcNAQkBFhphbmRyZWFzLnNvbGJlcmdA
dW5pbmV0dC5ubzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAt8jLoqI1VTlx
AZ2axiDIThWcAOXdu8KkVUWaN/SooO9O0QQ7KRUjSGKN9JK65AFRDXQkWPAu4Hln
O4noYlFSLnYyDxI66LCr71x4lgFJjqLeAvB/GqBqFfIZ3YK/NrhnUqFwZu63nLrZ
jcUZxNaPjOOSRSDaXpv1kb5k3jOiSGECAwEAATANBgkqhkiG9w0BAQUFAAOBgQBQ
Yj4cAafWaYfjBU2zi1ElwStIaJ5nyp/s/8B8SAPK2T79McMyccP3wSW13LHkmM1j
wKe3ACFXBvqGQN0IbcH49hu0FKhYFM/GPDJcIHFBsiyMBXChpye9vBaTNEBCtU3K
jjyG0hRT2mAQ9h+bkPmOvlEo/aH0xR68Z9hw4PF13w==
-----END CERTIFICATE-----
final key = File('sample.pem').readAsStringSync();
final parser = RSAPKCSParser();
final puk = parser.parsePEM(key).public;

Error

dart:core                                      List.[]
package:rsa_pkcs/src/parser.dart 77:52         RSAPKCSParser._pkcs8CertificatePrivateKey
package:rsa_pkcs/src/parser.dart 147:14        RSAPKCSParser._publicKey
package:rsa_pkcs/src/parser.dart 36:23         RSAPKCSParser.parsePEM
RangeError (index): Invalid value: Not in inclusive range 0..5: 6

I made a Pull Request #17 to fix this issue. Thanks.

dart2js compatability

Hi,

I get the following error when I try to use rsa_pkcs with dart2js:

Unsupported operation: Int64List not supported by dart2js.

Int64List is only used in this file: packages/asn1lib/asn1integer.dart

Compatibility with PointyCastle

PointyCastle has a new version 1.0.0-rc2, and i cannot use this package since the limit is PointyCastle's version 0.11.0.
Are you considering to update this package?

PKCS12

Do you have any plans on supporting PKCS12 / PFX aswell?

pkcs8 doesnt work in this package

hello i am trying to parse a key in pkcs8 format and it doesnt work, i am getting error:
type 'ASN1OctetString' is not a subtype of type 'ASN1Integer' in type cast
there are no tests indicating that it should work but description says that it does.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.