-
Notifications
You must be signed in to change notification settings - Fork 1
[WIP] Don't merge - these are for discussion #1
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
Draft
timhughes
wants to merge
11
commits into
tomtastic:main
Choose a base branch
from
timhughes:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9078669
move to a more standard directory structurei
timhughes 7faedfc
poetry run ruff format
timhughes ef4a2cd
poetry run ruff check --fix
timhughes a5e2e88
move cli out of main code
timhughes 7a7ce79
minor notes left and an experiment
timhughes 34bc3f4
created tests
timhughes d70e6d1
A bunch of minor fixes and TODO comments
timhughes 08844b5
readme update
timhughes 0574b51
extra todo comments
timhughes ff3d5c9
Ruff config and some random fixes
timhughes 04d8657
Here is an idea for a class based version
timhughes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| /scratch | ||
| poetry.lock | ||
| tests/* | ||
| __pycache__ | ||
| .mypy_cache | ||
| .ruff_cache | ||
| .venv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| repos: | ||
| - repo: https://github.com/pre-commit/pre-commit-hooks | ||
| rev: v5.0.0 | ||
| hooks: | ||
| - id: check-toml | ||
| - id: end-of-file-fixer | ||
| - id: trailing-whitespace | ||
| - repo: https://github.com/pre-commit/mirrors-mypy | ||
| rev: v1.13.0 # Use the sha / tag you want to point at | ||
| hooks: | ||
| - id: mypy | ||
| - repo: https://github.com/astral-sh/ruff-pre-commit | ||
| # Ruff version. | ||
| rev: v0.7.4 | ||
| hooks: | ||
| # Run the linter. | ||
| - id: ruff | ||
| name: ruff | ||
| description: "Run 'ruff' for extremely fast Python linting" | ||
| entry: ruff check --force-exclude | ||
| language: python | ||
| args: [--fix, --exit-non-zero-on-fix] | ||
| require_serial: true | ||
| # Run the formatter. | ||
| - id: ruff-format | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know about this, nice! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "cSpell.words": ["ATTRIBS", "caissuer", "ocsp", "tlsserial"], | ||
| "python.testing.pytestArgs": ["tests"], | ||
| "python.testing.unittestEnabled": false, | ||
| "python.testing.pytestEnabled": true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import logging | ||
| import socket | ||
| import sys | ||
|
|
||
| import certifi | ||
| from OpenSSL import SSL, crypto | ||
|
|
||
| hostname = "www.google.com" | ||
| port = 443 | ||
|
|
||
| methods = [ | ||
| (SSL.SSLv23_METHOD, "SSL.SSLv23_METHOD"), | ||
| (SSL.TLSv1_METHOD, "SSL.TLSv1_METHOD"), | ||
| (SSL.TLSv1_1_METHOD, "SSL.TLSv1_1_METHOD"), | ||
| (SSL.TLSv1_2_METHOD, "SSL.TLSv1_2_METHOD"), | ||
| ] | ||
|
|
||
| for method, method_name in methods: | ||
| try: | ||
| print(f"\n-- Method {method_name}") | ||
| context = SSL.Context(method=method) | ||
| context.load_verify_locations(cafile=certifi.where()) | ||
|
|
||
| conn = SSL.Connection( | ||
| context, socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| ) | ||
| conn.settimeout(5) | ||
| conn.connect((hostname, port)) | ||
| conn.setblocking(1) | ||
| conn.do_handshake() | ||
| conn.set_tlsext_host_name(hostname.encode()) | ||
| chain = conn.get_peer_cert_chain() | ||
|
|
||
| def decode(x: crypto.X509Name) -> str: | ||
| return "/".join( | ||
| ["=".join(z.decode("utf-8") for z in y) for y in x.get_components()] | ||
| ) | ||
|
|
||
| if chain: | ||
| for idx, cert in enumerate(chain): | ||
| print(f"{idx} subject: {decode(cert.get_subject())}") | ||
| print(f" issuer: {decode(cert.get_issuer())})") | ||
| print(f" serial: {cert.get_serial_number()}") | ||
| print(f' fingerprint: {cert.digest("sha1")}') | ||
|
|
||
| conn.close() | ||
| except SSL.Error: | ||
| logging.error(f"<><> Method {method_name} failed due to {sys.exc_info()[0]}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """ | ||
| This file allows you to run the program directly from the python module: | ||
|
|
||
| ~ $ python -m tlsserial --help | ||
|
|
||
| """ | ||
|
|
||
| import sys | ||
|
|
||
| from .cli import main | ||
|
|
||
| rc = 1 | ||
| try: | ||
| main() # pylint: disable=no-value-for-parameter # noqa | ||
| rc = 0 | ||
| except Exception as e: # pylint: disable=broad-exception-caught | ||
| print("Error:", e, file=sys.stderr) | ||
| sys.exit(rc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import logging | ||
| import os | ||
| from ssl import OPENSSL_VERSION | ||
|
|
||
| import click | ||
|
|
||
| from . import helper, tlsserial | ||
|
|
||
|
|
||
| # https://click.palletsprojects.com/en/8.1.x/quickstart/ | ||
| @click.command() | ||
| @click.option( | ||
| "--url", | ||
| cls=helper.MutuallyExclusiveOption, | ||
| mutually_exclusive=["file"], | ||
| help="host || host:port || https://host:port/other", | ||
| ) | ||
| @click.option( | ||
| "--file", | ||
| cls=helper.MutuallyExclusiveOption, | ||
| mutually_exclusive=["url"], | ||
| help="filename containing a PEM certificate", | ||
| ) | ||
| @click.option("--debug", is_flag=True, type=bool, default=False, help="Debug logging") | ||
| @click.option( | ||
| "--verbose", is_flag=True, type=bool, default=False, help="Verbose output" | ||
| ) | ||
| def main(url, file, debug, verbose) -> None: | ||
| """tlsserial groks X509 certificates for your pleasure""" | ||
| default_level = "DEBUG" if debug else "INFO" | ||
| logging.basicConfig( | ||
| level=getattr(logging, os.getenv("LOGLEVEL", default_level).upper()), | ||
| format="[%(levelname)s] %(asctime)s - %(message)s", | ||
| datefmt="%Y-%m-%dT%H:%M:%S", | ||
| ) | ||
| logging.debug("Logging is set to DEBUG level") | ||
| if url: | ||
| tlsserial.handle_url(url, verbose) | ||
| elif file: | ||
| tlsserial.handle_file(file, verbose) | ||
| else: | ||
| click.echo(f"Library version : {OPENSSL_VERSION}") | ||
| ctx = click.get_current_context() | ||
| click.echo(ctx.get_help()) |
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea why I failed to run black against these, but yeah, good stuff. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not know you could do this, sweet!