|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * EncapsulatedContentInfo |
| 4 | + * |
| 5 | + * @author Nurlan Mukhanov <nurike@gmail.com> |
| 6 | + * @copyright 2020 Nurlan Mukhanov |
| 7 | + * @license https://en.wikipedia.org/wiki/MIT_License MIT License |
| 8 | + * @link https://github.com/Falseclock/AdvancedCMS |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Falseclock\AdvancedCMS; |
| 12 | + |
| 13 | +use Adapik\CMS\Algorithm; |
| 14 | +use Adapik\CMS\Certificate; |
| 15 | +use Adapik\CMS\Exception\FormatException; |
| 16 | +use Exception; |
| 17 | +use FG\ASN1\Exception\ParserException; |
| 18 | +use FG\ASN1\ExplicitlyTaggedObject; |
| 19 | +use FG\ASN1\Universal\BitString; |
| 20 | +use FG\ASN1\Universal\Integer; |
| 21 | +use FG\ASN1\Universal\NullObject; |
| 22 | +use FG\ASN1\Universal\ObjectIdentifier; |
| 23 | +use FG\ASN1\Universal\OctetString; |
| 24 | +use FG\ASN1\Universal\Sequence; |
| 25 | + |
| 26 | +class Template |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @param Certificate $publicCertificate |
| 30 | + * @param Certificate $intermediateCertificate |
| 31 | + * @param string $hashAlgorithmOID |
| 32 | + * @return OCSPRequest |
| 33 | + * @throws FormatException |
| 34 | + * @throws ParserException |
| 35 | + */ |
| 36 | + public static function OCSPRequest(Certificate $publicCertificate, Certificate $intermediateCertificate, string $hashAlgorithmOID = Algorithm::OID_SHA1): OCSPRequest |
| 37 | + { |
| 38 | + /** @see Maps\TBSRequest */ |
| 39 | + $tbsRequest = Sequence::create([ |
| 40 | + // потомки TBSRequest |
| 41 | + # -- version |
| 42 | + # -- requestorName |
| 43 | + # requestList |
| 44 | + Sequence::create([ |
| 45 | + /** @see Request */ |
| 46 | + Sequence::create([ |
| 47 | + // потомки Request |
| 48 | + # reqCert |
| 49 | + /** @see CertID */ |
| 50 | + Sequence::create([ |
| 51 | + // потомки CertID |
| 52 | + # hashAlgorithm |
| 53 | + /** @see AlgorithmIdentifier */ |
| 54 | + Sequence::create([ |
| 55 | + // потомки AlgorithmIdentifier |
| 56 | + # algorithm |
| 57 | + ObjectIdentifier::create($hashAlgorithmOID), |
| 58 | + # parameters |
| 59 | + NullObject::create() |
| 60 | + ] |
| 61 | + ), |
| 62 | + # issuerNameHash |
| 63 | + OctetString::createFromString(self::getNameHash($hashAlgorithmOID, $intermediateCertificate)), |
| 64 | + # issuerKeyHash |
| 65 | + OctetString::createFromString(self::getKeyHash($hashAlgorithmOID, $intermediateCertificate)), # serialNumber |
| 66 | + Integer::create($publicCertificate->getSerial()) |
| 67 | + ] |
| 68 | + ) |
| 69 | + # singleRequestExtensions |
| 70 | + ] |
| 71 | + ) |
| 72 | + ] |
| 73 | + ), |
| 74 | + # requestExtensions |
| 75 | + ExplicitlyTaggedObject::create(2, |
| 76 | + Sequence::create([ |
| 77 | + Sequence::create([ |
| 78 | + ObjectIdentifier::create(OCSPRequest::OID_OCSPNonce), |
| 79 | + OctetString::createFromString(OctetString::createFromString((string)self::generateNonce())->getBinary()) |
| 80 | + ] |
| 81 | + ) |
| 82 | + ] |
| 83 | + ) |
| 84 | + ) |
| 85 | + ] |
| 86 | + ); |
| 87 | + |
| 88 | + return new OCSPRequest(Sequence::create([$tbsRequest/*, $optionalSignature*/])); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * @param int|null $length |
| 94 | + * @return string |
| 95 | + * @throws Exception |
| 96 | + */ |
| 97 | + private static function generateNonce(int $length = null): string |
| 98 | + { |
| 99 | + return random_bytes($length ?? OCSPRequest::OCSP_DEFAULT_NONCE_LENGTH); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + /** |
| 104 | + * @param string $algorithmOID |
| 105 | + * @param Certificate $certificate |
| 106 | + * @return string |
| 107 | + * @throws FormatException |
| 108 | + * @throws ParserException |
| 109 | + */ |
| 110 | + private static function getNameHash(string $algorithmOID, Certificate $certificate): string |
| 111 | + { |
| 112 | + $binary = $certificate->getBinary(); |
| 113 | + /** @var Sequence $certificate */ |
| 114 | + $certificate = Sequence::fromBinary($binary); |
| 115 | + return Algorithm::hashValue($algorithmOID, self::_getTBSCertificate($certificate)->getChildren()[5]->getBinary()); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param string $algorithmOID |
| 120 | + * @param Certificate $certificate |
| 121 | + * @return string |
| 122 | + * @throws FormatException |
| 123 | + * @throws ParserException |
| 124 | + */ |
| 125 | + public static function getKeyHash(string $algorithmOID, Certificate $certificate): string |
| 126 | + { |
| 127 | + $binary = $certificate->getBinary(); |
| 128 | + /** @var Sequence $certificate */ |
| 129 | + $certificate = Sequence::fromBinary($binary); |
| 130 | + $child = self::_getTBSCertificate($certificate)->getChildren()[6]; |
| 131 | + /** @var BitString $octet */ |
| 132 | + $octet = $child->findChildrenByType(BitString::class)[0]; |
| 133 | + |
| 134 | + return Algorithm::hashValue($algorithmOID, hex2bin($octet->getStringValue())); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @param Sequence $certificate |
| 139 | + * @return Sequence |
| 140 | + * @throws Exception |
| 141 | + */ |
| 142 | + private static function _getTBSCertificate(Sequence $certificate): Sequence |
| 143 | + { |
| 144 | + return $certificate->findChildrenByType(Sequence::class)[0]; |
| 145 | + } |
| 146 | +} |
0 commit comments