| 1 | <?php |
||
| 25 | class CertificationRequestInfo |
||
| 26 | { |
||
| 27 | const VERSION_1 = 0; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Version. |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $_version; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Subject. |
||
| 38 | * |
||
| 39 | * @var Name |
||
| 40 | */ |
||
| 41 | protected $_subject; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Public key info. |
||
| 45 | * |
||
| 46 | * @var PublicKeyInfo |
||
| 47 | */ |
||
| 48 | protected $_subjectPKInfo; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Attributes. |
||
| 52 | * |
||
| 53 | * @var null|Attributes |
||
| 54 | */ |
||
| 55 | protected $_attributes; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor. |
||
| 59 | * |
||
| 60 | * @param Name $subject Subject |
||
| 61 | * @param PublicKeyInfo $pkinfo Public key info |
||
| 62 | */ |
||
| 63 | 10 | public function __construct(Name $subject, PublicKeyInfo $pkinfo) |
|
| 64 | { |
||
| 65 | 10 | $this->_version = self::VERSION_1; |
|
| 66 | 10 | $this->_subject = $subject; |
|
| 67 | 10 | $this->_subjectPKInfo = $pkinfo; |
|
| 68 | 10 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Initialize from ASN.1. |
||
| 72 | * |
||
| 73 | * @param Sequence $seq |
||
| 74 | * |
||
| 75 | * @throws \UnexpectedValueException |
||
| 76 | * |
||
| 77 | * @return self |
||
| 78 | */ |
||
| 79 | 6 | public static function fromASN1(Sequence $seq): self |
|
| 80 | { |
||
| 81 | 6 | $version = $seq->at(0)->asInteger()->intNumber(); |
|
| 82 | 6 | if (self::VERSION_1 != $version) { |
|
| 83 | 1 | throw new \UnexpectedValueException( |
|
| 84 | 1 | "Version {$version} not supported."); |
|
| 85 | } |
||
| 86 | 5 | $subject = Name::fromASN1($seq->at(1)->asSequence()); |
|
| 87 | 5 | $pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence()); |
|
| 88 | 5 | $obj = new self($subject, $pkinfo); |
|
| 89 | 5 | if ($seq->hasTagged(0)) { |
|
| 90 | 2 | $obj->_attributes = Attributes::fromASN1( |
|
| 91 | 2 | $seq->getTagged(0)->asImplicit(Element::TYPE_SET)->asSet()); |
|
| 92 | } |
||
| 93 | 5 | return $obj; |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get version. |
||
| 98 | * |
||
| 99 | * @return int |
||
| 100 | */ |
||
| 101 | 2 | public function version(): int |
|
| 102 | { |
||
| 103 | 2 | return $this->_version; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get self with subject. |
||
| 108 | * |
||
| 109 | * @param Name $subject |
||
| 110 | * |
||
| 111 | * @return self |
||
| 112 | */ |
||
| 113 | 1 | public function withSubject(Name $subject): self |
|
| 114 | { |
||
| 115 | 1 | $obj = clone $this; |
|
| 116 | 1 | $obj->_subject = $subject; |
|
| 117 | 1 | return $obj; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get subject. |
||
| 122 | * |
||
| 123 | * @return Name |
||
| 124 | */ |
||
| 125 | 4 | public function subject(): Name |
|
| 126 | { |
||
| 127 | 4 | return $this->_subject; |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get subject public key info. |
||
| 132 | * |
||
| 133 | * @return PublicKeyInfo |
||
| 134 | */ |
||
| 135 | 4 | public function subjectPKInfo(): PublicKeyInfo |
|
| 136 | { |
||
| 137 | 4 | return $this->_subjectPKInfo; |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Whether certification request info has attributes. |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | 6 | public function hasAttributes(): bool |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get attributes. |
||
| 152 | * |
||
| 153 | * @throws \LogicException If not set |
||
| 154 | * |
||
| 155 | * @return Attributes |
||
| 156 | */ |
||
| 157 | 6 | public function attributes(): Attributes |
|
| 158 | { |
||
| 159 | 6 | if (!$this->hasAttributes()) { |
|
| 160 | 1 | throw new \LogicException('No attributes.'); |
|
| 161 | } |
||
| 162 | 5 | return $this->_attributes; |
|
|
1 ignored issue
–
show
|
|||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get instance of self with attributes. |
||
| 167 | * |
||
| 168 | * @param Attributes $attribs |
||
| 169 | */ |
||
| 170 | 1 | public function withAttributes(Attributes $attribs): self |
|
| 171 | { |
||
| 172 | 1 | $obj = clone $this; |
|
| 173 | 1 | $obj->_attributes = $attribs; |
|
| 174 | 1 | return $obj; |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get self with extension request attribute. |
||
| 179 | * |
||
| 180 | * @param Extensions $extensions Extensions to request |
||
| 181 | * |
||
| 182 | * @return self |
||
| 183 | */ |
||
| 184 | 3 | public function withExtensionRequest(Extensions $extensions): self |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Generate ASN.1 structure. |
||
| 198 | * |
||
| 199 | * @return Sequence |
||
| 200 | */ |
||
| 201 | 9 | public function toASN1(): Sequence |
|
| 202 | { |
||
| 203 | 9 | $elements = [new Integer($this->_version), |
|
| 204 | 9 | $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1(), ]; |
|
| 205 | 9 | if (isset($this->_attributes)) { |
|
| 206 | 3 | $elements[] = new ImplicitlyTaggedType(0, |
|
| 207 | 3 | $this->_attributes->toASN1()); |
|
|
1 ignored issue
–
show
|
|||
| 208 | } |
||
| 209 | 9 | return new Sequence(...$elements); |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Create signed CertificationRequest. |
||
| 214 | * |
||
| 215 | * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
||
| 216 | * @param PrivateKeyInfo $privkey_info Private key used for signing |
||
| 217 | * @param null|Crypto $crypto Crypto engine, use default if not set |
||
| 218 | * |
||
| 219 | * @return CertificationRequest |
||
| 220 | */ |
||
| 221 | 2 | public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 228 | } |
||
| 229 | } |
||
| 230 |