Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get X509Certificate for alias in Android #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.astrox.secure_p256_plugin

import android.content.Context
//import android.content.pm.PackageManager
import android.os.Build
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.util.Base64
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
Expand All @@ -13,6 +13,7 @@ import io.flutter.plugin.common.MethodChannel.Result
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.conscrypt.Conscrypt
import java.security.*
import java.security.cert.Certificate
import java.security.spec.ECGenParameterSpec
import java.security.spec.EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
Expand Down Expand Up @@ -54,6 +55,12 @@ class SecureP256Plugin : FlutterPlugin, MethodCallHandler {
result.success(keyPair.public.encoded)
}

"getCertificate" -> {
val alias = call.argument<String>("tag")!!
val certificate = getCertificate(alias)
result.success(Base64.encodeToString(certificate.encoded, Base64.NO_WRAP))
}

"sign" -> {
val cAlias = call.argument<String>("tag")!!
val payload = call.argument<ByteArray>("payload")!!
Expand Down Expand Up @@ -185,6 +192,12 @@ class SecureP256Plugin : FlutterPlugin, MethodCallHandler {
return agreement.generateSecret()
}

private fun getCertificate(alias: String): Certificate {
val ks: KeyStore = KeyStore.getInstance(storeProvider).apply { load(null) }
val entry = obtainPrivateKeyEntryFromAlias(alias, ks)
return entry.certificate as Certificate
}

// private fun hasStrongBox(): Boolean {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// return applicationContext!!.packageManager.hasSystemFeature(
Expand Down
23 changes: 20 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:agent_dart/identity/p256.dart';
import 'package:agent_dart/agent_dart.dart';
import 'package:asn1lib/asn1lib.dart' as asn1lib;
import 'package:convert/convert.dart';
import 'package:flutter/material.dart';
import 'package:secp256r1/secp256r1.dart';
import 'package:tuple/tuple.dart';
import 'package:x509/x509.dart' as x509;

import 'package:secp256r1/secp256r1.dart';

void main() {
runApp(const MyApp());
Expand All @@ -22,7 +25,7 @@ class _MyAppState extends State<MyApp> {
String _publicKey = 'Unknown';
String _signed = 'Unknown';
bool? _verified;
String? _sharedSecret, _decrypted;
String? _certificate, _sharedSecret, _decrypted;
Tuple2<Uint8List, Uint8List>? _encrypted;

final _payloadTEC = TextEditingController(text: 'Hello world');
Expand All @@ -42,6 +45,7 @@ class _MyAppState extends State<MyApp> {
body: ListView(
children: [
SelectableText('getPublicKey: $_publicKey\n'),
SelectableText('certificate: $_certificate\n'),
SelectableText('sign: $_signed\n'),
SelectableText('verify: $_verified\n'),
SelectableText('sharedSecret: $_sharedSecret\n'),
Expand Down Expand Up @@ -75,6 +79,19 @@ class _MyAppState extends State<MyApp> {
},
child: const Text('getPublicKey'),
),
ElevatedButton(
onPressed: () {
SecureP256.getCertificate(alias).then(
(r) => setState(() {
final decoded = base64Decode(r.toString());
final seq = asn1lib.ASN1Sequence.fromBytes(decoded);
final cert = x509.X509Certificate.fromAsn1(seq);
_certificate = cert.toString();
}),
);
},
child: const Text('getCertificate'),
),
ElevatedButton(
onPressed: () {
SecureP256.sign(
Expand Down
9 changes: 9 additions & 0 deletions lib/p256_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class SecureP256Channel extends SecureP256Platform {
return keyBytes;
}

@override
Future<String> getCertificate(String tag) async {
final signature = await methodChannel.invokeMethod(
Methods.getCertificate,
{'tag': tag},
);
return signature;
}

@override
Future<Uint8List> sign(String tag, Uint8List payload) async {
final signature = await methodChannel.invokeMethod(
Expand Down
4 changes: 4 additions & 0 deletions lib/p256_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ abstract class SecureP256Platform extends PlatformInterface {
return _instance.getPublicKey(tag);
}

Future<dynamic> getCertificate(String tag) {
return _instance.getCertificate(tag);
}

Future<Uint8List> sign(String tag, Uint8List payload) {
return _instance.sign(tag, payload);
}
Expand Down
6 changes: 6 additions & 0 deletions lib/secp256r1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class SecureP256 {
}
}

static Future<String> getCertificate(String tag) async {
assert(tag.isNotEmpty);
final certificate = await SecureP256Platform.instance.getCertificate(tag);
return certificate;
}

static Future<Uint8List> sign(String tag, Uint8List payload) async {
assert(tag.isNotEmpty);
assert(payload.isNotEmpty);
Expand Down
1 change: 1 addition & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Methods {

static const getPublicKey = 'getPublicKey';
static const sign = 'sign';
static const getCertificate = 'getCertificate';
static const verify = 'verify';
static const getSharedSecret = 'getSharedSecret';
}
6 changes: 4 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ version: 0.1.0-dev.7
repository: https://github.com/AstroxNetwork/flutter_secp256r1

environment:
sdk: '>=2.13.0 <4.0.0'
flutter: '>=2.0.0'
sdk: ">=2.13.0 <4.0.0"
flutter: ">=2.0.0"

dependencies:
asn1lib: ^1.5.2
flutter:
sdk: flutter
agent_dart: ^1.0.0-dev.8
plugin_platform_interface: ^2.0.2
tuple: ^2.0.0
x509: ^0.2.4+2

dev_dependencies:
flutter_test:
Expand Down