def getKeyFromPEM(PEMdata): ''' Return a private key that can be feeded to the rsa module. @param PEMdata: data of a PEM certificate (without header nor footer) @return: key dictionary with private d,p,q values ''' derdata = b64decode(PEMdata) privkey = derdecode(derdata)[0] return {'d': int(privkey[3]) , 'p': int(privkey[4]), 'q':int(privkey[5])}
def _encryptToken (self, token, publickey): ''' Return encrypted token @param token: An arbitrary string to be encrypted. Assumed to be "quite" random, no padding is done nor further security considerations. @param publickey: A string containing a PEM public key, normally multiline. We assume that there is NO header. Normally this value is retrieved from inside the database @return a string containing the token encrypted, ready to be sent to the user ''' derbits = b64decode(publickey) pubkey = derdecode(derbits)[0] # The PEM should be in this schema key = { 'n': int(pubkey[0]) , 'e': int(pubkey[1]) } return rsa.encrypt(token, key)
exit(-1) print "Encrypted token received:" print etoken[0:76] print "..." # Open the sample key, read the private key, and try to decrypt privdata = "" with open("johnsmith.rsa", "r") as f: for line in f: if line[0:5] == "-----": pass else: privdata += line derdata = b64decode(privdata) privkey = derdecode(derdata)[0] key = {"d": int(privkey[3]), "p": int(privkey[4]), "q": int(privkey[5])} token = rsa.decrypt(etoken, key) print "Decrypted token:" print token authURL = "https://" + "johnsmith" + ":" + token + "@" + "127.0.0.1" + ":" + "1356" serverConn = xmlrpclib.ServerProxy(authURL) print "Sending ping from authorized user. Response:" print serverConn.ping() serverConn = xmlrpclib.ServerProxy("https://*****:*****@127.0.0.1:1356") print "Sending ping from unauthorized user. Response:"