4 # SFA Certificate Signing and management. Root authorities can use this script to sign
5 # the certificate of another authority and become its parent.
10 # sfa-ca.py --sign PEER_CERT_FILENAME -o OUTPUT_FILENAME
12 ## import a cert and update the registry hierarchy
13 # sfa-ca.py --import CERT_FILENAME
16 # sfa-ca.py --display CERT_FILENAME
21 from optparse import OptionParser
22 from sfa.trust.certificate import Keypair, Certificate
23 from sfa.trust.gid import GID, create_uuid
24 from sfa.trust.hierarchy import Hierarchy
25 from sfa.util.config import Config
26 from collections import defaultdict
31 parser = OptionParser(usage="%(script_name)s [options]" % locals())
32 parser.add_option("-d", "--display", dest="display", default=None,
33 help="print contents of specified gid")
34 parser.add_option("-s", "--sign", dest="sign", default=None,
36 parser.add_option("-k", "--key", dest="key", default=None,
37 help="keyfile to use for signing")
38 parser.add_option("-a", "--authority", dest="authority", default=None,
39 help="sign the gid using the specified authority ")
40 parser.add_option("-i", "--import", dest="importgid", default=None,
41 help="gid file to import into the registry")
42 parser.add_option("-e", "--export", dest="export",
43 help="name of gid to export from registry")
44 parser.add_option("-o", "--outfile", dest="outfile",
45 help="where to write the exprted gid")
46 parser.add_option("-v", "--verbose", dest="verbose", default=False,
47 action="store_true", help="be verbose")
49 (options, args) = parser.parse_args()
56 elif options.importgid:
67 Display the sepcified GID
69 gidfile = os.path.abspath(options.display)
70 if not gidfile or not os.path.isfile(gidfile):
71 print "No such gid: %s" % gidfile
73 gid = GID(filename=gidfile)
74 gid.dump(dump_parents=True)
76 def sign_gid(gid, parent_key, parent_gid):
77 gid.set_issuer(parent_key, parent_gid.get_hrn())
78 gid.set_parent(parent_gid)
79 gid.set_intermediate_ca(True)
80 gid.set_pubkey(gid.get_pubkey())
86 Sign the specified gid
88 hierarchy = Hierarchy()
90 default_authority = config.SFA_INTERFACE_HRN
91 auth_info = hierarchy.get_auth_info(default_authority)
94 gidfile = os.path.abspath(options.sign)
95 if not os.path.isfile(gidfile):
96 print "no such gid: %s" % gidfile
98 gid = GID(filename=gidfile)
100 # remove previous parent
101 gid = GID(string=gid.save_to_string(save_parents=False))
103 # load the parent private info
104 authority = options.authority
105 # if no pkey was specified, then use the this authority's key
107 authority = default_authority
109 if not hierarchy.auth_exists(authority):
110 print "no such authority: %s" % authority
112 # load the parent gid and key
113 auth_info = hierarchy.get_auth_info(authority)
114 pkeyfile = auth_info.privkey_filename
115 parent_key = Keypair(filename=pkeyfile)
116 parent_gid = auth_info.gid_object
119 outfile = options.outfile
121 outfile = os.path.abspath('./signed-%s.gid' % gid.get_hrn())
123 # check if gid already has a parent
127 print "Signing %s gid with parent %s" % \
128 (gid.get_hrn(), parent_gid.get_hrn())
129 gid = sign_gid(gid, parent_key, parent_gid)
130 # save the signed gid
132 print "Writing signed gid %s" % outfile
133 gid.save_to_file(outfile, save_parents=True)
136 def export_gid(options):
137 from sfa.util.table import SfaTable
138 # lookup the record for the specified hrn
141 # check sfa table first
143 records = table.find({'hrn': hrn, type: 'authority'})
145 # check the authorities hierarchy
146 hierarchy = Hierarchy()
148 auth_info = hierarchy.get_auth_info()
149 gid = auth_info.gid_object
151 print "Record: %s not found" % hrn
155 gid = GID(string=record['gid'])
158 outfile = options.outfile
160 outfile = os.path.abspath('./%s.gid' % gid.get_hrn())
164 print "Writing %s gid to %s" % (gid.get_hrn(), outfile)
165 gid.save_to_file(outfile, save_parents=True)
167 def import_gid(options):
169 Import the specified gid into the registry (db and authorities
170 hierarchy) overwriting any previous gid.
172 from sfa.util.table import SfaTable
173 from sfa.util.record import SfaRecord
175 gidfile = os.path.abspath(options.importgid)
176 if not gidfile or not os.path.isfile(gidfile):
177 print "No such gid: %s" % gidfile
179 gid = GID(filename=gidfile)
181 # check if it exists within the hierarchy
182 hierarchy = Hierarchy()
183 if not hierarchy.auth_exists(gid.get_hrn()):
184 print "%s not found in hierarchy" % gid.get_hrn()
187 # check if record exists in db
189 records = table.find({'hrn': gid.get_hrn(), 'type': 'authority'})
191 print "%s not found in record database" % get.get_hrn()
194 # update the database record
196 record['gid'] = gid.save_to_string(save_parents=True)
199 print "Imported %s gid into db" % record['hrn']
201 # update the hierarchy
202 auth_info = hierarchy.get_auth_info(gid.get_hrn())
203 filename = auth_info.gid_filename
204 gid.save_to_file(filename, save_parents=True)
206 print "Writing %s gid to %s" % (gid.get_hrn(), filename)
208 # re-sign all existing gids signed by this authority
209 # create a dictionary of records keyed on the record's authority
210 record_dict = defaultdict(list)
211 # only get regords that belong to this authority
212 # or any of its sub authorities
213 child_records = table.find({'hrn': '%s*' % gid.get_hrn()})
214 if not child_records:
217 for record in child_records:
218 record_dict[record['authority']].append(record)
220 # start with the authority we just imported
221 authorities = [gid.get_hrn()]
223 next_authorities = []
224 for authority in authorities:
225 # create a new signed gid for each record at this authority
226 # and update the registry
227 auth_info = hierarchy.get_auth_info(authority)
228 records = record_dict[authority]
229 for record in records:
230 record_gid = GID(string=record['gid'])
231 parent_pkey = Keypair(filename=auth_info.privkey_filename)
232 parent_gid = GID(filename=auth_info.gid_filename)
234 print "re-signing %s gid with parent %s" % \
235 (record['hrn'], parent_gid.get_hrn())
236 signed_gid = sign_gid(record_gid, parent_pkey, parent_gid)
237 record['gid'] = signed_gid.save_to_string(save_parents=True)
240 # if this is an authority then update the hierarchy
241 if record['type'] == 'authority':
242 record_info = hierarchy.get_auth_info(record['hrn'])
244 print "Writing %s gid to %s" % (record['hrn'], record_info.gid_filename)
245 signed_gid.save_to_file(filename=record_info.gid_filename, save_parents=True)
247 # update list of next authorities
248 tmp_authorities = set([record['hrn'] for record in records \
249 if record['type'] == 'authority'])
250 next_authorities.extend(tmp_authorities)
252 # move on to next set of authorities
253 authorities = next_authorities
255 if __name__ == '__main__':