aboutsummaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/User/SslClientCert.php
blob: b6545dfad298983e06edb92929855264c518a2bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  AGPL http://www.gnu.org/licenses/agpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

/**
 * SemanticScuttle SSL client certificate management service
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  AGPL http://www.gnu.org/licenses/agpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class SemanticScuttle_Service_User_SslClientCert extends SemanticScuttle_DbService
{
    /**
     * Creates a new instance, sets database variable and table name.
     *
     * @param sql_db $db Database object
     */
    protected function __construct($db)
    {
        $this->db = $db;
        $this->tablename  = $GLOBALS['tableprefix'] .'users_sslclientcerts';
    }

    /**
     * Returns the single service instance
     *
     * @param sql_db $db Database object
     *
     * @return SemanticScuttle_Service_User
     */
    public static function getInstance($db)
    {
        static $instance;
        if (!isset($instance)) {
            $instance = new self($db);
        }
        return $instance;
    }

    /**
     * Determines if the browser provided a valid SSL client certificate
     *
     * @return boolean True if the client cert is there and is valid
     */
    public function hasValidCert()
    {
        if (!isset($_SERVER['SSL_CLIENT_M_SERIAL'])
            || !isset($_SERVER['SSL_CLIENT_V_END'])
            || !isset($_SERVER['SSL_CLIENT_VERIFY'])
            || $_SERVER['SSL_CLIENT_VERIFY'] !== 'SUCCESS'
            || !isset($_SERVER['SSL_CLIENT_I_DN'])
        ) {
            return false;
        }

        if ($_SERVER['SSL_CLIENT_V_REMAIN'] <= 0) {
            return false;
        }

        return true;
    }



    /**
     * Registers the currently available SSL client certificate
     * with the given user. As a result, the user will be able to login
     * using the certifiate
     *
     * @param integer $uId User ID to attach the client cert to.
     *
     * @return boolean True if registration was well, false if not.
     */
    public function registerCurrentCertificate($uId)
    {
        $serial         = $_SERVER['SSL_CLIENT_M_SERIAL'];
        $clientIssuerDn = $_SERVER['SSL_CLIENT_I_DN'];

        $query = 'INSERT INTO ' . $this->getTableName()
            . ' '. $this->db->sql_build_array(
                'INSERT', array(
                    'uId'               => $uId,
                    'sslSerial'         => $serial,
                    'sslClientIssuerDn' => $clientIssuerDn,
                    'sslName'           => $_SERVER['SSL_CLIENT_S_DN_CN'],
                    'sslEmail'          => $_SERVER['SSL_CLIENT_S_DN_Email']
                )
            );
        if (!($dbresult = $this->db->sql_query($query))) {
            message_die(
                GENERAL_ERROR, 'Could not load user for client certificate',
                '', __LINE__, __FILE__, $query, $this->db
            );
            return false;
        }

        return true;
    }



    /**
     * Takes values from the currently available SSL client certificate
     * and adds the available profile data to the user.
     *
     * @param integer $uId User ID to attach the client cert to.
     *
     * @return array Array of profile data that were registered.
     *               Database column name as key, new value as value
     */
    public function updateProfileFromCurentCert($uId)
    {
        $arData = array();

        if (isset($_SERVER['SSL_CLIENT_S_DN_CN'])
            && trim($_SERVER['SSL_CLIENT_S_DN_CN']) != ''
        ) {
            $arData['name'] = trim($_SERVER['SSL_CLIENT_S_DN_CN']);
        }

        if (count($arData)) {
            $us = SemanticScuttle_Service_Factory::get('User');
            foreach ($arData as $column => $value) {
                $us->_updateuser($uId, $column, $value);
            }
        }
        return $arData;
    }



    /**
     * Tries to detect the user ID from the SSL client certificate passed
     * to the web server.
     *
     * @return mixed Integer user ID if the certificate is valid and
     *               assigned to a user, boolean false otherwise
     */
    public function getUserIdFromCert()
    {
        if (!$this->hasValidCert()) {
            return false;
        }

        $serial         = $_SERVER['SSL_CLIENT_M_SERIAL'];
        $clientIssuerDn = $_SERVER['SSL_CLIENT_I_DN'];

        $query = 'SELECT uId'
            . ' FROM ' . $this->getTableName()
            . ' WHERE sslSerial = \'' . $this->db->sql_escape($serial) . '\''
            . ' AND sslClientIssuerDn = \''
            . $this->db->sql_escape($clientIssuerDn)
            . '\'';
        if (!($dbresult = $this->db->sql_query($query))) {
            message_die(
                GENERAL_ERROR, 'Could not load user for client certificate',
                '', __LINE__, __FILE__, $query, $this->db
            );
            return false;
        }

        $row = $this->db->sql_fetchrow($dbresult);
        $this->db->sql_freeresult($dbresult);

        if (!$row) {
            return false;
        }
        return (int)$row['uId'];
    }


    /**
     * Fetches all registered certificates for the user from the database
     * and returns it.
     *
     * @return array Array with all certificates for the user. Empty if
     *               there are none, SemanticScuttle_Model_User_SslClientCert
     *               objects otherwise.
     */
    public function getUserCerts($uId)
    {
        $query = 'SELECT * FROM ' . $this->getTableName()
            . ' ORDER BY sslSerial DESC';
        if (!($dbresult = $this->db->sql_query($query))) {
            message_die(
                GENERAL_ERROR, 'Could not load SSL client certificates',
                '', __LINE__, __FILE__, $query, $this->db
            );
            return array();
        }

        $certs = array();
        while ($row = $this->db->sql_fetchrow($dbresult)) {
            $certs[] = SemanticScuttle_Model_User_SslClientCert::fromDb($row);
        }
        $this->db->sql_freeresult($dbresult);
        return $certs;
    }



    /**
     * Deletes a SSL client certificate.
     * No security checks are made here.
     *
     * @param mixed $cert Certificate object or certificate database id.
     *                    Objects are of type
     *                    SemanticScuttle_Model_User_SslClientCert
     *
     * @return boolean True if all went well, false if it could not be deleted
     */
    public function delete($cert)
    {
        if ($cert instanceof SemanticScuttle_Model_User_SslClientCert) {
            $id = (int)$cert->id;
        } else {
            $id = (int)$cert;
        }

        if ($id === 0) {
            return false;
        }

        $query = 'DELETE FROM ' . $this->getTableName()
            .' WHERE uId = ' . $id;

        if (!($dbresult = $this->db->sql_query($query))) {
            message_die(
                GENERAL_ERROR, 'Could not delete user certificate',
                '', __LINE__, __FILE__, $query, $this->db
            );
            return false;
        }

        return true;
    }
}
?>