AP.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Introduces the notion of an Attribute Provider that attests and signs
  4. * attributes
  5. * Uses OpenID Signed Assertions(Sxip draft) for attesting attributes
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: See the COPYING file included in this distribution.
  9. *
  10. * @package OpenID
  11. * @author Santosh Subramanian <subrasan@cs.sunysb.edu>
  12. * @author Shishir Randive <srandive@cs.sunysb.edu>
  13. * Stony Brook University.
  14. *
  15. */
  16. require_once 'Auth/OpenID/SAML.php';
  17. /**
  18. * The Attribute_Provider class which signs the attribute,value pair
  19. * for a given openid.
  20. */
  21. class Attribute_Provider
  22. {
  23. private $public_key_certificate=null;
  24. private $private_key=null;
  25. private $authenticatedUser=null;
  26. private $notBefore=null;
  27. private $notOnOrAfter=null;
  28. private $rsadsa=null;
  29. private $acsURI=null;
  30. private $attribute=null;
  31. private $value=null;
  32. private $assertionTemplate=null;
  33. /**
  34. * Creates an Attribute_Provider object initialized with startup values.
  35. * @param string $public_key_certificate - The public key certificate
  36. of the signer.
  37. * @param string $private_key - The private key of the signer.
  38. * @param string $notBefore - Certificate validity time
  39. * @param string $notOnOrAfter - Certificate validity time
  40. * @param string $rsadsa - Choice of the algorithm (RSA/DSA)
  41. * @param string $acsURI - URI of the signer.
  42. * @param string $assertionTemplate - SAML template used for assertion
  43. */
  44. function Attribute_Provider($public_key_certificate,$private_key,$notBefore,$notOnOrAfter,$rsadsa,$acsURI,
  45. $assertionTemplate)
  46. {
  47. $this->public_key_certificate=$public_key_certificate;
  48. $this->private_key=$private_key;
  49. $this->notBefore=$notBefore;
  50. $this->notOnOrAfter=$notOnOrAfter;
  51. $this->rsadsa=$rsadsa;
  52. $this->acsURI=$acsURI;
  53. $this->assertionTemplate=$assertionTemplate;
  54. }
  55. /**
  56. * Create the signed assertion.
  57. * @param string $openid - Openid of the entity being asserted.
  58. * @param string $attribute - The attribute name being asserted.
  59. * @param string $value - The attribute value being asserted.
  60. */
  61. function sign($openid,$attribute,$value)
  62. {
  63. $samlObj = new SAML();
  64. $responseXmlString = $samlObj->createSamlAssertion($openid,
  65. $this->notBefore,
  66. $this->notOnOrAfter,
  67. $this->rsadsa,
  68. $this->acsURI,
  69. $attribute,
  70. sha1($value),
  71. $this->assertionTemplate);
  72. $signedAssertion=$samlObj->signAssertion($responseXmlString,
  73. $this->private_key,
  74. $this->public_key_certificate);
  75. return $signedAssertion;
  76. }
  77. }
  78. /**
  79. * The Attribute_Verifier class which verifies the signed assertion at the Relying party.
  80. */
  81. class Attribute_Verifier
  82. {
  83. /**
  84. * The certificate the Relying party trusts.
  85. */
  86. private $rootcert;
  87. /**
  88. * This function loads the public key certificate that the relying party trusts.
  89. * @param string $cert - Trusted public key certificate.
  90. */
  91. function load_trusted_root_cert($cert)
  92. {
  93. $this->rootcert=$cert;
  94. }
  95. /**
  96. * Verifies the certificate given the SAML document.
  97. * @param string - signed SAML assertion
  98. * return @boolean - true if verification is successful, false if unsuccessful.
  99. */
  100. function verify($responseXmlString)
  101. {
  102. $samlObj = new SAML();
  103. $ret = $samlObj->verifyAssertion($responseXmlString,$this->rootcert);
  104. return $ret;
  105. }
  106. }
  107. /**
  108. * This is a Store Request creating class at the Attribute Provider.
  109. */
  110. class AP_OP_StoreRequest
  111. {
  112. /**
  113. * Creates store request and adds it as an extension to AuthRequest object
  114. passed to it.
  115. * @param &Auth_OpenID_AuthRequest &$auth_request - A reference to
  116. the AuthRequest object.
  117. * @param &Attribute_Provider &$attributeProvider - A reference to the
  118. Attribute Provider object.
  119. * @param string $attribute - The attribute name being asserted.
  120. * @param string $value - The attribute value being asserted.
  121. * @param string $openid - Openid of the entity being asserted.
  122. * @return &Auth_OpenID_AuthRequest - Auth_OpenID_AuthRequest object
  123. returned with StoreRequest extension.
  124. */
  125. static function createStoreRequest(&$auth_request,&$attributeProvider,
  126. $attribute,$value,$openid)
  127. {
  128. if(!$auth_request){
  129. return null;
  130. }
  131. $signedAssertion=$attributeProvider->sign($openid,$attribute,$value);
  132. $store_request=new Auth_OpenID_AX_StoreRequest;
  133. $store_request->addValue($attribute,base64_encode($value));
  134. $store_request->addValue($attribute.'/signature',
  135. base64_encode($signedAssertion));
  136. if($store_request) {
  137. $auth_request->addExtension($store_request);
  138. return $auth_request;
  139. }
  140. }
  141. }
  142. /*
  143. *This is implemented at the RP Takes care of getting the attribute from the
  144. *AX_Fetch_Response object and verifying it.
  145. */
  146. class RP_OP_Verify
  147. {
  148. /**
  149. * Verifies a given signed assertion.
  150. * @param &Attribute_Verifier &$attributeVerifier - An instance of the class
  151. passed for the verification.
  152. * @param Auth_OpenID_Response - Response object for extraction.
  153. * @return boolean - true if successful, false if verification fails.
  154. */
  155. function verifyAssertion(&$attributeVerifier,$response)
  156. {
  157. $ax_resp=Auth_OpenID_AX_FetchResponse::fromSuccessResponse($response);
  158. if($ax_resp instanceof Auth_OpenID_AX_FetchResponse){
  159. $ax_args=$ax_resp->getExtensionArgs();
  160. if($ax_args) {
  161. $value=base64_decode($ax_args['value.ext1.1']);
  162. if($attributeVerifier->verify($value)){
  163. return base64_decode($ax_args['value.ext0.1']);
  164. } else {
  165. return null;
  166. }
  167. } else {
  168. return null;
  169. }
  170. } else {
  171. return null;
  172. }
  173. }
  174. }
  175. ?>