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.
 
 
 
 

98 line
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(
  22. usage="usage: %prog [options]",
  23. description="Reads a private key and outputs the "
  24. "corresponding public key. Both private and public keys use "
  25. "the format described in PKCS#1 v1.5",
  26. )
  27. parser.add_option(
  28. "-i",
  29. "--input",
  30. dest="infilename",
  31. type="string",
  32. help="Input filename. Reads from stdin if not specified",
  33. )
  34. parser.add_option(
  35. "-o",
  36. "--output",
  37. dest="outfilename",
  38. type="string",
  39. help="Output filename. Writes to stdout of not specified",
  40. )
  41. parser.add_option(
  42. "--inform",
  43. dest="inform",
  44. help="key format of input - default PEM",
  45. choices=("PEM", "DER"),
  46. default="PEM",
  47. )
  48. parser.add_option(
  49. "--outform",
  50. dest="outform",
  51. help="key format of output - default PEM",
  52. choices=("PEM", "DER"),
  53. default="PEM",
  54. )
  55. (cli, cli_args) = parser.parse_args(sys.argv)
  56. # Read the input data
  57. if cli.infilename:
  58. print(
  59. "Reading private key from %s in %s format" % (cli.infilename, cli.inform),
  60. file=sys.stderr,
  61. )
  62. with open(cli.infilename, "rb") as infile:
  63. in_data = infile.read()
  64. else:
  65. print("Reading private key from stdin in %s format" % cli.inform, file=sys.stderr)
  66. in_data = sys.stdin.read().encode("ascii")
  67. assert type(in_data) == bytes, type(in_data)
  68. # Take the public fields and create a public key
  69. priv_key = rsa.key.PrivateKey.load_pkcs1(in_data, cli.inform)
  70. pub_key = rsa.key.PublicKey(priv_key.n, priv_key.e)
  71. # Save to the output file
  72. out_data = pub_key.save_pkcs1(cli.outform)
  73. if cli.outfilename:
  74. print(
  75. "Writing public key to %s in %s format" % (cli.outfilename, cli.outform),
  76. file=sys.stderr,
  77. )
  78. with open(cli.outfilename, "wb") as outfile:
  79. outfile.write(out_data)
  80. else:
  81. print("Writing public key to stdout in %s format" % cli.outform, file=sys.stderr)
  82. sys.stdout.write(out_data.decode("ascii"))