2 # Implements SFA GID. GIDs are based on certificates, and the GID class is a
3 # descendant of the certificate class.
12 from sfa.trust.certificate import Certificate
13 from sfa.util.namespace import *
15 # Create a new uuid. Returns the UUID as a string.
18 return str(uuid.uuid4().int)
22 # (uuid, hrn, public_key)
24 # UUID is a unique identifier and is created by the python uuid module
25 # (or the utility function create_uuid() in gid.py).
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.
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
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.
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.
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.
45 class GID(Certificate):
51 # Create a new GID object
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
58 def __init__(self, create=False, subject=None, string=None, filename=None, uuid=None, hrn=None, urn=None):
60 Certificate.__init__(self, create, subject, string, filename)
67 self.hrn, type = urn_to_hrn(urn)
69 def set_uuid(self, uuid):
77 def set_hrn(self, hrn):
85 def set_urn(self, urn):
87 self.hrn, type = urn_to_hrn(urn)
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.
103 urn = hrn_to_urn(self.hrn, None)
105 dict = {"uuid": self.uuid,
107 str = xmlrpclib.dumps((dict,))
111 # Decode the subject-alt-name field of the X509 certificate into the
112 # fields of the GID. This is automatically called by the various get_*()
113 # functions in this class.
116 data = self.get_data()
118 dict = xmlrpclib.loads(self.get_data())[0][0]
122 self.uuid = dict.get("uuid", None)
123 self.urn = dict.get("urn", None)
124 self.hrn = dict.get("hrn", None)
126 self.hrn = urn_to_hrn(self.urn)[0]
129 # Dump the credential to stdout.
131 # @param indent specifies a number of spaces to indent the output
132 # @param dump_parents If true, also dump the parents of the GID
134 def dump(self, indent=0, dump_parents=False):
135 print " "*indent, " hrn:", self.get_hrn()
136 print " "*indent, " urn:", self.get_urn()
137 print " "*indent, "uuid:", self.get_uuid()
139 if self.parent and dump_parents:
140 print " "*indent, "parent:"
141 self.parent.dump(indent+4)
144 # Verify the chain of authenticity of the GID. First perform the checks
145 # of the certificate class (verifying that each parent signs the child,
146 # etc). In addition, GIDs also confirm that the parent's HRN is a prefix
147 # of the child's HRN.
149 # Verifying these prefixes prevents a rogue authority from signing a GID
150 # for a principal that is not a member of that authority. For example,
151 # planetlab.us.arizona cannot sign a GID for planetlab.us.princeton.foo.
153 def verify_chain(self, trusted_certs = None):
154 # do the normal certificate verification stuff
155 Certificate.verify_chain(self, trusted_certs)
158 # make sure the parent's hrn is a prefix of the child's hrn
159 if not self.get_hrn().startswith(self.parent.get_hrn()):
160 raise GidParentHrn(self.parent.get_subject())