Add openpgpg2pem.
[monkeysphere.git] / src / share / keytrans
index 60eab55aec0ed67e05273e8a99c2097e08be33c4..f482abcffbd4c7f26831f4c03a71620cdbb5e837 100755 (executable)
@@ -6,7 +6,8 @@
 # PKCS#1 DER, and OpenSSH-style public key lines.
 
 # How it behaves depends on the name under which it is invoked.  The
-# two implementations currently are: pem2openpgp and openpgp2ssh.
+# implementations currently are: pem2openpgp openpgpg2pem, and
+# openpgp2ssh.
 
 
 
 
 
 
+# openpgp2pem: take a stream of OpenPGP packets containing public or
+# secret key material on standard input, and a Key ID (or fingerprint)
+# as the first argument.  Find the matching key in the input stream,
+# and emit it on stdout in OpenSSL-PEM format.
+
+# Example usage:
+
+# gpg --export-secret-keys --export-options export-reset-subkey-passwd $KEYID | \
+#  openpgp2pem $KEYID
+
+#For private keys, this will produce the same PKCS#1 RSAPrivateKey
+#(PEM header: BEGIN RSA PRIVATE KEY) results as:
+
+#openssl rsa -in private.pem
+
+#For public keys, this will produce the same X.509
+#SubjectPublicKeyInfo (PEM header: BEGIN PUBLIC KEY) results as:
+
+#openssl rsa -in private.pem -pubout
+
+
+
+
+
 # openpgp2ssh: take a stream of OpenPGP packets containing public or
 # secret key material on standard input, and a Key ID (or fingerprint)
 # as the first argument.  Find the matching key in the input stream,
@@ -1103,6 +1128,22 @@ for (basename($0)) {
                      }
                     );
   }
+  elsif (/^openpgp2pem$/) {
+      my $fpr = shift;
+      my $instream;
+      open($instream,'-');
+      binmode($instream, ":bytes");
+      my $key = openpgp2rsa($instream, $fpr);
+      if (defined($key)) {
+       if ($key->is_private()) {
+         print $key->get_private_key_string();
+       } else {
+         print $key->get_public_key_x509_string();
+       }
+      } else {
+       die "No matching key found.\n";
+      }
+  }
   elsif (/^openpgp2ssh$/) {
       my $fpr = shift;
       my $instream;