1 /* asn1cades-2.0.1.js (c) 2014-2020 Kenji Urushima | kjur.github.com/jsrsasign/license
  2  */
  3 /*
  4  * asn1cades.js - ASN.1 DER encoder classes for RFC 5126 CAdES long term signature
  5  *
  6  * Copyright (c) 2014-2021 Kenji Urushima (kenji.urushima@gmail.com)
  7  *
  8  * This software is licensed under the terms of the MIT License.
  9  * https://kjur.github.io/jsrsasign/license
 10  *
 11  * The above copyright and license notice shall be 
 12  * included in all copies or substantial portions of the Software.
 13  */
 14 
 15 /**
 16  * @fileOverview
 17  * @name asn1cades-1.0.js
 18  * @author Kenji Urushima kenji.urushima@gmail.com
 19  * @version jsrsasign 10.0.5 asn1cades 2.0.1 (2021-Jan-17)
 20  * @since jsrsasign 4.7.0
 21  * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a>
 22  */
 23 
 24 /** 
 25  * kjur's class library name space
 26  * // already documented in asn1-1.0.js
 27  * @name KJUR
 28  * @namespace kjur's class library name space
 29  */
 30 if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
 31 
 32 /**
 33  * kjur's ASN.1 class library name space
 34  * // already documented in asn1-1.0.js
 35  * @name KJUR.asn1
 36  * @namespace
 37  */
 38 if (typeof KJUR.asn1 == "undefined" || !KJUR.asn1) KJUR.asn1 = {};
 39 
 40 /**
 41  * kjur's ASN.1 class for RFC 5126 CAdES long term signature
 42  * <p>
 43  * This name space provides 
 44  * <a href="https://tools.ietf.org/html/rfc5126">RFC 5126
 45  * CAdES(CMS Advanced Electronic Signature)</a> generator.
 46  *
 47  * <h4>SUPPORTED FORMATS</h4>
 48  * Following CAdES formats is supported by this library.
 49  * <ul>
 50  * <li>CAdES-BES - CAdES Basic Electronic Signature</li>
 51  * <li>CAdES-EPES - CAdES Explicit Policy-based Electronic Signature</li>
 52  * <li>CAdES-T - Electronic Signature with Time</li>
 53  * </ul>
 54  * </p>
 55  *
 56  * <h4>PROVIDED ATTRIBUTE CLASSES</h4>
 57  * <ul>
 58  * <li>{@link KJUR.asn1.cades.SignaturePolicyIdentifier} - for CAdES-EPES
 59  *   <ul>
 60  *   <li>{@link KJUR.asn1.cades.SignaturePolicyId}</li>
 61  *   </ul>
 62  * </li>
 63  * <li>{@link KJUR.asn1.cades.SignatureTimeStamp} - for CAdES-T</li>
 64  * <li>{@link KJUR.asn1.cades.CompleteCertificateRefs} - for CAdES-C(for future use)
 65  *   <ul>
 66  *   <li>{@link KJUR.asn1.cades.OtherCertID}</li>
 67  *   <li>{@link KJUR.asn1.cades.OtherHash}</li>
 68  *   <li>{@link KJUR.asn1.cades.OtherHashAlgAndValue}</li>
 69  *   <li>{@link KJUR.asn1.cades.OtherHashValue}</li>
 70  *   </ul>
 71  * </li>
 72  * </ul>
 73  * NOTE: Currntly CAdES-C is not supported since parser can't
 74  * handle unsigned attribute.
 75  * 
 76  * <h4>OTHER CLASSES</h4>
 77  * <ul>
 78  * <li>{@link KJUR.asn1.cades.CAdESUtil} - utilities for CAdES</li>
 79  * </ul>
 80  *
 81  * <h4>GENERATE CAdES-BES</h4>
 82  * To generate CAdES-BES, {@link KJUR.asn.cades} namespace 
 83  * classes are not required and already {@link KJUR.asn.cms} namespace 
 84  * provides attributes for CAdES-BES.
 85  * Create {@link KJUR.asn1.cms.SignedData} with following
 86  * mandatory attribute in CAdES-BES:
 87  * <ul>
 88  * <li>{@link KJUR.asn1.cms.ContentType}</li>
 89  * <li>{@link KJUR.asn1.cms.MessageDigest}</li>
 90  * <li>{@link KJUR.asn1.cms.SigningCertificate} or </li>
 91  * <li>{@link KJUR.asn1.cms.SigningCertificateV2}</li>
 92  * </ul>
 93  * CMSUtil.newSignedData method is very useful to generate CAdES-BES.
 94  * <pre>
 95  * sd = KJUR.asn1.cms.CMSUtil.newSignedData({
 96  *   content: {str: "aaa"},
 97  *   certs: [certPEM],
 98  *   signerInfos: [{
 99  *     hashAlg: 'sha256',
100  *     sAttr: {SigningCertificateV2: {array: [certPEM]}},
101  *     signerCert: certPEM,
102  *     sigAlg: 'SHA256withRSA',
103  *     signerPrvKey: pkcs8PrvKeyPEM
104  *   }]
105  * });
106  * signedDataHex = sd.getContentInfoEncodedHex();
107  * </pre>
108  * NOTE: ContentType and MessageDigest signed attributes
109  * are automatically added by default.
110  *
111  * <h4>GENERATE CAdES-BES with multiple signers</h4>
112  * If you need signature by multiple signers, you can 
113  * specify one or more items in 'signerInfos' property as below.
114  * <pre>
115  * sd = KJUR.asn1.cms.CMSUtil.newSignedData({
116  *   content: {str: "aaa"},
117  *   certs: [certPEM1, certPEM2],
118  *   signerInfos: [{
119  *     hashAlg: 'sha256',
120  *     sAttr: {SigningCertificateV2: {array: [certPEM1]}},
121  *     signerCert: certPEM1,
122  *     sigAlg: 'SHA256withRSA',
123  *     signerPrvKey: pkcs8PrvKeyPEM1
124  *   },{
125  *     hashAlg: 'sha1',
126  *     sAttr: {SigningCertificateV2: {array: [certPEM2]}},
127  *     signerCert: certPEM2,
128  *     sigAlg: 'SHA1withRSA',
129  *     signerPrvKey: pkcs8PrvKeyPEM2
130  *   }]
131  * });
132  * signedDataHex = sd.getContentInfoEncodedHex();
133  * </pre>
134  *
135  * <h4>GENERATE CAdES-EPES</h4>
136  * When you need a CAdES-EPES signature,
137  * you just need to add 'SignaturePolicyIdentifier'
138  * attribute as below.
139  * <pre>
140  * sd = KJUR.asn1.cms.CMSUtil.newSignedData({
141  *   content: {str: "aaa"},
142  *   certs: [certPEM],
143  *   signerInfos: [{
144  *     hashAlg: 'sha256',
145  *     sAttr: {
146  *       SigningCertificateV2: {array: [certPEM]},
147  *       SignaturePolicyIdentifier: {
148  *         oid: '1.2.3.4.5',
149  *         hash: {alg: 'sha1', hash: 'b1b2b3b4b...'}
150  *       },
151  *     },
152  *     signerCert: certPEM,
153  *     sigAlg: 'SHA256withRSA',
154  *     signerPrvKey: pkcs8PrvKeyPEM
155  *   }]
156  * });
157  * signedDataHex = sd.getContentInfoEncodedHex();
158  * </pre>
159  *
160  * <h4>GENERATE CAdES-T</h4>
161  * After a signed CAdES-BES or CAdES-EPES signature have been generated,
162  * you can generate CAdES-T by adding SigningTimeStamp unsigned attribute.
163  * <pre>
164  * beshex = "30..."; // hex of CAdES-BES or EPES data 
165  * info = KJUR.asn1.cades.CAdESUtil.parseSignedDataForAddingUnsigned(beshex);
166  * // You can refer a hexadecimal string of signature value 
167  * // in the first signerInfo in the CAdES-BES/EPES with a variable:
168  * // 'info.si[0].sigval'. You need to get RFC 3161 TimeStampToken
169  * // from a trusted time stamp authority. Otherwise you can also 
170  * // get it by 'KJUR.asn1.tsp' module. We suppose that we could 
171  * // get proper time stamp.
172  * tsthex0 = "30..."; // hex of TimeStampToken for signerInfo[0] sigval
173  * si0 = info.obj.signerInfoList[0];
174  * si0.addUnsigned(new KJUR.asn1.cades.SignatureTimeStamp({tst: tsthex0});
175  * esthex = info.obj.getContentInfoEncodedHex(); // CAdES-T
176  * </pre>
177  * </p>
178  *
179  * <h4>SAMPLE CODES</h4>
180  * <ul>
181  * <li><a href="../../tool_cades.html">demo program for CAdES-BES/EPES/T generation</a></li>
182  * <li><a href="../../test/qunit-do-asn1cades.html">Unit test code for KJUR.asn1.cades package</a></li>
183  * <li><a href="../../test/qunit-do-asn1tsp.html">Unit test code for KJUR.asn1.tsp package (See SimpleTSAAdaptor test)</a></li>
184  * <li><a href="../../test/qunit-do-asn1cms.html">Unit test code for KJUR.asn1.cms package (See newSignedData test)</a></li>
185  * </ul>
186  * 
187  * @name KJUR.asn1.cades
188  * @namespace
189  */
190 if (typeof KJUR.asn1.cades == "undefined" || !KJUR.asn1.cades) KJUR.asn1.cades = {};
191 
192 /**
193  * class for RFC 5126 CAdES SignaturePolicyIdentifier attribute
194  * @name KJUR.asn1.cades.SignaturePolicyIdentifier
195  * @class class for RFC 5126 CAdES SignaturePolicyIdentifier attribute
196  * @param {Array} params associative array of parameters
197  * @extends KJUR.asn1.cms.Attribute
198  * @since jsrsasign 4.7.0 asn1cades 1.0.0
199  * @see KJUR.asn1.cms.AttributeList
200  * @see KJUR.asn1.cms.CMSParser#setSignaturePolicyIdentifier
201  * @see KJUR.asn1.cades.SignaturePolicyId
202  * @see KJUR.asn1.cades.OtherHashAlgAndValue
203  *
204  * @description
205  * This class provides ASN.1 encoder for
206  * <a href="https://tools.ietf.org/html/rfc5126#section-5.8.1">
207  * SignaturePolicyIdentifier defined in RFC 5126 CAdES section 5.8.1</a>.
208  * <pre>
209  * SignaturePolicyIdentifier ::= CHOICE {
210  *    signaturePolicyId       SignaturePolicyId,
211  *    signaturePolicyImplied  SignaturePolicyImplied } -- not used
212  *
213  * SignaturePolicyImplied ::= NULL
214  * SignaturePolicyId ::= SEQUENCE {
215  *    sigPolicyId           SigPolicyId,
216  *    sigPolicyHash         SigPolicyHash,
217  *    sigPolicyQualifiers   SEQUENCE SIZE (1..MAX) OF
218  *                             SigPolicyQualifierInfo OPTIONAL }
219  * SigPolicyId ::= OBJECT IDENTIFIER
220  * SigPolicyHash ::= OtherHashAlgAndValue
221  * </pre>
222  *
223  * @example
224  * new KJUR.asn1.cades.SignaturePolicyIdentifier({
225  *   attr: "signaturePolicyIdentifier",
226  *   oid: '1.2.3.4.5',
227  *   alg: 'sha1',
228  *   hash: 'a1a2a3a4...'
229  * })
230  */
231 KJUR.asn1.cades.SignaturePolicyIdentifier = function(params) {
232     var _KJUR = KJUR,
233 	_KJUR_asn1 = _KJUR.asn1,
234 	_KJUR_asn1_cades = _KJUR_asn1.cades,
235 	_SignaturePolicyId = _KJUR_asn1_cades.SignaturePolicyId;
236 	
237     _KJUR_asn1_cades.SignaturePolicyIdentifier.superclass.constructor.call(this);
238 
239     this.typeOid = "1.2.840.113549.1.9.16.2.15";
240 
241     this.params = null;
242 
243     this.getValueArray = function() {
244 	return [new _SignaturePolicyId(this.params)];
245     };
246 
247     this.setByParam = function(params) {
248 	this.params = params;
249     };
250 
251     if (params != undefined) this.setByParam(params);
252 };
253 extendClass(KJUR.asn1.cades.SignaturePolicyIdentifier,
254                   KJUR.asn1.cms.Attribute);
255 
256 /**
257  * RFC 5126 CAdES SignaturePolicyId ASN.1 structure class<br/>
258  * @name KJUR.asn1.cades.SignaturePolicyId
259  * @class RFC 5126 CAdES SignaturePolicyId ASN.1 structure class
260  * @param {Array} params JSON object of parameters
261  * @extends KJUR.asn1.ASN1Object
262  * @since jsrsasign 10.0.0 asn1cades 2.0.0
263  * @see KJUR.asn1.cades.SignaturePolicyIdentifier
264  * @see KJUR.asn1.cades.OtherHashAlgAndValue
265  *
266  * @description
267  * This class provides ASN.1 encoder for
268  * <a href="https://tools.ietf.org/html/rfc5126#section-5.8.1">
269  * SignaturePolicyId defined in RFC 5126 CAdES section 5.8.1</a>.
270  * <pre>
271  * SignaturePolicyId ::= SEQUENCE {
272  *    sigPolicyId           SigPolicyId,
273  *    sigPolicyHash         SigPolicyHash,
274  *    sigPolicyQualifiers   SEQUENCE SIZE (1..MAX) OF
275  *                             SigPolicyQualifierInfo OPTIONAL }
276  * SigPolicyId ::= OBJECT IDENTIFIER
277  * SigPolicyHash ::= OtherHashAlgAndValue
278  * OtherHashAlgAndValue ::= SEQUENCE {
279  *    hashAlgorithm   AlgorithmIdentifier,
280  *    hashValue       OtherHashValue }
281  * OtherHashValue ::= OCTET STRING
282  * </pre>
283  * Following properties can be apply to constructor arguments 
284  * adding to {@link KJUR.asn1.cades.OtherHashAlgAndValue} constructor:
285  * <ul>
286  * <li>{String} oid - signature policy OID string or name (ex. 1.2.3.4)</li>
287  * </ul>
288  * 
289  * @example
290  * new KJUR.asn1.cades.SignaturePolicyId({
291  *   oid: "1.2.3.4.5",
292  *   alg: "sha256",
293  *   hash: "1234abcd..."
294  * });
295  */
296 KJUR.asn1.cades.SignaturePolicyId = function(params) {
297     var _KJUR = KJUR,
298 	_KJUR_asn1 = _KJUR.asn1,
299 	_DERSequence = _KJUR_asn1.DERSequence,
300 	_DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier,
301 	_KJUR_asn1_x509 = _KJUR_asn1.x509,
302 	_AlgorithmIdentifier = _KJUR_asn1_x509.AlgorithmIdentifier,
303 	_KJUR_asn1_cades = _KJUR_asn1.cades,
304 	_SignaturePolicyId = _KJUR_asn1_cades.SignaturePolicyId,
305 	_OtherHashAlgAndValue = _KJUR_asn1_cades.OtherHashAlgAndValue;
306 
307     _SignaturePolicyId.superclass.constructor.call(this);
308 
309     this.params = null;
310 
311     this.getEncodedHex = function() {
312 	var params = this.params;
313 
314 	var a = [];
315 	a.push(new _DERObjectIdentifier(params.oid));
316 	a.push(new _OtherHashAlgAndValue(params));
317 	var seq = new _DERSequence({array: a});
318 	return seq.getEncodedHex();
319     };
320 
321     this.setByParam = function(params) {
322 	this.params = params;
323     };
324 
325     if (params != undefined) this.setByParam(params);
326 };
327 extendClass(KJUR.asn1.cades.SignaturePolicyId, KJUR.asn1.ASN1Object);
328 
329 /**
330  * class for OtherHashAlgAndValue ASN.1 object<br/>
331  * @name KJUR.asn1.cades.OtherHashAlgAndValue
332  * @class class for OtherHashAlgAndValue ASN.1 object
333  * @param {Array} params associative array of parameters
334  * @extends KJUR.asn1.ASN1Object
335  * @since jsrsasign 4.7.0 asn1cades 1.0.0
336  *
337  * @description
338  * This class provides ASN.1 encoder for
339  * <a href="https://tools.ietf.org/html/rfc5126#section-5.8.1">
340  * OtherHashAlgAndValue defined in RFC 5126 CAdES section 5.8.1</a>.
341  * <pre>
342  * OtherHashAlgAndValue ::= SEQUENCE {
343  *    hashAlgorithm   AlgorithmIdentifier,
344  *    hashValue       OtherHashValue }
345  * OtherHashValue ::= OCTET STRING
346  * </pre>
347  * Following properties can be apply to constructor arguments:
348  * <ul>
349  * <li>{String} alg - hash algorithm name for "hashAlgorithm" field</li>
350  * <li>{String} hash - hexadecimal string for "hashValue" field</li>
351  * </ul>
352  * 
353  * @example
354  * // specify by hash
355  * new KJUR.asn1.cades.OtherHashAlgAndValue({
356  *   alg: "sha256",
357  *   hash: "12abcd..."
358  * })
359  *
360  * // or specify by cert PEM or hex
361  * new KJUR.asn1.cades.OtherHashAlgAndValue({
362  *   alg: "sha256",
363  *   cert: "-----BEGIN..."
364  * })
365  * new KJUR.asn1.cades.OtherHashAlgAndValue({
366  *   alg: "sha256",
367  *   cert: "3082..."
368  * })
369  */
370 KJUR.asn1.cades.OtherHashAlgAndValue = function(params) {
371     var _Error = Error,
372 	_KJUR = KJUR,
373 	_KJUR_asn1 = _KJUR.asn1,
374 	_DERSequence = _KJUR_asn1.DERSequence,
375 	_DEROctetString = _KJUR_asn1.DEROctetString,
376 	_KJUR_asn1_x509 = _KJUR_asn1.x509,
377 	_AlgorithmIdentifier = _KJUR_asn1_x509.AlgorithmIdentifier,
378 	_KJUR_asn1_cades = _KJUR_asn1.cades,
379 	_OtherHashAlgAndValue = _KJUR_asn1_cades.OtherHashAlgAndValue;
380 
381     _OtherHashAlgAndValue.superclass.constructor.call(this);
382 
383     this.params = null;
384 
385     this.getEncodedHex = function() {
386 	var params = this.params;
387 
388 	if (params.alg == undefined)
389 	    throw new _Error("property 'alg' not specified");
390 
391 	if (params.hash == undefined && params.cert == undefined)
392 	    throw new _Error("property 'hash' nor 'cert' not specified");
393 
394 	var hHash = null;
395 	if (params.hash != undefined) {
396 	    hHash = params.hash;
397 	} else if (params.cert != undefined) {
398 	    if (typeof params.cert != "string")
399 		throw new _Error("cert not string");
400 
401 	    var hCert = params.cert;
402 	    if (params.cert.indexOf("-----BEGIN") != -1) {
403 		hCert = pemtohex(params.cert);
404 	    }
405 	    hHash = KJUR.crypto.Util.hashHex(hCert, params.alg);
406 	}
407 
408 	var a = [];
409 	a.push(new _AlgorithmIdentifier({name: params.alg}));
410 	a.push(new _DEROctetString({hex: hHash}));
411 	var seq = new _DERSequence({array: a});
412 	return seq.getEncodedHex();
413     };
414 
415     if (params != undefined) this.setByParam(params);
416 };
417 extendClass(KJUR.asn1.cades.OtherHashAlgAndValue, KJUR.asn1.ASN1Object);
418 
419 /**
420  * class for OtherHashValue ASN.1 object<br/>
421  * @name KJUR.asn1.cades.OtherHashValue
422  * @class class for OtherHashValue ASN.1 object
423  * @param {Array} params JSON object of parameters
424  * @extends KJUR.asn1.ASN1Object
425  * @since jsrsasign 10.0.0 asn1cades 2.0.0
426  *
427  * This class provides ASN.1 encoder for
428  * <a href="https://tools.ietf.org/html/rfc5126#section-5.8.1">
429  * OtherHashAlgAndValue defined in RFC 5126 CAdES section 5.8.1</a>.
430  * <pre>
431  * OtherHashValue ::= OCTET STRING
432  * </pre>
433  *
434  * @example
435  * new KJUR.asn1.cades.OtherHashValue({hash: "12ab..."})
436  * new KJUR.asn1.cades.OtherHashValue({cert: "-----BEGIN..."})
437  * new KJUR.asn1.cades.OtherHashValue({cert: "3081..."})
438  */
439 KJUR.asn1.cades.OtherHashValue = function(params) {
440     KJUR.asn1.cades.OtherHashValue.superclass.constructor.call(this);
441 
442     var _Error = Error,
443 	_KJUR = KJUR,
444 	_isHex = _KJUR.lang.String.isHex,
445 	_KJUR_asn1 = _KJUR.asn1,
446 	_DEROctetString = _KJUR_asn1.DEROctetString,
447 	_hashHex = _KJUR.crypto.Util.hashHex;
448     
449     this.params = null;
450 
451     this.getEncodedHex = function() {
452 	var params = this.params;
453 
454 	if (params.hash == undefined && params.cert == undefined) {
455 	    throw new _Error("hash or cert not specified");
456 	}
457 
458 	var hHash = null;
459 	if (params.hash != undefined) {
460 	    hHash = params.hash;
461 	} else if (params.cert != undefined) {
462 	    if (typeof params.cert != "string") {
463 		throw new _Error("cert not string");
464 	    }
465 	    var hCert = params.cert;
466 	    if (params.cert.indexOf("-----BEGIN") != -1) {
467 		hCert = pemtohex(params.cert);
468 	    }
469 	    hHash = KJUR.crypto.Util.hashHex(hCert, "sha1");
470 	}
471 	return (new _DEROctetString({hex: hHash})).getEncodedHex();
472     };
473 
474     if (params != undefined) this.setByParam(params);
475 };
476 extendClass(KJUR.asn1.cades.OtherHashValue, KJUR.asn1.ASN1Object);
477 
478 /**
479  * class for RFC 5126 CAdES SignatureTimeStamp attribute<br/>
480  * @name KJUR.asn1.cades.SignatureTimeStamp
481  * @class class for RFC 5126 CAdES SignatureTimeStamp attribute
482  * @param {Array} params associative array of parameters
483  * @extends KJUR.asn1.cms.Attribute
484  * @since jsrsasign 4.7.0 asn1cades 1.0.0
485  * @description
486  * <pre>
487  * id-aa-signatureTimeStampToken OBJECT IDENTIFIER ::=
488  *    1.2.840.113549.1.9.16.2.14
489  * SignatureTimeStampToken ::= TimeStampToken
490  * </pre>
491  *
492  * @example
493  * // by TimeStampToken hex
494  * new KJUR.asn1.cades.SignatureTimeStamp({
495  *   attr: "timeStampToken",
496  *   tst: "3082..."})
497  *
498  * // by TimeStampToken or ASN1Object
499  * new KJUR.asn1.cades.SignatureTimeStamp({
500  *   attr: "timeStampToken",
501  *   tst: new TimeStampToken(...)})
502  *
503  * // by TimeStampResponse hex
504  * new KJUR.asn1.cades.SignatureTimeStamp({
505  *   attr: "timeStampToken",
506  *   res: "3082..."})
507  *
508  * // by TimeStampToken or ASN1Object
509  * new KJUR.asn1.cades.SignatureTimeStamp({
510  *   attr: "timeStampToken",
511  *   res: new TimeStampResponse(...)})
512  */
513 KJUR.asn1.cades.SignatureTimeStamp = function(params) {
514     var _Error = Error,
515 	_KJUR = KJUR,
516 	_isHex = _KJUR.lang.String.isHex,
517 	_KJUR_asn1 = _KJUR.asn1,
518 	_ASN1Object = _KJUR_asn1.ASN1Object,
519 	_KJUR_asn1_x509 = _KJUR_asn1.x509,
520 	_KJUR_asn1_cades = _KJUR_asn1.cades;
521 
522     _KJUR_asn1_cades.SignatureTimeStamp.superclass.constructor.call(this);
523     this.typeOid = "1.2.840.113549.1.9.16.2.14";
524     this.params = null;
525 
526     this.getValueArray = function() {
527 	var params = this.params;
528 
529 	if (params.tst != undefined) {
530 	    if (_isHex(params.tst)) {
531 		var dTST = new _ASN1Object();
532 		dTST.hTLV = params.tst;
533 		return [dTST];
534 	    } else if (params.tst instanceof _ASN1Object) {
535 		return [params.tst];
536 	    } else {
537 		throw new _Error("params.tst has wrong value");
538 	    }
539 	} else if (params.res != undefined) {
540 	    var hRes = params.res;
541 	    if (hRes instanceof _ASN1Object) {
542 		hRes = hRes.getEncodedHex();
543 	    }
544 	    if (typeof hRes != "string" || (! _isHex(hRes))) {
545 		throw new _Error("params.res has wrong value");
546 	    }
547 	    var hTST = ASN1HEX.getTLVbyList(hRes, 0, [1]);
548 	    var dTST = new _ASN1Object();
549 	    dTST.hTLV = params.tst;
550 	    return [dTST];
551 	}
552     };
553 
554     if (params != null) this.setByParam(params);
555 };
556 extendClass(KJUR.asn1.cades.SignatureTimeStamp,
557                   KJUR.asn1.cms.Attribute);
558 
559 /**
560  * class for RFC 5126 CAdES CompleteCertificateRefs attribute<br/>
561  * @name KJUR.asn1.cades.CompleteCertificateRefs
562  * @class class for RFC 5126 CAdES CompleteCertificateRefs attribute
563  * @param {Array} params associative array of parameters
564  * @extends KJUR.asn1.cms.Attribute
565  * @since jsrsasign 4.7.0 asn1cades 1.0.0
566  *
567  * @description
568  * <pre>
569  * id-aa-ets-certificateRefs OBJECT IDENTIFIER = 
570  *    1.2.840.113549.1.9.16.2.21
571  * CompleteCertificateRefs ::=  SEQUENCE OF OtherCertID
572  * OtherCertID ::= SEQUENCE {
573  *    otherCertHash    OtherHash,
574  *    issuerSerial     IssuerSerial OPTIONAL }
575  * OtherHash ::= CHOICE {
576  *    sha1Hash   OtherHashValue,  -- This contains a SHA-1 hash
577  *    otherHash  OtherHashAlgAndValue}
578  * OtherHashAlgAndValue ::= SEQUENCE {
579  *    hashAlgorithm   AlgorithmIdentifier,
580  *    hashValue       OtherHashValue }
581  * OtherHashValue ::= OCTET STRING
582  * </pre>
583  *
584  * @example
585  * o = new KJUR.asn1.cades.CompleteCertificateRefs({
586  *   array: [certPEM1,certPEM2],
587  *   otherhash: true // OPTION
588  * });
589  */
590 KJUR.asn1.cades.CompleteCertificateRefs = function(params) {
591     var _Error = Error,
592 	_KJUR = KJUR,
593 	_KJUR_asn1 = _KJUR.asn1,
594 	_DERSequence = _KJUR_asn1.DERSequence,
595 	_KJUR_asn1_cades = _KJUR_asn1.cades,
596 	_OtherCertID = _KJUR_asn1_cades.OtherCertID,
597 	_isHex = _KJUR.lang.String.isHex;
598 
599     _KJUR_asn1_cades.CompleteCertificateRefs.superclass.constructor.call(this);
600     this.typeOid = "1.2.840.113549.1.9.16.2.21";
601     
602     this.params = null;
603 
604     this.getValueArray = function() {
605 	var params = this.params;
606 	var a = [];
607 
608 	for (var i = 0; i < params.array.length; i++) {
609 	    var pOtherCertID = params.array[i];
610 
611 	    if (typeof pOtherCertID == "string") {
612 		if (pOtherCertID.indexOf("-----BEGIN") != -1) {
613 		    pOtherCertID = {cert: pOtherCertID};
614 		} else if (_isHex(pOtherCertID)) {
615 		    pOtherCertID = {hash: pOtherCertID};
616 		} else {
617 		    throw new _Error("unsupported value: " + pOtherCertID);
618 		}
619 	    }
620 
621 	    if (params.alg != undefined && pOtherCertID.alg == undefined)
622 		pOtherCertID.alg = params.alg;
623 
624 	    if (params.hasis != undefined && pOtherCertID.hasis == undefined)
625 		pOtherCertID.hasis = params.hasis;
626 
627 	    var dOtherCertID = new _OtherCertID(pOtherCertID);
628 	    a.push(dOtherCertID);
629 	}
630 
631 	var seq = new _DERSequence({array: a});
632 	return [seq];
633     };
634 
635     if (params != undefined) this.setByParam(params);
636 };
637 extendClass(KJUR.asn1.cades.CompleteCertificateRefs,
638                   KJUR.asn1.cms.Attribute);
639 
640 /**
641  * class for OtherCertID ASN.1 object
642  * @name KJUR.asn1.cades.OtherCertID
643  * @class class for OtherCertID ASN.1 object
644  * @param {Array} params associative array of parameters
645  * @extends KJUR.asn1.ASN1Object
646  * @since jsrsasign 4.7.0 asn1cades 1.0.0
647  * @see KJUR.asn1.cms.IssuerSerial
648  * @see KJUR.asn1.cms.ESSCertID
649  * @see KJUR.asn1.cms.ESSCertIDv2
650  *
651  * @description
652  * <pre>
653  * OtherCertID ::= SEQUENCE {
654  *    otherCertHash    OtherHash,
655  *    issuerSerial     IssuerSerial OPTIONAL }
656  * IssuerSerial ::= SEQUENCE {
657  *    issuer GeneralNames,
658  *    serialNumber CertificateSerialNumber }
659  * OtherHash ::= CHOICE {
660  *    sha1Hash   OtherHashValue,  -- This contains a SHA-1 hash
661  *    otherHash  OtherHashAlgAndValue}
662  * OtherHashValue ::= OCTET STRING
663  * OtherHashAlgAndValue ::= SEQUENCE {
664  *    hashAlgorithm   AlgorithmIdentifier,
665  *    hashValue       OtherHashValue }
666  * </pre>
667  *
668  * @example
669  * new KJUR.asn1.cades.OtherCertID(certPEM)
670  * new KJUR.asn1.cades.OtherCertID({cert:certPEM, hasis: false})
671  */
672 KJUR.asn1.cades.OtherCertID = function(params) {
673     var _KJUR = KJUR,
674 	_KJUR_asn1 = _KJUR.asn1,
675 	_DERSequence = _KJUR_asn1.DERSequence,
676 	_KJUR_asn1_cms = _KJUR_asn1.cms,
677 	_IssuerSerial = _KJUR_asn1_cms.IssuerSerial,
678 	_KJUR_asn1_cades = _KJUR_asn1.cades,
679 	_OtherHashValue = _KJUR_asn1_cades.OtherHashValue,
680 	_OtherHashAlgAndValue = _KJUR_asn1_cades.OtherHashAlgAndValue;
681 
682     _KJUR_asn1_cades.OtherCertID.superclass.constructor.call(this);
683 
684     this.params = params;
685 
686     this.getEncodedHex = function() {
687 	var params = this.params;
688 
689 	if (typeof params == "string") {
690 	    if (params.indexOf("-----BEGIN") != -1) {
691 		params = {cert: params};
692 	    } else if (_isHex(params)) {
693 		params = {hash: params};
694 	    }
695 	}
696 
697 	var a = [];
698 
699 	var dOtherHash = null;
700 	if (params.alg != undefined) {
701 	    dOtherHash = new _OtherHashAlgAndValue(params);
702 	} else {
703 	    dOtherHash = new _OtherHashValue(params);
704 	}
705 	a.push(dOtherHash);
706 
707 	if ((params.cert != undefined && params.hasis == true) ||
708 	    (params.issuer != undefined && params.serial != undefined)) {
709 	    var dIssuerSerial = new _IssuerSerial(params);
710 	    a.push(dIssuerSerial);
711 	}
712 	
713 	var seq = new _DERSequence({array: a});
714 	return seq.getEncodedHex();
715     };
716 
717     if (params != undefined) this.setByParam(params);
718 };
719 extendClass(KJUR.asn1.cades.OtherCertID, KJUR.asn1.ASN1Object);
720 
721 /**
722  * class for OtherHash ASN.1 object<br/>
723  * @name KJUR.asn1.cades.OtherHash
724  * @class class for OtherHash ASN.1 object
725  * @param {Array} params associative array of parameters
726  * @extends KJUR.asn1.ASN1Object
727  * @since jsrsasign 4.7.0 asn1cades 1.0.0
728  * @link KJUR.asn1.cades.OtherHashAlgAndValue
729  * @link KJUR.asn1.cades.OtherHashValue
730  *
731  * @description
732  * <pre>
733  * OtherHash ::= CHOICE {
734  *    sha1Hash   OtherHashValue,  -- This contains a SHA-1 hash
735  *    otherHash  OtherHashAlgAndValue}
736  * OtherHashValue ::= OCTET STRING
737  * </pre>
738  *
739  * @example
740  * // OtherHashAlgAndValue with SHA256 by PEM or Hex Cert
741  * o = new KJUR.asn1.cades.OtherHash({alg: 'sha256', cert: certPEMorHex});
742  * // OtherHashAlgAndValue with SHA256 by hash value
743  * o = new KJUR.asn1.cades.OtherHash({alg: 'sha256', hash: '1234'});
744  * // OtherHashValue(sha1) by PEM or Hex Cert
745  * o = new KJUR.asn1.cades.OtherHash({cert: certPEM});
746  * // OtherHashValue(sha1) by PEM or Hex Cert
747  * o = new KJUR.asn1.cades.OtherHash(certPEMStr);
748  * // OtherHashValue(sha1) by hash value
749  * o = new KJUR.asn1.cades.OtherHash("1234");
750  */
751 KJUR.asn1.cades.OtherHash = function(params) {
752     var _Error = Error,
753 	_KJUR = KJUR,
754 	_KJUR_asn1 = _KJUR.asn1,
755 	_KJUR_asn1_cms = _KJUR_asn1.cms,
756 	_KJUR_asn1_cades = _KJUR_asn1.cades,
757 	_OtherHashAlgAndValue = _KJUR_asn1_cades.OtherHashAlgAndValue,
758 	_OtherHashValue = _KJUR_asn1_cades.OtherHashValue,
759 	_hashHex = _KJUR.crypto.Util.hashHex,
760 	_isHex = _KJUR.lang.String.isHex;
761 
762     _KJUR_asn1_cades.OtherHash.superclass.constructor.call(this);
763 
764     this.params = null;
765 
766     this.getEncodedHex = function() {
767 	var params = this.params;
768 
769 	if (typeof params == "string") {
770 	    if (params.indexOf("-----BEGIN") != -1) {
771 		params = {cert: params};
772 	    } else if (_isHex(params)) {
773 		params = {hash: params};
774 	    }
775 	}
776 
777 	var dOtherHash = null;
778 	if (params.alg != undefined) {
779 	    dOtherHash = new _OtherHashAlgAndValue(params);
780 	} else {
781 	    dOtherHash = new _OtherHashValue(params);
782 	}
783 	return dOtherHash.getEncodedHex();
784     };
785 
786     if (params != undefined) this.setByParam(params);
787 };
788 extendClass(KJUR.asn1.cades.OtherHash, KJUR.asn1.ASN1Object);
789 
790 
791 // == BEGIN UTILITIES =====================================================
792 
793 /**
794  * CAdES utiliteis class
795  * @name KJUR.asn1.cades.CAdESUtil
796  * @class CAdES utilities class
797  * @since jsrsasign 4.7.0 asn1cades 1.0.0
798  */
799 KJUR.asn1.cades.CAdESUtil = new function() {
800 };
801 
802 /**
803  * parse CMS SignedData to add unsigned attributes
804  * @name parseSignedDataForAddingUnsigned
805  * @memberOf KJUR.asn1.cades.CAdESUtil
806  * @function
807  * @param {String} hex hexadecimal string of ContentInfo of CMS SignedData
808  * @return {Object} associative array of parsed data
809  * @see KJUR.asn1.cms.CMSParser#getCMSSignedData
810  * @see KJUR.asn1.cms.SignedData
811  *
812  * @description
813  * This method will parse a hexadecimal string of 
814  * ContentInfo with CMS SignedData to add a attribute
815  * to unsigned attributes field in a signerInfo field.
816  *
817  * @example
818  * param = KJUR.asn1.cades.CAdESUtil.parseSignedDataForAddingUnsigned(beshex);
819  * →
820  * {
821  *   version: 1,
822  *   hashalgs: ["sha256"],
823  *   econtent: ...,
824  *   sinfos: [{
825  *     version: 1
826  *     id: ...
827  *     hashalg: "sha256",
828  *     sattrs: {array: [...]},
829  *     sigalg: "SHA256withRSA",
830  *     sighex: ...
831  *   }]
832  * }
833  */
834 KJUR.asn1.cades.CAdESUtil.parseSignedDataForAddingUnsigned = function(hex) {
835     var parser = new KJUR.asn1.cms.CMSParser();
836     var param = parser.getCMSSignedData(hex);
837     return param;
838 };
839 
840 /**
841  * parse SignerInfo to add unsigned attributes (DEPRECATED)
842  * @name parseSignerInfoForAddingUnsigned
843  * @memberOf KJUR.asn1.cades.CAdESUtil
844  * @function
845  * @param {String} hex hexadecimal string of SignerInfo
846  * @return {Object} associative array of parsed data
847  * @deprecated since jsrsasign 10.1.5 no more necessary becase parseSignedDataForAddingUnsigned don't call this
848  *
849  * @description
850  * This method will parse a hexadecimal string of 
851  * SignerInfo to add a attribute
852  * to unsigned attributes field in a signerInfo field.
853  * Parsed result will be an associative array which has
854  * following properties:
855  * <ul>
856  * <li>version - hex TLV of version</li>
857  * <li>si - hex TLV of SignerIdentifier</li>
858  * <li>digalg - hex TLV of DigestAlgorithm</li>
859  * <li>sattrs - hex TLV of SignedAttributes</li>
860  * <li>sigalg - hex TLV of SignatureAlgorithm</li>
861  * <li>sig - hex TLV of signature</li>
862  * <li>sigval = hex V of signature</li>
863  * <li>obj - parsed KJUR.asn1.cms.SignerInfo object</li>
864  * </ul>
865  * NOTE: Parsing of unsigned attributes will be provided in the
866  * future version. That's way this version provides support
867  * for CAdES-T and not for CAdES-C.
868  */
869 KJUR.asn1.cades.CAdESUtil.parseSignerInfoForAddingUnsigned = function(hex, iSI, nth) {
870     var _ASN1HEX = ASN1HEX,
871 	_getChildIdx = _ASN1HEX.getChildIdx,
872 	_getTLV = _ASN1HEX.getTLV,
873 	_getV = _ASN1HEX.getV,
874 	_KJUR = KJUR,
875 	_KJUR_asn1 = _KJUR.asn1,
876 	_ASN1Object = _KJUR_asn1.ASN1Object,
877 	_KJUR_asn1_cms = _KJUR_asn1.cms,
878 	_AttributeList = _KJUR_asn1_cms.AttributeList,
879 	_SignerInfo = _KJUR_asn1_cms.SignerInfo;
880 
881     var r = {};
882     var aSIChildIdx = _getChildIdx(hex, iSI);
883     //alert(aSIChildIdx.join("="));
884 
885     if (aSIChildIdx.length != 6)
886         throw "not supported items for SignerInfo (!=6)"; 
887 
888     // 1. SignerInfo.CMSVersion
889     var iVersion = aSIChildIdx.shift();
890     r.version = _getTLV(hex, iVersion);
891 
892     // 2. SignerIdentifier(IssuerAndSerialNumber)
893     var iIdentifier = aSIChildIdx.shift();
894     r.si = _getTLV(hex, iIdentifier);
895 
896     // 3. DigestAlgorithm
897     var iDigestAlg = aSIChildIdx.shift();
898     r.digalg = _getTLV(hex, iDigestAlg);
899 
900     // 4. SignedAttrs
901     var iSignedAttrs = aSIChildIdx.shift();
902     r.sattrs = _getTLV(hex, iSignedAttrs);
903 
904     // 5. SigAlg
905     var iSigAlg = aSIChildIdx.shift();
906     r.sigalg = _getTLV(hex, iSigAlg);
907 
908     // 6. Signature
909     var iSig = aSIChildIdx.shift();
910     r.sig = _getTLV(hex, iSig);
911     r.sigval = _getV(hex, iSig);
912 
913     // 7. obj(SignerInfo)
914     var tmp = null;
915     r.obj = new _SignerInfo();
916 
917     tmp = new _ASN1Object();
918     tmp.hTLV = r.version;
919     r.obj.dCMSVersion = tmp;
920 
921     tmp = new _ASN1Object();
922     tmp.hTLV = r.si;
923     r.obj.dSignerIdentifier = tmp;
924 
925     tmp = new _ASN1Object();
926     tmp.hTLV = r.digalg;
927     r.obj.dDigestAlgorithm = tmp;
928 
929     tmp = new _ASN1Object();
930     tmp.hTLV = r.sattrs;
931     r.obj.dSignedAttrs = tmp;
932 
933     tmp = new _ASN1Object();
934     tmp.hTLV = r.sigalg;
935     r.obj.dSigAlg = tmp;
936 
937     tmp = new _ASN1Object();
938     tmp.hTLV = r.sig;
939     r.obj.dSig = tmp;
940 
941     r.obj.dUnsignedAttrs = new _AttributeList();
942 
943     return r;
944 };
945 
946