0e2c1753f920a654bd059248c74b865023fb4f51
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / Classes / Keypair.php
1 <?php
2 namespace TYPO3\CMS\Rsaauth;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Dmitry Dulepov <dmitry@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * This class contain an RSA key pair. Its purpose is to keep to keys
29 * and transfer these keys between other PHP classes.
30 *
31 * @author Dmitry Dulepov <dmitry@typo3.org>
32 */
33 class Keypair implements \TYPO3\CMS\Core\SingletonInterface {
34 /**
35 * RSA public exponent (3 or 0x10001)
36 *
37 * @var integer
38 */
39 protected $exponent = 0;
40
41 /**
42 * The private key
43 *
44 * @var string
45 */
46 protected $privateKey = '';
47
48 /**
49 * The public key modulus
50 *
51 * @var integer
52 */
53 protected $publicKeyModulus = 0;
54
55 /**
56 * Checks if this key pair already has been provided with all data.
57 *
58 * @return boolean
59 */
60 public function isReady() {
61 return $this->hasExponent() && $this->hasPrivateKey() && $this->hasPublicKeyModulus();
62 }
63
64 /**
65 * Retrieves the exponent.
66 *
67 * @return integer the exponent
68 */
69 public function getExponent() {
70 return $this->exponent;
71 }
72
73 /**
74 * Sets the exponent
75 *
76 * Note: This method must not be called more than one time.
77 *
78 * @param integer $exponent the new exponent
79 *
80 * @return void
81 *
82 * @throws \BadMethodCallException if the method was called more than one time
83 */
84 public function setExponent($exponent) {
85 if ($this->hasExponent()) {
86 throw new \BadMethodCallException('setExponent() must not be called more than one time.', 1296062830);
87 }
88
89 $this->exponent = $exponent;
90 }
91
92 /**
93 * Checks whether an exponent already has been set.
94 *
95 * @return boolean
96 */
97 protected function hasExponent() {
98 return $this->getExponent() !== 0;
99 }
100
101 /**
102 * Retrieves the private key.
103 *
104 * @return string The private key
105 */
106 public function getPrivateKey() {
107 return $this->privateKey;
108 }
109
110 /**
111 * Sets the private key.
112 *
113 * Note: This method must not be called more than one time.
114 *
115 * @param string $privateKey The new private key
116 *
117 * @return void
118 *
119 * @throws \BadMethodCallException if the method was called more than one time
120 */
121 public function setPrivateKey($privateKey) {
122 if ($this->hasPrivateKey()) {
123 throw new \BadMethodCallException('setPrivateKey() must not be called more than one time.', 1296062831);
124 }
125
126 $this->privateKey = $privateKey;
127 }
128
129 /**
130 * Checks whether a private key already has been set.
131 *
132 * @return boolean
133 */
134 protected function hasPrivateKey() {
135 return $this->getPrivateKey() !== '';
136 }
137
138 /**
139 * Retrieves the public key modulus
140 *
141 * @return integer the public key modulus
142 */
143 public function getPublicKeyModulus() {
144 return $this->publicKeyModulus;
145 }
146
147 /**
148 * Sets the public key modulus.
149 *
150 * Note: This method must not be called more than one time.
151 *
152 * @param integer $publicKeyModulus the new public key modulus
153 *
154 * @return void
155 *
156 * @throws \BadMethodCallException if the method was called more than one time
157 */
158 public function setPublicKey($publicKeyModulus) {
159 if ($this->hasPublicKeyModulus()) {
160 throw new \BadMethodCallException('setPublicKey() must not be called more than one time.', 1296062832);
161 }
162
163 $this->publicKeyModulus = $publicKeyModulus;
164 }
165
166 /**
167 * Checks whether a public key modulus already has been set.
168 *
169 * @return boolean
170 */
171 protected function hasPublicKeyModulus() {
172 return $this->getPublicKeyModulus() !== 0;
173 }
174 }