From 69f711b41eba44a7837b6f135a57d8b9ceebea77 Mon Sep 17 00:00:00 2001 From: YuryStrozhevsky Date: Sun, 28 Jun 2015 15:40:42 +0300 Subject: [PATCH] Fixed bug with zero-length OCTETSTRING --- org/pkijs/asn1.js | 52 ++++++++++++++++++++------------------------- org/pkijs/common.js | 15 +++++++++++++ package.json | 2 +- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/org/pkijs/asn1.js b/org/pkijs/asn1.js index fe6adf1..7641a56 100644 --- a/org/pkijs/asn1.js +++ b/org/pkijs/asn1.js @@ -344,20 +344,6 @@ function(in_window) return result; } //************************************************************************************** - function pad_number(input_number, full_length) - { - var str = input_number.toString(10); - var dif = full_length - str.length; - - var padding = new Array(dif); - for(var i = 0; i < dif; i++) - padding[i] = '0'; - - var padding_string = padding.join(''); - - return padding_string.concat(str); - } - //************************************************************************************** // #endregion //************************************************************************************** // #region Declaration of base block class @@ -2119,14 +2105,22 @@ function(in_window) /// Offset in ASN.1 BER encoded array where decoding should be started /// Maximum length of array of bytes which can be using in this function + this.value_block.is_constructed = this.id_block.is_constructed; + this.value_block.is_indefinite_form = this.len_block.is_indefinite_form; + // #region Ability to encode empty OCTET STRING if(input_length == 0) + { + if(this.id_block.error.length == 0) + this.block_length += this.id_block.block_length; + + if(this.len_block.error.length == 0) + this.block_length += this.len_block.block_length; + return input_offset; + } // #endregion - this.value_block.is_constructed = this.id_block.is_constructed; - this.value_block.is_indefinite_form = this.len_block.is_indefinite_form; - return in_window.org.pkijs.asn1.ASN1_block.prototype.fromBER.call(this, input_buffer, input_offset, input_length); } //************************************************************************************** @@ -4029,12 +4023,12 @@ function(in_window) { var output_array = new Array(7); - output_array[0] = pad_number(((this.year < 2000) ? (this.year - 1900) : (this.year - 2000)), 2); - output_array[1] = pad_number(this.month, 2); - output_array[2] = pad_number(this.day, 2); - output_array[3] = pad_number(this.hour, 2); - output_array[4] = pad_number(this.minute, 2); - output_array[5] = pad_number(this.second, 2); + output_array[0] = in_window.org.pkijs.padNumber(((this.year < 2000) ? (this.year - 1900) : (this.year - 2000)), 2); + output_array[1] = in_window.org.pkijs.padNumber(this.month, 2); + output_array[2] = in_window.org.pkijs.padNumber(this.day, 2); + output_array[3] = in_window.org.pkijs.padNumber(this.hour, 2); + output_array[4] = in_window.org.pkijs.padNumber(this.minute, 2); + output_array[5] = in_window.org.pkijs.padNumber(this.second, 2); output_array[6] = "Z"; return output_array.join(''); @@ -4199,12 +4193,12 @@ function(in_window) { var output_array = new Array(7); - output_array[0] = pad_number(this.year, 4); - output_array[1] = pad_number(this.month, 2); - output_array[2] = pad_number(this.day, 2); - output_array[3] = pad_number(this.hour, 2); - output_array[4] = pad_number(this.minute, 2); - output_array[5] = pad_number(this.second, 2); + output_array[0] = in_window.org.pkijs.padNumber(this.year, 4); + output_array[1] = in_window.org.pkijs.padNumber(this.month, 2); + output_array[2] = in_window.org.pkijs.padNumber(this.day, 2); + output_array[3] = in_window.org.pkijs.padNumber(this.hour, 2); + output_array[4] = in_window.org.pkijs.padNumber(this.minute, 2); + output_array[5] = in_window.org.pkijs.padNumber(this.second, 2); output_array[6] = "Z"; return output_array.join(''); diff --git a/org/pkijs/common.js b/org/pkijs/common.js index 9898356..31a6f8a 100644 --- a/org/pkijs/common.js +++ b/org/pkijs/common.js @@ -113,6 +113,21 @@ function(in_window) return new Date(current_date.getTime() + (current_date.getTimezoneOffset() * 60000)); } //************************************************************************************** + in_window.org.pkijs.padNumber = + function(input_number, full_length) + { + var str = input_number.toString(10); + var dif = full_length - str.length; + + var padding = new Array(dif); + for(var i = 0; i < dif; i++) + padding[i] = '0'; + + var padding_string = padding.join(''); + + return padding_string.concat(str); + } + //************************************************************************************** in_window.org.pkijs.getValue = function(args, item, default_value) { diff --git a/package.json b/package.json index 3f76ca3..cdbd69b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ ], "name": "asn1js", "description": "ASN1js is a pure JavaScript library implementing this standard. ASN.1 is the basis of all X.509 related data structures and numerous other protocols used on the web", - "version": "1.2.1", + "version": "1.2.2", "repository": { "type": "git", "url": "git://github.com/GlobalSign/ASN1.js.git"