fix attribute error
[sfa.git] / sfa / trust / credential.py
1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
3 #
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:
10 #
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
13 #
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 
21 # IN THE WORK.
22 #----------------------------------------------------------------------
23 ##
24 # Implements SFA Credentials
25 #
26 # Credentials are signed XML files that assign a subject gid privileges to an object gid
27 ##
28
29 ### $Id$
30 ### $URL$
31
32 import os
33 import datetime
34 from xml.dom.minidom import Document, parseString
35 from tempfile import mkstemp
36 from sfa.trust.certificate import Keypair
37 from sfa.trust.credential_legacy import CredentialLegacy
38 from sfa.trust.rights import *
39 from sfa.trust.gid import *
40 from sfa.util.faults import *
41
42 from sfa.util.sfalogging import logger
43 from dateutil.parser import parse
44
45
46
47 # Two years, in seconds 
48 DEFAULT_CREDENTIAL_LIFETIME = 60 * 60 * 24 * 365 * 2
49
50
51 # TODO:
52 # . make privs match between PG and PL
53 # . Need to add support for other types of credentials, e.g. tickets
54
55
56 signature_template = \
57 '''
58 <Signature xml:id="Sig_%s" xmlns="http://www.w3.org/2000/09/xmldsig#">
59     <SignedInfo>
60       <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
61       <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
62       <Reference URI="#%s">
63       <Transforms>
64         <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
65       </Transforms>
66       <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
67       <DigestValue></DigestValue>
68       </Reference>
69     </SignedInfo>
70     <SignatureValue />
71       <KeyInfo>
72         <X509Data>
73           <X509SubjectName/>
74           <X509IssuerSerial/>
75           <X509Certificate/>
76         </X509Data>
77       <KeyValue />
78       </KeyInfo>
79     </Signature>
80 '''
81
82 ##
83 # Convert a string into a bool
84
85 def str2bool(str):
86     if str.lower() in ['yes','true','1']:
87         return True
88     return False
89
90
91 ##
92 # Utility function to get the text of an XML element
93
94 def getTextNode(element, subele):
95     sub = element.getElementsByTagName(subele)[0]
96     if len(sub.childNodes) > 0:            
97         return sub.childNodes[0].nodeValue
98     else:
99         return None
100         
101 ##
102 # Utility function to set the text of an XML element
103 # It creates the element, adds the text to it,
104 # and then appends it to the parent.
105
106 def append_sub(doc, parent, element, text):
107     ele = doc.createElement(element)
108     ele.appendChild(doc.createTextNode(text))
109     parent.appendChild(ele)
110
111 ##
112 # Signature contains information about an xmlsec1 signature
113 # for a signed-credential
114 #
115
116 class Signature(object):
117    
118     def __init__(self, string=None):
119         self.refid = None
120         self.issuer_gid = None
121         self.xml = None
122         if string:
123             self.xml = string
124             self.decode()
125
126
127     def get_refid(self):
128         if not self.refid:
129             self.decode()
130         return self.refid
131
132     def get_xml(self):
133         if not self.xml:
134             self.encode()
135         return self.xml
136
137     def set_refid(self, id):
138         self.refid = id
139
140     def get_issuer_gid(self):
141         if not self.gid:
142             self.decode()
143         return self.gid        
144
145     def set_issuer_gid(self, gid):
146         self.gid = gid
147
148     def decode(self):
149         doc = parseString(self.xml)
150         sig = doc.getElementsByTagName("Signature")[0]
151         self.set_refid(sig.getAttribute("xml:id").strip("Sig_"))
152         keyinfo = sig.getElementsByTagName("X509Data")[0]
153         szgid = getTextNode(keyinfo, "X509Certificate")
154         szgid = "-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----" % szgid
155         self.set_issuer_gid(GID(string=szgid))        
156         
157     def encode(self):
158         self.xml = signature_template % (self.get_refid(), self.get_refid())
159
160
161 ##
162 # A credential provides a caller gid with privileges to an object gid.
163 # A signed credential is signed by the object's authority.
164 #
165 # Credentials are encoded in one of two ways.  The legacy style places
166 # it in the subjectAltName of an X509 certificate.  The new credentials
167 # are placed in signed XML.
168 #
169 # WARNING:
170 # In general, a signed credential obtained externally should
171 # not be changed else the signature is no longer valid.  So, once
172 # you have loaded an existing signed credential, do not call encode() or sign() on it.
173
174 class Credential(object):
175
176     ##
177     # Create a Credential object
178     #
179     # @param create If true, create a blank x509 certificate
180     # @param subject If subject!=None, create an x509 cert with the subject name
181     # @param string If string!=None, load the credential from the string
182     # @param filename If filename!=None, load the credential from the file
183     # FIXME: create and subject are ignored!
184     def __init__(self, create=False, subject=None, string=None, filename=None):
185         self.gidCaller = None
186         self.gidObject = None
187         self.expiration = None
188         self.privileges = None
189         self.issuer_privkey = None
190         self.issuer_gid = None
191         self.issuer_pubkey = None
192         self.parent = None
193         self.signature = None
194         self.xml = None
195         self.refid = None
196         self.legacy = None
197
198         # Check if this is a legacy credential, translate it if so
199         if string or filename:
200             if string:                
201                 str = string
202             elif filename:
203                 str = file(filename).read()
204                 
205             if str.strip().startswith("-----"):
206                 self.legacy = CredentialLegacy(False,string=str)
207                 self.translate_legacy(str)
208             else:
209                 self.xml = str
210                 self.decode()
211
212         # Find an xmlsec1 path
213         self.xmlsec_path = ''
214         paths = ['/usr/bin','/usr/local/bin','/bin','/opt/bin','/opt/local/bin']
215         for path in paths:
216             if os.path.isfile(path + '/' + 'xmlsec1'):
217                 self.xmlsec_path = path + '/' + 'xmlsec1'
218                 break
219
220     def get_subject(self):
221         if not self.gidObject:
222             self.decode()
223         return self.gidObject.get_subject()   
224
225     def get_signature(self):
226         if not self.signature:
227             self.decode()
228         return self.signature
229
230     def set_signature(self, sig):
231         self.signature = sig
232
233         
234     ##
235     # Translate a legacy credential into a new one
236     #
237     # @param String of the legacy credential
238
239     def translate_legacy(self, str):
240         legacy = CredentialLegacy(False,string=str)
241         self.gidCaller = legacy.get_gid_caller()
242         self.gidObject = legacy.get_gid_object()
243         lifetime = legacy.get_lifetime()
244         if not lifetime:
245             # Default to two years
246             self.set_lifetime(DEFAULT_CREDENTIAL_LIFETIME)
247         else:
248             self.set_lifetime(int(lifetime))
249         self.lifeTime = legacy.get_lifetime()
250         self.set_privileges(legacy.get_privileges())
251         self.get_privileges().delegate_all_privileges(legacy.get_delegate())
252
253     ##
254     # Need the issuer's private key and name
255     # @param key Keypair object containing the private key of the issuer
256     # @param gid GID of the issuing authority
257
258     def set_issuer_keys(self, privkey, gid):
259         self.issuer_privkey = privkey
260         self.issuer_gid = gid
261
262
263     ##
264     # Set this credential's parent
265     def set_parent(self, cred):
266         self.parent = cred
267         self.updateRefID()
268
269     ##
270     # set the GID of the caller
271     #
272     # @param gid GID object of the caller
273
274     def set_gid_caller(self, gid):
275         self.gidCaller = gid
276         # gid origin caller is the caller's gid by default
277         self.gidOriginCaller = gid
278
279     ##
280     # get the GID of the object
281
282     def get_gid_caller(self):
283         if not self.gidCaller:
284             self.decode()
285         return self.gidCaller
286
287     ##
288     # set the GID of the object
289     #
290     # @param gid GID object of the object
291
292     def set_gid_object(self, gid):
293         self.gidObject = gid
294
295     ##
296     # get the GID of the object
297
298     def get_gid_object(self):
299         if not self.gidObject:
300             self.decode()
301         return self.gidObject
302
303     ##
304     # set the lifetime of this credential
305     #
306     # @param lifetime lifetime of credential
307     # . if lifeTime is a datetime object, it is used for the expiration time
308     # . if lifeTime is an integer value, it is considered the number of seconds
309     #   remaining before expiration
310
311     def set_lifetime(self, lifeTime):
312         if isinstance(lifeTime, int):
313             self.expiration = datetime.timedelta(seconds=lifeTime) + datetime.datetime.utcnow()
314         else:
315             self.expiration = lifeTime
316
317     ##
318     # get the lifetime of the credential (in datetime format)
319
320     def get_lifetime(self):
321         if not self.expiration:
322             self.decode()
323         return self.expiration
324
325  
326     ##
327     # set the privileges
328     #
329     # @param privs either a comma-separated list of privileges of a RightList object
330
331     def set_privileges(self, privs):
332         if isinstance(privs, str):
333             self.privileges = RightList(string = privs)
334         else:
335             self.privileges = privs
336         
337
338     ##
339     # return the privileges as a RightList object
340
341     def get_privileges(self):
342         if not self.privileges:
343             self.decode()
344         return self.privileges
345
346     ##
347     # determine whether the credential allows a particular operation to be
348     # performed
349     #
350     # @param op_name string specifying name of operation ("lookup", "update", etc)
351
352     def can_perform(self, op_name):
353         rights = self.get_privileges()
354         
355         if not rights:
356             return False
357
358         return rights.can_perform(op_name)
359
360
361     ##
362     # Encode the attributes of the credential into an XML string    
363     # This should be done immediately before signing the credential.    
364     # WARNING:
365     # In general, a signed credential obtained externally should
366     # not be changed else the signature is no longer valid.  So, once
367     # you have loaded an existing signed credential, do not call encode() or sign() on it.
368
369     def encode(self):
370         # Create the XML document
371         doc = Document()
372         signed_cred = doc.createElement("signed-credential")
373         doc.appendChild(signed_cred)  
374         
375         # Fill in the <credential> bit        
376         cred = doc.createElement("credential")
377         cred.setAttribute("xml:id", self.get_refid())
378         signed_cred.appendChild(cred)
379         append_sub(doc, cred, "type", "privilege")
380         append_sub(doc, cred, "serial", "8")
381         append_sub(doc, cred, "owner_gid", self.gidCaller.save_to_string())
382         append_sub(doc, cred, "owner_urn", self.gidCaller.get_urn())
383         append_sub(doc, cred, "target_gid", self.gidObject.save_to_string())
384         append_sub(doc, cred, "target_urn", self.gidObject.get_urn())
385         append_sub(doc, cred, "uuid", "")
386         if not self.expiration:
387             self.set_lifetime(DEFAULT_CREDENTIAL_LIFETIME)
388         self.expiration = self.expiration.replace(microsecond=0)
389         append_sub(doc, cred, "expires", self.expiration.isoformat())
390         privileges = doc.createElement("privileges")
391         cred.appendChild(privileges)
392
393         if self.privileges:
394             rights = self.get_privileges()
395             for right in rights.rights:
396                 priv = doc.createElement("privilege")
397                 append_sub(doc, priv, "name", right.kind)
398                 append_sub(doc, priv, "can_delegate", str(right.delegate).lower())
399                 privileges.appendChild(priv)
400
401         # Add the parent credential if it exists
402         if self.parent:
403             sdoc = parseString(self.parent.get_xml())
404             p_cred = doc.importNode(sdoc.getElementsByTagName("credential")[0], True)
405             p = doc.createElement("parent")
406             p.appendChild(p_cred)
407             cred.appendChild(p)
408
409
410         # Create the <signatures> tag
411         signatures = doc.createElement("signatures")
412         signed_cred.appendChild(signatures)
413
414         # Add any parent signatures
415         if self.parent:
416             for cur_cred in self.get_credential_list()[1:]:
417                 sdoc = parseString(cur_cred.get_signature().get_xml())
418                 ele = doc.importNode(sdoc.getElementsByTagName("Signature")[0], True)
419                 signatures.appendChild(ele)
420                 
421         # Get the finished product
422         self.xml = doc.toxml()
423
424
425     def save_to_random_tmp_file(self):       
426         fp, filename = mkstemp(suffix='cred', text=True)
427         fp = os.fdopen(fp, "w")
428         self.save_to_file(filename, save_parents=True, filep=fp)
429         return filename
430     
431     def save_to_file(self, filename, save_parents=True, filep=None):
432         if not self.xml:
433             self.encode()
434         if filep:
435             f = filep 
436         else:
437             f = open(filename, "w")
438         f.write(self.xml)
439         f.close()
440
441     def save_to_string(self, save_parents=True):
442         if not self.xml:
443             self.encode()
444         return self.xml
445
446     def get_refid(self):
447         if not self.refid:
448             self.refid = 'ref0'
449         return self.refid
450
451     def set_refid(self, rid):
452         self.refid = rid
453
454     ##
455     # Figure out what refids exist, and update this credential's id
456     # so that it doesn't clobber the others.  Returns the refids of
457     # the parents.
458     
459     def updateRefID(self):
460         if not self.parent:
461             self.set_refid('ref0')
462             return []
463         
464         refs = []
465
466         next_cred = self.parent
467         while next_cred:
468             refs.append(next_cred.get_refid())
469             if next_cred.parent:
470                 next_cred = next_cred.parent
471             else:
472                 next_cred = None
473
474         
475         # Find a unique refid for this credential
476         rid = self.get_refid()
477         while rid in refs:
478             val = int(rid[3:])
479             rid = "ref%d" % (val + 1)
480
481         # Set the new refid
482         self.set_refid(rid)
483
484         # Return the set of parent credential ref ids
485         return refs
486
487     def get_xml(self):
488         if not self.xml:
489             self.encode()
490         return self.xml
491
492     ##
493     # Sign the XML file created by encode()
494     #
495     # WARNING:
496     # In general, a signed credential obtained externally should
497     # not be changed else the signature is no longer valid.  So, once
498     # you have loaded an existing signed credential, do not call encode() or sign() on it.
499
500     def sign(self):
501         if not self.issuer_privkey or not self.issuer_gid:
502             return
503         doc = parseString(self.get_xml())
504         sigs = doc.getElementsByTagName("signatures")[0]
505
506         # Create the signature template to be signed
507         signature = Signature()
508         signature.set_refid(self.get_refid())
509         sdoc = parseString(signature.get_xml())        
510         sig_ele = doc.importNode(sdoc.getElementsByTagName("Signature")[0], True)
511         sigs.appendChild(sig_ele)
512
513         self.xml = doc.toxml()
514
515
516         # Split the issuer GID into multiple certificates if it's a chain
517         chain = GID(filename=self.issuer_gid)
518         gid_files = []
519         while chain:
520             gid_files.append(chain.save_to_random_tmp_file(False))
521             if chain.get_parent():
522                 chain = chain.get_parent()
523             else:
524                 chain = None
525
526
527         # Call out to xmlsec1 to sign it
528         ref = 'Sig_%s' % self.get_refid()
529         filename = self.save_to_random_tmp_file()
530         signed = os.popen('%s --sign --node-id "%s" --privkey-pem %s,%s %s' \
531                  % (self.xmlsec_path, ref, self.issuer_privkey, ",".join(gid_files), filename)).read()
532         os.remove(filename)
533
534         for gid_file in gid_files:
535             os.remove(gid_file)
536
537         self.xml = signed
538
539         # This is no longer a legacy credential
540         if self.legacy:
541             self.legacy = None
542
543         # Update signatures
544         self.decode()       
545
546         
547     ##
548     # Retrieve the attributes of the credential from the XML.
549     # This is automatically called by the various get_* methods of
550     # this class and should not need to be called explicitly.
551
552     def decode(self):
553         if not self.xml:
554             return
555         doc = parseString(self.xml)
556         sigs = []
557         signed_cred = doc.getElementsByTagName("signed-credential")
558
559         # Is this a signed-cred or just a cred?
560         if len(signed_cred) > 0:
561             cred = signed_cred[0].getElementsByTagName("credential")[0]
562             signatures = signed_cred[0].getElementsByTagName("signatures")
563             if len(signatures) > 0:
564                 sigs = signatures[0].getElementsByTagName("Signature")
565         else:
566             cred = doc.getElementsByTagName("credential")[0]
567         
568
569         self.set_refid(cred.getAttribute("xml:id"))
570         self.set_lifetime(parse(getTextNode(cred, "expires")))
571         self.gidCaller = GID(string=getTextNode(cred, "owner_gid"))
572         self.gidObject = GID(string=getTextNode(cred, "target_gid"))   
573
574
575         # Process privileges
576         privs = cred.getElementsByTagName("privileges")[0]
577         rlist = RightList()
578         for priv in privs.getElementsByTagName("privilege"):
579             kind = getTextNode(priv, "name")
580             deleg = str2bool(getTextNode(priv, "can_delegate"))
581             if kind == '*':
582                 # Convert * into the default privileges for the credential's type                
583                 _ , type = urn_to_hrn(self.gidObject.get_urn())
584                 rl = rlist.determine_rights(type, self.gidObject.get_urn())
585                 for r in rl.rights:
586                     rlist.add(r)
587             else:
588                 rlist.add(Right(kind.strip(), deleg))
589         self.set_privileges(rlist)
590
591
592         # Is there a parent?
593         parent = cred.getElementsByTagName("parent")
594         if len(parent) > 0:
595             parent_doc = parent[0].getElementsByTagName("credential")[0]
596             parent_xml = parent_doc.toxml()
597             self.parent = Credential(string=parent_xml)
598             self.updateRefID()
599
600         # Assign the signatures to the credentials
601         for sig in sigs:
602             Sig = Signature(string=sig.toxml())
603
604             for cur_cred in self.get_credential_list():
605                 if cur_cred.get_refid() == Sig.get_refid():
606                     cur_cred.set_signature(Sig)
607                                     
608             
609     ##
610     # Verify
611     #   trusted_certs: A list of trusted GID filenames (not GID objects!) 
612     #                  Chaining is not supported within the GIDs by xmlsec1.
613     #    
614     # Verify that:
615     # . All of the signatures are valid and that the issuers trace back
616     #   to trusted roots (performed by xmlsec1)
617     # . The XML matches the credential schema
618     # . That the issuer of the credential is the authority in the target's urn
619     #    . In the case of a delegated credential, this must be true of the root
620     # . That all of the gids presented in the credential are valid
621     # . The credential is not expired
622     #
623     # -- For Delegates (credentials with parents)
624     # . The privileges must be a subset of the parent credentials
625     # . The privileges must have "can_delegate" set for each delegated privilege
626     # . The target gid must be the same between child and parents
627     # . The expiry time on the child must be no later than the parent
628     # . The signer of the child must be the owner of the parent
629     #
630     # -- Verify does *NOT*
631     # . ensure that an xmlrpc client's gid matches a credential gid, that
632     #   must be done elsewhere
633     #
634     # @param trusted_certs: The certificates of trusted CA certificates
635     def verify(self, trusted_certs):
636         if not self.xml:
637             self.decode()        
638
639 #        trusted_cert_objects = [GID(filename=f) for f in trusted_certs]
640         trusted_cert_objects = []
641         ok_trusted_certs = []
642         for f in trusted_certs:
643             try:
644                 # Failures here include unreadable files
645                 # or non PEM files
646                 trusted_cert_objects.append(GID(filename=f))
647                 ok_trusted_certs.append(f)
648             except Exception, exc:
649                 logger.error("Failed to load trusted cert from %s: %r", f, exc)
650         trusted_certs = ok_trusted_certs
651
652         # Use legacy verification if this is a legacy credential
653         if self.legacy:
654             self.legacy.verify_chain(trusted_cert_objects)
655             if self.legacy.client_gid:
656                 self.legacy.client_gid.verify_chain(trusted_cert_objects)
657             if self.legacy.object_gid:
658                 self.legacy.object_gid.verify_chain(trusted_cert_objects)
659             return True
660         
661         # make sure it is not expired
662         if self.get_lifetime() < datetime.datetime.utcnow():
663             raise CredentialNotVerifiable("Credential expired at %s" % self.expiration.isoformat())
664
665         # Verify the signatures
666         filename = self.save_to_random_tmp_file()
667         cert_args = " ".join(['--trusted-pem %s' % x for x in trusted_certs])
668
669         # Verify the gids of this cred and of its parents
670         for cur_cred in self.get_credential_list():
671             cur_cred.get_gid_object().verify_chain(trusted_cert_objects)
672             cur_cred.get_gid_caller().verify_chain(trusted_cert_objects) 
673
674         refs = []
675         refs.append("Sig_%s" % self.get_refid())
676
677         parentRefs = self.updateRefID()
678         for ref in parentRefs:
679             refs.append("Sig_%s" % ref)
680
681         for ref in refs:
682             verified = os.popen('%s --verify --node-id "%s" %s %s 2>&1' \
683                             % (self.xmlsec_path, ref, cert_args, filename)).read()
684             if not verified.strip().startswith("OK"):
685                 raise CredentialNotVerifiable("xmlsec1 error verifying cert: " + verified)
686         os.remove(filename)
687
688         # Verify the parents (delegation)
689         if self.parent:
690             self.verify_parent(self.parent)
691
692         # Make sure the issuer is the target's authority
693         self.verify_issuer()
694         return True
695
696     ##
697     # Creates a list of the credential and its parents, with the root 
698     # (original delegated credential) as the last item in the list
699     def get_credential_list(self):    
700         cur_cred = self
701         list = []
702         while cur_cred:
703             list.append(cur_cred)
704             if cur_cred.parent:
705                 cur_cred = cur_cred.parent
706             else:
707                 cur_cred = None
708         return list
709     
710     ##
711     # Make sure the credential's target gid was signed by (or is the same) the entity that signed
712     # the original credential or an authority over that namespace.
713     def verify_issuer(self):                
714         root_cred = self.get_credential_list()[-1]
715         root_target_gid = root_cred.get_gid_object()
716         root_cred_signer = root_cred.get_signature().get_issuer_gid()
717
718         if root_target_gid.is_signed_by_cert(root_cred_signer):
719             # cred signer matches target signer, return success
720             return
721
722         root_target_gid_str = root_target_gid.save_to_string()
723         root_cred_signer_str = root_cred_signer.save_to_string()
724         if root_target_gid_str == root_cred_signer_str:
725             # cred signer is target, return success
726             return
727
728         # See if it the signer is an authority over the domain of the target
729         # Maybe should be (hrn, type) = urn_to_hrn(root_cred_signer.get_urn())
730         root_cred_signer_type = root_cred_signer.get_type()
731         if (root_cred_signer_type == 'authority'):
732             #logger.debug('Cred signer is an authority')
733             # signer is an authority, see if target is in authority's domain
734             hrn = root_cred_signer.get_hrn()
735             if root_target_gid.get_hrn().startswith(hrn):
736                 return
737
738         # We've required that the credential be signed by an authority
739         # for that domain. Reasonable and probably correct.
740         # A looser model would also allow the signer to be an authority
741         # in my control framework - eg My CA or CH. Even if it is not
742         # the CH that issued these, eg, user credentials.
743
744         # Give up, credential does not pass issuer verification
745
746         raise CredentialNotVerifiable("Could not verify credential owned by %s for object %s. Cred signer %s not the trusted authority for Cred target %s" % (self.gidCaller.get_urn(), self.gidObject.get_urn(), root_cred_signer.get_hrn(), root_target_gid.get_hrn()))
747
748
749     ##
750     # -- For Delegates (credentials with parents) verify that:
751     # . The privileges must be a subset of the parent credentials
752     # . The privileges must have "can_delegate" set for each delegated privilege
753     # . The target gid must be the same between child and parents
754     # . The expiry time on the child must be no later than the parent
755     # . The signer of the child must be the owner of the parent        
756     def verify_parent(self, parent_cred):
757         # make sure the rights given to the child are a subset of the
758         # parents rights (and check delegate bits)
759         if not parent_cred.get_privileges().is_superset(self.get_privileges()):
760             raise ChildRightsNotSubsetOfParent(
761                 self.parent.get_privileges().save_to_string() + " " +
762                 self.get_privileges().save_to_string())
763
764         # make sure my target gid is the same as the parent's
765         if not parent_cred.get_gid_object().save_to_string() == \
766            self.get_gid_object().save_to_string():
767             raise CredentialNotVerifiable("Target gid not equal between parent and child")
768
769         # make sure my expiry time is <= my parent's
770         if not parent_cred.get_lifetime() >= self.get_lifetime():
771             raise CredentialNotVerifiable("Delegated credential expires after parent")
772
773         # make sure my signer is the parent's caller
774         if not parent_cred.get_gid_caller().save_to_string(False) == \
775            self.get_signature().get_issuer_gid().save_to_string(False):
776             raise CredentialNotVerifiable("Delegated credential not signed by parent caller")
777                 
778         # Recurse
779         if parent_cred.parent:
780             parent_cred.verify_parent(parent_cred.parent)
781
782
783     def delegate(self, delegee_gid, keyfile):
784         """
785         Return a delegated copy of this credential, delegated to the 
786         specified gid's user.    
787         """
788         # get the gid of the object we are delegating
789         object_gid = self.get_gid_object()
790         object_hrn = object_gid.get_hrn()        
791  
792         # the hrn of the user who will be delegated to
793         if isinstance(delegee_gid, str):
794             delegee_gid = GID(string=records[0]['gid'])
795         delegee_hrn = delegee_gid.get_hrn()
796    
797         user_key = Keypair(filename=keyfile)
798         user_hrn = self.get_gid_caller().get_hrn()
799         subject_string = "%s delegated to %s" % (object_hrn, delegee_hrn)
800         dcred = Credential(subject=subject_string)
801         dcred.set_gid_caller(delegee_gid)
802         dcred.set_gid_object(object_gid)
803         privs = self.get_privileges()
804         dcred.set_privileges(self.get_privileges())
805         dcred.get_privileges().delegate_all_privileges(True)
806         dcred.set_pubkey(object_gid.get_pubkey())
807         dcred.set_issuer(user_key, user_hrn)
808         dcred.set_parent(self)
809         dcred.encode()
810         dcred.sign()
811
812         return dcred 
813     ##
814     # Dump the contents of a credential to stdout in human-readable format
815     #
816     # @param dump_parents If true, also dump the parent certificates
817
818     def dump(self, dump_parents=False):
819         print "CREDENTIAL", self.get_subject()
820
821         print "      privs:", self.get_privileges().save_to_string()
822
823         print "  gidCaller:"
824         gidCaller = self.get_gid_caller()
825         if gidCaller:
826             gidCaller.dump(8, dump_parents)
827
828         print "  gidObject:"
829         gidObject = self.get_gid_object()
830         if gidObject:
831             gidObject.dump(8, dump_parents)
832
833
834         if self.parent and dump_parents:
835             print "PARENT",
836             self.parent.dump_parents()
837