Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hard-coded fingerprint #55

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ async def main():
identity='*YOUR_GATEWAY_THREEMA_ID',
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
key='private:YOUR_PRIVATE_KEY',
verify_fingerprint=True,
)
try:
async with connection:
Expand Down
1 change: 0 additions & 1 deletion examples/e2e_blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def main():
identity='*YOUR_GATEWAY_THREEMA_ID',
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
key='private:YOUR_PRIVATE_KEY',
verify_fingerprint=True,
blocking=True,
)
try:
Expand Down
1 change: 0 additions & 1 deletion examples/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ async def main():
connection = Connection(
identity='*YOUR_GATEWAY_THREEMA_ID',
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
verify_fingerprint=True,
)
try:
async with connection:
Expand Down
1 change: 0 additions & 1 deletion examples/lookup_blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def main():
connection = Connection(
identity='*YOUR_GATEWAY_THREEMA_ID',
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
verify_fingerprint=True,
blocking=True,
)
try:
Expand Down
1 change: 0 additions & 1 deletion examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ async def main():
connection = Connection(
identity='*YOUR_GATEWAY_THREEMA_ID',
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
verify_fingerprint=True,
)
try:
async with connection:
Expand Down
1 change: 0 additions & 1 deletion examples/simple_blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def main():
connection = Connection(
identity='*YOUR_GATEWAY_THREEMA_ID',
secret='YOUR_GATEWAY_THREEMA_ID_SECRET',
verify_fingerprint=True,
blocking=True,
)
try:
Expand Down
19 changes: 3 additions & 16 deletions threema/gateway/_gateway.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import binascii
import enum

import aiohttp
Expand Down Expand Up @@ -69,14 +68,9 @@ class Connection(AioRunMixin):
end-to-end mode.
- `key_file`: A file where the private key is stored
in. Can be used instead of passing the key directly.
- `fingerprint`: A binary fingerprint of an DER-encoded TLS
certificate. Will fall back to a stored fingerprint which will
be invalid as soon as the certificate expires.
- `verify_fingerprint`: Set to `True` if you want to verify the
TLS certificate of the Threema Gateway Server by a
fingerprint. (Recommended)
- `blocking`: Whether to use a blocking API, without the need
for an explicit event loop.
- `session`: An optional :class:`aiohttp.ClientSession`.
"""
async_functions = {
'__exit__',
Expand All @@ -89,8 +83,6 @@ class Connection(AioRunMixin):
'upload',
'download',
}
fingerprint = binascii.unhexlify(
b'42b1038e72f00c8c4dad78a3ebdc6d7a50c5ef288da9019b9171e4d675c08a17')
urls = {
'get_public_key': 'https://msgapi.threema.ch/pubkeys/{}',
'get_id_by_phone': 'https://msgapi.threema.ch/lookup/phone/{}',
Expand All @@ -108,15 +100,10 @@ class Connection(AioRunMixin):
def __init__(
self, identity, secret,
key=None, key_file=None,
fingerprint=None, verify_fingerprint=False, blocking=False,
blocking=False, session=None,
):
super().__init__(blocking=blocking)
if fingerprint is None and verify_fingerprint:
fingerprint = self.fingerprint
if fingerprint is not None:
fingerprint = aiohttp.Fingerprint(fingerprint)
connector = aiohttp.TCPConnector(ssl=fingerprint)
self._session = aiohttp.ClientSession(connector=connector)
self._session = session if session is not None else aiohttp.ClientSession()
self._key = None
self._key_file = None
self.id = identity
Expand Down
17 changes: 2 additions & 15 deletions threema/gateway/bin/gateway_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
import re

import aiohttp
import click
import logbook
import logbook.more
Expand Down Expand Up @@ -60,11 +59,8 @@ async def get_public_key(self, _):
@click.option('-v', '--verbosity', type=click.IntRange(0, len(_logging_levels)),
default=0, help="Logging verbosity.")
@click.option('-c', '--colored', is_flag=True, help='Colourise logging output.')
@click.option('-vf', '--verify-fingerprint', is_flag=True,
help='Verify the certificate fingerprint.')
@click.option('--fingerprint', type=str, help='A hex-encoded fingerprint.')
@click.pass_context
def cli(ctx, verbosity, colored, verify_fingerprint, fingerprint):
def cli(ctx, verbosity, colored):
"""
Command Line Interface. Use --help for details.
"""
Expand All @@ -84,15 +80,8 @@ def cli(ctx, verbosity, colored, verify_fingerprint, fingerprint):
global _logging_handler
_logging_handler = handler

# Fingerprint
if fingerprint is not None:
fingerprint = binascii.unhexlify(fingerprint)

# Store on context
ctx.obj = {
'verify_fingerprint': verify_fingerprint,
'fingerprint': fingerprint
}
ctx.obj = {}


@cli.command(short_help='Show version information.', help="""
Expand Down Expand Up @@ -516,8 +505,6 @@ def main():
exc = None
try:
cli()
except aiohttp.client_exceptions.ServerFingerprintMismatch:
error = 'Fingerprints did not match!'
except Exception as exc_:
error = str(exc_)
exc = exc_
Expand Down