myPKI.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import sys, getopt, os
  2. def usage():
  3. print(' USAGE: ./myPKI.py [OPTIONS]')
  4. print(' OPTIONS:')
  5. print('\t-A --initall\t-i -g -d -s')
  6. print('\t-i --init\tInitialise directorys of PKI')
  7. print('\t-g --genca\tGenerate CA root')
  8. print('\t-d --gender\tGenerate DER cert for browser know my self-signed cert')
  9. print('\t-s --gensign\tGenerate and sign certificate for hosts')
  10. print('\t-n --genauth\tGenerate and sign certificate for client authenticationf in .pfx format')
  11. print('\t-h --help\tShows this help')
  12. def generate_client_auth_cert():
  13. tld = input("=> TLD of your certificate :")
  14. client_name = input("=> Client Name :")
  15. # Changement du fichier de config
  16. os.system('sed -i "s/DNSCHANGEME/'+ tld +'/" ./config/ca.config')
  17. os.system('sed -i "s/Root Certificate/'+ tld +'/" ./config/ca.config')
  18. print("=> Création du certificat client")
  19. os.system("openssl genrsa -out ./certificats/" +client_name+ "_" +tld+ ".key 4096")
  20. print("[*] " +client_name+"_"+tld+".key is done")
  21. os.system('openssl req -new -config config/ca.config -key ./certificats/'+client_name+ '_' +tld+ '.key -out ./certificats/'+ client_name +'-remote.csr')
  22. print("[*] CSR Request completed !")
  23. os.system('openssl x509 -req -days 3650 -in ./certificats/'+client_name+'-remote.csr -CA ./certificats/ca.crt -CAkey ./certificats/ca.key -set_serial 001 -out ./certificats/'+client_name+'-remote.pem')
  24. print("[*] Sign OK")
  25. os.system("openssl pkcs12 -export -out ./"+client_name+".full.pfx -inkey ./certificats/"+client_name+'_'+tld+".key -in ./certificats/"+client_name+"-remote.pem -certfile ./certificats/ca.crt")
  26. print("[*] Certificate OK")
  27. def generate_sign():
  28. tld = input("=> TLD of your certificate :")
  29. ip_addr = input("=> IPv4 :")
  30. # Changement du fichier de config
  31. os.system('sed -i "s/DNSCHANGEME/'+ tld +'/" ./config/ca.config')
  32. os.system('sed -i "s/0.0.0.0/'+ ip_addr +'/" ./config/ca.config')
  33. os.system('sed -i "s/Root Certificate/'+ tld +'/" ./config/ca.config')
  34. # Création des certificats
  35. os.system("openssl genrsa -out ./certificats/"+tld+".key 4096")
  36. os.system("openssl req -days 3650 -new -config config/ca.config -key ./certificats/"+tld+".key -out certificats/"+tld+".csr")
  37. os.system("openssl ca -out "+tld+".crt -config config/ca.config -infiles certificats/"+tld+".csr ")
  38. # On restaure ca.config d'origine
  39. os.system('sed -i "s/'+ tld +'/DNSCHANGEME/" ./config/ca.config')
  40. os.system('sed -i "s/'+ ip_addr +'/0.0.0.0/" ./config/ca.config')
  41. os.system('sed -i "s/'+ tld +'/Root Certificate/" ./config/ca.config')
  42. print("[*] Certificate Signed !")
  43. def generate_der():
  44. os.system("openssl x509 -in certificats/ca.crt -outform DER -out certificats/ca.der")
  45. print("[*] Put public CA ./certificats/ca.der on all browser you see !")
  46. def generate_ca():
  47. if not os.path.isfile("./certificats/ca.key"):
  48. os.system("openssl genrsa -out ./certificats/ca.key 4096")
  49. os.system("openssl req -utf8 -new -x509 -days 3000 -config ./config/ca.config -key ./certificats/ca.key -out ./certificats/ca.crt")
  50. print("[*] CA Certificate done !")
  51. else:
  52. print("[x] CA Certificate already exist...")
  53. def init_dir():
  54. if not os.path.exists("./db/ca.db.certs"):
  55. print("[*] Creating directories")
  56. os.makedirs("./db/ca.db.certs")
  57. os.makedirs("./config")
  58. os.makedirs("./certificats")
  59. os.system("echo '01'> ./db/ca.db.serial")
  60. os.system("cp /dev/null ./db/ca.db.index")
  61. os.system("cp ./ca.config.sample ./config/ca.config")
  62. os.system("touch ./db/ca.db.index.attr")
  63. else:
  64. print("[x] Directorys already exist")
  65. def main():
  66. try:
  67. opts, args = getopt.getopt(sys.argv[1:], "Ahigdsn", ["initall", "help", "init", "genca", "gender", "gensign", "genauth"])
  68. for o, a in opts:
  69. if o in ("--help"):
  70. usage()
  71. sys.exit()
  72. elif o in ("--genca"):
  73. generate_ca()
  74. elif o in ("--init"):
  75. init_dir()
  76. elif o in ("--gender"):
  77. generate_der()
  78. elif o in ("--gensign"):
  79. generate_sign()
  80. elif o in ("--genauth"):
  81. generate_client_auth_cert()
  82. elif o in ("--initall"):
  83. init_dir()
  84. generate_ca()
  85. generate_der()
  86. generate_sign()
  87. sys.exit()
  88. except getopt.GetoptError as err:
  89. sys.stderr.write(str(err))
  90. usage()
  91. sys.exit(2)
  92. if __name__ == "__main__":
  93. main()