Issues (15)

src/structures/TimestampRequest.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace vakata\asn1\structures;
4
5
use \vakata\asn1\ASN1;
0 ignored issues
show
The type \vakata\asn1\ASN1 was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use \vakata\asn1\ASN1Exception;
0 ignored issues
show
The type \vakata\asn1\ASN1Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use \vakata\asn1\Encoder;
0 ignored issues
show
The type \vakata\asn1\Encoder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class TimestampRequest extends Structure
10
{
11
    /**
12
     * Generate a timestamp request (tsq) for a file path
13
     * @param  string           $path        the path to the file to be timestamped
14
     * @param  boolean|string   $nonce       should a nonce be used - defaults to true, could be a value to use as nonce
15
     * @param  boolean          $requireCert should a certificate be returned in the response, defaults to false
16
     * @param  string           $alg         the algorithm to use, defaults to 'sha1'
17
     * @param  string|null      $policy      the policy to use, defaults to null
18
     * @return string                        the raw timestamp request
19
     * @codeCoverageIgnore
20
     */
21
    public static function generateFromFile($path, $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null)
22
    {
23
        return static::generateFromData(file_get_contents($path), $nonce, $requireCert, $alg, $policy);
24
    }
25
    /**
26
     * Generate a timestamp request (tsq) for a string
27
     * @param  string           $data        the data to be timestamped
28
     * @param  boolean|string   $nonce       should a nonce be used - defaults to true, could be a value to use as nonce
29
     * @param  boolean          $requireCert should a certificate be returned in the response, defaults to false
30
     * @param  string           $alg         the algorithm to use, defaults to 'sha1'
31
     * @param  string|null      $policy      the policy to use, defaults to null
32
     * @return string                        the raw timestamp request
33
     */
34
    public static function generateFromData($data, $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null)
35
    {
36
        if (!in_array($alg, ['sha1', 'sha256', 'sha384', 'sha512', 'md5'])) {
37
            throw new ASN1Exception('Unsupported hash algorithm');
38
        }
39
        $hash = hash($alg, $data, true);
40
        if ($nonce === true) {
41
            $nonce = rand(1, PHP_INT_MAX);
42
        }
43
        if (!$nonce) {
44
            $nonce = null;
45
        }
46
        
47
        $src = [
48
            'version' => 'v1',
49
            'reqPolicy' => $policy,
50
            'messageImprint' => [
51
                'hashAlgorithm' => [ "algorithm" => $alg, 'parameters' => null ],
52
                'hashedMessage' => base64_encode($hash),
53
            ],
54
            'nonce' => $nonce,
55
            'certReq' => $requireCert
56
        ];
57
58
        return Encoder::encode($src, static::map());
59
    }
60
    /**
61
     * Generate a timestamp request (tsq) for a given hash
62
     * @param  string           $data        the hash to be timestamped (raw binary)
63
     * @param  boolean|string   $nonce       should a nonce be used - defaults to true, could be a value to use as nonce
64
     * @param  boolean          $requireCert should a certificate be returned in the response, defaults to false
65
     * @param  string           $alg         the algorithm to use, defaults to 'sha1'
66
     * @param  string|null      $policy      the policy to use, defaults to null
67
     * @return string                        the raw timestamp request
68
     */
69
    public static function generateFromHash($data, $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null)
70
    {
71
        if (!in_array($alg, ['sha1', 'sha256', 'sha384', 'sha512', 'md5'])) {
72
            throw new ASN1Exception('Unsupported hash algorithm');
73
        }
74
        if ($nonce === true) {
75
            $nonce = rand(1, PHP_INT_MAX);
76
        }
77
        if (!$nonce) {
78
            $nonce = null;
79
        }
80
        
81
        $src = [
82
            'version' => 'v1',
83
            'reqPolicy' => $policy,
84
            'messageImprint' => [
85
                'hashAlgorithm' => [ "algorithm" => $alg, 'parameters' => null ],
86
                'hashedMessage' => base64_encode($data),
87
            ],
88
            'nonce' => $nonce,
89
            'certReq' => $requireCert
90
        ];
91
92
        return Encoder::encode($src, static::map());
93
    }
94
95
    public static function map()
96
    {
97
        return [
98
            'tag' => ASN1::TYPE_SEQUENCE,
99
            'children' => [
100
                'version' => [
101
                    'tag' => ASN1::TYPE_INTEGER,
102
                    'map' => [1=>'v1','v2','v3']
103
                ],
104
                'reqPolicy' => [
105
                    'tag' => ASN1::TYPE_OBJECT_IDENTIFIER,
106
                    'optional' => true,
107
                ],
108
                'messageImprint' => [
109
                    'tag' => ASN1::TYPE_SEQUENCE,
110
                    'children' => [
111
                       'hashAlgorithm' => Common::AlgorithmIdentifier(),
112
                       'hashedMessage' => [
113
                            'tag' => ASN1::TYPE_OCTET_STRING
114
                       ]
115
                    ]
116
                ],
117
                'nonce' => [
118
                    'tag' => ASN1::TYPE_INTEGER,
119
                    'optional' => true
120
                ],
121
                'certReq' => [
122
                    'tag' => ASN1::TYPE_BOOLEAN,
123
                    'optional' => true
124
                ]
125
            ]
126
        ];
127
    }
128
}
129