vendor/guzzlehttp/guzzle/src/Handler/TlsVersion.php

84 lines · 2837 bytes

 1<?php
 2
 3namespace GuzzleHttp\Handler;
 4
 5/**
 6 * @internal
 7 */
 8final class TlsVersion
 9{
10    /**
11     * @param mixed $value
12     */
13    public static function ordinal(string $option, $value): int
14    {
15        if ($value === \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT) {
16            return 10;
17        }
18        if ($value === \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT) {
19            return 11;
20        }
21        if ($value === \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT) {
22            return 12;
23        }
24        if (\defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && $value === \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT) {
25            return 13;
26        }
27
28        throw new \InvalidArgumentException(\sprintf('Invalid %s request option: unknown version provided', $option));
29    }
30
31    /**
32     * @param mixed $min
33     * @param mixed $max
34     */
35    public static function assertRange($min, $max): void
36    {
37        if ($min === null || $max === null) {
38            return;
39        }
40
41        if (self::ordinal('crypto_method_max', $max) < self::ordinal('crypto_method', $min)) {
42            throw new \InvalidArgumentException('Invalid crypto_method_max request option: maximum TLS version must be greater than or equal to crypto_method');
43        }
44    }
45
46    /**
47     * @param mixed $value
48     */
49    public static function streamProtocolVersion(string $option, $value): int
50    {
51        if ($value === \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT) {
52            return self::requireStreamProto('STREAM_CRYPTO_PROTO_TLSv1_0', $option);
53        }
54        if ($value === \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT) {
55            return self::requireStreamProto('STREAM_CRYPTO_PROTO_TLSv1_1', $option);
56        }
57        if ($value === \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT) {
58            return self::requireStreamProto('STREAM_CRYPTO_PROTO_TLSv1_2', $option);
59        }
60        if (\defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && $value === \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT) {
61            return self::requireStreamProto('STREAM_CRYPTO_PROTO_TLSv1_3', $option);
62        }
63
64        throw new \InvalidArgumentException(\sprintf('Invalid %s request option: unknown version provided', $option));
65    }
66
67    /**
68     * Resolves a STREAM_CRYPTO_PROTO_* constant. The ssl.max_proto_version
69     * context option and these constants were added in PHP 7.3.0 (TLS 1.3 in
70     * 7.4.0); on older runtimes the option cannot be honored, so reject loudly.
71     */
72    private static function requireStreamProto(string $constant, string $option): int
73    {
74        if (\defined($constant)) {
75            /** @var int */
76            return \constant($constant);
77        }
78
79        throw new \InvalidArgumentException(\sprintf(
80            'Invalid %s request option: maximum TLS version control is not supported by your version of PHP',
81            $option
82        ));
83    }
84}