1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
22 #----------------------------------------------------------------------
24 # Implements SFA GID. GIDs are based on certificates, and the GID class is a
25 # descendant of the certificate class.
32 from sfa.trust.certificate import Certificate
33 from sfa.util.namespace import *
34 from sfa.util.sfalogging import logger
37 # Create a new uuid. Returns the UUID as a string.
40 return str(uuid.uuid4().int)
44 # (uuid, urn, public_key)
46 # UUID is a unique identifier and is created by the python uuid module
47 # (or the utility function create_uuid() in gid.py).
49 # HRN is a human readable name. It is a dotted form similar to a backward domain
50 # name. For example, planetlab.us.arizona.bakers.
52 # URN is a human readable identifier of form:
53 # "urn:publicid:IDN+toplevelauthority[:sub-auth.]*[\res. type]\ +object name"
54 # For example, urn:publicid:IDN+planetlab:us:arizona+user+bakers
56 # PUBLIC_KEY is the public key of the principal identified by the UUID/HRN.
57 # It is a Keypair object as defined in the cert.py module.
59 # It is expected that there is a one-to-one pairing between UUIDs and HRN,
60 # but it is uncertain how this would be inforced or if it needs to be enforced.
62 # These fields are encoded using xmlrpc into the subjectAltName field of the
63 # x509 certificate. Note: Call encode() once the fields have been filled in
64 # to perform this encoding.
67 class GID(Certificate):
73 # Create a new GID object
75 # @param create If true, create the X509 certificate
76 # @param subject If subject!=None, create the X509 cert and set the subject name
77 # @param string If string!=None, load the GID from a string
78 # @param filename If filename!=None, load the GID from a file
80 def __init__(self, create=False, subject=None, string=None, filename=None, uuid=None, hrn=None, urn=None):
82 Certificate.__init__(self, create, subject, string, filename)
84 logger.debug("Creating GID for subject: %s" % subject)
89 self.urn = hrn_to_urn(hrn, 'unknown')
92 self.hrn, type = urn_to_hrn(urn)
94 def set_uuid(self, uuid):
95 if isinstance(uuid, str):
105 def set_hrn(self, hrn):
113 def set_urn(self, urn):
115 self.hrn, type = urn_to_hrn(urn)
125 _, t = urn_to_hrn(self.urn)
129 # Encode the GID fields and package them into the subject-alt-name field
130 # of the X509 certificate. This must be called prior to signing the
131 # certificate. It may only be called once per certificate.
137 urn = hrn_to_urn(self.hrn, None)
142 str += ", " + "URI:" + uuid.UUID(int=self.uuid).urn
144 self.set_data(str, 'subjectAltName')
150 # Decode the subject-alt-name field of the X509 certificate into the
151 # fields of the GID. This is automatically called by the various get_*()
152 # functions in this class.
155 data = self.get_data('subjectAltName')
158 if data.lower().startswith('uri:http://<params>'):
159 dict = xmlrpclib.loads(data[11:])[0][0]
161 spl = data.split(', ')
163 if val.lower().startswith('uri:urn:uuid:'):
164 dict['uuid'] = uuid.UUID(val[4:]).int
165 elif val.lower().startswith('uri:urn:publicid:idn+'):
166 dict['urn'] = val[4:]
168 self.uuid = dict.get("uuid", None)
169 self.urn = dict.get("urn", None)
170 self.hrn = dict.get("hrn", None)
172 self.hrn = urn_to_hrn(self.urn)[0]
175 # Dump the credential to stdout.
177 # @param indent specifies a number of spaces to indent the output
178 # @param dump_parents If true, also dump the parents of the GID
180 def dump(self, indent=0, dump_parents=False):
181 print " "*indent, " hrn:", self.get_hrn()
182 print " "*indent, " urn:", self.get_urn()
183 print " "*indent, "uuid:", self.get_uuid()
185 if self.parent and dump_parents:
186 print " "*indent, "parent:"
187 self.parent.dump(indent+4, dump_parents)
190 # Verify the chain of authenticity of the GID. First perform the checks
191 # of the certificate class (verifying that each parent signs the child,
192 # etc). In addition, GIDs also confirm that the parent's HRN is a prefix
193 # of the child's HRN.
195 # Verifying these prefixes prevents a rogue authority from signing a GID
196 # for a principal that is not a member of that authority. For example,
197 # planetlab.us.arizona cannot sign a GID for planetlab.us.princeton.foo.
199 def verify_chain(self, trusted_certs = None):
200 # do the normal certificate verification stuff
201 trusted_root = Certificate.verify_chain(self, trusted_certs)
204 # make sure the parent's hrn is a prefix of the child's hrn
205 if not self.get_hrn().startswith(self.parent.get_hrn()):
206 #print self.get_hrn(), " ", self.parent.get_hrn()
207 raise GidParentHrn("This cert %s HRN doesnt start with parent HRN %s" % (self.get_hrn(), self.parent.get_hrn()))
209 # make sure that the trusted root's hrn is a prefix of the child's
210 trusted_gid = GID(string=trusted_root.save_to_string())
211 trusted_type = trusted_gid.get_type()
212 trusted_hrn = trusted_gid.get_hrn()
213 #if trusted_type == 'authority':
214 # trusted_hrn = trusted_hrn[:trusted_hrn.rindex('.')]
215 cur_hrn = self.get_hrn()
216 if not self.get_hrn().startswith(trusted_hrn):
217 raise GidParentHrn("Trusted roots HRN %s isnt start of this cert %s" % (trusted_hrn, cur_hrn))