Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/src/impl_ffi/impl_ffi.ec_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ void _validateEllipticCurveKey(_EvpPKey key, EllipticCurve curve) {

_EvpPKey _importPkcs8EcPrivateKey(List<int> keyData, EllipticCurve curve) {
return _Scope.sync((scope) {
final k = ssl.EVP_parse_private_key(scope.createCBS(keyData));
_checkData(k.address != 0, fallback: 'unable to parse key');
final cbs = scope.createCBS(keyData);
final k = ssl.EVP_parse_private_key(cbs);

_checkData(
k.address != 0 && cbs.ref.len == 0,
fallback: 'unable to parse key',
);

final key = _EvpPKey.wrap(k);
_validateEllipticCurveKey(key, curve);
return key;
Expand All @@ -99,13 +105,15 @@ _EvpPKey _importPkcs8EcPrivateKey(List<int> keyData, EllipticCurve curve) {

_EvpPKey _importSpkiEcPublicKey(List<int> keyData, EllipticCurve curve) {
return _Scope.sync((scope) {
// TODO: When calling EVP_parse_public_key it might wise to check that CBS_len(cbs) == 0 is true afterwards
// otherwise it might be that all of the contents of the key was not consumed and we should throw
// a FormatException. Notice that this the case for private/public keys, and RSA keys.
final k = ssl.EVP_parse_public_key(scope.createCBS(keyData));
_checkData(k.address != 0, fallback: 'unable to parse key');
final key = _EvpPKey.wrap(k);
final cbs = scope.createCBS(keyData);
final k = ssl.EVP_parse_public_key(cbs);

_checkData(
k.address != 0 && cbs.ref.len == 0,
fallback: 'unable to parse key',
);

final key = _EvpPKey.wrap(k);
_validateEllipticCurveKey(key, curve);

return key;
Expand Down
20 changes: 16 additions & 4 deletions lib/src/impl_ffi/impl_ffi.rsa_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ part of 'impl_ffi.dart';

_EvpPKey _importPkcs8RsaPrivateKey(List<int> keyData) {
return _Scope.sync((scope) {
final k = ssl.EVP_parse_private_key(scope.createCBS(keyData));
_checkData(k.address != 0, fallback: 'unable to parse key');
final cbs = scope.createCBS(keyData);
final k = ssl.EVP_parse_private_key(cbs);

_checkData(
k.address != 0 && cbs.ref.len == 0,
fallback: 'unable to parse key',
);

final key = _EvpPKey.wrap(k);

_checkData(
Expand All @@ -37,8 +43,14 @@ _EvpPKey _importPkcs8RsaPrivateKey(List<int> keyData) {

_EvpPKey _importSpkiRsaPublicKey(List<int> keyData) {
return _Scope.sync((scope) {
final k = ssl.EVP_parse_public_key(scope.createCBS(keyData));
_checkData(k.address != 0, fallback: 'unable to parse key');
final cbs = scope.createCBS(keyData);
final k = ssl.EVP_parse_public_key(cbs);

_checkData(
k.address != 0 && cbs.ref.len == 0,
fallback: 'unable to parse key',
);

final key = _EvpPKey.wrap(k);

_checkData(
Expand Down