This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysign.py
216 lines (158 loc) · 6.22 KB
/
pysign.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
""" Module to call the others"""
import getopt
import sys
import os
import string
#custom modules
from liteDb.lister import list_chain
from liteDb.initializer import DbCert
from sign.tryOverride import pkcs7Work
from digest.Hasher import DigestMan
from digest.cryptUtility import Cutil
def usage():
""" Helper method shows the main usage"""
help="""Program Usage :
--listall :Lists all chains
--list [chain name] :Lists proper chain or all if no chain name given
--sign [file_name] [keyplace] [certs..] "Signs a file ;key is private key
cert is certificate(s) that should be included in the sign"
--verify [file] [signature_place]:Verifies the directory
--delete [chain_name] :Deletes the chain in db
--update [chain_name] [trust degree]: Changes the trust degree of a chain
--import [alias] [chain(s)]: Import the given chain to db
--hash [root dir] : Computes all the sums that are in dir and stores in a file with same name as root_dir
--initdb :Deletes all the things in db and returns a fresh copy of it
--showsigner [signature_file] :Prints the chain that is in the signature
--help :Prints that screen
"""
print help
def main(argv):
""" The main arguments passed"""
try:
opts,args=getopt.getopt(argv, "", ["help","showsigner=","initdb","listall","list=","sign=","verify=","delete=","update=","import=","hash=","help"])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt,arg in opts:
#listing a proper chain
if opt in ("--list"):
if args:
usage()
sys.exit(2)
else:
if not list_chain(arg):
print "No available chain use --listall"
sys.exit(2)
print "Chain loaded look at your browser (Firefox)"
#Listing all chains:
elif opt in ("--listall"):
if args:
usage()
sys.exit(2)
else:
dc=DbCert()
res=dc.list_chains()
if not res:
print "No chains in db"
sys.exit(2)
else:
print res
#signing a document
elif opt in ("--sign"):
if not args or len(args)<2:
usage()
sys.exit(2)
if not os.path.exists(arg):
print "Provide a valid file to sign"
sys.exit(2)
arg=Cutil.file_operator(arg, op=0)
if arg==-1 or not arg:
sys.exit(2)
#string.strip(arg)
#dg=DigestMan()
#print dg.gen_buf_hash(arg)
p7=pkcs7Work()
p7.make_sign(args[0],args[1:], arg,"signature.sig")
sys.exit(0)
#verifying a signature
elif opt in ("--verify"):
if not args:
usage()
sys.exit(2)
if not os.path.exists(arg):
print "Provide a valid file to sign"
sys.exit(2)
arg=Cutil.file_operator(arg, op=0)
if arg==-1 or not arg:
sys.exit(2)
#string.strip(arg)
#dg=DigestMan()
#print dg.gen_buf_hash(arg)
p7=pkcs7Work()
#print args[0]
if p7.verify_sign(arg, args[0]):
print "Verification succesful"
sys.exit(0)
#importing a chain into db
elif opt in ("--import"):
dc=DbCert()
if not args:
usage()
sys.exit(2)
else:
if not dc.import_chain(args,arg):
print "Chain insertion Failed"
else:
print "Chain inserted into database"
sys.exit(0)
#deleting a chain from the db
elif opt in ("--delete"):
if args:
usage()
sys.exit(2)
dc=DbCert()
if dc.delete_chain(arg):
print "Chain deleted succesfully"
else:
print "Chain deletion can not be completed"
#updating the trust degreee
elif opt in ("--update"):
if not args or len(args)>1:
usage()
sys.exit(2)
dc=DbCert()
if dc.change_trust(arg, int(args[0])):
print "Chain trust degree changed"
else:
print "Chain degree changing failed"
sys.exit(0)
elif opt in ("--hash"):
if args:
usage()
sys.exit(2)
dg=DigestMan()
if dg.store_hash(arg)==True:
print "Now you can sign the sum file"
else:
print "Hash computing failed"
sys.exit(0)
elif opt in ("--initdb"):
dc=DbCert()
if not dc.c_tables():
""" Initialization process failed"""
sys.exit(0)
elif opt in ("--showsigner"):
if args:
usage()
sys.exit(2)
p7=pkcs7Work()
if not p7.print_signers(arg):
"The chain into signature can not be shown"
sys.exit(0)
elif opt in ("--help"):
usage()
sys.exit(2)
#print opts
#print args
if __name__=="__main__":
main(sys.argv[1:])