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