pki: Switch to binary mode on Windows when reading/writing DER to FDs
authorMartin Willi <martin@revosec.ch>
Sat, 8 Mar 2014 13:57:54 +0000 (14:57 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 4 Jun 2014 13:53:11 +0000 (15:53 +0200)
13 files changed:
src/pki/commands/acert.c
src/pki/commands/gen.c
src/pki/commands/issue.c
src/pki/commands/keyid.c
src/pki/commands/pkcs7.c
src/pki/commands/print.c
src/pki/commands/pub.c
src/pki/commands/req.c
src/pki/commands/self.c
src/pki/commands/signcrl.c
src/pki/commands/verify.c
src/pki/pki.c
src/pki/pki.h

index 4a11c47..185aa40 100644 (file)
@@ -196,6 +196,7 @@ static int acert()
        }
        else
        {
+               set_file_mode(stdin, CERT_ASN1_DER);
                if (!chunk_from_fd(0, &encoding))
                {
                        fprintf(stderr, "%s: ", strerror(errno));
@@ -232,6 +233,7 @@ static int acert()
                error = "encoding attribute certificate failed";
                goto end;
        }
+       set_file_mode(stdout, form);
        if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
        {
                error = "writing attribute certificate key failed";
index b74be7d..ce28a09 100644 (file)
@@ -133,6 +133,7 @@ static int gen()
                return 1;
        }
        key->destroy(key);
+       set_file_mode(stdout, form);
        if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
        {
                fprintf(stderr, "writing private key failed\n");
@@ -163,4 +164,3 @@ static void __attribute__ ((constructor))reg()
                }
        });
 }
-
index 339a880..aaa2c2f 100644 (file)
@@ -402,6 +402,7 @@ static int issue()
                {
                        chunk_t chunk;
 
+                       set_file_mode(stdin, CERT_ASN1_DER);
                        if (!chunk_from_fd(0, &chunk))
                        {
                                fprintf(stderr, "%s: ", strerror(errno));
@@ -500,6 +501,7 @@ static int issue()
                error = "encoding certificate failed";
                goto end;
        }
+       set_file_mode(stdout, form);
        if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
        {
                error = "writing certificate key failed";
index 64bb3cc..c3ac0c2 100644 (file)
@@ -91,6 +91,7 @@ static int keyid()
        {
                chunk_t chunk;
 
+               set_file_mode(stdin, CERT_ASN1_DER);
                if (!chunk_from_fd(0, &chunk))
                {
                        fprintf(stderr, "reading input failed: %s\n", strerror(errno));
index 6c75693..28bcd13 100644 (file)
@@ -58,6 +58,7 @@ static bool write_to_stream(FILE *stream, chunk_t data)
 {
        size_t len, total = 0;
 
+       set_file_mode(stream, CERT_ASN1_DER);
        while (total < data.len)
        {
                len = fwrite(data.ptr + total, 1, data.len - total, stream);
index 15ace03..fb07169 100644 (file)
@@ -604,6 +604,7 @@ static int print()
        {
                chunk_t chunk;
 
+               set_file_mode(stdin, CERT_ASN1_DER);
                if (!chunk_from_fd(0, &chunk))
                {
                        fprintf(stderr, "reading input failed: %s\n", strerror(errno));
index 260044c..b8d2f70 100644 (file)
@@ -110,6 +110,7 @@ static int pub()
        {
                chunk_t chunk;
 
+               set_file_mode(stdin, CERT_ASN1_DER);
                if (!chunk_from_fd(0, &chunk))
                {
                        fprintf(stderr, "reading input failed: %s\n", strerror(errno));
@@ -163,6 +164,7 @@ static int pub()
                return 1;
        }
        public->destroy(public);
+       set_file_mode(stdout, form);
        if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
        {
                fprintf(stderr, "writing public key failed\n");
index 1dce8cb..0236835 100644 (file)
@@ -118,6 +118,7 @@ static int req()
        {
                chunk_t chunk;
 
+               set_file_mode(stdin, CERT_ASN1_DER);
                if (!chunk_from_fd(0, &chunk))
                {
                        fprintf(stderr, "reading private key failed: %s\n", strerror(errno));
@@ -150,6 +151,7 @@ static int req()
                error = "encoding certificate request failed";
                goto end;
        }
+       set_file_mode(stdout, form);
        if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
        {
                error = "writing certificate request failed";
index 80f5053..daefcdc 100644 (file)
@@ -292,6 +292,7 @@ static int self()
        {
                chunk_t chunk;
 
+               set_file_mode(stdin, CERT_ASN1_DER);
                if (!chunk_from_fd(0, &chunk))
                {
                        fprintf(stderr, "%s: ", strerror(errno));
@@ -360,6 +361,7 @@ static int self()
                error = "encoding certificate failed";
                goto end;
        }
+       set_file_mode(stdout, form);
        if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
        {
                error = "writing certificate key failed";
index 3be020a..e5f49ef 100644 (file)
@@ -405,6 +405,7 @@ static int sign_crl()
                error = "encoding CRL failed";
                goto error;
        }
+       set_file_mode(stdout, form);
        if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
        {
                error = "writing CRL failed";
index f30dda9..6cfaaf2 100644 (file)
@@ -59,6 +59,7 @@ static int verify()
        {
                chunk_t chunk;
 
+               set_file_mode(stdin, CERT_ASN1_DER);
                if (!chunk_from_fd(0, &chunk))
                {
                        fprintf(stderr, "reading certificate failed: %s\n", strerror(errno));
index 55b300e..5d37bfb 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <time.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 #include <utils/debug.h>
 #include <credentials/sets/callback_cred.h>
@@ -154,6 +155,33 @@ bool calculate_lifetime(char *format, char *nbstr, char *nastr, time_t span,
 }
 
 /**
+ * Set output file mode appropriate for credential encoding form on Windows
+ */
+void set_file_mode(FILE *stream, cred_encoding_type_t enc)
+{
+#ifdef WIN32
+       int fd;
+
+       switch (enc)
+       {
+               case CERT_PEM:
+               case PRIVKEY_PEM:
+               case PUBKEY_PEM:
+                       /* keep default text mode */
+                       return;
+               default:
+                       /* switch to binary mode */
+                       break;
+       }
+       fd = fileno(stream);
+       if (fd != -1)
+       {
+               _setmode(fd, _O_BINARY);
+       }
+#endif
+}
+
+/**
  * Callback credential set pki uses
  */
 static callback_cred_t *cb_set;
index 616fac4..1f08277 100644 (file)
@@ -50,4 +50,9 @@ bool get_form(char *form, cred_encoding_type_t *enc, credential_type_t type);
 bool calculate_lifetime(char *format, char *nbstr, char *nastr, time_t span,
                                                time_t *nb, time_t *na);
 
+/**
+ * Set output file mode appropriate for credential encoding form on Windows
+ */
+void set_file_mode(FILE *stream, cred_encoding_type_t enc);
+
 #endif /** PKI_H_ @}*/