Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

x509-pem.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2016 Joyent, Inc.
  2. var x509 = require('./x509');
  3. module.exports = {
  4. read: read,
  5. verify: x509.verify,
  6. sign: x509.sign,
  7. write: write
  8. };
  9. var assert = require('assert-plus');
  10. var asn1 = require('asn1');
  11. var Buffer = require('safer-buffer').Buffer;
  12. var algs = require('../algs');
  13. var utils = require('../utils');
  14. var Key = require('../key');
  15. var PrivateKey = require('../private-key');
  16. var pem = require('./pem');
  17. var Identity = require('../identity');
  18. var Signature = require('../signature');
  19. var Certificate = require('../certificate');
  20. function read(buf, options) {
  21. if (typeof (buf) !== 'string') {
  22. assert.buffer(buf, 'buf');
  23. buf = buf.toString('ascii');
  24. }
  25. var lines = buf.trim().split(/[\r\n]+/g);
  26. var m;
  27. var si = -1;
  28. while (!m && si < lines.length) {
  29. m = lines[++si].match(/*JSSTYLED*/
  30. /[-]+[ ]*BEGIN CERTIFICATE[ ]*[-]+/);
  31. }
  32. assert.ok(m, 'invalid PEM header');
  33. var m2;
  34. var ei = lines.length;
  35. while (!m2 && ei > 0) {
  36. m2 = lines[--ei].match(/*JSSTYLED*/
  37. /[-]+[ ]*END CERTIFICATE[ ]*[-]+/);
  38. }
  39. assert.ok(m2, 'invalid PEM footer');
  40. lines = lines.slice(si, ei + 1);
  41. var headers = {};
  42. while (true) {
  43. lines = lines.slice(1);
  44. m = lines[0].match(/*JSSTYLED*/
  45. /^([A-Za-z0-9-]+): (.+)$/);
  46. if (!m)
  47. break;
  48. headers[m[1].toLowerCase()] = m[2];
  49. }
  50. /* Chop off the first and last lines */
  51. lines = lines.slice(0, -1).join('');
  52. buf = Buffer.from(lines, 'base64');
  53. return (x509.read(buf, options));
  54. }
  55. function write(cert, options) {
  56. var dbuf = x509.write(cert, options);
  57. var header = 'CERTIFICATE';
  58. var tmp = dbuf.toString('base64');
  59. var len = tmp.length + (tmp.length / 64) +
  60. 18 + 16 + header.length*2 + 10;
  61. var buf = Buffer.alloc(len);
  62. var o = 0;
  63. o += buf.write('-----BEGIN ' + header + '-----\n', o);
  64. for (var i = 0; i < tmp.length; ) {
  65. var limit = i + 64;
  66. if (limit > tmp.length)
  67. limit = tmp.length;
  68. o += buf.write(tmp.slice(i, limit), o);
  69. buf[o++] = 10;
  70. i = limit;
  71. }
  72. o += buf.write('-----END ' + header + '-----\n', o);
  73. return (buf.slice(0, o));
  74. }