Subversion Repositories oidplus

Rev

Rev 1382 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1382 daniel-mar 1
/*
2
 * OIDplus 2.0
3
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
18
var OIDplusObjectTypePluginAid = {
19
 
20
        OID: "1.3.6.1.4.1.37476.2.5.2.4.8.11",
21
 
1400 daniel-mar 22
        generateRandomAID: function(current_aid) {
1382 daniel-mar 23
                var aid_max_len = 32; // 32 nibbles = 16 bytes
24
                var characters  = '0123456789ABCDEF';
25
                var counter     = 0;
1400 daniel-mar 26
 
1382 daniel-mar 27
                if (current_aid.length >= aid_max_len) {
28
                                alertWarning(_L("The AID has reached its maximum size."));
29
                                return;
30
                }
31
 
32
 
33
                do {
34
                        var candidate = current_aid == "" ? "F" : current_aid; // category "F" = Unregistered Proprietary AID
35
                        for ( var i = 0 ; i < aid_max_len - candidate.length ; i++ ) {
36
                                candidate += characters.charAt(Math.floor(Math.random() * characters.length));
37
                        }
38
 
39
                        var isPrefixOfExistingAIDs = false;
40
                        var currentExistingAIDs = [];
41
                        var dummy = null;
42
                        var element = null;
43
                        $("#crudTable tbody a").each(function() {
44
                            var tmp = this.href.split("?goto=aid%3A");
45
                            if (tmp.length > 1) {
46
                                currentExistingAIDs.push(tmp[1].split("&")[0]);
47
                            }
48
                        });
49
                        $.each(currentExistingAIDs, function(dummy, element) {
50
                            if (candidate.startsWith(element)) {
51
                                isPrefixOfExistingAIDs = true;
52
                                return false; // breaks
53
                            }
54
                        });
55
 
56
                        if (counter++ > 1000) {
57
                                alertWarning(_L("Cannot find a free slot for a random AID! Please try again, or try generating a random AID in a subordinate node."));
58
                                return;
59
                        }
60
                } while (isPrefixOfExistingAIDs || ((candidate.endsWith('FF') && (candidate.length==aid_max_len))));
61
 
62
                // Note that 16 byte AIDs ending with 0xFF *were* reserved by ISO in ISO 7816-4:1994,
63
                // but modern versions of ISO 7816-4 and ISO 7816-4 do not mention this case anymore.
64
                // It can be assumed that the usage is safe, but just to be sure, we exclude 16-byte
65
                // AIDs ending with 0xFF, in case there are some software implementations which
66
                // deny such AIDs.
67
 
68
                $("#id").val(candidate);
69
        }
70
 
962 daniel-mar 71
};