certs.me 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. certs.me -- Test certificate generation
  3. This file provides targets to generate SSL test certificates using OpenSSL.
  4. WARNING: All these certificates are for internal use only.
  5. To generate a certificate request to send to a certificate authority like Verisign, do:
  6. This will create a certificate request file in "server.csr" and a private key in "server.key"
  7. me cert-request
  8. To use appweb with HTTP, you need a server certificate.
  9. This command will generate a self-signed test certificate called "self.crt" with a private key "self.key".
  10. me self-signed-cert
  11. To create a CA and then a server cert based on this CA.
  12. This command will generate a CA certificate in ca.crt with a CA private key in ca.key. The last minted certificate
  13. serial number is in ca.srl.
  14. me ca-cert
  15. To generate a certificate signed by the test CA.
  16. This will generate a certificate in "test.crt" with a private key in "test.key".
  17. me test-cert
  18. To generate an Elliptic Curve certificate signed by the test CA.
  19. This will generate a certificate in "ec.crt" with a private key in "ec.key".
  20. me ec-cert
  21. For all cert targets:
  22. me generate-certs
  23. */
  24. Me.load({
  25. settings: {
  26. certs: {
  27. days: 3650,
  28. bits: 2048,
  29. gendh: false,
  30. }
  31. },
  32. mixin: `
  33. function ossl(command, input) {
  34. trace('Run', 'openssl ' + command)
  35. let cmd = Cmd()
  36. cmd.env = {BIN: me.dir.bin}
  37. let result = cmd.start('openssl ' + command, {detach: true})
  38. if (input) {
  39. cmd.write(Path(input).readString())
  40. }
  41. cmd.finalize()
  42. cmd.wait()
  43. if (cmd.status != 0) {
  44. throw new IOError('Command failed, status ' + cmd.status + '\n' + cmd.error)
  45. }
  46. return cmd.response
  47. }
  48. `,
  49. targets: {
  50. 'install-certs': {
  51. type: 'file',
  52. path: '${BIN}/',
  53. files: [ 'samples/**' ],
  54. },
  55. 'generate-certs': {
  56. depends: [ 'ca-cert', 'self-signed-cert', 'test-cert', 'ec-cert' ],
  57. },
  58. 'generate-samples': {
  59. depends: [ 'generate-certs', 'get-roots' ]
  60. action: `
  61. for each (f in Path('samples').files('**')) {
  62. trace('Generate', f)
  63. f.remove()
  64. Path(f.basename).rename(f)
  65. }
  66. rm(['*.old', '*.csr', 'ca.db', 'ca.db.attr', '*.pem'])
  67. `
  68. },
  69. 'get-roots': {
  70. action: `
  71. let http = Http()
  72. http.verify = false
  73. // http.get('https://pki.google.com/roots.pem')
  74. http.get('https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt')
  75. let dest = Path('${BIN}/roots.crt')
  76. trace('Create', dest)
  77. dest.write(http.response)
  78. `
  79. },
  80. /*
  81. WARNING: Self-signed server certificate for testing ONLY.
  82. Use a self-signed certificate when you just want quick and dirty testing.
  83. The browser will say it doesn't recognize this certificate, but that is ok for testing only.
  84. Creates a private key in self.key.
  85. See: http://www.sslshopper.com/article-most-common-openssl-commands.html
  86. */
  87. 'self-signed-cert': {
  88. message: 'Make: Self-signed Certificate: self.crt',
  89. action: `
  90. ossl('genrsa -out ${BIN}/self.key ${settings.certs.bits}')
  91. ossl('req -new -x509 -days ${settings.certs.days} -key ${BIN}/self.key -out ${BIN}/self.crt', 'self.ans')
  92. `,
  93. },
  94. /*
  95. Setup a test certificate authority. Use this if you will be generating multiple certificates for clients and
  96. servers. NOTE: this is only for test. The certificate authority is not a real entity!
  97. */
  98. 'ca-cert': {
  99. message: 'Make: CA Certificate: ca.crt',
  100. action: `
  101. ossl('genrsa -out ${BIN}/ca.key ${settings.certs.bits}')
  102. ossl('req -config openssl.conf -new -x509 -days ${settings.certs.days} -key ${BIN}/ca.key -out ${BIN}/ca.crt -extensions caExtensions')
  103. me.dir.bin.join('ca.srl').write('9999991000\n')
  104. me.dir.bin.join('ca.db').write('')
  105. `,
  106. },
  107. /*
  108. Test cert signed by the test CA above. This is used for test as the server cert and on the client side
  109. when validating client certs.
  110. */
  111. 'test-cert': {
  112. message: 'Make: Test Certificate: test.crt',
  113. action: `
  114. ossl('genrsa -out ${BIN}/test.key ${settings.certs.bits}')
  115. ossl('req -new -key ${BIN}/test.key -out ${BIN}/test.csr', 'test.ans')
  116. ossl('ca -batch -config openssl.conf -notext -in ${BIN}/test.csr -out ${BIN}/test.crt -extensions server')
  117. `,
  118. },
  119. 'ec-cert': {
  120. message: 'Make: EC Test Certificate: ec.crt',
  121. action: `
  122. ossl('ecparam -genkey -name prime256v1 -out ${BIN}/ec.key')
  123. ossl('req -new -key ${BIN}/ec.key -out ${BIN}/ec.csr', 'ec.ans')
  124. ossl('req -x509 -days 365 -key ${BIN}/ec.key -in ${BIN}/ec.csr -out ${BIN}/ec.crt')
  125. `
  126. },
  127. 'dhparams': {
  128. message: 'Generate: Local DH parameters for OpenSSL ... may take a few minutes',
  129. action: `
  130. me.dir.bin.join('dh.c').write(Cmd.run('openssl dhparam -C ${settings.certs.bits} -out ${BIN}/dh.pem') + '\n')
  131. `
  132. }
  133. /*
  134. Generate a certificate request to send to a certificate authority like Verisign
  135. Generates a server key in "server.key"
  136. */
  137. 'cert-request': {
  138. action: `
  139. ossl('genrsa -out ${BIN}/server.key ${settings.certs.bits}')
  140. ossl('req -new -key ${BIN}/server.key -out ${BIN}/server.csr', 'test.ans')
  141. `,
  142. },
  143. 'show-certs': {
  144. action: `
  145. print(ossl('x509 -in ${BIN}/ca.crt -noout -text'))
  146. print(ossl('x509 -in ${BIN}/test.crt -noout -text'))
  147. print(ossl('x509 -in ${BIN}/self.crt -noout -text'))
  148. `
  149. }
  150. }
  151. })