2 # -*- coding: utf-8 -*-
4 from __future__ import absolute_import, division, unicode_literals
11 del absolute_import, division, unicode_literals
13 # Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
15 # This program is free software; you can redistribute it and/or modify it under
16 # the terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
20 # This program is free software; you can redistribute it and/or modify it under
21 # the terms of the GNU Lesser General Public License as published by the Free
22 # Software Foundation; either version 2.1 of the License, or (at your option)
25 # This program is distributed in the hope that it will be useful, but WITHOUT
26 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
27 # FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU
28 # Lesser General Public License for more details.
30 # You should have received a copy of the GNU General Public License and the GNU
31 # Lesser General Public along with this program; if not, see
32 # <https://www.gnu.org/licenses/>.
35 This script searches the ProtonMail key server for the specified key and
38 Usage: pmkey-import-hkp.py [search strings]
41 c = gpg.Context(armor=True)
42 server = hkp4py.KeyServer("hkps://api.protonmail.ch")
51 keyterms = sys.argv[1:]
52 elif len(sys.argv) == 2:
54 keyterms.append(keyterm)
56 key_term = input("Enter the key ID, UID or search string: ")
57 keyterms = key_term.split()
59 for keyterm in keyterms:
60 if keyterm.count("@") == 2 and keyterm.startswith("@") is True:
61 ksearch.append(keyterm[1:])
62 ksearch.append(keyterm[1:])
63 ksearch.append(keyterm[1:])
64 elif keyterm.count("@") == 1 and keyterm.startswith("@") is True:
65 ksearch.append("{0}@protonmail.com".format(keyterm[1:]))
66 ksearch.append("{0}@protonmail.ch".format(keyterm[1:]))
67 ksearch.append("{0}@pm.me".format(keyterm[1:]))
68 elif keyterm.count("@") == 0:
69 ksearch.append("{0}@protonmail.com".format(keyterm))
70 ksearch.append("{0}@protonmail.ch".format(keyterm))
71 ksearch.append("{0}@pm.me".format(keyterm))
72 elif keyterm.count("@") == 2 and keyterm.startswith("@") is False:
73 uidlist = keyterm.split("@")
75 ksearch.append("{0}@protonmail.com".format(uid))
76 ksearch.append("{0}@protonmail.ch".format(uid))
77 ksearch.append("{0}@pm.me".format(uid))
78 elif keyterm.count("@") > 2:
79 uidlist = keyterm.split("@")
81 ksearch.append("{0}@protonmail.com".format(uid))
82 ksearch.append("{0}@protonmail.ch".format(uid))
83 ksearch.append("{0}@pm.me".format(uid))
85 ksearch.append(keyterm)
88 print("Checking for key for: {0}".format(k))
90 keys = server.search(k)
91 if isinstance(keys, list) is True:
95 import_result = c.key_import(key.key_blob)
96 except Exception as e:
97 import_result = c.key_import(key.key)
100 results.append(import_result)
102 for result in results:
103 if result is not None and hasattr(result, "considered") is False:
104 print("{0} for {1}".format(result.decode(), k))
105 elif result is not None and hasattr(result, "considered") is True:
106 num_keys = len(result.imports)
107 new_revs = result.new_revocations
108 new_sigs = result.new_signatures
109 new_subs = result.new_sub_keys
110 new_uids = result.new_user_ids
111 new_scrt = result.secret_imported
112 nochange = result.unchanged
114 The total number of keys considered for import was: {0}
116 With UIDs wholely or partially matching the following string:
120 Number of keys revoked: {2}
121 Number of new signatures: {3}
122 Number of new subkeys: {4}
123 Number of new user IDs: {5}
124 Number of new secret keys: {6}
125 Number of unchanged keys: {7}
127 The key IDs for all considered keys were:
128 """.format(num_keys, k, new_revs, new_sigs, new_subs, new_uids, new_scrt,
130 for i in range(num_keys):
131 print(result.imports[i].fpr)