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.13.0.min.js" integrity="sha256-nVy9sLakeQ03Xl+jOjBdjdfnZ9UVwwqNgAYszhxi4VQ=" crossorigin="anonymous"></script>

<h2>Web PKI - Local CAdES Signature</h2>
<h4>Signature from local computer file and signed CAdES (P7S) saved in the same directory with suffix '-signed'</h4>
<p>Select a certificate:
  <br/>
  <select id="certificateSelect"></select>
</p>
<p>
  <button id="refreshButton" type="button">Refresh Certificates</button>
</p>
<p>
  <button id="selectFile" type="button">Select a file</button>
  <span id="selectedFileName"> !!!! </span>
</p>
<br/>
<p>
  <button id="signButton" type="button">Sign File</button>
</p>

<p> Signed Files:</p>
<div id="signedFilesPanel"></div>
<br/>
<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 from local computer file and signed CAdES (P7S) saved in the same directory with suffix '-signed'

var fileInfo = null;
var signedFileInfo = 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
  });
}

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 signFile() {
  // get selected certificate thumbprint
  var selectedCertThumb = $('#certificateSelect').val();
  if (!selectedCertThumb) {
    alert('Select a certificate!');
    return;
  }

  signedFileInfo = null;

  log('Signing file...');

  pki.signCades({
    fileId: fileInfo.id,
    output: {
      mode: pki.outputModes.autoSave,
      fileNameSuffix: '-signed'
    },
    certificateThumbprint: selectedCertThumb,
    includeEncapsulatedContent: true,      // includes original file in the P7S
    autoDetectCosign: true,                // if the file to sign is a P7S, it will perform a...