From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
"César Belley" <cesar.belley@lse.epita.fr>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Cleber Rosa" <crosa@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Eduardo Habkost" <ehabkost@redhat.com>
Subject: [PULL 14/18] scripts: Add u2f-setup-gen script
Date: Wed, 26 Aug 2020 16:52:35 +0200 [thread overview]
Message-ID: <20200826145239.6077-15-kraxel@redhat.com> (raw)
In-Reply-To: <20200826145239.6077-1-kraxel@redhat.com>
From: César Belley <cesar.belley@lse.epita.fr>
This patch adds the script used to generate setup directories, needed
for the device u2f-emulated configuration in directory mode:
python u2f-setup-gen.py $DIR
qemu -usb -device u2f-emulated,dir=$DIR
Signed-off-by: César Belley <cesar.belley@lse.epita.fr>
Message-id: 20200826114209.28821-11-cesar.belley@lse.epita.fr
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
scripts/u2f-setup-gen.py | 170 +++++++++++++++++++++++++++++++++++++++
1 file changed, 170 insertions(+)
create mode 100755 scripts/u2f-setup-gen.py
diff --git a/scripts/u2f-setup-gen.py b/scripts/u2f-setup-gen.py
new file mode 100755
index 000000000000..2122598fed8e
--- /dev/null
+++ b/scripts/u2f-setup-gen.py
@@ -0,0 +1,170 @@
+#!/usr/bin/env python3
+#
+# Libu2f-emu setup directory generator for USB U2F key emulation.
+#
+# Copyright (c) 2020 César Belley <cesar.belley@lse.epita.fr>
+# Written by César Belley <cesar.belley@lse.epita.fr>
+#
+# This work is licensed under the terms of the GNU GPL, version 2
+# or, at your option, any later version. See the COPYING file in
+# the top-level directory.
+
+import sys
+import os
+from random import randint
+from typing import Tuple
+
+from cryptography.hazmat.backends import default_backend
+from cryptography.hazmat.primitives.asymmetric import ec
+from cryptography.hazmat.primitives.serialization import Encoding, \
+ NoEncryption, PrivateFormat, PublicFormat
+from OpenSSL import crypto
+
+
+def write_setup_dir(dirpath: str, privkey_pem: bytes, cert_pem: bytes,
+ entropy: bytes, counter: int) -> None:
+ """
+ Write the setup directory.
+
+ Args:
+ dirpath: The directory path.
+ key_pem: The private key PEM.
+ cert_pem: The certificate PEM.
+ entropy: The 48 bytes of entropy.
+ counter: The counter value.
+ """
+ # Directory
+ os.mkdir(dirpath)
+
+ # Private key
+ with open(f'{dirpath}/private-key.pem', 'bw') as f:
+ f.write(privkey_pem)
+
+ # Certificate
+ with open(f'{dirpath}/certificate.pem', 'bw') as f:
+ f.write(cert_pem)
+
+ # Entropy
+ with open(f'{dirpath}/entropy', 'wb') as f:
+ f.write(entropy)
+
+ # Counter
+ with open(f'{dirpath}/counter', 'w') as f:
+ f.write(f'{str(counter)}\n')
+
+
+def generate_ec_key_pair() -> Tuple[str, str]:
+ """
+ Generate an ec key pair.
+
+ Returns:
+ The private and public key PEM.
+ """
+ # Key generation
+ privkey = ec.generate_private_key(ec.SECP256R1, default_backend())
+ pubkey = privkey.public_key()
+
+ # PEM serialization
+ privkey_pem = privkey.private_bytes(encoding=Encoding.PEM,
+ format=PrivateFormat.TraditionalOpenSSL,
+ encryption_algorithm=NoEncryption())
+ pubkey_pem = pubkey.public_bytes(encoding=Encoding.PEM,
+ format=PublicFormat.SubjectPublicKeyInfo)
+ return privkey_pem, pubkey_pem
+
+
+def generate_certificate(privkey_pem: str, pubkey_pem: str) -> str:
+ """
+ Generate a x509 certificate from a key pair.
+
+ Args:
+ privkey_pem: The private key PEM.
+ pubkey_pem: The public key PEM.
+
+ Returns:
+ The certificate PEM.
+ """
+ # Convert key pair
+ privkey = crypto.load_privatekey(crypto.FILETYPE_PEM, privkey_pem)
+ pubkey = crypto.load_publickey(crypto.FILETYPE_PEM, pubkey_pem)
+
+ # New x509v3 certificate
+ cert = crypto.X509()
+ cert.set_version(0x2)
+
+ # Serial number
+ cert.set_serial_number(randint(1, 2 ** 64))
+
+ # Before / After
+ cert.gmtime_adj_notBefore(0)
+ cert.gmtime_adj_notAfter(4 * (365 * 24 * 60 * 60))
+
+ # Public key
+ cert.set_pubkey(pubkey)
+
+ # Subject name and issueer
+ cert.get_subject().CN = "U2F emulated"
+ cert.set_issuer(cert.get_subject())
+
+ # Extensions
+ cert.add_extensions([
+ crypto.X509Extension(b"subjectKeyIdentifier",
+ False, b"hash", subject=cert),
+ ])
+ cert.add_extensions([
+ crypto.X509Extension(b"authorityKeyIdentifier",
+ False, b"keyid:always", issuer=cert),
+ ])
+ cert.add_extensions([
+ crypto.X509Extension(b"basicConstraints", True, b"CA:TRUE")
+ ])
+
+ # Signature
+ cert.sign(privkey, 'sha256')
+
+ return crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
+
+
+def generate_setup_dir(dirpath: str) -> None:
+ """
+ Generates the setup directory.
+
+ Args:
+ dirpath: The directory path.
+ """
+ # Key pair
+ privkey_pem, pubkey_pem = generate_ec_key_pair()
+
+ # Certificate
+ certificate_pem = generate_certificate(privkey_pem, pubkey_pem)
+
+ # Entropy
+ entropy = os.urandom(48)
+
+ # Counter
+ counter = 0
+
+ # Write
+ write_setup_dir(dirpath, privkey_pem, certificate_pem, entropy, counter)
+
+
+def main() -> None:
+ """
+ Main function
+ """
+ # Dir path
+ if len(sys.argv) != 2:
+ sys.stderr.write(f'Usage: {sys.argv[0]} <setup_dir>\n')
+ exit(2)
+ dirpath = sys.argv[1]
+
+ # Dir non existence
+ if os.path.exists(dirpath):
+ sys.stderr.write(f'Directory: {dirpath} already exists.\n')
+ exit(1)
+
+ generate_setup_dir(dirpath)
+
+
+if __name__ == '__main__':
+ main()
--
2.27.0
next prev parent reply other threads:[~2020-08-26 14:55 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-26 14:52 [PULL 00/18] Usb 20200826 patches Gerd Hoffmann
2020-08-26 14:52 ` [PULL 01/18] hw: xhci: check return value of 'usb_packet_map' Gerd Hoffmann
2020-08-26 14:52 ` [PULL 02/18] hw: ehci: destroy sglist in error path Gerd Hoffmann
2020-08-26 14:52 ` [PULL 03/18] hw: ehci: check return value of 'usb_packet_map' Gerd Hoffmann
2020-08-26 14:52 ` [PULL 04/18] ehci: drop pointless warn_report for guest bugs Gerd Hoffmann
2020-08-26 14:52 ` [PULL 05/18] hw/usb: Regroup USB HID protocol values Gerd Hoffmann
2020-08-26 14:52 ` [PULL 06/18] docs: Add USB U2F key device documentation Gerd Hoffmann
2020-08-26 14:52 ` [PULL 07/18] hw/usb: Add U2F key base class Gerd Hoffmann
2020-08-26 14:52 ` [PULL 08/18] hw/usb: Add U2F key base class implementation Gerd Hoffmann
2020-08-26 14:52 ` [PULL 09/18] hw/usb: Add U2F key passthru mode Gerd Hoffmann
2020-08-26 14:52 ` [PULL 10/18] hw/usb: Add U2F key emulated mode Gerd Hoffmann
2020-08-26 14:52 ` [PULL 11/18] meson: Add U2F key to meson Gerd Hoffmann
2020-08-26 14:52 ` [PULL 12/18] docs/system: Add U2F key to the USB devices examples Gerd Hoffmann
2020-08-26 14:52 ` [PULL 13/18] docs/qdev-device-use.txt: Add USB U2F key to the QDEV " Gerd Hoffmann
2020-08-26 14:52 ` Gerd Hoffmann [this message]
2020-08-26 14:52 ` [PULL 15/18] hw/usb: Add U2F device check to passthru mode Gerd Hoffmann
2020-08-26 14:52 ` [PULL 16/18] hw/usb: Add U2F device autoscan " Gerd Hoffmann
2020-08-26 14:52 ` [PULL 17/18] usb-host: workaround libusb bug Gerd Hoffmann
2020-08-26 14:52 ` [PULL 18/18] usb: fix setup_len init (CVE-2020-14364) Gerd Hoffmann
2020-08-26 16:05 ` [PULL 00/18] Usb 20200826 patches Peter Maydell
-- strict thread matches above, loose matches on Subject: below --
2020-08-28 8:08 [PULL 00/18] Usb 20200828 patches Gerd Hoffmann
2020-08-28 8:08 ` [PULL 14/18] scripts: Add u2f-setup-gen script Gerd Hoffmann
2020-08-31 8:32 [PULL 00/18] Usb 20200831 patches Gerd Hoffmann
2020-08-31 8:32 ` [PULL 14/18] scripts: Add u2f-setup-gen script Gerd Hoffmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200826145239.6077-15-kraxel@redhat.com \
--to=kraxel@redhat.com \
--cc=berrange@redhat.com \
--cc=cesar.belley@lse.epita.fr \
--cc=crosa@redhat.com \
--cc=ehabkost@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).