3 # gpgsign.py - part of the FDroid server tools
4 # Copyright (C) 2015, Ciaran Gultnieks, ciaran@ciarang.com
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.
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.
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/>.
22 from argparse import ArgumentParser
33 Sign a JAR file with Java's jarsigner.
35 This method requires a properly initialized config object.
37 This does use old hashing algorithms, i.e. SHA1, but that's not
38 broken yet for file verification. This could be set to SHA256,
39 but then Android < 4.3 would not be able to verify it.
40 https://code.google.com/p/android/issues/detail?id=38321
42 args = [config['jarsigner'], '-keystore', config['keystore'],
43 '-storepass:file', config['keystorepassfile'],
44 '-digestalg', 'SHA1', '-sigalg', 'SHA1withRSA',
45 jar, config['repo_keyalias']]
46 if config['keystore'] == 'NONE':
47 args += config['smartcardoptions']
48 else: # smardcards never use -keypass
49 args += ['-keypass:file', config['keypassfile']]
50 p = common.FDroidPopen(args)
52 logging.critical("Failed to sign %s!" % jar)
56 def sign_index_v1(repodir, json_name):
58 Sign index-v1.json to make index-v1.jar
60 This is a bit different than index.jar: instead of their being index.xml
61 and index_unsigned.jar, the presence of index-v1.json means that there is
62 unsigned data. That file is then stuck into a jar and signed by the
63 signing process. index-v1.json is never published to the repo. It is
64 included in the binary transparency log, if that is enabled.
66 name, ext = common.get_extension(json_name)
67 index_file = os.path.join(repodir, json_name)
68 jar_file = os.path.join(repodir, name + '.jar')
69 with zipfile.ZipFile(jar_file, 'w', zipfile.ZIP_DEFLATED) as jar:
70 jar.write(index_file, json_name)
76 global config, options
78 # Parse command line...
79 parser = ArgumentParser(usage="%(prog)s [options]")
80 common.setup_global_opts(parser)
81 options = parser.parse_args()
83 config = common.read_config(options)
85 if 'jarsigner' not in config:
86 logging.critical('Java jarsigner not found! Install in standard location or set java_paths!')
90 if config['archive_older'] != 0:
91 repodirs.append('archive')
94 for output_dir in repodirs:
95 if not os.path.isdir(output_dir):
96 logging.error("Missing output directory '" + output_dir + "'")
99 unsigned = os.path.join(output_dir, 'index_unsigned.jar')
100 if os.path.exists(unsigned):
102 os.rename(unsigned, os.path.join(output_dir, 'index.jar'))
103 logging.info('Signed index in ' + output_dir)
106 json_name = 'index-v1.json'
107 index_file = os.path.join(output_dir, json_name)
108 if os.path.exists(index_file):
109 sign_index_v1(output_dir, json_name)
110 os.remove(index_file)
111 logging.info('Signed ' + index_file)
115 logging.info("Nothing to do")
118 if __name__ == "__main__":