6148918c608a3bb9f596a07e8455cfd26a2cf7e2
[sfa.git] / archive / server / keycreator / prog.py
1 import sys
2 from OpenSSL import crypto
3 from M2Crypto import X509
4 sys.path.append('../../util/sec')
5 sys.path.append('../../util')
6 from sec import *
7 from util import *
8 from db import *
9
10 SUFFIX1 = '_srr'
11 SUFFIX2 = '_crr'
12
13 #get input from user
14 hrn = raw_input('Enter the hrn of the object: ')
15
16 #generate certificate and the private key
17 name = get_leaf(hrn)
18 create_self_cert(name)
19
20 #extract the public key from the certificate and input to the database
21 cert = X509.load_cert(name+'.cert')
22 pubkey_pem = cert.get_pubkey().as_pem(cipher=None)
23
24 cnx = get_plDB_conn()
25 tablename = obtain_authority(hrn).replace('.','$')
26 t1 = tablename+SUFFIX1
27 t2 = tablename+SUFFIX2
28
29 querystr = "SELECT * FROM "+t1+" WHERE hrn='"+name+"';"
30 res = cnx.query(querystr)
31 if res:
32         querystr = "UPDATE "+t1+" SET pubkey = '"+pubkey_pem+"' WHERE hrn = '"+name+"';"
33         cnx.query(querystr)
34 else:
35     querystr = "SELECT * FROM "+t2+" WHERE hrn='"+name+"';"
36     res = cnx.query(querystr)
37     if res:
38         querystr = "UPDATE "+t2+" SET pubkey = '"+pubkey_pem+"' WHERE hrn = '"+name+"';"
39         cnx.query(querystr)
40     else:
41         print 'The record with name:'+hrn+"' does not exist in the system.\n"
42         os.exit(1)
43
44 print "Public key is successfully added to '"+hrn+"' record.\nThe certificate and key are generated.\n"
45
46
47
48