wp-database-backup
/
includes
/
admin
/
Destination
/
Google
/
google-api-php-client
/
src
/
auth
/
Google_PemVerifier.php
Google_PemVerifier.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 7.13, at includes/admin/Destination/Google/google-api-php-client/src/auth/Google_PemVerifier.php
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | /* |
| 6 | * Copyright 2011 Google Inc. |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * Verifies signatures using PEM encoded certificates. |
| 23 | * |
| 24 | * @author Brian Eaton <beaton@google.com> |
| 25 | */ |
| 26 | class Google_PemVerifier extends Google_Verifier { |
| 27 | private $publicKey; |
| 28 | |
| 29 | /** |
| 30 | * Constructs a verifier from the supplied PEM-encoded certificate. |
| 31 | * |
| 32 | * $pem: a PEM encoded certificate (not a file). |
| 33 | * @param $pem |
| 34 | * @throws Google_AuthException |
| 35 | * @throws Google_Exception |
| 36 | */ |
| 37 | function __construct($pem) { |
| 38 | if (!function_exists('openssl_x509_read')) { |
| 39 | throw new Google_Exception('Google API PHP client needs the openssl PHP extension'); |
| 40 | } |
| 41 | $this->publicKey = openssl_x509_read($pem); |
| 42 | if (!$this->publicKey) { |
| 43 | throw new Google_AuthException("Unable to parse PEM: ".esc_html($pem)); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | function __destruct() { |
| 48 | if ($this->publicKey) { |
| 49 | openssl_x509_free($this->publicKey); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Verifies the signature on data. |
| 55 | * |
| 56 | * Returns true if the signature is valid, false otherwise. |
| 57 | * @param $data |
| 58 | * @param $signature |
| 59 | * @throws Google_AuthException |
| 60 | * @return bool |
| 61 | */ |
| 62 | function verify($data, $signature) { |
| 63 | $status = openssl_verify($data, $signature, $this->publicKey, "sha256"); |
| 64 | if ($status === -1) { |
| 65 | throw new Google_AuthException('Signature verification error: ' . esc_html(openssl_error_string())); |
| 66 | } |
| 67 | return $status === 1; |
| 68 | } |
| 69 | } |
| 70 |