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