You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.9 KiB

  1. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Utility functions."""
  15. import sys
  16. from optparse import OptionParser
  17. import rsa.key
  18. def private_to_public() -> None:
  19. """Reads a private key and outputs the corresponding public key."""
  20. # Parse the CLI options
  21. parser = OptionParser(usage='usage: %prog [options]',
  22. description='Reads a private key and outputs the '
  23. 'corresponding public key. Both private and public keys use '
  24. 'the format described in PKCS#1 v1.5')
  25. parser.add_option('-i', '--input', dest='infilename', type='string',
  26. help='Input filename. Reads from stdin if not specified')
  27. parser.add_option('-o', '--output', dest='outfilename', type='string',
  28. help='Output filename. Writes to stdout of not specified')
  29. parser.add_option('--inform', dest='inform',
  30. help='key format of input - default PEM',
  31. choices=('PEM', 'DER'), default='PEM')
  32. parser.add_option('--outform', dest='outform',
  33. help='key format of output - default PEM',
  34. choices=('PEM', 'DER'), default='PEM')
  35. (cli, cli_args) = parser.parse_args(sys.argv)
  36. # Read the input data
  37. if cli.infilename:
  38. print('Reading private key from %s in %s format' %
  39. (cli.infilename, cli.inform), file=sys.stderr)
  40. with open(cli.infilename, 'rb') as infile:
  41. in_data = infile.read()
  42. else:
  43. print('Reading private key from stdin in %s format' % cli.inform,
  44. file=sys.stderr)
  45. in_data = sys.stdin.read().encode('ascii')
  46. assert type(in_data) == bytes, type(in_data)
  47. # Take the public fields and create a public key
  48. priv_key = rsa.key.PrivateKey.load_pkcs1(in_data, cli.inform)
  49. pub_key = rsa.key.PublicKey(priv_key.n, priv_key.e)
  50. # Save to the output file
  51. out_data = pub_key.save_pkcs1(cli.outform)
  52. if cli.outfilename:
  53. print('Writing public key to %s in %s format' %
  54. (cli.outfilename, cli.outform), file=sys.stderr)
  55. with open(cli.outfilename, 'wb') as outfile:
  56. outfile.write(out_data)
  57. else:
  58. print('Writing public key to stdout in %s format' % cli.outform,
  59. file=sys.stderr)
  60. sys.stdout.write(out_data.decode('ascii'))