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