new credentials are now working.. at least for list and get_resources
[sfa.git] / sfa / trust / credential.py
1 ##
2 # Implements SFA Credentials
3 #
4 # Credentials are signed XML files that assign a subject gid privileges to an object gid
5 ##
6
7 ### $Id$
8 ### $URL$
9
10 import xmlrpclib
11 import random
12 import os
13 import datetime
14
15 from xml.dom.minidom import Document, parseString
16 from sfa.trust.credential_legacy import CredentialLegacy
17 from sfa.trust.certificate import Certificate
18 from sfa.trust.rights import *
19 from sfa.trust.gid import *
20 from sfa.util.faults import *
21 from sfa.util.sfalogging import logger
22
23
24 # TODO:
25 # . Need to implement full verification (parent signatures etc).
26 # . remove verify_chain
27 # . make delegation per privilege instead of global
28 # . make privs match between PG and PL
29 # . what about tickets?  do they need to be redone to be like credentials?
30 # . Need to test delegation, xml verification
31
32 signature_template = \
33 '''
34 <Signature xml:id="Sig_%s" xmlns="http://www.w3.org/2000/09/xmldsig#">
35     <SignedInfo>
36       <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
37       <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
38       <Reference URI="#%s">
39       <Transforms>
40         <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
41       </Transforms>
42       <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
43       <DigestValue></DigestValue>
44       </Reference>
45     </SignedInfo>
46     <SignatureValue />
47       <KeyInfo>
48         <X509Data>
49           <X509SubjectName/>
50           <X509IssuerSerial/>
51           <X509Certificate/>
52         </X509Data>
53       <KeyValue />
54       </KeyInfo>
55     </Signature>
56 '''
57
58
59
60 ##
61 # Utility function to get the text of an XML element
62 #
63 def getTextNode(element, subele):
64     sub = element.getElementsByTagName(subele)[0]
65     if len(sub.childNodes) > 0:            
66         return sub.childNodes[0].nodeValue
67     else:
68         return None
69         
70
71
72 ##
73 # Signature contains information about an xmlsec1 signature
74 # for a signed-credential
75
76 class Signature(object):
77     refid = None
78     issuer_gid = None
79     xml = None
80     
81     def __init__(self, string=None):
82         if string:
83             self.xml = string
84             self.decode()
85
86
87
88     def get_refid(self):
89         if not self.refid:
90             self.decode()
91         return self.refid
92
93     def get_xml(self):
94         if not self.xml:
95             self.encode()
96         return self.xml
97
98     def set_refid(self, id):
99         self.refid = id
100
101     def get_issuer_gid(self):
102         if not self.gid:
103             self.decode()
104         return self.gid        
105
106     def set_issuer_gid(self, gid):
107         self.gid = gid
108
109     def decode(self):
110         doc = parseString(self.xml)
111         sig = doc.getElementsByTagName("Signature")[0]
112         self.set_refid(sig.getAttribute("xml:id").strip("Sig_"))
113         keyinfo = sig.getElementsByTagName("X509Data")[0]
114         szgid = getTextNode(keyinfo, "X509Certificate")
115         szgid = "-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----" % szgid
116         self.set_issuer_gid(GID(string=szgid))        
117         
118     def encode(self):
119         self.xml = signature_template % (self.get_refid(), self.get_refid())
120
121
122 ##
123 # Credential is a tuple:
124 #    (GIDCaller, GIDObject, Expiration (in UTC time), Privileges)
125 #
126 # These fields are encoded in one of two ways.  The legacy style places
127 # it in the subjectAltName of an X509 certificate.  The new credentials
128 # are placed in signed XML.
129
130
131 class Credential(object):
132     gidCaller = None
133     gidObject = None
134     expiration = None
135     privileges = None
136     issuer_privkey = None
137     issuer_gid = None
138     issuer_pubkey = None
139     parent_xml = None
140     signatures = []
141     xml = None
142     refid = None
143     
144     ##
145     # Create a Credential object
146     #
147     # @param create If true, create a blank x509 certificate
148     # @param subject If subject!=None, create an x509 cert with the subject name
149     # @param string If string!=None, load the credential from the string
150     # @param filename If filename!=None, load the credential from the file
151
152     def __init__(self, create=False, subject=None, string=None, filename=None):
153
154         # Check if this is a legacy credential, translate it if so
155         if string or filename:
156             if string:                
157                 str = string
158             elif filename:
159                 str = file(filename).read()
160                 
161             if str.strip().startswith("-----"):
162                 self.translate_legacy(str)
163             else:
164                 self.xml = str
165                 self.decode()
166                 
167     ##
168     # Translate a legacy credential into a new one
169     #
170     # @param String of the legacy credential
171
172     def translate_legacy(self, str):
173         legacy = CredentialLegacy(False,string=str)
174         self.gidCaller = legacy.get_gid_caller()
175         self.gidObject = legacy.get_gid_object()
176         lifetime = legacy.get_lifetime()
177         if not lifetime:
178             # Default to two years
179             self.set_lifetime(1051200)
180         else:
181             self.set_lifetime(int(lifetime))
182         self.lifeTime = legacy.get_lifetime()
183         self.set_privileges(legacy.get_privileges())
184         self.get_privileges().delegate_all_privileges(legacy.get_delegate())
185
186     ##
187     # Need the issuer's private key and name
188     # @param key Keypair object containing the private key of the issuer
189     # @param gid GID of the issuing authority
190
191     def set_issuer_keys(self, privkey, gid):
192         self.issuer_privkey = privkey
193         self.issuer_gid = gid
194
195     #def set_issuer(self, issuer):
196     #    issuer = issuer
197
198     #def set_subject(self, subject):
199     #    subject = subject
200         
201     #def set_pubkey(self, pubkey):
202     #    self.issuer_pubkey = pubkey
203
204
205     ##
206     # Store the parent's credential in self.parent_xml
207     # Store the parent's signatures in self.signatures
208     # Update this credential's refid
209     def set_parent(self, cred):
210         if not cred.xml:
211             cred.encode()
212
213         doc = parseString(cred.xml)
214         signed = doc.getElementsByTagName("signed-credential")[0]
215         cred = signed.getElementsByTagName("credential")[0]
216         signatures = signed.getElementsByTagName("signatures")[0]
217         sigs = signatures.getElementsByTagName("Signature")
218
219         self.parent_xml = cred.toxml()
220         self.signatures = []
221         for sig in sigs:
222             self.signatures.append(Signature(string=sig.toxml()))
223
224         self.updateRefID()
225         
226
227     ##
228     # set the GID of the caller
229     #
230     # @param gid GID object of the caller
231
232     def set_gid_caller(self, gid):
233         self.gidCaller = gid
234         # gid origin caller is the caller's gid by default
235         self.gidOriginCaller = gid
236
237     ##
238     # get the GID of the object
239
240     def get_gid_caller(self):
241         if not self.gidCaller:
242             self.decode()
243         return self.gidCaller
244
245     ##
246     # set the GID of the object
247     #
248     # @param gid GID object of the object
249
250     def set_gid_object(self, gid):
251         self.gidObject = gid
252
253     ##
254     # get the GID of the object
255
256     def get_gid_object(self):
257         if not self.gidObject:
258             self.decode()
259         return self.gidObject
260
261     ##
262     # set the lifetime of this credential
263     #
264     # @param lifetime lifetime of credential
265     # . if lifeTime is a datetime object, it is used for the expiration time
266     # . if lifeTime is an integer value, it is considered the number of minutes
267     #   remaining before expiration
268
269     def set_lifetime(self, lifeTime):
270         if isinstance(lifeTime, int):
271             self.expiration = datetime.timedelta(seconds=lifeTime*60) + datetime.datetime.utcnow()
272         else:
273             self.expiration = lifeTime
274
275     ##
276     # get the lifetime of the credential (in minutes)
277
278     def get_lifetime(self):
279         if not self.lifeTime:
280             self.decode()
281         return self.expiration
282
283  
284     ##
285     # set the privileges
286     #
287     # @param privs either a comma-separated list of privileges of a RightList object
288
289     def set_privileges(self, privs):
290         if isinstance(privs, str):
291             self.privileges = RightList(string = privs)
292         else:
293             self.privileges = privs
294         
295
296     ##
297     # return the privileges as a RightList object
298
299     def get_privileges(self):
300         if not self.privileges:
301             self.decode()
302         return self.privileges
303
304     ##
305     # determine whether the credential allows a particular operation to be
306     # performed
307     #
308     # @param op_name string specifying name of operation ("lookup", "update", etc)
309
310     def can_perform(self, op_name):
311         rights = self.get_privileges()
312         
313         if not rights:
314             return False
315
316         return rights.can_perform(op_name)
317
318     def append_sub(self, doc, parent, element, text):
319         ele = doc.createElement(element)
320         ele.appendChild(doc.createTextNode(text))
321         parent.appendChild(ele)
322
323     ##
324     # Encode the attributes of the credential into an XML string    
325     # This should be done immediately before signing the credential.    
326
327     def encode(self):
328         p_sigs = None
329
330         # Create the XML document
331         doc = Document()
332         signed_cred = doc.createElement("signed-credential")
333         doc.appendChild(signed_cred)  
334         
335         # Fill in the <credential> bit        
336         cred = doc.createElement("credential")
337         cred.setAttribute("xml:id", self.get_refid())
338         signed_cred.appendChild(cred)
339         self.append_sub(doc, cred, "type", "privilege")
340         self.append_sub(doc, cred, "serial", "8")
341         self.append_sub(doc, cred, "owner_gid", self.gidCaller.save_to_string())
342         self.append_sub(doc, cred, "owner_urn", self.gidCaller.get_urn())
343         self.append_sub(doc, cred, "target_gid", self.gidObject.save_to_string())
344         self.append_sub(doc, cred, "target_urn", self.gidObject.get_urn())
345         self.append_sub(doc, cred, "uuid", "")
346         if  not self.expiration:
347             self.set_lifetime(3600)
348         self.expiration = self.expiration.replace(microsecond=0)
349         self.append_sub(doc, cred, "expires", self.expiration.isoformat())
350         privileges = doc.createElement("privileges")
351         cred.appendChild(privileges)
352
353         if self.privileges:
354             rights = self.get_privileges()
355             for right in rights.rights:
356                 priv = doc.createElement("privilege")
357                 self.append_sub(doc, priv, "name", right.kind)
358                 self.append_sub(doc, priv, "can_delegate", str(right.delegate))
359                 privileges.appendChild(priv)
360
361         # Add the parent credential if it exists
362         if self.parent_xml:
363             sdoc = parseString(self.parent_xml)
364             p_cred = doc.importNode(sdoc.getElementsByTagName("credential")[0], True)
365             p = doc.createElement("parent")
366             p.appendChild(p_cred)
367             cred.appendChild(p)
368
369
370         # Create the <signatures> tag
371         signatures = doc.createElement("signatures")
372         signed_cred.appendChild(signatures)
373
374         # Add any parent signatures
375         if self.parent_xml:
376             for sig in self.signatures:
377                 sdoc = parseString(sig.get_xml())
378                 ele = doc.importNode(sdoc.getElementsByTagName("Signature")[0], True)
379                 signatures.appendChild(ele)
380
381         # Get the finished product
382         self.xml = doc.toxml()
383         #print doc.toxml()
384         #self.sign()
385
386
387     def save_to_random_tmp_file(self):
388         filename = "/tmp/cred_%d" % random.randint(0,999999999)
389         self.save_to_file(filename)
390         return filename
391     
392     def save_to_file(self, filename, save_parents=True):
393         if not self.xml:
394             self.encode()
395         f = open(filename, "w")
396         f.write(self.xml)
397         f.close()
398
399     def save_to_string(self, save_parents=True):
400         if not self.xml:
401             self.encode()
402         return self.xml
403
404     def get_refid(self):
405         if not self.refid:
406             self.refid = 'ref0'
407         return self.refid
408
409     def set_refid(self, rid):
410         self.refid = rid
411
412     ##
413     # Figure out what refids exist, and update this credential's id
414     # so that it doesn't clobber the others.  Returns the refids of
415     # the parents.
416     
417     def updateRefID(self):
418         if not self.parent_xml:
419             self.set_refid('ref0')
420             return []
421         
422         refs = []
423
424         next_cred = Credential(string=self.parent_xml)
425         while next_cred:
426             refs.append(next_cred.get_refid())
427             if next_cred.parent_xml:
428                 next_cred = Credential(string=next_cred.parent_xml)
429             else:
430                 next_cred = None
431         
432         # Find a unique refid for this credential
433         rid = self.get_refid()
434         while rid in refs:
435             val = int(rid[3:])
436             rid = "ref%d" % (val + 1)
437
438         # Set the new refid
439         self.set_refid(rid)
440
441         # Return the set of parent credential ref ids
442         return refs
443
444     def get_xml(self):
445         if not self.xml:
446             self.encode()
447         return self.xml
448
449     def sign(self):
450         if not self.issuer_privkey or not self.issuer_gid:
451             return
452         
453         doc = parseString(self.get_xml())
454         sigs = doc.getElementsByTagName("signatures")[0]
455
456         # Create the signature template to be signed
457         signature = Signature()
458         signature.set_refid(self.get_refid())
459         sdoc = parseString(signature.get_xml())        
460         sig_ele = doc.importNode(sdoc.getElementsByTagName("Signature")[0], True)
461         sigs.appendChild(sig_ele)
462
463         self.xml = doc.toxml()
464
465         # Call out to xmlsec1 to sign it
466         ref = 'Sig_%s' % self.get_refid()
467         filename = self.save_to_random_tmp_file()
468         signed = os.popen('/usr/bin/xmlsec1 --sign --node-id "%s" --privkey-pem %s,%s %s' \
469                  % (ref, self.issuer_privkey, self.issuer_gid, filename)).read()
470         os.remove(filename)
471
472
473         self.xml = signed
474
475     def getTextNode(self, element, subele):
476         sub = element.getElementsByTagName(subele)[0]
477         if len(sub.childNodes) > 0:            
478             return sub.childNodes[0].nodeValue
479         else:
480             return None
481         
482     ##
483     # Retrieve the attributes of the credential from the XML.
484     # This is automatically caleld by the various get_* methods of
485     # this class and should not need to be called explicitly.
486
487     def decode(self):
488         doc = parseString(self.xml)
489         sigs = None
490         signed_cred = doc.getElementsByTagName("signed-credential")
491
492         # Is this a signed-cred or just a cred?
493         if len(signed_cred) > 0:
494             cred = signed_cred[0].getElementsByTagName("credential")[0]
495             signatures = signed_cred[0].getElementsByTagName("signatures")
496             if len(signatures) > 0:
497                 sigs = signatures[0].getElementsByTagName("Signature")
498         else:
499             cred = doc.getElementsByTagName("credential")[0]
500         
501
502
503         self.set_refid(cred.getAttribute("xml:id"))
504         sz_expires = getTextNode(cred, "expires")
505         if sz_expires != '':
506             self.expiration = datetime.datetime.strptime(sz_expires, '%Y-%m-%dT%H:%M:%S')            
507         self.lifeTime = getTextNode(cred, "expires")
508         self.gidCaller = GID(string=getTextNode(cred, "owner_gid"))
509         self.gidObject = GID(string=getTextNode(cred, "target_gid"))
510         privs = cred.getElementsByTagName("privileges")[0]
511         sz_privs = ''
512         delegates = []
513         for priv in privs.getElementsByTagName("privilege"):
514             sz_privs += getTextNode(priv, "name")
515             sz_privs += ","
516             delegates.append(getTextNode(priv, "can_delegate"))
517
518         # Can we delegate?
519         delegate = False
520         if "false" not in delegates:
521             self.delegate = True
522
523         # Make the rights list
524         sz_privs.rstrip(", ")
525         self.privileges = RightList(string=sz_privs)
526         self.delegate
527
528         # Is there a parent?
529         parent = cred.getElementsByTagName("parent")
530         if len(parent) > 0:
531             self.parent_xml = getTextNode(cred, "parent")
532             self.updateRefID()
533
534         # Get the signatures
535         for sig in sigs:
536             self.signatures.append(Signature(string=sig.toxml()))
537             
538     ##
539     # Verify that:
540     # . All of the signatures are valid and that the issuers trace back
541     #   to trusted roots (performed by xmlsec1)
542     # . That the issuer of the credential is the authority in the target's urn
543     #    . In the case of a delegated credential, this must be true of the root
544     # . That all of the gids presented in the credential are valid
545     #
546     # -- For Delegates (credentials with parents)
547     # . The privileges must be a subset of the parent credentials
548     # . The privileges must have "can_delegate" set for each delegated privilege
549     # . The target gid must be the same between child and parents
550     # . The expiry time on the child must be no later than the parent
551     # . The signer of the child must be the owner of the parent
552     #
553     # -- Verify does *NOT*
554     # . ensure that an xmlrpc client's gid matches a credential gid, that
555     #   must be done elsewhere
556     #
557     # @param trusted_certs: The certificates of trusted CA certificates
558     
559     def verify(self, trusted_certs):
560         if not self.xml:
561             self.decode()        
562
563         # Verify the signatures
564         filename = self.save_to_random_tmp_file()
565         cert_args = " ".join(['--trusted-pem %s' % x for x in trusted_certs])
566
567         # Verify the gids of this cred and of its parents
568         trusted_cert_objects = [GID(filename=f) for f in trusted_certs]
569
570         cur_cred = self
571         while cur_cred:
572             cur_cred.get_gid_object().verify_chain(trusted_cert_objects)
573             cur_cred.get_gid_caller().verify_chain(trusted_cert_objects)
574             if self.parent_xml:
575                 cur_cred = Credential(string=self.parent_xml)
576             else:
577                 cur_cred = None
578         
579         refs = []
580         refs.append("Sig_%s" % self.get_refid())
581
582         parentRefs = self.updateRefID()
583         for ref in parentRefs:
584             refs.append("Sig_%s" % ref)
585
586         for ref in refs:
587             verified = os.popen('/usr/bin/xmlsec1 --verify --node-id "%s" %s %s 2>&1' \
588                             % (ref, cert_args, filename)).read()
589             if not verified.strip().startswith("OK"):
590                 raise CredentialNotVerifiable("xmlsec1 error: " + verified)
591
592         os.remove(filename)
593
594         # Verify the parents (delegation)
595         if self.parent_xml:
596             self.verify_parent(Credential(string=self.parent_xml))
597
598         # Make sure the issuer is the target's authority
599         self.verify_issuer()
600
601
602
603     ##
604     # Make sure the issuer of this credential is the target's authority
605     def verify_issuer(self):        
606         target_authority = get_authority(self.get_gid_object().get_urn())
607
608         # Find the root credential's refid
609         cur_cred = self
610         root_refid = None
611         while cur_cred:            
612             if cur_cred.parent_xml:
613                 cur_cred = Credential(string=cur_cred.parent_xml)
614             else:
615                 root_refid = cur_cred.get_refid()                
616                 cur_cred = None
617
618         # Find the signature for the root credential
619         root_issuer = None
620         for sig in self.signatures:
621             if sig.get_refid().lower() == root_refid.lower():
622                 root_issuer = sig.get_issuer_gid().get_urn()
623                 
624                 
625         # Ensure that the signer of the root credential is the target_authority
626         target_authority = hrn_to_urn(target_authority, 'authority')
627
628         if root_issuer != target_authority:
629             raise CredentialNotVerifiable("issuer (%s) != authority of target (%s)" \
630                                           % (root_issuer, target_authority))
631
632     ##
633     # -- For Delegates (credentials with parents) verify that:
634     # . The privileges must be a subset of the parent credentials
635     # . The privileges must have "can_delegate" set for each delegated privilege
636     # . The target gid must be the same between child and parents
637     # . The expiry time on the child must be no later than the parent
638     # . The signer of the child must be the owner of the parent
639         
640     def verify_parent(self, parent_cred):
641         # make sure the rights given to the child are a subset of the
642         # parents rights (and check delegate bits)
643         if not parent_cred.get_privileges().is_superset(self.get_privileges()):
644             raise ChildRightsNotSubsetOfParent(
645                 self.parent.get_privileges().save_to_string() + " " +
646                 self.get_privileges().save_to_string())
647         
648         if parent_cred.parent_xml:
649             parent_cred.verify_parent(Credential(string=parent_cred.parent_xml))
650
651     ##
652     # Dump the contents of a credential to stdout in human-readable format
653     #
654     # @param dump_parents If true, also dump the parent certificates
655
656     def dump(self, dump_parents=False):
657         print "CREDENTIAL", self.get_subject()
658
659         print "      privs:", self.get_privileges().save_to_string()
660
661         print "  gidCaller:"
662         gidCaller = self.get_gid_caller()
663         if gidCaller:
664             gidCaller.dump(8, dump_parents)
665
666         print "  gidObject:"
667         gidObject = self.get_gid_object()
668         if gidObject:
669             gidObject.dump(8, dump_parents)
670
671
672         if self.parent_xml and dump_parents:
673            print "PARENT",
674            Credential(string=self.parent_xml).dump_parents()
675