chiark / gitweb /
Merge branch 'master' into 'master'
[fdroidserver.git] / fdroidserver / signindex.py
1 #!/usr/bin/env python3
2 #
3 # gpgsign.py - part of the FDroid server tools
4 # Copyright (C) 2015, Ciaran Gultnieks, ciaran@ciarang.com
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Affero General Public License for more details.
15 #
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 import os
20 import zipfile
21 from argparse import ArgumentParser
22 import logging
23
24 from . import _
25 from . import common
26 from .exception import FDroidException
27
28 config = None
29 options = None
30
31
32 def sign_jar(jar):
33     """
34     Sign a JAR file with Java's jarsigner.
35
36     This method requires a properly initialized config object.
37
38     This does use old hashing algorithms, i.e. SHA1, but that's not
39     broken yet for file verification.  This could be set to SHA256,
40     but then Android < 4.3 would not be able to verify it.
41     https://code.google.com/p/android/issues/detail?id=38321
42     """
43     args = [config['jarsigner'], '-keystore', config['keystore'],
44             '-storepass:env', 'FDROID_KEY_STORE_PASS',
45             '-digestalg', 'SHA1', '-sigalg', 'SHA1withRSA',
46             jar, config['repo_keyalias']]
47     if config['keystore'] == 'NONE':
48         args += config['smartcardoptions']
49     else:  # smardcards never use -keypass
50         args += ['-keypass:env', 'FDROID_KEY_PASS']
51     env_vars = {
52         'FDROID_KEY_STORE_PASS': config['keystorepass'],
53         'FDROID_KEY_PASS': config['keypass'],
54     }
55     p = common.FDroidPopen(args, envs=env_vars)
56     if p.returncode != 0:
57         raise FDroidException("Failed to sign %s!" % jar)
58
59
60 def sign_index_v1(repodir, json_name):
61     """
62     Sign index-v1.json to make index-v1.jar
63
64     This is a bit different than index.jar: instead of their being index.xml
65     and index_unsigned.jar, the presence of index-v1.json means that there is
66     unsigned data.  That file is then stuck into a jar and signed by the
67     signing process.  index-v1.json is never published to the repo.  It is
68     included in the binary transparency log, if that is enabled.
69     """
70     name, ext = common.get_extension(json_name)
71     index_file = os.path.join(repodir, json_name)
72     jar_file = os.path.join(repodir, name + '.jar')
73     with zipfile.ZipFile(jar_file, 'w', zipfile.ZIP_DEFLATED) as jar:
74         jar.write(index_file, json_name)
75     sign_jar(jar_file)
76
77
78 def main():
79
80     global config, options
81
82     # Parse command line...
83     parser = ArgumentParser(usage="%(prog)s [options]")
84     common.setup_global_opts(parser)
85     options = parser.parse_args()
86
87     config = common.read_config(options)
88
89     if 'jarsigner' not in config:
90         raise FDroidException(
91             _('Java jarsigner not found! Install in standard location or set java_paths!'))
92
93     repodirs = ['repo']
94     if config['archive_older'] != 0:
95         repodirs.append('archive')
96
97     signed = 0
98     for output_dir in repodirs:
99         if not os.path.isdir(output_dir):
100             raise FDroidException("Missing output directory '" + output_dir + "'")
101
102         unsigned = os.path.join(output_dir, 'index_unsigned.jar')
103         if os.path.exists(unsigned):
104             sign_jar(unsigned)
105             os.rename(unsigned, os.path.join(output_dir, 'index.jar'))
106             logging.info('Signed index in ' + output_dir)
107             signed += 1
108
109         json_name = 'index-v1.json'
110         index_file = os.path.join(output_dir, json_name)
111         if os.path.exists(index_file):
112             sign_index_v1(output_dir, json_name)
113             os.remove(index_file)
114             logging.info('Signed ' + index_file)
115             signed += 1
116
117     if signed == 0:
118         logging.info(_("Nothing to do"))
119
120
121 if __name__ == "__main__":
122     main()