forked from kjur/jsrsasign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasn1-1.0.js
1546 lines (1451 loc) · 51 KB
/
asn1-1.0.js
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! asn1-1.0.7.js (c) 2013-2015 Kenji Urushima | kjur.github.com/jsrsasign/license
*/
/*
* asn1.js - ASN.1 DER encoder classes
*
* Copyright (c) 2013-2015 Kenji Urushima ([email protected])
*
* This software is licensed under the terms of the MIT License.
* http://kjur.github.com/jsrsasign/license
*
* The above copyright and license notice shall be
* included in all copies or substantial portions of the Software.
*/
/**
* @fileOverview
* @name asn1-1.0.js
* @author Kenji Urushima [email protected]
* @version asn1 1.0.7 (2015-Jun-11)
* @since jsrsasign 2.1
* @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
*/
/**
* kjur's class library name space
* <p>
* This name space provides following name spaces:
* <ul>
* <li>{@link KJUR.asn1} - ASN.1 primitive hexadecimal encoder</li>
* <li>{@link KJUR.asn1.x509} - ASN.1 structure for X.509 certificate and CRL</li>
* <li>{@link KJUR.crypto} - Java Cryptographic Extension(JCE) style MessageDigest/Signature
* class and utilities</li>
* </ul>
* </p>
* NOTE: Please ignore method summary and document of this namespace. This caused by a bug of jsdoc2.
* @name KJUR
* @namespace kjur's class library name space
*/
if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
/**
* kjur's ASN.1 class library name space
* <p>
* This is ITU-T X.690 ASN.1 DER encoder class library and
* class structure and methods is very similar to
* org.bouncycastle.asn1 package of
* well known BouncyCaslte Cryptography Library.
*
* <h4>PROVIDING ASN.1 PRIMITIVES</h4>
* Here are ASN.1 DER primitive classes.
* <ul>
* <li>0x01 {@link KJUR.asn1.DERBoolean}</li>
* <li>0x02 {@link KJUR.asn1.DERInteger}</li>
* <li>0x03 {@link KJUR.asn1.DERBitString}</li>
* <li>0x04 {@link KJUR.asn1.DEROctetString}</li>
* <li>0x05 {@link KJUR.asn1.DERNull}</li>
* <li>0x06 {@link KJUR.asn1.DERObjectIdentifier}</li>
* <li>0x0a {@link KJUR.asn1.DEREnumerated}</li>
* <li>0x0c {@link KJUR.asn1.DERUTF8String}</li>
* <li>0x12 {@link KJUR.asn1.DERNumericString}</li>
* <li>0x13 {@link KJUR.asn1.DERPrintableString}</li>
* <li>0x14 {@link KJUR.asn1.DERTeletexString}</li>
* <li>0x16 {@link KJUR.asn1.DERIA5String}</li>
* <li>0x17 {@link KJUR.asn1.DERUTCTime}</li>
* <li>0x18 {@link KJUR.asn1.DERGeneralizedTime}</li>
* <li>0x30 {@link KJUR.asn1.DERSequence}</li>
* <li>0x31 {@link KJUR.asn1.DERSet}</li>
* </ul>
*
* <h4>OTHER ASN.1 CLASSES</h4>
* <ul>
* <li>{@link KJUR.asn1.ASN1Object}</li>
* <li>{@link KJUR.asn1.DERAbstractString}</li>
* <li>{@link KJUR.asn1.DERAbstractTime}</li>
* <li>{@link KJUR.asn1.DERAbstractStructured}</li>
* <li>{@link KJUR.asn1.DERTaggedObject}</li>
* </ul>
* </p>
* NOTE: Please ignore method summary and document of this namespace.
* This caused by a bug of jsdoc2.
* @name KJUR.asn1
* @namespace
*/
if (typeof KJUR.asn1 == "undefined" || !KJUR.asn1) KJUR.asn1 = {};
/**
* ASN1 utilities class
* @name KJUR.asn1.ASN1Util
* @class ASN1 utilities class
* @since asn1 1.0.2
*/
KJUR.asn1.ASN1Util = new function() {
this.integerToByteHex = function(i) {
var h = i.toString(16);
if ((h.length % 2) == 1) h = '0' + h;
return h;
};
this.bigIntToMinTwosComplementsHex = function(bigIntegerValue) {
var h = bigIntegerValue.toString(16);
if (h.substr(0, 1) != '-') {
if (h.length % 2 == 1) {
h = '0' + h;
} else {
if (! h.match(/^[0-7]/)) {
h = '00' + h;
}
}
} else {
var hPos = h.substr(1);
var xorLen = hPos.length;
if (xorLen % 2 == 1) {
xorLen += 1;
} else {
if (! h.match(/^[0-7]/)) {
xorLen += 2;
}
}
var hMask = '';
for (var i = 0; i < xorLen; i++) {
hMask += 'f';
}
var biMask = new BigInteger(hMask, 16);
var biNeg = biMask.xor(bigIntegerValue).add(BigInteger.ONE);
h = biNeg.toString(16).replace(/^-/, '');
}
return h;
};
/**
* get PEM string from hexadecimal data and header string
* @name getPEMStringFromHex
* @memberOf KJUR.asn1.ASN1Util
* @function
* @param {String} dataHex hexadecimal string of PEM body
* @param {String} pemHeader PEM header string (ex. 'RSA PRIVATE KEY')
* @return {String} PEM formatted string of input data
* @description
* @example
* var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex('616161', 'RSA PRIVATE KEY');
* // value of pem will be:
* -----BEGIN PRIVATE KEY-----
* YWFh
* -----END PRIVATE KEY-----
*/
this.getPEMStringFromHex = function(dataHex, pemHeader) {
var ns1 = KJUR.asn1;
var dataWA = CryptoJS.enc.Hex.parse(dataHex);
var dataB64 = CryptoJS.enc.Base64.stringify(dataWA);
var pemBody = dataB64.replace(/(.{64})/g, "$1\r\n");
pemBody = pemBody.replace(/\r\n$/, '');
return "-----BEGIN " + pemHeader + "-----\r\n" +
pemBody +
"\r\n-----END " + pemHeader + "-----\r\n";
};
/**
* generate ASN1Object specifed by JSON parameters
* @name newObject
* @memberOf KJUR.asn1.ASN1Util
* @function
* @param {Array} param JSON parameter to generate ASN1Object
* @return {KJUR.asn1.ASN1Object} generated object
* @since asn1 1.0.3
* @description
* generate any ASN1Object specified by JSON param
* including ASN.1 primitive or structured.
* Generally 'param' can be described as follows:
* <blockquote>
* {TYPE-OF-ASNOBJ: ASN1OBJ-PARAMETER}
* </blockquote>
* 'TYPE-OF-ASN1OBJ' can be one of following symbols:
* <ul>
* <li>'bool' - DERBoolean</li>
* <li>'int' - DERInteger</li>
* <li>'bitstr' - DERBitString</li>
* <li>'octstr' - DEROctetString</li>
* <li>'null' - DERNull</li>
* <li>'oid' - DERObjectIdentifier</li>
* <li>'enum' - DEREnumerated</li>
* <li>'utf8str' - DERUTF8String</li>
* <li>'numstr' - DERNumericString</li>
* <li>'prnstr' - DERPrintableString</li>
* <li>'telstr' - DERTeletexString</li>
* <li>'ia5str' - DERIA5String</li>
* <li>'utctime' - DERUTCTime</li>
* <li>'gentime' - DERGeneralizedTime</li>
* <li>'seq' - DERSequence</li>
* <li>'set' - DERSet</li>
* <li>'tag' - DERTaggedObject</li>
* </ul>
* @example
* newObject({'prnstr': 'aaa'});
* newObject({'seq': [{'int': 3}, {'prnstr': 'aaa'}]})
* // ASN.1 Tagged Object
* newObject({'tag': {'tag': 'a1',
* 'explicit': true,
* 'obj': {'seq': [{'int': 3}, {'prnstr': 'aaa'}]}}});
* // more simple representation of ASN.1 Tagged Object
* newObject({'tag': ['a1',
* true,
* {'seq': [
* {'int': 3},
* {'prnstr': 'aaa'}]}
* ]});
*/
this.newObject = function(param) {
var ns1 = KJUR.asn1;
var keys = Object.keys(param);
if (keys.length != 1)
throw "key of param shall be only one.";
var key = keys[0];
if (":bool:int:bitstr:octstr:null:oid:enum:utf8str:numstr:prnstr:telstr:ia5str:utctime:gentime:seq:set:tag:".indexOf(":" + key + ":") == -1)
throw "undefined key: " + key;
if (key == "bool") return new ns1.DERBoolean(param[key]);
if (key == "int") return new ns1.DERInteger(param[key]);
if (key == "bitstr") return new ns1.DERBitString(param[key]);
if (key == "octstr") return new ns1.DEROctetString(param[key]);
if (key == "null") return new ns1.DERNull(param[key]);
if (key == "oid") return new ns1.DERObjectIdentifier(param[key]);
if (key == "enum") return new ns1.DEREnumerated(param[key]);
if (key == "utf8str") return new ns1.DERUTF8String(param[key]);
if (key == "numstr") return new ns1.DERNumericString(param[key]);
if (key == "prnstr") return new ns1.DERPrintableString(param[key]);
if (key == "telstr") return new ns1.DERTeletexString(param[key]);
if (key == "ia5str") return new ns1.DERIA5String(param[key]);
if (key == "utctime") return new ns1.DERUTCTime(param[key]);
if (key == "gentime") return new ns1.DERGeneralizedTime(param[key]);
if (key == "seq") {
var paramList = param[key];
var a = [];
for (var i = 0; i < paramList.length; i++) {
var asn1Obj = ns1.ASN1Util.newObject(paramList[i]);
a.push(asn1Obj);
}
return new ns1.DERSequence({'array': a});
}
if (key == "set") {
var paramList = param[key];
var a = [];
for (var i = 0; i < paramList.length; i++) {
var asn1Obj = ns1.ASN1Util.newObject(paramList[i]);
a.push(asn1Obj);
}
return new ns1.DERSet({'array': a});
}
if (key == "tag") {
var tagParam = param[key];
if (Object.prototype.toString.call(tagParam) === '[object Array]' &&
tagParam.length == 3) {
var obj = ns1.ASN1Util.newObject(tagParam[2]);
return new ns1.DERTaggedObject({tag: tagParam[0], explicit: tagParam[1], obj: obj});
} else {
var newParam = {};
if (tagParam.explicit !== undefined)
newParam.explicit = tagParam.explicit;
if (tagParam.tag !== undefined)
newParam.tag = tagParam.tag;
if (tagParam.obj === undefined)
throw "obj shall be specified for 'tag'.";
newParam.obj = ns1.ASN1Util.newObject(tagParam.obj);
return new ns1.DERTaggedObject(newParam);
}
}
};
/**
* get encoded hexadecimal string of ASN1Object specifed by JSON parameters
* @name jsonToASN1HEX
* @memberOf KJUR.asn1.ASN1Util
* @function
* @param {Array} param JSON parameter to generate ASN1Object
* @return hexadecimal string of ASN1Object
* @since asn1 1.0.4
* @description
* As for ASN.1 object representation of JSON object,
* please see {@link newObject}.
* @example
* jsonToASN1HEX({'prnstr': 'aaa'});
*/
this.jsonToASN1HEX = function(param) {
var asn1Obj = this.newObject(param);
return asn1Obj.getEncodedHex();
};
};
/**
* get dot noted oid number string from hexadecimal value of OID
* @name oidHexToInt
* @memberOf KJUR.asn1.ASN1Util
* @function
* @param {String} hex hexadecimal value of object identifier
* @return {String} dot noted string of object identifier
* @since jsrsasign 4.8.3 asn1 1.0.7
* @description
* This static method converts from hexadecimal string representation of
* ASN.1 value of object identifier to oid number string.
* @example
* KJUR.asn1.ASN1Util.oidHexToInt('550406') → "2.5.4.6"
*/
KJUR.asn1.ASN1Util.oidHexToInt = function(hex) {
var s = "";
var i01 = parseInt(hex.substr(0, 2), 16);
var i0 = Math.floor(i01 / 40);
var i1 = i01 % 40;
var s = i0 + "." + i1;
var binbuf = "";
for (var i = 2; i < hex.length; i += 2) {
var value = parseInt(hex.substr(i, 2), 16);
var bin = ("00000000" + value.toString(2)).slice(- 8);
binbuf = binbuf + bin.substr(1, 7);
if (bin.substr(0, 1) == "0") {
var bi = new BigInteger(binbuf, 2);
s = s + "." + bi.toString(10);
binbuf = "";
}
};
return s;
};
/**
* get hexadecimal value of object identifier from dot noted oid value
* @name oidIntToHex
* @memberOf KJUR.asn1.ASN1Util
* @function
* @param {String} oidString dot noted string of object identifier
* @return {String} hexadecimal value of object identifier
* @since jsrsasign 4.8.3 asn1 1.0.7
* @description
* This static method converts from object identifier value string.
* to hexadecimal string representation of it.
* @example
* KJUR.asn1.ASN1Util.oidIntToHex("2.5.4.6") → "550406"
*/
KJUR.asn1.ASN1Util.oidIntToHex = function(oidString) {
var itox = function(i) {
var h = i.toString(16);
if (h.length == 1) h = '0' + h;
return h;
};
var roidtox = function(roid) {
var h = '';
var bi = new BigInteger(roid, 10);
var b = bi.toString(2);
var padLen = 7 - b.length % 7;
if (padLen == 7) padLen = 0;
var bPad = '';
for (var i = 0; i < padLen; i++) bPad += '0';
b = bPad + b;
for (var i = 0; i < b.length - 1; i += 7) {
var b8 = b.substr(i, 7);
if (i != b.length - 7) b8 = '1' + b8;
h += itox(parseInt(b8, 2));
}
return h;
};
if (! oidString.match(/^[0-9.]+$/)) {
throw "malformed oid string: " + oidString;
}
var h = '';
var a = oidString.split('.');
var i0 = parseInt(a[0]) * 40 + parseInt(a[1]);
h += itox(i0);
a.splice(0, 2);
for (var i = 0; i < a.length; i++) {
h += roidtox(a[i]);
}
return h;
};
// ********************************************************************
// Abstract ASN.1 Classes
// ********************************************************************
// ********************************************************************
/**
* base class for ASN.1 DER encoder object
* @name KJUR.asn1.ASN1Object
* @class base class for ASN.1 DER encoder object
* @property {Boolean} isModified flag whether internal data was changed
* @property {String} hTLV hexadecimal string of ASN.1 TLV
* @property {String} hT hexadecimal string of ASN.1 TLV tag(T)
* @property {String} hL hexadecimal string of ASN.1 TLV length(L)
* @property {String} hV hexadecimal string of ASN.1 TLV value(V)
* @description
*/
KJUR.asn1.ASN1Object = function() {
var isModified = true;
var hTLV = null;
var hT = '00';
var hL = '00';
var hV = '';
/**
* get hexadecimal ASN.1 TLV length(L) bytes from TLV value(V)
* @name getLengthHexFromValue
* @memberOf KJUR.asn1.ASN1Object
* @function
* @return {String} hexadecimal string of ASN.1 TLV length(L)
*/
this.getLengthHexFromValue = function() {
if (typeof this.hV == "undefined" || this.hV == null) {
throw "this.hV is null or undefined.";
}
if (this.hV.length % 2 == 1) {
throw "value hex must be even length: n=" + hV.length + ",v=" + this.hV;
}
var n = this.hV.length / 2;
var hN = n.toString(16);
if (hN.length % 2 == 1) {
hN = "0" + hN;
}
if (n < 128) {
return hN;
} else {
var hNlen = hN.length / 2;
if (hNlen > 15) {
throw "ASN.1 length too long to represent by 8x: n = " + n.toString(16);
}
var head = 128 + hNlen;
return head.toString(16) + hN;
}
};
/**
* get hexadecimal string of ASN.1 TLV bytes
* @name getEncodedHex
* @memberOf KJUR.asn1.ASN1Object
* @function
* @return {String} hexadecimal string of ASN.1 TLV
*/
this.getEncodedHex = function() {
if (this.hTLV == null || this.isModified) {
this.hV = this.getFreshValueHex();
this.hL = this.getLengthHexFromValue();
this.hTLV = this.hT + this.hL + this.hV;
this.isModified = false;
//alert("first time: " + this.hTLV);
}
return this.hTLV;
};
/**
* get hexadecimal string of ASN.1 TLV value(V) bytes
* @name getValueHex
* @memberOf KJUR.asn1.ASN1Object
* @function
* @return {String} hexadecimal string of ASN.1 TLV value(V) bytes
*/
this.getValueHex = function() {
this.getEncodedHex();
return this.hV;
}
this.getFreshValueHex = function() {
return '';
};
};
// == BEGIN DERAbstractString ================================================
/**
* base class for ASN.1 DER string classes
* @name KJUR.asn1.DERAbstractString
* @class base class for ASN.1 DER string classes
* @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
* @property {String} s internal string of value
* @extends KJUR.asn1.ASN1Object
* @description
* <br/>
* As for argument 'params' for constructor, you can specify one of
* following properties:
* <ul>
* <li>str - specify initial ASN.1 value(V) by a string</li>
* <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
* </ul>
* NOTE: 'params' can be omitted.
*/
KJUR.asn1.DERAbstractString = function(params) {
KJUR.asn1.DERAbstractString.superclass.constructor.call(this);
var s = null;
var hV = null;
/**
* get string value of this string object
* @name getString
* @memberOf KJUR.asn1.DERAbstractString
* @function
* @return {String} string value of this string object
*/
this.getString = function() {
return this.s;
};
/**
* set value by a string
* @name setString
* @memberOf KJUR.asn1.DERAbstractString
* @function
* @param {String} newS value by a string to set
*/
this.setString = function(newS) {
this.hTLV = null;
this.isModified = true;
this.s = newS;
this.hV = stohex(this.s);
};
/**
* set value by a hexadecimal string
* @name setStringHex
* @memberOf KJUR.asn1.DERAbstractString
* @function
* @param {String} newHexString value by a hexadecimal string to set
*/
this.setStringHex = function(newHexString) {
this.hTLV = null;
this.isModified = true;
this.s = null;
this.hV = newHexString;
};
this.getFreshValueHex = function() {
return this.hV;
};
if (typeof params != "undefined") {
if (typeof params == "string") {
this.setString(params);
} else if (typeof params['str'] != "undefined") {
this.setString(params['str']);
} else if (typeof params['hex'] != "undefined") {
this.setStringHex(params['hex']);
}
}
};
YAHOO.lang.extend(KJUR.asn1.DERAbstractString, KJUR.asn1.ASN1Object);
// == END DERAbstractString ================================================
// == BEGIN DERAbstractTime ==================================================
/**
* base class for ASN.1 DER Generalized/UTCTime class
* @name KJUR.asn1.DERAbstractTime
* @class base class for ASN.1 DER Generalized/UTCTime class
* @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'})
* @extends KJUR.asn1.ASN1Object
* @description
* @see KJUR.asn1.ASN1Object - superclass
*/
KJUR.asn1.DERAbstractTime = function(params) {
KJUR.asn1.DERAbstractTime.superclass.constructor.call(this);
var s = null;
var date = null;
// --- PRIVATE METHODS --------------------
this.localDateToUTC = function(d) {
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var utcDate = new Date(utc);
return utcDate;
};
/*
* format date string by Data object
* @name formatDate
* @memberOf KJUR.asn1.AbstractTime;
* @param {Date} dateObject
* @param {string} type 'utc' or 'gen'
* @param {boolean} withMillis flag for with millisections or not
* @description
* 'withMillis' flag is supported from asn1 1.0.6.
*/
this.formatDate = function(dateObject, type, withMillis) {
var pad = this.zeroPadding;
var d = this.localDateToUTC(dateObject);
var year = String(d.getFullYear());
if (type == 'utc') year = year.substr(2, 2);
var month = pad(String(d.getMonth() + 1), 2);
var day = pad(String(d.getDate()), 2);
var hour = pad(String(d.getHours()), 2);
var min = pad(String(d.getMinutes()), 2);
var sec = pad(String(d.getSeconds()), 2);
var s = year + month + day + hour + min + sec;
if (withMillis === true) {
var millis = d.getMilliseconds();
if (millis != 0) {
var sMillis = pad(String(millis), 3);
sMillis = sMillis.replace(/[0]+$/, "");
s = s + "." + sMillis;
}
}
return s + "Z";
};
this.zeroPadding = function(s, len) {
if (s.length >= len) return s;
return new Array(len - s.length + 1).join('0') + s;
};
// --- PUBLIC METHODS --------------------
/**
* get string value of this string object
* @name getString
* @memberOf KJUR.asn1.DERAbstractTime
* @function
* @return {String} string value of this time object
*/
this.getString = function() {
return this.s;
};
/**
* set value by a string
* @name setString
* @memberOf KJUR.asn1.DERAbstractTime
* @function
* @param {String} newS value by a string to set such like "130430235959Z"
*/
this.setString = function(newS) {
this.hTLV = null;
this.isModified = true;
this.s = newS;
this.hV = stohex(newS);
};
/**
* set value by a Date object
* @name setByDateValue
* @memberOf KJUR.asn1.DERAbstractTime
* @function
* @param {Integer} year year of date (ex. 2013)
* @param {Integer} month month of date between 1 and 12 (ex. 12)
* @param {Integer} day day of month
* @param {Integer} hour hours of date
* @param {Integer} min minutes of date
* @param {Integer} sec seconds of date
*/
this.setByDateValue = function(year, month, day, hour, min, sec) {
var dateObject = new Date(Date.UTC(year, month - 1, day, hour, min, sec, 0));
this.setByDate(dateObject);
};
this.getFreshValueHex = function() {
return this.hV;
};
};
YAHOO.lang.extend(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object);
// == END DERAbstractTime ==================================================
// == BEGIN DERAbstractStructured ============================================
/**
* base class for ASN.1 DER structured class
* @name KJUR.asn1.DERAbstractStructured
* @class base class for ASN.1 DER structured class
* @property {Array} asn1Array internal array of ASN1Object
* @extends KJUR.asn1.ASN1Object
* @description
* @see KJUR.asn1.ASN1Object - superclass
*/
KJUR.asn1.DERAbstractStructured = function(params) {
KJUR.asn1.DERAbstractString.superclass.constructor.call(this);
var asn1Array = null;
/**
* set value by array of ASN1Object
* @name setByASN1ObjectArray
* @memberOf KJUR.asn1.DERAbstractStructured
* @function
* @param {array} asn1ObjectArray array of ASN1Object to set
*/
this.setByASN1ObjectArray = function(asn1ObjectArray) {
this.hTLV = null;
this.isModified = true;
this.asn1Array = asn1ObjectArray;
};
/**
* append an ASN1Object to internal array
* @name appendASN1Object
* @memberOf KJUR.asn1.DERAbstractStructured
* @function
* @param {ASN1Object} asn1Object to add
*/
this.appendASN1Object = function(asn1Object) {
this.hTLV = null;
this.isModified = true;
this.asn1Array.push(asn1Object);
};
this.asn1Array = new Array();
if (typeof params != "undefined") {
if (typeof params['array'] != "undefined") {
this.asn1Array = params['array'];
}
}
};
YAHOO.lang.extend(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object);
// ********************************************************************
// ASN.1 Object Classes
// ********************************************************************
// ********************************************************************
/**
* class for ASN.1 DER Boolean
* @name KJUR.asn1.DERBoolean
* @class class for ASN.1 DER Boolean
* @extends KJUR.asn1.ASN1Object
* @description
* @see KJUR.asn1.ASN1Object - superclass
*/
KJUR.asn1.DERBoolean = function() {
KJUR.asn1.DERBoolean.superclass.constructor.call(this);
this.hT = "01";
this.hTLV = "0101ff";
};
YAHOO.lang.extend(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object);
// ********************************************************************
/**
* class for ASN.1 DER Integer
* @name KJUR.asn1.DERInteger
* @class class for ASN.1 DER Integer
* @extends KJUR.asn1.ASN1Object
* @description
* <br/>
* As for argument 'params' for constructor, you can specify one of
* following properties:
* <ul>
* <li>int - specify initial ASN.1 value(V) by integer value</li>
* <li>bigint - specify initial ASN.1 value(V) by BigInteger object</li>
* <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
* </ul>
* NOTE: 'params' can be omitted.
*/
KJUR.asn1.DERInteger = function(params) {
KJUR.asn1.DERInteger.superclass.constructor.call(this);
this.hT = "02";
/**
* set value by Tom Wu's BigInteger object
* @name setByBigInteger
* @memberOf KJUR.asn1.DERInteger
* @function
* @param {BigInteger} bigIntegerValue to set
*/
this.setByBigInteger = function(bigIntegerValue) {
this.hTLV = null;
this.isModified = true;
this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
};
/**
* set value by integer value
* @name setByInteger
* @memberOf KJUR.asn1.DERInteger
* @function
* @param {Integer} integer value to set
*/
this.setByInteger = function(intValue) {
var bi = new BigInteger(String(intValue), 10);
this.setByBigInteger(bi);
};
/**
* set value by integer value
* @name setValueHex
* @memberOf KJUR.asn1.DERInteger
* @function
* @param {String} hexadecimal string of integer value
* @description
* <br/>
* NOTE: Value shall be represented by minimum octet length of
* two's complement representation.
* @example
* new KJUR.asn1.DERInteger(123);
* new KJUR.asn1.DERInteger({'int': 123});
* new KJUR.asn1.DERInteger({'hex': '1fad'});
*/
this.setValueHex = function(newHexString) {
this.hV = newHexString;
};
this.getFreshValueHex = function() {
return this.hV;
};
if (typeof params != "undefined") {
if (typeof params['bigint'] != "undefined") {
this.setByBigInteger(params['bigint']);
} else if (typeof params['int'] != "undefined") {
this.setByInteger(params['int']);
} else if (typeof params == "number") {
this.setByInteger(params);
} else if (typeof params['hex'] != "undefined") {
this.setValueHex(params['hex']);
}
}
};
YAHOO.lang.extend(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object);
// ********************************************************************
/**
* class for ASN.1 DER encoded BitString primitive
* @name KJUR.asn1.DERBitString
* @class class for ASN.1 DER encoded BitString primitive
* @extends KJUR.asn1.ASN1Object
* @description
* <br/>
* As for argument 'params' for constructor, you can specify one of
* following properties:
* <ul>
* <li>bin - specify binary string (ex. '10111')</li>
* <li>array - specify array of boolean (ex. [true,false,true,true])</li>
* <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li>
* </ul>
* NOTE: 'params' can be omitted.
*/
KJUR.asn1.DERBitString = function(params) {
KJUR.asn1.DERBitString.superclass.constructor.call(this);
this.hT = "03";
/**
* set ASN.1 value(V) by a hexadecimal string including unused bits
* @name setHexValueIncludingUnusedBits
* @memberOf KJUR.asn1.DERBitString
* @function
* @param {String} newHexStringIncludingUnusedBits
*/
this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) {
this.hTLV = null;
this.isModified = true;
this.hV = newHexStringIncludingUnusedBits;
};
/**
* set ASN.1 value(V) by unused bit and hexadecimal string of value
* @name setUnusedBitsAndHexValue
* @memberOf KJUR.asn1.DERBitString
* @function
* @param {Integer} unusedBits
* @param {String} hValue
*/
this.setUnusedBitsAndHexValue = function(unusedBits, hValue) {
if (unusedBits < 0 || 7 < unusedBits) {
throw "unused bits shall be from 0 to 7: u = " + unusedBits;
}
var hUnusedBits = "0" + unusedBits;
this.hTLV = null;
this.isModified = true;
this.hV = hUnusedBits + hValue;
};
/**
* set ASN.1 DER BitString by binary string
* @name setByBinaryString
* @memberOf KJUR.asn1.DERBitString
* @function
* @param {String} binaryString binary value string (i.e. '10111')
* @description
* Its unused bits will be calculated automatically by length of
* 'binaryValue'. <br/>
* NOTE: Trailing zeros '0' will be ignored.
*/
this.setByBinaryString = function(binaryString) {
binaryString = binaryString.replace(/0+$/, '');
var unusedBits = 8 - binaryString.length % 8;
if (unusedBits == 8) unusedBits = 0;
for (var i = 0; i <= unusedBits; i++) {
binaryString += '0';
}
var h = '';
for (var i = 0; i < binaryString.length - 1; i += 8) {
var b = binaryString.substr(i, 8);
var x = parseInt(b, 2).toString(16);
if (x.length == 1) x = '0' + x;
h += x;
}
this.hTLV = null;
this.isModified = true;
this.hV = '0' + unusedBits + h;
};
/**
* set ASN.1 TLV value(V) by an array of boolean
* @name setByBooleanArray
* @memberOf KJUR.asn1.DERBitString
* @function
* @param {array} booleanArray array of boolean (ex. [true, false, true])
* @description
* NOTE: Trailing falses will be ignored.
*/
this.setByBooleanArray = function(booleanArray) {
var s = '';
for (var i = 0; i < booleanArray.length; i++) {
if (booleanArray[i] == true) {
s += '1';
} else {
s += '0';
}
}
this.setByBinaryString(s);
};
/**
* generate an array of false with specified length
* @name newFalseArray
* @memberOf KJUR.asn1.DERBitString
* @function
* @param {Integer} nLength length of array to generate
* @return {array} array of boolean faluse
* @description
* This static method may be useful to initialize boolean array.
*/
this.newFalseArray = function(nLength) {
var a = new Array(nLength);
for (var i = 0; i < nLength; i++) {
a[i] = false;
}
return a;
};
this.getFreshValueHex = function() {
return this.hV;
};
if (typeof params != "undefined") {
if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) {
this.setHexValueIncludingUnusedBits(params);
} else if (typeof params['hex'] != "undefined") {
this.setHexValueIncludingUnusedBits(params['hex']);
} else if (typeof params['bin'] != "undefined") {
this.setByBinaryString(params['bin']);
} else if (typeof params['array'] != "undefined") {
this.setByBooleanArray(params['array']);
}
}
};
YAHOO.lang.extend(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object);
// ********************************************************************
/**
* class for ASN.1 DER OctetString
* @name KJUR.asn1.DEROctetString
* @class class for ASN.1 DER OctetString
* @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
* @extends KJUR.asn1.DERAbstractString
* @description
* @see KJUR.asn1.DERAbstractString - superclass
*/
KJUR.asn1.DEROctetString = function(params) {
KJUR.asn1.DEROctetString.superclass.constructor.call(this, params);
this.hT = "04";
};
YAHOO.lang.extend(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString);
// ********************************************************************
/**
* class for ASN.1 DER Null
* @name KJUR.asn1.DERNull
* @class class for ASN.1 DER Null
* @extends KJUR.asn1.ASN1Object
* @description
* @see KJUR.asn1.ASN1Object - superclass
*/
KJUR.asn1.DERNull = function() {
KJUR.asn1.DERNull.superclass.constructor.call(this);
this.hT = "05";
this.hTLV = "0500";
};
YAHOO.lang.extend(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object);
// ********************************************************************
/**
* class for ASN.1 DER ObjectIdentifier
* @name KJUR.asn1.DERObjectIdentifier
* @class class for ASN.1 DER ObjectIdentifier
* @param {Array} params associative array of parameters (ex. {'oid': '2.5.4.5'})
* @extends KJUR.asn1.ASN1Object
* @description
* <br/>
* As for argument 'params' for constructor, you can specify one of
* following properties:
* <ul>
* <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li>
* <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
* </ul>
* NOTE: 'params' can be omitted.
*/
KJUR.asn1.DERObjectIdentifier = function(params) {
var itox = function(i) {
var h = i.toString(16);