Apply by doing:
	cd /usr/src
	patch -p0 < 013_openssl2.patch

And then rebuild and install OpenSSL:
	cd lib/libssl
	make obj
	make depend
	make includes
	make
	make install

Index: lib/libssl/src/crypto/asn1/tasn_dec.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/tasn_dec.c,v
retrieving revision 1.4
retrieving revision 1.4.10.1
diff -u -p -r1.4 -r1.4.10.1
--- lib/libssl/src/crypto/asn1/tasn_dec.c	30 Sep 2003 15:19:38 -0000	1.4
+++ lib/libssl/src/crypto/asn1/tasn_dec.c	5 Oct 2006 17:36:24 -0000	1.4.10.1
@@ -628,6 +628,8 @@ static int asn1_d2i_ex_primitive(ASN1_VA
 		ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
 		return 0;
 	} else if(ret == -1) return -1;
+
+	ret = 0;
 	/* SEQUENCE, SET and "OTHER" are left in encoded form */
 	if((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
 		/* Clear context cache for type OTHER because the auto clear when
Index: lib/libssl/src/crypto/dh/dh.h
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/dh/dh.h,v
retrieving revision 1.5
retrieving revision 1.5.16.1
diff -u -p -r1.5 -r1.5.16.1
--- lib/libssl/src/crypto/dh/dh.h	15 May 2002 02:29:12 -0000	1.5
+++ lib/libssl/src/crypto/dh/dh.h	5 Oct 2006 17:36:24 -0000	1.5.16.1
@@ -70,6 +70,8 @@
 #include <openssl/crypto.h>
 #include <openssl/ossl_typ.h>
 	
+#define OPENSSL_DH_MAX_MODULUS_BITS	10000
+
 #define DH_FLAG_CACHE_MONT_P	0x01
 
 #ifdef  __cplusplus
@@ -200,6 +202,7 @@ void ERR_load_DH_strings(void);
 /* Reason codes. */
 #define DH_R_BAD_GENERATOR				 101
 #define DH_R_NO_PRIVATE_VALUE				 100
+#define DH_R_MODULUS_TOO_LARGE				 103
 
 #ifdef  __cplusplus
 }
Index: lib/libssl/src/crypto/dh/dh_err.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/dh/dh_err.c,v
retrieving revision 1.5
retrieving revision 1.5.4.1
diff -u -p -r1.5 -r1.5.4.1
--- lib/libssl/src/crypto/dh/dh_err.c	29 Apr 2005 05:39:20 -0000	1.5
+++ lib/libssl/src/crypto/dh/dh_err.c	5 Oct 2006 17:36:24 -0000	1.5.4.1
@@ -79,6 +79,7 @@ static ERR_STRING_DATA DH_str_reasons[]=
 	{
 {DH_R_BAD_GENERATOR                      ,"bad generator"},
 {DH_R_NO_PRIVATE_VALUE                   ,"no private value"},
+{DH_R_MODULUS_TOO_LARGE                  ,"modulus too large"},
 {0,NULL}
 	};
 
Index: lib/libssl/src/crypto/dh/dh_key.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/dh/dh_key.c,v
retrieving revision 1.8
retrieving revision 1.8.4.1
diff -u -p -r1.8 -r1.8.4.1
--- lib/libssl/src/crypto/dh/dh_key.c	29 Apr 2005 05:39:20 -0000	1.8
+++ lib/libssl/src/crypto/dh/dh_key.c	5 Oct 2006 17:36:24 -0000	1.8.4.1
@@ -164,6 +164,12 @@ static int compute_key(unsigned char *ke
 	BIGNUM *tmp;
 	int ret= -1;
 
+	if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS)
+		{
+		DHerr(DH_F_DH_COMPUTE_KEY,DH_R_MODULUS_TOO_LARGE);
+		return -1;
+		}
+
 	ctx = BN_CTX_new();
 	if (ctx == NULL) goto err;
 	BN_CTX_start(ctx);
Index: lib/libssl/src/crypto/dsa/dsa.h
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/dsa/dsa.h,v
retrieving revision 1.7
retrieving revision 1.7.4.1
diff -u -p -r1.7 -r1.7.4.1
--- lib/libssl/src/crypto/dsa/dsa.h	29 Apr 2005 05:39:20 -0000	1.7
+++ lib/libssl/src/crypto/dsa/dsa.h	5 Oct 2006 17:36:24 -0000	1.7.4.1
@@ -79,6 +79,8 @@
 # include <openssl/dh.h>
 #endif
 
+#define OPENSSL_DSA_MAX_MODULUS_BITS	3072
+
 #define DSA_FLAG_CACHE_MONT_P	0x01
 
 #if defined(OPENSSL_FIPS)
@@ -245,8 +247,10 @@ void ERR_load_DSA_strings(void);
 #define DSA_F_SIG_CB					 114
 
 /* Reason codes. */
+#define DSA_R_BAD_Q_VALUE				 102
 #define DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE		 100
 #define DSA_R_MISSING_PARAMETERS			 101
+#define DSA_R_MODULUS_TOO_LARGE				 103
 
 #ifdef  __cplusplus
 }
Index: lib/libssl/src/crypto/dsa/dsa_err.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/dsa/dsa_err.c,v
retrieving revision 1.6
retrieving revision 1.6.16.1
diff -u -p -r1.6 -r1.6.16.1
--- lib/libssl/src/crypto/dsa/dsa_err.c	15 May 2002 02:29:12 -0000	1.6
+++ lib/libssl/src/crypto/dsa/dsa_err.c	5 Oct 2006 17:36:24 -0000	1.6.16.1
@@ -85,8 +85,10 @@ static ERR_STRING_DATA DSA_str_functs[]=
 
 static ERR_STRING_DATA DSA_str_reasons[]=
 	{
+{DSA_R_BAD_Q_VALUE                       ,"bad q value"},
 {DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE       ,"data too large for key size"},
 {DSA_R_MISSING_PARAMETERS                ,"missing parameters"},
+{DSA_R_MODULUS_TOO_LARGE                 ,"modulus too large"},
 {0,NULL}
 	};
 
Index: lib/libssl/src/crypto/dsa/dsa_ossl.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/dsa/dsa_ossl.c,v
retrieving revision 1.8
retrieving revision 1.8.4.1
diff -u -p -r1.8 -r1.8.4.1
--- lib/libssl/src/crypto/dsa/dsa_ossl.c	29 Apr 2005 05:39:20 -0000	1.8
+++ lib/libssl/src/crypto/dsa/dsa_ossl.c	5 Oct 2006 17:36:24 -0000	1.8.4.1
@@ -245,6 +245,18 @@ static int dsa_do_verify(const unsigned 
 		return -1;
 		}
 
+	if (BN_num_bits(dsa->q) != 160)
+		{
+		DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_BAD_Q_VALUE);
+		return -1;
+		}
+
+	if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS)
+		{
+		DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MODULUS_TOO_LARGE);
+		return -1;
+		}
+
 	BN_init(&u1);
 	BN_init(&u2);
 	BN_init(&t1);
Index: lib/libssl/src/crypto/rsa/rsa.h
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/rsa/rsa.h,v
retrieving revision 1.9
retrieving revision 1.9.4.1
diff -u -p -r1.9 -r1.9.4.1
--- lib/libssl/src/crypto/rsa/rsa.h	29 Apr 2005 05:39:26 -0000	1.9
+++ lib/libssl/src/crypto/rsa/rsa.h	5 Oct 2006 17:36:24 -0000	1.9.4.1
@@ -154,6 +154,11 @@ struct rsa_st
 	BN_BLINDING *blinding;
 	};
 
+#define OPENSSL_RSA_MAX_MODULUS_BITS	16384
+
+#define OPENSSL_RSA_SMALL_MODULUS_BITS	3072
+#define OPENSSL_RSA_MAX_PUBEXP_BITS	64 /* exponent limit enforced for "small" modulus only */
+
 #define RSA_3	0x3L
 #define RSA_F4	0x10001L
 
@@ -347,6 +352,7 @@ void ERR_load_RSA_strings(void);
 #define RSA_R_INVALID_MESSAGE_LENGTH			 131
 #define RSA_R_IQMP_NOT_INVERSE_OF_Q			 126
 #define RSA_R_KEY_SIZE_TOO_SMALL			 120
+#define RSA_R_MODULUS_TOO_LARGE				 105
 #define RSA_R_NULL_BEFORE_BLOCK_MISSING			 113
 #define RSA_R_N_DOES_NOT_EQUAL_P_Q			 127
 #define RSA_R_OAEP_DECODING_ERROR			 121
Index: lib/libssl/src/crypto/rsa/rsa_eay.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/rsa/rsa_eay.c,v
retrieving revision 1.15
retrieving revision 1.15.4.1
diff -u -p -r1.15 -r1.15.4.1
--- lib/libssl/src/crypto/rsa/rsa_eay.c	29 Apr 2005 05:39:26 -0000	1.15
+++ lib/libssl/src/crypto/rsa/rsa_eay.c	5 Oct 2006 17:36:24 -0000	1.15.4.1
@@ -259,6 +259,28 @@ static int RSA_eay_private_encrypt(int f
 	BN_init(&f);
 	BN_init(&ret);
 
+	if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
+		{
+		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
+		return -1;
+		}
+
+	if (BN_ucmp(rsa->n, rsa->e) <= 0)
+		{
+		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
+		return -1;
+		}
+
+	/* for large moduli, enforce exponent limit */
+	if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
+		{
+		if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
+			{
+			RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
+			return -1;
+			}
+		}
+
 	if ((ctx=BN_CTX_new()) == NULL) goto err;
 	num=BN_num_bytes(rsa->n);
 	if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
@@ -503,6 +525,28 @@ static int RSA_eay_public_decrypt(int fl
 	unsigned char *p;
 	unsigned char *buf=NULL;
 	BN_CTX *ctx=NULL;
+
+	if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
+		{
+		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
+		return -1;
+		}
+
+	if (BN_ucmp(rsa->n, rsa->e) <= 0)
+		{
+		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
+		return -1;
+		}
+
+	/* for large moduli, enforce exponent limit */
+	if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
+		{
+		if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
+			{
+			RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
+			return -1;
+			}
+		}
 
 	BN_init(&f);
 	BN_init(&ret);
Index: lib/libssl/src/crypto/rsa/rsa_err.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/rsa/rsa_err.c,v
retrieving revision 1.5
retrieving revision 1.5.16.1
diff -u -p -r1.5 -r1.5.16.1
--- lib/libssl/src/crypto/rsa/rsa_err.c	15 May 2002 02:29:16 -0000	1.5
+++ lib/libssl/src/crypto/rsa/rsa_err.c	5 Oct 2006 17:36:24 -0000	1.5.16.1
@@ -116,6 +116,7 @@ static ERR_STRING_DATA RSA_str_reasons[]
 {RSA_R_INVALID_MESSAGE_LENGTH            ,"invalid message length"},
 {RSA_R_IQMP_NOT_INVERSE_OF_Q             ,"iqmp not inverse of q"},
 {RSA_R_KEY_SIZE_TOO_SMALL                ,"key size too small"},
+{RSA_R_MODULUS_TOO_LARGE                 ,"modulus too large"},
 {RSA_R_NULL_BEFORE_BLOCK_MISSING         ,"null before block missing"},
 {RSA_R_N_DOES_NOT_EQUAL_P_Q              ,"n does not equal p q"},
 {RSA_R_OAEP_DECODING_ERROR               ,"oaep decoding error"},
Index: lib/libssl/src/ssl/s2_clnt.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/ssl/s2_clnt.c,v
retrieving revision 1.11
retrieving revision 1.11.4.1
diff -u -p -r1.11 -r1.11.4.1
--- lib/libssl/src/ssl/s2_clnt.c	29 Apr 2005 05:39:31 -0000	1.11
+++ lib/libssl/src/ssl/s2_clnt.c	5 Oct 2006 17:36:24 -0000	1.11.4.1
@@ -538,7 +538,8 @@ static int get_server_hello(SSL *s)
 		CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509);
 		}
 
-	if (s->session->peer != s->session->sess_cert->peer_key->x509)
+	if (s->session->sess_cert == NULL ||
+	    s->session->peer != s->session->sess_cert->peer_key->x509)
 		/* can't happen */
 		{
 		ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
Index: lib/libssl/src/ssl/ssl_lib.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/ssl/ssl_lib.c,v
retrieving revision 1.13
retrieving revision 1.13.4.1
diff -u -p -r1.13 -r1.13.4.1
--- lib/libssl/src/ssl/ssl_lib.c	29 Apr 2005 05:39:31 -0000	1.13
+++ lib/libssl/src/ssl/ssl_lib.c	5 Oct 2006 17:36:24 -0000	1.13.4.1
@@ -1167,7 +1167,7 @@ char *SSL_get_shared_ciphers(const SSL *
 		c=sk_SSL_CIPHER_value(sk,i);
 		for (cp=c->name; *cp; )
 			{
-			if (len-- == 0)
+			if (len-- <= 0)
 				{
 				*p='\0';
 				return(buf);
