summaryrefslogtreecommitdiffhomepage
path: root/npcompatsrv/fribid_npapi_compat.user.js
blob: 263fcde17e0f993b7cd618b1da930ddec6b975da (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302

// ==UserScript==
// @name FriBID NPAPI Compatibility
// @description FriBID compatibility script for pages using BankID through NPAPI
// @namespace https://fribid.se/
// @version 1.0.4
// @run-at document-start
// @grant none
// @match https://*/*
// ==/UserScript==

/*

  Copyright (c) 2014 Samuel Lidén Borell <samuel@kodafritt.se>
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the 'Software'), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

function main() {
    if (!window.navigator.plugins['Nexus Personal']) {
        FriBID_NPAPI_Compat = {
            // TODO we might need to use HTTPS
            'REQ_URL': 'http://127.0.0.1:20048/FriBID_NPAPI_Request',
            
            'TYPE_AUTH': 'application/x-personal-authentication',
            'TYPE_REGUTIL': 'application/x-personal-regutil',
            'TYPE_SIGN': 'application/x-personal-signer2',
            'TYPE_OLDSIGN': 'application/x-personal-signer',
            'TYPE_VER': 'application/x-personal-version',
            'TYPE_WEBADMIN': 'application/x-personal-webadmin',
            
            'hasParam': function(obj, name) {
                if (typeof name != 'string') return false;
                name = name.toLowerCase();
                
                if (!obj.FriBID_NPAPI_Compat_Params) {
                    this.setDefaults(obj);
                }
                return name in obj.FriBID_NPAPI_Compat_Params;
            },
            
            'setParam': function(obj, name, value) {
                if (typeof name != 'string') return this.setLastError(obj, 8004);
                name = name.toLowerCase();
                
                if (!obj.FriBID_NPAPI_Compat_Params) {
                    this.setDefaults(obj);
                }
                obj.FriBID_NPAPI_Compat_Params[name] = value;
                return 0;
            },
            
            'getParam': function(obj, name) {
                if (typeof name != 'string') return this.setLastError(obj, 8004);
                name = name.toLowerCase();
                
                if (!obj.FriBID_NPAPI_Compat_Params) {
                    this.setDefaults(obj);
                }
                return obj.FriBID_NPAPI_Compat_Params[name];
            },
            
            'setDefaults': function(obj) {
                params = {};
                switch (obj.type) {
                    case this.TYPE_AUTH:
                        params['challenge'] = '';
                        params['onlyacceptmru'] = 'false';
                        params['policys'] = '';
                        params['servertime'] = '';
                        params['signature'] = '';
                        params['subjects'] = '';
                        break;
                    case this.TYPE_SIGN:
                        params['nonce'] = '';
                        params['nonvisibledata'] = '';
                        params['onlyacceptmru'] = 'false';
                        params['policys'] = '';
                        params['servertime'] = '';
                        params['signature'] = '';
                        params['subjects'] = '';
                        params['textcharacterencoding'] = '';
                        params['texttobesigned'] = '';
                        break;
                    case this.TYPE_REGUTIL:
                        params['subjectdn'] = '';
                        params['onetimepassword'] = '';
                        params['keysize'] = '';
                        params['minlen'] = '';
                        params['minchars'] = '';
                        params['mindigits'] = '';
                        params['maxlen'] = '';
                        params['keyusage'] = '';
                        params['rfc2797cmcoid'] = 'true';
                        break;
                }
                obj.FriBID_NPAPI_Compat_Params = params;
            },
            
            'hasRequiredParams': function (obj) {
                switch (obj.type) {
                    case this.TYPE_AUTH:
                        return this.getParam(obj, 'challenge') != '';
                    case this.TYPE_SIGN:
                        return this.getParam(obj, 'nonce') != '' && this.getParam(obj, 'texttobesigned') != '';
                    default:
                        throw new Exception('called on wrong type of <object>: '+obj.type);
                }
            },
            
            'performAction': function(obj, actionName) {
                if (typeof actionName != 'string') return this.setLastError(obj, 8008);
                actionName = actionName.toLowerCase();
                
                if (obj.type == this.TYPE_AUTH) {
                    if (actionName != 'authenticate') return this.setLastError(obj, 8008);
                } else if (obj.type == this.TYPE_SIGN) {
                    if (actionName != 'sign') return this.setLastError(obj, 8008);
                } else {
                    throw new Exception('called on wrong type of <object>: '+obj.type);
                }
                
                if (!this.hasRequiredParams(obj)) {
                    return this.setLastError(obj, 8016);
                }
                
                var resp = this.sendPluginRequest(actionName, obj.FriBID_NPAPI_Compat_Params);
                if (typeof resp == 'int') {
                    return resp; // Failed to send request
                } else {
                    this.updateParams(obj, resp.params);
                    return this.setLastError(obj, resp.errorCode);
                }
            },
            
            'getLastError': function(obj) {
                var errnum = obj.FriBID_NPAPI_Compat_LastError;
                return errnum ? errnum : 0;
            },
            
            'setLastError': function(obj, errnum) {
                obj.FriBID_NPAPI_Compat_LastError = errnum;
                return errnum;
            },
            
            'updateParams': function(obj, params) {
                for (var name in params) {
                    if (params.hasOwnProperty(name)) {
                        obj.FriBID_NPAPI_Compat_Params[name] = params[name];
                    }
                }
            },
            
            'sendPluginRequest': function(requestType, params) {
                var data = 'requestType='+requestType;
                for (var name in params) {
                    if (params.hasOwnProperty(name)) {
                        // All parameter values should be Base64 encoded or integers
                        data += '&' + name + '=' + escape(params[name]);
                    }
                }
                
                var req = new XMLHttpRequest();
                req.timeout = 0;
                req.open('POST', this.REQ_URL, false);
                req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                try {
                    req.send(data);
                } catch (e) {
                    alert("Failed to connect to FriBID npcompatsrv.\n\nAn exception ocurred: "+e);
                    throw e;
                }
                if (req.status == 200) {
                    try {
                        return JSON.parse(req.responseText);
                    } catch (e) {
                        return 3;
                    }
                } else {
                    alert("Failed to connect to FriBID npcompatsrv.\n\nError status: "+req.status);
                    return 2;
                }
            }
        };
        
        // TODO add all properties to these objects
        window.navigator.plugins['Nexus Personal'] = { };
        window.navigator.mimeTypes[FriBID_NPAPI_Compat.TYPE_AUTH] = { 'enabledPlugin': true };
        window.navigator.mimeTypes[FriBID_NPAPI_Compat.TYPE_REGUTIL] = { 'enabledPlugin': true };
        window.navigator.mimeTypes[FriBID_NPAPI_Compat.TYPE_SIGN] = { 'enabledPlugin': true };
        window.navigator.mimeTypes[FriBID_NPAPI_Compat.TYPE_OLDSIGN] = { 'enabledPlugin': true };
        window.navigator.mimeTypes[FriBID_NPAPI_Compat.TYPE_VER] = { 'enabledPlugin': true };
        window.navigator.mimeTypes[FriBID_NPAPI_Compat.TYPE_WEBUTIL] = { 'enabledPlugin': true };

        window.HTMLObjectElement.prototype.GetVersion = function() {
            var fribid = FriBID_NPAPI_Compat;
            if (this.type == fribid.TYPE_VER) {
                var bestbefore = Math.ceil(Date.now()/1000 + 29*24*3600);
                return 'Personal=4.15.0.14&libai_so=4.15.0.14&libP11_so=4.15.0.14&libtokenapi_so=4.15.0.14&libCardSiemens_so=4.15.0.14&libCardSetec_so=4.15.0.14&libCardPrisma_so=4.15.0.14&libBranding_so=4.15.0.14&libplugins_so=4.15.0.14&personal_bin=4.15.0.14&platform=linux&distribution=unknown&os_version=unknown&best_before='+bestbefore+'&';
            } else {
                throw new Exception('xxx');
            }
        };

        window.HTMLObjectElement.prototype.SetParam = function(name, value) {
            var fribid = FriBID_NPAPI_Compat;
            if (this.type == fribid.TYPE_AUTH || this.type == fribid.TYPE_SIGN) {
                if (fribid.hasParam(this, name)) {
                    return fribid.setLastError(this, fribid.setParam(this, name, value));
                } else {
                    return fribid.setLastError(this, 8004);
                }
            } else if (this.type == fribid.TYPE_REGUTIL) {
                if (fribid.hasParam(this, name)) {
                    return fribid.setLastError(this, fribid.setParam(this, name, value));
                } else {
                    return fribid.setLastError(this, 640);
                }
            } else {
                throw new Exception('xxx');
            }
        };

        window.HTMLObjectElement.prototype.GetParam = function(name) {
            var fribid = FriBID_NPAPI_Compat;
            if (this.type == fribid.TYPE_AUTH || this.type == fribid.TYPE_SIGN) {
                if (fribid.hasParam(this, name)) {
                    var val = fribid.getParam(this, name);
                    fribid.setLastError(this, 0);
                    return val;
                } else {
                    return fribid.setLastError(this, 8004);
                }
            } else if (this.type == fribid.TYPE_REGUTIL) {
                if (fribid.hasParam(this, name)) {
                    var val = fribid.getParam(this, name);
                    fribid.setLastError(this, 0);
                    return val;
                } else {
                    return fribid.setLastError(this, 640);
                }
            } else {
                throw new Exception('xxx');
            }
        };
        
        window.HTMLObjectElement.prototype.Reset = function() {
            var fribid = FriBID_NPAPI_Compat;
            if (this.type == fribid.TYPE_AUTH || this.type == fribid.TYPE_SIGN) {
                fribid.setDefaults(this);
                return fribid.setLastError(this, 0);
            } else {
                throw new Exception('xxx');
            }
        };
        
        window.HTMLObjectElement.prototype.GetLastError = function() {
            var fribid = FriBID_NPAPI_Compat;
            if (this.type == fribid.TYPE_AUTH || this.type == fribid.TYPE_SIGN ||
                this.type == fribid.TYPE_REGUTIL || this.type == fribid.TYPE_WEBADMIN) {
                return fribid.getLastError(this);
            } else {
                throw new Exception('xxx');
            }
        };

        window.HTMLObjectElement.prototype.PerformAction = function(actionName) {
            var fribid = FriBID_NPAPI_Compat;
            if (this.type == fribid.TYPE_AUTH || this.type == fribid.TYPE_SIGN) {
                return fribid.performAction(this, actionName);
            } else if (this.type == fribid.TYPE_WEBADMIN) {
                return fribid.setLastError(this, 8008);
            } else {
                throw new Exception('xxx');
            }
        };
    }
}

var s = document.createElement("script");
s.appendChild(document.createTextNode('('+main+')();'));
(document.body || document.head || document.documentElement).appendChild(s);