Skip to content

Commit 648e865

Browse files
committed
Remove fingerprint and verify_fingerprint from Connection
The fingerprint will change from time to time and hard-coding it in this library we cannot forcibly deploy (unlike e.g. the Threema apps) is a surprising footgun since your services may suddenly fail (when Threema changes the fingerprint). As pointed out in #17, hard-coding the fingerprint (over the public key) is also undesirable. Furthermore, we want users to use their custom `aiohttp.ClientSession` instance. Therefore, we have decided to remove it. If you want to retain this feature, all you have to do is provide your own `aiohttp.ClientSession` in the following way: Connection(session=aiohttp.ClientSession( connector=aiohttp.TCPConnector(ssl=<fingerprint>))) See the aiohttp docs for details. Closes #17 Resolves #13 (by providing your own `SSLContext`)
1 parent ad3b718 commit 648e865

8 files changed

+5
-37
lines changed

examples/e2e.py

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ async def main():
136136
identity='*YOUR_GATEWAY_THREEMA_ID',
137137
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
138138
key='private:YOUR_PRIVATE_KEY',
139-
verify_fingerprint=True,
140139
)
141140
try:
142141
async with connection:

examples/e2e_blocking.py

-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def main():
134134
identity='*YOUR_GATEWAY_THREEMA_ID',
135135
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
136136
key='private:YOUR_PRIVATE_KEY',
137-
verify_fingerprint=True,
138137
blocking=True,
139138
)
140139
try:

examples/lookup.py

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ async def main():
1919
connection = Connection(
2020
identity='*YOUR_GATEWAY_THREEMA_ID',
2121
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
22-
verify_fingerprint=True,
2322
)
2423
try:
2524
async with connection:

examples/lookup_blocking.py

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def main():
1717
connection = Connection(
1818
identity='*YOUR_GATEWAY_THREEMA_ID',
1919
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
20-
verify_fingerprint=True,
2120
blocking=True,
2221
)
2322
try:

examples/simple.py

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ async def main():
5555
connection = Connection(
5656
identity='*YOUR_GATEWAY_THREEMA_ID',
5757
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
58-
verify_fingerprint=True,
5958
)
6059
try:
6160
async with connection:

examples/simple_blocking.py

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def main():
5353
connection = Connection(
5454
identity='*YOUR_GATEWAY_THREEMA_ID',
5555
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
56-
verify_fingerprint=True,
5756
blocking=True,
5857
)
5958
try:

threema/gateway/_gateway.py

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import binascii
21
import enum
32

43
import aiohttp
@@ -69,14 +68,9 @@ class Connection(AioRunMixin):
6968
end-to-end mode.
7069
- `key_file`: A file where the private key is stored
7170
in. Can be used instead of passing the key directly.
72-
- `fingerprint`: A binary fingerprint of an DER-encoded TLS
73-
certificate. Will fall back to a stored fingerprint which will
74-
be invalid as soon as the certificate expires.
75-
- `verify_fingerprint`: Set to `True` if you want to verify the
76-
TLS certificate of the Threema Gateway Server by a
77-
fingerprint. (Recommended)
7871
- `blocking`: Whether to use a blocking API, without the need
7972
for an explicit event loop.
73+
- `session`: An optional :class:`aiohttp.ClientSession`.
8074
"""
8175
async_functions = {
8276
'__exit__',
@@ -89,8 +83,6 @@ class Connection(AioRunMixin):
8983
'upload',
9084
'download',
9185
}
92-
fingerprint = binascii.unhexlify(
93-
b'42b1038e72f00c8c4dad78a3ebdc6d7a50c5ef288da9019b9171e4d675c08a17')
9486
urls = {
9587
'get_public_key': 'https://msgapi.threema.ch/pubkeys/{}',
9688
'get_id_by_phone': 'https://msgapi.threema.ch/lookup/phone/{}',
@@ -108,15 +100,10 @@ class Connection(AioRunMixin):
108100
def __init__(
109101
self, identity, secret,
110102
key=None, key_file=None,
111-
fingerprint=None, verify_fingerprint=False, blocking=False,
103+
blocking=False, session=None,
112104
):
113105
super().__init__(blocking=blocking)
114-
if fingerprint is None and verify_fingerprint:
115-
fingerprint = self.fingerprint
116-
if fingerprint is not None:
117-
fingerprint = aiohttp.Fingerprint(fingerprint)
118-
connector = aiohttp.TCPConnector(ssl=fingerprint)
119-
self._session = aiohttp.ClientSession(connector=connector)
106+
self._session = session if session is not None else aiohttp.ClientSession()
120107
self._key = None
121108
self._key_file = None
122109
self.id = identity

threema/gateway/bin/gateway_client.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
import re
77

8-
import aiohttp
98
import click
109
import logbook
1110
import logbook.more
@@ -60,11 +59,8 @@ async def get_public_key(self, _):
6059
@click.option('-v', '--verbosity', type=click.IntRange(0, len(_logging_levels)),
6160
default=0, help="Logging verbosity.")
6261
@click.option('-c', '--colored', is_flag=True, help='Colourise logging output.')
63-
@click.option('-vf', '--verify-fingerprint', is_flag=True,
64-
help='Verify the certificate fingerprint.')
65-
@click.option('--fingerprint', type=str, help='A hex-encoded fingerprint.')
6662
@click.pass_context
67-
def cli(ctx, verbosity, colored, verify_fingerprint, fingerprint):
63+
def cli(ctx, verbosity, colored):
6864
"""
6965
Command Line Interface. Use --help for details.
7066
"""
@@ -84,15 +80,8 @@ def cli(ctx, verbosity, colored, verify_fingerprint, fingerprint):
8480
global _logging_handler
8581
_logging_handler = handler
8682

87-
# Fingerprint
88-
if fingerprint is not None:
89-
fingerprint = binascii.unhexlify(fingerprint)
90-
9183
# Store on context
92-
ctx.obj = {
93-
'verify_fingerprint': verify_fingerprint,
94-
'fingerprint': fingerprint
95-
}
84+
ctx.obj = {}
9685

9786

9887
@cli.command(short_help='Show version information.', help="""
@@ -516,8 +505,6 @@ def main():
516505
exc = None
517506
try:
518507
cli()
519-
except aiohttp.client_exceptions.ServerFingerprintMismatch:
520-
error = 'Fingerprints did not match!'
521508
except Exception as exc_:
522509
error = str(exc_)
523510
exc = exc_

0 commit comments

Comments
 (0)