Skip to content

Commit

Permalink
add export for PIV
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed May 11, 2024
1 parent 450abd7 commit 192df09
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/controller/applets/piv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class PivController extends Controller {
}
var cert = X509Certificate.fromAsn1(o);
slotInfo.cert = cert;
slotInfo.certBytes = bytes;
}
}
slots[slot] = slotInfo;
Expand Down
6 changes: 6 additions & 0 deletions lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ class MessageLookup extends MessageLookupByLibrary {
"New Management Key should be 24 bytes long. Please save it in a safe place."),
"pivChangePUK": MessageLookupByLibrary.simpleMessage("Change PUK"),
"pivChangePUKPrompt": m5,
"pivDelete": MessageLookupByLibrary.simpleMessage("Delete"),
"pivEmpty": MessageLookupByLibrary.simpleMessage("Empty"),
"pivExport": MessageLookupByLibrary.simpleMessage("Export"),
"pivExportCertificate":
MessageLookupByLibrary.simpleMessage("Export Certificate"),
"pivGenerate": MessageLookupByLibrary.simpleMessage("Generate"),
"pivImport": MessageLookupByLibrary.simpleMessage("Import"),
"pivKeyManagement":
MessageLookupByLibrary.simpleMessage("Key Management"),
"pivManagementKeyVerificationFailed":
Expand Down
5 changes: 5 additions & 0 deletions lib/generated/intl/messages_zh_Hans.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ class MessageLookup extends MessageLookupByLibrary {
"pivChangeManagementKeyPrompt": MessageLookupByLibrary.simpleMessage(
"新管理密钥的长度应当为 24 字节。请妥善保管管理密钥,否则您将无法管理 PIV 应用。"),
"pivChangePUK": MessageLookupByLibrary.simpleMessage("修改 PUK"),
"pivDelete": MessageLookupByLibrary.simpleMessage("删除"),
"pivEmpty": MessageLookupByLibrary.simpleMessage("空"),
"pivExport": MessageLookupByLibrary.simpleMessage("导出"),
"pivExportCertificate": MessageLookupByLibrary.simpleMessage("导出证书"),
"pivGenerate": MessageLookupByLibrary.simpleMessage("生成"),
"pivImport": MessageLookupByLibrary.simpleMessage("导入"),
"pivKeyManagement":
MessageLookupByLibrary.simpleMessage("密钥管理(Key Management)"),
"pivManagementKeyVerificationFailed":
Expand Down
50 changes: 50 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@
"pivOriginGenerated": "Generated",
"pivOriginImported": "Imported",
"pivCertificate": "Certificate",
"pivImport": "Import",
"pivGenerate": "Generate",
"pivExport": "Export",
"pivDelete": "Delete",
"pivExportCertificate": "Export Certificate",
"validationHexString": "Please input a valid hexadecimal string.",
"validationExactLength": "Need exact {length} characters"
}
5 changes: 5 additions & 0 deletions lib/l10n/intl_zh_Hans.arb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@
"pivOriginGenerated": "内部生成",
"pivOriginImported": "外部导入",
"pivCertificate": "证书",
"pivImport": "导入",
"pivGenerate": "生成",
"pivExport": "导出",
"pivDelete": "删除",
"pivExportCertificate": "导出证书",
"validationHexString": "请输入十六进制字符串",
"validationExactLength": "需要 {length} 个字符"
}
1 change: 1 addition & 0 deletions lib/models/piv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class SlotInfo {
final bool defaultValue;
final int retriesCount;
final int remainingCount;
List<int>? certBytes;
X509Certificate? cert;

SlotInfo(this.number, this.algorithm, this.pinPolicy, this.touchPolicy, this.origin, this.public, this.defaultValue, this.retriesCount, this.remainingCount);
Expand Down
54 changes: 49 additions & 5 deletions lib/views/applets/piv.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';

import 'package:canokey_console/controller/applets/piv.dart';
import 'package:canokey_console/generated/l10n.dart';
Expand All @@ -22,10 +24,12 @@ import 'package:canokey_console/helper/widgets/validators.dart';
import 'package:canokey_console/models/piv.dart';
import 'package:canokey_console/views/layout/layout.dart';
import 'package:convert/convert.dart';
import 'package:file_saver/file_saver.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:logging/logging.dart';
import 'package:lucide_icons/lucide_icons.dart';
import 'package:pem/pem.dart';

final log = Logger('Console:PIV:View');

Expand Down Expand Up @@ -550,32 +554,72 @@ class _PivPageState extends State<PivPage> with SingleTickerProviderStateMixin,
elevation: 0,
padding: Spacing.xy(20, 16),
backgroundColor: contentTheme.primary,
child: CustomizedText.labelMedium('Generate', color: contentTheme.onSecondary),
child: CustomizedText.labelMedium(S.of(context).pivGenerate, color: contentTheme.onSecondary),
),
Spacing.width(12),
CustomizedButton.rounded(
onPressed: () {},
elevation: 0,
padding: Spacing.xy(20, 16),
backgroundColor: contentTheme.primary,
child: CustomizedText.labelMedium('Import', color: contentTheme.onPrimary),
child: CustomizedText.labelMedium(S.of(context).pivImport, color: contentTheme.onPrimary),
),
if (slot != null) ...[
Spacing.width(12),
CustomizedButton.rounded(
onPressed: () {},
onPressed: () {
// A dialog with two buttons: DER and PEM
Get.dialog(Dialog(
child: SizedBox(
width: 300,
child: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [
Padding(padding: Spacing.all(16), child: CustomizedText.labelLarge(S.of(context).pivExportCertificate)),
Divider(height: 0, thickness: 1),
Padding(
padding: Spacing.all(16),
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
CustomizedButton.rounded(
onPressed: () async {
// Export DER
await FileSaver.instance.saveFile(name: 'certificate.der', bytes: slot.certBytes! as Uint8List);
if (mounted) {
Navigator.pop(context);
}
},
elevation: 0,
padding: Spacing.xy(20, 16),
backgroundColor: contentTheme.primary,
child: CustomizedText.labelMedium('DER', color: contentTheme.onPrimary),
),
Spacing.width(12),
CustomizedButton.rounded(
onPressed: () async {
String pem = PemCodec(PemLabel.certificate).encode(slot.certBytes!);
await FileSaver.instance.saveFile(name: 'certificate.pem', bytes: utf8.encode(pem));
if (mounted) {
Navigator.pop(context);
}
},
elevation: 0,
padding: Spacing.xy(20, 16),
backgroundColor: contentTheme.primary,
child: CustomizedText.labelMedium('PEM', color: contentTheme.onPrimary),
)
]))
]))));
},
elevation: 0,
padding: Spacing.xy(20, 16),
backgroundColor: contentTheme.primary,
child: CustomizedText.labelMedium('Export', color: contentTheme.onPrimary),
child: CustomizedText.labelMedium(S.of(context).pivExport, color: contentTheme.onPrimary),
),
Spacing.width(12),
CustomizedButton.rounded(
onPressed: () {},
elevation: 0,
padding: Spacing.xy(20, 16),
backgroundColor: contentTheme.danger,
child: CustomizedText.labelMedium('Delete', color: contentTheme.onDanger),
child: CustomizedText.labelMedium(S.of(context).pivDelete, color: contentTheme.onDanger),
),
],
],
Expand Down
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
#include "generated_plugin_registrant.h"

#include <ccid/ccid_plugin.h>
#include <file_saver/file_saver_plugin.h>
#include <flutter_webrtc/flutter_web_r_t_c_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) ccid_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "CcidPlugin");
ccid_plugin_register_with_registrar(ccid_registrar);
g_autoptr(FlPluginRegistrar) file_saver_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSaverPlugin");
file_saver_plugin_register_with_registrar(file_saver_registrar);
g_autoptr(FlPluginRegistrar) flutter_webrtc_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterWebRTCPlugin");
flutter_web_r_t_c_plugin_register_with_registrar(flutter_webrtc_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
ccid
file_saver
flutter_webrtc
url_launcher_linux
)
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation
import ccid
import cryptography_flutter
import device_info_plus
import file_saver
import flutter_webrtc
import mobile_scanner
import path_provider_foundation
Expand All @@ -18,6 +19,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
CcidPlugin.register(with: registry.registrar(forPlugin: "CcidPlugin"))
CryptographyFlutterPlugin.register(with: registry.registrar(forPlugin: "CryptographyFlutterPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
FileSaverPlugin.register(with: registry.registrar(forPlugin: "FileSaverPlugin"))
FlutterWebRTCPlugin.register(with: registry.registrar(forPlugin: "FlutterWebRTCPlugin"))
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dependencies:
dart_des: ^1.0.2
x509: ^0.2.4+2
asn1lib: ^1.5.2
pem: ^2.0.5
file_saver: ^0.2.12

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include "generated_plugin_registrant.h"

#include <ccid/ccid_plugin_c_api.h>
#include <file_saver/file_saver_plugin.h>
#include <flutter_webrtc/flutter_web_r_t_c_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
CcidPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("CcidPluginCApi"));
FileSaverPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FileSaverPlugin"));
FlutterWebRTCPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterWebRTCPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
ccid
file_saver
flutter_webrtc
url_launcher_windows
)
Expand Down

0 comments on commit 192df09

Please sign in to comment.