2 # Implements SFA GID. GIDs are based on certificates, and the GID class is a
3 # descendant of the certificate class.
11 from sfa.trust.certificate import Certificate
12 from sfa.util.namespace import *
13 from sfa.util.sfalogging import logger
16 # Create a new uuid. Returns the UUID as a string.
19 return str(uuid.uuid4().int)
23 # (uuid, urn, public_key)
25 # UUID is a unique identifier and is created by the python uuid module
26 # (or the utility function create_uuid() in gid.py).
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.
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
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.
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.
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.
46 class GID(Certificate):
52 # Create a new GID object
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
59 def __init__(self, create=False, subject=None, string=None, filename=None, uuid=None, hrn=None, urn=None):
61 Certificate.__init__(self, create, subject, string, filename)
63 logger.info("subject: %s" % subject)
70 self.hrn, type = urn_to_hrn(urn)
72 def set_uuid(self, uuid):
80 def set_hrn(self, hrn):
88 def set_urn(self, urn):
90 self.hrn, type = urn_to_hrn(urn)
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.
106 urn = hrn_to_urn(self.hrn, None)
109 szUUID = "URI:" + uuid.UUID(int=self.uuid).urn
112 str = szURN + ", " + szUUID
113 self.set_data(str, 'subjectAltName')
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.
124 data = self.get_data('subjectAltName')
127 if data.lower().startswith('uri:http://<params>'):
128 dict = xmlrpclib.loads(data[11:])[0][0]
130 spl = data.split(', ')
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:]
137 self.uuid = dict.get("uuid", None)
138 self.urn = dict.get("urn", None)
139 self.hrn = dict.get("hrn", None)
141 self.hrn = urn_to_hrn(self.urn)[0]
144 # Dump the credential to stdout.
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
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()
154 if self.parent and dump_parents:
155 print " "*indent, "parent:"
156 self.parent.dump(indent+4)
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.
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.
168 def verify_chain(self, trusted_certs = None):
169 # do the normal certificate verification stuff
170 Certificate.verify_chain(self, trusted_certs)
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())