Google_P12Signer

Signs data.

Defined (1)

The class is defined in the following location(s).

/google-api-php-client/api-code/auth/Google_P12Signer.php  
  1. class Google_P12Signer extends Google_Signer { 
  2. // OpenSSL private key resource 
  3. private $privateKey; 
  4.  
  5. // Creates a new signer from a .p12 file. 
  6. function __construct($p12, $password) { 
  7. if (!function_exists('openssl_x509_read')) { 
  8. throw new Exception( 
  9. 'The Google PHP API library needs the openssl PHP extension'); 
  10.  
  11. // This throws on error 
  12. $certs = array(); 
  13. if (!openssl_pkcs12_read($p12, $certs, $password)) { 
  14. throw new Google_AuthException("Unable to parse the p12 file. " . 
  15. "Is this a .p12 file? Is the password correct? OpenSSL error: " . 
  16. openssl_error_string()); 
  17. // TODO(beaton): is this part of the contract for the openssl_pkcs12_read 
  18. // method? What happens if there are multiple private keys? Do we care? 
  19. if (!array_key_exists("pkey", $certs) || !$certs["pkey"]) { 
  20. throw new Google_AuthException("No private key found in p12 file."); 
  21. $this->privateKey = openssl_pkey_get_private($certs["pkey"]); 
  22. if (!$this->privateKey) { 
  23. throw new Google_AuthException("Unable to load private key in "); 
  24.  
  25. function __destruct() { 
  26. if ($this->privateKey) { 
  27. openssl_pkey_free($this->privateKey); 
  28.  
  29. function sign($data) { 
  30. if(version_compare(PHP_VERSION, '5.3.0') < 0) { 
  31. throw new Google_AuthException( 
  32. "PHP 5.3.0 or higher is required to use service accounts."); 
  33. if (!openssl_sign($data, $signature, $this->privateKey, "sha256")) { 
  34. throw new Google_AuthException("Unable to sign data"); 
  35. return $signature;