2
0

x509-certificate.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. var BasicConstraints = require('./basic-constraints');
  11. var KeyUsageExtension = require('./key-usage-extension');
  12. var validator = require('./input-validator');
  13. var constants = require('./constants');
  14. var forge = require('node-forge');
  15. module.exports = X509Certificate;
  16. /**
  17. * @class X509Certificate
  18. * @classdesc Encapsulates an X.509 certificate. To instantiate this object, use
  19. * the method {@link CertificateService.newX509Certificate}.
  20. * @property {string} publicKey The public key of the certificate, in PEM
  21. * format.
  22. * @property {Date} notBefore The starting time of the certificate's validity.
  23. * @property {Date} notAfter The ending time of the certificate's validity.
  24. * @property {string} serialNumber The serial number of the certificate.
  25. * @property {string} subjectCommonName The subject common name of the
  26. * certificate.
  27. * @property {string} subjectOrganizationalUnit The subject organizational unit
  28. * of the certificate.
  29. * @property {string} subjectOrganization The subject organization of the
  30. * certificate.
  31. * @property {string} subjectLocality The subject locality of the certificate.
  32. * @property {string} subjectCountry The subject country of the certificate.
  33. * @property {string} issuerCommonName The issuer common name of the
  34. * certificate.
  35. * @property {string} issuerOrganizationalUnit The issuer organizational unit of
  36. * the certificate.
  37. * @property {string} issuerOrganization The issuer organization of the
  38. * certificate.
  39. * @property {string} issuerLocality The issuer locality of the certificate.
  40. * @property {string} issuerCountry The issuer country of the certificate.
  41. * @property {BasicConstraints} basicConstraints The basic constraints of the
  42. * certificate.
  43. * @property {KeyUsageExtension} keyUsageExtension The key usage extension of
  44. * the certificate.
  45. */
  46. function X509Certificate(certificate) {
  47. var forgeCertificate_ = forge.pki.certificateFromPem(certificate, true);
  48. validator.checkIsDefinedAndNotNull(
  49. forgeCertificate_.publicKey, 'Public key of certificate');
  50. this.publicKey = forge.pki.publicKeyToPem(forgeCertificate_.publicKey, 64);
  51. validator.checkIsDefinedAndNotNull(
  52. forgeCertificate_.validity.notBefore,
  53. 'Starting time of validity of certificate');
  54. this.notBefore = forgeCertificate_.validity.notBefore;
  55. validator.checkIsDefinedAndNotNull(
  56. forgeCertificate_.validity.notAfter,
  57. 'Ending time of validity of certificate');
  58. this.notAfter = forgeCertificate_.validity.notAfter;
  59. this.serialNumber = forgeCertificate_.serialNumber;
  60. if (!this.serialNumber) {
  61. this.serialNumber = '';
  62. }
  63. var field = forgeCertificate_.subject.getField({name: 'commonName'});
  64. this.subjectCommonName = (field) ? field.value : '';
  65. field = forgeCertificate_.subject.getField({shortName: 'OU'});
  66. this.subjectOrganizationalUnit = (field) ? field.value : '';
  67. field = forgeCertificate_.subject.getField({name: 'organizationName'});
  68. this.subjectOrganization = (field) ? field.value : '';
  69. field = forgeCertificate_.subject.getField({name: 'localityName'});
  70. this.subjectLocality = (field) ? field.value : '';
  71. field = forgeCertificate_.subject.getField({name: 'countryName'});
  72. this.subjectCountry = (field) ? field.value : '';
  73. field = forgeCertificate_.issuer.getField({name: 'commonName'});
  74. this.issuerCommonName = (field) ? field.value : '';
  75. field = forgeCertificate_.issuer.getField({shortName: 'OU'});
  76. this.issuerOrganizationalUnit = (field) ? field.value : '';
  77. field = forgeCertificate_.issuer.getField({name: 'organizationName'});
  78. this.issuerOrganization = (field) ? field.value : '';
  79. field = forgeCertificate_.issuer.getField({name: 'localityName'});
  80. this.issuerLocality = (field) ? field.value : '';
  81. field = forgeCertificate_.issuer.getField({name: 'countryName'});
  82. this.issuerCountry = (field) ? field.value : '';
  83. var constraints = forgeCertificate_.getExtension({name: 'basicConstraints'});
  84. this.basicConstraints =
  85. (constraints) ? new BasicConstraints(constraints) : null;
  86. var extension = forgeCertificate_.getExtension({name: 'keyUsage'});
  87. this.keyUsageExtension =
  88. (extension) ? new KeyUsageExtension(extension) : null;
  89. /**
  90. * Verifies that the certificate provided as input was signed by this
  91. * certificate.
  92. *
  93. * @function verify
  94. * @memberof X509Certificate
  95. * @param {string}
  96. * certificate The certificate whose signature is to be verified,
  97. * in PEM format.
  98. * @returns {boolean} <code>true</code> if the certificate provided as
  99. * input was signed by this certificate, <code>false</code>
  100. * otherwise.
  101. * @throws {Error}
  102. * If the input data validation fails.
  103. */
  104. this.verify = function(certificate) {
  105. validator.checkIsNonEmptyString(
  106. certificate,
  107. 'Certificate, in PEM format, containing signature to be verified');
  108. return forgeCertificate_.verify(forge.pki.certificateFromPem(certificate));
  109. };
  110. /**
  111. * Serializes this certificate into its PEM string representation.
  112. *
  113. * @function toPem
  114. * @memberof X509Certificate
  115. * @returns {string} The PEM string representation of this certificate.
  116. */
  117. this.toPem = function() {
  118. return forge.pki.certificateToPem(
  119. forgeCertificate_, constants.PEM_LINE_LENGTH);
  120. };
  121. return Object.freeze(this);
  122. }