/* * Copyright (c) 2014, GMO GlobalSign * Copyright (c) 2015, Peculiar Ventures * All rights reserved. * * Author 2014-2015, Yury Strozhevsky <www.strozhevsky.com>. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. *
*/
( function(in_window)
{ //************************************************************************************** // #region Declaration of global variables //************************************************************************************** // #region "org" namespace if(typeof in_window.org === "undefined")
in_window.org = {}; else
{ if(typeof in_window.org !== "object") thrownew Error("Name org already exists and it's not an object");
} // #endregion
// #region "org.pkijs" namespace if(typeof in_window.org.pkijs === "undefined")
in_window.org.pkijs = {}; else
{ if(typeof in_window.org.pkijs !== "object") thrownew Error("Name org.pkijs already exists and it's not an object" + " but " + (typeof in_window.org.pkijs));
} // #endregion
if(typeof window != "undefined")
{ if("crypto" in window)
{ var engineName = "webcrypto"; var cryptoObject = window.crypto; var subtleObject = null;
// Apple Safari support if("webkitSubtle" in window.crypto)
subtleObject = window.crypto.webkitSubtle;
if("subtle" in window.crypto)
subtleObject = window.crypto.subtle;
local.engine = {
name: engineName,
crypto: cryptoObject,
subtle: subtleObject
};
}
} //**************************************************************************************
in_window.org.pkijs.setEngine = function(name, crypto, subtle)
{ /// <summary>Setting the global "crypto engine" parameters</summary> /// <param name="name" type="String">Auxiliary name for "crypto engine"</param> /// <param name="crypto" type="Object">Object handling all root cryptographic requests (in fact currently it must handle only "getRandomValues")</param> /// <param name="subtle" type="Object">Object handling all main cryptographic requests</param>
return names;
}; //**************************************************************************************
in_window.org.pkijs.inheriteObjectFields = function(from)
{ for(var i in from.prototype)
{ if(typeof from.prototype[i] === "function") continue;
this[i] = from.prototype[i];
}
}; //**************************************************************************************
in_window.org.pkijs.getUTCDate = function(date)
{ /// <summary>Making UTC date from local date</summary> /// <param name="date" type="Date">Date to convert from</param>
var current_date = date; returnnew 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';
return ret_buf;
}; //**************************************************************************************
in_window.org.pkijs.copyBuffer = function(input_buffer)
{ var result = new ArrayBuffer(input_buffer.byteLength);
var resultView = new Uint8Array(result); var inputView = new Uint8Array(input_buffer);
for(var i = 0; i < inputView.length; i++)
resultView[i] = inputView[i];
return result;
}; //**************************************************************************************
in_window.org.pkijs.getCrypto = function()
{ var crypto_temp;
return crypto_temp;
}; //**************************************************************************************
in_window.org.pkijs.stringPrep = function(input_string)
{ /// <summary>String preparation function. In a future here will be realization of algorithm from RFC4518.</summary> /// <param name="input_string" type="String">JavaScript string. As soon as for each ASN.1 string type we have a specific transformation function here we will work with pure JavaScript string</param> /// <returns type="String">Formated string</returns>
var result = input_string.replace(/^\s+|\s+$/g, ""); // Trim input string
result = result.replace(/\s+/g, " "); // Change all sequence of SPACE down to SPACE char
result = result.toLowerCase();
return result;
}; //**************************************************************************************
in_window.org.pkijs.bufferToHexCodes = function(input_buffer, input_offset, input_lenght)
{ var result = "";
var int_buffer = new Uint8Array(input_buffer, input_offset, input_lenght);
for(var i = 0; i < int_buffer.length; i++)
{ var str = int_buffer[i].toString(16).toUpperCase();
result = result + ((str.length === 1) ? "0" : "") + str;
}
return result;
}; //**************************************************************************************
in_window.org.pkijs.bufferFromHexCodes = function(hexString)
{ /// <summary>Create an ArrayBuffer from string having hexdecimal codes</summary> /// <param name="hexString" type="String">String to create ArrayBuffer from</param>
// #region Initial variables var stringLength = hexString.length;
var resultBuffer = new ArrayBuffer(stringLength >> 1); var resultView = new Uint8Array(resultBuffer);
return resultBuffer;
}; //**************************************************************************************
in_window.org.pkijs.getRandomValues = function(view)
{ /// <param name="view" type="Uint8Array">New array which gives a length for random value</param>
if(local.engine.crypto !== null) return local.engine.crypto.getRandomValues(view); else thrownew Error("No support for Web Cryptography API");
}; //**************************************************************************************
in_window.org.pkijs.getAlgorithmParameters = function(algorithmName, operation)
{ /// <param name="algorithmName" type="String">Algorithm name to get common parameters for</param> /// <param name="operation" type="String">Kind of operation: "sign", "encrypt", "generatekey", "importkey", "exportkey", "verify"</param>
// #region Create ASN.1 structure of CMS ECDSA signature var r_integer = new in_window.org.pkijs.asn1.INTEGER();
r_integer.value_block.is_hex_only = true;
r_integer.value_block.value_hex = in_window.org.pkijs.copyBuffer(r_corrected_buffer);
var s_integer = new in_window.org.pkijs.asn1.INTEGER();
s_integer.value_block.is_hex_only = true;
s_integer.value_block.value_hex = in_window.org.pkijs.copyBuffer(s_corrected_buffer);
var asn1 = new in_window.org.pkijs.asn1.SEQUENCE({
value: [
r_integer,
s_integer
]
}); // #endregion
return asn1.toBER(false);
}; //**************************************************************************************
in_window.org.pkijs.createECDSASignatureFromCMS = function(cmsSignature)
{ /// <summary>Create a single ArrayBuffer from CMS ECDSA signature</summary> /// <param name="cmsSignature" type="in_window.org.pkijs.asn1.SEQUENCE">ASN.1 SEQUENCE contains CMS ECDSA signature</param>
// #region Initial variables var length = 0;
var r_start = 0; var s_start = 0;
var r_length = cmsSignature.value_block.value[0].value_block.value_hex.byteLength; var s_length = cmsSignature.value_block.value[1].value_block.value_hex.byteLength; // #endregion
// #region Get length of final "ArrayBuffer" var r_view = new Uint8Array(cmsSignature.value_block.value[0].value_block.value_hex); if((r_view[0] === 0x00) && (r_view[1] & 0x80))
{
length = r_length - 1;
r_start = 1;
} else
length = r_length;
switch(algorithm.name.toUpperCase())
{ case"AES-CBC": switch(algorithm.length)
{ case 128:
result = "2.16.840.1.101.3.4.1.2"; break; case 192:
result = "2.16.840.1.101.3.4.1.22"; break; case 256:
result = "2.16.840.1.101.3.4.1.42"; break; default:
} break; case"AES-GCM": switch(algorithm.length)
{ case 128:
result = "2.16.840.1.101.3.4.1.6"; break; case 192:
result = "2.16.840.1.101.3.4.1.26"; break; case 256:
result = "2.16.840.1.101.3.4.1.46"; break; default:
} break; default:
}
return result;
}; //**************************************************************************************
in_window.org.pkijs.getAlgorithmByEncryptionOID = function(oid)
{ /// <summary>Get encryption algorithm name by OID</summary> /// <param name="oid" type="String">OID of encryption algorithm</param>
var result = "";
switch(oid)
{ case"2.16.840.1.101.3.4.1.2": case"2.16.840.1.101.3.4.1.22": case"2.16.840.1.101.3.4.1.42":
result = "AES-CBC"; break; case"2.16.840.1.101.3.4.1.6": case"2.16.840.1.101.3.4.1.26": case"2.16.840.1.101.3.4.1.46":
result = "AES-GCM"; break; default:
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.