Fix MPFR Flog implementation
[sxemacs] / src / openssl.c
index 93c014c..dc97da1 100644 (file)
@@ -26,21 +26,21 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
- * 
+ *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
- * 
+ *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
@@ -55,10 +55,10 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from 
+ * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -70,7 +70,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
- * 
+ *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
@@ -97,11 +97,11 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  *  - all of EVP interface functionality minus `engine' support
  *  - all of PEM interface functionality
  *  - a simple SSL client
- * 
+ *
  *  In addition, we are trying hard to provide not only an exact elisp
  *  copy of openssl, but also a _comprehensive_ one
  *
- * 
+ *
  * * src/openssl.c: functions overview:
  *
  * - General
@@ -163,7 +163,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  *  ossl-connect - constructor for SSL connection objects
  *  ossl-finish - destructor of SSL connection objects
  *  ossl-pending - predicate if data is available for read
- *  ossl-read - 
+ *  ossl-read -
  *  ossl-write -
  *  ossl-x509-get-subject
  *  ossl-x509-get-issuer
@@ -172,7 +172,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  *  ossl-sslcipher-name
  *  ossl-sslcipher-bits
  *
- * 
+ *
  * * Todo (internally):
  *  - implement the usage of engines
  *  - implement X.509 stuff
@@ -277,7 +277,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  *    (ossl-ec-pkey-p pkey)
  *    ;; generate an ec (elliptic curve) key
  *    ;; Note: this is probably disabled in your openssl
- *    (when (featurep 'openssl-ec) 
+ *    (when (featurep 'openssl-ec)
  *      (setq pkey (ossl-ec-generate-key))
  *      (ossl-ec-pkey-p pkey))
  *
@@ -285,7 +285,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  *  Note: For these functions you must have enabled DH in your OpenSSL lib
  *    ;; not yet
  *
- *  - HYBRID 
+ *  - HYBRID
  *    (setq key (ossl-rsa-generate-key 2048 3))
  *    (setq enc (ossl-seal 'AES-256-ECB "a tight secret" key))
  *    (ossl-open 'AES-256-ECB (car enc) key (cadr enc) (caddr enc))
@@ -428,7 +428,7 @@ extern Lisp_Object Qfile_writable_p;
 /*
  *
  * AUXILIARY
- * 
+ *
  */
 DEFUN("ossl-version", Fossl_version, 0, 0, 0, /*
 Return a descriptive version number of the OpenSSL in use.
@@ -494,7 +494,7 @@ This yields a plain list of symbols.
 
 
 #define ossl_digest_fun(var, fun)                                      \
-{                                                                      \
+do {                                                                   \
        int __kl;                                                       \
        const EVP_MD *__md;                                             \
                                                                        \
@@ -506,7 +506,7 @@ This yields a plain list of symbols.
        if (!__md) {                                                    \
                EVP_cleanup();                                          \
                return -1;                                              \
-       }                                                               \
+       }                                                               \
                                                                        \
        __kl = fun(__md);                                               \
                                                                        \
@@ -569,7 +569,7 @@ Return the block size of DIGEST in bytes.
 
 
 #define ossl_cipher_fun(var, fun)                                      \
-{                                                                      \
+do {                                                                   \
        int __kl;                                                       \
        const EVP_CIPHER *__ciph;                                       \
                                                                        \
@@ -581,7 +581,7 @@ Return the block size of DIGEST in bytes.
        if (!__ciph) {                                                  \
                EVP_cleanup();                                          \
                return -1;                                              \
-       }                                                               \
+       }                                                               \
                                                                        \
        __kl = fun(__ciph);                                             \
                                                                        \
@@ -704,9 +704,9 @@ Return the operation mode of CIPHER.
 
 \f
 /*
- * 
+ *
  * RAND
- * 
+ *
  */
 DEFUN("ossl-rand-bytes", Fossl_rand_bytes, 1, 1, 0, /*
 Return COUNT bytes of randomness.
@@ -876,9 +876,11 @@ binary string data.
        file = Fexpand_file_name(file, Qnil);
 
        if (((fp = fopen((char *)XSTRING_DATA(file),"rb")) == NULL) ||
-           (fseek(fp, 0, SEEK_SET)))
+           (fseek(fp, 0, SEEK_SET))) {
+               if (fp)
+                       fclose(fp);
                return wrong_type_argument(Qfile_readable_p, file);
-
+       }
 
        OpenSSL_add_all_digests();
        md = EVP_get_digestbyname(
@@ -886,12 +888,14 @@ binary string data.
 
        if (!md) {
                EVP_cleanup();
+               fclose(fp);
                error ("no such digest");
        }
 
        mdctx = xnew(EVP_MD_CTX);
        EVP_MD_CTX_init(mdctx);
        md_blocksize = (unsigned int)(EVP_MD_block_size(md) / 8);
+       SXE_SET_UNUSED(md_blocksize);
 
        EVP_DigestInit_ex(mdctx, md, NULL);
 
@@ -906,7 +910,7 @@ binary string data.
                        return Qnil;
                }
                EVP_DigestUpdate(mdctx, md_value, n);
-       } while (n > 0);
+       } while (n > 0);
 
        EVP_DigestFinal_ex(mdctx, md_value, &md_len);
        EVP_MD_CTX_cleanup(mdctx);
@@ -919,10 +923,10 @@ binary string data.
 }
 
 
-/* 
+/*
  *
  * HMAC (aka keyed hashes)
- * 
+ *
  */
 DEFUN("ossl-hmac", Fossl_hmac, 3, 3, 0, /*
 Return the message authentication code of MSG
@@ -1049,7 +1053,7 @@ binary string data.
                        return Qnil;
                }
                HMAC_Update(hmacctx, outbuf, n);
-       } while (n > 0);
+       } while (n > 0);
 
        HMAC_Final(hmacctx, outbuf, &outlen);
        HMAC_CTX_cleanup(hmacctx);
@@ -1062,10 +1066,10 @@ binary string data.
 }
 
 
-/* 
- * 
+/*
+ *
  * SYMMETRIC CIPHER
- * 
+ *
  */
 DEFUN("ossl-bytes-to-key", Fossl_bytes_to_key, 5, 5, 0, /*
 Derive a key and initialisation vector (iv) suitable for a cipher.
@@ -1087,7 +1091,7 @@ unique key and IV.
 COUNT \(a positive integer\) is the iteration count to use. This
 indicates how often the hash algorithm is called recursively.
 
-Note: You probably want to put a wrapping encoder function 
+Note: You probably want to put a wrapping encoder function
 \(like `base16-encode-string'\) around it, since this returns
 binary string data.
 */
@@ -1167,7 +1171,7 @@ you have compiled. See `ossl-available-ciphers'.
 
 STRING is the text to be encrypted.
 
-KEY should be a key generated suitably for this cipher, for example 
+KEY should be a key generated suitably for this cipher, for example
 by `ossl-bytes-to-key'.
 
 Optional fourth argument IV should be an initialisation vector
@@ -1360,7 +1364,7 @@ binary string data.
        }
 
        fseek(fp, 0, SEEK_END);
-       file_size = ftell(fp); 
+       file_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
 
 
@@ -1535,7 +1539,7 @@ non-nil, use this IV instead.
                           C_STRING_ALLOCA, string_ext, OSSL_CODING);
        string_len = OSSL_STRING_LENGTH(string);
 
-       if (!string_len) 
+       if (!string_len)
                error ("string must be of non-zero positive length.");
 
        OpenSSL_add_all_algorithms();
@@ -1674,7 +1678,7 @@ encrypted data redirected.
        }
 
        fseek(fp, 0, SEEK_END);
-       file_size = ftell(fp); 
+       file_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
 
 
@@ -1790,10 +1794,10 @@ encrypted data redirected.
 }
 
 
-/* 
- * 
+/*
+ *
  * ASYMMETRIC CIPHER
- * 
+ *
  */
 /* This is an opaque object for storing PKEYs in lisp */
 Lisp_Object Qevp_pkeyp;
@@ -1809,7 +1813,6 @@ mark_evp_pkey(Lisp_Object obj)
 static void
 print_evp_pkey(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
 {
-       char buf[256];
        EVP_PKEY *pkey;
        X509 *x509;
 
@@ -1821,7 +1824,7 @@ print_evp_pkey(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
        if (x509) {
                X509_NAME *iss = X509_get_issuer_name(x509);
                X509_NAME *sub = X509_get_subject_name(x509);
-               write_c_string(" X509 Certificate", printcharfun); 
+               write_c_string(" X509 Certificate", printcharfun);
                write_c_string(" iss:", printcharfun);
                write_c_string(X509_NAME_oneline(sub, NULL, 0), printcharfun);
                write_c_string(" sub:", printcharfun);
@@ -1833,26 +1836,25 @@ print_evp_pkey(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
                        write_c_string(";", printcharfun);
 
                if (rsa_pkey_p(pkey))
-                       write_c_string(" RSA", printcharfun); 
+                       write_c_string(" RSA", printcharfun);
                else if (dsa_pkey_p(pkey))
-                       write_c_string(" DSA", printcharfun); 
+                       write_c_string(" DSA", printcharfun);
                else if (ec_pkey_p(pkey))
-                       write_c_string(" EC", printcharfun); 
+                       write_c_string(" EC", printcharfun);
 
                if (ossl_pkey_has_private_data(pkey))
-                       write_c_string(" private/public key", printcharfun); 
+                       write_c_string(" private/public key", printcharfun);
                else if (ossl_pkey_has_public_data(pkey))
-                       write_c_string(" public key", printcharfun); 
+                       write_c_string(" public key", printcharfun);
       &nb