|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Bip70\Test\X509; |
|
6
|
|
|
|
|
7
|
|
|
use Bip70\Exception\X509Exception; |
|
8
|
|
|
use Bip70\Protobuf\Proto\PaymentRequest; |
|
9
|
|
|
use Bip70\Protobuf\Proto\X509Certificates; |
|
10
|
|
|
use Bip70\X509\Exception\InvalidCertificateChainException; |
|
11
|
|
|
use Bip70\X509\Exception\InvalidX509Signature; |
|
12
|
|
|
use Bip70\X509\PKIType; |
|
13
|
|
|
use Bip70\X509\QualifiedCertificate; |
|
14
|
|
|
use Bip70\X509\RequestValidation; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
use Sop\CryptoEncoding\PEM; |
|
17
|
|
|
use X509\Certificate\Certificate; |
|
18
|
|
|
use X509\Certificate\CertificateBundle; |
|
19
|
|
|
|
|
20
|
|
|
class RequestValidationTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testValidateX509DetailsRequiresSignatureType() |
|
23
|
|
|
{ |
|
24
|
|
|
$request = new PaymentRequest(); |
|
25
|
|
|
$request->setPkiType(PKIType::NONE); |
|
26
|
|
|
|
|
27
|
|
|
$validator = new RequestValidation(); |
|
28
|
|
|
|
|
29
|
|
|
$this->expectException(X509Exception::class); |
|
30
|
|
|
$this->expectExceptionMessage("Cannot verify a request without a signature. You should check before calling verify."); |
|
31
|
|
|
|
|
32
|
|
|
$validator->verifyX509Details($request); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testValidateX509SignatureRequiresSignatureType() |
|
36
|
|
|
{ |
|
37
|
|
|
$request = new PaymentRequest(); |
|
38
|
|
|
$request->setPkiType(PKIType::NONE); |
|
39
|
|
|
|
|
40
|
|
|
$validator = new RequestValidation(); |
|
41
|
|
|
$certificate = Certificate::fromPEM(PEM::fromFile(__DIR__ . "/../../data/selfsigned.cert.pem")); |
|
42
|
|
|
|
|
43
|
|
|
$this->expectException(X509Exception::class); |
|
44
|
|
|
$this->expectExceptionMessage("Unknown signature scheme"); |
|
45
|
|
|
|
|
46
|
|
|
$validator->validateX509Signature($certificate, $request); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testValidateCertificateChainRequiresCertificates() |
|
50
|
|
|
{ |
|
51
|
|
|
$x509 = new X509Certificates(); |
|
52
|
|
|
$validator = new RequestValidation(); |
|
53
|
|
|
|
|
54
|
|
|
$this->expectException(InvalidCertificateChainException::class); |
|
55
|
|
|
$this->expectExceptionMessage("No certificates in bundle"); |
|
56
|
|
|
|
|
57
|
|
|
$validator->validateCertificateChain($x509); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testSelfSignedChainValidationFails() |
|
61
|
|
|
{ |
|
62
|
|
|
$cert = Certificate::fromPEM(PEM::fromFile(__DIR__ . "/../../data/selfsigned.cert.pem")); |
|
63
|
|
|
$x509Certs = new X509Certificates(); |
|
64
|
|
|
$x509Certs->addCertificate($cert->toDER()); |
|
65
|
|
|
|
|
66
|
|
|
$validator = new RequestValidation(); |
|
67
|
|
|
|
|
68
|
|
|
$this->expectException(\RuntimeException::class); |
|
69
|
|
|
$this->expectExceptionMessage("No certification paths"); |
|
70
|
|
|
|
|
71
|
|
|
$validator->validateCertificateChain($x509Certs); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testSelfSignedChainValidationSucceedsIfInStore() |
|
75
|
|
|
{ |
|
76
|
|
|
$cert = Certificate::fromPEM(PEM::fromFile(__DIR__ . "/../../data/selfsigned.cert.pem")); |
|
77
|
|
|
$trustStore = new CertificateBundle($cert); |
|
78
|
|
|
$x509Certs = new X509Certificates(); |
|
79
|
|
|
$x509Certs->addCertificate($cert->toDER()); |
|
80
|
|
|
|
|
81
|
|
|
$validator = new RequestValidation(null, $trustStore); |
|
82
|
|
|
|
|
83
|
|
|
$result = $validator->validateCertificateChain($x509Certs); |
|
84
|
|
|
$this->assertInstanceOf(QualifiedCertificate::class, $result); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testDetectsInvalidSignature() |
|
88
|
|
|
{ |
|
89
|
|
|
$cert = Certificate::fromPEM(PEM::fromFile(__DIR__ . "/../../data/selfsigned.cert.pem")); |
|
90
|
|
|
$x509 = new X509Certificates(); |
|
91
|
|
|
$x509->addCertificate($cert); |
|
92
|
|
|
|
|
93
|
|
|
$request = new PaymentRequest(); |
|
94
|
|
|
$request->setPkiType(PKIType::X509_SHA1); |
|
95
|
|
|
$request->setPkiData($x509->serialize()); |
|
96
|
|
|
$request->setSignature("invalid signature"); |
|
97
|
|
|
$request->setSerializedPaymentDetails("serialized details aren't checked"); |
|
98
|
|
|
|
|
99
|
|
|
$validator = new RequestValidation(); |
|
100
|
|
|
|
|
101
|
|
|
$this->expectException(InvalidX509Signature::class); |
|
102
|
|
|
|
|
103
|
|
|
$validator->validateX509Signature($cert, $request); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|