1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
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:
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
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
22 #----------------------------------------------------------------------
24 # Implements SFA Credentials
26 # Credentials are signed XML files that assign a subject gid privileges to an object gid
34 from xml.dom.minidom import Document, parseString
35 from tempfile import mkstemp
36 from sfa.trust.keypair 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 *
42 from sfa.util.sfalogging import logger
43 from dateutil.parser import parse
47 # Two years, in seconds
48 DEFAULT_CREDENTIAL_LIFETIME = 60 * 60 * 24 * 365 * 2
52 # . make privs match between PG and PL
53 # . Need to add support for other types of credentials, e.g. tickets
56 signature_template = \
58 <Signature xml:id="Sig_%s" xmlns="http://www.w3.org/2000/09/xmldsig#">
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"/>
64 <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
66 <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
67 <DigestValue></DigestValue>
83 # Convert a string into a bool
86 if str.lower() in ['yes','true','1']:
92 # Utility function to get the text of an XML element
94 def getTextNode(element, subele):
95 sub = element.getElementsByTagName(subele)[0]
96 if len(sub.childNodes) > 0:
97 return sub.childNodes[0].nodeValue
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.
106 def append_sub(doc, parent, element, text):
107 ele = doc.createElement(element)
108 ele.appendChild(doc.createTextNode(text))
109 parent.appendChild(ele)
112 # Signature contains information about an xmlsec1 signature
113 # for a signed-credential
116 class Signature(object):
118 def __init__(self, string=None):
120 self.issuer_gid = None
137 def set_refid(self, id):
140 def get_issuer_gid(self):
145 def set_issuer_gid(self, gid):
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))
158 self.xml = signature_template % (self.get_refid(), self.get_refid())
162 # A credential provides a caller gid with privileges to an object gid.
163 # A signed credential is signed by the object's authority.
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.
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.
174 class Credential(object):
177 # Create a Credential object
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
193 self.signature = None
198 # Check if this is a legacy credential, translate it if so
199 if string or filename:
203 str = file(filename).read()
205 if str.strip().startswith("-----"):
206 self.legacy = CredentialLegacy(False,string=str)
207 self.translate_legacy(str)
212 # Find an xmlsec1 path
213 self.xmlsec_path = ''
214 paths = ['/usr/bin','/usr/local/bin','/bin','/opt/bin','/opt/local/bin']
216 if os.path.isfile(path + '/' + 'xmlsec1'):
217 self.xmlsec_path = path + '/' + 'xmlsec1'
220 def get_subject(self):
221 if not self.gidObject:
223 return self.gidObject.get_subject()
225 def get_signature(self):
226 if not self.signature:
228 return self.signature
230 def set_signature(self, sig):
235 # Translate a legacy credential into a new one
237 # @param String of the legacy credential
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()
245 # Default to two years
246 self.set_lifetime(DEFAULT_CREDENTIAL_LIFETIME)
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())
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
258 def set_issuer_keys(self, privkey, gid):
259 self.issuer_privkey = privkey
260 self.issuer_gid = gid
264 # Set this credential's parent
265 def set_parent(self, cred):
270 # set the GID of the caller
272 # @param gid GID object of the caller
274 def set_gid_caller(self, gid):
276 # gid origin caller is the caller's gid by default
277 self.gidOriginCaller = gid
280 # get the GID of the object
282 def get_gid_caller(self):
283 if not self.gidCaller:
285 return self.gidCaller
288 # set the GID of the object
290 # @param gid GID object of the object
292 def set_gid_object(self, gid):
296 # get the GID of the object
298 def get_gid_object(self):
299 if not self.gidObject:
301 return self.gidObject
304 # set the lifetime of this credential
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
311 def set_lifetime(self, lifeTime):
312 if isinstance(lifeTime, int):
313 self.expiration = datetime.timedelta(seconds=lifeTime) + datetime.datetime.utcnow()
315 self.expiration = lifeTime
318 # get the lifetime of the credential (in datetime format)
320 def get_lifetime(self):
321 if not self.expiration:
323 return self.expiration
329 # @param privs either a comma-separated list of privileges of a RightList object
331 def set_privileges(self, privs):
332 if isinstance(privs, str):
333 self.privileges = RightList(string = privs)
335 self.privileges = privs
339 # return the privileges as a RightList object
341 def get_privileges(self):
342 if not self.privileges:
344 return self.privileges
347 # determine whether the credential allows a particular operation to be
350 # @param op_name string specifying name of operation ("lookup", "update", etc)
352 def can_perform(self, op_name):
353 rights = self.get_privileges()
358 return rights.can_perform(op_name)
362 # Encode the attributes of the credential into an XML string
363 # This should be done immediately before signing the credential.
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.
370 # Create the XML document
372 signed_cred = doc.createElement("signed-credential")
373 doc.appendChild(signed_cred)
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)
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)
401 # Add the parent credential if it exists
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)
410 # Create the <signatures> tag
411 signatures = doc.createElement("signatures")
412 signed_cred.appendChild(signatures)
414 # Add any parent signatures
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)
421 # Get the finished product
422 self.xml = doc.toxml()
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)
431 def save_to_file(self, filename, save_parents=True, filep=None):
437 f = open(filename, "w")
441 def save_to_string(self, save_parents=True):
451 def set_refid(self, rid):
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
459 def updateRefID(self):
461 self.set_refid('ref0')
466 next_cred = self.parent
468 refs.append(next_cred.get_refid())
470 next_cred = next_cred.parent
475 # Find a unique refid for this credential
476 rid = self.get_refid()
479 rid = "ref%d" % (val + 1)
484 # Return the set of parent credential ref ids
493 # Sign the XML file created by encode()
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.
501 if not self.issuer_privkey or not self.issuer_gid:
503 doc = parseString(self.get_xml())
504 sigs = doc.getElementsByTagName("signatures")[0]
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)
513 self.xml = doc.toxml()
516 # Split the issuer GID into multiple certificates if it's a chain
517 chain = GID(filename=self.issuer_gid)
520 gid_files.append(chain.save_to_random_tmp_file(False))
521 if chain.get_parent():
522 chain = chain.get_parent()
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()
534 for gid_file in gid_files:
539 # This is no longer a legacy credential
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.
555 doc = parseString(self.xml)
557 signed_cred = doc.getElementsByTagName("signed-credential")
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")
566 cred = doc.getElementsByTagName("credential")[0]
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"))
576 privs = cred.getElementsByTagName("privileges")[0]
578 for priv in privs.getElementsByTagName("privilege"):
579 kind = getTextNode(priv, "name")
580 deleg = str2bool(getTextNode(priv, "can_delegate"))
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())
588 rlist.add(Right(kind.strip(), deleg))
589 self.set_privileges(rlist)
593 parent = cred.getElementsByTagName("parent")
595 parent_doc = parent[0].getElementsByTagName("credential")[0]
596 parent_xml = parent_doc.toxml()
597 self.parent = Credential(string=parent_xml)
600 # Assign the signatures to the credentials
602 Sig = Signature(string=sig.toxml())
604 for cur_cred in self.get_credential_list():
605 if cur_cred.get_refid() == Sig.get_refid():
606 cur_cred.set_signature(Sig)
611 # trusted_certs: A list of trusted GID filenames (not GID objects!)
612 # Chaining is not supported within the GIDs by xmlsec1.
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
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
630 # -- Verify does *NOT*
631 # . ensure that an xmlrpc client's gid matches a credential gid, that
632 # must be done elsewhere
634 # @param trusted_certs: The certificates of trusted CA certificates
635 def verify(self, trusted_certs):
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:
644 # Failures here include unreadable 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
652 # Use legacy verification if this is a legacy credential
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)
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())
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])
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)
675 refs.append("Sig_%s" % self.get_refid())
677 parentRefs = self.updateRefID()
678 for ref in parentRefs:
679 refs.append("Sig_%s" % ref)
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)
688 # Verify the parents (delegation)
690 self.verify_parent(self.parent)
692 # Make sure the issuer is the target's authority
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):
703 list.append(cur_cred)
705 cur_cred = cur_cred.parent
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()
718 if root_target_gid.is_signed_by_cert(root_cred_signer):
719 # cred signer matches target signer, return success
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
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):
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.
744 # Give up, credential does not pass issuer verification
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()))
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())
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")
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")
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")
779 if parent_cred.parent:
780 parent_cred.verify_parent(parent_cred.parent)
783 def delegate(self, delegee_gid, keyfile):
785 Return a delegated copy of this credential, delegated to the
786 specified gid's user.
788 # get the gid of the object we are delegating
789 object_gid = self.get_gid_object()
790 object_hrn = self.get_hrn()
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()
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)
814 # Dump the contents of a credential to stdout in human-readable format
816 # @param dump_parents If true, also dump the parent certificates
818 def dump(self, dump_parents=False):
819 print "CREDENTIAL", self.get_subject()
821 print " privs:", self.get_privileges().save_to_string()
824 gidCaller = self.get_gid_caller()
826 gidCaller.dump(8, dump_parents)
829 gidObject = self.get_gid_object()
831 gidObject.dump(8, dump_parents)
834 if self.parent and dump_parents:
836 self.parent.dump_parents()