-
Notifications
You must be signed in to change notification settings - Fork 2
/
UpdateFile.php
162 lines (141 loc) · 4.59 KB
/
UpdateFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace ParagonIE\Gossamer\Client;
use ParagonIE\Gossamer\GossamerException;
use ParagonIE\Gossamer\Release\Common;
use ParagonIE\Gossamer\Release\Verifier;
use Psr\Http\Message\StreamInterface;
use SodiumException;
/**
* Class UpdateFile
* @package ParagonIE\Gossamer\Client
*/
class UpdateFile
{
/** @var int $algorithm */
protected $algorithm = Common::SIGN_ALG_ED25519_BLAKE2B;
/** @var array{attestor: string, attestation: string, ledgerhash: string}[] $attestations */
protected $attestations = [];
/** @var array $metadata */
protected $metadata = [];
/** @var string $publicKey */
protected $publicKey;
/** @var AttestPolicy $attestPolicy */
protected $attestPolicy;
/** @var string $signature */
protected $signature;
/** @var string $tmpDir */
protected $tmpDir;
/**
* UpdateFile constructor.
*
* @param string $publicKey
* @param string $signature
* @param array $metadata
* @param array{attestor: string, attestation: string, ledgerhash: string}[] $attestations
* @param ?AttestPolicy $attestPolicy
*/
public function __construct(
$publicKey,
$signature,
$metadata = [],
$attestations = [],
$attestPolicy = null
) {
$this->publicKey = $publicKey;
$this->signature = $signature;
$this->metadata = $metadata;
$this->attestations = $attestations;
$this->tmpDir = \sys_get_temp_dir();
if (is_null($attestPolicy)) {
$attestPolicy = new AttestPolicy();
}
$this->attestPolicy = $attestPolicy;
}
/**
* @param int $algId
* @return self
*/
public function setAlgorithm($algId = Common::SIGN_ALG_ED25519_BLAKE2B)
{
$this->algorithm = $algId;
return $this;
}
/**
* @param AttestPolicy $policy
* @return self
*/
public function setAttestPolicy(AttestPolicy $policy)
{
$this->attestPolicy = $policy;
return $this;
}
/**
* Should we install this file?
*
* @param string|resource|StreamInterface $streamOrFilePath
* @return bool
* @throws GossamerException
* @throws \SodiumException
*/
public function isFileValid($streamOrFilePath)
{
$signatureValid = $this->isSignatureValid($streamOrFilePath);
$attestationsPass = $this->passesAttestationPolicy();
return $signatureValid && $attestationsPass;
}
/**
* Do the set of attestations registered for this update pass
* the local policy?
*
* @return bool
*/
public function passesAttestationPolicy()
{
return $this->attestPolicy->passes($this->attestations);
}
/**
* Is the signature we see valid for a given file?
*
* @param string|resource|StreamInterface $streamOrFilePath
* @return bool
* @throws GossamerException
* @throws SodiumException
*/
public function isSignatureValid($streamOrFilePath)
{
if ($streamOrFilePath instanceof StreamInterface) {
// PSR-7 Stream
$tmpFile = \tempnam($this->tmpDir, 'gossamer');
$pos = $streamOrFilePath->tell();
$streamOrFilePath->rewind();
// Copy stream to temp file:
\file_put_contents($tmpFile, $streamOrFilePath->getContents());
$valid = (new Verifier($this->algorithm))
->verify($tmpFile, $this->signature, [$this->publicKey]);
$streamOrFilePath->seek($pos);
unlink($tmpFile);
} elseif (is_resource($streamOrFilePath)) {
// PHP resource
$tmpFile = \tempnam($this->tmpDir, 'gossamer');
$pos = \ftell($streamOrFilePath);
\fseek($streamOrFilePath, 0);
// Copy stream to temp file (via handler):
$outFile = \fopen($tmpFile, 'rb');
if (\is_bool(\stream_copy_to_stream($outFile, $streamOrFilePath))) {
throw new GossamerException('Could not copy to stream');
}
\fclose($outFile);
$valid = (new Verifier($this->algorithm))
->verify($tmpFile, $this->signature, [$this->publicKey]);
\fseek($streamOrFilePath, $pos);
unlink($tmpFile);
} elseif (is_string($streamOrFilePath)) {
// File path
$valid = (new Verifier($this->algorithm))
->verify($streamOrFilePath, $this->signature, [$this->publicKey]);
} else {
throw new GossamerException('Invalid type for Argument 1');
}
return $valid;
}
}