remove hrn for the the encoded format
[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 from sfa.util.namespace import *
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 # URN is a human readable identifier of form:
31 #   "urn:publicid:IDN+toplevelauthority[:sub-auth.]*[\res. type]\ +object name"
32 #   For  example, urn:publicid:IDN+planetlab:us:arizona+user+bakers      
33 #
34 # PUBLIC_KEY is the public key of the principal identified by the UUID/HRN.
35 # It is a Keypair object as defined in the cert.py module.
36 #
37 # It is expected that there is a one-to-one pairing between UUIDs and HRN,
38 # but it is uncertain how this would be inforced or if it needs to be enforced.
39 #
40 # These fields are encoded using xmlrpc into the subjectAltName field of the
41 # x509 certificate. Note: Call encode() once the fields have been filled in
42 # to perform this encoding.
43
44
45 class GID(Certificate):
46     uuid = None
47     hrn = None
48     urn = None
49
50     ##
51     # Create a new GID object
52     #
53     # @param create If true, create the X509 certificate
54     # @param subject If subject!=None, create the X509 cert and set the subject name
55     # @param string If string!=None, load the GID from a string
56     # @param filename If filename!=None, load the GID from a file
57
58     def __init__(self, create=False, subject=None, string=None, filename=None, uuid=None, hrn=None, urn=None):
59         
60         Certificate.__init__(self, create, subject, string, filename)
61         if uuid:
62             self.uuid = uuid
63         if hrn:
64             self.hrn = hrn
65         if urn:
66             self.urn = urn
67             self.hrn, type = urn_to_hrn(urn)
68
69     def set_uuid(self, uuid):
70         self.uuid = uuid
71
72     def get_uuid(self):
73         if not self.uuid:
74             self.decode()
75         return self.uuid
76
77     def set_hrn(self, hrn):
78         self.hrn = hrn
79
80     def get_hrn(self):
81         if not self.hrn:
82             self.decode()
83         return self.hrn
84
85     def set_urn(self, urn):
86         self.urn = urn
87         self.hrn, type = urn_to_hrn(urn)
88  
89     def get_urn(self):
90         if not self.urn:
91             self.decode()
92         return self.urn            
93
94     ##
95     # Encode the GID fields and package them into the subject-alt-name field
96     # of the X509 certificate. This must be called prior to signing the
97     # certificate. It may only be called once per certificate.
98
99     def encode(self):
100         dict = {"uuid": self.uuid,
101                 "urn": self.urn}
102         str = xmlrpclib.dumps((dict,))
103         self.set_data(str)
104
105     ##
106     # Decode the subject-alt-name field of the X509 certificate into the
107     # fields of the GID. This is automatically called by the various get_*()
108     # functions in this class.
109
110     def decode(self):
111         data = self.get_data()
112         if data:
113             dict = xmlrpclib.loads(self.get_data())[0][0]
114         else:
115             dict = {}
116
117         self.uuid = dict.get("uuid", None)
118         self.urn = dict.get("urn", None)
119         self.hrn = urn_to_hrn(self.urn)[0]
120
121     ##
122     # Dump the credential to stdout.
123     #
124     # @param indent specifies a number of spaces to indent the output
125     # @param dump_parents If true, also dump the parents of the GID
126
127     def dump(self, indent=0, dump_parents=False):
128         print " "*indent, " hrn:", self.get_hrn()
129         print " "*indent, " urn:", self.get_urn()
130         print " "*indent, "uuid:", self.get_uuid()
131
132         if self.parent and dump_parents:
133             print " "*indent, "parent:"
134             self.parent.dump(indent+4)
135
136     ##
137     # Verify the chain of authenticity of the GID. First perform the checks
138     # of the certificate class (verifying that each parent signs the child,
139     # etc). In addition, GIDs also confirm that the parent's HRN is a prefix
140     # of the child's HRN.
141     #
142     # Verifying these prefixes prevents a rogue authority from signing a GID
143     # for a principal that is not a member of that authority. For example,
144     # planetlab.us.arizona cannot sign a GID for planetlab.us.princeton.foo.
145
146     def verify_chain(self, trusted_certs = None):
147         # do the normal certificate verification stuff
148         Certificate.verify_chain(self, trusted_certs)
149
150         if self.parent:
151             # make sure the parent's hrn is a prefix of the child's hrn
152             if not self.get_hrn().startswith(self.parent.get_hrn()):
153                 raise GidParentHrn(self.parent.get_subject())
154
155         return
156
157
158
159
160