konstantinullrich / crypton

A simple Dart library for asymmetric encryption and digital signatures
https://pub.dev/packages/crypton
MIT License
34 stars 12 forks source link

RangeError (index): Index out of range: index should be less than 2: 2 #17

Closed mcavazotti closed 4 years ago

mcavazotti commented 4 years ago

Stacktrace:

[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: RangeError (index): Index out of range: index should be less than 2: 2
E/flutter ( 6935): 0      _Uint8ArrayView.[]  (dart:typed_data-patch/typed_data_patch.dart:3946:7)
E/flutter ( 6935): 1      new ASN1Boolean.fromBytes 
package:asn1lib/asn1boolean.dart:20
E/flutter ( 6935): 2      ASN1Parser._doPrimitive 
package:asn1lib/asn1parser.dart:91
E/flutter ( 6935): 3      ASN1Parser.nextObject 
package:asn1lib/asn1parser.dart:51
E/flutter ( 6935): 4      new RSAPublicKey.fromString 
package:crypton/…/rsa/public_key.dart:25
E/flutter ( 6935): 5      RSAPublicKey.fromPEM 
package:crypton/…/rsa/public_key.dart:41
E/flutter ( 6935): 6      AppEncryption.initialize 
package:careconnect_patient/services/app_encryption.dart:41
E/flutter ( 6935): <asynchronous suspension>
E/flutter ( 6935): 7      SplashScreen.build.<anonymous closure> 
package:careconnect_patient/screens/splash_screen.dart:18
E/flutter ( 6935): 8      _rootRunUnary  (dart:async/zone.dart:1198:47)
E/flutter ( 6935): 9      _CustomZone.runUnary  (dart:async/zone.dart:1100:19)
E/flutter ( 6935): 10     _FutureListener.handleValue  (dart:async/future_impl.dart:143:18)
E/flutter ( 6935): 11     Future._propagateToListeners.handleValueCallback  (dart:async/future_impl.dart:696:45)
E/flutter ( 6935): 12     Future._propagateToListeners  (dart:async/future_impl.dart:725:32)
E/flutter ( 6935): 13     Future._completeWithValue  (dart:async/future_impl.dart:529:5)
E/flutter ( 6935): 14     Future._asyncCompleteWithValue.<anonymous closure>  (dart:async/future_impl.dart:567:7)
E/flutter ( 6935): 15     _rootRun  (dart:async/zone.dart:1190:13)
E/flutter ( 6935): 16     _CustomZone.run  (dart:async/zone.dart:1093:19)
E/flutter ( 6935): 17     _CustomZone.runGuarded  (dart:async/zone.dart:997:7)
E/flutter ( 6935): 18     _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:1037:23)
E/flutter ( 6935): 19     _microtaskLoop  (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 6935): 20     _startMicrotaskLoop  (dart:async/schedule_microtask.dart:50:5)

Code:

_serverPublicKey = RSAPublicKey.fromPEM(
            "-----BEGIN RSA PUBLIC KEY-----\nMIGJAoGBALePbOELWu7hMBtuF2SKmDRZg57e0opAeA8jXo2S+qcd1gfYh/Dztkw4zkedimDvukkMAKQUpoW2gJcm+7r5dQ/ZRofGL64Gx1eeUkNjzSRESXN/bJk3bI9bZ7269tT3IcyGu6jTlqrS3EWqLF7DgnLqBnPHo+gQEpLveI3FBhL7AgMBAAE=\n-----END RSA PUBLIC KEY-----");
konstantinullrich commented 4 years ago

Let me take a look

bomba1988 commented 4 years ago

I have the same issue. It seems to only appear when using an "external" generated key. When I inject a key which got generated by crypton in my process it is working fine. In my case the keys are generated using NodeJs crypto library. See the following code as example to reproduce key generation:

const crypto = require("crypto");
var { publicKey, privateKey } = crypto.generateKeyPairSync(
    'rsa', {
    modulusLength: 4096,
    publicKeyEncoding: {
      type: 'pkcs1',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs1',
      format: 'pem',
    }
  });
  console.log(privateKey);

I try to import the private key using "RSAPrivateKey.fromPEM", but it is the same error if I strip down the key to be usable with the "fromString" function.

bomba1988 commented 4 years ago

I found a solution which is working for me. In nodejs crypto I have to generate a "rsa-pss" key of type "pkcs8".

const crypto = require("crypto");
var { publicKey, privateKey } = crypto.generateKeyPairSync(
    'rsa-pss', {
    modulusLength: 4096,

    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem',
    }
  });

  console.log(privateKey.toString());

While digging the issue I found that there are several sub versions of pkcs1 and pointycastle is currently only supporting up to v2.0 and it seems that the newer versions v2.1 and/or v2.2 have breaking changes which prevent the import (https://github.com/PointyCastle/pointycastle/blob/master/tutorials/rsa.md#standards-supported). That is a bit of wild guess, since I have not debugged the ASN1 parsing in detail.