All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Roskin <proski@gnu.org>
To: "Luis R. Rodriguez" <mcgrof@gmail.com>, linux-wireless@vger.kernel.org
Subject: [PATCH] crda: prevent key2pub.py from generating bogus output on failure
Date: Fri, 09 Jan 2009 19:16:50 -0500	[thread overview]
Message-ID: <20090110001650.3681.35686.stgit@dv.roinet.com> (raw)

Specify output file on the command line, so that it's not created in
case of fatal errors, such as asbence of M2Crypto.

Signed-off-by: Pavel Roskin <proski@gnu.org>
---
 Makefile         |    2 +-
 utils/key2pub.py |   57 ++++++++++++++++++++++++++++--------------------------
 2 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/Makefile b/Makefile
index c871182..292dad0 100644
--- a/Makefile
+++ b/Makefile
@@ -64,7 +64,7 @@ $(REG_BIN):
 
 keys-%.c: utils/key2pub.py $(wildcard $(PUBKEY_DIR)/*.pem)
 	$(NQ) '  GEN ' $@
-	$(Q)./utils/key2pub.py --$* $(wildcard $(PUBKEY_DIR)/*.pem) > $@
+	$(Q)./utils/key2pub.py --$* $(wildcard $(PUBKEY_DIR)/*.pem) $@
 
 %.o: %.c regdb.h
 	$(NQ) '  CC  ' $@
diff --git a/utils/key2pub.py b/utils/key2pub.py
index 40c60eb..47f50e3 100755
--- a/utils/key2pub.py
+++ b/utils/key2pub.py
@@ -3,7 +3,7 @@
 import sys
 from M2Crypto import RSA
 
-def print_ssl(name, val):
+def print_ssl(output, name, val):
     while val[0] == '\0':
         val = val[1:]
     while len(val) % 4:
@@ -13,22 +13,22 @@ def print_ssl(name, val):
         vnew.append((val[0], val[1], val[2], val[3], ))
         val = val[4:]
     vnew.reverse()
-    sys.stdout.write('static BN_ULONG %s[%d] = {\n' % (name, len(vnew)))
+    output.write('static BN_ULONG %s[%d] = {\n' % (name, len(vnew)))
     idx = 0
     for v1, v2, v3, v4 in vnew:
         if not idx:
-            sys.stdout.write('\t')
-        sys.stdout.write('0x%.2x%.2x%.2x%.2x, ' % (ord(v1), ord(v2), ord(v3), ord(v4)))
+            output.write('\t')
+        output.write('0x%.2x%.2x%.2x%.2x, ' % (ord(v1), ord(v2), ord(v3), ord(v4)))
         idx += 1
         if idx == 4:
             idx = 0
-            sys.stdout.write('\n')
+            output.write('\n')
     if idx:
-        sys.stdout.write('\n')
-    sys.stdout.write('};\n\n')
+        output.write('\n')
+    output.write('};\n\n')
 
-def print_ssl_keys(n):
-    sys.stdout.write(r'''
+def print_ssl_keys(output, n):
+    output.write(r'''
 struct pubkey {
 	struct bignum_st e, n;
 };
@@ -43,29 +43,29 @@ struct pubkey {
 static struct pubkey keys[] = {
 ''')
     for n in xrange(n + 1):
-        sys.stdout.write('	KEYS(e_%d, n_%d),\n' % (n, n))
-    sys.stdout.write('};\n')
+        output.write('	KEYS(e_%d, n_%d),\n' % (n, n))
+    output.write('};\n')
     pass
 
-def print_gcrypt(name, val):
+def print_gcrypt(output, name, val):
     while val[0] == '\0':
         val = val[1:]
-    sys.stdout.write('static const __u8 %s[%d] = {\n' % (name, len(val)))
+    output.write('static const __u8 %s[%d] = {\n' % (name, len(val)))
     idx = 0
     for v in val:
         if not idx:
-            sys.stdout.write('\t')
-        sys.stdout.write('0x%.2x, ' % ord(v))
+            output.write('\t')
+        output.write('0x%.2x, ' % ord(v))
         idx += 1
         if idx == 8:
             idx = 0
-            sys.stdout.write('\n')
+            output.write('\n')
     if idx:
-        sys.stdout.write('\n')
-    sys.stdout.write('};\n\n')
+        output.write('\n')
+    output.write('};\n\n')
 
-def print_gcrypt_keys(n):
-    sys.stdout.write(r'''
+def print_gcrypt_keys(output, n):
+    output.write(r'''
 struct key_params {
 	const __u8 *e, *n;
 	__u32 len_e, len_n; 
@@ -79,8 +79,8 @@ struct key_params {
 static const struct key_params keys[] = {
 ''')
     for n in xrange(n + 1):
-        sys.stdout.write('	KEYS(e_%d, n_%d),\n' % (n, n))
-    sys.stdout.write('};\n')
+        output.write('	KEYS(e_%d, n_%d),\n' % (n, n))
+    output.write('};\n')
     
 
 modes = {
@@ -90,14 +90,17 @@ modes = {
 
 try:
     mode = sys.argv[1]
-    files = sys.argv[2:]
+    files = sys.argv[2:-1]
+    outfile = sys.argv[-1]
 except IndexError:
     mode = None
 
 if not mode in modes:
-    print 'Usage: %s [%s] files' % (sys.argv[0], '|'.join(modes.keys()))
+    print 'Usage: %s [%s] input-file... output-file' % (sys.argv[0], '|'.join(modes.keys()))
     sys.exit(2)
 
+output = open(outfile, 'w')
+
 # load key
 idx = 0
 for f in files:
@@ -106,8 +109,8 @@ for f in files:
     except RSA.RSAError:
         key = RSA.load_key(f)
 
-    modes[mode][0]('e_%d' % idx, key.e[4:])
-    modes[mode][0]('n_%d' % idx, key.n[4:])
+    modes[mode][0](output, 'e_%d' % idx, key.e[4:])
+    modes[mode][0](output, 'n_%d' % idx, key.n[4:])
     idx += 1
 
-modes[mode][1](idx - 1)
+modes[mode][1](output, idx - 1)

             reply	other threads:[~2009-01-10  0:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-10  0:16 Pavel Roskin [this message]
2009-01-10  0:18 ` [PATCH] crda: prevent key2pub.py from generating bogus output on failure Luis R. Rodriguez
2009-01-10  0:19   ` Luis R. Rodriguez
2009-01-15 17:58 ` Luis R. Rodriguez

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=20090110001650.3681.35686.stgit@dv.roinet.com \
    --to=proski@gnu.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=mcgrof@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.