8576e20
From 43b730ce56f010e9d33573fcb020df49798c1ed8 Mon Sep 17 00:00:00 2001
8576e20
From: Kamil Dudka <kdudka@redhat.com>
8576e20
Date: Mon, 10 Sep 2012 15:32:40 +0200
8576e20
Subject: [PATCH 1/4] kex: do not ignore failure of libssh2_md5_init()
8576e20
8576e20
The MD5 algorithm is disabled when running in FIPS mode.
8576e20
---
8576e20
 src/hostkey.c      |    4 +++-
8576e20
 src/kex.c          |   13 +++++++++----
8576e20
 src/libgcrypt.h    |    6 +++++-
8576e20
 src/libssh2_priv.h |    1 +
8576e20
 src/openssl.h      |    3 +++
8576e20
 5 files changed, 21 insertions(+), 6 deletions(-)
8576e20
8576e20
diff --git a/src/hostkey.c b/src/hostkey.c
8576e20
index 53f7479..753563d 100644
8576e20
--- a/src/hostkey.c
8576e20
+++ b/src/hostkey.c
8576e20
@@ -429,7 +429,9 @@ libssh2_hostkey_hash(LIBSSH2_SESSION * session, int hash_type)
8576e20
     switch (hash_type) {
8576e20
 #if LIBSSH2_MD5
8576e20
     case LIBSSH2_HOSTKEY_HASH_MD5:
8576e20
-        return (char *) session->server_hostkey_md5;
8576e20
+        return (session->server_hostkey_md5_valid)
8576e20
+          ? (char *) session->server_hostkey_md5
8576e20
+          : NULL;
8576e20
         break;
8576e20
 #endif /* LIBSSH2_MD5 */
8576e20
     case LIBSSH2_HOSTKEY_HASH_SHA1:
8576e20
diff --git a/src/kex.c b/src/kex.c
8576e20
index 0a72cb7..07e717f 100644
8576e20
--- a/src/kex.c
8576e20
+++ b/src/kex.c
8576e20
@@ -218,10 +218,15 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
8576e20
         {
8576e20
             libssh2_md5_ctx fingerprint_ctx;
8576e20
 
8576e20
-            libssh2_md5_init(&fingerprint_ctx);
8576e20
-            libssh2_md5_update(fingerprint_ctx, session->server_hostkey,
8576e20
-                               session->server_hostkey_len);
8576e20
-            libssh2_md5_final(fingerprint_ctx, session->server_hostkey_md5);
8576e20
+            if (libssh2_md5_init(&fingerprint_ctx)) {
8576e20
+                libssh2_md5_update(fingerprint_ctx, session->server_hostkey,
8576e20
+                                   session->server_hostkey_len);
8576e20
+                libssh2_md5_final(fingerprint_ctx, session->server_hostkey_md5);
8576e20
+                session->server_hostkey_md5_valid = TRUE;
8576e20
+            }
8576e20
+            else {
8576e20
+                session->server_hostkey_md5_valid = FALSE;
8576e20
+            }
8576e20
         }
8576e20
 #ifdef LIBSSH2DEBUG
8576e20
         {
8576e20
diff --git a/src/libgcrypt.h b/src/libgcrypt.h
8576e20
index 04516e5..1f0276e 100644
8576e20
--- a/src/libgcrypt.h
8576e20
+++ b/src/libgcrypt.h
8576e20
@@ -68,7 +68,11 @@
8576e20
   gcry_md_hash_buffer (GCRY_MD_SHA1, out, message, len)
8576e20
 
8576e20
 #define libssh2_md5_ctx gcry_md_hd_t
8576e20
-#define libssh2_md5_init(ctx) gcry_md_open (ctx,  GCRY_MD_MD5, 0);
8576e20
+
8576e20
+/* returns 0 in case of failure */
8576e20
+#define libssh2_md5_init(ctx) \
8576e20
+  (GPG_ERR_NO_ERROR == gcry_md_open (ctx,  GCRY_MD_MD5, 0))
8576e20
+
8576e20
 #define libssh2_md5_update(ctx, data, len) gcry_md_write (ctx, data, len)
8576e20
 #define libssh2_md5_final(ctx, out) \
8576e20
   memcpy (out, gcry_md_read (ctx, 0), MD5_DIGEST_LENGTH), gcry_md_close (ctx)
8576e20
diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h
8576e20
index 23fbc65..196864d 100644
8576e20
--- a/src/libssh2_priv.h
8576e20
+++ b/src/libssh2_priv.h
8576e20
@@ -597,6 +597,7 @@ struct _LIBSSH2_SESSION
8576e20
     uint32_t server_hostkey_len;
8576e20
 #if LIBSSH2_MD5
8576e20
     unsigned char server_hostkey_md5[MD5_DIGEST_LENGTH];
8576e20
+    int server_hostkey_md5_valid;
8576e20
 #endif                          /* ! LIBSSH2_MD5 */
8576e20
     unsigned char server_hostkey_sha1[SHA_DIGEST_LENGTH];
8576e20
 
8576e20
diff --git a/src/openssl.h b/src/openssl.h
8576e20
index 6d2aeed..4835ab6 100644
8576e20
--- a/src/openssl.h
8576e20
+++ b/src/openssl.h
8576e20
@@ -113,7 +113,10 @@
8576e20
 void libssh2_sha1(const unsigned char *message, unsigned long len, unsigned char *out);
8576e20
 
8576e20
 #define libssh2_md5_ctx EVP_MD_CTX
8576e20
+
8576e20
+/* returns 0 in case of failure */
8576e20
 #define libssh2_md5_init(ctx) EVP_DigestInit(ctx, EVP_get_digestbyname("md5"))
8576e20
+
8576e20
 #define libssh2_md5_update(ctx, data, len) EVP_DigestUpdate(&(ctx), data, len)
8576e20
 #define libssh2_md5_final(ctx, out) EVP_DigestFinal(&(ctx), out, NULL)
8576e20
 void libssh2_md5(const unsigned char *message, unsigned long len, unsigned char *out);
8576e20
-- 
8576e20
1.7.1
8576e20
8576e20
8576e20
From bfbb5a4dc75c04b3532063c03b80796dd6d69da4 Mon Sep 17 00:00:00 2001
8576e20
From: Kamil Dudka <kdudka@redhat.com>
8576e20
Date: Mon, 10 Sep 2012 15:59:16 +0200
8576e20
Subject: [PATCH 2/4] openssl: do not ignore failure of EVP_CipherInit()
8576e20
8576e20
---
8576e20
 src/openssl.c |    3 +--
8576e20
 1 files changed, 1 insertions(+), 2 deletions(-)
8576e20
8576e20
diff --git a/src/openssl.c b/src/openssl.c
8576e20
index 481982c..c61cb0e 100644
8576e20
--- a/src/openssl.c
8576e20
+++ b/src/openssl.c
8576e20
@@ -175,8 +175,7 @@ _libssh2_cipher_init(_libssh2_cipher_ctx * h,
8576e20
                      unsigned char *iv, unsigned char *secret, int encrypt)
8576e20
 {
8576e20
     EVP_CIPHER_CTX_init(h);
8576e20
-    EVP_CipherInit(h, algo(), secret, iv, encrypt);
8576e20
-    return 0;
8576e20
+    return !EVP_CipherInit(h, algo(), secret, iv, encrypt);
8576e20
 }
8576e20
 
8576e20
 int
8576e20
-- 
8576e20
1.7.1
8576e20
8576e20
8576e20
From 5d567faecce45a891de2a3e10fad7b221a4f6540 Mon Sep 17 00:00:00 2001
8576e20
From: Kamil Dudka <kdudka@redhat.com>
8576e20
Date: Tue, 11 Sep 2012 09:33:34 +0200
8576e20
Subject: [PATCH 3/4] crypt: use hard-wired cipher block sizes consistently
8576e20
8576e20
---
8576e20
 src/crypt.c        |    8 +++++---
8576e20
 src/crypto.h       |    2 +-
8576e20
 src/libgcrypt.c    |    8 +-------
8576e20
 src/libssh2_priv.h |    2 +-
8576e20
 src/openssl.c      |    7 +------
8576e20
 src/transport.c    |    3 ++-
8576e20
 6 files changed, 11 insertions(+), 19 deletions(-)
8576e20
8576e20
diff --git a/src/crypt.c b/src/crypt.c
8576e20
index 93d99c4..931ae8b 100644
8576e20
--- a/src/crypt.c
8576e20
+++ b/src/crypt.c
8576e20
@@ -96,11 +96,12 @@ crypt_init(LIBSSH2_SESSION * session,
8576e20
 
8576e20
 static int
8576e20
 crypt_encrypt(LIBSSH2_SESSION * session, unsigned char *block,
8576e20
-                 void **abstract)
8576e20
+              size_t blocksize, void **abstract)
8576e20
 {
8576e20
     struct crypt_ctx *cctx = *(struct crypt_ctx **) abstract;
8576e20
     (void) session;
8576e20
-    return _libssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, block);
8576e20
+    return _libssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, block,
8576e20
+                                 blocksize);
8576e20
 }
8576e20
 
8576e20
 static int
8576e20
@@ -248,7 +249,8 @@ crypt_init_arcfour128(LIBSSH2_SESSION * session,
8576e20
         unsigned char block[8];
8576e20
         size_t discard = 1536;
8576e20
         for (; discard; discard -= 8)
8576e20
-            _libssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, block);
8576e20
+            _libssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, block,
8576e20
+                                  method->blocksize);
8576e20
     }
8576e20
 
8576e20
     return rc;
8576e20
diff --git a/src/crypto.h b/src/crypto.h
8576e20
index 8cf34f5..5dc5931 100644
8576e20
--- a/src/crypto.h
8576e20
+++ b/src/crypto.h
8576e20
@@ -103,7 +103,7 @@ int _libssh2_cipher_init(_libssh2_cipher_ctx * h,
8576e20
 
8576e20
 int _libssh2_cipher_crypt(_libssh2_cipher_ctx * ctx,
8576e20
                           _libssh2_cipher_type(algo),
8576e20
-                          int encrypt, unsigned char *block);
8576e20
+                          int encrypt, unsigned char *block, size_t blocksize);
8576e20
 
8576e20
 int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
8576e20
                               unsigned char **method,
8576e20
diff --git a/src/libgcrypt.c b/src/libgcrypt.c
8576e20
index 5c2787b..29770c7 100644
8576e20
--- a/src/libgcrypt.c
8576e20
+++ b/src/libgcrypt.c
8576e20
@@ -553,17 +553,11 @@ _libssh2_cipher_init(_libssh2_cipher_ctx * h,
8576e20
 int
8576e20
 _libssh2_cipher_crypt(_libssh2_cipher_ctx * ctx,
8576e20
                       _libssh2_cipher_type(algo),
8576e20
-                      int encrypt, unsigned char *block)
8576e20
+                      int encrypt, unsigned char *block, size_t blklen)
8576e20
 {
8576e20
     int cipher = _libssh2_gcry_cipher (algo);
8576e20
-    size_t blklen = gcry_cipher_get_algo_blklen(cipher);
8576e20
     int ret;
8576e20
 
8576e20
-    if (blklen == 1) {
8576e20
-/* Hack for arcfour. */
8576e20
-        blklen = 8;
8576e20
-    }
8576e20
-
8576e20
     if (encrypt) {
8576e20
         ret = gcry_cipher_encrypt(*ctx, block, blklen, block, blklen);
8576e20
     } else {
8576e20
diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h
8576e20
index 196864d..4ec9f73 100644
8576e20
--- a/src/libssh2_priv.h
8576e20
+++ b/src/libssh2_priv.h
8576e20
@@ -883,7 +883,7 @@ struct _LIBSSH2_CRYPT_METHOD
8576e20
                  int *free_iv, unsigned char *secret, int *free_secret,
8576e20
                  int encrypt, void **abstract);
8576e20
     int (*crypt) (LIBSSH2_SESSION * session, unsigned char *block,
8576e20
-                  void **abstract);
8576e20
+                  size_t blocksize, void **abstract);
8576e20
     int (*dtor) (LIBSSH2_SESSION * session, void **abstract);
8576e20
 
8576e20
       _libssh2_cipher_type(algo);
8576e20
diff --git a/src/openssl.c b/src/openssl.c
8576e20
index c61cb0e..8643591 100644
8576e20
--- a/src/openssl.c
8576e20
+++ b/src/openssl.c
8576e20
@@ -181,18 +181,13 @@ _libssh2_cipher_init(_libssh2_cipher_ctx * h,
8576e20
 int
8576e20
 _libssh2_cipher_crypt(_libssh2_cipher_ctx * ctx,
8576e20
                       _libssh2_cipher_type(algo),
8576e20
-                      int encrypt, unsigned char *block)
8576e20
+                      int encrypt, unsigned char *block, size_t blocksize)
8576e20
 {
8576e20
-    int blocksize = ctx->cipher->block_size;
8576e20
     unsigned char buf[EVP_MAX_BLOCK_LENGTH];
8576e20
     int ret;
8576e20
     (void) algo;
8576e20
     (void) encrypt;
8576e20
 
8576e20
-    if (blocksize == 1) {
8576e20
-/* Hack for arcfour. */
8576e20
-        blocksize = 8;
8576e20
-    }
8576e20
     ret = EVP_Cipher(ctx, buf, block, blocksize);
8576e20
     if (ret == 1) {
8576e20
         memcpy(block, buf, blocksize);
8576e20
diff --git a/src/transport.c b/src/transport.c
8576e20
index 15425b9..b4ec037 100644
8576e20
--- a/src/transport.c
8576e20
+++ b/src/transport.c
8576e20
@@ -139,7 +139,7 @@ decrypt(LIBSSH2_SESSION * session, unsigned char *source,
8576e20
     assert((len % blocksize) == 0);
8576e20
 
8576e20
     while (len >= blocksize) {
8576e20
-        if (session->remote.crypt->crypt(session, source,
8576e20
+        if (session->remote.crypt->crypt(session, source, blocksize,
8576e20
                                          &session->remote.crypt_abstract)) {
8576e20
             LIBSSH2_FREE(session, p->payload);
8576e20
             return LIBSSH2_ERROR_DECRYPT;
15d584a
@@ -834,6 +834,7 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session,
8576e20
         for(i = 0; i < packet_length; i += session->local.crypt->blocksize) {
8576e20
             unsigned char *ptr = &p->outbuf[i];
8576e20
             if (session->local.crypt->crypt(session, ptr,
8576e20
+                                            session->local.crypt->blocksize,
8576e20
                                             &session->local.crypt_abstract))
8576e20
                 return LIBSSH2_ERROR_ENCRYPT;     /* encryption failure */
8576e20
         }
8576e20
-- 
8576e20
1.7.1
8576e20
8576e20
8576e20
From 9f6fd5af8282ef82c0ff8d89cf9ebc121bb35b59 Mon Sep 17 00:00:00 2001
8576e20
From: Kamil Dudka <kdudka@redhat.com>
8576e20
Date: Tue, 11 Sep 2012 09:02:31 +0200
8576e20
Subject: [PATCH 4/4] Revert "aes: the init function fails when OpenSSL has AES support"
8576e20
8576e20
This partially reverts commit f4f2298ef3635acd031cc2ee0e71026cdcda5864.
8576e20
8576e20
We need to use the EVP_aes_???_ctr() functions in FIPS mode.
8576e20
---
8576e20
 src/openssl.h |    6 ++++++
8576e20
 1 files changed, 6 insertions(+), 0 deletions(-)
8576e20
8576e20
diff --git a/src/openssl.h b/src/openssl.h
8576e20
index 4835ab6..6f21a1a 100644
8576e20
--- a/src/openssl.h
8576e20
+++ b/src/openssl.h
8576e20
@@ -151,9 +151,15 @@ void libssh2_md5(const unsigned char *message, unsigned long len, unsigned char
8576e20
 #define _libssh2_cipher_aes256 EVP_aes_256_cbc
8576e20
 #define _libssh2_cipher_aes192 EVP_aes_192_cbc
8576e20
 #define _libssh2_cipher_aes128 EVP_aes_128_cbc
8576e20
+#ifdef HAVE_EVP_AES_128_CTR
8576e20
+#define _libssh2_cipher_aes128ctr EVP_aes_128_ctr
8576e20
+#define _libssh2_cipher_aes192ctr EVP_aes_192_ctr
8576e20
+#define _libssh2_cipher_aes256ctr EVP_aes_256_ctr
8576e20
+#else
8576e20
 #define _libssh2_cipher_aes128ctr _libssh2_EVP_aes_128_ctr
8576e20
 #define _libssh2_cipher_aes192ctr _libssh2_EVP_aes_192_ctr
8576e20
 #define _libssh2_cipher_aes256ctr _libssh2_EVP_aes_256_ctr
8576e20
+#endif
8576e20
 #define _libssh2_cipher_blowfish EVP_bf_cbc
8576e20
 #define _libssh2_cipher_arcfour EVP_rc4
8576e20
 #define _libssh2_cipher_cast5 EVP_cast5_cbc
8576e20
-- 
8576e20
1.7.1
8576e20