-
Notifications
You must be signed in to change notification settings - Fork 13
/
identity.dart
85 lines (74 loc) · 2.3 KB
/
identity.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import 'dart:convert';
import 'dart:typed_data';
import 'package:basic_utils/basic_utils.dart';
class Identity {
final String name;
late final Uint8List cert;
late final Uint8List privateKey;
late final String certString;
late final String privateKeyString;
late final Map<String, String?> subject;
final List<String> pages = [];
Identity(this.name,
{days = 365000,
String? existingCertString,
String? existingPrivateKeyString}) {
AsymmetricKeyPair keyPair = CryptoUtils.generateRSAKeyPair();
if (existingCertString != null) {
certString = existingCertString;
var x509 = X509Utils.x509CertificateFromPem(certString);
subject = x509.subject.entries.fold({}, (accum, entry) {
var a = ASN1ObjectIdentifier.fromIdentifierString(entry.key);
if (entry.value != null && a.readableName != null) {
accum[a.readableName!] = entry.value;
}
return accum;
});
} else {
Map<String, String> newSubject = {'commonName': name};
subject = newSubject;
var x = X509Utils.generateRsaCsrPem(
newSubject,
keyPair.privateKey as RSAPrivateKey,
keyPair.publicKey as RSAPublicKey);
certString =
X509Utils.generateSelfSignedCertificate(keyPair.privateKey, x, 100);
}
if (existingPrivateKeyString != null) {
privateKeyString = existingPrivateKeyString;
CryptoUtils.rsaPrivateKeyFromPem(privateKeyString);
} else {
privateKeyString = CryptoUtils.encodeRSAPrivateKeyToPem(
keyPair.privateKey as RSAPrivateKey);
}
var utf8encoder = const Utf8Encoder();
cert = utf8encoder.convert(certString);
privateKey = utf8encoder.convert(privateKeyString);
}
static bool validateCert(certString) {
try {
X509Utils.x509CertificateFromPem(certString);
} catch (e) {
return false;
}
return true;
}
static bool validatePrivateKey(privateKeyString) {
try {
CryptoUtils.rsaPrivateKeyFromPem(privateKeyString);
} catch (e) {
return false;
}
return true;
}
addPage(String page) {
pages.add(page);
}
matches(Uri uri) {
var check = uri.toString();
return pages.any((page) {
return check == page ||
check.startsWith(page.endsWith("/") ? page : page + "/");
});
}
}