blob: bbe4fc7f0135b12e071a8bf40e66fd4f39994942 [file] [log] [blame]
Mao Huang700663d2015-08-12 09:58:59 +08001# Copyright 2015 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""The DRM Keys Provisioning Server (DKPS) implementation."""
6
7# TODO(littlecvr): Implement "without filter mode", which lets OEM encrypts DRM
8# keys directly with ODM's public key, and the key server
9# merely stores them without knowing anything about them.
10
Yilin Yang71e39412019-09-24 09:26:46 +080011from __future__ import print_function
12
Mao Huang700663d2015-08-12 09:58:59 +080013import argparse
Mao Huang9076f632015-09-24 17:38:47 +080014import hashlib
Mao Huang700663d2015-08-12 09:58:59 +080015import imp
16import json
Mao Huang041483e2015-09-14 23:28:18 +080017import logging
Mao Huang901437f2016-06-24 11:39:15 +080018import logging.config
Mao Huang700663d2015-08-12 09:58:59 +080019import os
20import shutil
Mao Huang700663d2015-08-12 09:58:59 +080021import sqlite3
22import textwrap
Yilin Yang2a2bb112019-10-23 11:20:33 +080023import xmlrpc.server
Mao Huang700663d2015-08-12 09:58:59 +080024
25import gnupg
Yilin Yang8cc5dfb2019-10-22 15:58:53 +080026from six.moves import input
Mao Huang700663d2015-08-12 09:58:59 +080027
28
29SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
30FILTERS_DIR = os.path.join(SCRIPT_DIR, 'filters')
Mao Huang9076f632015-09-24 17:38:47 +080031PARSERS_DIR = os.path.join(SCRIPT_DIR, 'parsers')
Mao Huang700663d2015-08-12 09:58:59 +080032CREATE_DATABASE_SQL_FILE_PATH = os.path.join(
33 SCRIPT_DIR, 'sql', 'create_database.sql')
34
Mao Huang901437f2016-06-24 11:39:15 +080035DEFAULT_BIND_ADDR = '0.0.0.0' # all addresses
36DEFAULT_BIND_PORT = 5438
37
38DEFAULT_DATABASE_FILE_NAME = 'dkps.db'
39DEFAULT_GNUPG_DIR_NAME = 'gnupg'
40DEFAULT_LOG_FILE_NAME = 'dkps.log'
41
42DEFAULT_LOGGING_CONFIG = {
43 'version': 1,
44 'formatters': {
45 'default': {
46 'format': '%(asctime)s:%(levelname)s:%(funcName)s:'
47 '%(lineno)d:%(message)s'}},
48 'handlers': {
49 'file': {
50 'class': 'logging.handlers.RotatingFileHandler',
51 'formatter': 'default',
52 'filename': DEFAULT_LOG_FILE_NAME,
53 'maxBytes': 1024 * 1024, # 1M
54 'backupCount': 3},
55 'console': {
56 'class': 'logging.StreamHandler',
57 'formatter': 'default',
58 'stream': 'ext://sys.stdout'}},
59 'root': {
60 'level': 'INFO',
61 # only log to file by default, but also log to console if invoked
62 # directly from the command line
63 'handlers': ['file'] + ['console'] if __name__ == '__main__' else []}}
64
Mao Huang700663d2015-08-12 09:58:59 +080065
66class ProjectNotFoundException(ValueError):
67 """Raised when no project was found in the database."""
68 pass
69
70
71class InvalidUploaderException(ValueError):
72 """Raised when the signature of the uploader can't be verified."""
73 pass
74
75
76class InvalidRequesterException(ValueError):
77 """Raised when the signature of the requester can't be verified."""
78 pass
79
80
81def GetSQLite3Connection(database_file_path):
82 """Returns a tuple of SQLite3's (connection, cursor) to database_file_path.
83
84 If the connection has been created before, it is returned directly. If it's
85 not, this function creates the connection, ensures that the foreign key
86 constraint is enabled, and returns.
87
88 Args:
89 database_file_path: path to the SQLite3 database file.
90 """
91 database_file_path = os.path.realpath(database_file_path)
92
93 # Return if the connection to database_file_path has been created before.
94 try:
95 connection = GetSQLite3Connection.connection_dict[database_file_path]
96 return (connection, connection.cursor())
97 except KeyError:
98 pass
99 except AttributeError:
100 GetSQLite3Connection.connection_dict = {}
101
102 # Create connection.
103 connection = sqlite3.connect(database_file_path)
104 connection.row_factory = sqlite3.Row
105 cursor = connection.cursor()
106
107 # Enable foreign key constraint since SQLite3 disables it by default.
108 cursor.execute('PRAGMA foreign_keys = ON')
109 # Check if foreign key constraint is enabled.
110 cursor.execute('PRAGMA foreign_keys')
111 if cursor.fetchone()[0] != 1:
112 raise RuntimeError('Failed to enable SQLite3 foreign key constraint')
113
114 GetSQLite3Connection.connection_dict[database_file_path] = connection
115
116 return (connection, cursor)
117
118
119class DRMKeysProvisioningServer(object):
120 """The DRM Keys Provisioning Server (DKPS) class."""
121
122 def __init__(self, database_file_path, gnupg_homedir):
123 """DKPS constructor.
124
125 Args:
126 database_file_path: path to the SQLite3 database file.
127 gnupg_homedir: path to the GnuPG home directory.
128 """
129 self.database_file_path = database_file_path
130 self.gnupg_homedir = gnupg_homedir
131
132 if not os.path.isdir(self.gnupg_homedir):
133 self.gpg = None
134 else:
chuntsenf0780db2019-05-03 02:21:59 +0800135 self.gpg = gnupg.GPG(gnupghome=self.gnupg_homedir)
Mao Huang700663d2015-08-12 09:58:59 +0800136
137 if not os.path.isfile(self.database_file_path):
138 self.db_connection, self.db_cursor = (None, None)
139 else:
140 self.db_connection, self.db_cursor = GetSQLite3Connection(
141 self.database_file_path)
142
Mao Huang3963b372015-12-11 15:20:19 +0800143 def Initialize(self, gpg_gen_key_args_dict=None, server_key_file_path=None):
144 """Creates the SQLite3 database and GnuPG home, and imports, or generates a
145 GPG key for the server to use.
Mao Huang700663d2015-08-12 09:58:59 +0800146
147 Args:
148 gpg_gen_key_args_dict: will be passed directly as the keyword arguments to
Mao Huang3963b372015-12-11 15:20:19 +0800149 python-gnupg's gen_key() function if server_key_file_path is None.
150 Can be used to customize the key generator process, such as key_type,
151 key_length, etc. See python-gnupg's doc for what can be customized.
152 server_key_file_path: path to the server key to use. If not None, the
153 system will simply import this key and use it as the server key; if
154 None, the system will generate a new key.
Mao Huang700663d2015-08-12 09:58:59 +0800155
156 Raises:
157 RuntimeError is the database and GnuPG home have already been initialized.
158 """
Mao Huang700663d2015-08-12 09:58:59 +0800159 # Create GPG instance and database connection.
chuntsenf0780db2019-05-03 02:21:59 +0800160 self.gpg = gnupg.GPG(gnupghome=self.gnupg_homedir)
Mao Huang700663d2015-08-12 09:58:59 +0800161 self.db_connection, self.db_cursor = GetSQLite3Connection(
162 self.database_file_path)
163
Mao Huang3963b372015-12-11 15:20:19 +0800164 # If any key exists, the system has already been initialized.
165 if self.gpg.list_keys():
166 raise RuntimeError('Already initialized')
Mao Huang700663d2015-08-12 09:58:59 +0800167
Mao Huang3963b372015-12-11 15:20:19 +0800168 if server_key_file_path: # use existing key
Mao Huang901437f2016-06-24 11:39:15 +0800169 # TODO(littlecvr): make sure the server key doesn't have passphrase.
Mao Huang3963b372015-12-11 15:20:19 +0800170 server_key_fingerprint, _ = self._ImportGPGKey(server_key_file_path)
171 else: # generate a new GPG key
172 if gpg_gen_key_args_dict is None:
173 gpg_gen_key_args_dict = {}
174 if 'name_real' not in gpg_gen_key_args_dict:
175 gpg_gen_key_args_dict['name_real'] = 'DKPS Server'
176 if 'name_email' not in gpg_gen_key_args_dict:
177 gpg_gen_key_args_dict['name_email'] = 'chromeos-factory-dkps@google.com'
178 if 'name_comment' not in gpg_gen_key_args_dict:
179 gpg_gen_key_args_dict['name_comment'] = 'DRM Keys Provisioning Server'
180 key_input_data = self.gpg.gen_key_input(**gpg_gen_key_args_dict)
181 server_key_fingerprint = self.gpg.gen_key(key_input_data).fingerprint
Mao Huang700663d2015-08-12 09:58:59 +0800182
183 # Create and set up the schema of the database.
184 with open(CREATE_DATABASE_SQL_FILE_PATH) as f:
185 create_database_sql = f.read()
186 with self.db_connection:
187 self.db_cursor.executescript(create_database_sql)
188
189 # Record the server key fingerprint.
190 with self.db_connection:
191 self.db_cursor.execute(
192 'INSERT INTO settings (key, value) VALUES (?, ?)',
Mao Huang3963b372015-12-11 15:20:19 +0800193 ('server_key_fingerprint', server_key_fingerprint))
Mao Huang700663d2015-08-12 09:58:59 +0800194
195 def Destroy(self):
196 """Destroys the database and GnuPG home directory.
197
198 This is the opposite of Initialize(). It essentially removes the SQLite3
199 database file and GnuPG home directory.
200 """
201 # Remove database.
202 if self.db_connection:
203 self.db_connection.close()
204 if os.path.exists(self.database_file_path):
205 os.remove(self.database_file_path)
206
207 # Remove GnuPG home.
208 if self.gpg:
209 self.gpg = None
210 if os.path.exists(self.gnupg_homedir):
211 shutil.rmtree(self.gnupg_homedir)
212
213 def AddProject(self, name, uploader_key_file_path, requester_key_file_path,
Mao Huang9076f632015-09-24 17:38:47 +0800214 parser_module_file_name, filter_module_file_name=None):
Mao Huang700663d2015-08-12 09:58:59 +0800215 """Adds a project.
216
217 Args:
218 name: name of the project, must be unique.
219 uploader_key_file_path: path to the OEM's public key file.
220 requester_key_file_path: path to the ODM's public key file.
Mao Huang9076f632015-09-24 17:38:47 +0800221 parser_module_file_name: file name of the parser python module.
Mao Huang700663d2015-08-12 09:58:59 +0800222 filter_module_file_name: file name of the filter python module.
223
224 Raises:
225 ValueError if either the uploader's or requester's key are imported (which
226 means they are used by another project).
227 """
Mao Huang9076f632015-09-24 17:38:47 +0800228 # Try to load the parser and filter modules.
229 self._LoadParserModule(parser_module_file_name)
Mao Huang700663d2015-08-12 09:58:59 +0800230 if filter_module_file_name is not None:
231 self._LoadFilterModule(filter_module_file_name)
232
233 # Try to import uploader and requester keys and add project info into the
234 # database, if failed at any step, delete imported keys.
235 uploader_key_fingerprint, requester_key_fingerprint = (None, None)
236 uploader_key_already_exists, requester_key_already_exists = (False, False)
237 try:
238 uploader_key_fingerprint, uploader_key_already_exists = (
239 self._ImportGPGKey(uploader_key_file_path))
240 if uploader_key_already_exists:
241 raise ValueError('Uploader key already exists')
242 requester_key_fingerprint, requester_key_already_exists = (
243 self._ImportGPGKey(requester_key_file_path))
244 if requester_key_already_exists:
245 raise ValueError('Requester key already exists')
246 with self.db_connection:
247 self.db_cursor.execute(
Mao Huang9076f632015-09-24 17:38:47 +0800248 'INSERT INTO projects ('
249 ' name, uploader_key_fingerprint, requester_key_fingerprint, '
250 ' parser_module_file_name, filter_module_file_name) '
251 'VALUES (?, ?, ?, ?, ?)',
Mao Huang700663d2015-08-12 09:58:59 +0800252 (name, uploader_key_fingerprint, requester_key_fingerprint,
Mao Huang9076f632015-09-24 17:38:47 +0800253 parser_module_file_name, filter_module_file_name))
Mao Huang700663d2015-08-12 09:58:59 +0800254 except BaseException:
255 if not uploader_key_already_exists and uploader_key_fingerprint:
256 self.gpg.delete_keys(uploader_key_fingerprint)
257 if not requester_key_already_exists and requester_key_fingerprint:
258 self.gpg.delete_keys(requester_key_fingerprint)
259 raise
260
261 def UpdateProject(self, name, uploader_key_file_path=None,
262 requester_key_file_path=None, filter_module_file_name=None):
263 """Updates a project.
264
265 Args:
266 name: name of the project, must be unique.
267 uploader_key_file_path: path to the OEM's public key file.
268 requester_key_file_path: path to the ODM's public key file.
269 filter_module_file_name: file name of the filter python module.
270
271 Raises:
272 RuntimeError if SQLite3 can't update the project row (for any reason).
273 """
274 # Try to load the filter module.
275 if filter_module_file_name is not None:
276 self._LoadFilterModule(filter_module_file_name)
277
278 project = self._FetchProjectByName(name)
279
280 # Try to import uploader and requester keys and add project info into the
281 # database, if failed at any step, delete any newly imported keys.
282 uploader_key_fingerprint, requester_key_fingerprint = (None, None)
283 old_uploader_key_fingerprint = project['uploader_key_fingerprint']
284 old_requester_key_fingerprint = project['requester_key_fingerprint']
285 same_uploader_key, same_requester_key = (True, True)
286 try:
287 sql_set_clause_list = ['filter_module_file_name = ?']
288 sql_parameters = [filter_module_file_name]
289
290 if uploader_key_file_path:
291 uploader_key_fingerprint, same_uploader_key = self._ImportGPGKey(
292 uploader_key_file_path)
293 sql_set_clause_list.append('uploader_key_fingerprint = ?')
294 sql_parameters.append(uploader_key_fingerprint)
295
296 if requester_key_file_path:
297 requester_key_fingerprint, same_requester_key = self._ImportGPGKey(
298 uploader_key_file_path)
299 sql_set_clause_list.append('requester_key_fingerprint = ?')
300 sql_parameters.append(requester_key_fingerprint)
301
302 sql_set_clause = ','.join(sql_set_clause_list)
303 sql_parameters.append(name)
304 with self.db_connection:
305 self.db_cursor.execute(
306 'UPDATE projects SET %s WHERE name = ?' % sql_set_clause,
307 tuple(sql_parameters))
308 if self.db_cursor.rowcount != 1:
309 raise RuntimeError('Failed to update project %s' % name)
310 except BaseException:
311 if not same_uploader_key and uploader_key_fingerprint:
312 self.gpg.delete_keys(uploader_key_fingerprint)
313 if not same_requester_key and requester_key_fingerprint:
314 self.gpg.delete_keys(requester_key_fingerprint)
315 raise
316
317 if not same_uploader_key:
318 self.gpg.delete_keys(old_uploader_key_fingerprint)
319 if not same_requester_key:
320 self.gpg.delete_keys(old_requester_key_fingerprint)
321
322 def RemoveProject(self, name):
323 """Removes a project.
324
325 Args:
326 name: the name of the project specified when added.
327 """
328 project = self._FetchProjectByName(name)
329
330 self.gpg.delete_keys(project['uploader_key_fingerprint'])
331 self.gpg.delete_keys(project['requester_key_fingerprint'])
332
333 with self.db_connection:
334 self.db_cursor.execute(
335 'DELETE FROM drm_keys WHERE project_name = ?', (name,))
336 self.db_cursor.execute('DELETE FROM projects WHERE name = ?', (name,))
337
338 def ListProjects(self):
339 """Lists all projects."""
340 self.db_cursor.execute('SELECT * FROM projects ORDER BY name ASC')
341 return self.db_cursor.fetchall()
342
343 def Upload(self, encrypted_serialized_drm_keys):
344 """Uploads a list of DRM keys to the server. This is an atomic operation. It
345 will either succeed and save all the keys, or fail and save no keys.
346
347 Args:
348 encrypted_serialized_drm_keys: the serialized DRM keys signed by the
349 uploader and encrypted by the server's public key.
350
351 Raises:
352 InvalidUploaderException if the signature of the uploader can not be
353 verified.
354 """
355 decrypted_obj = self.gpg.decrypt(encrypted_serialized_drm_keys)
356 project = self._FetchProjectByUploaderKeyFingerprint(
357 decrypted_obj.fingerprint)
358 serialized_drm_keys = decrypted_obj.data
359
Mao Huang9076f632015-09-24 17:38:47 +0800360 # Pass to the parse function.
361 parser_module = self._LoadParserModule(project['parser_module_file_name'])
362 drm_key_list = parser_module.Parse(serialized_drm_keys)
363
364 drm_key_hash_list = []
365 for drm_key in drm_key_list:
Yilin Yang0412c272019-12-05 16:57:40 +0800366 drm_key_hash = hashlib.sha1(
367 json.dumps(drm_key).encode('utf-8')).hexdigest()
368 drm_key_hash_list.append(drm_key_hash)
Mao Huang9076f632015-09-24 17:38:47 +0800369
Mao Huangecbeb122016-06-22 20:30:38 +0800370 # Pass to the filter function if needed.
371 if project['filter_module_file_name']: # filter module can be null
372 filter_module = self._LoadFilterModule(project['filter_module_file_name'])
373 filtered_drm_key_list = filter_module.Filter(drm_key_list)
374 else:
375 # filter module is optional
376 filtered_drm_key_list = drm_key_list
Mao Huang700663d2015-08-12 09:58:59 +0800377
378 # Fetch server key for signing.
379 server_key_fingerprint = self._FetchServerKeyFingerprint()
380
381 # Sign and encrypt each key by server's private key and requester's public
382 # key, respectively.
383 encrypted_serialized_drm_key_list = []
384 requester_key_fingerprint = project['requester_key_fingerprint']
385 for drm_key in filtered_drm_key_list:
386 encrypted_obj = self.gpg.encrypt(
387 json.dumps(drm_key), requester_key_fingerprint,
chuntsenf0780db2019-05-03 02:21:59 +0800388 always_trust=True, sign=server_key_fingerprint)
Mao Huang700663d2015-08-12 09:58:59 +0800389 encrypted_serialized_drm_key_list.append(encrypted_obj.data)
390
391 # Insert into the database.
392 with self.db_connection:
393 self.db_cursor.executemany(
Mao Huang9076f632015-09-24 17:38:47 +0800394 'INSERT INTO drm_keys ('
395 ' project_name, drm_key_hash, encrypted_drm_key) '
396 'VALUES (?, ?, ?)',
Yilin Yang7c865822019-11-01 14:50:26 +0800397 list(zip([project['name']] * len(encrypted_serialized_drm_key_list),
398 drm_key_hash_list, encrypted_serialized_drm_key_list)))
Mao Huang700663d2015-08-12 09:58:59 +0800399
400 def AvailableKeyCount(self, requester_signature):
401 """Queries the number of remaining keys.
402
403 Args:
404 requester_signature: a message signed by the requester. Since the server
405 doesn't need any additional info from the requester, the requester can
406 simply sign a random string and send it here.
407
408 Returns:
409 The number of remaining keys that can be requested.
410
411 Raises:
412 InvalidRequesterException if the signature of the requester can not be
413 verified.
414 """
415 verified = self.gpg.verify(requester_signature)
416 if not verified:
417 raise InvalidRequesterException(
418 'Invalid requester, check your signing key')
419
420 project = self._FetchProjectByRequesterKeyFingerprint(verified.fingerprint)
421
422 self.db_cursor.execute(
423 'SELECT COUNT(*) AS available_key_count FROM drm_keys '
424 'WHERE project_name = ? AND device_serial_number IS NULL',
425 (project['name'],))
426 return self.db_cursor.fetchone()['available_key_count']
427
428 def Request(self, encrypted_device_serial_number):
429 """Requests a DRM key by device serial number.
430
431 Args:
432 encrypted_device_serial_number: the device serial number signed by the
433 requester and encrypted by the server's public key.
434
435 Raises:
436 InvalidRequesterException if the signature of the requester can not be
437 verified. RuntimeError if no available keys left in the database.
438 """
439 decrypted_obj = self.gpg.decrypt(encrypted_device_serial_number)
440 project = self._FetchProjectByRequesterKeyFingerprint(
441 decrypted_obj.fingerprint)
442 device_serial_number = decrypted_obj.data
443
444 def FetchDRMKeyByDeviceSerialNumber(project_name, device_serial_number):
445 self.db_cursor.execute(
446 'SELECT * FROM drm_keys WHERE project_name = ? AND '
447 'device_serial_number = ?',
448 (project_name, device_serial_number))
449 return self.db_cursor.fetchone()
450
451 row = FetchDRMKeyByDeviceSerialNumber(project['name'], device_serial_number)
452 if row: # the SN has already paired
453 return row['encrypted_drm_key']
454
455 # Find an unpaired key.
456 with self.db_connection:
457 # SQLite3 does not support using LIMIT clause in UPDATE statement by
458 # default, unless SQLITE_ENABLE_UPDATE_DELETE_LIMIT flag is defined during
459 # compilation. Since this script may be deployed on partner's computer,
460 # we'd better assume they don't have this flag on.
461 self.db_cursor.execute(
462 'UPDATE drm_keys SET device_serial_number = ? '
463 'WHERE id = (SELECT id FROM drm_keys WHERE project_name = ? AND '
464 ' device_serial_number IS NULL LIMIT 1)',
465 (device_serial_number, project['name']))
466 if self.db_cursor.rowcount != 1: # insufficient keys
467 raise RuntimeError(
468 'Insufficient DRM keys, ask for the OEM to upload more')
469
470 row = FetchDRMKeyByDeviceSerialNumber(project['name'], device_serial_number)
471 if row:
472 return row['encrypted_drm_key']
473 else:
474 raise RuntimeError('Failed to find paired DRM key')
475
476 def ListenForever(self, ip, port):
477 """Starts the XML RPC server waiting for commands.
478
479 Args:
480 ip: IP to bind.
481 port: port to bind.
482 """
Yilin Yang2a2bb112019-10-23 11:20:33 +0800483 class Server(xmlrpc.server.SimpleXMLRPCServer):
Mao Huang901437f2016-06-24 11:39:15 +0800484 def _dispatch(self, method, params):
485 # Catch exceptions and log them. Without this, SimpleXMLRPCServer simply
486 # output the error message to stdout, and we won't be able to see what
487 # happened in the log file.
488 logging.info('%s called', method)
489 try:
Yilin Yang2a2bb112019-10-23 11:20:33 +0800490 result = xmlrpc.server.SimpleXMLRPCServer._dispatch(
Mao Huang901437f2016-06-24 11:39:15 +0800491 self, method, params)
Mao Huang901437f2016-06-24 11:39:15 +0800492 return result
493 except BaseException as e:
494 logging.exception(e)
495 raise
496
497 server = Server((ip, port), allow_none=True)
Mao Huang700663d2015-08-12 09:58:59 +0800498
499 server.register_introspection_functions()
500 server.register_function(self.AvailableKeyCount)
501 server.register_function(self.Upload)
502 server.register_function(self.Request)
503
504 server.serve_forever()
505
506 def _ImportGPGKey(self, key_file_path):
507 """Imports a GPG key from a file.
508
509 Args:
510 key_file_path: path to the GPG key file.
511
512 Returns:
513 A tuple (key_fingerprint, key_already_exists). The 1st element is the
514 imported key's fingerprint, and the 2nd element is True if the key was
515 already in the database before importing, False otherwise.
516 """
517 with open(key_file_path) as f:
518 import_results = self.gpg.import_keys(f.read())
chuntsenf0780db2019-05-03 02:21:59 +0800519 key_already_exists = (import_results.imported == 0)
Mao Huang700663d2015-08-12 09:58:59 +0800520 key_fingerprint = import_results.fingerprints[0]
521 return (key_fingerprint, key_already_exists)
522
523 def _LoadFilterModule(self, filter_module_file_name):
524 """Loads the filter module.
525
526 Args:
Mao Huang9076f632015-09-24 17:38:47 +0800527 filter_module_file_name: file name of the filter module in FILTERS_DIR.
Mao Huang700663d2015-08-12 09:58:59 +0800528
529 Returns:
530 The loaded filter module on success.
531
532 Raises:
533 Exception if failed, see imp.load_source()'s doc for what could be raised.
534 """
535 return imp.load_source(
536 'filter_module', os.path.join(FILTERS_DIR, filter_module_file_name))
537
Mao Huang9076f632015-09-24 17:38:47 +0800538 def _LoadParserModule(self, parser_module_file_name):
539 """Loads the parser module.
540
541 Args:
542 parser_module_file_name: file name of the parser module in PARSERS_DIR.
543
544 Returns:
545 The loaded parser module on success.
546
547 Raises:
548 Exception if failed, see imp.load_source()'s doc for what could be raised.
549 """
550 return imp.load_source(
551 'parser_module', os.path.join(PARSERS_DIR, parser_module_file_name))
552
Mao Huang700663d2015-08-12 09:58:59 +0800553 def _FetchServerKeyFingerprint(self):
554 """Returns the server GPG key's fingerprint."""
555 self.db_cursor.execute(
556 "SELECT * FROM settings WHERE key = 'server_key_fingerprint'")
557 row = self.db_cursor.fetchone()
558 if not row:
559 raise ValueError('Server key fingerprint not exists')
560 return row['value']
561
Peter Shih86430492018-02-26 14:51:58 +0800562 def _FetchOneProject(self, name=None,
563 uploader_key_fingerprint=None,
564 requester_key_fingerprint=None,
Mao Huang700663d2015-08-12 09:58:59 +0800565 exception_type=None, error_msg=None):
566 """Fetches the project by name, uploader key fingerprint, or requester key
567 fingerprint.
568
569 This function combines the name, uploader_key_fingerprint,
570 requester_key_fingerprint conditions (if not None) with the AND operator,
571 and tries to fetch one project from the database.
572
573 Args:
574 name: name of the project.
575 uploader_key_fingerprint: uploader key fingerprint of the project.
576 requester_key_fingerprint: requester key fingerprint of the project.
577 exception_type: if no project was found and exception_type is not None,
578 raise exception_type with error_msg.
579 error_msg: if no project was found and exception_type is not None, raise
580 exception_type with error_msg.
581
582 Returns:
583 A project that matches the name, uploader_key_fingerprint, and
584 requester_key_fingerprint conditiions.
585
586 Raises:
587 exception_type with error_msg if not project was found.
588 """
Peter Shih86430492018-02-26 14:51:58 +0800589 # pylint: disable=unused-argument
Mao Huang700663d2015-08-12 09:58:59 +0800590 where_clause_list = []
591 params = []
592 local_vars = locals()
593 for param_name in ['name', 'uploader_key_fingerprint',
594 'requester_key_fingerprint']:
595 if local_vars[param_name] is not None:
596 where_clause_list.append('%s = ?' % param_name)
597 params.append(locals()[param_name])
598 if not where_clause_list:
599 raise ValueError('No conditions given to fetch the project')
600 where_clause = 'WHERE ' + ' AND '.join(where_clause_list)
601
602 self.db_cursor.execute(
603 'SELECT * FROM projects %s' % where_clause, tuple(params))
604 project = self.db_cursor.fetchone()
605
606 if not project and exception_type:
607 raise exception_type(error_msg)
608
609 return project
610
611 def _FetchProjectByName(self, name):
612 return self._FetchOneProject(
613 name=name, exception_type=ProjectNotFoundException,
614 error_msg=('Project %s not found' % name))
615
616 def _FetchProjectByUploaderKeyFingerprint(self, uploader_key_fingerprint):
617 return self._FetchOneProject(
618 uploader_key_fingerprint=uploader_key_fingerprint,
619 exception_type=InvalidUploaderException,
620 error_msg='Invalid uploader, check your signing key')
621
622 def _FetchProjectByRequesterKeyFingerprint(self, requester_key_fingerprint):
623 return self._FetchOneProject(
624 requester_key_fingerprint=requester_key_fingerprint,
625 exception_type=InvalidRequesterException,
626 error_msg='Invalid requester, check your signing key')
627
628
629def _ParseArguments():
630 parser = argparse.ArgumentParser()
631 parser.add_argument(
Mao Huang901437f2016-06-24 11:39:15 +0800632 '-d', '--database_file_path',
633 default=os.path.join(SCRIPT_DIR, DEFAULT_DATABASE_FILE_NAME),
Mao Huang700663d2015-08-12 09:58:59 +0800634 help='path to the SQLite3 database file, default to "dkps.db" in the '
635 'same directory of this script')
636 parser.add_argument(
Mao Huang901437f2016-06-24 11:39:15 +0800637 '-g', '--gnupg_homedir',
638 default=os.path.join(SCRIPT_DIR, DEFAULT_GNUPG_DIR_NAME),
Mao Huang700663d2015-08-12 09:58:59 +0800639 help='path to the GnuGP home directory, default to "gnupg" in the same '
640 'directory of this script')
Mao Huang901437f2016-06-24 11:39:15 +0800641 parser.add_argument(
642 '-l', '--log_file_path',
643 default=os.path.join(SCRIPT_DIR, DEFAULT_LOG_FILE_NAME),
644 help='path to the log file, default to "dkps.log" in the same directory '
645 'of this script')
Mao Huang700663d2015-08-12 09:58:59 +0800646 subparsers = parser.add_subparsers(dest='command')
647
648 parser_add = subparsers.add_parser('add', help='adds a new project')
649 parser_add.add_argument('-n', '--name', required=True,
650 help='name of the new project')
651 parser_add.add_argument('-u', '--uploader_key_file_path', required=True,
652 help="path to the uploader's public key file")
653 parser_add.add_argument('-r', '--requester_key_file_path', required=True,
654 help="path to the requester's public key file")
Mao Huang9076f632015-09-24 17:38:47 +0800655 parser_add.add_argument('-p', '--parser_module_file_name', required=True,
656 help='file name of the parser module')
Mao Huang700663d2015-08-12 09:58:59 +0800657 parser_add.add_argument('-f', '--filter_module_file_name', default=None,
658 help='file name of the filter module')
659
660 subparsers.add_parser('destroy', help='destroys the database')
661
662 parser_update = subparsers.add_parser('update',
663 help='updates an existing project')
664 parser_update.add_argument('-n', '--name', required=True,
665 help='name of the project')
666 parser_update.add_argument('-u', '--uploader_key_file_path', default=None,
667 help="path to the uploader's public key file")
668 parser_update.add_argument('-r', '--requester_key_file_path', default=None,
669 help="path to the requester's public key file")
670 parser_update.add_argument('-f', '--filter_module_file_name', default=None,
671 help='file name of the filter module')
672
673 parser_init = subparsers.add_parser('init', help='initializes the database')
674 parser_init.add_argument(
675 '-g', '--gpg_gen_key_args', action='append', nargs=2, default={},
676 help='arguments to use when generating GPG key for server')
Mao Huang24be1382016-06-23 17:53:57 +0800677 parser_init.add_argument(
678 '-s', '--server_key_file_path', default=None,
679 help="path to the server's private key file")
Mao Huang700663d2015-08-12 09:58:59 +0800680
681 subparsers.add_parser('list', help='lists all projects')
682
683 parser_listen = subparsers.add_parser(
684 'listen', help='starts the server, waiting for upload or request keys')
685 parser_listen.add_argument(
Mao Huang901437f2016-06-24 11:39:15 +0800686 '--ip', default=DEFAULT_BIND_ADDR,
687 help='IP to bind, default to %s' % DEFAULT_BIND_ADDR)
Mao Huang700663d2015-08-12 09:58:59 +0800688 parser_listen.add_argument(
Mao Huang901437f2016-06-24 11:39:15 +0800689 '--port', type=int, default=DEFAULT_BIND_PORT,
690 help='port to listen, default to %s' % DEFAULT_BIND_PORT)
Mao Huang700663d2015-08-12 09:58:59 +0800691
692 parser_rm = subparsers.add_parser('rm', help='removes an existing project')
693 parser_rm.add_argument('-n', '--name', required=True,
694 help='name of the project to remove')
695
696 return parser.parse_args()
697
698
699def main():
700 args = _ParseArguments()
701
Mao Huang901437f2016-06-24 11:39:15 +0800702 logging_config = DEFAULT_LOGGING_CONFIG
703 logging_config['handlers']['file']['filename'] = args.log_file_path
704 logging.config.dictConfig(logging_config)
Mao Huang041483e2015-09-14 23:28:18 +0800705
Mao Huang700663d2015-08-12 09:58:59 +0800706 dkps = DRMKeysProvisioningServer(args.database_file_path, args.gnupg_homedir)
707 if args.command == 'init':
708 # Convert from command line arguments to a dict.
709 gpg_gen_key_args_dict = {}
710 for pair in args.gpg_gen_key_args:
711 gpg_gen_key_args_dict[pair[0]] = pair[1]
Mao Huang24be1382016-06-23 17:53:57 +0800712 dkps.Initialize(gpg_gen_key_args_dict, args.server_key_file_path)
Mao Huang700663d2015-08-12 09:58:59 +0800713 elif args.command == 'destroy':
714 message = (
715 'This action will remove all projects and keys information and is NOT '
716 'recoverable! Are you sure? (y/N)')
Yilin Yang8cc5dfb2019-10-22 15:58:53 +0800717 answer = input(textwrap.fill(message, 80) + ' ')
Mao Huang700663d2015-08-12 09:58:59 +0800718 if answer.lower() != 'y' and answer.lower() != 'yes':
Yilin Yang71e39412019-09-24 09:26:46 +0800719 print('OK, nothing will be removed.')
Mao Huang700663d2015-08-12 09:58:59 +0800720 else:
Yilin Yang71e39412019-09-24 09:26:46 +0800721 print('Removing all projects and keys information...', end=' ')
Mao Huang700663d2015-08-12 09:58:59 +0800722 dkps.Destroy()
Yilin Yang71e39412019-09-24 09:26:46 +0800723 print('done.')
Mao Huang700663d2015-08-12 09:58:59 +0800724 elif args.command == 'listen':
725 dkps.ListenForever(args.ip, args.port)
726 elif args.command == 'list':
Yilin Yang71e39412019-09-24 09:26:46 +0800727 print(dkps.ListProjects())
Mao Huang700663d2015-08-12 09:58:59 +0800728 elif args.command == 'add':
729 dkps.AddProject(
730 args.name, args.uploader_key_file_path, args.requester_key_file_path,
Mao Huangecbeb122016-06-22 20:30:38 +0800731 args.parser_module_file_name, args.filter_module_file_name)
Mao Huang700663d2015-08-12 09:58:59 +0800732 elif args.command == 'update':
733 dkps.UpdateProject(
734 args.name, args.uploader_key_file_path, args.requester_key_file_path,
735 args.filter_module_file_name)
736 elif args.command == 'rm':
737 dkps.RemoveProject(args.name)
738 else:
739 raise ValueError('Unknown command %s' % args.command)
740
741
742if __name__ == '__main__':
743 main()