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