1 #! python 2 3 import sys 4 import os 5 import socket 6 import thread 7 import math 8 9 try: 10 import cryptoIDlib 11 cryptoIDlibLoaded = True 12 except: 13 cryptoIDlibLoaded = False 14 15 16 if __name__ != "__main__": 17 raise "This must be run as a command, not used as a module!" 18 19 20 from tlslite.api import * 21 22 if len(sys.argv) == 1 or (len(sys.argv)==2 and sys.argv[1].lower().endswith("help")): 23 print "" 24 print "Version: 0.3.8" 25 print "" 26 print "RNG: %s" % prngName 27 print "" 28 print "Modules:" 29 if cryptlibpyLoaded: 30 print " cryptlib_py : Loaded" 31 else: 32 print " cryptlib_py : Not Loaded" 33 if m2cryptoLoaded: 34 print " M2Crypto : Loaded" 35 else: 36 print " M2Crypto : Not Loaded" 37 if pycryptoLoaded: 38 print " pycrypto : Loaded" 39 else: 40 print " pycrypto : Not Loaded" 41 if gmpyLoaded: 42 print " GMPY : Loaded" 43 else: 44 print " GMPY : Not Loaded" 45 if cryptoIDlibLoaded: 46 print " cryptoIDlib : Loaded" 47 else: 48 print " cryptoIDlib : Not Loaded" 49 print "" 50 print "Commands:" 51 print "" 52 print " createsrp <db>" 53 print " createsharedkey <db>" 54 print "" 55 print " add <db> <user> <pass> [<bits>]" 56 print " del <db> <user>" 57 print " check <db> <user> [<pass>]" 58 print " list <db>" 59 sys.exit() 60 61 cmd = sys.argv[1].lower() 62 63 class Args: 64 def __init__(self, argv): 65 self.argv = argv 66 def get(self, index): 67 if len(self.argv)<=index: 68 raise SyntaxError("Not enough arguments") 69 return self.argv[index] 70 def getLast(self, index): 71 if len(self.argv)>index+1: 72 raise SyntaxError("Too many arguments") 73 return self.get(index) 74 75 args = Args(sys.argv) 76 77 def reformatDocString(s): 78 lines = s.splitlines() 79 newLines = [] 80 for line in lines: 81 newLines.append(" " + line.strip()) 82 return "\n".join(newLines) 83 84 try: 85 if cmd == "help": 86 command = args.getLast(2).lower() 87 if command == "valid": 88 print "" 89 else: 90 print "Bad command: '%s'" % command 91 92 elif cmd == "createsrp": 93 dbName = args.get(2) 94 95 db = VerifierDB(dbName) 96 db.create() 97 98 elif cmd == "createsharedkey": 99 dbName = args.getLast(2) 100 101 db = SharedKeyDB(dbName) 102 db.create() 103 104 elif cmd == "add": 105 dbName = args.get(2) 106 username = args.get(3) 107 password = args.get(4) 108 109 try: 110 db = VerifierDB(dbName) 111 db.open() 112 if username in db: 113 print "User already in database!" 114 sys.exit() 115 bits = int(args.getLast(5)) 116 N, g, salt, verifier = VerifierDB.makeVerifier(username, password, bits) 117 db[username] = N, g, salt, verifier 118 except ValueError: 119 db = SharedKeyDB(dbName) 120 db.open() 121 if username in db: 122 print "User already in database!" 123 sys.exit() 124 args.getLast(4) 125 db[username] = password 126 127 elif cmd == "del": 128 dbName = args.get(2) 129 username = args.getLast(3) 130 131 try: 132 db = VerifierDB(dbName) 133 db.open() 134 except ValueError: 135 db = SharedKeyDB(dbName) 136 db.open() 137 138 del(db[username]) 139 140 elif cmd == "check": 141 dbName = args.get(2) 142 username = args.get(3) 143 if len(sys.argv)>=5: 144 password = args.getLast(4) 145 else: 146 password = None 147 148 try: 149 db = VerifierDB(dbName) 150 db.open() 151 except ValueError: 152 db = SharedKeyDB(dbName) 153 db.open() 154 155 try: 156 db[username] 157 print "Username exists" 158 159 if password: 160 if db.check(username, password): 161 print "Password is correct" 162 else: 163 print "Password is wrong" 164 except KeyError: 165 print "Username does not exist" 166 sys.exit() 167 168 elif cmd == "list": 169 dbName = args.get(2) 170 171 try: 172 db = VerifierDB(dbName) 173 db.open() 174 except ValueError: 175 db = SharedKeyDB(dbName) 176 db.open() 177 178 if isinstance(db, VerifierDB): 179 print "Verifier Database" 180 def numBits(n): 181 if n==0: 182 return 0 183 return int(math.floor(math.log(n, 2))+1) 184 for username in db.keys(): 185 N, g, s, v = db[username] 186 print numBits(N), username 187 else: 188 print "Shared Key Database" 189 for username in db.keys(): 190 print username 191 else: 192 print "Bad command: '%s'" % cmd 193 except: 194 raise 195