Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from subprocess import call
- from random import randint
- def randomstring(size):
- chars="abcdefghijklmnopqrstuvwxyz-0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789-"
- string=""
- for i in range(0,size):
- string+=chars[randint(0,len(chars)-1)]
- return string
- class ssl:
- def __init__(self,keylen=2048,certage=365):
- self.KEYLEN = keylen
- self.CERTAGE = certage
- self.HASKEY = False
- self.HASREQ = False
- self.HASCERT = False
- def genpriv(self):
- if not self.HASKEY:
- filename = randomstring(10)
- command = "openssl genrsa -out /tmp/" + filename + ".key " + str(self.KEYLEN)
- call(command.split(" "))
- with open("/tmp/" + filename + ".key","r") as f:
- self.KEY = f.read()
- command = "rm /tmp/" + filename + ".key"
- call(command.split(" "))
- self.HASKEY = True
- def genreq(self,country,state,locality,org,ou,cn):
- if self.HASKEY and not self.HASREQ:
- filename = randomstring(10)
- with open("/tmp/" + filename + ".key","w") as f:
- f.write(self.KEY)
- command = 'openssl req -new -key /tmp/' + filename + '.key -out /tmp/' + filename + '.csr -sha256 -subj'
- call(command.split(" ") + ['/C=' + country + '/ST=' + state + '/L=' + locality + '/O=' + org + '/OU=' + ou + '/CN=' + cn ])
- with open("/tmp/" + filename + ".csr","r") as f:
- self.REQ = f.read()
- command = "rm /tmp/" + filename + ".key /tmp/" + filename + ".csr"
- call(command.split(" "))
- self.HASREQ = True
- def gencert(self):
- if self.HASKEY and self.HASREQ and not self.HASCERT:
- filename = randomstring(10)
- with open("/tmp/" + filename + ".key","w") as f:
- f.write(self.KEY)
- with open("/tmp/" + filename + ".csr","w") as f:
- f.write(self.REQ)
- command = "openssl x509 -req -days " + str(self.CERTAGE) + " -in /tmp/" + filename + ".csr -signkey /tmp/" + filename + ".key -out /tmp/" + filename + ".crt"
- call(command.split(" "))
- with open("/tmp/" + filename + ".crt","r") as f:
- self.CERT = f.read()
- command = "rm /tmp/" + filename + ".key /tmp/" + filename + ".csr /tmp/" + filename + ".crt"
- call(command.split(" "))
- self.HASCERT = True
- def addkey(self,keytext):
- if not self.HASKEY:
- self.KEY = keytext
- self.HASKEY = True
- def addcert(self,certtext):
- if not self.HASREQ and not self.HASCERT:
- self.CERT = certtext
- self.HASCERT = True
- self.HASREQ = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement