removed another bunch of references to geni
[sfa.git] / sfa / trust / gid.py
1 ##
2 # Implements SFA GID. GIDs are based on certificates, and the GID class is a
3 # descendant of the certificate class.
4 ##
5
6 ### $Id$
7 ### $URL$
8
9 import xmlrpclib
10 import uuid
11
12 from sfa.trust.certificate import Certificate
13
14 ##
15 # Create a new uuid. Returns the UUID as a string.
16
17 def create_uuid():
18     return str(uuid.uuid4().int)
19
20 ##
21 # GID is a tuplie:
22 #    (uuid, hrn, public_key)
23 #
24 # UUID is a unique identifier and is created by the python uuid module
25 #    (or the utility function create_uuid() in gid.py).
26 #
27 # HRN is a human readable name. It is a dotted form similar to a backward domain
28 #    name. For example, planetlab.us.arizona.bakers.
29 #
30 # PUBLIC_KEY is the public key of the principal identified by the UUID/HRN.
31 # It is a Keypair object as defined in the cert.py module.
32 #
33 # It is expected that there is a one-to-one pairing between UUIDs and HRN,
34 # but it is uncertain how this would be inforced or if it needs to be enforced.
35 #
36 # These fields are encoded using xmlrpc into the subjectAltName field of the
37 # x509 certificate. Note: Call encode() once the fields have been filled in
38 # to perform this encoding.
39
40
41 class GID(Certificate):
42     uuid = None
43     hrn = None
44
45     ##
46     # Create a new GID object
47     #
48     # @param create If true, create the X509 certificate
49     # @param subject If subject!=None, create the X509 cert and set the subject name
50     # @param string If string!=None, load the GID from a string
51     # @param filename If filename!=None, load the GID from a file
52
53     def __init__(self, create=False, subject=None, string=None, filename=None, uuid=None, hrn=None):
54         Certificate.__init__(self, create, subject, string, filename)
55         if uuid:
56             self.uuid = uuid
57         if hrn:
58             self.hrn = hrn
59
60     def set_uuid(self, uuid):
61         self.uuid = uuid
62
63     def get_uuid(self):
64         if not self.uuid:
65             self.decode()
66         return self.uuid
67
68     def set_hrn(self, hrn):
69         self.hrn = hrn
70
71     def get_hrn(self):
72         if not self.hrn:
73             self.decode()
74         return self.hrn
75
76     ##
77     # Encode the GID fields and package them into the subject-alt-name field
78     # of the X509 certificate. This must be called prior to signing the
79     # certificate. It may only be called once per certificate.
80
81     def encode(self):
82         dict = {"uuid": self.uuid,
83                 "hrn": self.hrn}
84         str = xmlrpclib.dumps((dict,))
85         self.set_data(str)
86
87     ##
88     # Decode the subject-alt-name field of the X509 certificate into the
89     # fields of the GID. This is automatically called by the various get_*()
90     # functions in this class.
91
92     def decode(self):
93         data = self.get_data()
94         if data:
95             dict = xmlrpclib.loads(self.get_data())[0][0]
96         else:
97             dict = {}
98
99         self.uuid = dict.get("uuid", None)
100         self.hrn = dict.get("hrn", None)
101
102     ##
103     # Dump the credential to stdout.
104     #
105     # @param indent specifies a number of spaces to indent the output
106     # @param dump_parents If true, also dump the parents of the GID
107
108     def dump(self, indent=0, dump_parents=False):
109         print " "*indent, " hrn:", self.get_hrn()
110         print " "*indent, "uuid:", self.get_uuid()
111
112         if self.parent and dump_parents:
113             print " "*indent, "parent:"
114             self.parent.dump(indent+4)
115
116     ##
117     # Verify the chain of authenticity of the GID. First perform the checks
118     # of the certificate class (verifying that each parent signs the child,
119     # etc). In addition, GIDs also confirm that the parent's HRN is a prefix
120     # of the child's HRN.
121     #
122     # Verifying these prefixes prevents a rogue authority from signing a GID
123     # for a principal that is not a member of that authority. For example,
124     # planetlab.us.arizona cannot sign a GID for planetlab.us.princeton.foo.
125
126     def verify_chain(self, trusted_certs = None):
127         # do the normal certificate verification stuff
128         Certificate.verify_chain(self, trusted_certs)
129
130         if self.parent:
131             # make sure the parent's hrn is a prefix of the child's hrn
132             if not self.get_hrn().startswith(self.parent.get_hrn()):
133                 raise GidParentHrn(self.parent.get_subject())
134
135         return
136
137
138
139
140