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