Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. from subprocess import call
  2. from random import randint
  3.  
  4. def randomstring(size):
  5. chars="abcdefghijklmnopqrstuvwxyz-0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789-"
  6. string=""
  7. for i in range(0,size):
  8. string+=chars[randint(0,len(chars)-1)]
  9. return string
  10.  
  11.  
  12. class ssl:
  13. def __init__(self,keylen=2048,certage=365):
  14. self.KEYLEN = keylen
  15. self.CERTAGE = certage
  16. self.HASKEY = False
  17. self.HASREQ = False
  18. self.HASCERT = False
  19.  
  20. def genpriv(self):
  21. if not self.HASKEY:
  22. filename = randomstring(10)
  23. command = "openssl genrsa -out /tmp/" + filename + ".key " + str(self.KEYLEN)
  24. call(command.split(" "))
  25. with open("/tmp/" + filename + ".key","r") as f:
  26. self.KEY = f.read()
  27. command = "rm /tmp/" + filename + ".key"
  28. call(command.split(" "))
  29. self.HASKEY = True
  30.  
  31. def genreq(self,country,state,locality,org,ou,cn):
  32. if self.HASKEY and not self.HASREQ:
  33. filename = randomstring(10)
  34. with open("/tmp/" + filename + ".key","w") as f:
  35. f.write(self.KEY)
  36. command = 'openssl req -new -key /tmp/' + filename + '.key -out /tmp/' + filename + '.csr -sha256 -subj'
  37. call(command.split(" ") + ['/C=' + country + '/ST=' + state + '/L=' + locality + '/O=' + org + '/OU=' + ou + '/CN=' + cn ])
  38. with open("/tmp/" + filename + ".csr","r") as f:
  39. self.REQ = f.read()
  40. command = "rm /tmp/" + filename + ".key /tmp/" + filename + ".csr"
  41. call(command.split(" "))
  42. self.HASREQ = True
  43.  
  44. def gencert(self):
  45. if self.HASKEY and self.HASREQ and not self.HASCERT:
  46. filename = randomstring(10)
  47. with open("/tmp/" + filename + ".key","w") as f:
  48. f.write(self.KEY)
  49. with open("/tmp/" + filename + ".csr","w") as f:
  50. f.write(self.REQ)
  51. command = "openssl x509 -req -days " + str(self.CERTAGE) + " -in /tmp/" + filename + ".csr -signkey /tmp/" + filename + ".key -out /tmp/" + filename + ".crt"
  52. call(command.split(" "))
  53. with open("/tmp/" + filename + ".crt","r") as f:
  54. self.CERT = f.read()
  55. command = "rm /tmp/" + filename + ".key /tmp/" + filename + ".csr /tmp/" + filename + ".crt"
  56. call(command.split(" "))
  57. self.HASCERT = True
  58.  
  59. def addkey(self,keytext):
  60. if not self.HASKEY:
  61. self.KEY = keytext
  62. self.HASKEY = True
  63.  
  64. def addcert(self,certtext):
  65. if not self.HASREQ and not self.HASCERT:
  66. self.CERT = certtext
  67. self.HASCERT = True
  68. self.HASREQ = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement