Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Crypto;
3
 
4
use Aws\Exception\CryptoException;
5
use GuzzleHttp\Psr7;
6
use GuzzleHttp\Psr7\StreamDecoratorTrait;
7
use Psr\Http\Message\StreamInterface;
8
use Aws\Crypto\Polyfill\AesGcm;
9
use Aws\Crypto\Polyfill\Key;
10
 
11
/**
12
 * @internal Represents a stream of data to be gcm decrypted.
13
 */
14
class AesGcmDecryptingStream implements AesStreamInterface
15
{
16
    use StreamDecoratorTrait;
17
 
18
    private $aad;
19
 
20
    private $initializationVector;
21
 
22
    private $key;
23
 
24
    private $keySize;
25
 
26
    private $cipherText;
27
 
28
    private $tag;
29
 
30
    private $tagLength;
31
 
32
    /**
1441 ariadna 33
     * @var StreamInterface
34
     */
35
    private $stream;
36
 
37
    /**
1 efrain 38
     * @param StreamInterface $cipherText
39
     * @param string $key
40
     * @param string $initializationVector
41
     * @param string $tag
42
     * @param string $aad
43
     * @param int $tagLength
44
     * @param int $keySize
45
     */
46
    public function __construct(
47
        StreamInterface $cipherText,
48
        $key,
49
        $initializationVector,
50
        $tag,
51
        $aad = '',
52
        $tagLength = 128,
53
        $keySize = 256
54
    ) {
55
        $this->cipherText = $cipherText;
56
        $this->key = $key;
57
        $this->initializationVector = $initializationVector;
58
        $this->tag = $tag;
59
        $this->aad = $aad;
60
        $this->tagLength = $tagLength;
61
        $this->keySize = $keySize;
1441 ariadna 62
        // unsetting the property forces the first access to go through
63
        // __get().
64
        unset($this->stream);
1 efrain 65
    }
66
 
67
    public function getOpenSslName()
68
    {
69
        return "aes-{$this->keySize}-gcm";
70
    }
71
 
72
    public function getAesName()
73
    {
74
        return 'AES/GCM/NoPadding';
75
    }
76
 
77
    public function getCurrentIv()
78
    {
79
        return $this->initializationVector;
80
    }
81
 
82
    public function createStream()
83
    {
84
        if (version_compare(PHP_VERSION, '7.1', '<')) {
85
            return Psr7\Utils::streamFor(AesGcm::decrypt(
86
                (string) $this->cipherText,
87
                $this->initializationVector,
88
                new Key($this->key),
89
                $this->aad,
90
                $this->tag,
91
                $this->keySize
92
            ));
93
        } else {
94
            $result = \openssl_decrypt(
95
                (string)$this->cipherText,
96
                $this->getOpenSslName(),
97
                $this->key,
98
                OPENSSL_RAW_DATA,
99
                $this->initializationVector,
100
                $this->tag,
101
                $this->aad
102
            );
103
            if ($result === false) {
104
                throw new CryptoException('The requested object could not be'
105
                    . ' decrypted due to an invalid authentication tag.');
106
            }
107
            return Psr7\Utils::streamFor($result);
108
        }
109
    }
110
 
1441 ariadna 111
    public function isWritable(): bool
1 efrain 112
    {
113
        return false;
114
    }
115
}