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, urn, 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)
106 szUUID = "URI:" + uuid.UUID(int=self.uuid).urn
109 str = szURN + ", " + szUUID
110 self.set_data(str, 'subjectAltName')
114 # Decode the subject-alt-name field of the X509 certificate into the
115 # fields of the GID. This is automatically called by the various get_*()
116 # functions in this class.
119 data = self.get_data('subjectAltName')
122 if data.lower().startswith('uri:http://<params>'):
123 dict = xmlrpclib.loads(data[11:])[0][0]
125 spl = data.split(', ')
127 if val.lower().startswith('uri:urn:uuid:'):
128 dict['uuid'] = uuid.UUID(val[4:]).int
129 elif val.lower().startswith('uri:urn:publicid:idn+'):
130 dict['urn'] = val[4:]
132 self.uuid = dict.get("uuid", None)
133 self.urn = dict.get("urn", None)
134 self.hrn = dict.get("hrn", None)
136 self.hrn = urn_to_hrn(self.urn)[0]
139 # Dump the credential to stdout.
141 # @param indent specifies a number of spaces to indent the output
142 # @param dump_parents If true, also dump the parents of the GID
144 def dump(self, indent=0, dump_parents=False):
145 print " "*indent, " hrn:", self.get_hrn()
146 print " "*indent, " urn:", self.get_urn()
147 print " "*indent, "uuid:", self.get_uuid()
149 if self.parent and dump_parents:
150 print " "*indent, "parent:"
151 self.parent.dump(indent+4)
154 # Verify the chain of authenticity of the GID. First perform the checks
155 # of the certificate class (verifying that each parent signs the child,
156 # etc). In addition, GIDs also confirm that the parent's HRN is a prefix
157 # of the child's HRN.
159 # Verifying these prefixes prevents a rogue authority from signing a GID
160 # for a principal that is not a member of that authority. For example,
161 # planetlab.us.arizona cannot sign a GID for planetlab.us.princeton.foo.
163 def verify_chain(self, trusted_certs = None):
164 # do the normal certificate verification stuff
165 Certificate.verify_chain(self, trusted_certs)
168 # make sure the parent's hrn is a prefix of the child's hrn
169 if not self.get_hrn().startswith(self.parent.get_hrn()):
170 raise GidParentHrn(self.parent.get_subject())