PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / lib / Google2 / Signer / P12.php

P12.php in InfiniteWP Client trunk, at lib/Google2/Signer/P12.php

95 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 if (!class_exists('Google_Client')) {
19 require_once dirname(__FILE__) . '/../autoload.php';
20 }
21
22 /**
23 * Signs data.
24 *
25 * Only used for testing.
26 *
27 * @author Brian Eaton <beaton@google.com>
28 */
29 class Google_Signer_P12 extends Google_Signer_Abstract
30 {
31 // OpenSSL private key resource
32 private $privateKey;
33
34 // Creates a new signer from a .p12 file.
35 public function __construct($p12, $password)
36 {
37 if (!function_exists('openssl_x509_read')) {
38 throw new Google_Exception(
39 'The Google PHP API library needs the openssl PHP extension'
40 );
41 }
42
43 // If the private key is provided directly, then this isn't in the p12
44 // format. Different versions of openssl support different p12 formats
45 // and the key from google wasn't being accepted by the version available
46 // at the time.
47 if (!$password && strpos($p12, "-----BEGIN RSA PRIVATE KEY-----") !== false) {
48 $this->privateKey = openssl_pkey_get_private($p12);
49 } elseif($password === 'notasecret' && strpos($p12, "-----BEGIN PRIVATE KEY-----") !== false) {
50 $this->privateKey = openssl_pkey_get_private($p12);
51 } else {
52 // This throws on error
53 $certs = array();
54 if (!openssl_pkcs12_read($p12, $certs, $password)) {
55 throw new Google_Auth_Exception(
56 "Unable to parse the p12 file. " .
57 "Is this a .p12 file? Is the password correct? OpenSSL error: " .
58 openssl_error_string()
59 );
60 }
61 // TODO(beaton): is this part of the contract for the openssl_pkcs12_read
62 // method? What happens if there are multiple private keys? Do we care?
63 if (!array_key_exists("pkey", $certs) || !$certs["pkey"]) {
64 throw new Google_Auth_Exception("No private key found in p12 file.");
65 }
66 $this->privateKey = openssl_pkey_get_private($certs['pkey']);
67 }
68
69 if (!$this->privateKey) {
70 throw new Google_Auth_Exception("Unable to load private key");
71 }
72 }
73
74 public function __destruct()
75 {
76 if ($this->privateKey) {
77 openssl_pkey_free($this->privateKey);
78 }
79 }
80
81 public function sign($data)
82 {
83 if (version_compare(PHP_VERSION, '5.3.0') < 0) {
84 throw new Google_Auth_Exception(
85 "PHP 5.3.0 or higher is required to use service accounts."
86 );
87 }
88 $hash = defined("OPENSSL_ALGO_SHA256") ? OPENSSL_ALGO_SHA256 : "sha256";
89 if (!openssl_sign($data, $signature, $this->privateKey, $hash)) {
90 throw new Google_Auth_Exception("Unable to sign data");
91 }
92 return $signature;
93 }
94 }
95