myPKI.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. # Todo :
  4. # - Fix options in command line. -d and -s not working
  5. import sys, getopt, os
  6. def usage():
  7. print ' USAGE: ./myPKI.py [OPTIONS]'
  8. print
  9. print ' OPTIONS:'
  10. print '\t-A --initall\t-i -g -d -s'
  11. print '\t-i --init\tInitialise directorys of PKI'
  12. print '\t-g --genca\tGenerate CA root'
  13. print '\t-d --gender\tGenerate DER cert for browser'
  14. print '\t-s --gensign\tGenerate and sign certificate'
  15. print '\t-h --help\tShows this help'
  16. def generate_sign():
  17. tld = raw_input("=> TLD of your certificate :")
  18. ip_addr = raw_input("=> IPv4 :")
  19. # Changement du fichier de config
  20. os.system('sed -i "s/DNSCHANGEME/'+ tld +'/" ./config/ca.config')
  21. os.system('sed -i "s/0.0.0.0/'+ ip_addr +'/" ./config/ca.config')
  22. os.system('sed -i "s/Root Certificate/'+ tld +'/" ./config/ca.config')
  23. # Création des certificats
  24. os.system("openssl genrsa -out ./certificats/"+tld+".key 4096")
  25. os.system("openssl req -days 3000 -new -config config/ca.config -key ./certificats/"+tld+".key -out certificats/"+tld+".csr")
  26. os.system("openssl ca -out "+tld+".crt -config config/ca.config -infiles certificats/"+tld+".csr ")
  27. # On restaure ca.config d'origine
  28. os.system('sed -i "s/'+ tld +'/DNSCHANGEME/" ./config/ca.config')
  29. os.system('sed -i "s/'+ ip_addr +'/0.0.0.0/" ./config/ca.config')
  30. os.system('sed -i "s/'+ tld +'/Root Certificate/" ./config/ca.config')
  31. print "[*] Certificate Signed !"
  32. def generate_der():
  33. os.system("openssl x509 -in certificats/ca.crt -outform DER -out certificats/ca.der")
  34. print "[*] Put public CA ./certificats/ca.der on all browser you see !"
  35. def generate_ca():
  36. if not os.path.isfile("./certificats/ca.key"):
  37. os.system("openssl genrsa -out ./certificats/ca.key 4096")
  38. os.system("openssl req -utf8 -new -x509 -days 3000 -config ./config/ca.config -key ./certificats/ca.key -out ./certificats/ca.crt")
  39. print "[*] CA Certificate done !"
  40. else:
  41. print "[x] CA Certificate already exist..."
  42. def init_dir():
  43. if not os.path.exists("./db/ca.db.certs"):
  44. print "[*] Creating directories"
  45. os.makedirs("./db/ca.db.certs")
  46. os.makedirs("./config")
  47. os.makedirs("./certificats")
  48. os.system("echo '01'> ./db/ca.db.serial")
  49. os.system("cp /dev/null ./db/ca.db.index")
  50. os.system("cp ./ca.config.sample ./config/ca.config")
  51. os.system("touch ./db/ca.db.index.attr")
  52. else:
  53. print "[x] Directorys already exist"
  54. def main():
  55. try:
  56. opts, args = getopt.getopt(sys.argv[1:], "Ahigds", ["initall", "help", "init", "genca", "gender", "gensign"])
  57. for o, a in opts:
  58. if o in ("--help"):
  59. usage()
  60. sys.exit()
  61. elif o in ("--genca"):
  62. generate_ca()
  63. elif o in ("--init"):
  64. init_dir()
  65. elif o in ("--gender"):
  66. generate_der()
  67. elif o in ("--gensign"):
  68. generate_sign()
  69. elif o in ("--initall"):
  70. init_dir()
  71. generate_ca()
  72. generate_der()
  73. generate_sign()
  74. sys.exit()
  75. except getopt.GetoptError, err:
  76. sys.stderr.write(str(err))
  77. usage()
  78. sys.exit(2)
  79. if __name__ == "__main__":
  80. main()