OAuthSignatureMethod_RSA_SHA1.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * The RSA-SHA1 signature method uses the RSASSA-PKCS1-v1_5 signature algorithm as defined in
  4. * [RFC3447] section 8.2 (more simply known as PKCS#1), using SHA-1 as the hash function for
  5. * EMSA-PKCS1-v1_5. It is assumed that the Consumer has provided its RSA public key in a
  6. * verified way to the Service Provider, in a manner which is beyond the scope of this
  7. * specification.
  8. * - Chapter 9.3 ("RSA-SHA1")
  9. */
  10. abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {
  11. public function get_name() {
  12. return "RSA-SHA1";
  13. }
  14. // Up to the SP to implement this lookup of keys. Possible ideas are:
  15. // (1) do a lookup in a table of trusted certs keyed off of consumer
  16. // (2) fetch via http using a url provided by the requester
  17. // (3) some sort of specific discovery code based on request
  18. //
  19. // Either way should return a string representation of the certificate
  20. protected abstract function fetch_public_cert(&$request);
  21. // Up to the SP to implement this lookup of keys. Possible ideas are:
  22. // (1) do a lookup in a table of trusted certs keyed off of consumer
  23. //
  24. // Either way should return a string representation of the certificate
  25. protected abstract function fetch_private_cert(&$request);
  26. public function build_signature($request, $consumer, $token) {
  27. $base_string = $request->get_signature_base_string();
  28. $request->base_string = $base_string;
  29. // Fetch the private key cert based on the request
  30. $cert = $this->fetch_private_cert($request);
  31. // Pull the private key ID from the certificate
  32. $privatekeyid = openssl_get_privatekey($cert);
  33. // Sign using the key
  34. $ok = openssl_sign($base_string, $signature, $privatekeyid);
  35. // Release the key resource
  36. openssl_free_key($privatekeyid);
  37. return base64_encode($signature);
  38. }
  39. public function check_signature($request, $consumer, $token, $signature) {
  40. $decoded_sig = base64_decode($signature);
  41. $base_string = $request->get_signature_base_string();
  42. // Fetch the public key cert based on the request
  43. $cert = $this->fetch_public_cert($request);
  44. // Pull the public key ID from the certificate
  45. $publickeyid = openssl_get_publickey($cert);
  46. // Check the computed signature against the one passed in the query
  47. $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
  48. // Release the key resource
  49. openssl_free_key($publickeyid);
  50. return $ok == 1;
  51. }
  52. }