|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jose\Algorithm\Signature; |
|
13
|
|
|
|
|
14
|
|
|
use Assert\Assertion; |
|
15
|
|
|
use Jose\Algorithm\SignatureAlgorithmInterface; |
|
16
|
|
|
use Jose\KeyConverter\RSAKey; |
|
17
|
|
|
use Jose\Object\JWKInterface; |
|
18
|
|
|
use Jose\Util\RSA as JoseRSA; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class RSA. |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class RSA implements SignatureAlgorithmInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Probabilistic Signature Scheme. |
|
27
|
|
|
*/ |
|
28
|
|
|
const SIGNATURE_PSS = 1; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Use the PKCS#1. |
|
32
|
|
|
*/ |
|
33
|
|
|
const SIGNATURE_PKCS1 = 2; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract protected function getAlgorithm(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract protected function getSignatureMethod(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function verify(JWKInterface $key, $input, $signature) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->checkKey($key); |
|
51
|
|
|
|
|
52
|
|
|
$pub = RSAKey::toPublic(new RSAKey($key)); |
|
53
|
|
|
|
|
54
|
|
|
if ($this->getSignatureMethod() === self::SIGNATURE_PSS) { |
|
55
|
|
|
|
|
56
|
|
|
return JoseRSA::verify($pub, $input, $signature, $this->getAlgorithm()); |
|
|
|
|
|
|
57
|
|
|
} else { |
|
58
|
|
|
|
|
59
|
|
|
return 1 === openssl_verify($input, $signature, $pub->toPEM(), $this->getAlgorithm()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function sign(JWKInterface $key, $input) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->checkKey($key); |
|
69
|
|
|
Assertion::true($key->has('d'), 'The key is not a private key'); |
|
70
|
|
|
|
|
71
|
|
|
$priv = new RSAKey($key); |
|
72
|
|
|
|
|
73
|
|
|
if ($this->getSignatureMethod() === self::SIGNATURE_PSS) { |
|
74
|
|
|
$result = JoseRSA::sign($priv, $input, $this->getAlgorithm()); |
|
|
|
|
|
|
75
|
|
|
Assertion::string($result, 'An error occurred during the creation of the signature'); |
|
76
|
|
|
|
|
77
|
|
|
return $result; |
|
78
|
|
|
} else { |
|
79
|
|
|
$result = openssl_sign($input, $signature, $priv->toPEM(), $this->getAlgorithm()); |
|
80
|
|
|
Assertion::true($result, 'Unable to sign'); |
|
81
|
|
|
|
|
82
|
|
|
return $signature; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param JWKInterface $key |
|
88
|
|
|
*/ |
|
89
|
|
|
private function checkKey(JWKInterface $key) |
|
90
|
|
|
{ |
|
91
|
|
|
Assertion::eq($key->get('kty'), 'RSA', 'Wrong key type.'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.