1+ <?php
2+
3+ /*
4+ * Copyright (c) 2020-2021 Estonian Information System Authority
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in all
14+ * copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
25+ namespace web_eid \web_eid_authtoken_validation_php \certificate ;
26+
27+ use web_eid \web_eid_authtoken_validation_php \util \X509 ;
28+ use UnexpectedValueException ;
29+ use BadFunctionCallException ;
30+
31+ final class CertificateData
32+ {
33+
34+ public function __construct ()
35+ {
36+ throw new BadFunctionCallException ('Utility class ' );
37+ }
38+
39+ /**
40+ * Get commonName from x509 certificate
41+ *
42+ * @throws UnexpectedValueException
43+ */
44+ public static function getSubjectCN (X509 $ certificate ): string
45+ {
46+ return self ::getField ($ certificate , 'CN ' );
47+ }
48+
49+ /**
50+ * Get surname from x509 certificate
51+ *
52+ * @throws UnexpectedValueException
53+ */
54+ public static function getSubjectSurname (X509 $ certificate ): string
55+ {
56+ return self ::getField ($ certificate , 'SN ' );
57+ }
58+
59+ /**
60+ * Get given name from x509 certificate
61+ *
62+ * @throws UnexpectedValueException
63+ */
64+ public static function getSubjectGivenName (X509 $ certificate ): string
65+ {
66+ return self ::getField ($ certificate , 'GN ' );
67+ }
68+
69+ /**
70+ * Get serialNumber (ID-code) from x509 certificate
71+ *
72+ * @throws UnexpectedValueException
73+ */
74+ public static function getSubjectIdCode (X509 $ certificate ): string
75+ {
76+ return self ::getField ($ certificate , 'serialNumber ' );
77+ }
78+
79+ /**
80+ * Get country code from x509 certificate
81+ *
82+ * @throws UnexpectedValueException
83+ */
84+ public static function getSubjectCountryCode (X509 $ certificate ): string
85+ {
86+ return self ::getField ($ certificate , 'C ' );
87+ }
88+
89+ /**
90+ * Get specified subject field from x509 certificate
91+ *
92+ * @throws UnexpectedValueException field identifier not found
93+ * @return string
94+ */
95+ private static function getField (X509 $ certificate , string $ fieldId ): string
96+ {
97+ $ result = $ certificate ->getSubjectProp ($ fieldId );
98+ if ($ result ) {
99+ return $ result ;
100+ }
101+ throw new UnexpectedValueException ('fieldId ' .$ fieldId .' not found in certificate subject ' );
102+ }
103+
104+
105+ }
0 commit comments