Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from concurrent.futures import ThreadPoolExecutor
  3.  
  4. import asyncio
  5. import os
  6.  
  7. io_thread_pool = ThreadPoolExecutor()
  8.  
  9.  
  10. @asyncio.coroutine
  11. def verify_chain(*paths, loop=None):
  12. if loop is None:
  13. loop = asyncio.get_event_loop()
  14.  
  15. chain = b''
  16. for path in paths:
  17. with open(path, 'rb') as pem:
  18. line = yield from loop.run_in_executor(io_thread_pool, pem.read)
  19. chain += line
  20. chain += b'\n'
  21.  
  22. p = yield from asyncio.create_subprocess_exec(
  23. 'certtool', '--verify-chain', stdin=asyncio.subprocess.PIPE)
  24. yield from p.communicate(chain)
  25. assert p.returncode == 0
  26.  
  27. @asyncio.coroutine
  28. def ensure_private_key(path):
  29. if os.path.exists(path):
  30. return
  31.  
  32. p = yield from asyncio.create_subprocess_exec(
  33. 'certtool', '--generate-privkey', '--outfile', path)
  34.  
  35. yield from p.wait()
  36. assert p.returncode == 0
  37.  
  38. @asyncio.coroutine
  39. def ensure_self_signed_certificate(certpath, keypath, template):
  40. yield from ensure_private_key(keypath)
  41.  
  42. if os.path.exists(certpath):
  43. return
  44.  
  45. p = yield from asyncio.create_subprocess_exec(
  46. 'certtool', '--generate-self-signed', '--outfile', certpath,
  47. '--load-privkey', keypath, '--template', template)
  48. yield from p.wait()
  49. assert p.returncode == 0
  50.  
  51. @asyncio.coroutine
  52. def ensure_ca_certificate(capem, cakey):
  53. yield from ensure_self_signed_certificate(capem, cakey, 'ca.template')
  54.  
  55. @asyncio.coroutine
  56. def ensure_certificate(certpath, keypath, capem, cakey, template):
  57. yield from ensure_private_key(keypath)
  58. if os.path.exists(certpath):
  59. return
  60.  
  61. p = yield from asyncio.create_subprocess_exec(
  62. 'certtool', '--generate-certificate',
  63. '--outfile', certpath, '--load-privkey', keypath,
  64. '--load-ca-certificate', capem, '--load-ca-privkey', cakey,
  65. '--template', template)
  66. yield from p.wait()
  67. assert p.returncode == 0
  68.  
  69. @asyncio.coroutine
  70. def main():
  71. yield from ensure_ca_certificate('ca.pem', 'ca.key')
  72. yield from asyncio.gather(
  73. ensure_certificate('server.pem', 'server.key', 'ca.pem', 'ca.key', 'server.template'),
  74. ensure_certificate('client.pem', 'client.key', 'ca.pem', 'ca.key', 'client.template'))
  75.  
  76. yield from asyncio.gather(
  77. verify_chain('ca.pem'),
  78. verify_chain('server.pem', 'ca.pem'),
  79. verify_chain('client.pem', 'ca.pem'))
  80.  
  81. if __name__ == '__main__':
  82. loop = asyncio.get_event_loop()
  83. try:
  84. loop.run_until_complete(main())
  85. finally:
  86. loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement