Web PKI - Local CAdES Signature

by Lacuna Software

HTML

<script type="text/javascript" src="https://cdn.lacunasoftware.com/libs/web-pki/lacuna-web-pki-2.16.5.min.js"
 integrity="sha256-4CwnsE/1P/zm4eAcIIPTyPts1gFxG16ZCQDTMMxOtIE=" crossorigin="anonymous"></script>

<h2>Web PKI - Local CAdES Signature</h2>
<h3>Signature sample with file passed from JavasSript and signed CAdES (P7S) returned to JavasSript</h3>
<p>Select a certificate:
  <br/>
  <select id="certificateSelect"></select>
</p>
<p>
  <button id="refreshButton" type="button">Refresh Certificates</button>
</p>
<p>
  <button id="signButton" type="button">Sign File</button>
</p>

<p>Signed Document P7S Content:</p>
<textarea id="contentResult" rows="8" cols="100"></textarea>
<p><a id="downloadLink" href='#' download="signed-document.p7s">Download Signed Document P7S</a></p>
<h4>Logs:</h4>
<div id="logPanel"></div>

CSS

@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700);
body {
  font-family: 'Open Sans', sans-serif;
  font-style: normal;
  font-weight: 400;
  font-size: 14px;
}

#logPanel {
  font-family: monospace;
}

JavaScript

// ------------------------------------------------------------------------------------------
// LacunaWebPKI: Local CAdES Signature
// Signature sample with file passed from JavasSript and signed CAdES (P7S) returned to JavasSript

var fileContentSample = null;
var signedCadesContent = null;

var pki = new LacunaWebPKI('AiEAKi5sYWN1bmFzb2Z0d2FyZS5jb20sanNmaWRkbGUubmV0AAADAFBybwgAABg/D+l74AgAAVP0dSRjd5vjlQ8EFzCfN2tOR/S2ENB6x1vRXssle+wxvbEeT3/vmzm7eKDMWr8kgl2W/BfUqYCOC0PnLOVKS8Em0OXtYD+F3WI1+3zQcvI2W34ppGVx1zizoRfyq+Q4KZ9rZPtFYyoBmr1SR5ZyWPqXWNY7p5XWFkxvFmQU11jho5Cd8VX6cv2OuOEw4eBptN5whzV+9XPHEmPav+TJB0hsYbw0TzwLC253i8WhJEQyIVenNGNNHT3foxsFmDTvXqF/HgjOwqgyJJ1hL0wIqsN0pB5weYhjGLwlRt1FqLV1xTScs5nP3xWTK/CPBpq3NDdTobQMsc7PEF46RT1qXbg=');

function start() {
  log('Initializing component');
  pki.init({
    ready: onWebPkiReady,
    defaultFail: onWebPkiError,
    requiredApiVersion: pki.apiVersions.v1_5_2
  });
}

function onWebPkiReady() {
  refreshCertificates();
}

function onWebPkiError(ex) {
  log('Error: ' + ex.userMessage + ' (' + ex.code + ')');
}

// ------------------------------------------

function refreshCertificates() {
  log('Listing certificates');
  pki.listCertificates({
    selectId: 'certificateSelect'
  }).success(function(certs) {
    log(certs.length + ' certificates found');
  });
}

// ------------------------------------------

function signCades() {
  // get selected certificate thumbprint
  var selectedCertThumb = $('#certificateSelect').val();
  if (!selectedCertThumb) {
    alert('Select a certificate!');
    return;
  }

  log('Signing file...');

  pki.signCades({
    content: fileContentSample,
    output: {
      mode: pki.outputModes.returnContent
    },
    certificateThumbprint: selectedCertThumb,
    includeEncapsulatedContent: true,      // includes original file in the P7S
    autoDetectCosign: true,                // if the file to sign is a P7S, it will perform a...