title: Verify a signature using a certificate RSA Javascript
body: <p>I am attempting to verify a signature with a certificate. We have to download the required certificate from the CA, verify the certificate, then verify the signature. I have no idea, and I'm hoping someone can shed some light. Here's what I have / know so far.</p>
  
  <p>To sign a message, I used the following code:</p>
  
  <pre><code>function sign(sk, m, certname) {
  var key = new RSAKey();
  key.setPrivate(sk.n, sk.e, sk.d);
  var h = CryptoJS.SHA256(JSON.stringify(m)).toString(CryptoJS.enc.Hex);
  h = new BigInteger(h, 16);
  var sig = key.doPrivate(h).toString(16);
  var obj = { "type": "SIGNED", "msg": m, "certname": certname, "sig": sig };
  return JSON.stringify(obj);
  }
  </code></pre>
  
  <p>To verify a signature, I used the following code:</p>
  
  <pre><code>function verify(pk, signed) {
  var key = new RSAKey();
  var s = JSON.stringify(signed.sig).toString(CryptoJS.enc.Hex);
  s = new BigInteger(s, 16);
  key.setPublic(pk.n, pk.e);
  var v = key.doPublic(s).toString(16);
  var h = CryptoJS.SHA256(JSON.stringify(signed.msg)).toString(CryptoJS.enc.Hex);
  return (v == h);
  }
  </code></pre>
  
  <p>To verify a certificate, I used the following code: (EDIT: this is the new certificate verification function).</p>
  
  <pre><code>function verifyCertificate(signedCert, certname) {
  var key = new RSAKey();
  var s = JSON.stringify(signedCert.sig).toString(CryptoJS.enc.Hex);
  s = new BigInteger(s, 16);
  key.setPublic(CApk.n, CApk.e);
  var v = key.doPublic(s).toString(16);
  var h = CryptoJS.SHA256(JSON.stringify(signedCert.msg)).toString(CryptoJS.enc.Hex);
  return (v == h);
  }
  </code></pre>
  
  <p>And that's that. Can anyone please help. I don't know how to go about this. </p>
  
  <p>EDIT: Okay, I think I have solved my own question (with assistance from the responses). This is the code that returns all positive results:</p>
  
  <pre><code>function verifyWithCert(sig) {
  // 1. Download the required certificate from the CA
  // 2. Verify the certificate
  // 3. Verify the message
  var certKey = new RSAKey();
  var loadedCert = loadCert(sig.certname);
  var certS = JSON.stringify(loadedCert.sig).toString(CryptoJS.enc.Hex);
  certS = new BigInteger(certS, 16);
  certKey.setPublic(CApk.n, CApk.e);
  var certV = certKey.doPublic(certS).toString(16);
  var certH = CryptoJS.SHA256(JSON.stringify(loadedCert.msg)).toString(CryptoJS.enc.Hex);
  var verifyResult;
  if (certV == certH) {
      verifyResult = true;
  }
  var Sigkey = new RSAKey();
  var s = JSON.stringify(sig.sig).toString(CryptoJS.enc.Hex);
  s = new BigInteger(s, 16);
  Sigkey.setPublic(loadedCert.msg.subject.pk.n, loadedCert.msg.subject.pk.e);
  var v = Sigkey.doPublic(s).toString(16);
  var h = CryptoJS.SHA256(JSON.stringify(sig.msg)).toString(CryptoJS.enc.Hex);
  var verifySignature;
  if (v == h) {
      verifySignature = true;
  }
  var result = { "certificateFound": loadedCert ,"certificateVerified": verifyResult ,"signatureVerified": verifySignature };
  return result;
  }
  </code></pre>