From a47513aeeb728b6316ba6765afdd7e5adbf4f2e3 Mon Sep 17 00:00:00 2001 From: StevenWdV Date: Fri, 17 Dec 2021 15:21:53 +0100 Subject: Add PHP wrapper, use header for right platform for PHP & Swift, simplify Swift Windows fix --- wrappers/php/src/Discovery.php | 72 ++++++++++++++++++++++ wrappers/php/src/Impl/GoSlice.php | 43 +++++++++++++ wrappers/php/src/InvalidSignatureException.php | 12 ++++ .../src/InvalidSignatureUnknownKeyException.php | 12 ++++ wrappers/php/src/SignatureTooOldException.php | 12 ++++ wrappers/php/src/UnknownVerifyException.php | 12 ++++ wrappers/php/src/VerifyException.php | 14 +++++ 7 files changed, 177 insertions(+) create mode 100644 wrappers/php/src/Discovery.php create mode 100644 wrappers/php/src/Impl/GoSlice.php create mode 100644 wrappers/php/src/InvalidSignatureException.php create mode 100644 wrappers/php/src/InvalidSignatureUnknownKeyException.php create mode 100644 wrappers/php/src/SignatureTooOldException.php create mode 100644 wrappers/php/src/UnknownVerifyException.php create mode 100644 wrappers/php/src/VerifyException.php (limited to 'wrappers/php/src') diff --git a/wrappers/php/src/Discovery.php b/wrappers/php/src/Discovery.php new file mode 100644 index 0000000..3ae7010 --- /dev/null +++ b/wrappers/php/src/Discovery.php @@ -0,0 +1,72 @@ +Verify($signatureData->slice(), $jsonData->slice(), $expectedNameData->slice(), $minSignTime); + + switch ($result) { + case 0: + return; + case 1: + throw new InvalidArgumentException('unknown expected file name', $result); + case 2: + throw new InvalidSignatureException(); + case 3: + throw new InvalidSignatureUnknownKeyException(); + case 4: + throw new SignatureTooOldException(); + default: + throw new UnknownVerifyException($result); + } + } + + /** @internal Use for testing only, see Go documentation. */ + public static function insecureTestingSetExtraKey(string $keyString): void + { + $ffi = self::ffi(); + $keyData = new GoSlice($ffi, $keyString); + $ffi->InsecureTestingSetExtraKey($keyData->slice()); + } +} diff --git a/wrappers/php/src/Impl/GoSlice.php b/wrappers/php/src/Impl/GoSlice.php new file mode 100644 index 0000000..441b460 --- /dev/null +++ b/wrappers/php/src/Impl/GoSlice.php @@ -0,0 +1,43 @@ +cData = $cData; + FFI::memcpy($cData, $data, $len); + + $slice = $ffi->new('GoSlice'); + if (!$slice) throw new RuntimeException('error allocating buffer'); + $this->slice = $slice; + $slice->data = FFI::addr($cData); // $cData must not be destroyed while $slice is in use + $slice->cap = $slice->len = $len; + } + + public function slice(): CData + { + return $this->slice; + } + + public function __destruct() + { + // Make sure we do not unknowingly use a slice with deallocated data + $this->slice->data = null; + $this->slice->cap = $this->slice->len = 0; + } +} diff --git a/wrappers/php/src/InvalidSignatureException.php b/wrappers/php/src/InvalidSignatureException.php new file mode 100644 index 0000000..d5d4164 --- /dev/null +++ b/wrappers/php/src/InvalidSignatureException.php @@ -0,0 +1,12 @@ +