From 467a3842173f970a8816573fab61f2b60869cd1a Mon Sep 17 00:00:00 2001
From: Damyan Mitev <damyan.mitev@vereign.com>
Date: Tue, 1 Oct 2019 18:23:41 +0300
Subject: [PATCH] small fixes

---
 javascript/src/utilities/signingUtilities.js | 37 +++++++++++++-------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/javascript/src/utilities/signingUtilities.js b/javascript/src/utilities/signingUtilities.js
index d16b356..92852ac 100644
--- a/javascript/src/utilities/signingUtilities.js
+++ b/javascript/src/utilities/signingUtilities.js
@@ -87,7 +87,11 @@ const encryptionAlgorithm = {
 
 // Convert a hex string to a byte array
 function hexStringToBytes(hex) {
-  for (let bytes = [], c = 0; c < hex.length; c += 2) {
+  let bytes, c;
+  if (hex.length % 2 === 1) {
+    hex = "0" + hex;
+  }
+  for (bytes = [], c = 0; c < hex.length; c += 2) {
     bytes.push(parseInt(hex.substr(c, 2), 16));
   }
   return bytes;
@@ -103,14 +107,11 @@ export class CertificateData {
   constructor(parameters = {}) {
     this.serialNumber = null; //string || ArrayBuffer || Uint8Array
 
-    this.keyPair = null; //{publicKey, privateKey}
+    this.keyPair = null; // write only; {publicKey, privateKey}
 
-    this.signatureAlgorithm = null; //TODO remove from here and from dashboard
-    this.algorithms = {
-      hashAlg: null, //"SHA-256"
-      signAlg: null, //"RSASSA-PKCS1-v1_5"
-      keyLength: 0 //2048
-    };
+    this.signatureAlgorithm = null; //read-only, initialized form pkijs.Certificate object
+
+    this.algorithms = null; // write only; {hashAlg: "SHA-256", signAlg: "RSASSA-PKCS1-v1_5", keyLength: 2048};
 
     this.issuer = null; //same as subject
 
@@ -128,7 +129,7 @@ export class CertificateData {
     this.validity = {
       notBefore: null, //new Date()
       notAfter: null, //new Date()
-      validYears: 1 //int
+      validYears: null //int
     };
 
     this.isCA = false;
@@ -155,7 +156,6 @@ export class CertificateData {
       signatureAlgorithm = `${signatureAlgorithm}`;
     }
     this.signatureAlgorithm = signatureAlgorithm;
-    this.algorithms.signAlg = signatureAlgorithm;
 
     this.issuer = {};
     const issuer = certificate.issuer.typesAndValues;
@@ -190,7 +190,13 @@ export class CertificateData {
     }
 
     if ("keyPair" in parameters) {
-      this.keyPair = parameters.keyPair;
+      this.keyPair = {};
+      if ("publicKey" in parameters.keyPair) {
+        this.keyPair.publicKey = parameters.keyPair.publicKey;
+      }
+      if ("privateKey" in parameters.keyPair) {
+        this.keyPair.privateKey = parameters.keyPair.privateKey;
+      }
     }
 
     if ("signatureAlgorithm" in parameters) {
@@ -198,6 +204,7 @@ export class CertificateData {
     }
 
     if ("algorithms" in parameters) {
+      this.algorithms = {};
       if ("hashAlg" in parameters.algorithms) {
         this.algorithms.hashAlg = parameters.algorithms.hashAlg;
       }
@@ -315,14 +322,18 @@ function fixPkijsRDN() {
 //*********************************************************************************
 function createCertificate(certData, issuerData = null) {
 
-  if (typeof certData === "undefined") {
+  if (typeof certData === "undefined" || certData === null) {
     return Promise.reject("No Certificate data provided");
   }
 
-  if (typeof certData.subject === "undefined") {
+  if (typeof certData.subject === "undefined" || certData.subject === null) {
     return Promise.reject("No Certificate subject data provided");
   }
 
+  if (typeof certData.subject.commonName === "undefined" || certData.subject.commonName === null) {
+    return Promise.reject("No Certificate common name provided");
+  }
+
   //region Get a "crypto" extension
   const crypto = pkijs.getCrypto();
 
-- 
GitLab