@@ -34,97 +34,155 @@ function stringToNSData(data: string) {
34
34
export class Rsa {
35
35
36
36
importPublicKey ( tag : string , key : string ) {
37
- let pubKey = RSAKeyUtils . importPublicKeyFromPEMTagName ( stripPEMHeader ( key ) , tag ) ;
38
- return new RsaKey ( pubKey ) ;
37
+ try {
38
+ let pubKey = RSAKeyUtils . importPublicKeyFromPEMTagName ( stripPEMHeader ( key ) , tag ) ;
39
+ return new RsaKey ( pubKey ) ;
40
+ }
41
+ catch ( err ) {
42
+ console . warn ( "Rsa.importPublicKey failed with error: " + err ) ;
43
+ return null ;
44
+ }
39
45
}
40
46
importPrivateKey ( tag : string , key : string ) {
41
- let privKey = RSAKeyUtils . importPrivateKeyFromPEMTagName ( stripPEMHeader ( key ) , tag ) ;
42
- return new RsaKey ( privKey ) ;
47
+ try {
48
+ let privKey = RSAKeyUtils . importPrivateKeyFromPEMTagName ( stripPEMHeader ( key ) , tag ) ;
49
+ return new RsaKey ( privKey ) ;
50
+ }
51
+ catch ( err ) {
52
+ console . warn ( "Rsa.importPrivateKey failed with error: " + err ) ;
53
+ return null ;
54
+ }
43
55
}
44
56
removeKeyFromKeychain ( tag : string ) {
45
- RSAKeyUtils . removeKeyFromKeychain ( tag ) ;
57
+ try {
58
+ RSAKeyUtils . removeKeyFromKeychain ( tag ) ;
59
+ }
60
+ catch ( err ) {
61
+ console . warn ( "Rsa.removeKeyFromKeychain failed with error: " + err ) ;
62
+ }
46
63
}
47
64
loadKey ( tag : string ) : RsaKey {
48
65
66
+ if ( tag == null ) {
67
+ return null ;
68
+ }
69
+
49
70
const privTagData = stringToNSData ( tag ) ;
50
71
const query = NSMutableDictionary . new ( ) ;
51
72
query . setValueForKey ( kSecClassKey , kSecClass ) ;
52
73
query . setValueForKey ( privTagData , kSecAttrApplicationTag ) ;
53
74
query . setValueForKey ( kSecAttrKeyTypeRSA , kSecAttrKeyType ) ;
54
75
query . setValueForKey ( true , kSecReturnRef ) ;
55
76
const keyRef = new interop . Reference < any > ( ) ;
56
- let status = SecItemCopyMatching ( query , keyRef ) ;
57
- // CFRelease(query);
58
- if ( status != errSecSuccess ) {
59
- console . log ( 'error: ' + status ) ;
60
- throw new Error ( `loadKey failed with status ${ status } ` ) ;
77
+ try {
78
+ let status = SecItemCopyMatching ( query , keyRef ) ;
79
+ // CFRelease(query);
80
+ if ( status != errSecSuccess ) {
81
+ console . warn ( 'Rsa.loadKey failed with status ' + status ) ;
82
+ return null ;
83
+ }
84
+ else if ( keyRef . value == null ) {
85
+
86
+ console . warn ( 'Rsa.loadKey returned null ' ) ;
87
+ return null ;
88
+ }
89
+ else {
90
+ return new RsaKey ( keyRef . value ) ;
91
+ }
61
92
}
62
- else {
63
- return new RsaKey ( keyRef ) ;
93
+ catch ( err ) {
94
+ console . warn ( 'Rsa.loadKey failed with error ' + err ) ;
95
+ return null ;
64
96
}
65
97
66
98
}
67
99
generateKey ( tag : string , keySize : number , permanent ?: boolean ) {
68
100
69
- const privTagData = stringToNSData ( tag ) ;
70
- const params = NSMutableDictionary . new ( ) ;
71
- params . setValueForKey ( kSecAttrKeyTypeRSA , kSecAttrKeyType ) ;
72
- params . setValueForKey ( NSNumber . numberWithInt ( keySize ) , kSecAttrKeySizeInBits ) ;
73
- const privAttrs = NSMutableDictionary . new ( ) ;
74
-
75
- if ( permanent ) {
76
- privAttrs . setValueForKey ( kCFBooleanTrue , kSecAttrIsPermanent ) ;
77
- }
78
-
79
- privAttrs . setValueForKey ( privTagData , kSecAttrApplicationTag ) ;
80
- params . setObjectForKey ( privAttrs , kSecPrivateKeyAttrs ) ;
81
-
82
- const err = new interop . Reference < NSError > ( ) ;
83
- const keyPair = SecKeyCreateRandomKey ( params , err ) ;
84
- if ( keyPair === null ) {
85
- console . log ( "No key returned: " , err . value ) ;
86
- throw err ;
87
- } else {
88
- console . log ( "Key returned: " , keyPair ) ;
89
- let result = new RsaKey ( keyPair ) ;
90
- return result ;
101
+ try {
102
+ const privTagData = stringToNSData ( tag ) ;
103
+ const params = NSMutableDictionary . new ( ) ;
104
+ params . setValueForKey ( kSecAttrKeyTypeRSA , kSecAttrKeyType ) ;
105
+ params . setValueForKey ( NSNumber . numberWithInt ( keySize ) , kSecAttrKeySizeInBits ) ;
106
+ const privAttrs = NSMutableDictionary . new ( ) ;
107
+
108
+ if ( permanent ) {
109
+ privAttrs . setValueForKey ( kCFBooleanTrue , kSecAttrIsPermanent ) ;
110
+ }
111
+
112
+ privAttrs . setValueForKey ( privTagData , kSecAttrApplicationTag ) ;
113
+ params . setObjectForKey ( privAttrs , kSecPrivateKeyAttrs ) ;
114
+
115
+ const err = new interop . Reference < NSError > ( ) ;
116
+ const keyPair = SecKeyCreateRandomKey ( params , err ) ;
117
+ if ( keyPair === null ) {
118
+ console . log ( "No key returned: " , err . value ) ;
119
+ throw err ;
120
+ } else {
121
+ console . log ( "Key returned: " , keyPair ) ;
122
+ let result = new RsaKey ( keyPair ) ;
123
+ return result ;
124
+ }
91
125
}
126
+ catch ( err ) {
127
+
128
+ console . warn ( 'Rsa.generateKey failed with error ' + err ) ;
129
+ return null ;
92
130
131
+ }
93
132
94
133
}
95
134
96
135
sign ( data : string , key : RsaKey , alg : RsaHashAlgorithm ) {
97
- //let err = new interop.Reference<NSError>();
98
- let nsData = stringToNSData ( data ) ;
99
- let signature = SecKeyCreateSignature ( key . valueOf ( ) , alg , nsData , undefined ) ;
100
- let result = signature . base64EncodedStringWithOptions ( 0 ) ;
101
- // if (nsData) {
102
- // CFRelease(nsData);
103
- // }
104
- // if (signature) {
105
- // CFRelease(signature);
106
- // }
107
- // if (err && err.value) {
108
- // CFRelease(err);
109
- // }
110
- return result ;
136
+ let err = new interop . Reference < NSError > ( ) ;
137
+ try {
138
+ let nsData = stringToNSData ( data ) ;
139
+ let signature = SecKeyCreateSignature ( key . valueOf ( ) , alg , nsData , err ) ;
140
+ let result = signature . base64EncodedStringWithOptions ( 0 ) ;
141
+ // if (nsData) {
142
+ // CFRelease(nsData);
143
+ // }
144
+ // if (signature) {
145
+ // CFRelease(signature);
146
+ // }
147
+ // if (err && err.value) {
148
+ // CFRelease(err);
149
+ // }
150
+ if ( err && err . value ) {
151
+ console . warn ( 'Rsa.verify failed with error ' + err ) ;
152
+ return null ;
153
+ }
154
+ return result ;
155
+ }
156
+ catch ( err ) {
157
+ console . warn ( 'Rsa.sign failed with error ' + err ) ;
158
+ return null ;
159
+ }
111
160
}
112
161
verify ( signature : string , data : string , key : RsaKey , alg : RsaHashAlgorithm ) {
113
- // let err = new interop.Reference<NSError>();
114
- console . log ( signature , data , key , alg ) ;
115
- let signatureBytes = NSData . alloc ( ) . initWithBase64Encoding ( signature ) ;
116
- let nsData = stringToNSData ( data ) ;
117
- let result = SecKeyVerifySignature ( key . valueOf ( ) , alg , nsData , signatureBytes , undefined ) ;
118
- // if (nsData) {
119
- // CFRelease(nsData);
120
- // }
121
- // if (signatureBytes) {
122
- // CFRelease(signatureBytes);
123
- // }
124
- // if (err && err.value) {
125
- // CFRelease(err);
126
- // }
127
- return result ;
162
+ try {
163
+ let err = new interop . Reference < NSError > ( ) ;
164
+ console . log ( signature , data , key , alg ) ;
165
+ let signatureBytes = NSData . alloc ( ) . initWithBase64Encoding ( signature ) ;
166
+ let nsData = stringToNSData ( data ) ;
167
+ let pubKey = key . valueOf ( ) ;
168
+
169
+ let result = SecKeyVerifySignature ( pubKey , alg , nsData , signatureBytes , err ) ;
170
+ // if (nsData) {
171
+ // CFRelease(nsData);
172
+ // }
173
+ // if (signatureBytes) {
174
+ // CFRelease(signatureBytes);
175
+ // }
176
+ if ( err && err . value ) {
177
+ console . warn ( 'Rsa.verify failed with error ' + err ) ;
178
+ return null ;
179
+ }
180
+ return result ;
181
+ }
182
+ catch ( err ) {
183
+ console . warn ( 'Rsa.verify failed with error ' + err ) ;
184
+ return null ;
185
+ }
128
186
}
129
187
// encrypt(data: string, key: RsaKey, alg: RsaEncryptionAlgorithm) {
130
188
// let rawData = stringToNSData(data);
@@ -156,14 +214,18 @@ export class RsaKey {
156
214
pubKeyRef = SecKeyCopyPublicKey ( this . _secKeyRef ) ;
157
215
console . log ( pubKeyRef ) ;
158
216
err = new interop . Reference < NSError > ( ) ;
159
- // pubKeyData = SecKeyCopyExternalRepresentation(this._secKeyRef, err);
160
- // console.log(pubKeyData);
217
+ // pubKeyData = SecKeyCopyExternalRepresentation(this._secKeyRef, err);
218
+ // console.log(pubKeyData);
161
219
if ( err && err . value ) {
162
220
console . log ( "ERR" , err . value . localizedDescription ) ;
163
221
throw err . value . localizedDescription ;
164
222
}
165
223
return RSAKeyUtils . exportPublicKeyToPEM ( pubKeyRef ) ;
166
224
}
225
+ catch ( err ) {
226
+ console . warn ( 'RsaKey.getPublicKey failed with error ' + err ) ;
227
+ return null ;
228
+ }
167
229
finally {
168
230
// if (pubKeyData) {
169
231
// CFRelease(pubKeyData);
0 commit comments