summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordjm <>2009-01-09 12:14:11 +0000
committerdjm <>2009-01-09 12:14:11 +0000
commita0fdc9ec41594852f67ec77dfad9cb06bacc4186 (patch)
treec43f6b3a4d93ad2cb3dcf93275295679d895a033
parent5a3c0a05c7f2c5d3c584b7c8d6aec836dd724c80 (diff)
downloadopenbsd-a0fdc9ec41594852f67ec77dfad9cb06bacc4186.tar.gz
openbsd-a0fdc9ec41594852f67ec77dfad9cb06bacc4186.tar.bz2
openbsd-a0fdc9ec41594852f67ec77dfad9cb06bacc4186.zip
import openssl-0.9.8j
-rw-r--r--src/lib/libcrypto/aes/aes.h4
-rw-r--r--src/lib/libcrypto/aes/aes_cbc.c2
-rw-r--r--src/lib/libcrypto/aes/aes_core.c8
-rw-r--r--src/lib/libcrypto/aes/asm/aes-586.pl3
-rwxr-xr-xsrc/lib/libcrypto/aes/asm/aes-x86_64.pl7
-rw-r--r--src/lib/libcrypto/asn1/a_mbstr.c2
-rw-r--r--src/lib/libcrypto/asn1/a_sign.c7
-rw-r--r--src/lib/libcrypto/asn1/a_strex.c2
-rw-r--r--src/lib/libcrypto/asn1/a_strnid.c2
-rw-r--r--src/lib/libcrypto/asn1/a_verify.c7
-rw-r--r--src/lib/libcrypto/asn1/asn1_gen.c2
-rw-r--r--src/lib/libcrypto/asn1/asn1t.h2
-rw-r--r--src/lib/libcrypto/asn1/asn_mime.c2
-rw-r--r--src/lib/libcrypto/asn1/asn_moid.c2
-rw-r--r--src/lib/libcrypto/asn1/asn_pack.c2
-rw-r--r--src/lib/libcrypto/asn1/nsseq.c2
-rw-r--r--src/lib/libcrypto/asn1/p5_pbe.c2
-rw-r--r--src/lib/libcrypto/asn1/p5_pbev2.c2
-rw-r--r--src/lib/libcrypto/asn1/p8_pkey.c2
-rw-r--r--src/lib/libcrypto/asn1/t_bitst.c2
-rw-r--r--src/lib/libcrypto/asn1/t_crl.c2
-rw-r--r--src/lib/libcrypto/asn1/t_spki.c2
-rw-r--r--src/lib/libcrypto/asn1/t_x509.c11
-rw-r--r--src/lib/libcrypto/asn1/t_x509a.c2
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c2
-rw-r--r--src/lib/libcrypto/asn1/tasn_enc.c2
-rw-r--r--src/lib/libcrypto/asn1/tasn_fre.c2
-rw-r--r--src/lib/libcrypto/asn1/tasn_new.c2
-rw-r--r--src/lib/libcrypto/asn1/tasn_prn.c2
-rw-r--r--src/lib/libcrypto/asn1/tasn_typ.c2
-rw-r--r--src/lib/libcrypto/asn1/tasn_utl.c2
-rw-r--r--src/lib/libcrypto/asn1/x_algor.c2
-rw-r--r--src/lib/libcrypto/asn1/x_bignum.c2
-rw-r--r--src/lib/libcrypto/asn1/x_exten.c2
-rw-r--r--src/lib/libcrypto/asn1/x_long.c2
-rw-r--r--src/lib/libcrypto/asn1/x_x509a.c2
-rw-r--r--src/lib/libcrypto/bf/bf_skey.c7
-rw-r--r--src/lib/libcrypto/bf/blowfish.h4
-rw-r--r--src/lib/libcrypto/bio/bss_bio.c2
-rw-r--r--src/lib/libcrypto/bio/bss_dgram.c64
-rw-r--r--src/lib/libcrypto/bio/bss_file.c2
-rw-r--r--src/lib/libcrypto/bn/bn.h15
-rw-r--r--src/lib/libcrypto/bn/bn_div.c15
-rw-r--r--src/lib/libcrypto/bn/bn_gf2m.c6
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c19
-rw-r--r--src/lib/libcrypto/bn/bn_nist.c776
-rw-r--r--src/lib/libcrypto/bn/bn_rand.c6
-rw-r--r--src/lib/libcrypto/bn/bn_shift.c2
-rw-r--r--src/lib/libcrypto/bn/bn_x931p.c40
-rw-r--r--src/lib/libcrypto/buffer/buf_str.c116
-rw-r--r--src/lib/libcrypto/buffer/buffer.c58
-rw-r--r--src/lib/libcrypto/camellia/camellia.h5
-rw-r--r--src/lib/libcrypto/camellia/cmll_misc.c13
-rw-r--r--src/lib/libcrypto/cast/c_skey.c7
-rw-r--r--src/lib/libcrypto/cast/cast.h4
-rw-r--r--src/lib/libcrypto/cms/cms_sd.c2
-rw-r--r--src/lib/libcrypto/cms/cms_smime.c9
-rw-r--r--src/lib/libcrypto/comp/c_zlib.c4
-rw-r--r--src/lib/libcrypto/conf/conf_mall.c4
-rw-r--r--src/lib/libcrypto/conf/conf_mod.c2
-rw-r--r--src/lib/libcrypto/conf/conf_sap.c2
-rw-r--r--src/lib/libcrypto/cryptlib.c351
-rw-r--r--src/lib/libcrypto/cryptlib.h1
-rw-r--r--src/lib/libcrypto/crypto.h85
-rw-r--r--src/lib/libcrypto/des/des_enc.c4
-rw-r--r--src/lib/libcrypto/des/ecb_enc.c47
-rw-r--r--src/lib/libcrypto/des/enc_read.c4
-rw-r--r--src/lib/libcrypto/des/enc_writ.c4
-rw-r--r--src/lib/libcrypto/des/set_key.c9
-rw-r--r--src/lib/libcrypto/dh/dh.h11
-rw-r--r--src/lib/libcrypto/dh/dh_asn1.c2
-rw-r--r--src/lib/libcrypto/dh/dh_check.c4
-rw-r--r--src/lib/libcrypto/dh/dh_err.c6
-rw-r--r--src/lib/libcrypto/dh/dh_gen.c4
-rw-r--r--src/lib/libcrypto/dh/dh_key.c4
-rw-r--r--src/lib/libcrypto/dsa/dsa.h39
-rw-r--r--src/lib/libcrypto/dsa/dsa_asn1.c82
-rw-r--r--src/lib/libcrypto/dsa/dsa_err.c10
-rw-r--r--src/lib/libcrypto/dsa/dsa_gen.c3
-rw-r--r--src/lib/libcrypto/dsa/dsa_key.c4
-rw-r--r--src/lib/libcrypto/dsa/dsa_lib.c49
-rw-r--r--src/lib/libcrypto/dsa/dsa_ossl.c3
-rw-r--r--src/lib/libcrypto/dsa/dsa_sign.c31
-rw-r--r--src/lib/libcrypto/dsa/dsa_vrf.c32
-rw-r--r--src/lib/libcrypto/ec/ec_key.c16
-rw-r--r--src/lib/libcrypto/engine/eng_all.c3
-rw-r--r--src/lib/libcrypto/engine/eng_cnf.c11
-rw-r--r--src/lib/libcrypto/engine/eng_err.c3
-rw-r--r--src/lib/libcrypto/engine/eng_int.h2
-rw-r--r--src/lib/libcrypto/engine/eng_padlock.c4
-rw-r--r--src/lib/libcrypto/engine/eng_pkey.c42
-rw-r--r--src/lib/libcrypto/engine/engine.h16
-rw-r--r--src/lib/libcrypto/err/err.c781
-rw-r--r--src/lib/libcrypto/err/err.h12
-rw-r--r--src/lib/libcrypto/err/err_all.c13
-rw-r--r--src/lib/libcrypto/err/err_prn.c70
-rw-r--r--src/lib/libcrypto/err/openssl.ec2
-rw-r--r--src/lib/libcrypto/evp/bio_md.c9
-rw-r--r--src/lib/libcrypto/evp/digest.c154
-rw-r--r--src/lib/libcrypto/evp/e_aes.c35
-rw-r--r--src/lib/libcrypto/evp/e_camellia.c2
-rw-r--r--src/lib/libcrypto/evp/e_des.c9
-rw-r--r--src/lib/libcrypto/evp/e_des3.c29
-rw-r--r--src/lib/libcrypto/evp/e_null.c2
-rw-r--r--src/lib/libcrypto/evp/e_rc4.c1
-rw-r--r--src/lib/libcrypto/evp/evp.h80
-rw-r--r--src/lib/libcrypto/evp/evp_enc.c267
-rw-r--r--src/lib/libcrypto/evp/evp_err.c16
-rw-r--r--src/lib/libcrypto/evp/evp_lib.c39
-rw-r--r--src/lib/libcrypto/evp/evp_locl.h30
-rw-r--r--src/lib/libcrypto/evp/evp_pbe.c2
-rw-r--r--src/lib/libcrypto/evp/evp_pkey.c2
-rw-r--r--src/lib/libcrypto/evp/m_dss.c2
-rw-r--r--src/lib/libcrypto/evp/m_dss1.c3
-rw-r--r--src/lib/libcrypto/evp/m_md4.c1
-rw-r--r--src/lib/libcrypto/evp/m_md5.c1
-rw-r--r--src/lib/libcrypto/evp/m_sha1.c7
-rw-r--r--src/lib/libcrypto/evp/names.c7
-rw-r--r--src/lib/libcrypto/evp/p5_crpt.c2
-rw-r--r--src/lib/libcrypto/evp/p5_crpt2.c2
-rw-r--r--src/lib/libcrypto/evp/p_sign.c24
-rw-r--r--src/lib/libcrypto/evp/p_verify.c26
-rw-r--r--src/lib/libcrypto/hmac/hmac.c10
-rw-r--r--src/lib/libcrypto/hmac/hmac.h1
-rw-r--r--src/lib/libcrypto/idea/idea.h3
-rw-r--r--src/lib/libcrypto/md32_common.h2
-rw-r--r--src/lib/libcrypto/md4/md4.h3
-rw-r--r--src/lib/libcrypto/md4/md4_dgst.c7
-rw-r--r--src/lib/libcrypto/md5/md5.h3
-rw-r--r--src/lib/libcrypto/md5/md5_dgst.c7
-rw-r--r--src/lib/libcrypto/mem_dbg.c28
-rw-r--r--src/lib/libcrypto/o_init.c86
-rw-r--r--src/lib/libcrypto/objects/obj_dat.pl4
-rw-r--r--src/lib/libcrypto/objects/obj_mac.num68
-rw-r--r--src/lib/libcrypto/objects/objects.txt108
-rw-r--r--src/lib/libcrypto/ocsp/ocsp_asn.c2
-rw-r--r--src/lib/libcrypto/ocsp/ocsp_ht.c5
-rw-r--r--src/lib/libcrypto/ocsp/ocsp_srv.c2
-rw-r--r--src/lib/libcrypto/ocsp/ocsp_vfy.c2
-rw-r--r--src/lib/libcrypto/opensslv.h6
-rw-r--r--src/lib/libcrypto/ossl_typ.h4
-rw-r--r--src/lib/libcrypto/pem/pem.h1
-rw-r--r--src/lib/libcrypto/pem/pem_all.c174
-rw-r--r--src/lib/libcrypto/pem/pem_lib.c3
-rw-r--r--src/lib/libcrypto/pem/pem_x509.c2
-rw-r--r--src/lib/libcrypto/pem/pem_xaux.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_add.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_asn.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_attr.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_crpt.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_crt.c39
-rw-r--r--src/lib/libcrypto/pkcs12/p12_decr.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_init.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_key.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_kiss.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_mutl.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_npas.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_p8d.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_p8e.c2
-rw-r--r--src/lib/libcrypto/pkcs12/p12_utl.c2
-rw-r--r--src/lib/libcrypto/pkcs12/pkcs12.h2
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_asn1.c2
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_attr.c2
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_mime.c2
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_smime.c3
-rw-r--r--src/lib/libcrypto/rand/rand.h29
-rw-r--r--src/lib/libcrypto/rand/rand_err.c20
-rw-r--r--src/lib/libcrypto/rand/rand_lib.c71
-rw-r--r--src/lib/libcrypto/rand/randfile.c66
-rw-r--r--src/lib/libcrypto/rc2/rc2.h4
-rw-r--r--src/lib/libcrypto/rc2/rc2_skey.c17
-rwxr-xr-xsrc/lib/libcrypto/rc4/asm/rc4-x86_64.pl2
-rw-r--r--src/lib/libcrypto/rc4/rc4.h3
-rw-r--r--src/lib/libcrypto/rc4/rc4_skey.c16
-rw-r--r--src/lib/libcrypto/ripemd/ripemd.h4
-rw-r--r--src/lib/libcrypto/ripemd/rmd_dgst.c7
-rw-r--r--src/lib/libcrypto/ripemd/rmd_locl.h2
-rw-r--r--src/lib/libcrypto/rsa/rsa.h41
-rw-r--r--src/lib/libcrypto/rsa/rsa_asn1.c2
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c41
-rw-r--r--src/lib/libcrypto/rsa/rsa_err.c10
-rw-r--r--src/lib/libcrypto/rsa/rsa_gen.c3
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c272
-rw-r--r--src/lib/libcrypto/rsa/rsa_oaep.c2
-rw-r--r--src/lib/libcrypto/rsa/rsa_pss.c6
-rw-r--r--src/lib/libcrypto/rsa/rsa_sign.c24
-rw-r--r--src/lib/libcrypto/rsa/rsa_ssl.c2
-rw-r--r--src/lib/libcrypto/rsa/rsa_x931.c2
-rw-r--r--src/lib/libcrypto/sha/asm/sha1-586.pl2
-rw-r--r--src/lib/libcrypto/sha/sha.h3
-rw-r--r--src/lib/libcrypto/sha/sha1_one.c2
-rw-r--r--src/lib/libcrypto/sha/sha1dgst.c4
-rw-r--r--src/lib/libcrypto/sha/sha256.c10
-rw-r--r--src/lib/libcrypto/sha/sha512.c12
-rw-r--r--src/lib/libcrypto/sha/sha_locl.h7
-rw-r--r--src/lib/libcrypto/stack/safestack.h44
-rw-r--r--src/lib/libcrypto/ui/ui_openssl.c2
-rw-r--r--src/lib/libcrypto/util/mkerr.pl3
-rw-r--r--src/lib/libcrypto/x509/by_dir.c4
-rw-r--r--src/lib/libcrypto/x509/x509_att.c12
-rw-r--r--src/lib/libcrypto/x509/x509_cmp.c8
-rw-r--r--src/lib/libcrypto/x509/x509_trs.c2
-rw-r--r--src/lib/libcrypto/x509/x509_vfy.c12
-rw-r--r--src/lib/libcrypto/x509/x509_vpm.c2
-rw-r--r--src/lib/libcrypto/x509/x509cset.c2
-rw-r--r--src/lib/libcrypto/x509/x509spki.c2
-rw-r--r--src/lib/libcrypto/x509v3/ext_dat.h2
-rw-r--r--src/lib/libcrypto/x509v3/pcy_cache.c2
-rw-r--r--src/lib/libcrypto/x509v3/pcy_data.c10
-rw-r--r--src/lib/libcrypto/x509v3/pcy_int.h2
-rw-r--r--src/lib/libcrypto/x509v3/pcy_lib.c2
-rw-r--r--src/lib/libcrypto/x509v3/pcy_map.c2
-rw-r--r--src/lib/libcrypto/x509v3/pcy_node.c2
-rw-r--r--src/lib/libcrypto/x509v3/pcy_tree.c20
-rw-r--r--src/lib/libcrypto/x509v3/v3_akey.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_akeya.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_alt.c5
-rw-r--r--src/lib/libcrypto/x509v3/v3_bcons.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_bitst.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_conf.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_cpols.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_crld.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_enum.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_extku.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_genn.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_ia5.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_info.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_int.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_lib.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_ncons.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_ocsp.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_pcons.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_pku.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_pmaps.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_prn.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_purp.c8
-rw-r--r--src/lib/libcrypto/x509v3/v3_skey.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_sxnet.c2
-rw-r--r--src/lib/libcrypto/x509v3/v3_utl.c23
-rw-r--r--src/lib/libcrypto/x509v3/x509v3.h6
-rw-r--r--src/lib/libssl/d1_clnt.c3
-rw-r--r--src/lib/libssl/d1_enc.c19
-rw-r--r--src/lib/libssl/d1_lib.c1
-rw-r--r--src/lib/libssl/d1_pkt.c22
-rw-r--r--src/lib/libssl/d1_srvr.c6
-rw-r--r--src/lib/libssl/dtls1.h7
-rw-r--r--src/lib/libssl/s23_clnt.c16
-rw-r--r--src/lib/libssl/s23_srvr.c9
-rw-r--r--src/lib/libssl/s3_clnt.c52
-rw-r--r--src/lib/libssl/s3_lib.c40
-rw-r--r--src/lib/libssl/s3_pkt.c16
-rw-r--r--src/lib/libssl/s3_srvr.c42
-rw-r--r--src/lib/libssl/ssl.h16
-rw-r--r--src/lib/libssl/ssl_asn1.c2
-rw-r--r--src/lib/libssl/ssl_ciph.c13
-rw-r--r--src/lib/libssl/ssl_err.c5
-rw-r--r--src/lib/libssl/ssl_lib.c36
-rw-r--r--src/lib/libssl/ssl_locl.h8
-rw-r--r--src/lib/libssl/ssl_sess.c22
-rw-r--r--src/lib/libssl/t1_enc.c42
-rw-r--r--src/lib/libssl/t1_lib.c14
-rw-r--r--src/lib/libssl/test/CAss.cnf2
-rw-r--r--src/lib/libssl/test/Uss.cnf2
263 files changed, 3222 insertions, 2494 deletions
diff --git a/src/lib/libcrypto/aes/aes.h b/src/lib/libcrypto/aes/aes.h
index baf0222d49..450f2b4051 100644
--- a/src/lib/libcrypto/aes/aes.h
+++ b/src/lib/libcrypto/aes/aes.h
@@ -66,6 +66,10 @@
66#define AES_MAXNR 14 66#define AES_MAXNR 14
67#define AES_BLOCK_SIZE 16 67#define AES_BLOCK_SIZE 16
68 68
69#ifdef OPENSSL_FIPS
70#define FIPS_AES_SIZE_T int
71#endif
72
69#ifdef __cplusplus 73#ifdef __cplusplus
70extern "C" { 74extern "C" {
71#endif 75#endif
diff --git a/src/lib/libcrypto/aes/aes_cbc.c b/src/lib/libcrypto/aes/aes_cbc.c
index d2ba6bcdb4..373864cd4b 100644
--- a/src/lib/libcrypto/aes/aes_cbc.c
+++ b/src/lib/libcrypto/aes/aes_cbc.c
@@ -59,6 +59,7 @@
59#include <openssl/aes.h> 59#include <openssl/aes.h>
60#include "aes_locl.h" 60#include "aes_locl.h"
61 61
62#if !defined(OPENSSL_FIPS_AES_ASM)
62void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, 63void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
63 const unsigned long length, const AES_KEY *key, 64 const unsigned long length, const AES_KEY *key,
64 unsigned char *ivec, const int enc) { 65 unsigned char *ivec, const int enc) {
@@ -129,3 +130,4 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
129 } 130 }
130 } 131 }
131} 132}
133#endif
diff --git a/src/lib/libcrypto/aes/aes_core.c b/src/lib/libcrypto/aes/aes_core.c
index 3a80e18b0a..cffdd4daec 100644
--- a/src/lib/libcrypto/aes/aes_core.c
+++ b/src/lib/libcrypto/aes/aes_core.c
@@ -37,6 +37,10 @@
37 37
38#include <stdlib.h> 38#include <stdlib.h>
39#include <openssl/aes.h> 39#include <openssl/aes.h>
40#ifdef OPENSSL_FIPS
41#include <openssl/fips.h>
42#endif
43
40#include "aes_locl.h" 44#include "aes_locl.h"
41 45
42/* 46/*
@@ -631,6 +635,10 @@ int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
631 int i = 0; 635 int i = 0;
632 u32 temp; 636 u32 temp;
633 637
638#ifdef OPENSSL_FIPS
639 FIPS_selftest_check();
640#endif
641
634 if (!userKey || !key) 642 if (!userKey || !key)
635 return -1; 643 return -1;
636 if (bits != 128 && bits != 192 && bits != 256) 644 if (bits != 128 && bits != 192 && bits != 256)
diff --git a/src/lib/libcrypto/aes/asm/aes-586.pl b/src/lib/libcrypto/aes/asm/aes-586.pl
index 89fa261794..3bc46a968e 100644
--- a/src/lib/libcrypto/aes/asm/aes-586.pl
+++ b/src/lib/libcrypto/aes/asm/aes-586.pl
@@ -955,8 +955,9 @@ my $mark=&DWP(60+240,"esp"); #copy of aes_key->rounds
955 955
956 &align (4); 956 &align (4);
957 &set_label("enc_tail"); 957 &set_label("enc_tail");
958 &push ($key eq "edi" ? $key : ""); # push ivp 958 &mov ($s0,$key eq "edi" ? $key : "");
959 &mov ($key,$_out); # load out 959 &mov ($key,$_out); # load out
960 &push ($s0); # push ivp
960 &mov ($s1,16); 961 &mov ($s1,16);
961 &sub ($s1,$s2); 962 &sub ($s1,$s2);
962 &cmp ($key,$acc); # compare with inp 963 &cmp ($key,$acc); # compare with inp
diff --git a/src/lib/libcrypto/aes/asm/aes-x86_64.pl b/src/lib/libcrypto/aes/asm/aes-x86_64.pl
index 44e0bf8cae..f616f1751f 100755
--- a/src/lib/libcrypto/aes/asm/aes-x86_64.pl
+++ b/src/lib/libcrypto/aes/asm/aes-x86_64.pl
@@ -1198,19 +1198,20 @@ AES_cbc_encrypt:
1198 ret 1198 ret
1199.align 4 1199.align 4
1200.Lcbc_enc_tail: 1200.Lcbc_enc_tail:
1201 cmp $inp,$out 1201 mov %rax,%r11
1202 je .Lcbc_enc_in_place 1202 mov %rcx,%r12
1203 mov %r10,%rcx 1203 mov %r10,%rcx
1204 mov $inp,%rsi 1204 mov $inp,%rsi
1205 mov $out,%rdi 1205 mov $out,%rdi
1206 .long 0xF689A4F3 # rep movsb 1206 .long 0xF689A4F3 # rep movsb
1207.Lcbc_enc_in_place:
1208 mov \$16,%rcx # zero tail 1207 mov \$16,%rcx # zero tail
1209 sub %r10,%rcx 1208 sub %r10,%rcx
1210 xor %rax,%rax 1209 xor %rax,%rax
1211 .long 0xF689AAF3 # rep stosb 1210 .long 0xF689AAF3 # rep stosb
1212 mov $out,$inp # this is not a mistake! 1211 mov $out,$inp # this is not a mistake!
1213 movq \$16,$_len # len=16 1212 movq \$16,$_len # len=16
1213 mov %r11,%rax
1214 mov %r12,%rcx
1214 jmp .Lcbc_enc_loop # one more spin... 1215 jmp .Lcbc_enc_loop # one more spin...
1215#----------------------------- DECRYPT -----------------------------# 1216#----------------------------- DECRYPT -----------------------------#
1216.align 16 1217.align 16
diff --git a/src/lib/libcrypto/asn1/a_mbstr.c b/src/lib/libcrypto/asn1/a_mbstr.c
index 2d4800a22a..1bcd046893 100644
--- a/src/lib/libcrypto/asn1/a_mbstr.c
+++ b/src/lib/libcrypto/asn1/a_mbstr.c
@@ -1,5 +1,5 @@
1/* a_mbstr.c */ 1/* a_mbstr.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/a_sign.c b/src/lib/libcrypto/asn1/a_sign.c
index 1081950518..4dee45fbb8 100644
--- a/src/lib/libcrypto/asn1/a_sign.c
+++ b/src/lib/libcrypto/asn1/a_sign.c
@@ -267,7 +267,12 @@ int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
267 goto err; 267 goto err;
268 } 268 }
269 269
270 EVP_SignInit_ex(&ctx,type, NULL); 270 if (!EVP_SignInit_ex(&ctx,type, NULL))
271 {
272 outl=0;
273 ASN1err(ASN1_F_ASN1_ITEM_SIGN,ERR_R_EVP_LIB);
274 goto err;
275 }
271 EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl); 276 EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl);
272 if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out, 277 if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out,
273 (unsigned int *)&outl,pkey)) 278 (unsigned int *)&outl,pkey))
diff --git a/src/lib/libcrypto/asn1/a_strex.c b/src/lib/libcrypto/asn1/a_strex.c
index c2dbb6f9a5..7fc14d3296 100644
--- a/src/lib/libcrypto/asn1/a_strex.c
+++ b/src/lib/libcrypto/asn1/a_strex.c
@@ -1,5 +1,5 @@
1/* a_strex.c */ 1/* a_strex.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/a_strnid.c b/src/lib/libcrypto/asn1/a_strnid.c
index 613bbc4a7d..fe515b52ba 100644
--- a/src/lib/libcrypto/asn1/a_strnid.c
+++ b/src/lib/libcrypto/asn1/a_strnid.c
@@ -1,5 +1,5 @@
1/* a_strnid.c */ 1/* a_strnid.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/a_verify.c b/src/lib/libcrypto/asn1/a_verify.c
index fdce6e4380..da3efaaf8d 100644
--- a/src/lib/libcrypto/asn1/a_verify.c
+++ b/src/lib/libcrypto/asn1/a_verify.c
@@ -100,7 +100,12 @@ int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
100 p=buf_in; 100 p=buf_in;
101 101
102 i2d(data,&p); 102 i2d(data,&p);
103 EVP_VerifyInit_ex(&ctx,type, NULL); 103 if (!EVP_VerifyInit_ex(&ctx,type, NULL))
104 {
105 ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_EVP_LIB);
106 ret=0;
107 goto err;
108 }
104 EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl); 109 EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl);
105 110
106 OPENSSL_cleanse(buf_in,(unsigned int)inl); 111 OPENSSL_cleanse(buf_in,(unsigned int)inl);
diff --git a/src/lib/libcrypto/asn1/asn1_gen.c b/src/lib/libcrypto/asn1/asn1_gen.c
index 26c832781e..2da38292c8 100644
--- a/src/lib/libcrypto/asn1/asn1_gen.c
+++ b/src/lib/libcrypto/asn1/asn1_gen.c
@@ -1,5 +1,5 @@
1/* asn1_gen.c */ 1/* asn1_gen.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2002. 3 * project 2002.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/asn1t.h b/src/lib/libcrypto/asn1/asn1t.h
index bf315e65ed..ac14f9415b 100644
--- a/src/lib/libcrypto/asn1/asn1t.h
+++ b/src/lib/libcrypto/asn1/asn1t.h
@@ -1,5 +1,5 @@
1/* asn1t.h */ 1/* asn1t.h */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/asn_mime.c b/src/lib/libcrypto/asn1/asn_mime.c
index fe7c4ec7ab..bc80b20d63 100644
--- a/src/lib/libcrypto/asn1/asn_mime.c
+++ b/src/lib/libcrypto/asn1/asn_mime.c
@@ -526,6 +526,8 @@ int SMIME_text(BIO *in, BIO *out)
526 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 526 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
527 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0) 527 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
528 BIO_write(out, iobuf, len); 528 BIO_write(out, iobuf, len);
529 if (len < 0)
530 return 0;
529 return 1; 531 return 1;
530} 532}
531 533
diff --git a/src/lib/libcrypto/asn1/asn_moid.c b/src/lib/libcrypto/asn1/asn_moid.c
index 9132350f10..1ea6a59248 100644
--- a/src/lib/libcrypto/asn1/asn_moid.c
+++ b/src/lib/libcrypto/asn1/asn_moid.c
@@ -1,5 +1,5 @@
1/* asn_moid.c */ 1/* asn_moid.c */
2/* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/asn_pack.c b/src/lib/libcrypto/asn1/asn_pack.c
index e8b671b7b5..f1a5a05632 100644
--- a/src/lib/libcrypto/asn1/asn_pack.c
+++ b/src/lib/libcrypto/asn1/asn_pack.c
@@ -1,5 +1,5 @@
1/* asn_pack.c */ 1/* asn_pack.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/nsseq.c b/src/lib/libcrypto/asn1/nsseq.c
index 50e2d4d07a..e551c57d59 100644
--- a/src/lib/libcrypto/asn1/nsseq.c
+++ b/src/lib/libcrypto/asn1/nsseq.c
@@ -1,5 +1,5 @@
1/* nsseq.c */ 1/* nsseq.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/p5_pbe.c b/src/lib/libcrypto/asn1/p5_pbe.c
index da91170094..c4582f8041 100644
--- a/src/lib/libcrypto/asn1/p5_pbe.c
+++ b/src/lib/libcrypto/asn1/p5_pbe.c
@@ -1,5 +1,5 @@
1/* p5_pbe.c */ 1/* p5_pbe.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/p5_pbev2.c b/src/lib/libcrypto/asn1/p5_pbev2.c
index c834a38ddf..2b0516afee 100644
--- a/src/lib/libcrypto/asn1/p5_pbev2.c
+++ b/src/lib/libcrypto/asn1/p5_pbev2.c
@@ -1,5 +1,5 @@
1/* p5_pbev2.c */ 1/* p5_pbev2.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999-2004. 3 * project 1999-2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/p8_pkey.c b/src/lib/libcrypto/asn1/p8_pkey.c
index 24b409132f..0a1957556e 100644
--- a/src/lib/libcrypto/asn1/p8_pkey.c
+++ b/src/lib/libcrypto/asn1/p8_pkey.c
@@ -1,5 +1,5 @@
1/* p8_pkey.c */ 1/* p8_pkey.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/t_bitst.c b/src/lib/libcrypto/asn1/t_bitst.c
index 397332d9b8..2e59a25fa1 100644
--- a/src/lib/libcrypto/asn1/t_bitst.c
+++ b/src/lib/libcrypto/asn1/t_bitst.c
@@ -1,5 +1,5 @@
1/* t_bitst.c */ 1/* t_bitst.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/t_crl.c b/src/lib/libcrypto/asn1/t_crl.c
index 929b3e5904..bdb244c015 100644
--- a/src/lib/libcrypto/asn1/t_crl.c
+++ b/src/lib/libcrypto/asn1/t_crl.c
@@ -1,5 +1,5 @@
1/* t_crl.c */ 1/* t_crl.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/t_spki.c b/src/lib/libcrypto/asn1/t_spki.c
index c2a5797dd8..a73369b949 100644
--- a/src/lib/libcrypto/asn1/t_spki.c
+++ b/src/lib/libcrypto/asn1/t_spki.c
@@ -1,5 +1,5 @@
1/* t_spki.c */ 1/* t_spki.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/t_x509.c b/src/lib/libcrypto/asn1/t_x509.c
index eb776b7b3b..8b09e5890f 100644
--- a/src/lib/libcrypto/asn1/t_x509.c
+++ b/src/lib/libcrypto/asn1/t_x509.c
@@ -393,8 +393,9 @@ int ASN1_GENERALIZEDTIME_print(BIO *bp, ASN1_GENERALIZEDTIME *tm)
393 d= (v[6]-'0')*10+(v[7]-'0'); 393 d= (v[6]-'0')*10+(v[7]-'0');
394 h= (v[8]-'0')*10+(v[9]-'0'); 394 h= (v[8]-'0')*10+(v[9]-'0');
395 m= (v[10]-'0')*10+(v[11]-'0'); 395 m= (v[10]-'0')*10+(v[11]-'0');
396 if ( (v[12] >= '0') && (v[12] <= '9') && 396 if (tm->length >= 14 &&
397 (v[13] >= '0') && (v[13] <= '9')) 397 (v[12] >= '0') && (v[12] <= '9') &&
398 (v[13] >= '0') && (v[13] <= '9'))
398 s= (v[12]-'0')*10+(v[13]-'0'); 399 s= (v[12]-'0')*10+(v[13]-'0');
399 400
400 if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s", 401 if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
@@ -428,8 +429,9 @@ int ASN1_UTCTIME_print(BIO *bp, ASN1_UTCTIME *tm)
428 d= (v[4]-'0')*10+(v[5]-'0'); 429 d= (v[4]-'0')*10+(v[5]-'0');
429 h= (v[6]-'0')*10+(v[7]-'0'); 430 h= (v[6]-'0')*10+(v[7]-'0');
430 m= (v[8]-'0')*10+(v[9]-'0'); 431 m= (v[8]-'0')*10+(v[9]-'0');
431 if ( (v[10] >= '0') && (v[10] <= '9') && 432 if (tm->length >=12 &&
432 (v[11] >= '0') && (v[11] <= '9')) 433 (v[10] >= '0') && (v[10] <= '9') &&
434 (v[11] >= '0') && (v[11] <= '9'))
433 s= (v[10]-'0')*10+(v[11]-'0'); 435 s= (v[10]-'0')*10+(v[11]-'0');
434 436
435 if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s", 437 if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
@@ -501,4 +503,3 @@ err:
501 OPENSSL_free(b); 503 OPENSSL_free(b);
502 return(ret); 504 return(ret);
503 } 505 }
504
diff --git a/src/lib/libcrypto/asn1/t_x509a.c b/src/lib/libcrypto/asn1/t_x509a.c
index ffbbfb51f4..8b18801a17 100644
--- a/src/lib/libcrypto/asn1/t_x509a.c
+++ b/src/lib/libcrypto/asn1/t_x509a.c
@@ -1,5 +1,5 @@
1/* t_x509a.c */ 1/* t_x509a.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index 0ee406231e..ced641698e 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -1,5 +1,5 @@
1/* tasn_dec.c */ 1/* tasn_dec.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/tasn_enc.c b/src/lib/libcrypto/asn1/tasn_enc.c
index be19b36acd..2721f904a6 100644
--- a/src/lib/libcrypto/asn1/tasn_enc.c
+++ b/src/lib/libcrypto/asn1/tasn_enc.c
@@ -1,5 +1,5 @@
1/* tasn_enc.c */ 1/* tasn_enc.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/tasn_fre.c b/src/lib/libcrypto/asn1/tasn_fre.c
index bb7c1e2af4..d7c017fa1d 100644
--- a/src/lib/libcrypto/asn1/tasn_fre.c
+++ b/src/lib/libcrypto/asn1/tasn_fre.c
@@ -1,5 +1,5 @@
1/* tasn_fre.c */ 1/* tasn_fre.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/tasn_new.c b/src/lib/libcrypto/asn1/tasn_new.c
index 531dad365c..5c6a2ebd4d 100644
--- a/src/lib/libcrypto/asn1/tasn_new.c
+++ b/src/lib/libcrypto/asn1/tasn_new.c
@@ -1,5 +1,5 @@
1/* tasn_new.c */ 1/* tasn_new.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/tasn_prn.c b/src/lib/libcrypto/asn1/tasn_prn.c
index 719639b511..b9c96a6dbe 100644
--- a/src/lib/libcrypto/asn1/tasn_prn.c
+++ b/src/lib/libcrypto/asn1/tasn_prn.c
@@ -1,5 +1,5 @@
1/* tasn_prn.c */ 1/* tasn_prn.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/tasn_typ.c b/src/lib/libcrypto/asn1/tasn_typ.c
index 6f17f1bec7..6252213d15 100644
--- a/src/lib/libcrypto/asn1/tasn_typ.c
+++ b/src/lib/libcrypto/asn1/tasn_typ.c
@@ -1,5 +1,5 @@
1/* tasn_typ.c */ 1/* tasn_typ.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/tasn_utl.c b/src/lib/libcrypto/asn1/tasn_utl.c
index 34d520b180..ca9ec7a32f 100644
--- a/src/lib/libcrypto/asn1/tasn_utl.c
+++ b/src/lib/libcrypto/asn1/tasn_utl.c
@@ -1,5 +1,5 @@
1/* tasn_utl.c */ 1/* tasn_utl.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/x_algor.c b/src/lib/libcrypto/asn1/x_algor.c
index 33533aba86..99e53429b7 100644
--- a/src/lib/libcrypto/asn1/x_algor.c
+++ b/src/lib/libcrypto/asn1/x_algor.c
@@ -1,5 +1,5 @@
1/* x_algor.c */ 1/* x_algor.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/x_bignum.c b/src/lib/libcrypto/asn1/x_bignum.c
index 869c05d931..9cf3204a1b 100644
--- a/src/lib/libcrypto/asn1/x_bignum.c
+++ b/src/lib/libcrypto/asn1/x_bignum.c
@@ -1,5 +1,5 @@
1/* x_bignum.c */ 1/* x_bignum.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/x_exten.c b/src/lib/libcrypto/asn1/x_exten.c
index 1732e66712..3a21239926 100644
--- a/src/lib/libcrypto/asn1/x_exten.c
+++ b/src/lib/libcrypto/asn1/x_exten.c
@@ -1,5 +1,5 @@
1/* x_exten.c */ 1/* x_exten.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/x_long.c b/src/lib/libcrypto/asn1/x_long.c
index 0db233cb95..bf35457c1f 100644
--- a/src/lib/libcrypto/asn1/x_long.c
+++ b/src/lib/libcrypto/asn1/x_long.c
@@ -1,5 +1,5 @@
1/* x_long.c */ 1/* x_long.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/asn1/x_x509a.c b/src/lib/libcrypto/asn1/x_x509a.c
index 13db5fd03f..b603f82de7 100644
--- a/src/lib/libcrypto/asn1/x_x509a.c
+++ b/src/lib/libcrypto/asn1/x_x509a.c
@@ -1,5 +1,5 @@
1/* a_x509a.c */ 1/* a_x509a.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/bf/bf_skey.c b/src/lib/libcrypto/bf/bf_skey.c
index 3673cdee6e..6ac2aeb279 100644
--- a/src/lib/libcrypto/bf/bf_skey.c
+++ b/src/lib/libcrypto/bf/bf_skey.c
@@ -59,10 +59,15 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <string.h> 60#include <string.h>
61#include <openssl/blowfish.h> 61#include <openssl/blowfish.h>
62#include <openssl/crypto.h>
63#ifdef OPENSSL_FIPS
64#include <openssl/fips.h>
65#endif
66
62#include "bf_locl.h" 67#include "bf_locl.h"
63#include "bf_pi.h" 68#include "bf_pi.h"
64 69
65void BF_set_key(BF_KEY *key, int len, const unsigned char *data) 70FIPS_NON_FIPS_VCIPHER_Init(BF)
66 { 71 {
67 int i; 72 int i;
68 BF_LONG *p,ri,in[2]; 73 BF_LONG *p,ri,in[2];
diff --git a/src/lib/libcrypto/bf/blowfish.h b/src/lib/libcrypto/bf/blowfish.h
index cd49e85ab2..d24ffccb65 100644
--- a/src/lib/libcrypto/bf/blowfish.h
+++ b/src/lib/libcrypto/bf/blowfish.h
@@ -104,7 +104,9 @@ typedef struct bf_key_st
104 BF_LONG S[4*256]; 104 BF_LONG S[4*256];
105 } BF_KEY; 105 } BF_KEY;
106 106
107 107#ifdef OPENSSL_FIPS
108void private_BF_set_key(BF_KEY *key, int len, const unsigned char *data);
109#endif
108void BF_set_key(BF_KEY *key, int len, const unsigned char *data); 110void BF_set_key(BF_KEY *key, int len, const unsigned char *data);
109 111
110void BF_encrypt(BF_LONG *data,const BF_KEY *key); 112void BF_encrypt(BF_LONG *data,const BF_KEY *key);
diff --git a/src/lib/libcrypto/bio/bss_bio.c b/src/lib/libcrypto/bio/bss_bio.c
index 0f9f0955b4..76bd48e767 100644
--- a/src/lib/libcrypto/bio/bss_bio.c
+++ b/src/lib/libcrypto/bio/bss_bio.c
@@ -919,6 +919,6 @@ int BIO_nwrite(BIO *bio, char **buf, int num)
919 919
920 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf); 920 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
921 if (ret > 0) 921 if (ret > 0)
922 bio->num_read += ret; 922 bio->num_write += ret;
923 return ret; 923 return ret;
924 } 924 }
diff --git a/src/lib/libcrypto/bio/bss_dgram.c b/src/lib/libcrypto/bio/bss_dgram.c
index ea2c3fff63..c3da6dc82f 100644
--- a/src/lib/libcrypto/bio/bss_dgram.c
+++ b/src/lib/libcrypto/bio/bss_dgram.c
@@ -82,7 +82,7 @@ static int dgram_new(BIO *h);
82static int dgram_free(BIO *data); 82static int dgram_free(BIO *data);
83static int dgram_clear(BIO *bio); 83static int dgram_clear(BIO *bio);
84 84
85int BIO_dgram_should_retry(int s); 85static int BIO_dgram_should_retry(int s);
86 86
87static BIO_METHOD methods_dgramp= 87static BIO_METHOD methods_dgramp=
88 { 88 {
@@ -345,30 +345,90 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
345 345
346 memcpy(&(data->peer), to, sizeof(struct sockaddr)); 346 memcpy(&(data->peer), to, sizeof(struct sockaddr));
347 break; 347 break;
348#if defined(SO_RCVTIMEO)
348 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT: 349 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
350#ifdef OPENSSL_SYS_WINDOWS
351 {
352 struct timeval *tv = (struct timeval *)ptr;
353 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
354 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
355 (void*)&timeout, sizeof(timeout)) < 0)
356 { perror("setsockopt"); ret = -1; }
357 }
358#else
349 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr, 359 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
350 sizeof(struct timeval)) < 0) 360 sizeof(struct timeval)) < 0)
351 { perror("setsockopt"); ret = -1; } 361 { perror("setsockopt"); ret = -1; }
362#endif
352 break; 363 break;
353 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT: 364 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
365#ifdef OPENSSL_SYS_WINDOWS
366 {
367 int timeout, sz = sizeof(timeout);
368 struct timeval *tv = (struct timeval *)ptr;
369 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
370 (void*)&timeout, &sz) < 0)
371 { perror("getsockopt"); ret = -1; }
372 else
373 {
374 tv->tv_sec = timeout / 1000;
375 tv->tv_usec = (timeout % 1000) * 1000;
376 ret = sizeof(*tv);
377 }
378 }
379#else
354 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 380 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
355 ptr, (void *)&ret) < 0) 381 ptr, (void *)&ret) < 0)
356 { perror("getsockopt"); ret = -1; } 382 { perror("getsockopt"); ret = -1; }
383#endif
357 break; 384 break;
385#endif
386#if defined(SO_SNDTIMEO)
358 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT: 387 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
388#ifdef OPENSSL_SYS_WINDOWS
389 {
390 struct timeval *tv = (struct timeval *)ptr;
391 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
392 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
393 (void*)&timeout, sizeof(timeout)) < 0)
394 { perror("setsockopt"); ret = -1; }
395 }
396#else
359 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr, 397 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
360 sizeof(struct timeval)) < 0) 398 sizeof(struct timeval)) < 0)
361 { perror("setsockopt"); ret = -1; } 399 { perror("setsockopt"); ret = -1; }
400#endif
362 break; 401 break;
363 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT: 402 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
403#ifdef OPENSSL_SYS_WINDOWS
404 {
405 int timeout, sz = sizeof(timeout);
406 struct timeval *tv = (struct timeval *)ptr;
407 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
408 (void*)&timeout, &sz) < 0)
409 { perror("getsockopt"); ret = -1; }
410 else
411 {
412 tv->tv_sec = timeout / 1000;
413 tv->tv_usec = (timeout % 1000) * 1000;
414 ret = sizeof(*tv);
415 }
416 }
417#else
364 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, 418 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
365 ptr, (void *)&ret) < 0) 419 ptr, (void *)&ret) < 0)
366 { perror("getsockopt"); ret = -1; } 420 { perror("getsockopt"); ret = -1; }
421#endif
367 break; 422 break;
423#endif
368 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP: 424 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
369 /* fall-through */ 425 /* fall-through */
370 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP: 426 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
427#ifdef OPENSSL_SYS_WINDOWS
428 if ( data->_errno == WSAETIMEDOUT)
429#else
371 if ( data->_errno == EAGAIN) 430 if ( data->_errno == EAGAIN)
431#endif
372 { 432 {
373 ret = 1; 433 ret = 1;
374 data->_errno = 0; 434 data->_errno = 0;
@@ -403,7 +463,7 @@ static int dgram_puts(BIO *bp, const char *str)
403 return(ret); 463 return(ret);
404 } 464 }
405 465
406int BIO_dgram_should_retry(int i) 466static int BIO_dgram_should_retry(int i)
407 { 467 {
408 int err; 468 int err;
409 469
diff --git a/src/lib/libcrypto/bio/bss_file.c b/src/lib/libcrypto/bio/bss_file.c
index 4df9927c43..9ad46fa081 100644
--- a/src/lib/libcrypto/bio/bss_file.c
+++ b/src/lib/libcrypto/bio/bss_file.c
@@ -279,7 +279,7 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
279#endif 279#endif
280 { 280 {
281#if defined(OPENSSL_SYS_WINDOWS) 281#if defined(OPENSSL_SYS_WINDOWS)
282 int fd = fileno((FILE*)ptr); 282 int fd = _fileno((FILE*)ptr);
283 if (num & BIO_FP_TEXT) 283 if (num & BIO_FP_TEXT)
284 _setmode(fd,_O_TEXT); 284 _setmode(fd,_O_TEXT);
285 else 285 else
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h
index 6d754d5547..f1719a5877 100644
--- a/src/lib/libcrypto/bn/bn.h
+++ b/src/lib/libcrypto/bn/bn.h
@@ -408,8 +408,8 @@ BIGNUM *BN_CTX_get(BN_CTX *ctx);
408void BN_CTX_end(BN_CTX *ctx); 408void BN_CTX_end(BN_CTX *ctx);
409int BN_rand(BIGNUM *rnd, int bits, int top,int bottom); 409int BN_rand(BIGNUM *rnd, int bits, int top,int bottom);
410int BN_pseudo_rand(BIGNUM *rnd, int bits, int top,int bottom); 410int BN_pseudo_rand(BIGNUM *rnd, int bits, int top,int bottom);
411int BN_rand_range(BIGNUM *rnd, BIGNUM *range); 411int BN_rand_range(BIGNUM *rnd, const BIGNUM *range);
412int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range); 412int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range);
413int BN_num_bits(const BIGNUM *a); 413int BN_num_bits(const BIGNUM *a);
414int BN_num_bits_word(BN_ULONG); 414int BN_num_bits_word(BN_ULONG);
415BIGNUM *BN_new(void); 415BIGNUM *BN_new(void);
@@ -531,6 +531,17 @@ int BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb);
531int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, 531int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx,
532 int do_trial_division, BN_GENCB *cb); 532 int do_trial_division, BN_GENCB *cb);
533 533
534int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx);
535
536int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
537 const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2,
538 const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb);
539int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
540 BIGNUM *Xp1, BIGNUM *Xp2,
541 const BIGNUM *Xp,
542 const BIGNUM *e, BN_CTX *ctx,
543 BN_GENCB *cb);
544
534BN_MONT_CTX *BN_MONT_CTX_new(void ); 545BN_MONT_CTX *BN_MONT_CTX_new(void );
535void BN_MONT_CTX_init(BN_MONT_CTX *ctx); 546void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
536int BN_mod_mul_montgomery(BIGNUM *r,const BIGNUM *a,const BIGNUM *b, 547int BN_mod_mul_montgomery(BIGNUM *r,const BIGNUM *a,const BIGNUM *b,
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index 8655eb118e..1e8e57626b 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -187,6 +187,17 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
187 BN_ULONG d0,d1; 187 BN_ULONG d0,d1;
188 int num_n,div_n; 188 int num_n,div_n;
189 189
190 /* Invalid zero-padding would have particularly bad consequences
191 * in the case of 'num', so don't just rely on bn_check_top() for this one
192 * (bn_check_top() works only for BN_DEBUG builds) */
193 if (num->top > 0 && num->d[num->top - 1] == 0)
194 {
195 BNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);
196 return 0;
197 }
198
199 bn_check_top(num);
200
190 if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) 201 if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))
191 { 202 {
192 return BN_div_no_branch(dv, rm, num, divisor, ctx); 203 return BN_div_no_branch(dv, rm, num, divisor, ctx);
@@ -194,7 +205,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
194 205
195 bn_check_top(dv); 206 bn_check_top(dv);
196 bn_check_top(rm); 207 bn_check_top(rm);
197 bn_check_top(num); 208 /* bn_check_top(num); */ /* 'num' has been checked already */
198 bn_check_top(divisor); 209 bn_check_top(divisor);
199 210
200 if (BN_is_zero(divisor)) 211 if (BN_is_zero(divisor))
@@ -419,7 +430,7 @@ static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,
419 430
420 bn_check_top(dv); 431 bn_check_top(dv);
421 bn_check_top(rm); 432 bn_check_top(rm);
422 bn_check_top(num); 433 /* bn_check_top(num); */ /* 'num' has been checked in BN_div() */
423 bn_check_top(divisor); 434 bn_check_top(divisor);
424 435
425 if (BN_is_zero(divisor)) 436 if (BN_is_zero(divisor))
diff --git a/src/lib/libcrypto/bn/bn_gf2m.c b/src/lib/libcrypto/bn/bn_gf2m.c
index 6a793857e1..306f029f27 100644
--- a/src/lib/libcrypto/bn/bn_gf2m.c
+++ b/src/lib/libcrypto/bn/bn_gf2m.c
@@ -384,7 +384,11 @@ int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[])
384 if (zz == 0) break; 384 if (zz == 0) break;
385 d1 = BN_BITS2 - d0; 385 d1 = BN_BITS2 - d0;
386 386
387 if (d0) z[dN] = (z[dN] << d1) >> d1; /* clear up the top d1 bits */ 387 /* clear up the top d1 bits */
388 if (d0)
389 z[dN] = (z[dN] << d1) >> d1;
390 else
391 z[dN] = 0;
388 z[0] ^= zz; /* reduction t^0 component */ 392 z[0] ^= zz; /* reduction t^0 component */
389 393
390 for (k = 1; p[k] != 0; k++) 394 for (k = 1; p[k] != 0; k++)
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index 2649b8c538..32a8fbaf51 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -139,25 +139,6 @@ const BIGNUM *BN_value_one(void)
139 return(&const_one); 139 return(&const_one);
140 } 140 }
141 141
142char *BN_options(void)
143 {
144 static int init=0;
145 static char data[16];
146
147 if (!init)
148 {
149 init++;
150#ifdef BN_LLONG
151 BIO_snprintf(data,sizeof data,"bn(%d,%d)",
152 (int)sizeof(BN_ULLONG)*8,(int)sizeof(BN_ULONG)*8);
153#else
154 BIO_snprintf(data,sizeof data,"bn(%d,%d)",
155 (int)sizeof(BN_ULONG)*8,(int)sizeof(BN_ULONG)*8);
156#endif
157 }
158 return(data);
159 }
160
161int BN_num_bits_word(BN_ULONG l) 142int BN_num_bits_word(BN_ULONG l)
162 { 143 {
163 static const char bits[256]={ 144 static const char bits[256]={
diff --git a/src/lib/libcrypto/bn/bn_nist.c b/src/lib/libcrypto/bn/bn_nist.c
index e14232fdbb..2ca5b01391 100644
--- a/src/lib/libcrypto/bn/bn_nist.c
+++ b/src/lib/libcrypto/bn/bn_nist.c
@@ -59,109 +59,266 @@
59#include "bn_lcl.h" 59#include "bn_lcl.h"
60#include "cryptlib.h" 60#include "cryptlib.h"
61 61
62
62#define BN_NIST_192_TOP (192+BN_BITS2-1)/BN_BITS2 63#define BN_NIST_192_TOP (192+BN_BITS2-1)/BN_BITS2
63#define BN_NIST_224_TOP (224+BN_BITS2-1)/BN_BITS2 64#define BN_NIST_224_TOP (224+BN_BITS2-1)/BN_BITS2
64#define BN_NIST_256_TOP (256+BN_BITS2-1)/BN_BITS2 65#define BN_NIST_256_TOP (256+BN_BITS2-1)/BN_BITS2
65#define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2 66#define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2
66#define BN_NIST_521_TOP (521+BN_BITS2-1)/BN_BITS2 67#define BN_NIST_521_TOP (521+BN_BITS2-1)/BN_BITS2
67 68
69/* pre-computed tables are "carry-less" values of modulus*(i+1) */
68#if BN_BITS2 == 64 70#if BN_BITS2 == 64
69static const BN_ULONG _nist_p_192[] = 71static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
70 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFEULL, 72 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFFULL},
71 0xFFFFFFFFFFFFFFFFULL}; 73 {0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFDULL,0xFFFFFFFFFFFFFFFFULL},
72static const BN_ULONG _nist_p_224[] = 74 {0xFFFFFFFFFFFFFFFDULL,0xFFFFFFFFFFFFFFFCULL,0xFFFFFFFFFFFFFFFFULL}
75 };
76static const BN_ULONG _nist_p_192_sqr[] = {
77 0x0000000000000001ULL,0x0000000000000002ULL,0x0000000000000001ULL,
78 0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFDULL,0xFFFFFFFFFFFFFFFFULL
79 };
80static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
73 {0x0000000000000001ULL,0xFFFFFFFF00000000ULL, 81 {0x0000000000000001ULL,0xFFFFFFFF00000000ULL,
74 0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL}; 82 0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL},
75static const BN_ULONG _nist_p_256[] = 83 {0x0000000000000002ULL,0xFFFFFFFE00000000ULL,
84 0xFFFFFFFFFFFFFFFFULL,0x00000001FFFFFFFFULL} /* this one is "carry-full" */
85 };
86static const BN_ULONG _nist_p_224_sqr[] = {
87 0x0000000000000001ULL,0xFFFFFFFE00000000ULL,
88 0xFFFFFFFFFFFFFFFFULL,0x0000000200000000ULL,
89 0x0000000000000000ULL,0xFFFFFFFFFFFFFFFEULL,
90 0xFFFFFFFFFFFFFFFFULL
91 };
92static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
76 {0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL, 93 {0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL,
77 0x0000000000000000ULL,0xFFFFFFFF00000001ULL}; 94 0x0000000000000000ULL,0xFFFFFFFF00000001ULL},
78static const BN_ULONG _nist_p_384[] = 95 {0xFFFFFFFFFFFFFFFEULL,0x00000001FFFFFFFFULL,
79 {0x00000000FFFFFFFFULL,0xFFFFFFFF00000000ULL, 96 0x0000000000000000ULL,0xFFFFFFFE00000002ULL},
80 0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFFULL, 97 {0xFFFFFFFFFFFFFFFDULL,0x00000002FFFFFFFFULL,
81 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL}; 98 0x0000000000000000ULL,0xFFFFFFFD00000003ULL},
99 {0xFFFFFFFFFFFFFFFCULL,0x00000003FFFFFFFFULL,
100 0x0000000000000000ULL,0xFFFFFFFC00000004ULL},
101 {0xFFFFFFFFFFFFFFFBULL,0x00000004FFFFFFFFULL,
102 0x0000000000000000ULL,0xFFFFFFFB00000005ULL},
103 };
104static const BN_ULONG _nist_p_256_sqr[] = {
105 0x0000000000000001ULL,0xFFFFFFFE00000000ULL,
106 0xFFFFFFFFFFFFFFFFULL,0x00000001FFFFFFFEULL,
107 0x00000001FFFFFFFEULL,0x00000001FFFFFFFEULL,
108 0xFFFFFFFE00000001ULL,0xFFFFFFFE00000002ULL
109 };
110static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
111 {0x00000000FFFFFFFFULL,0xFFFFFFFF00000000ULL,0xFFFFFFFFFFFFFFFEULL,
112 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
113 {0x00000001FFFFFFFEULL,0xFFFFFFFE00000000ULL,0xFFFFFFFFFFFFFFFDULL,
114 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
115 {0x00000002FFFFFFFDULL,0xFFFFFFFD00000000ULL,0xFFFFFFFFFFFFFFFCULL,
116 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
117 {0x00000003FFFFFFFCULL,0xFFFFFFFC00000000ULL,0xFFFFFFFFFFFFFFFBULL,
118 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
119 {0x00000004FFFFFFFBULL,0xFFFFFFFB00000000ULL,0xFFFFFFFFFFFFFFFAULL,
120 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
121 };
122static const BN_ULONG _nist_p_384_sqr[] = {
123 0xFFFFFFFE00000001ULL,0x0000000200000000ULL,0xFFFFFFFE00000000ULL,
124 0x0000000200000000ULL,0x0000000000000001ULL,0x0000000000000000ULL,
125 0x00000001FFFFFFFEULL,0xFFFFFFFE00000000ULL,0xFFFFFFFFFFFFFFFDULL,
126 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL
127 };
82static const BN_ULONG _nist_p_521[] = 128static const BN_ULONG _nist_p_521[] =
83 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL, 129 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
84 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL, 130 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
85 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL, 131 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
86 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL, 132 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
87 0x00000000000001FFULL}; 133 0x00000000000001FFULL};
134static const BN_ULONG _nist_p_521_sqr[] = {
135 0x0000000000000001ULL,0x0000000000000000ULL,0x0000000000000000ULL,
136 0x0000000000000000ULL,0x0000000000000000ULL,0x0000000000000000ULL,
137 0x0000000000000000ULL,0x0000000000000000ULL,0xFFFFFFFFFFFFFC00ULL,
138 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
139 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
140 0xFFFFFFFFFFFFFFFFULL,0x000000000003FFFFULL
141 };
88#elif BN_BITS2 == 32 142#elif BN_BITS2 == 32
89static const BN_ULONG _nist_p_192[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE, 143static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
90 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}; 144 {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
91static const BN_ULONG _nist_p_224[] = {0x00000001,0x00000000,0x00000000, 145 {0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
92 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}; 146 {0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFC,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}
93static const BN_ULONG _nist_p_256[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF, 147 };
94 0x00000000,0x00000000,0x00000000,0x00000001,0xFFFFFFFF}; 148static const BN_ULONG _nist_p_192_sqr[] = {
95static const BN_ULONG _nist_p_384[] = {0xFFFFFFFF,0x00000000,0x00000000, 149 0x00000001,0x00000000,0x00000002,0x00000000,0x00000001,0x00000000,
96 0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF, 150 0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF
97 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}; 151 };
152static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
153 {0x00000001,0x00000000,0x00000000,0xFFFFFFFF,
154 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
155 {0x00000002,0x00000000,0x00000000,0xFFFFFFFE,
156 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}
157 };
158static const BN_ULONG _nist_p_224_sqr[] = {
159 0x00000001,0x00000000,0x00000000,0xFFFFFFFE,
160 0xFFFFFFFF,0xFFFFFFFF,0x00000000,0x00000002,
161 0x00000000,0x00000000,0xFFFFFFFE,0xFFFFFFFF,
162 0xFFFFFFFF,0xFFFFFFFF
163 };
164static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
165 {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0x00000000,
166 0x00000000,0x00000000,0x00000001,0xFFFFFFFF},
167 {0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0x00000001,
168 0x00000000,0x00000000,0x00000002,0xFFFFFFFE},
169 {0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFF,0x00000002,
170 0x00000000,0x00000000,0x00000003,0xFFFFFFFD},
171 {0xFFFFFFFC,0xFFFFFFFF,0xFFFFFFFF,0x00000003,
172 0x00000000,0x00000000,0x00000004,0xFFFFFFFC},
173 {0xFFFFFFFB,0xFFFFFFFF,0xFFFFFFFF,0x00000004,
174 0x00000000,0x00000000,0x00000005,0xFFFFFFFB},
175 };
176static const BN_ULONG _nist_p_256_sqr[] = {
177 0x00000001,0x00000000,0x00000000,0xFFFFFFFE,
178 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE,0x00000001,
179 0xFFFFFFFE,0x00000001,0xFFFFFFFE,0x00000001,
180 0x00000001,0xFFFFFFFE,0x00000002,0xFFFFFFFE
181 };
182static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
183 {0xFFFFFFFF,0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,
184 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
185 {0xFFFFFFFE,0x00000001,0x00000000,0xFFFFFFFE,0xFFFFFFFD,0xFFFFFFFF,
186 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
187 {0xFFFFFFFD,0x00000002,0x00000000,0xFFFFFFFD,0xFFFFFFFC,0xFFFFFFFF,
188 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
189 {0xFFFFFFFC,0x00000003,0x00000000,0xFFFFFFFC,0xFFFFFFFB,0xFFFFFFFF,
190 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
191 {0xFFFFFFFB,0x00000004,0x00000000,0xFFFFFFFB,0xFFFFFFFA,0xFFFFFFFF,
192 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
193 };
194static const BN_ULONG _nist_p_384_sqr[] = {
195 0x00000001,0xFFFFFFFE,0x00000000,0x00000002,0x00000000,0xFFFFFFFE,
196 0x00000000,0x00000002,0x00000001,0x00000000,0x00000000,0x00000000,
197 0xFFFFFFFE,0x00000001,0x00000000,0xFFFFFFFE,0xFFFFFFFD,0xFFFFFFFF,
198 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF
199 };
98static const BN_ULONG _nist_p_521[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF, 200static const BN_ULONG _nist_p_521[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
99 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF, 201 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
100 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF, 202 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
101 0xFFFFFFFF,0x000001FF}; 203 0xFFFFFFFF,0x000001FF};
204static const BN_ULONG _nist_p_521_sqr[] = {
205 0x00000001,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
206 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
207 0x00000000,0x00000000,0x00000000,0x00000000,0xFFFFFC00,0xFFFFFFFF,
208 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
209 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
210 0xFFFFFFFF,0xFFFFFFFF,0x0003FFFF
211 };
212#else
213#error "unsupported BN_BITS2"
102#endif 214#endif
103 215
216
217static const BIGNUM _bignum_nist_p_192 =
218 {
219 (BN_ULONG *)_nist_p_192[0],
220 BN_NIST_192_TOP,
221 BN_NIST_192_TOP,
222 0,
223 BN_FLG_STATIC_DATA
224 };
225
226static const BIGNUM _bignum_nist_p_224 =
227 {
228 (BN_ULONG *)_nist_p_224[0],
229 BN_NIST_224_TOP,
230 BN_NIST_224_TOP,
231 0,
232 BN_FLG_STATIC_DATA
233 };
234
235static const BIGNUM _bignum_nist_p_256 =
236 {
237 (BN_ULONG *)_nist_p_256[0],
238 BN_NIST_256_TOP,
239 BN_NIST_256_TOP,
240 0,
241 BN_FLG_STATIC_DATA
242 };
243
244static const BIGNUM _bignum_nist_p_384 =
245 {
246 (BN_ULONG *)_nist_p_384[0],
247 BN_NIST_384_TOP,
248 BN_NIST_384_TOP,
249 0,
250 BN_FLG_STATIC_DATA
251 };
252
253static const BIGNUM _bignum_nist_p_521 =
254 {
255 (BN_ULONG *)_nist_p_521,
256 BN_NIST_521_TOP,
257 BN_NIST_521_TOP,
258 0,
259 BN_FLG_STATIC_DATA
260 };
261
262
104const BIGNUM *BN_get0_nist_prime_192(void) 263const BIGNUM *BN_get0_nist_prime_192(void)
105 { 264 {
106 static BIGNUM const_nist_192 = { (BN_ULONG *)_nist_p_192, 265 return &_bignum_nist_p_192;
107 BN_NIST_192_TOP, BN_NIST_192_TOP, 0, BN_FLG_STATIC_DATA };
108 return &const_nist_192;
109 } 266 }
110 267
111const BIGNUM *BN_get0_nist_prime_224(void) 268const BIGNUM *BN_get0_nist_prime_224(void)
112 { 269 {
113 static BIGNUM const_nist_224 = { (BN_ULONG *)_nist_p_224, 270 return &_bignum_nist_p_224;
114 BN_NIST_224_TOP, BN_NIST_224_TOP, 0, BN_FLG_STATIC_DATA };
115 return &const_nist_224;
116 } 271 }
117 272
118const BIGNUM *BN_get0_nist_prime_256(void) 273const BIGNUM *BN_get0_nist_prime_256(void)
119 { 274 {
120 static BIGNUM const_nist_256 = { (BN_ULONG *)_nist_p_256, 275 return &_bignum_nist_p_256;
121 BN_NIST_256_TOP, BN_NIST_256_TOP, 0, BN_FLG_STATIC_DATA };
122 return &const_nist_256;
123 } 276 }
124 277
125const BIGNUM *BN_get0_nist_prime_384(void) 278const BIGNUM *BN_get0_nist_prime_384(void)
126 { 279 {
127 static BIGNUM const_nist_384 = { (BN_ULONG *)_nist_p_384, 280 return &_bignum_nist_p_384;
128 BN_NIST_384_TOP, BN_NIST_384_TOP, 0, BN_FLG_STATIC_DATA };
129 return &const_nist_384;
130 } 281 }
131 282
132const BIGNUM *BN_get0_nist_prime_521(void) 283const BIGNUM *BN_get0_nist_prime_521(void)
133 { 284 {
134 static BIGNUM const_nist_521 = { (BN_ULONG *)_nist_p_521, 285 return &_bignum_nist_p_521;
135 BN_NIST_521_TOP, BN_NIST_521_TOP, 0, BN_FLG_STATIC_DATA };
136 return &const_nist_521;
137 } 286 }
138 287
139#define BN_NIST_ADD_ONE(a) while (!(*(a)=(*(a)+1)&BN_MASK2)) ++(a);
140 288
141static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max) 289static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max)
142 { 290 {
143 int i; 291 int i;
144 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a); 292 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
145 for (i = (top); i != 0; i--) 293
146 *_tmp1++ = *_tmp2++; 294#ifdef BN_DEBUG
147 for (i = (max) - (top); i != 0; i--) 295 OPENSSL_assert(top <= max);
148 *_tmp1++ = (BN_ULONG) 0; 296#endif
149 } 297 for (i = (top); i != 0; i--)
298 *_tmp1++ = *_tmp2++;
299 for (i = (max) - (top); i != 0; i--)
300 *_tmp1++ = (BN_ULONG) 0;
301 }
150 302
151static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top) 303static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top)
152 { 304 {
153 int i; 305 int i;
154 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a); 306 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
155 for (i = (top); i != 0; i--) 307 for (i = (top); i != 0; i--)
156 *_tmp1++ = *_tmp2++; 308 *_tmp1++ = *_tmp2++;
157 } 309 }
158 310
159#if BN_BITS2 == 64 311#if BN_BITS2 == 64
160#define bn_cp_64(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0; 312#define bn_cp_64(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
161#define bn_64_set_0(to, n) (to)[n] = (BN_ULONG)0; 313#define bn_64_set_0(to, n) (to)[n] = (BN_ULONG)0;
162/* TBD */ 314/*
163#define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0; 315 * two following macros are implemented under assumption that they
164#define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0; 316 * are called in a sequence with *ascending* n, i.e. as they are...
317 */
318#define bn_cp_32_naked(to, n, from, m) (((n)&1)?(to[(n)/2]|=((m)&1)?(from[(m)/2]&BN_MASK2h):(from[(m)/2]<<32))\
319 :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l)))
320#define bn_32_set_0(to, n) (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0));
321#define bn_cp_32(to,n,from,m) ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n)
165#else 322#else
166#define bn_cp_64(to, n, from, m) \ 323#define bn_cp_64(to, n, from, m) \
167 { \ 324 { \
@@ -182,9 +339,9 @@ static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top)
182 339
183#define nist_set_192(to, from, a1, a2, a3) \ 340#define nist_set_192(to, from, a1, a2, a3) \
184 { \ 341 { \
185 if (a3 != 0) bn_cp_64(to, 0, from, (a3) - 3) else bn_64_set_0(to, 0)\ 342 bn_cp_64(to, 0, from, (a3) - 3) \
186 bn_cp_64(to, 1, from, (a2) - 3) \ 343 bn_cp_64(to, 1, from, (a2) - 3) \
187 if (a1 != 0) bn_cp_64(to, 2, from, (a1) - 3) else bn_64_set_0(to, 2)\ 344 bn_cp_64(to, 2, from, (a1) - 3) \
188 } 345 }
189 346
190int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, 347int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
@@ -198,6 +355,16 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
198 c_d[BN_NIST_192_TOP], 355 c_d[BN_NIST_192_TOP],
199 *res; 356 *res;
200 size_t mask; 357 size_t mask;
358 static const BIGNUM _bignum_nist_p_192_sqr = {
359 (BN_ULONG *)_nist_p_192_sqr,
360 sizeof(_nist_p_192_sqr)/sizeof(_nist_p_192_sqr[0]),
361 sizeof(_nist_p_192_sqr)/sizeof(_nist_p_192_sqr[0]),
362 0,BN_FLG_STATIC_DATA };
363
364 field = &_bignum_nist_p_192; /* just to make sure */
365
366 if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_192_sqr)>=0)
367 return BN_nnmod(r, a, field, ctx);
201 368
202 i = BN_ucmp(field, a); 369 i = BN_ucmp(field, a);
203 if (i == 0) 370 if (i == 0)
@@ -208,9 +375,6 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
208 else if (i > 0) 375 else if (i > 0)
209 return (r == a) ? 1 : (BN_copy(r ,a) != NULL); 376 return (r == a) ? 1 : (BN_copy(r ,a) != NULL);
210 377
211 if (top == BN_NIST_192_TOP)
212 return BN_usub(r, a, field);
213
214 if (r != a) 378 if (r != a)
215 { 379 {
216 if (!bn_wexpand(r, BN_NIST_192_TOP)) 380 if (!bn_wexpand(r, BN_NIST_192_TOP))
@@ -224,23 +388,26 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
224 nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP); 388 nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP);
225 389
226 nist_set_192(t_d, buf, 0, 3, 3); 390 nist_set_192(t_d, buf, 0, 3, 3);
227 carry = bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP); 391 carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
228 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_192,BN_NIST_192_TOP);
229 mask = ~mask | (0-(size_t)carry);
230 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
231
232 nist_set_192(t_d, buf, 4, 4, 0); 392 nist_set_192(t_d, buf, 4, 4, 0);
233 carry = bn_add_words(r_d, res, t_d, BN_NIST_192_TOP); 393 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
234 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_192,BN_NIST_192_TOP);
235 mask = ~mask | (0-(size_t)carry);
236 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
237
238 nist_set_192(t_d, buf, 5, 5, 5) 394 nist_set_192(t_d, buf, 5, 5, 5)
239 carry = bn_add_words(r_d, res, t_d, BN_NIST_192_TOP); 395 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
240 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_192,BN_NIST_192_TOP);
241 mask = ~mask | (0-(size_t)carry);
242 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
243 396
397 if (carry > 0)
398 carry = (int)bn_sub_words(r_d,r_d,_nist_p_192[carry-1],BN_NIST_192_TOP);
399 else
400 carry = 1;
401
402 /*
403 * we need 'if (carry==0 || result>=modulus) result-=modulus;'
404 * as comparison implies subtraction, we can write
405 * 'tmp=result-modulus; if (!carry || !borrow) result=tmp;'
406 * this is what happens below, but without explicit if:-) a.
407 */
408 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_192[0],BN_NIST_192_TOP);
409 mask &= 0-(size_t)carry;
410 res = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));
244 nist_cp_bn(r_d, res, BN_NIST_192_TOP); 411 nist_cp_bn(r_d, res, BN_NIST_192_TOP);
245 r->top = BN_NIST_192_TOP; 412 r->top = BN_NIST_192_TOP;
246 bn_correct_top(r); 413 bn_correct_top(r);
@@ -248,21 +415,22 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
248 return 1; 415 return 1;
249 } 416 }
250 417
418typedef BN_ULONG (*bn_addsub_f)(BN_ULONG *,const BN_ULONG *,const BN_ULONG *,int);
419
251#define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \ 420#define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
252 { \ 421 { \
253 if (a7 != 0) bn_cp_32(to, 0, from, (a7) - 7) else bn_32_set_0(to, 0)\ 422 bn_cp_32(to, 0, from, (a7) - 7) \
254 if (a6 != 0) bn_cp_32(to, 1, from, (a6) - 7) else bn_32_set_0(to, 1)\ 423 bn_cp_32(to, 1, from, (a6) - 7) \
255 if (a5 != 0) bn_cp_32(to, 2, from, (a5) - 7) else bn_32_set_0(to, 2)\ 424 bn_cp_32(to, 2, from, (a5) - 7) \
256 if (a4 != 0) bn_cp_32(to, 3, from, (a4) - 7) else bn_32_set_0(to, 3)\ 425 bn_cp_32(to, 3, from, (a4) - 7) \
257 if (a3 != 0) bn_cp_32(to, 4, from, (a3) - 7) else bn_32_set_0(to, 4)\ 426 bn_cp_32(to, 4, from, (a3) - 7) \
258 if (a2 != 0) bn_cp_32(to, 5, from, (a2) - 7) else bn_32_set_0(to, 5)\ 427 bn_cp_32(to, 5, from, (a2) - 7) \
259 if (a1 != 0) bn_cp_32(to, 6, from, (a1) - 7) else bn_32_set_0(to, 6)\ 428 bn_cp_32(to, 6, from, (a1) - 7) \
260 } 429 }
261 430
262int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, 431int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
263 BN_CTX *ctx) 432 BN_CTX *ctx)
264 { 433 {
265#if BN_BITS2 == 32
266 int top = a->top, i; 434 int top = a->top, i;
267 int carry; 435 int carry;
268 BN_ULONG *r_d, *a_d = a->d; 436 BN_ULONG *r_d, *a_d = a->d;
@@ -271,6 +439,18 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
271 c_d[BN_NIST_224_TOP], 439 c_d[BN_NIST_224_TOP],
272 *res; 440 *res;
273 size_t mask; 441 size_t mask;
442 union { bn_addsub_f f; size_t p; } u;
443 static const BIGNUM _bignum_nist_p_224_sqr = {
444 (BN_ULONG *)_nist_p_224_sqr,
445 sizeof(_nist_p_224_sqr)/sizeof(_nist_p_224_sqr[0]),
446 sizeof(_nist_p_224_sqr)/sizeof(_nist_p_224_sqr[0]),
447 0,BN_FLG_STATIC_DATA };
448
449
450 field = &_bignum_nist_p_224; /* just to make sure */
451
452 if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_224_sqr)>=0)
453 return BN_nnmod(r, a, field, ctx);
274 454
275 i = BN_ucmp(field, a); 455 i = BN_ucmp(field, a);
276 if (i == 0) 456 if (i == 0)
@@ -281,9 +461,6 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
281 else if (i > 0) 461 else if (i > 0)
282 return (r == a)? 1 : (BN_copy(r ,a) != NULL); 462 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
283 463
284 if (top == BN_NIST_224_TOP)
285 return BN_usub(r, a, field);
286
287 if (r != a) 464 if (r != a)
288 { 465 {
289 if (!bn_wexpand(r, BN_NIST_224_TOP)) 466 if (!bn_wexpand(r, BN_NIST_224_TOP))
@@ -294,67 +471,77 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
294 else 471 else
295 r_d = a_d; 472 r_d = a_d;
296 473
474#if BN_BITS2==64
475 /* copy upper 256 bits of 448 bit number ... */
476 nist_cp_bn_0(t_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP);
477 /* ... and right shift by 32 to obtain upper 224 bits */
478 nist_set_224(buf, t_d, 14, 13, 12, 11, 10, 9, 8);
479 /* truncate lower part to 224 bits too */
480 r_d[BN_NIST_224_TOP-1] &= BN_MASK2l;
481#else
297 nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP); 482 nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
298 483#endif
299 nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0); 484 nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0);
300 carry = bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP); 485 carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
301 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_224,BN_NIST_224_TOP);
302 mask = ~mask | (0-(size_t)carry);
303 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
304
305 nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0); 486 nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0);
306 carry = bn_add_words(r_d, res, t_d, BN_NIST_224_TOP); 487 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
307 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_224,BN_NIST_224_TOP);
308 mask = ~mask | (0-(size_t)carry);
309 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
310
311 nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7); 488 nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);
312#if BRANCH_FREE 489 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
313 carry = bn_sub_words(r_d, res, t_d, BN_NIST_224_TOP);
314 bn_add_words(c_d,r_d,_nist_p_224,BN_NIST_224_TOP);
315 mask = 0-(size_t)carry;
316 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
317#else
318 if (bn_sub_words(r_d, res, t_d, BN_NIST_224_TOP))
319 bn_add_words(r_d,r_d,_nist_p_224,BN_NIST_224_TOP);
320#endif
321 nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11); 490 nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);
322#if BRANCH_FREE 491 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
323 carry = bn_sub_words(r_d, res, t_d, BN_NIST_224_TOP);
324 bn_add_words(c_d,r_d,_nist_p_224,BN_NIST_224_TOP);
325 mask = 0-(size_t)carry;
326 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
327 492
328 nist_cp_bn(r_d, res, BN_NIST_224_TOP); 493#if BN_BITS2==64
329#else 494 carry = (int)(r_d[BN_NIST_224_TOP-1]>>32);
330 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP)) 495#endif
331 bn_add_words(r_d,r_d,_nist_p_224,BN_NIST_224_TOP); 496 u.f = bn_sub_words;
497 if (carry > 0)
498 {
499 carry = (int)bn_sub_words(r_d,r_d,_nist_p_224[carry-1],BN_NIST_224_TOP);
500#if BN_BITS2==64
501 carry=(int)(~(r_d[BN_NIST_224_TOP-1]>>32))&1;
332#endif 502#endif
503 }
504 else if (carry < 0)
505 {
506 /* it's a bit more comlicated logic in this case.
507 * if bn_add_words yields no carry, then result
508 * has to be adjusted by unconditionally *adding*
509 * the modulus. but if it does, then result has
510 * to be compared to the modulus and conditionally
511 * adjusted by *subtracting* the latter. */
512 carry = (int)bn_add_words(r_d,r_d,_nist_p_224[-carry-1],BN_NIST_224_TOP);
513 mask = 0-(size_t)carry;
514 u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
515 }
516 else
517 carry = 1;
518
519 /* otherwise it's effectively same as in BN_nist_mod_192... */
520 mask = 0-(size_t)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP);
521 mask &= 0-(size_t)carry;
522 res = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));
523 nist_cp_bn(r_d, res, BN_NIST_224_TOP);
333 r->top = BN_NIST_224_TOP; 524 r->top = BN_NIST_224_TOP;
334 bn_correct_top(r); 525 bn_correct_top(r);
335 526
336 return 1; 527 return 1;
337#else /* BN_BITS!=32 */
338 return 0;
339#endif
340 } 528 }
341 529
342#define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \ 530#define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
343 { \ 531 { \
344 if (a8 != 0) bn_cp_32(to, 0, from, (a8) - 8) else bn_32_set_0(to, 0)\ 532 bn_cp_32(to, 0, from, (a8) - 8) \
345 if (a7 != 0) bn_cp_32(to, 1, from, (a7) - 8) else bn_32_set_0(to, 1)\ 533 bn_cp_32(to, 1, from, (a7) - 8) \
346 if (a6 != 0) bn_cp_32(to, 2, from, (a6) - 8) else bn_32_set_0(to, 2)\ 534 bn_cp_32(to, 2, from, (a6) - 8) \
347 if (a5 != 0) bn_cp_32(to, 3, from, (a5) - 8) else bn_32_set_0(to, 3)\ 535 bn_cp_32(to, 3, from, (a5) - 8) \
348 if (a4 != 0) bn_cp_32(to, 4, from, (a4) - 8) else bn_32_set_0(to, 4)\ 536 bn_cp_32(to, 4, from, (a4) - 8) \
349 if (a3 != 0) bn_cp_32(to, 5, from, (a3) - 8) else bn_32_set_0(to, 5)\ 537 bn_cp_32(to, 5, from, (a3) - 8) \
350 if (a2 != 0) bn_cp_32(to, 6, from, (a2) - 8) else bn_32_set_0(to, 6)\ 538 bn_cp_32(to, 6, from, (a2) - 8) \
351 if (a1 != 0) bn_cp_32(to, 7, from, (a1) - 8) else bn_32_set_0(to, 7)\ 539 bn_cp_32(to, 7, from, (a1) - 8) \
352 } 540 }
353 541
354int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, 542int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
355 BN_CTX *ctx) 543 BN_CTX *ctx)
356 { 544 {
357#if BN_BITS2 == 32
358 int i, top = a->top; 545 int i, top = a->top;
359 int carry = 0; 546 int carry = 0;
360 register BN_ULONG *a_d = a->d, *r_d; 547 register BN_ULONG *a_d = a->d, *r_d;
@@ -363,6 +550,17 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
363 c_d[BN_NIST_256_TOP], 550 c_d[BN_NIST_256_TOP],
364 *res; 551 *res;
365 size_t mask; 552 size_t mask;
553 union { bn_addsub_f f; size_t p; } u;
554 static const BIGNUM _bignum_nist_p_256_sqr = {
555 (BN_ULONG *)_nist_p_256_sqr,
556 sizeof(_nist_p_256_sqr)/sizeof(_nist_p_256_sqr[0]),
557 sizeof(_nist_p_256_sqr)/sizeof(_nist_p_256_sqr[0]),
558 0,BN_FLG_STATIC_DATA };
559
560 field = &_bignum_nist_p_256; /* just to make sure */
561
562 if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_256_sqr)>=0)
563 return BN_nnmod(r, a, field, ctx);
366 564
367 i = BN_ucmp(field, a); 565 i = BN_ucmp(field, a);
368 if (i == 0) 566 if (i == 0)
@@ -373,9 +571,6 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
373 else if (i > 0) 571 else if (i > 0)
374 return (r == a)? 1 : (BN_copy(r ,a) != NULL); 572 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
375 573
376 if (top == BN_NIST_256_TOP)
377 return BN_usub(r, a, field);
378
379 if (r != a) 574 if (r != a)
380 { 575 {
381 if (!bn_wexpand(r, BN_NIST_256_TOP)) 576 if (!bn_wexpand(r, BN_NIST_256_TOP))
@@ -391,111 +586,84 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
391 /*S1*/ 586 /*S1*/
392 nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0); 587 nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0);
393 /*S2*/ 588 /*S2*/
394 nist_set_256(c_d,buf, 0, 15, 14, 13, 12, 0, 0, 0); 589 nist_set_256(c_d, buf, 0, 15, 14, 13, 12, 0, 0, 0);
395 carry = bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP); 590 carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
396 mask = 0-(size_t)bn_sub_words(c_d,t_d,_nist_p_256,BN_NIST_256_TOP); 591 /* left shift */
397 mask = ~mask | (0-(size_t)carry); 592 {
398 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)t_d&~mask)); 593 register BN_ULONG *ap,t,c;
399 594 ap = t_d;
400 carry = bn_add_words(t_d, res, res, BN_NIST_256_TOP); 595 c=0;
401 mask = 0-(size_t)bn_sub_words(c_d,t_d,_nist_p_256,BN_NIST_256_TOP); 596 for (i = BN_NIST_256_TOP; i != 0; --i)
402 mask = ~mask | (0-(size_t)carry); 597 {
403 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)t_d&~mask)); 598 t= *ap;
404 599 *(ap++)=((t<<1)|c)&BN_MASK2;
405 carry = bn_add_words(r_d, r_d, res, BN_NIST_256_TOP); 600 c=(t & BN_TBIT)?1:0;
406 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP); 601 }
407 mask = ~mask | (0-(size_t)carry); 602 carry <<= 1;
408 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask)); 603 carry |= c;
409 604 }
605 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
410 /*S3*/ 606 /*S3*/
411 nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8); 607 nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8);
412 carry = bn_add_words(r_d, res, t_d, BN_NIST_256_TOP); 608 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
413 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
414 mask = ~mask | (0-(size_t)carry);
415 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
416
417 /*S4*/ 609 /*S4*/
418 nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9); 610 nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9);
419 carry = bn_add_words(r_d, res, t_d, BN_NIST_256_TOP); 611 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
420 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
421 mask = ~mask | (0-(size_t)carry);
422 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
423
424 /*D1*/ 612 /*D1*/
425 nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11); 613 nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11);
426#if BRANCH_FREE 614 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
427 carry = bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP);
428 bn_add_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
429 mask = 0-(size_t)carry;
430 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
431#else
432 if (bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP))
433 bn_add_words(r_d,r_d,_nist_p_256,BN_NIST_256_TOP);
434#endif
435 /*D2*/ 615 /*D2*/
436 nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12); 616 nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12);
437#if BRANCH_FREE 617 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
438 carry = bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP);
439 bn_add_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
440 mask = 0-(size_t)carry;
441 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
442#else
443 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
444 bn_add_words(r_d,r_d,_nist_p_256,BN_NIST_256_TOP);
445#endif
446 /*D3*/ 618 /*D3*/
447 nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13); 619 nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13);
448#if BRANCH_FREE 620 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
449 carry = bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP);
450 bn_add_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
451 mask = 0-(size_t)carry;
452 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
453#else
454 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
455 bn_add_words(r_d,r_d,_nist_p_256,BN_NIST_256_TOP);
456#endif
457 /*D4*/ 621 /*D4*/
458 nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14); 622 nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14);
459#if BRANCH_FREE 623 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
460 carry = bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP);
461 bn_add_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
462 mask = 0-(size_t)carry;
463 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
464 624
465 nist_cp_bn(r_d, res, BN_NIST_384_TOP); 625 /* see BN_nist_mod_224 for explanation */
466#else 626 u.f = bn_sub_words;
467 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP)) 627 if (carry > 0)
468 bn_add_words(r_d,r_d,_nist_p_256,BN_NIST_256_TOP); 628 carry = (int)bn_sub_words(r_d,r_d,_nist_p_256[carry-1],BN_NIST_256_TOP);
469#endif 629 else if (carry < 0)
630 {
631 carry = (int)bn_add_words(r_d,r_d,_nist_p_256[-carry-1],BN_NIST_256_TOP);
632 mask = 0-(size_t)carry;
633 u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
634 }
635 else
636 carry = 1;
637
638 mask = 0-(size_t)(*u.f)(c_d,r_d,_nist_p_256[0],BN_NIST_256_TOP);
639 mask &= 0-(size_t)carry;
640 res = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));
641 nist_cp_bn(r_d, res, BN_NIST_256_TOP);
470 r->top = BN_NIST_256_TOP; 642 r->top = BN_NIST_256_TOP;
471 bn_correct_top(r); 643 bn_correct_top(r);
472 644
473 return 1; 645 return 1;
474#else /* BN_BITS!=32 */
475 return 0;
476#endif
477 } 646 }
478 647
479#define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \ 648#define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
480 { \ 649 { \
481 if (a12 != 0) bn_cp_32(to, 0, from, (a12) - 12) else bn_32_set_0(to, 0)\ 650 bn_cp_32(to, 0, from, (a12) - 12) \
482 if (a11 != 0) bn_cp_32(to, 1, from, (a11) - 12) else bn_32_set_0(to, 1)\ 651 bn_cp_32(to, 1, from, (a11) - 12) \
483 if (a10 != 0) bn_cp_32(to, 2, from, (a10) - 12) else bn_32_set_0(to, 2)\ 652 bn_cp_32(to, 2, from, (a10) - 12) \
484 if (a9 != 0) bn_cp_32(to, 3, from, (a9) - 12) else bn_32_set_0(to, 3)\ 653 bn_cp_32(to, 3, from, (a9) - 12) \
485 if (a8 != 0) bn_cp_32(to, 4, from, (a8) - 12) else bn_32_set_0(to, 4)\ 654 bn_cp_32(to, 4, from, (a8) - 12) \
486 if (a7 != 0) bn_cp_32(to, 5, from, (a7) - 12) else bn_32_set_0(to, 5)\ 655 bn_cp_32(to, 5, from, (a7) - 12) \
487 if (a6 != 0) bn_cp_32(to, 6, from, (a6) - 12) else bn_32_set_0(to, 6)\ 656 bn_cp_32(to, 6, from, (a6) - 12) \
488 if (a5 != 0) bn_cp_32(to, 7, from, (a5) - 12) else bn_32_set_0(to, 7)\ 657 bn_cp_32(to, 7, from, (a5) - 12) \
489 if (a4 != 0) bn_cp_32(to, 8, from, (a4) - 12) else bn_32_set_0(to, 8)\ 658 bn_cp_32(to, 8, from, (a4) - 12) \
490 if (a3 != 0) bn_cp_32(to, 9, from, (a3) - 12) else bn_32_set_0(to, 9)\ 659 bn_cp_32(to, 9, from, (a3) - 12) \
491 if (a2 != 0) bn_cp_32(to, 10, from, (a2) - 12) else bn_32_set_0(to, 10)\ 660 bn_cp_32(to, 10, from, (a2) - 12) \
492 if (a1 != 0) bn_cp_32(to, 11, from, (a1) - 12) else bn_32_set_0(to, 11)\ 661 bn_cp_32(to, 11, from, (a1) - 12) \
493 } 662 }
494 663
495int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, 664int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
496 BN_CTX *ctx) 665 BN_CTX *ctx)
497 { 666 {
498#if BN_BITS2 == 32
499 int i, top = a->top; 667 int i, top = a->top;
500 int carry = 0; 668 int carry = 0;
501 register BN_ULONG *r_d, *a_d = a->d; 669 register BN_ULONG *r_d, *a_d = a->d;
@@ -504,6 +672,18 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
504 c_d[BN_NIST_384_TOP], 672 c_d[BN_NIST_384_TOP],
505 *res; 673 *res;
506 size_t mask; 674 size_t mask;
675 union { bn_addsub_f f; size_t p; } u;
676 static const BIGNUM _bignum_nist_p_384_sqr = {
677 (BN_ULONG *)_nist_p_384_sqr,
678 sizeof(_nist_p_384_sqr)/sizeof(_nist_p_384_sqr[0]),
679 sizeof(_nist_p_384_sqr)/sizeof(_nist_p_384_sqr[0]),
680 0,BN_FLG_STATIC_DATA };
681
682
683 field = &_bignum_nist_p_384; /* just to make sure */
684
685 if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_384_sqr)>=0)
686 return BN_nnmod(r, a, field, ctx);
507 687
508 i = BN_ucmp(field, a); 688 i = BN_ucmp(field, a);
509 if (i == 0) 689 if (i == 0)
@@ -514,9 +694,6 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
514 else if (i > 0) 694 else if (i > 0)
515 return (r == a)? 1 : (BN_copy(r ,a) != NULL); 695 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
516 696
517 if (top == BN_NIST_384_TOP)
518 return BN_usub(r, a, field);
519
520 if (r != a) 697 if (r != a)
521 { 698 {
522 if (!bn_wexpand(r, BN_NIST_384_TOP)) 699 if (!bn_wexpand(r, BN_NIST_384_TOP))
@@ -544,149 +721,116 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
544 } 721 }
545 *ap=c; 722 *ap=c;
546 } 723 }
547 carry = bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2), 724 carry = (int)bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2),
548 t_d, BN_NIST_256_TOP); 725 t_d, BN_NIST_256_TOP);
549 /*
550 * we need if (result>=modulus) subtract(result,modulus);
551 * in n-bit space this can be expressed as
552 * if (carry || result>=modulus) subtract(result,modulus);
553 * the catch is that comparison implies subtraction and
554 * therefore one can write tmp=subtract(result,modulus);
555 * and then if(carry || !borrow) result=tmp; this's what
556 * happens below, but without explicit if:-) a.
557 */
558 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
559 mask = ~mask | (0-(size_t)carry);
560 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
561
562 /*S2 */ 726 /*S2 */
563 carry = bn_add_words(r_d, res, buf, BN_NIST_384_TOP); 727 carry += (int)bn_add_words(r_d, r_d, buf, BN_NIST_384_TOP);
564 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
565 mask = ~mask | (0-(size_t)carry);
566 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
567
568 /*S3*/ 728 /*S3*/
569 nist_set_384(t_d,buf,20,19,18,17,16,15,14,13,12,23,22,21); 729 nist_set_384(t_d,buf,20,19,18,17,16,15,14,13,12,23,22,21);
570 carry = bn_add_words(r_d, res, t_d, BN_NIST_384_TOP); 730 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
571 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
572 mask = ~mask | (0-(size_t)carry);
573 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
574
575 /*S4*/ 731 /*S4*/
576 nist_set_384(t_d,buf,19,18,17,16,15,14,13,12,20,0,23,0); 732 nist_set_384(t_d,buf,19,18,17,16,15,14,13,12,20,0,23,0);
577 carry = bn_add_words(r_d, res, t_d, BN_NIST_384_TOP); 733 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
578 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
579 mask = ~mask | (0-(size_t)carry);
580 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
581
582 /*S5*/ 734 /*S5*/
583 nist_set_384(t_d, buf,0,0,0,0,23,22,21,20,0,0,0,0); 735 nist_set_384(t_d, buf,0,0,0,0,23,22,21,20,0,0,0,0);
584 carry = bn_add_words(r_d, res, t_d, BN_NIST_384_TOP); 736 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
585 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
586 mask = ~mask | (0-(size_t)carry);
587 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
588
589 /*S6*/ 737 /*S6*/
590 nist_set_384(t_d,buf,0,0,0,0,0,0,23,22,21,0,0,20); 738 nist_set_384(t_d,buf,0,0,0,0,0,0,23,22,21,0,0,20);
591 carry = bn_add_words(r_d, res, t_d, BN_NIST_384_TOP); 739 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
592 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
593 mask = ~mask | (0-(size_t)carry);
594 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
595
596 /*D1*/ 740 /*D1*/
597 nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23); 741 nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23);
598#if BRANCH_FREE 742 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
599 carry = bn_sub_words(r_d, res, t_d, BN_NIST_384_TOP);
600 bn_add_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
601 mask = 0-(size_t)carry;
602 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
603#else
604 if (bn_sub_words(r_d, res, t_d, BN_NIST_384_TOP))
605 bn_add_words(r_d,r_d,_nist_p_384,BN_NIST_384_TOP);
606#endif
607 /*D2*/ 743 /*D2*/
608 nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0); 744 nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0);
609#if BRANCH_FREE 745 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
610 carry = bn_sub_words(r_d, res, t_d, BN_NIST_384_TOP);
611 bn_add_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
612 mask = 0-(size_t)carry;
613 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
614#else
615 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP))
616 bn_add_words(r_d,r_d,_nist_p_384,BN_NIST_384_TOP);
617#endif
618 /*D3*/ 746 /*D3*/
619 nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0); 747 nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0);
620#if BRANCH_FREE 748 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
621 carry = bn_sub_words(r_d, res, t_d, BN_NIST_384_TOP); 749
622 bn_add_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP); 750 /* see BN_nist_mod_224 for explanation */
623 mask = 0-(size_t)carry; 751 u.f = bn_sub_words;
624 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask)); 752 if (carry > 0)
753 carry = (int)bn_sub_words(r_d,r_d,_nist_p_384[carry-1],BN_NIST_384_TOP);
754 else if (carry < 0)
755 {
756 carry = (int)bn_add_words(r_d,r_d,_nist_p_384[-carry-1],BN_NIST_384_TOP);
757 mask = 0-(size_t)carry;
758 u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
759 }
760 else
761 carry = 1;
625 762
763 mask = 0-(size_t)(*u.f)(c_d,r_d,_nist_p_384[0],BN_NIST_384_TOP);
764 mask &= 0-(size_t)carry;
765 res = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));
626 nist_cp_bn(r_d, res, BN_NIST_384_TOP); 766 nist_cp_bn(r_d, res, BN_NIST_384_TOP);
627#else
628 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP))
629 bn_add_words(r_d,r_d,_nist_p_384,BN_NIST_384_TOP);
630#endif
631 r->top = BN_NIST_384_TOP; 767 r->top = BN_NIST_384_TOP;
632 bn_correct_top(r); 768 bn_correct_top(r);
633 769
634 return 1; 770 return 1;
635#else /* BN_BITS!=32 */
636 return 0;
637#endif
638 } 771 }
639 772
773#define BN_NIST_521_RSHIFT (521%BN_BITS2)
774#define BN_NIST_521_LSHIFT (BN_BITS2-BN_NIST_521_RSHIFT)
775#define BN_NIST_521_TOP_MASK ((BN_ULONG)BN_MASK2>>BN_NIST_521_LSHIFT)
776
640int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, 777int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
641 BN_CTX *ctx) 778 BN_CTX *ctx)
642 { 779 {
643#if BN_BITS2 == 64 780 int top = a->top, i;
644#define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF 781 BN_ULONG *r_d, *a_d = a->d,
645#elif BN_BITS2 == 32 782 t_d[BN_NIST_521_TOP],
646#define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF 783 val,tmp,*res;
647#endif 784 size_t mask;
648 int top, ret = 0; 785 static const BIGNUM _bignum_nist_p_521_sqr = {
649 BN_ULONG *r_d; 786 (BN_ULONG *)_nist_p_521_sqr,
650 BIGNUM *tmp; 787 sizeof(_nist_p_521_sqr)/sizeof(_nist_p_521_sqr[0]),
651 788 sizeof(_nist_p_521_sqr)/sizeof(_nist_p_521_sqr[0]),
652 /* check whether a reduction is necessary */ 789 0,BN_FLG_STATIC_DATA };
653 top = a->top;
654 if (top < BN_NIST_521_TOP || ( top == BN_NIST_521_TOP &&
655 (!(a->d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))))
656 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
657 790
658 BN_CTX_start(ctx); 791 field = &_bignum_nist_p_521; /* just to make sure */
659 tmp = BN_CTX_get(ctx);
660 if (!tmp)
661 goto err;
662 792
663 if (!bn_wexpand(tmp, BN_NIST_521_TOP)) 793 if (BN_is_negative(a) || BN_ucmp(a,&_bignum_nist_p_521_sqr)>=0)
664 goto err; 794 return BN_nnmod(r, a, field, ctx);
665 nist_cp_bn(tmp->d, a->d, BN_NIST_521_TOP);
666 795
667 tmp->top = BN_NIST_521_TOP; 796 i = BN_ucmp(field, a);
668 tmp->d[BN_NIST_521_TOP-1] &= BN_NIST_521_TOP_MASK; 797 if (i == 0)
669 bn_correct_top(tmp); 798 {
799 BN_zero(r);
800 return 1;
801 }
802 else if (i > 0)
803 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
670 804
671 if (!BN_rshift(r, a, 521)) 805 if (r != a)
672 goto err; 806 {
807 if (!bn_wexpand(r,BN_NIST_521_TOP))
808 return 0;
809 r_d = r->d;
810 nist_cp_bn(r_d,a_d, BN_NIST_521_TOP);
811 }
812 else
813 r_d = a_d;
673 814
674 if (!BN_uadd(r, tmp, r)) 815 /* upper 521 bits, copy ... */
675 goto err; 816 nist_cp_bn_0(t_d,a_d + (BN_NIST_521_TOP-1), top - (BN_NIST_521_TOP-1),BN_NIST_521_TOP);
676 top = r->top; 817 /* ... and right shift */
677 r_d = r->d; 818 for (val=t_d[0],i=0; i<BN_NIST_521_TOP-1; i++)
678 if (top == BN_NIST_521_TOP &&
679 (r_d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))
680 { 819 {
681 BN_NIST_ADD_ONE(r_d) 820 tmp = val>>BN_NIST_521_RSHIFT;
682 r->d[BN_NIST_521_TOP-1] &= BN_NIST_521_TOP_MASK; 821 val = t_d[i+1];
822 t_d[i] = (tmp | val<<BN_NIST_521_LSHIFT) & BN_MASK2;
683 } 823 }
824 t_d[i] = val>>BN_NIST_521_RSHIFT;
825 /* lower 521 bits */
826 r_d[i] &= BN_NIST_521_TOP_MASK;
827
828 bn_add_words(r_d,r_d,t_d,BN_NIST_521_TOP);
829 mask = 0-(size_t)bn_sub_words(t_d,r_d,_nist_p_521,BN_NIST_521_TOP);
830 res = (BN_ULONG *)(((size_t)t_d&~mask) | ((size_t)r_d&mask));
831 nist_cp_bn(r_d,res,BN_NIST_521_TOP);
832 r->top = BN_NIST_521_TOP;
684 bn_correct_top(r); 833 bn_correct_top(r);
685 834
686 ret = 1; 835 return 1;
687err:
688 BN_CTX_end(ctx);
689
690 bn_check_top(r);
691 return ret;
692 } 836 }
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c
index f51830b12b..b376c28ff3 100644
--- a/src/lib/libcrypto/bn/bn_rand.c
+++ b/src/lib/libcrypto/bn/bn_rand.c
@@ -227,7 +227,7 @@ int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
227 227
228 228
229/* random number r: 0 <= r < range */ 229/* random number r: 0 <= r < range */
230static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range) 230static int bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range)
231 { 231 {
232 int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand; 232 int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand;
233 int n; 233 int n;
@@ -294,12 +294,12 @@ static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
294 } 294 }
295 295
296 296
297int BN_rand_range(BIGNUM *r, BIGNUM *range) 297int BN_rand_range(BIGNUM *r, const BIGNUM *range)
298 { 298 {
299 return bn_rand_range(0, r, range); 299 return bn_rand_range(0, r, range);
300 } 300 }
301 301
302int BN_pseudo_rand_range(BIGNUM *r, BIGNUM *range) 302int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
303 { 303 {
304 return bn_rand_range(1, r, range); 304 return bn_rand_range(1, r, range);
305 } 305 }
diff --git a/src/lib/libcrypto/bn/bn_shift.c b/src/lib/libcrypto/bn/bn_shift.c
index de9312dce2..c4d301afc4 100644
--- a/src/lib/libcrypto/bn/bn_shift.c
+++ b/src/lib/libcrypto/bn/bn_shift.c
@@ -177,7 +177,7 @@ int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
177 nw=n/BN_BITS2; 177 nw=n/BN_BITS2;
178 rb=n%BN_BITS2; 178 rb=n%BN_BITS2;
179 lb=BN_BITS2-rb; 179 lb=BN_BITS2-rb;
180 if (nw > a->top || a->top == 0) 180 if (nw >= a->top || a->top == 0)
181 { 181 {
182 BN_zero(r); 182 BN_zero(r);
183 return(1); 183 return(1);
diff --git a/src/lib/libcrypto/bn/bn_x931p.c b/src/lib/libcrypto/bn/bn_x931p.c
index c64410dd3a..04c5c874ec 100644
--- a/src/lib/libcrypto/bn/bn_x931p.c
+++ b/src/lib/libcrypto/bn/bn_x931p.c
@@ -1,5 +1,5 @@
1/* bn_x931p.c */ 1/* bn_x931p.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2005. 3 * project 2005.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -59,18 +59,15 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <openssl/bn.h> 60#include <openssl/bn.h>
61 61
62#ifdef OPENSSL_FIPS
63
64/* X9.31 routines for prime derivation */ 62/* X9.31 routines for prime derivation */
65 63
66
67/* X9.31 prime derivation. This is used to generate the primes pi 64/* X9.31 prime derivation. This is used to generate the primes pi
68 * (p1, p2, q1, q2) from a parameter Xpi by checking successive odd 65 * (p1, p2, q1, q2) from a parameter Xpi by checking successive odd
69 * integers. 66 * integers.
70 */ 67 */
71 68
72static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx, 69static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx,
73 void (*cb)(int, int, void *), void *cb_arg) 70 BN_GENCB *cb)
74 { 71 {
75 int i = 0; 72 int i = 0;
76 if (!BN_copy(pi, Xpi)) 73 if (!BN_copy(pi, Xpi))
@@ -80,16 +77,14 @@ static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx,
80 for(;;) 77 for(;;)
81 { 78 {
82 i++; 79 i++;
83 if (cb) 80 BN_GENCB_call(cb, 0, i);
84 cb(0, i, cb_arg);
85 /* NB 27 MR is specificed in X9.31 */ 81 /* NB 27 MR is specificed in X9.31 */
86 if (BN_is_prime_fasttest(pi, 27, cb, ctx, cb_arg, 1)) 82 if (BN_is_prime_fasttest_ex(pi, 27, ctx, 1, cb))
87 break; 83 break;
88 if (!BN_add_word(pi, 2)) 84 if (!BN_add_word(pi, 2))
89 return 0; 85 return 0;
90 } 86 }
91 if (cb) 87 BN_GENCB_call(cb, 2, i);
92 cb(2, i, cb_arg);
93 return 1; 88 return 1;
94 } 89 }
95 90
@@ -98,10 +93,9 @@ static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx,
98 * not NULL they will be returned too: this is needed for testing. 93 * not NULL they will be returned too: this is needed for testing.
99 */ 94 */
100 95
101int BN_X931_derive_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, 96int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
102 void (*cb)(int, int, void *), void *cb_arg,
103 const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2, 97 const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2,
104 const BIGNUM *e, BN_CTX *ctx) 98 const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)
105 { 99 {
106 int ret = 0; 100 int ret = 0;
107 101
@@ -124,10 +118,10 @@ int BN_X931_derive_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
124 118
125 pm1 = BN_CTX_get(ctx); 119 pm1 = BN_CTX_get(ctx);
126 120
127 if (!bn_x931_derive_pi(p1, Xp1, ctx, cb, cb_arg)) 121 if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))
128 goto err; 122 goto err;
129 123
130 if (!bn_x931_derive_pi(p2, Xp2, ctx, cb, cb_arg)) 124 if (!bn_x931_derive_pi(p2, Xp2, ctx, cb))
131 goto err; 125 goto err;
132 126
133 if (!BN_mul(p1p2, p1, p2, ctx)) 127 if (!BN_mul(p1p2, p1, p2, ctx))
@@ -166,8 +160,7 @@ int BN_X931_derive_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
166 for (;;) 160 for (;;)
167 { 161 {
168 int i = 1; 162 int i = 1;
169 if (cb) 163 BN_GENCB_call(cb, 0, i++);
170 cb(0, i++, cb_arg);
171 if (!BN_copy(pm1, p)) 164 if (!BN_copy(pm1, p))
172 goto err; 165 goto err;
173 if (!BN_sub_word(pm1, 1)) 166 if (!BN_sub_word(pm1, 1))
@@ -179,14 +172,13 @@ int BN_X931_derive_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
179 * offering similar or better guarantees 50 MR is considerably 172 * offering similar or better guarantees 50 MR is considerably
180 * better. 173 * better.
181 */ 174 */
182 && BN_is_prime_fasttest(p, 50, cb, ctx, cb_arg, 1)) 175 && BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb))
183 break; 176 break;
184 if (!BN_add(p, p, p1p2)) 177 if (!BN_add(p, p, p1p2))
185 goto err; 178 goto err;
186 } 179 }
187 180
188 if (cb) 181 BN_GENCB_call(cb, 3, 0);
189 cb(3, 0, cb_arg);
190 182
191 ret = 1; 183 ret = 1;
192 184
@@ -248,11 +240,11 @@ int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)
248 * are generated using the previous function and supplied as input. 240 * are generated using the previous function and supplied as input.
249 */ 241 */
250 242
251int BN_X931_generate_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, 243int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
252 BIGNUM *Xp1, BIGNUM *Xp2, 244 BIGNUM *Xp1, BIGNUM *Xp2,
253 const BIGNUM *Xp, 245 const BIGNUM *Xp,
254 const BIGNUM *e, BN_CTX *ctx, 246 const BIGNUM *e, BN_CTX *ctx,
255 void (*cb)(int, int, void *), void *cb_arg) 247 BN_GENCB *cb)
256 { 248 {
257 int ret = 0; 249 int ret = 0;
258 250
@@ -266,8 +258,7 @@ int BN_X931_generate_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
266 goto error; 258 goto error;
267 if (!BN_rand(Xp2, 101, 0, 0)) 259 if (!BN_rand(Xp2, 101, 0, 0))
268 goto error; 260 goto error;
269 if (!BN_X931_derive_prime(p, p1, p2, cb, cb_arg, 261 if (!BN_X931_derive_prime_ex(p, p1, p2, Xp, Xp1, Xp2, e, ctx, cb))
270 Xp, Xp1, Xp2, e, ctx))
271 goto error; 262 goto error;
272 263
273 ret = 1; 264 ret = 1;
@@ -279,4 +270,3 @@ int BN_X931_generate_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
279 270
280 } 271 }
281 272
282#endif
diff --git a/src/lib/libcrypto/buffer/buf_str.c b/src/lib/libcrypto/buffer/buf_str.c
new file mode 100644
index 0000000000..28dd1e401e
--- /dev/null
+++ b/src/lib/libcrypto/buffer/buf_str.c
@@ -0,0 +1,116 @@
1/* crypto/buffer/buf_str.c */
2/* ====================================================================
3 * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56#include <stdio.h>
57#include "cryptlib.h"
58#include <openssl/buffer.h>
59
60char *BUF_strdup(const char *str)
61 {
62 if (str == NULL) return(NULL);
63 return BUF_strndup(str, strlen(str));
64 }
65
66char *BUF_strndup(const char *str, size_t siz)
67 {
68 char *ret;
69
70 if (str == NULL) return(NULL);
71
72 ret=OPENSSL_malloc(siz+1);
73 if (ret == NULL)
74 {
75 BUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE);
76 return(NULL);
77 }
78 BUF_strlcpy(ret,str,siz+1);
79 return(ret);
80 }
81
82void *BUF_memdup(const void *data, size_t siz)
83 {
84 void *ret;
85
86 if (data == NULL) return(NULL);
87
88 ret=OPENSSL_malloc(siz);
89 if (ret == NULL)
90 {
91 BUFerr(BUF_F_BUF_MEMDUP,ERR_R_MALLOC_FAILURE);
92 return(NULL);
93 }
94 return memcpy(ret, data, siz);
95 }
96
97size_t BUF_strlcpy(char *dst, const char *src, size_t size)
98 {
99 size_t l = 0;
100 for(; size > 1 && *src; size--)
101 {
102 *dst++ = *src++;
103 l++;
104 }
105 if (size)
106 *dst = '\0';
107 return l + strlen(src);
108 }
109
110size_t BUF_strlcat(char *dst, const char *src, size_t size)
111 {
112 size_t l = 0;
113 for(; size > 0 && *dst; size--, dst++)
114 l++;
115 return l + BUF_strlcpy(dst, src, size);
116 }
diff --git a/src/lib/libcrypto/buffer/buffer.c b/src/lib/libcrypto/buffer/buffer.c
index 3bf03c7eff..b3e947771d 100644
--- a/src/lib/libcrypto/buffer/buffer.c
+++ b/src/lib/libcrypto/buffer/buffer.c
@@ -161,61 +161,3 @@ int BUF_MEM_grow_clean(BUF_MEM *str, int len)
161 } 161 }
162 return(len); 162 return(len);
163 } 163 }
164
165char *BUF_strdup(const char *str)
166 {
167 if (str == NULL) return(NULL);
168 return BUF_strndup(str, strlen(str));
169 }
170
171char *BUF_strndup(const char *str, size_t siz)
172 {
173 char *ret;
174
175 if (str == NULL) return(NULL);
176
177 ret=OPENSSL_malloc(siz+1);
178 if (ret == NULL)
179 {
180 BUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE);
181 return(NULL);
182 }
183 BUF_strlcpy(ret,str,siz+1);
184 return(ret);
185 }
186
187void *BUF_memdup(const void *data, size_t siz)
188 {
189 void *ret;
190
191 if (data == NULL) return(NULL);
192
193 ret=OPENSSL_malloc(siz);
194 if (ret == NULL)
195 {
196 BUFerr(BUF_F_BUF_MEMDUP,ERR_R_MALLOC_FAILURE);
197 return(NULL);
198 }
199 return memcpy(ret, data, siz);
200 }
201
202size_t BUF_strlcpy(char *dst, const char *src, size_t size)
203 {
204 size_t l = 0;
205 for(; size > 1 && *src; size--)
206 {
207 *dst++ = *src++;
208 l++;
209 }
210 if (size)
211 *dst = '\0';
212 return l + strlen(src);
213 }
214
215size_t BUF_strlcat(char *dst, const char *src, size_t size)
216 {
217 size_t l = 0;
218 for(; size > 0 && *dst; size--, dst++)
219 l++;
220 return l + BUF_strlcpy(dst, src, size);
221 }
diff --git a/src/lib/libcrypto/camellia/camellia.h b/src/lib/libcrypto/camellia/camellia.h
index 3c8a359543..b8a8b6e10b 100644
--- a/src/lib/libcrypto/camellia/camellia.h
+++ b/src/lib/libcrypto/camellia/camellia.h
@@ -87,6 +87,11 @@ struct camellia_key_st
87 87
88typedef struct camellia_key_st CAMELLIA_KEY; 88typedef struct camellia_key_st CAMELLIA_KEY;
89 89
90#ifdef OPENSSL_FIPS
91int private_Camellia_set_key(const unsigned char *userKey, const int bits,
92 CAMELLIA_KEY *key);
93#endif
94
90int Camellia_set_key(const unsigned char *userKey, const int bits, 95int Camellia_set_key(const unsigned char *userKey, const int bits,
91 CAMELLIA_KEY *key); 96 CAMELLIA_KEY *key);
92 97
diff --git a/src/lib/libcrypto/camellia/cmll_misc.c b/src/lib/libcrypto/camellia/cmll_misc.c
index f1047b54e0..2cd7aba9bb 100644
--- a/src/lib/libcrypto/camellia/cmll_misc.c
+++ b/src/lib/libcrypto/camellia/cmll_misc.c
@@ -52,11 +52,24 @@
52#include <openssl/opensslv.h> 52#include <openssl/opensslv.h>
53#include <openssl/camellia.h> 53#include <openssl/camellia.h>
54#include "cmll_locl.h" 54#include "cmll_locl.h"
55#include <openssl/crypto.h>
56#ifdef OPENSSL_FIPS
57#include <openssl/fips.h>
58#endif
55 59
56const char CAMELLIA_version[]="CAMELLIA" OPENSSL_VERSION_PTEXT; 60const char CAMELLIA_version[]="CAMELLIA" OPENSSL_VERSION_PTEXT;
57 61
58int Camellia_set_key(const unsigned char *userKey, const int bits, 62int Camellia_set_key(const unsigned char *userKey, const int bits,
59 CAMELLIA_KEY *key) 63 CAMELLIA_KEY *key)
64#ifdef OPENSSL_FIPS
65 {
66 if (FIPS_mode())
67 FIPS_BAD_ABORT(CAMELLIA)
68 return private_Camellia_set_key(userKey, bits, key);
69 }
70int private_Camellia_set_key(const unsigned char *userKey, const int bits,
71 CAMELLIA_KEY *key)
72#endif
60 { 73 {
61 if (!userKey || !key) 74 if (!userKey || !key)
62 { 75 {
diff --git a/src/lib/libcrypto/cast/c_skey.c b/src/lib/libcrypto/cast/c_skey.c
index 76e40005c9..68e690a60c 100644
--- a/src/lib/libcrypto/cast/c_skey.c
+++ b/src/lib/libcrypto/cast/c_skey.c
@@ -57,6 +57,11 @@
57 */ 57 */
58 58
59#include <openssl/cast.h> 59#include <openssl/cast.h>
60#include <openssl/crypto.h>
61#ifdef OPENSSL_FIPS
62#include <openssl/fips.h>
63#endif
64
60#include "cast_lcl.h" 65#include "cast_lcl.h"
61#include "cast_s.h" 66#include "cast_s.h"
62 67
@@ -72,7 +77,7 @@
72#define S6 CAST_S_table6 77#define S6 CAST_S_table6
73#define S7 CAST_S_table7 78#define S7 CAST_S_table7
74 79
75void CAST_set_key(CAST_KEY *key, int len, const unsigned char *data) 80FIPS_NON_FIPS_VCIPHER_Init(CAST)
76 { 81 {
77 CAST_LONG x[16]; 82 CAST_LONG x[16];
78 CAST_LONG z[16]; 83 CAST_LONG z[16];
diff --git a/src/lib/libcrypto/cast/cast.h b/src/lib/libcrypto/cast/cast.h
index 90b45b950a..1faf5806aa 100644
--- a/src/lib/libcrypto/cast/cast.h
+++ b/src/lib/libcrypto/cast/cast.h
@@ -83,7 +83,9 @@ typedef struct cast_key_st
83 int short_key; /* Use reduced rounds for short key */ 83 int short_key; /* Use reduced rounds for short key */
84 } CAST_KEY; 84 } CAST_KEY;
85 85
86 86#ifdef OPENSSL_FIPS
87void private_CAST_set_key(CAST_KEY *key, int len, const unsigned char *data);
88#endif
87void CAST_set_key(CAST_KEY *key, int len, const unsigned char *data); 89void CAST_set_key(CAST_KEY *key, int len, const unsigned char *data);
88void CAST_ecb_encrypt(const unsigned char *in,unsigned char *out,CAST_KEY *key, 90void CAST_ecb_encrypt(const unsigned char *in,unsigned char *out,CAST_KEY *key,
89 int enc); 91 int enc);
diff --git a/src/lib/libcrypto/cms/cms_sd.c b/src/lib/libcrypto/cms/cms_sd.c
index 591bfbec33..cdac3b870d 100644
--- a/src/lib/libcrypto/cms/cms_sd.c
+++ b/src/lib/libcrypto/cms/cms_sd.c
@@ -830,7 +830,7 @@ int CMS_SignerInfo_verify(CMS_SignerInfo *si)
830 cms_fixup_mctx(&mctx, si->pkey); 830 cms_fixup_mctx(&mctx, si->pkey);
831 r = EVP_VerifyFinal(&mctx, 831 r = EVP_VerifyFinal(&mctx,
832 si->signature->data, si->signature->length, si->pkey); 832 si->signature->data, si->signature->length, si->pkey);
833 if (!r) 833 if (r <= 0)
834 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE); 834 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
835 err: 835 err:
836 EVP_MD_CTX_cleanup(&mctx); 836 EVP_MD_CTX_cleanup(&mctx);
diff --git a/src/lib/libcrypto/cms/cms_smime.c b/src/lib/libcrypto/cms/cms_smime.c
index f79c504e91..b9463f9abb 100644
--- a/src/lib/libcrypto/cms/cms_smime.c
+++ b/src/lib/libcrypto/cms/cms_smime.c
@@ -68,7 +68,10 @@ static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
68 if (out == NULL) 68 if (out == NULL)
69 tmpout = BIO_new(BIO_s_null()); 69 tmpout = BIO_new(BIO_s_null());
70 else if (flags & CMS_TEXT) 70 else if (flags & CMS_TEXT)
71 {
71 tmpout = BIO_new(BIO_s_mem()); 72 tmpout = BIO_new(BIO_s_mem());
73 BIO_set_mem_eof_return(tmpout, 0);
74 }
72 else 75 else
73 tmpout = out; 76 tmpout = out;
74 77
@@ -89,11 +92,13 @@ static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
89 if (!BIO_get_cipher_status(in)) 92 if (!BIO_get_cipher_status(in))
90 goto err; 93 goto err;
91 } 94 }
95 if (i < 0)
96 goto err;
92 break; 97 break;
93 } 98 }
94 99
95 if (tmpout) 100 if (tmpout && (BIO_write(tmpout, buf, i) != i))
96 BIO_write(tmpout, buf, i); 101 goto err;
97 } 102 }
98 103
99 if(flags & CMS_TEXT) 104 if(flags & CMS_TEXT)
diff --git a/src/lib/libcrypto/comp/c_zlib.c b/src/lib/libcrypto/comp/c_zlib.c
index 0f34597e70..eccfd09137 100644
--- a/src/lib/libcrypto/comp/c_zlib.c
+++ b/src/lib/libcrypto/comp/c_zlib.c
@@ -727,6 +727,7 @@ static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
727 case BIO_CTRL_RESET: 727 case BIO_CTRL_RESET:
728 ctx->ocount = 0; 728 ctx->ocount = 0;
729 ctx->odone = 0; 729 ctx->odone = 0;
730 ret = 1;
730 break; 731 break;
731 732
732 case BIO_CTRL_FLUSH: 733 case BIO_CTRL_FLUSH:
@@ -771,7 +772,7 @@ static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
771 } 772 }
772 ctx->obufsize = obs; 773 ctx->obufsize = obs;
773 } 774 }
774 775 ret = 1;
775 break; 776 break;
776 777
777 case BIO_C_DO_STATE_MACHINE: 778 case BIO_C_DO_STATE_MACHINE:
@@ -783,7 +784,6 @@ static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
783 default: 784 default:
784 ret = BIO_ctrl(b->next_bio, cmd, num, ptr); 785 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
785 break; 786 break;
786
787 } 787 }
788 788
789 return ret; 789 return ret;
diff --git a/src/lib/libcrypto/conf/conf_mall.c b/src/lib/libcrypto/conf/conf_mall.c
index 4ba40cf44c..1cc1fd5534 100644
--- a/src/lib/libcrypto/conf/conf_mall.c
+++ b/src/lib/libcrypto/conf/conf_mall.c
@@ -1,5 +1,5 @@
1/* conf_mall.c */ 1/* conf_mall.c */
2/* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -63,6 +63,7 @@
63#include <openssl/dso.h> 63#include <openssl/dso.h>
64#include <openssl/x509.h> 64#include <openssl/x509.h>
65#include <openssl/asn1.h> 65#include <openssl/asn1.h>
66#include <openssl/evp.h>
66#ifndef OPENSSL_NO_ENGINE 67#ifndef OPENSSL_NO_ENGINE
67#include <openssl/engine.h> 68#include <openssl/engine.h>
68#endif 69#endif
@@ -76,5 +77,6 @@ void OPENSSL_load_builtin_modules(void)
76#ifndef OPENSSL_NO_ENGINE 77#ifndef OPENSSL_NO_ENGINE
77 ENGINE_add_conf_module(); 78 ENGINE_add_conf_module();
78#endif 79#endif
80 EVP_add_alg_module();
79 } 81 }
80 82
diff --git a/src/lib/libcrypto/conf/conf_mod.c b/src/lib/libcrypto/conf/conf_mod.c
index 628e8333a6..ee9c677d9b 100644
--- a/src/lib/libcrypto/conf/conf_mod.c
+++ b/src/lib/libcrypto/conf/conf_mod.c
@@ -1,5 +1,5 @@
1/* conf_mod.c */ 1/* conf_mod.c */
2/* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/conf/conf_sap.c b/src/lib/libcrypto/conf/conf_sap.c
index 9c53bac1a8..760dc2632d 100644
--- a/src/lib/libcrypto/conf/conf_sap.c
+++ b/src/lib/libcrypto/conf/conf_sap.c
@@ -1,5 +1,5 @@
1/* conf_sap.c */ 1/* conf_sap.c */
2/* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/cryptlib.c b/src/lib/libcrypto/cryptlib.c
index 8c68623828..8f9e88e403 100644
--- a/src/lib/libcrypto/cryptlib.c
+++ b/src/lib/libcrypto/cryptlib.c
@@ -121,275 +121,17 @@
121static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */ 121static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */
122#endif 122#endif
123 123
124DECLARE_STACK_OF(CRYPTO_dynlock)
125IMPLEMENT_STACK_OF(CRYPTO_dynlock)
126
127/* real #defines in crypto.h, keep these upto date */
128static const char* const lock_names[CRYPTO_NUM_LOCKS] =
129 {
130 "<<ERROR>>",
131 "err",
132 "ex_data",
133 "x509",
134 "x509_info",
135 "x509_pkey",
136 "x509_crl",
137 "x509_req",
138 "dsa",
139 "rsa",
140 "evp_pkey",
141 "x509_store",
142 "ssl_ctx",
143 "ssl_cert",
144 "ssl_session",
145 "ssl_sess_cert",
146 "ssl",
147 "ssl_method",
148 "rand",
149 "rand2",
150 "debug_malloc",
151 "BIO",
152 "gethostbyname",
153 "getservbyname",
154 "readdir",
155 "RSA_blinding",
156 "dh",
157 "debug_malloc2",
158 "dso",
159 "dynlock",
160 "engine",
161 "ui",
162 "ecdsa",
163 "ec",
164 "ecdh",
165 "bn",
166 "ec_pre_comp",
167 "store",
168 "comp",
169#if CRYPTO_NUM_LOCKS != 39
170# error "Inconsistency between crypto.h and cryptlib.c"
171#endif
172 };
173
174/* This is for applications to allocate new type names in the non-dynamic
175 array of lock names. These are numbered with positive numbers. */
176static STACK *app_locks=NULL;
177
178/* For applications that want a more dynamic way of handling threads, the
179 following stack is used. These are externally numbered with negative
180 numbers. */
181static STACK_OF(CRYPTO_dynlock) *dyn_locks=NULL;
182
183
184static void (MS_FAR *locking_callback)(int mode,int type, 124static void (MS_FAR *locking_callback)(int mode,int type,
185 const char *file,int line)=NULL; 125 const char *file,int line)=NULL;
186static int (MS_FAR *add_lock_callback)(int *pointer,int amount, 126static int (MS_FAR *add_lock_callback)(int *pointer,int amount,
187 int type,const char *file,int line)=NULL; 127 int type,const char *file,int line)=NULL;
188static unsigned long (MS_FAR *id_callback)(void)=NULL; 128static unsigned long (MS_FAR *id_callback)(void)=NULL;
189static struct CRYPTO_dynlock_value *(MS_FAR *dynlock_create_callback)
190 (const char *file,int line)=NULL;
191static void (MS_FAR *dynlock_lock_callback)(int mode,
192 struct CRYPTO_dynlock_value *l, const char *file,int line)=NULL;
193static void (MS_FAR *dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l,
194 const char *file,int line)=NULL;
195
196int CRYPTO_get_new_lockid(char *name)
197 {
198 char *str;
199 int i;
200
201#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
202 /* A hack to make Visual C++ 5.0 work correctly when linking as
203 * a DLL using /MT. Without this, the application cannot use
204 * and floating point printf's.
205 * It also seems to be needed for Visual C 1.5 (win16) */
206 SSLeay_MSVC5_hack=(double)name[0]*(double)name[1];
207#endif
208
209 if ((app_locks == NULL) && ((app_locks=sk_new_null()) == NULL))
210 {
211 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
212 return(0);
213 }
214 if ((str=BUF_strdup(name)) == NULL)
215 {
216 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
217 return(0);
218 }
219 i=sk_push(app_locks,str);
220 if (!i)
221 OPENSSL_free(str);
222 else
223 i+=CRYPTO_NUM_LOCKS; /* gap of one :-) */
224 return(i);
225 }
226 129
227int CRYPTO_num_locks(void) 130int CRYPTO_num_locks(void)
228 { 131 {
229 return CRYPTO_NUM_LOCKS; 132 return CRYPTO_NUM_LOCKS;
230 } 133 }
231 134
232int CRYPTO_get_new_dynlockid(void)
233 {
234 int i = 0;
235 CRYPTO_dynlock *pointer = NULL;
236
237 if (dynlock_create_callback == NULL)
238 {
239 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK);
240 return(0);
241 }
242 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
243 if ((dyn_locks == NULL)
244 && ((dyn_locks=sk_CRYPTO_dynlock_new_null()) == NULL))
245 {
246 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
247 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
248 return(0);
249 }
250 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
251
252 pointer = (CRYPTO_dynlock *)OPENSSL_malloc(sizeof(CRYPTO_dynlock));
253 if (pointer == NULL)
254 {
255 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
256 return(0);
257 }
258 pointer->references = 1;
259 pointer->data = dynlock_create_callback(__FILE__,__LINE__);
260 if (pointer->data == NULL)
261 {
262 OPENSSL_free(pointer);
263 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
264 return(0);
265 }
266
267 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
268 /* First, try to find an existing empty slot */
269 i=sk_CRYPTO_dynlock_find(dyn_locks,NULL);
270 /* If there was none, push, thereby creating a new one */
271 if (i == -1)
272 /* Since sk_push() returns the number of items on the
273 stack, not the location of the pushed item, we need
274 to transform the returned number into a position,
275 by decreasing it. */
276 i=sk_CRYPTO_dynlock_push(dyn_locks,pointer) - 1;
277 else
278 /* If we found a place with a NULL pointer, put our pointer
279 in it. */
280 (void)sk_CRYPTO_dynlock_set(dyn_locks,i,pointer);
281 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
282
283 if (i == -1)
284 {
285 dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
286 OPENSSL_free(pointer);
287 }
288 else
289 i += 1; /* to avoid 0 */
290 return -i;
291 }
292
293void CRYPTO_destroy_dynlockid(int i)
294 {
295 CRYPTO_dynlock *pointer = NULL;
296 if (i)
297 i = -i-1;
298 if (dynlock_destroy_callback == NULL)
299 return;
300
301 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
302
303 if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks))
304 {
305 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
306 return;
307 }
308 pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
309 if (pointer != NULL)
310 {
311 --pointer->references;
312#ifdef REF_CHECK
313 if (pointer->references < 0)
314 {
315 fprintf(stderr,"CRYPTO_destroy_dynlockid, bad reference count\n");
316 abort();
317 }
318 else
319#endif
320 if (pointer->references <= 0)
321 {
322 (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
323 }
324 else
325 pointer = NULL;
326 }
327 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
328
329 if (pointer)
330 {
331 dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
332 OPENSSL_free(pointer);
333 }
334 }
335
336struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i)
337 {
338 CRYPTO_dynlock *pointer = NULL;
339 if (i)
340 i = -i-1;
341
342 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
343
344 if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks))
345 pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
346 if (pointer)
347 pointer->references++;
348
349 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
350
351 if (pointer)
352 return pointer->data;
353 return NULL;
354 }
355
356struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))
357 (const char *file,int line)
358 {
359 return(dynlock_create_callback);
360 }
361
362void (*CRYPTO_get_dynlock_lock_callback(void))(int mode,
363 struct CRYPTO_dynlock_value *l, const char *file,int line)
364 {
365 return(dynlock_lock_callback);
366 }
367
368void (*CRYPTO_get_dynlock_destroy_callback(void))
369 (struct CRYPTO_dynlock_value *l, const char *file,int line)
370 {
371 return(dynlock_destroy_callback);
372 }
373
374void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
375 (const char *file, int line))
376 {
377 dynlock_create_callback=func;
378 }
379
380void CRYPTO_set_dynlock_lock_callback(void (*func)(int mode,
381 struct CRYPTO_dynlock_value *l, const char *file, int line))
382 {
383 dynlock_lock_callback=func;
384 }
385
386void CRYPTO_set_dynlock_destroy_callback(void (*func)
387 (struct CRYPTO_dynlock_value *l, const char *file, int line))
388 {
389 dynlock_destroy_callback=func;
390 }
391
392
393void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file, 135void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file,
394 int line) 136 int line)
395 { 137 {
@@ -445,6 +187,14 @@ unsigned long CRYPTO_thread_id(void)
445 return(ret); 187 return(ret);
446 } 188 }
447 189
190static void (*do_dynlock_cb)(int mode, int type, const char *file, int line);
191
192void int_CRYPTO_set_do_dynlock_callback(
193 void (*dyn_cb)(int mode, int type, const char *file, int line))
194 {
195 do_dynlock_cb = dyn_cb;
196 }
197
448void CRYPTO_lock(int mode, int type, const char *file, int line) 198void CRYPTO_lock(int mode, int type, const char *file, int line)
449 { 199 {
450#ifdef LOCK_DEBUG 200#ifdef LOCK_DEBUG
@@ -472,17 +222,8 @@ void CRYPTO_lock(int mode, int type, const char *file, int line)
472#endif 222#endif
473 if (type < 0) 223 if (type < 0)
474 { 224 {
475 if (dynlock_lock_callback != NULL) 225 if (do_dynlock_cb)
476 { 226 do_dynlock_cb(mode, type, file, line);
477 struct CRYPTO_dynlock_value *pointer
478 = CRYPTO_get_dynlock_value(type);
479
480 OPENSSL_assert(pointer != NULL);
481
482 dynlock_lock_callback(mode, pointer, file, line);
483
484 CRYPTO_destroy_dynlockid(type);
485 }
486 } 227 }
487 else 228 else
488 if (locking_callback != NULL) 229 if (locking_callback != NULL)
@@ -527,21 +268,9 @@ int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
527 return(ret); 268 return(ret);
528 } 269 }
529 270
530const char *CRYPTO_get_lock_name(int type)
531 {
532 if (type < 0)
533 return("dynamic");
534 else if (type < CRYPTO_NUM_LOCKS)
535 return(lock_names[type]);
536 else if (type-CRYPTO_NUM_LOCKS > sk_num(app_locks))
537 return("ERROR");
538 else
539 return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS));
540 }
541
542#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ 271#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
543 defined(__INTEL__) || \ 272 defined(__INTEL__) || \
544 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) 273 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
545 274
546unsigned long OPENSSL_ia32cap_P=0; 275unsigned long OPENSSL_ia32cap_P=0;
547unsigned long *OPENSSL_ia32cap_loc(void) { return &OPENSSL_ia32cap_P; } 276unsigned long *OPENSSL_ia32cap_loc(void) { return &OPENSSL_ia32cap_P; }
@@ -577,6 +306,62 @@ void OPENSSL_cpuid_setup(void) {}
577#endif 306#endif
578 307
579#if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL) 308#if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL)
309
310#ifdef OPENSSL_FIPS
311
312#include <tlhelp32.h>
313#if defined(__GNUC__) && __GNUC__>=2
314static int DllInit(void) __attribute__((constructor));
315#elif defined(_MSC_VER)
316static int DllInit(void);
317# ifdef _WIN64
318# pragma section(".CRT$XCU",read)
319 __declspec(allocate(".CRT$XCU"))
320# else
321# pragma data_seg(".CRT$XCU")
322# endif
323 static int (*p)(void) = DllInit;
324# pragma data_seg()
325#endif
326
327static int DllInit(void)
328{
329#if defined(_WIN32_WINNT)
330 union { int(*f)(void); BYTE *p; } t = { DllInit };
331 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
332 IMAGE_DOS_HEADER *dos_header;
333 IMAGE_NT_HEADERS *nt_headers;
334 MODULEENTRY32 me32 = {sizeof(me32)};
335
336 hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,0);
337 if (hModuleSnap != INVALID_HANDLE_VALUE &&
338 Module32First(hModuleSnap,&me32)) do
339 {
340 if (t.p >= me32.modBaseAddr &&
341 t.p < me32.modBaseAddr+me32.modBaseSize)
342 {
343 dos_header=(IMAGE_DOS_HEADER *)me32.modBaseAddr;
344 if (dos_header->e_magic==IMAGE_DOS_SIGNATURE)
345 {
346 nt_headers=(IMAGE_NT_HEADERS *)
347 ((BYTE *)dos_header+dos_header->e_lfanew);
348 if (nt_headers->Signature==IMAGE_NT_SIGNATURE &&
349 me32.modBaseAddr!=(BYTE*)nt_headers->OptionalHeader.ImageBase)
350 OPENSSL_NONPIC_relocated=1;
351 }
352 break;
353 }
354 } while (Module32Next(hModuleSnap,&me32));
355
356 if (hModuleSnap != INVALID_HANDLE_VALUE)
357 CloseHandle(hModuleSnap);
358#endif
359 OPENSSL_cpuid_setup();
360 return 0;
361}
362
363#else
364
580#ifdef __CYGWIN__ 365#ifdef __CYGWIN__
581/* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */ 366/* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
582#include <windows.h> 367#include <windows.h>
@@ -620,6 +405,8 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
620 } 405 }
621#endif 406#endif
622 407
408#endif
409
623#if defined(_WIN32) && !defined(__CYGWIN__) 410#if defined(_WIN32) && !defined(__CYGWIN__)
624#include <tchar.h> 411#include <tchar.h>
625 412
diff --git a/src/lib/libcrypto/cryptlib.h b/src/lib/libcrypto/cryptlib.h
index 5ceaa964b5..fc249c57f3 100644
--- a/src/lib/libcrypto/cryptlib.h
+++ b/src/lib/libcrypto/cryptlib.h
@@ -103,7 +103,6 @@ extern unsigned long OPENSSL_ia32cap_P;
103void OPENSSL_showfatal(const char *,...); 103void OPENSSL_showfatal(const char *,...);
104void *OPENSSL_stderr(void); 104void *OPENSSL_stderr(void);
105extern int OPENSSL_NONPIC_relocated; 105extern int OPENSSL_NONPIC_relocated;
106int OPENSSL_isservice(void);
107 106
108#ifdef __cplusplus 107#ifdef __cplusplus
109} 108}
diff --git a/src/lib/libcrypto/crypto.h b/src/lib/libcrypto/crypto.h
index d2b5ffe332..0e4fb0723c 100644
--- a/src/lib/libcrypto/crypto.h
+++ b/src/lib/libcrypto/crypto.h
@@ -219,7 +219,13 @@ typedef struct openssl_item_st
219#define CRYPTO_LOCK_EC_PRE_COMP 36 219#define CRYPTO_LOCK_EC_PRE_COMP 36
220#define CRYPTO_LOCK_STORE 37 220#define CRYPTO_LOCK_STORE 37
221#define CRYPTO_LOCK_COMP 38 221#define CRYPTO_LOCK_COMP 38
222#ifndef OPENSSL_FIPS
222#define CRYPTO_NUM_LOCKS 39 223#define CRYPTO_NUM_LOCKS 39
224#else
225#define CRYPTO_LOCK_FIPS 39
226#define CRYPTO_LOCK_FIPS2 40
227#define CRYPTO_NUM_LOCKS 41
228#endif
223 229
224#define CRYPTO_LOCK 1 230#define CRYPTO_LOCK 1
225#define CRYPTO_UNLOCK 2 231#define CRYPTO_UNLOCK 2
@@ -341,14 +347,7 @@ DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
341 347
342/* Set standard debugging functions (not done by default 348/* Set standard debugging functions (not done by default
343 * unless CRYPTO_MDEBUG is defined) */ 349 * unless CRYPTO_MDEBUG is defined) */
344#define CRYPTO_malloc_debug_init() do {\ 350void CRYPTO_malloc_debug_init(void);
345 CRYPTO_set_mem_debug_functions(\
346 CRYPTO_dbg_malloc,\
347 CRYPTO_dbg_realloc,\
348 CRYPTO_dbg_free,\
349 CRYPTO_dbg_set_options,\
350 CRYPTO_dbg_get_options);\
351 } while(0)
352 351
353int CRYPTO_mem_ctrl(int mode); 352int CRYPTO_mem_ctrl(int mode);
354int CRYPTO_is_mem_check_on(void); 353int CRYPTO_is_mem_check_on(void);
@@ -363,6 +362,7 @@ int CRYPTO_is_mem_check_on(void);
363#define is_MemCheck_on() CRYPTO_is_mem_check_on() 362#define is_MemCheck_on() CRYPTO_is_mem_check_on()
364 363
365#define OPENSSL_malloc(num) CRYPTO_malloc((int)num,__FILE__,__LINE__) 364#define OPENSSL_malloc(num) CRYPTO_malloc((int)num,__FILE__,__LINE__)
365#define OPENSSL_strdup(str) CRYPTO_strdup((str),__FILE__,__LINE__)
366#define OPENSSL_realloc(addr,num) \ 366#define OPENSSL_realloc(addr,num) \
367 CRYPTO_realloc((char *)addr,(int)num,__FILE__,__LINE__) 367 CRYPTO_realloc((char *)addr,(int)num,__FILE__,__LINE__)
368#define OPENSSL_realloc_clean(addr,old_num,num) \ 368#define OPENSSL_realloc_clean(addr,old_num,num) \
@@ -427,6 +427,9 @@ const char *CRYPTO_get_lock_name(int type);
427int CRYPTO_add_lock(int *pointer,int amount,int type, const char *file, 427int CRYPTO_add_lock(int *pointer,int amount,int type, const char *file,
428 int line); 428 int line);
429 429
430void int_CRYPTO_set_do_dynlock_callback(
431 void (*do_dynlock_cb)(int mode, int type, const char *file, int line));
432
430int CRYPTO_get_new_dynlockid(void); 433int CRYPTO_get_new_dynlockid(void);
431void CRYPTO_destroy_dynlockid(int i); 434void CRYPTO_destroy_dynlockid(int i);
432struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i); 435struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i);
@@ -451,6 +454,10 @@ int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
451 void (*f)(void *,int), 454 void (*f)(void *,int),
452 void (*so)(long), 455 void (*so)(long),
453 long (*go)(void)); 456 long (*go)(void));
457void CRYPTO_set_mem_info_functions(
458 int (*push_info_fn)(const char *info, const char *file, int line),
459 int (*pop_info_fn)(void),
460 int (*remove_all_info_fn)(void));
454void CRYPTO_get_mem_functions(void *(**m)(size_t),void *(**r)(void *, size_t), void (**f)(void *)); 461void CRYPTO_get_mem_functions(void *(**m)(size_t),void *(**r)(void *, size_t), void (**f)(void *));
455void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)); 462void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *));
456void CRYPTO_get_mem_ex_functions(void *(**m)(size_t,const char *,int), 463void CRYPTO_get_mem_ex_functions(void *(**m)(size_t,const char *,int),
@@ -467,6 +474,7 @@ void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
467void *CRYPTO_malloc_locked(int num, const char *file, int line); 474void *CRYPTO_malloc_locked(int num, const char *file, int line);
468void CRYPTO_free_locked(void *); 475void CRYPTO_free_locked(void *);
469void *CRYPTO_malloc(int num, const char *file, int line); 476void *CRYPTO_malloc(int num, const char *file, int line);
477char *CRYPTO_strdup(const char *str, const char *file, int line);
470void CRYPTO_free(void *); 478void CRYPTO_free(void *);
471void *CRYPTO_realloc(void *addr,int num, const char *file, int line); 479void *CRYPTO_realloc(void *addr,int num, const char *file, int line);
472void *CRYPTO_realloc_clean(void *addr,int old_num,int num,const char *file, 480void *CRYPTO_realloc_clean(void *addr,int old_num,int num,const char *file,
@@ -506,6 +514,9 @@ void CRYPTO_dbg_free(void *addr,int before_p);
506void CRYPTO_dbg_set_options(long bits); 514void CRYPTO_dbg_set_options(long bits);
507long CRYPTO_dbg_get_options(void); 515long CRYPTO_dbg_get_options(void);
508 516
517int CRYPTO_dbg_push_info(const char *info, const char *file, int line);
518int CRYPTO_dbg_pop_info(void);
519int CRYPTO_dbg_remove_all_info(void);
509 520
510#ifndef OPENSSL_NO_FP_API 521#ifndef OPENSSL_NO_FP_API
511void CRYPTO_mem_leaks_fp(FILE *); 522void CRYPTO_mem_leaks_fp(FILE *);
@@ -521,6 +532,61 @@ void OpenSSLDie(const char *file,int line,const char *assertion);
521 532
522unsigned long *OPENSSL_ia32cap_loc(void); 533unsigned long *OPENSSL_ia32cap_loc(void);
523#define OPENSSL_ia32cap (*(OPENSSL_ia32cap_loc())) 534#define OPENSSL_ia32cap (*(OPENSSL_ia32cap_loc()))
535int OPENSSL_isservice(void);
536
537#ifdef OPENSSL_FIPS
538#define FIPS_ERROR_IGNORED(alg) OpenSSLDie(__FILE__, __LINE__, \
539 alg " previous FIPS forbidden algorithm error ignored");
540
541#define FIPS_BAD_ABORT(alg) OpenSSLDie(__FILE__, __LINE__, \
542 #alg " Algorithm forbidden in FIPS mode");
543
544#ifdef OPENSSL_FIPS_STRICT
545#define FIPS_BAD_ALGORITHM(alg) FIPS_BAD_ABORT(alg)
546#else
547#define FIPS_BAD_ALGORITHM(alg) \
548 { \
549 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD); \
550 ERR_add_error_data(2, "Algorithm=", #alg); \
551 return 0; \
552 }
553#endif
554
555/* Low level digest API blocking macro */
556
557#define FIPS_NON_FIPS_MD_Init(alg) \
558 int alg##_Init(alg##_CTX *c) \
559 { \
560 if (FIPS_mode()) \
561 FIPS_BAD_ALGORITHM(alg) \
562 return private_##alg##_Init(c); \
563 } \
564 int private_##alg##_Init(alg##_CTX *c)
565
566/* For ciphers the API often varies from cipher to cipher and each needs to
567 * be treated as a special case. Variable key length ciphers (Blowfish, RC4,
568 * CAST) however are very similar and can use a blocking macro.
569 */
570
571#define FIPS_NON_FIPS_VCIPHER_Init(alg) \
572 void alg##_set_key(alg##_KEY *key, int len, const unsigned char *data) \
573 { \
574 if (FIPS_mode()) \
575 FIPS_BAD_ABORT(alg) \
576 private_##alg##_set_key(key, len, data); \
577 } \
578 void private_##alg##_set_key(alg##_KEY *key, int len, \
579 const unsigned char *data)
580
581#else
582
583#define FIPS_NON_FIPS_VCIPHER_Init(alg) \
584 void alg##_set_key(alg##_KEY *key, int len, const unsigned char *data)
585
586#define FIPS_NON_FIPS_MD_Init(alg) \
587 int alg##_Init(alg##_CTX *c)
588
589#endif /* def OPENSSL_FIPS */
524 590
525/* BEGIN ERROR CODES */ 591/* BEGIN ERROR CODES */
526/* The following lines are auto generated by the script mkerr.pl. Any changes 592/* The following lines are auto generated by the script mkerr.pl. Any changes
@@ -528,6 +594,9 @@ unsigned long *OPENSSL_ia32cap_loc(void);
528 */ 594 */
529void ERR_load_CRYPTO_strings(void); 595void ERR_load_CRYPTO_strings(void);
530 596
597#define OPENSSL_HAVE_INIT 1
598void OPENSSL_init(void);
599
531/* Error codes for the CRYPTO functions. */ 600/* Error codes for the CRYPTO functions. */
532 601
533/* Function codes. */ 602/* Function codes. */
diff --git a/src/lib/libcrypto/des/des_enc.c b/src/lib/libcrypto/des/des_enc.c
index 1c37ab96d3..cf71965aca 100644
--- a/src/lib/libcrypto/des/des_enc.c
+++ b/src/lib/libcrypto/des/des_enc.c
@@ -289,6 +289,8 @@ void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
289 289
290#ifndef DES_DEFAULT_OPTIONS 290#ifndef DES_DEFAULT_OPTIONS
291 291
292#if !defined(OPENSSL_FIPS_DES_ASM)
293
292#undef CBC_ENC_C__DONT_UPDATE_IV 294#undef CBC_ENC_C__DONT_UPDATE_IV
293#include "ncbc_enc.c" /* DES_ncbc_encrypt */ 295#include "ncbc_enc.c" /* DES_ncbc_encrypt */
294 296
@@ -404,4 +406,6 @@ void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
404 tin[0]=tin[1]=0; 406 tin[0]=tin[1]=0;
405 } 407 }
406 408
409#endif
410
407#endif /* DES_DEFAULT_OPTIONS */ 411#endif /* DES_DEFAULT_OPTIONS */
diff --git a/src/lib/libcrypto/des/ecb_enc.c b/src/lib/libcrypto/des/ecb_enc.c
index 00d5b91e8c..75ae6cf8bb 100644
--- a/src/lib/libcrypto/des/ecb_enc.c
+++ b/src/lib/libcrypto/des/ecb_enc.c
@@ -57,54 +57,7 @@
57 */ 57 */
58 58
59#include "des_locl.h" 59#include "des_locl.h"
60#include "des_ver.h"
61#include "spr.h" 60#include "spr.h"
62#include <openssl/opensslv.h>
63#include <openssl/bio.h>
64
65OPENSSL_GLOBAL const char libdes_version[]="libdes" OPENSSL_VERSION_PTEXT;
66OPENSSL_GLOBAL const char DES_version[]="DES" OPENSSL_VERSION_PTEXT;
67
68const char *DES_options(void)
69 {
70 static int init=1;
71 static char buf[32];
72
73 if (init)
74 {
75 const char *ptr,*unroll,*risc,*size;
76
77#ifdef DES_PTR
78 ptr="ptr";
79#else
80 ptr="idx";
81#endif
82#if defined(DES_RISC1) || defined(DES_RISC2)
83#ifdef DES_RISC1
84 risc="risc1";
85#endif
86#ifdef DES_RISC2
87 risc="risc2";
88#endif
89#else
90 risc="cisc";
91#endif
92#ifdef DES_UNROLL
93 unroll="16";
94#else
95 unroll="4";
96#endif
97 if (sizeof(DES_LONG) != sizeof(long))
98 size="int";
99 else
100 size="long";
101 BIO_snprintf(buf,sizeof buf,"des(%s,%s,%s,%s)",ptr,risc,unroll,
102 size);
103 init=0;
104 }
105 return(buf);
106 }
107
108 61
109void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output, 62void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output,
110 DES_key_schedule *ks, int enc) 63 DES_key_schedule *ks, int enc)
diff --git a/src/lib/libcrypto/des/enc_read.c b/src/lib/libcrypto/des/enc_read.c
index c70fb686b8..e7da2ec66b 100644
--- a/src/lib/libcrypto/des/enc_read.c
+++ b/src/lib/libcrypto/des/enc_read.c
@@ -147,7 +147,11 @@ int DES_enc_read(int fd, void *buf, int len, DES_key_schedule *sched,
147 /* first - get the length */ 147 /* first - get the length */
148 while (net_num < HDRSIZE) 148 while (net_num < HDRSIZE)
149 { 149 {
150#ifndef _WIN32
150 i=read(fd,(void *)&(net[net_num]),HDRSIZE-net_num); 151 i=read(fd,(void *)&(net[net_num]),HDRSIZE-net_num);
152#else
153 i=_read(fd,(void *)&(net[net_num]),HDRSIZE-net_num);
154#endif
151#ifdef EINTR 155#ifdef EINTR
152 if ((i == -1) && (errno == EINTR)) continue; 156 if ((i == -1) && (errno == EINTR)) continue;
153#endif 157#endif
diff --git a/src/lib/libcrypto/des/enc_writ.c b/src/lib/libcrypto/des/enc_writ.c
index af5b8c2349..c2f032c9a6 100644
--- a/src/lib/libcrypto/des/enc_writ.c
+++ b/src/lib/libcrypto/des/enc_writ.c
@@ -153,7 +153,11 @@ int DES_enc_write(int fd, const void *_buf, int len,
153 { 153 {
154 /* eay 26/08/92 I was not doing writing from where we 154 /* eay 26/08/92 I was not doing writing from where we
155 * got up to. */ 155 * got up to. */
156#ifndef _WIN32
156 i=write(fd,(void *)&(outbuf[j]),outnum-j); 157 i=write(fd,(void *)&(outbuf[j]),outnum-j);
158#else
159 i=_write(fd,(void *)&(outbuf[j]),outnum-j);
160#endif
157 if (i == -1) 161 if (i == -1)
158 { 162 {
159#ifdef EINTR 163#ifdef EINTR
diff --git a/src/lib/libcrypto/des/set_key.c b/src/lib/libcrypto/des/set_key.c
index a43ef3c881..c0806d593c 100644
--- a/src/lib/libcrypto/des/set_key.c
+++ b/src/lib/libcrypto/des/set_key.c
@@ -64,6 +64,10 @@
64 * 1.0 First working version 64 * 1.0 First working version
65 */ 65 */
66#include "des_locl.h" 66#include "des_locl.h"
67#ifdef OPENSSL_FIPS
68#include <openssl/fips.h>
69#endif
70
67 71
68OPENSSL_IMPLEMENT_GLOBAL(int,DES_check_key); /* defaults to false */ 72OPENSSL_IMPLEMENT_GLOBAL(int,DES_check_key); /* defaults to false */
69 73
@@ -349,6 +353,10 @@ void DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule)
349 k = &schedule->ks->deslong[0]; 353 k = &schedule->ks->deslong[0];
350 in = &(*key)[0]; 354 in = &(*key)[0];
351 355
356#ifdef OPENSSL_FIPS
357 FIPS_selftest_check();
358#endif
359
352 c2l(in,c); 360 c2l(in,c);
353 c2l(in,d); 361 c2l(in,d);
354 362
@@ -405,3 +413,4 @@ void des_fixup_key_parity(des_cblock *key)
405 des_set_odd_parity(key); 413 des_set_odd_parity(key);
406 } 414 }
407*/ 415*/
416
diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h
index ccdf35ae1c..10475ac4b3 100644
--- a/src/lib/libcrypto/dh/dh.h
+++ b/src/lib/libcrypto/dh/dh.h
@@ -77,6 +77,8 @@
77# define OPENSSL_DH_MAX_MODULUS_BITS 10000 77# define OPENSSL_DH_MAX_MODULUS_BITS 10000
78#endif 78#endif
79 79
80#define OPENSSL_DH_FIPS_MIN_MODULUS_BITS 1024
81
80#define DH_FLAG_CACHE_MONT_P 0x01 82#define DH_FLAG_CACHE_MONT_P 0x01
81#define DH_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DH 83#define DH_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DH
82 * implementation now uses constant time 84 * implementation now uses constant time
@@ -167,6 +169,11 @@ struct dh_st
167 169
168const DH_METHOD *DH_OpenSSL(void); 170const DH_METHOD *DH_OpenSSL(void);
169 171
172#ifdef OPENSSL_FIPS
173DH * FIPS_dh_new(void);
174void FIPS_dh_free(DH *dh);
175#endif
176
170void DH_set_default_method(const DH_METHOD *meth); 177void DH_set_default_method(const DH_METHOD *meth);
171const DH_METHOD *DH_get_default_method(void); 178const DH_METHOD *DH_get_default_method(void);
172int DH_set_method(DH *dh, const DH_METHOD *meth); 179int DH_set_method(DH *dh, const DH_METHOD *meth);
@@ -218,6 +225,9 @@ void ERR_load_DH_strings(void);
218#define DH_F_DHPARAMS_PRINT 100 225#define DH_F_DHPARAMS_PRINT 100
219#define DH_F_DHPARAMS_PRINT_FP 101 226#define DH_F_DHPARAMS_PRINT_FP 101
220#define DH_F_DH_BUILTIN_GENPARAMS 106 227#define DH_F_DH_BUILTIN_GENPARAMS 106
228#define DH_F_DH_COMPUTE_KEY 107
229#define DH_F_DH_GENERATE_KEY 108
230#define DH_F_DH_GENERATE_PARAMETERS 109
221#define DH_F_DH_NEW_METHOD 105 231#define DH_F_DH_NEW_METHOD 105
222#define DH_F_GENERATE_KEY 103 232#define DH_F_GENERATE_KEY 103
223#define DH_F_GENERATE_PARAMETERS 104 233#define DH_F_GENERATE_PARAMETERS 104
@@ -225,6 +235,7 @@ void ERR_load_DH_strings(void);
225/* Reason codes. */ 235/* Reason codes. */
226#define DH_R_BAD_GENERATOR 101 236#define DH_R_BAD_GENERATOR 101
227#define DH_R_INVALID_PUBKEY 102 237#define DH_R_INVALID_PUBKEY 102
238#define DH_R_KEY_SIZE_TOO_SMALL 104
228#define DH_R_MODULUS_TOO_LARGE 103 239#define DH_R_MODULUS_TOO_LARGE 103
229#define DH_R_NO_PRIVATE_VALUE 100 240#define DH_R_NO_PRIVATE_VALUE 100
230 241
diff --git a/src/lib/libcrypto/dh/dh_asn1.c b/src/lib/libcrypto/dh/dh_asn1.c
index 769b5b68c5..76740af2bd 100644
--- a/src/lib/libcrypto/dh/dh_asn1.c
+++ b/src/lib/libcrypto/dh/dh_asn1.c
@@ -1,5 +1,5 @@
1/* dh_asn1.c */ 1/* dh_asn1.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c
index b846913004..316cb9221d 100644
--- a/src/lib/libcrypto/dh/dh_check.c
+++ b/src/lib/libcrypto/dh/dh_check.c
@@ -70,6 +70,8 @@
70 * should hold. 70 * should hold.
71 */ 71 */
72 72
73#ifndef OPENSSL_FIPS
74
73int DH_check(const DH *dh, int *ret) 75int DH_check(const DH *dh, int *ret)
74 { 76 {
75 int ok=0; 77 int ok=0;
@@ -140,3 +142,5 @@ err:
140 if (q != NULL) BN_free(q); 142 if (q != NULL) BN_free(q);
141 return(ok); 143 return(ok);
142 } 144 }
145
146#endif
diff --git a/src/lib/libcrypto/dh/dh_err.c b/src/lib/libcrypto/dh/dh_err.c
index a2d8196ecb..13263c81c1 100644
--- a/src/lib/libcrypto/dh/dh_err.c
+++ b/src/lib/libcrypto/dh/dh_err.c
@@ -1,6 +1,6 @@
1/* crypto/dh/dh_err.c */ 1/* crypto/dh/dh_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -74,6 +74,9 @@ static ERR_STRING_DATA DH_str_functs[]=
74{ERR_FUNC(DH_F_DHPARAMS_PRINT), "DHparams_print"}, 74{ERR_FUNC(DH_F_DHPARAMS_PRINT), "DHparams_print"},
75{ERR_FUNC(DH_F_DHPARAMS_PRINT_FP), "DHparams_print_fp"}, 75{ERR_FUNC(DH_F_DHPARAMS_PRINT_FP), "DHparams_print_fp"},
76{ERR_FUNC(DH_F_DH_BUILTIN_GENPARAMS), "DH_BUILTIN_GENPARAMS"}, 76{ERR_FUNC(DH_F_DH_BUILTIN_GENPARAMS), "DH_BUILTIN_GENPARAMS"},
77{ERR_FUNC(DH_F_DH_COMPUTE_KEY), "DH_compute_key"},
78{ERR_FUNC(DH_F_DH_GENERATE_KEY), "DH_generate_key"},
79{ERR_FUNC(DH_F_DH_GENERATE_PARAMETERS), "DH_generate_parameters"},
77{ERR_FUNC(DH_F_DH_NEW_METHOD), "DH_new_method"}, 80{ERR_FUNC(DH_F_DH_NEW_METHOD), "DH_new_method"},
78{ERR_FUNC(DH_F_GENERATE_KEY), "GENERATE_KEY"}, 81{ERR_FUNC(DH_F_GENERATE_KEY), "GENERATE_KEY"},
79{ERR_FUNC(DH_F_GENERATE_PARAMETERS), "GENERATE_PARAMETERS"}, 82{ERR_FUNC(DH_F_GENERATE_PARAMETERS), "GENERATE_PARAMETERS"},
@@ -84,6 +87,7 @@ static ERR_STRING_DATA DH_str_reasons[]=
84 { 87 {
85{ERR_REASON(DH_R_BAD_GENERATOR) ,"bad generator"}, 88{ERR_REASON(DH_R_BAD_GENERATOR) ,"bad generator"},
86{ERR_REASON(DH_R_INVALID_PUBKEY) ,"invalid public key"}, 89{ERR_REASON(DH_R_INVALID_PUBKEY) ,"invalid public key"},
90{ERR_REASON(DH_R_KEY_SIZE_TOO_SMALL) ,"key size too small"},
87{ERR_REASON(DH_R_MODULUS_TOO_LARGE) ,"modulus too large"}, 91{ERR_REASON(DH_R_MODULUS_TOO_LARGE) ,"modulus too large"},
88{ERR_REASON(DH_R_NO_PRIVATE_VALUE) ,"no private value"}, 92{ERR_REASON(DH_R_NO_PRIVATE_VALUE) ,"no private value"},
89{0,NULL} 93{0,NULL}
diff --git a/src/lib/libcrypto/dh/dh_gen.c b/src/lib/libcrypto/dh/dh_gen.c
index cfd5b11868..999e1deb40 100644
--- a/src/lib/libcrypto/dh/dh_gen.c
+++ b/src/lib/libcrypto/dh/dh_gen.c
@@ -66,6 +66,8 @@
66#include <openssl/bn.h> 66#include <openssl/bn.h>
67#include <openssl/dh.h> 67#include <openssl/dh.h>
68 68
69#ifndef OPENSSL_FIPS
70
69static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb); 71static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb);
70 72
71int DH_generate_parameters_ex(DH *ret, int prime_len, int generator, BN_GENCB *cb) 73int DH_generate_parameters_ex(DH *ret, int prime_len, int generator, BN_GENCB *cb)
@@ -173,3 +175,5 @@ err:
173 } 175 }
174 return ok; 176 return ok;
175 } 177 }
178
179#endif
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c
index e7db440342..79dd331863 100644
--- a/src/lib/libcrypto/dh/dh_key.c
+++ b/src/lib/libcrypto/dh/dh_key.c
@@ -62,6 +62,8 @@
62#include <openssl/rand.h> 62#include <openssl/rand.h>
63#include <openssl/dh.h> 63#include <openssl/dh.h>
64 64
65#ifndef OPENSSL_FIPS
66
65static int generate_key(DH *dh); 67static int generate_key(DH *dh);
66static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); 68static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
67static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, 69static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
@@ -261,3 +263,5 @@ static int dh_finish(DH *dh)
261 BN_MONT_CTX_free(dh->method_mont_p); 263 BN_MONT_CTX_free(dh->method_mont_p);
262 return(1); 264 return(1);
263 } 265 }
266
267#endif
diff --git a/src/lib/libcrypto/dsa/dsa.h b/src/lib/libcrypto/dsa/dsa.h
index 3a8fe5b56b..702c50d6dc 100644
--- a/src/lib/libcrypto/dsa/dsa.h
+++ b/src/lib/libcrypto/dsa/dsa.h
@@ -88,6 +88,8 @@
88# define OPENSSL_DSA_MAX_MODULUS_BITS 10000 88# define OPENSSL_DSA_MAX_MODULUS_BITS 10000
89#endif 89#endif
90 90
91#define OPENSSL_DSA_FIPS_MIN_MODULUS_BITS 1024
92
91#define DSA_FLAG_CACHE_MONT_P 0x01 93#define DSA_FLAG_CACHE_MONT_P 0x01
92#define DSA_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DSA 94#define DSA_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DSA
93 * implementation now uses constant time 95 * implementation now uses constant time
@@ -97,6 +99,25 @@
97 * be used for all exponents. 99 * be used for all exponents.
98 */ 100 */
99 101
102/* If this flag is set the DSA method is FIPS compliant and can be used
103 * in FIPS mode. This is set in the validated module method. If an
104 * application sets this flag in its own methods it is its reposibility
105 * to ensure the result is compliant.
106 */
107
108#define DSA_FLAG_FIPS_METHOD 0x0400
109
110/* If this flag is set the operations normally disabled in FIPS mode are
111 * permitted it is then the applications responsibility to ensure that the
112 * usage is compliant.
113 */
114
115#define DSA_FLAG_NON_FIPS_ALLOW 0x0400
116
117#ifdef OPENSSL_FIPS
118#define FIPS_DSA_SIZE_T int
119#endif
120
100#ifdef __cplusplus 121#ifdef __cplusplus
101extern "C" { 122extern "C" {
102#endif 123#endif
@@ -189,6 +210,11 @@ void DSA_set_default_method(const DSA_METHOD *);
189const DSA_METHOD *DSA_get_default_method(void); 210const DSA_METHOD *DSA_get_default_method(void);
190int DSA_set_method(DSA *dsa, const DSA_METHOD *); 211int DSA_set_method(DSA *dsa, const DSA_METHOD *);
191 212
213#ifdef OPENSSL_FIPS
214DSA * FIPS_dsa_new(void);
215void FIPS_dsa_free (DSA *r);
216#endif
217
192DSA * DSA_new(void); 218DSA * DSA_new(void);
193DSA * DSA_new_method(ENGINE *engine); 219DSA * DSA_new_method(ENGINE *engine);
194void DSA_free (DSA *r); 220void DSA_free (DSA *r);
@@ -249,6 +275,11 @@ int DSA_print_fp(FILE *bp, const DSA *x, int off);
249DH *DSA_dup_DH(const DSA *r); 275DH *DSA_dup_DH(const DSA *r);
250#endif 276#endif
251 277
278#ifdef OPENSSL_FIPS
279int FIPS_dsa_sig_encode(unsigned char *out, DSA_SIG *sig);
280int FIPS_dsa_sig_decode(DSA_SIG *sig, const unsigned char *in, int inlen);
281#endif
282
252/* BEGIN ERROR CODES */ 283/* BEGIN ERROR CODES */
253/* The following lines are auto generated by the script mkerr.pl. Any changes 284/* The following lines are auto generated by the script mkerr.pl. Any changes
254 * made after this point may be overwritten when the script is next run. 285 * made after this point may be overwritten when the script is next run.
@@ -261,11 +292,16 @@ void ERR_load_DSA_strings(void);
261#define DSA_F_D2I_DSA_SIG 110 292#define DSA_F_D2I_DSA_SIG 110
262#define DSA_F_DSAPARAMS_PRINT 100 293#define DSA_F_DSAPARAMS_PRINT 100
263#define DSA_F_DSAPARAMS_PRINT_FP 101 294#define DSA_F_DSAPARAMS_PRINT_FP 101
295#define DSA_F_DSA_BUILTIN_KEYGEN 119
296#define DSA_F_DSA_BUILTIN_PARAMGEN 118
264#define DSA_F_DSA_DO_SIGN 112 297#define DSA_F_DSA_DO_SIGN 112
265#define DSA_F_DSA_DO_VERIFY 113 298#define DSA_F_DSA_DO_VERIFY 113
299#define DSA_F_DSA_GENERATE_PARAMETERS 117
266#define DSA_F_DSA_NEW_METHOD 103 300#define DSA_F_DSA_NEW_METHOD 103
267#define DSA_F_DSA_PRINT 104 301#define DSA_F_DSA_PRINT 104
268#define DSA_F_DSA_PRINT_FP 105 302#define DSA_F_DSA_PRINT_FP 105
303#define DSA_F_DSA_SET_DEFAULT_METHOD 115
304#define DSA_F_DSA_SET_METHOD 116
269#define DSA_F_DSA_SIGN 106 305#define DSA_F_DSA_SIGN 106
270#define DSA_F_DSA_SIGN_SETUP 107 306#define DSA_F_DSA_SIGN_SETUP 107
271#define DSA_F_DSA_SIG_NEW 109 307#define DSA_F_DSA_SIG_NEW 109
@@ -276,8 +312,11 @@ void ERR_load_DSA_strings(void);
276/* Reason codes. */ 312/* Reason codes. */
277#define DSA_R_BAD_Q_VALUE 102 313#define DSA_R_BAD_Q_VALUE 102
278#define DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 100 314#define DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 100
315#define DSA_R_KEY_SIZE_TOO_SMALL 106
279#define DSA_R_MISSING_PARAMETERS 101 316#define DSA_R_MISSING_PARAMETERS 101
280#define DSA_R_MODULUS_TOO_LARGE 103 317#define DSA_R_MODULUS_TOO_LARGE 103
318#define DSA_R_NON_FIPS_METHOD 104
319#define DSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE 105
281 320
282#ifdef __cplusplus 321#ifdef __cplusplus
283} 322}
diff --git a/src/lib/libcrypto/dsa/dsa_asn1.c b/src/lib/libcrypto/dsa/dsa_asn1.c
index 23fce555aa..0645facb4b 100644
--- a/src/lib/libcrypto/dsa/dsa_asn1.c
+++ b/src/lib/libcrypto/dsa/dsa_asn1.c
@@ -1,5 +1,5 @@
1/* dsa_asn1.c */ 1/* dsa_asn1.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -61,6 +61,11 @@
61#include <openssl/dsa.h> 61#include <openssl/dsa.h>
62#include <openssl/asn1.h> 62#include <openssl/asn1.h>
63#include <openssl/asn1t.h> 63#include <openssl/asn1t.h>
64#include <openssl/bn.h>
65#ifdef OPENSSL_FIPS
66#include <openssl/fips.h>
67#endif
68
64 69
65/* Override the default new methods */ 70/* Override the default new methods */
66static int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) 71static int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
@@ -83,7 +88,7 @@ ASN1_SEQUENCE_cb(DSA_SIG, sig_cb) = {
83 ASN1_SIMPLE(DSA_SIG, s, CBIGNUM) 88 ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
84} ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG) 89} ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG)
85 90
86IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG) 91IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA_SIG,DSA_SIG,DSA_SIG)
87 92
88/* Override the default free and new methods */ 93/* Override the default free and new methods */
89static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) 94static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
@@ -138,3 +143,76 @@ ASN1_CHOICE_cb(DSAPublicKey, dsa_cb) = {
138} ASN1_CHOICE_END_cb(DSA, DSAPublicKey, write_params) 143} ASN1_CHOICE_END_cb(DSA, DSAPublicKey, write_params)
139 144
140IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey) 145IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
146
147int DSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig,
148 unsigned int *siglen, DSA *dsa)
149 {
150 DSA_SIG *s;
151#ifdef OPENSSL_FIPS
152 if(FIPS_mode() && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
153 {
154 DSAerr(DSA_F_DSA_SIGN, DSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
155 return 0;
156 }
157#endif
158 s=DSA_do_sign(dgst,dlen,dsa);
159 if (s == NULL)
160 {
161 *siglen=0;
162 return(0);
163 }
164 *siglen=i2d_DSA_SIG(s,&sig);
165 DSA_SIG_free(s);
166 return(1);
167 }
168
169int DSA_size(const DSA *r)
170 {
171 int ret,i;
172 ASN1_INTEGER bs;
173 unsigned char buf[4]; /* 4 bytes looks really small.
174 However, i2d_ASN1_INTEGER() will not look
175 beyond the first byte, as long as the second
176 parameter is NULL. */
177
178 i=BN_num_bits(r->q);
179 bs.length=(i+7)/8;
180 bs.data=buf;
181 bs.type=V_ASN1_INTEGER;
182 /* If the top bit is set the asn1 encoding is 1 larger. */
183 buf[0]=0xff;
184
185 i=i2d_ASN1_INTEGER(&bs,NULL);
186 i+=i; /* r and s */
187 ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
188 return(ret);
189 }
190
191/* data has already been hashed (probably with SHA or SHA-1). */
192/* returns
193 * 1: correct signature
194 * 0: incorrect signature
195 * -1: error
196 */
197int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
198 const unsigned char *sigbuf, int siglen, DSA *dsa)
199 {
200 DSA_SIG *s;
201 int ret=-1;
202#ifdef OPENSSL_FIPS
203 if(FIPS_mode() && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
204 {
205 DSAerr(DSA_F_DSA_VERIFY, DSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
206 return 0;
207 }
208#endif
209
210 s = DSA_SIG_new();
211 if (s == NULL) return(ret);
212 if (d2i_DSA_SIG(&s,&sigbuf,siglen) == NULL) goto err;
213 ret=DSA_do_verify(dgst,dgst_len,s,dsa);
214err:
215 DSA_SIG_free(s);
216 return(ret);
217 }
218
diff --git a/src/lib/libcrypto/dsa/dsa_err.c b/src/lib/libcrypto/dsa/dsa_err.c
index 768711994b..872839af94 100644
--- a/src/lib/libcrypto/dsa/dsa_err.c
+++ b/src/lib/libcrypto/dsa/dsa_err.c
@@ -1,6 +1,6 @@
1/* crypto/dsa/dsa_err.c */ 1/* crypto/dsa/dsa_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -73,11 +73,16 @@ static ERR_STRING_DATA DSA_str_functs[]=
73{ERR_FUNC(DSA_F_D2I_DSA_SIG), "d2i_DSA_SIG"}, 73{ERR_FUNC(DSA_F_D2I_DSA_SIG), "d2i_DSA_SIG"},
74{ERR_FUNC(DSA_F_DSAPARAMS_PRINT), "DSAparams_print"}, 74{ERR_FUNC(DSA_F_DSAPARAMS_PRINT), "DSAparams_print"},
75{ERR_FUNC(DSA_F_DSAPARAMS_PRINT_FP), "DSAparams_print_fp"}, 75{ERR_FUNC(DSA_F_DSAPARAMS_PRINT_FP), "DSAparams_print_fp"},
76{ERR_FUNC(DSA_F_DSA_BUILTIN_KEYGEN), "DSA_BUILTIN_KEYGEN"},
77{ERR_FUNC(DSA_F_DSA_BUILTIN_PARAMGEN), "DSA_BUILTIN_PARAMGEN"},
76{ERR_FUNC(DSA_F_DSA_DO_SIGN), "DSA_do_sign"}, 78{ERR_FUNC(DSA_F_DSA_DO_SIGN), "DSA_do_sign"},
77{ERR_FUNC(DSA_F_DSA_DO_VERIFY), "DSA_do_verify"}, 79{ERR_FUNC(DSA_F_DSA_DO_VERIFY), "DSA_do_verify"},
80{ERR_FUNC(DSA_F_DSA_GENERATE_PARAMETERS), "DSA_generate_parameters"},
78{ERR_FUNC(DSA_F_DSA_NEW_METHOD), "DSA_new_method"}, 81{ERR_FUNC(DSA_F_DSA_NEW_METHOD), "DSA_new_method"},
79{ERR_FUNC(DSA_F_DSA_PRINT), "DSA_print"}, 82{ERR_FUNC(DSA_F_DSA_PRINT), "DSA_print"},
80{ERR_FUNC(DSA_F_DSA_PRINT_FP), "DSA_print_fp"}, 83{ERR_FUNC(DSA_F_DSA_PRINT_FP), "DSA_print_fp"},
84{ERR_FUNC(DSA_F_DSA_SET_DEFAULT_METHOD), "DSA_set_default_method"},
85{ERR_FUNC(DSA_F_DSA_SET_METHOD), "DSA_set_method"},
81{ERR_FUNC(DSA_F_DSA_SIGN), "DSA_sign"}, 86{ERR_FUNC(DSA_F_DSA_SIGN), "DSA_sign"},
82{ERR_FUNC(DSA_F_DSA_SIGN_SETUP), "DSA_sign_setup"}, 87{ERR_FUNC(DSA_F_DSA_SIGN_SETUP), "DSA_sign_setup"},
83{ERR_FUNC(DSA_F_DSA_SIG_NEW), "DSA_SIG_new"}, 88{ERR_FUNC(DSA_F_DSA_SIG_NEW), "DSA_SIG_new"},
@@ -91,8 +96,11 @@ static ERR_STRING_DATA DSA_str_reasons[]=
91 { 96 {
92{ERR_REASON(DSA_R_BAD_Q_VALUE) ,"bad q value"}, 97{ERR_REASON(DSA_R_BAD_Q_VALUE) ,"bad q value"},
93{ERR_REASON(DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE),"data too large for key size"}, 98{ERR_REASON(DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE),"data too large for key size"},
99{ERR_REASON(DSA_R_KEY_SIZE_TOO_SMALL) ,"key size too small"},
94{ERR_REASON(DSA_R_MISSING_PARAMETERS) ,"missing parameters"}, 100{ERR_REASON(DSA_R_MISSING_PARAMETERS) ,"missing parameters"},
95{ERR_REASON(DSA_R_MODULUS_TOO_LARGE) ,"modulus too large"}, 101{ERR_REASON(DSA_R_MODULUS_TOO_LARGE) ,"modulus too large"},
102{ERR_REASON(DSA_R_NON_FIPS_METHOD) ,"non fips method"},
103{ERR_REASON(DSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE),"operation not allowed in fips mode"},
96{0,NULL} 104{0,NULL}
97 }; 105 };
98 106
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c
index ca0b86a6cf..6f1728e3cf 100644
--- a/src/lib/libcrypto/dsa/dsa_gen.c
+++ b/src/lib/libcrypto/dsa/dsa_gen.c
@@ -82,6 +82,8 @@
82#include <openssl/rand.h> 82#include <openssl/rand.h>
83#include <openssl/sha.h> 83#include <openssl/sha.h>
84 84
85#ifndef OPENSSL_FIPS
86
85static int dsa_builtin_paramgen(DSA *ret, int bits, 87static int dsa_builtin_paramgen(DSA *ret, int bits,
86 unsigned char *seed_in, int seed_len, 88 unsigned char *seed_in, int seed_len,
87 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb); 89 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
@@ -320,3 +322,4 @@ err:
320 return ok; 322 return ok;
321 } 323 }
322#endif 324#endif
325#endif
diff --git a/src/lib/libcrypto/dsa/dsa_key.c b/src/lib/libcrypto/dsa/dsa_key.c
index c4aa86bc6d..5e39124230 100644
--- a/src/lib/libcrypto/dsa/dsa_key.c
+++ b/src/lib/libcrypto/dsa/dsa_key.c
@@ -64,6 +64,8 @@
64#include <openssl/dsa.h> 64#include <openssl/dsa.h>
65#include <openssl/rand.h> 65#include <openssl/rand.h>
66 66
67#ifndef OPENSSL_FIPS
68
67static int dsa_builtin_keygen(DSA *dsa); 69static int dsa_builtin_keygen(DSA *dsa);
68 70
69int DSA_generate_key(DSA *dsa) 71int DSA_generate_key(DSA *dsa)
@@ -126,3 +128,5 @@ err:
126 return(ok); 128 return(ok);
127 } 129 }
128#endif 130#endif
131
132#endif
diff --git a/src/lib/libcrypto/dsa/dsa_lib.c b/src/lib/libcrypto/dsa/dsa_lib.c
index e9b75902db..7ac9dc8c89 100644
--- a/src/lib/libcrypto/dsa/dsa_lib.c
+++ b/src/lib/libcrypto/dsa/dsa_lib.c
@@ -76,6 +76,14 @@ static const DSA_METHOD *default_DSA_method = NULL;
76 76
77void DSA_set_default_method(const DSA_METHOD *meth) 77void DSA_set_default_method(const DSA_METHOD *meth)
78 { 78 {
79#ifdef OPENSSL_FIPS
80 if (FIPS_mode() && !(meth->flags & DSA_FLAG_FIPS_METHOD))
81 {
82 DSAerr(DSA_F_DSA_SET_DEFAULT_METHOD, DSA_R_NON_FIPS_METHOD);
83 return;
84 }
85#endif
86
79 default_DSA_method = meth; 87 default_DSA_method = meth;
80 } 88 }
81 89
@@ -96,6 +104,13 @@ int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
96 /* NB: The caller is specifically setting a method, so it's not up to us 104 /* NB: The caller is specifically setting a method, so it's not up to us
97 * to deal with which ENGINE it comes from. */ 105 * to deal with which ENGINE it comes from. */
98 const DSA_METHOD *mtmp; 106 const DSA_METHOD *mtmp;
107#ifdef OPENSSL_FIPS
108 if (FIPS_mode() && !(meth->flags & DSA_FLAG_FIPS_METHOD))
109 {
110 DSAerr(DSA_F_DSA_SET_METHOD, DSA_R_NON_FIPS_METHOD);
111 return 0;
112 }
113#endif
99 mtmp = dsa->meth; 114 mtmp = dsa->meth;
100 if (mtmp->finish) mtmp->finish(dsa); 115 if (mtmp->finish) mtmp->finish(dsa);
101#ifndef OPENSSL_NO_ENGINE 116#ifndef OPENSSL_NO_ENGINE
@@ -147,6 +162,18 @@ DSA *DSA_new_method(ENGINE *engine)
147 } 162 }
148 } 163 }
149#endif 164#endif
165#ifdef OPENSSL_FIPS
166 if (FIPS_mode() && !(ret->meth->flags & DSA_FLAG_FIPS_METHOD))
167 {
168 DSAerr(DSA_F_DSA_NEW_METHOD, DSA_R_NON_FIPS_METHOD);
169#ifndef OPENSSL_NO_ENGINE
170 if (ret->engine)
171 ENGINE_finish(ret->engine);
172#endif
173 OPENSSL_free(ret);
174 return NULL;
175 }
176#endif
150 177
151 ret->pad=0; 178 ret->pad=0;
152 ret->version=0; 179 ret->version=0;
@@ -233,28 +260,6 @@ int DSA_up_ref(DSA *r)
233 return ((i > 1) ? 1 : 0); 260 return ((i > 1) ? 1 : 0);
234 } 261 }
235 262
236int DSA_size(const DSA *r)
237 {
238 int ret,i;
239 ASN1_INTEGER bs;
240 unsigned char buf[4]; /* 4 bytes looks really small.
241 However, i2d_ASN1_INTEGER() will not look
242 beyond the first byte, as long as the second
243 parameter is NULL. */
244
245 i=BN_num_bits(r->q);
246 bs.length=(i+7)/8;
247 bs.data=buf;
248 bs.type=V_ASN1_INTEGER;
249 /* If the top bit is set the asn1 encoding is 1 larger. */
250 buf[0]=0xff;
251
252 i=i2d_ASN1_INTEGER(&bs,NULL);
253 i+=i; /* r and s */
254 ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
255 return(ret);
256 }
257
258int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 263int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
259 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 264 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
260 { 265 {
diff --git a/src/lib/libcrypto/dsa/dsa_ossl.c b/src/lib/libcrypto/dsa/dsa_ossl.c
index 75ff7cc4af..412cf1d88b 100644
--- a/src/lib/libcrypto/dsa/dsa_ossl.c
+++ b/src/lib/libcrypto/dsa/dsa_ossl.c
@@ -65,6 +65,8 @@
65#include <openssl/rand.h> 65#include <openssl/rand.h>
66#include <openssl/asn1.h> 66#include <openssl/asn1.h>
67 67
68#ifndef OPENSSL_FIPS
69
68static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); 70static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
69static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp); 71static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp);
70static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, 72static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
@@ -391,3 +393,4 @@ static int dsa_finish(DSA *dsa)
391 return(1); 393 return(1);
392} 394}
393 395
396#endif
diff --git a/src/lib/libcrypto/dsa/dsa_sign.c b/src/lib/libcrypto/dsa/dsa_sign.c
index 89205026f0..4cfbbe57a8 100644
--- a/src/lib/libcrypto/dsa/dsa_sign.c
+++ b/src/lib/libcrypto/dsa/dsa_sign.c
@@ -64,29 +64,32 @@
64#include <openssl/dsa.h> 64#include <openssl/dsa.h>
65#include <openssl/rand.h> 65#include <openssl/rand.h>
66#include <openssl/asn1.h> 66#include <openssl/asn1.h>
67#ifdef OPENSSL_FIPS
68#include <openssl/fips.h>
69#endif
67 70
68DSA_SIG * DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
69 {
70 return dsa->meth->dsa_do_sign(dgst, dlen, dsa);
71 }
72 71
73int DSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig, 72DSA_SIG * DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
74 unsigned int *siglen, DSA *dsa)
75 { 73 {
76 DSA_SIG *s; 74#ifdef OPENSSL_FIPS
77 s=DSA_do_sign(dgst,dlen,dsa); 75 if(FIPS_mode() && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
78 if (s == NULL)
79 { 76 {
80 *siglen=0; 77 DSAerr(DSA_F_DSA_DO_SIGN, DSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
81 return(0); 78 return NULL;
82 } 79 }
83 *siglen=i2d_DSA_SIG(s,&sig); 80#endif
84 DSA_SIG_free(s); 81 return dsa->meth->dsa_do_sign(dgst, dlen, dsa);
85 return(1);
86 } 82 }
87 83
88int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) 84int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
89 { 85 {
86#ifdef OPENSSL_FIPS
87 if(FIPS_mode() && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
88 {
89 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
90 return 0;
91 }
92#endif
90 return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp); 93 return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp);
91 } 94 }
92 95
diff --git a/src/lib/libcrypto/dsa/dsa_vrf.c b/src/lib/libcrypto/dsa/dsa_vrf.c
index c4aeddd056..c75e423048 100644
--- a/src/lib/libcrypto/dsa/dsa_vrf.c
+++ b/src/lib/libcrypto/dsa/dsa_vrf.c
@@ -64,31 +64,21 @@
64#include <openssl/dsa.h> 64#include <openssl/dsa.h>
65#include <openssl/rand.h> 65#include <openssl/rand.h>
66#include <openssl/asn1.h> 66#include <openssl/asn1.h>
67#ifdef OPENSSL_FIPS
68#include <openssl/fips.h>
69#endif
70
67#include <openssl/asn1_mac.h> 71#include <openssl/asn1_mac.h>
68 72
69int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, 73int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
70 DSA *dsa) 74 DSA *dsa)
71 { 75 {
76#ifdef OPENSSL_FIPS
77 if(FIPS_mode() && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
78 {
79 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
80 return 0;
81 }
82#endif
72 return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa); 83 return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
73 } 84 }
74
75/* data has already been hashed (probably with SHA or SHA-1). */
76/* returns
77 * 1: correct signature
78 * 0: incorrect signature
79 * -1: error
80 */
81int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
82 const unsigned char *sigbuf, int siglen, DSA *dsa)
83 {
84 DSA_SIG *s;
85 int ret=-1;
86
87 s = DSA_SIG_new();
88 if (s == NULL) return(ret);
89 if (d2i_DSA_SIG(&s,&sigbuf,siglen) == NULL) goto err;
90 ret=DSA_do_verify(dgst,dgst_len,s,dsa);
91err:
92 DSA_SIG_free(s);
93 return(ret);
94 }
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c
index 3d6c900b95..12fb0e6d6d 100644
--- a/src/lib/libcrypto/ec/ec_key.c
+++ b/src/lib/libcrypto/ec/ec_key.c
@@ -296,7 +296,7 @@ int EC_KEY_check_key(const EC_KEY *eckey)
296 { 296 {
297 int ok = 0; 297 int ok = 0;
298 BN_CTX *ctx = NULL; 298 BN_CTX *ctx = NULL;
299 BIGNUM *order = NULL; 299 const BIGNUM *order = NULL;
300 EC_POINT *point = NULL; 300 EC_POINT *point = NULL;
301 301
302 if (!eckey || !eckey->group || !eckey->pub_key) 302 if (!eckey || !eckey->group || !eckey->pub_key)
@@ -307,8 +307,6 @@ int EC_KEY_check_key(const EC_KEY *eckey)
307 307
308 if ((ctx = BN_CTX_new()) == NULL) 308 if ((ctx = BN_CTX_new()) == NULL)
309 goto err; 309 goto err;
310 if ((order = BN_new()) == NULL)
311 goto err;
312 if ((point = EC_POINT_new(eckey->group)) == NULL) 310 if ((point = EC_POINT_new(eckey->group)) == NULL)
313 goto err; 311 goto err;
314 312
@@ -319,17 +317,13 @@ int EC_KEY_check_key(const EC_KEY *eckey)
319 goto err; 317 goto err;
320 } 318 }
321 /* testing whether pub_key * order is the point at infinity */ 319 /* testing whether pub_key * order is the point at infinity */
322 if (!EC_GROUP_get_order(eckey->group, order, ctx)) 320 order = &eckey->group->order;
321 if (BN_is_zero(order))
323 { 322 {
324 ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER); 323 ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
325 goto err; 324 goto err;
326 } 325 }
327 if (!EC_POINT_copy(point, eckey->pub_key)) 326 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx))
328 {
329 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
330 goto err;
331 }
332 if (!EC_POINT_mul(eckey->group, point, order, NULL, NULL, ctx))
333 { 327 {
334 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB); 328 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB);
335 goto err; 329 goto err;
@@ -366,8 +360,6 @@ int EC_KEY_check_key(const EC_KEY *eckey)
366err: 360err:
367 if (ctx != NULL) 361 if (ctx != NULL)
368 BN_CTX_free(ctx); 362 BN_CTX_free(ctx);
369 if (order != NULL)
370 BN_free(order);
371 if (point != NULL) 363 if (point != NULL)
372 EC_POINT_free(point); 364 EC_POINT_free(point);
373 return(ok); 365 return(ok);
diff --git a/src/lib/libcrypto/engine/eng_all.c b/src/lib/libcrypto/engine/eng_all.c
index 8599046717..d29cd57dc2 100644
--- a/src/lib/libcrypto/engine/eng_all.c
+++ b/src/lib/libcrypto/engine/eng_all.c
@@ -107,6 +107,9 @@ void ENGINE_load_builtin_engines(void)
107#if defined(__OpenBSD__) || defined(__FreeBSD__) 107#if defined(__OpenBSD__) || defined(__FreeBSD__)
108 ENGINE_load_cryptodev(); 108 ENGINE_load_cryptodev();
109#endif 109#endif
110#if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
111 ENGINE_load_capi();
112#endif
110#endif 113#endif
111 } 114 }
112 115
diff --git a/src/lib/libcrypto/engine/eng_cnf.c b/src/lib/libcrypto/engine/eng_cnf.c
index a97e01e619..08066cea59 100644
--- a/src/lib/libcrypto/engine/eng_cnf.c
+++ b/src/lib/libcrypto/engine/eng_cnf.c
@@ -1,5 +1,5 @@
1/* eng_cnf.c */ 1/* eng_cnf.c */
2/* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Stephen Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -98,6 +98,8 @@ static int int_engine_configure(char *name, char *value, const CONF *cnf)
98 CONF_VALUE *ecmd; 98 CONF_VALUE *ecmd;
99 char *ctrlname, *ctrlvalue; 99 char *ctrlname, *ctrlvalue;
100 ENGINE *e = NULL; 100 ENGINE *e = NULL;
101 int soft = 0;
102
101 name = skip_dot(name); 103 name = skip_dot(name);
102#ifdef ENGINE_CONF_DEBUG 104#ifdef ENGINE_CONF_DEBUG
103 fprintf(stderr, "Configuring engine %s\n", name); 105 fprintf(stderr, "Configuring engine %s\n", name);
@@ -125,6 +127,8 @@ static int int_engine_configure(char *name, char *value, const CONF *cnf)
125 /* Override engine name to use */ 127 /* Override engine name to use */
126 if (!strcmp(ctrlname, "engine_id")) 128 if (!strcmp(ctrlname, "engine_id"))
127 name = ctrlvalue; 129 name = ctrlvalue;
130 else if (!strcmp(ctrlname, "soft_load"))
131 soft = 1;
128 /* Load a dynamic ENGINE */ 132 /* Load a dynamic ENGINE */
129 else if (!strcmp(ctrlname, "dynamic_path")) 133 else if (!strcmp(ctrlname, "dynamic_path"))
130 { 134 {
@@ -147,6 +151,11 @@ static int int_engine_configure(char *name, char *value, const CONF *cnf)
147 if (!e) 151 if (!e)
148 { 152 {
149 e = ENGINE_by_id(name); 153 e = ENGINE_by_id(name);
154 if (!e && soft)
155 {
156 ERR_clear_error();
157 return 1;
158 }
150 if (!e) 159 if (!e)
151 return 0; 160 return 0;
152 } 161 }
diff --git a/src/lib/libcrypto/engine/eng_err.c b/src/lib/libcrypto/engine/eng_err.c
index 369f2e22d3..574ffbb5c0 100644
--- a/src/lib/libcrypto/engine/eng_err.c
+++ b/src/lib/libcrypto/engine/eng_err.c
@@ -1,6 +1,6 @@
1/* crypto/engine/eng_err.c */ 1/* crypto/engine/eng_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2008 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -92,6 +92,7 @@ static ERR_STRING_DATA ENGINE_str_functs[]=
92{ERR_FUNC(ENGINE_F_ENGINE_LIST_REMOVE), "ENGINE_LIST_REMOVE"}, 92{ERR_FUNC(ENGINE_F_ENGINE_LIST_REMOVE), "ENGINE_LIST_REMOVE"},
93{ERR_FUNC(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY), "ENGINE_load_private_key"}, 93{ERR_FUNC(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY), "ENGINE_load_private_key"},
94{ERR_FUNC(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY), "ENGINE_load_public_key"}, 94{ERR_FUNC(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY), "ENGINE_load_public_key"},
95{ERR_FUNC(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT), "ENGINE_load_ssl_client_cert"},
95{ERR_FUNC(ENGINE_F_ENGINE_NEW), "ENGINE_new"}, 96{ERR_FUNC(ENGINE_F_ENGINE_NEW), "ENGINE_new"},
96{ERR_FUNC(ENGINE_F_ENGINE_REMOVE), "ENGINE_remove"}, 97{ERR_FUNC(ENGINE_F_ENGINE_REMOVE), "ENGINE_remove"},
97{ERR_FUNC(ENGINE_F_ENGINE_SET_DEFAULT_STRING), "ENGINE_set_default_string"}, 98{ERR_FUNC(ENGINE_F_ENGINE_SET_DEFAULT_STRING), "ENGINE_set_default_string"},
diff --git a/src/lib/libcrypto/engine/eng_int.h b/src/lib/libcrypto/engine/eng_int.h
index a5b1edebf4..a66f107a44 100644
--- a/src/lib/libcrypto/engine/eng_int.h
+++ b/src/lib/libcrypto/engine/eng_int.h
@@ -170,6 +170,8 @@ struct engine_st
170 ENGINE_LOAD_KEY_PTR load_privkey; 170 ENGINE_LOAD_KEY_PTR load_privkey;
171 ENGINE_LOAD_KEY_PTR load_pubkey; 171 ENGINE_LOAD_KEY_PTR load_pubkey;
172 172
173 ENGINE_SSL_CLIENT_CERT_PTR load_ssl_client_cert;
174
173 const ENGINE_CMD_DEFN *cmd_defns; 175 const ENGINE_CMD_DEFN *cmd_defns;
174 int flags; 176 int flags;
175 /* reference count on the structure itself */ 177 /* reference count on the structure itself */
diff --git a/src/lib/libcrypto/engine/eng_padlock.c b/src/lib/libcrypto/engine/eng_padlock.c
index 1ba9d85db4..743558ab33 100644
--- a/src/lib/libcrypto/engine/eng_padlock.c
+++ b/src/lib/libcrypto/engine/eng_padlock.c
@@ -234,8 +234,8 @@ padlock_bind_fn(ENGINE *e, const char *id)
234 return 1; 234 return 1;
235} 235}
236 236
237IMPLEMENT_DYNAMIC_CHECK_FN (); 237IMPLEMENT_DYNAMIC_CHECK_FN ()
238IMPLEMENT_DYNAMIC_BIND_FN (padlock_bind_fn); 238IMPLEMENT_DYNAMIC_BIND_FN (padlock_bind_fn)
239#endif /* DYNAMIC_ENGINE */ 239#endif /* DYNAMIC_ENGINE */
240 240
241/* ===== Here comes the "real" engine ===== */ 241/* ===== Here comes the "real" engine ===== */
diff --git a/src/lib/libcrypto/engine/eng_pkey.c b/src/lib/libcrypto/engine/eng_pkey.c
index bc8b21abec..1dfa2e3664 100644
--- a/src/lib/libcrypto/engine/eng_pkey.c
+++ b/src/lib/libcrypto/engine/eng_pkey.c
@@ -69,6 +69,13 @@ int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
69 return 1; 69 return 1;
70 } 70 }
71 71
72int ENGINE_set_load_ssl_client_cert_function(ENGINE *e,
73 ENGINE_SSL_CLIENT_CERT_PTR loadssl_f)
74 {
75 e->load_ssl_client_cert = loadssl_f;
76 return 1;
77 }
78
72ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e) 79ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
73 { 80 {
74 return e->load_privkey; 81 return e->load_privkey;
@@ -79,6 +86,11 @@ ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
79 return e->load_pubkey; 86 return e->load_pubkey;
80 } 87 }
81 88
89ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE *e)
90 {
91 return e->load_ssl_client_cert;
92 }
93
82/* API functions to load public/private keys */ 94/* API functions to load public/private keys */
83 95
84EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id, 96EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
@@ -152,3 +164,33 @@ EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
152 } 164 }
153 return pkey; 165 return pkey;
154 } 166 }
167
168int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
169 STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **ppkey,
170 STACK_OF(X509) **pother, UI_METHOD *ui_method, void *callback_data)
171 {
172
173 if(e == NULL)
174 {
175 ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
176 ERR_R_PASSED_NULL_PARAMETER);
177 return 0;
178 }
179 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
180 if(e->funct_ref == 0)
181 {
182 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
183 ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
184 ENGINE_R_NOT_INITIALISED);
185 return 0;
186 }
187 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
188 if (!e->load_ssl_client_cert)
189 {
190 ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
191 ENGINE_R_NO_LOAD_FUNCTION);
192 return 0;
193 }
194 return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
195 ui_method, callback_data);
196 }
diff --git a/src/lib/libcrypto/engine/engine.h b/src/lib/libcrypto/engine/engine.h
index 3ec59338ff..f503595ece 100644
--- a/src/lib/libcrypto/engine/engine.h
+++ b/src/lib/libcrypto/engine/engine.h
@@ -93,6 +93,8 @@
93#include <openssl/err.h> 93#include <openssl/err.h>
94#endif 94#endif
95 95
96#include <openssl/x509.h>
97
96#include <openssl/ossl_typ.h> 98#include <openssl/ossl_typ.h>
97#include <openssl/symhacks.h> 99#include <openssl/symhacks.h>
98 100
@@ -278,6 +280,9 @@ typedef int (*ENGINE_CTRL_FUNC_PTR)(ENGINE *, int, long, void *, void (*f)(void)
278/* Generic load_key function pointer */ 280/* Generic load_key function pointer */
279typedef EVP_PKEY * (*ENGINE_LOAD_KEY_PTR)(ENGINE *, const char *, 281typedef EVP_PKEY * (*ENGINE_LOAD_KEY_PTR)(ENGINE *, const char *,
280 UI_METHOD *ui_method, void *callback_data); 282 UI_METHOD *ui_method, void *callback_data);
283typedef int (*ENGINE_SSL_CLIENT_CERT_PTR)(ENGINE *, SSL *ssl,
284 STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **pkey,
285 STACK_OF(X509) **pother, UI_METHOD *ui_method, void *callback_data);
281/* These callback types are for an ENGINE's handler for cipher and digest logic. 286/* These callback types are for an ENGINE's handler for cipher and digest logic.
282 * These handlers have these prototypes; 287 * These handlers have these prototypes;
283 * int foo(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid); 288 * int foo(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid);
@@ -334,6 +339,9 @@ void ENGINE_load_ubsec(void);
334void ENGINE_load_cryptodev(void); 339void ENGINE_load_cryptodev(void);
335void ENGINE_load_padlock(void); 340void ENGINE_load_padlock(void);
336void ENGINE_load_builtin_engines(void); 341void ENGINE_load_builtin_engines(void);
342#ifndef OPENSSL_NO_CAPIENG
343void ENGINE_load_capi(void);
344#endif
337 345
338/* Get and set global flags (ENGINE_TABLE_FLAG_***) for the implementation 346/* Get and set global flags (ENGINE_TABLE_FLAG_***) for the implementation
339 * "registry" handling. */ 347 * "registry" handling. */
@@ -459,6 +467,8 @@ int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f);
459int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f); 467int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f);
460int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f); 468int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f);
461int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f); 469int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f);
470int ENGINE_set_load_ssl_client_cert_function(ENGINE *e,
471 ENGINE_SSL_CLIENT_CERT_PTR loadssl_f);
462int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f); 472int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f);
463int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f); 473int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f);
464int ENGINE_set_flags(ENGINE *e, int flags); 474int ENGINE_set_flags(ENGINE *e, int flags);
@@ -494,6 +504,7 @@ ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e);
494ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e); 504ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e);
495ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e); 505ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e);
496ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e); 506ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e);
507ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE *e);
497ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e); 508ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e);
498ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e); 509ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e);
499const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid); 510const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid);
@@ -529,6 +540,10 @@ EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
529 UI_METHOD *ui_method, void *callback_data); 540 UI_METHOD *ui_method, void *callback_data);
530EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id, 541EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
531 UI_METHOD *ui_method, void *callback_data); 542 UI_METHOD *ui_method, void *callback_data);
543int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
544 STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **ppkey,
545 STACK_OF(X509) **pother,
546 UI_METHOD *ui_method, void *callback_data);
532 547
533/* This returns a pointer for the current ENGINE structure that 548/* This returns a pointer for the current ENGINE structure that
534 * is (by default) performing any RSA operations. The value returned 549 * is (by default) performing any RSA operations. The value returned
@@ -723,6 +738,7 @@ void ERR_load_ENGINE_strings(void);
723#define ENGINE_F_ENGINE_LIST_REMOVE 121 738#define ENGINE_F_ENGINE_LIST_REMOVE 121
724#define ENGINE_F_ENGINE_LOAD_PRIVATE_KEY 150 739#define ENGINE_F_ENGINE_LOAD_PRIVATE_KEY 150
725#define ENGINE_F_ENGINE_LOAD_PUBLIC_KEY 151 740#define ENGINE_F_ENGINE_LOAD_PUBLIC_KEY 151
741#define ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT 192
726#define ENGINE_F_ENGINE_NEW 122 742#define ENGINE_F_ENGINE_NEW 122
727#define ENGINE_F_ENGINE_REMOVE 123 743#define ENGINE_F_ENGINE_REMOVE 123
728#define ENGINE_F_ENGINE_SET_DEFAULT_STRING 189 744#define ENGINE_F_ENGINE_SET_DEFAULT_STRING 189
diff --git a/src/lib/libcrypto/err/err.c b/src/lib/libcrypto/err/err.c
index b6ff070e8f..292404a2fb 100644
--- a/src/lib/libcrypto/err/err.c
+++ b/src/lib/libcrypto/err/err.c
@@ -119,479 +119,9 @@
119#include <openssl/bio.h> 119#include <openssl/bio.h>
120#include <openssl/err.h> 120#include <openssl/err.h>
121 121
122static void err_load_strings(int lib, ERR_STRING_DATA *str); 122static unsigned long get_error_values(int inc,int top,
123 123 const char **file,int *line,
124static void ERR_STATE_free(ERR_STATE *s); 124 const char **data,int *flags);
125#ifndef OPENSSL_NO_ERR
126static ERR_STRING_DATA ERR_str_libraries[]=
127 {
128{ERR_PACK(ERR_LIB_NONE,0,0) ,"unknown library"},
129{ERR_PACK(ERR_LIB_SYS,0,0) ,"system library"},
130{ERR_PACK(ERR_LIB_BN,0,0) ,"bignum routines"},
131{ERR_PACK(ERR_LIB_RSA,0,0) ,"rsa routines"},
132{ERR_PACK(ERR_LIB_DH,0,0) ,"Diffie-Hellman routines"},
133{ERR_PACK(ERR_LIB_EVP,0,0) ,"digital envelope routines"},
134{ERR_PACK(ERR_LIB_BUF,0,0) ,"memory buffer routines"},
135{ERR_PACK(ERR_LIB_OBJ,0,0) ,"object identifier routines"},
136{ERR_PACK(ERR_LIB_PEM,0,0) ,"PEM routines"},
137{ERR_PACK(ERR_LIB_DSA,0,0) ,"dsa routines"},
138{ERR_PACK(ERR_LIB_X509,0,0) ,"x509 certificate routines"},
139{ERR_PACK(ERR_LIB_ASN1,0,0) ,"asn1 encoding routines"},
140{ERR_PACK(ERR_LIB_CONF,0,0) ,"configuration file routines"},
141{ERR_PACK(ERR_LIB_CRYPTO,0,0) ,"common libcrypto routines"},
142{ERR_PACK(ERR_LIB_EC,0,0) ,"elliptic curve routines"},
143{ERR_PACK(ERR_LIB_SSL,0,0) ,"SSL routines"},
144{ERR_PACK(ERR_LIB_BIO,0,0) ,"BIO routines"},
145{ERR_PACK(ERR_LIB_PKCS7,0,0) ,"PKCS7 routines"},
146{ERR_PACK(ERR_LIB_X509V3,0,0) ,"X509 V3 routines"},
147{ERR_PACK(ERR_LIB_PKCS12,0,0) ,"PKCS12 routines"},
148{ERR_PACK(ERR_LIB_RAND,0,0) ,"random number generator"},
149{ERR_PACK(ERR_LIB_DSO,0,0) ,"DSO support routines"},
150{ERR_PACK(ERR_LIB_ENGINE,0,0) ,"engine routines"},
151{ERR_PACK(ERR_LIB_OCSP,0,0) ,"OCSP routines"},
152{ERR_PACK(ERR_LIB_CMS,0,0) ,"CMS routines"},
153{0,NULL},
154 };
155
156static ERR_STRING_DATA ERR_str_functs[]=
157 {
158 {ERR_PACK(0,SYS_F_FOPEN,0), "fopen"},
159 {ERR_PACK(0,SYS_F_CONNECT,0), "connect"},
160 {ERR_PACK(0,SYS_F_GETSERVBYNAME,0), "getservbyname"},
161 {ERR_PACK(0,SYS_F_SOCKET,0), "socket"},
162 {ERR_PACK(0,SYS_F_IOCTLSOCKET,0), "ioctlsocket"},
163 {ERR_PACK(0,SYS_F_BIND,0), "bind"},
164 {ERR_PACK(0,SYS_F_LISTEN,0), "listen"},
165 {ERR_PACK(0,SYS_F_ACCEPT,0), "accept"},
166#ifdef OPENSSL_SYS_WINDOWS
167 {ERR_PACK(0,SYS_F_WSASTARTUP,0), "WSAstartup"},
168#endif
169 {ERR_PACK(0,SYS_F_OPENDIR,0), "opendir"},
170 {ERR_PACK(0,SYS_F_FREAD,0), "fread"},
171 {0,NULL},
172 };
173
174static ERR_STRING_DATA ERR_str_reasons[]=
175 {
176{ERR_R_SYS_LIB ,"system lib"},
177{ERR_R_BN_LIB ,"BN lib"},
178{ERR_R_RSA_LIB ,"RSA lib"},
179{ERR_R_DH_LIB ,"DH lib"},
180{ERR_R_EVP_LIB ,"EVP lib"},
181{ERR_R_BUF_LIB ,"BUF lib"},
182{ERR_R_OBJ_LIB ,"OBJ lib"},
183{ERR_R_PEM_LIB ,"PEM lib"},
184{ERR_R_DSA_LIB ,"DSA lib"},
185{ERR_R_X509_LIB ,"X509 lib"},
186{ERR_R_ASN1_LIB ,"ASN1 lib"},
187{ERR_R_CONF_LIB ,"CONF lib"},
188{ERR_R_CRYPTO_LIB ,"CRYPTO lib"},
189{ERR_R_EC_LIB ,"EC lib"},
190{ERR_R_SSL_LIB ,"SSL lib"},
191{ERR_R_BIO_LIB ,"BIO lib"},
192{ERR_R_PKCS7_LIB ,"PKCS7 lib"},
193{ERR_R_X509V3_LIB ,"X509V3 lib"},
194{ERR_R_PKCS12_LIB ,"PKCS12 lib"},
195{ERR_R_RAND_LIB ,"RAND lib"},
196{ERR_R_DSO_LIB ,"DSO lib"},
197{ERR_R_ENGINE_LIB ,"ENGINE lib"},
198{ERR_R_OCSP_LIB ,"OCSP lib"},
199
200{ERR_R_NESTED_ASN1_ERROR ,"nested asn1 error"},
201{ERR_R_BAD_ASN1_OBJECT_HEADER ,"bad asn1 object header"},
202{ERR_R_BAD_GET_ASN1_OBJECT_CALL ,"bad get asn1 object call"},
203{ERR_R_EXPECTING_AN_ASN1_SEQUENCE ,"expecting an asn1 sequence"},
204{ERR_R_ASN1_LENGTH_MISMATCH ,"asn1 length mismatch"},
205{ERR_R_MISSING_ASN1_EOS ,"missing asn1 eos"},
206
207{ERR_R_FATAL ,"fatal"},
208{ERR_R_MALLOC_FAILURE ,"malloc failure"},
209{ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED ,"called a function you should not call"},
210{ERR_R_PASSED_NULL_PARAMETER ,"passed a null parameter"},
211{ERR_R_INTERNAL_ERROR ,"internal error"},
212{ERR_R_DISABLED ,"called a function that was disabled at compile-time"},
213
214{0,NULL},
215 };
216#endif
217
218
219/* Define the predeclared (but externally opaque) "ERR_FNS" type */
220struct st_ERR_FNS
221 {
222 /* Works on the "error_hash" string table */
223 LHASH *(*cb_err_get)(int create);
224 void (*cb_err_del)(void);
225 ERR_STRING_DATA *(*cb_err_get_item)(const ERR_STRING_DATA *);
226 ERR_STRING_DATA *(*cb_err_set_item)(ERR_STRING_DATA *);
227 ERR_STRING_DATA *(*cb_err_del_item)(ERR_STRING_DATA *);
228 /* Works on the "thread_hash" error-state table */
229 LHASH *(*cb_thread_get)(int create);
230 void (*cb_thread_release)(LHASH **hash);
231 ERR_STATE *(*cb_thread_get_item)(const ERR_STATE *);
232 ERR_STATE *(*cb_thread_set_item)(ERR_STATE *);
233 void (*cb_thread_del_item)(const ERR_STATE *);
234 /* Returns the next available error "library" numbers */
235 int (*cb_get_next_lib)(void);
236 };
237
238/* Predeclarations of the "err_defaults" functions */
239static LHASH *int_err_get(int create);
240static void int_err_del(void);
241static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
242static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *);
243static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *);
244static LHASH *int_thread_get(int create);
245static void int_thread_release(LHASH **hash);
246static ERR_STATE *int_thread_get_item(const ERR_STATE *);
247static ERR_STATE *int_thread_set_item(ERR_STATE *);
248static void int_thread_del_item(const ERR_STATE *);
249static int int_err_get_next_lib(void);
250/* The static ERR_FNS table using these defaults functions */
251static const ERR_FNS err_defaults =
252 {
253 int_err_get,
254 int_err_del,
255 int_err_get_item,
256 int_err_set_item,
257 int_err_del_item,
258 int_thread_get,
259 int_thread_release,
260 int_thread_get_item,
261 int_thread_set_item,
262 int_thread_del_item,
263 int_err_get_next_lib
264 };
265
266/* The replacable table of ERR_FNS functions we use at run-time */
267static const ERR_FNS *err_fns = NULL;
268
269/* Eg. rather than using "err_get()", use "ERRFN(err_get)()". */
270#define ERRFN(a) err_fns->cb_##a
271
272/* The internal state used by "err_defaults" - as such, the setting, reading,
273 * creating, and deleting of this data should only be permitted via the
274 * "err_defaults" functions. This way, a linked module can completely defer all
275 * ERR state operation (together with requisite locking) to the implementations
276 * and state in the loading application. */
277static LHASH *int_error_hash = NULL;
278static LHASH *int_thread_hash = NULL;
279static int int_thread_hash_references = 0;
280static int int_err_library_number= ERR_LIB_USER;
281
282/* Internal function that checks whether "err_fns" is set and if not, sets it to
283 * the defaults. */
284static void err_fns_check(void)
285 {
286 if (err_fns) return;
287
288 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
289 if (!err_fns)
290 err_fns = &err_defaults;
291 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
292 }
293
294/* API functions to get or set the underlying ERR functions. */
295
296const ERR_FNS *ERR_get_implementation(void)
297 {
298 err_fns_check();
299 return err_fns;
300 }
301
302int ERR_set_implementation(const ERR_FNS *fns)
303 {
304 int ret = 0;
305
306 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
307 /* It's too late if 'err_fns' is non-NULL. BTW: not much point setting
308 * an error is there?! */
309 if (!err_fns)
310 {
311 err_fns = fns;
312 ret = 1;
313 }
314 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
315 return ret;
316 }
317
318/* These are the callbacks provided to "lh_new()" when creating the LHASH tables
319 * internal to the "err_defaults" implementation. */
320
321/* static unsigned long err_hash(ERR_STRING_DATA *a); */
322static unsigned long err_hash(const void *a_void);
323/* static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b); */
324static int err_cmp(const void *a_void, const void *b_void);
325/* static unsigned long pid_hash(ERR_STATE *pid); */
326static unsigned long pid_hash(const void *pid_void);
327/* static int pid_cmp(ERR_STATE *a,ERR_STATE *pid); */
328static int pid_cmp(const void *a_void,const void *pid_void);
329static unsigned long get_error_values(int inc,int top,const char **file,int *line,
330 const char **data,int *flags);
331
332/* The internal functions used in the "err_defaults" implementation */
333
334static LHASH *int_err_get(int create)
335 {
336 LHASH *ret = NULL;
337
338 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
339 if (!int_error_hash && create)
340 {
341 CRYPTO_push_info("int_err_get (err.c)");
342 int_error_hash = lh_new(err_hash, err_cmp);
343 CRYPTO_pop_info();
344 }
345 if (int_error_hash)
346 ret = int_error_hash;
347 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
348
349 return ret;
350 }
351
352static void int_err_del(void)
353 {
354 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
355 if (int_error_hash)
356 {
357 lh_free(int_error_hash);
358 int_error_hash = NULL;
359 }
360 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
361 }
362
363static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
364 {
365 ERR_STRING_DATA *p;
366 LHASH *hash;
367
368 err_fns_check();
369 hash = ERRFN(err_get)(0);
370 if (!hash)
371 return NULL;
372
373 CRYPTO_r_lock(CRYPTO_LOCK_ERR);
374 p = (ERR_STRING_DATA *)lh_retrieve(hash, d);
375 CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
376
377 return p;
378 }
379
380static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *d)
381 {
382 ERR_STRING_DATA *p;
383 LHASH *hash;
384
385 err_fns_check();
386 hash = ERRFN(err_get)(1);
387 if (!hash)
388 return NULL;
389
390 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
391 p = (ERR_STRING_DATA *)lh_insert(hash, d);
392 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
393
394 return p;
395 }
396
397static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *d)
398 {
399 ERR_STRING_DATA *p;
400 LHASH *hash;
401
402 err_fns_check();
403 hash = ERRFN(err_get)(0);
404 if (!hash)
405 return NULL;
406
407 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
408 p = (ERR_STRING_DATA *)lh_delete(hash, d);
409 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
410
411 return p;
412 }
413
414static LHASH *int_thread_get(int create)
415 {
416 LHASH *ret = NULL;
417
418 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
419 if (!int_thread_hash && create)
420 {
421 CRYPTO_push_info("int_thread_get (err.c)");
422 int_thread_hash = lh_new(pid_hash, pid_cmp);
423 CRYPTO_pop_info();
424 }
425 if (int_thread_hash)
426 {
427 int_thread_hash_references++;
428 ret = int_thread_hash;
429 }
430 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
431 return ret;
432 }
433
434static void int_thread_release(LHASH **hash)
435 {
436 int i;
437
438 if (hash == NULL || *hash == NULL)
439 return;
440
441 i = CRYPTO_add(&int_thread_hash_references, -1, CRYPTO_LOCK_ERR);
442
443#ifdef REF_PRINT
444 fprintf(stderr,"%4d:%s\n",int_thread_hash_references,"ERR");
445#endif
446 if (i > 0) return;
447#ifdef REF_CHECK
448 if (i < 0)
449 {
450 fprintf(stderr,"int_thread_release, bad reference count\n");
451 abort(); /* ok */
452 }
453#endif
454 *hash = NULL;
455 }
456
457static ERR_STATE *int_thread_get_item(const ERR_STATE *d)
458 {
459 ERR_STATE *p;
460 LHASH *hash;
461
462 err_fns_check();
463 hash = ERRFN(thread_get)(0);
464 if (!hash)
465 return NULL;
466
467 CRYPTO_r_lock(CRYPTO_LOCK_ERR);
468 p = (ERR_STATE *)lh_retrieve(hash, d);
469 CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
470
471 ERRFN(thread_release)(&hash);
472 return p;
473 }
474
475static ERR_STATE *int_thread_set_item(ERR_STATE *d)
476 {
477 ERR_STATE *p;
478 LHASH *hash;
479
480 err_fns_check();
481 hash = ERRFN(thread_get)(1);
482 if (!hash)
483 return NULL;
484
485 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
486 p = (ERR_STATE *)lh_insert(hash, d);
487 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
488
489 ERRFN(thread_release)(&hash);
490 return p;
491 }
492
493static void int_thread_del_item(const ERR_STATE *d)
494 {
495 ERR_STATE *p;
496 LHASH *hash;
497
498 err_fns_check();
499 hash = ERRFN(thread_get)(0);
500 if (!hash)
501 return;
502
503 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
504 p = (ERR_STATE *)lh_delete(hash, d);
505 /* make sure we don't leak memory */
506 if (int_thread_hash_references == 1
507 && int_thread_hash && (lh_num_items(int_thread_hash) == 0))
508 {
509 lh_free(int_thread_hash);
510 int_thread_hash = NULL;
511 }
512 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
513
514 ERRFN(thread_release)(&hash);
515 if (p)
516 ERR_STATE_free(p);
517 }
518
519static int int_err_get_next_lib(void)
520 {
521 int ret;
522
523 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
524 ret = int_err_library_number++;
525 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
526
527 return ret;
528 }
529
530
531#ifndef OPENSSL_NO_ERR
532#define NUM_SYS_STR_REASONS 127
533#define LEN_SYS_STR_REASON 32
534
535static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
536/* SYS_str_reasons is filled with copies of strerror() results at
537 * initialization.
538 * 'errno' values up to 127 should cover all usual errors,
539 * others will be displayed numerically by ERR_error_string.
540 * It is crucial that we have something for each reason code
541 * that occurs in ERR_str_reasons, or bogus reason strings
542 * will be returned for SYSerr(), which always gets an errno
543 * value and never one of those 'standard' reason codes. */
544
545static void build_SYS_str_reasons(void)
546 {
547 /* OPENSSL_malloc cannot be used here, use static storage instead */
548 static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON];
549 int i;
550 static int init = 1;
551
552 CRYPTO_r_lock(CRYPTO_LOCK_ERR);
553 if (!init)
554 {
555 CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
556 return;
557 }
558
559 CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
560 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
561 if (!init)
562 {
563 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
564 return;
565 }
566
567 for (i = 1; i <= NUM_SYS_STR_REASONS; i++)
568 {
569 ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
570
571 str->error = (unsigned long)i;
572 if (str->string == NULL)
573 {
574 char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]);
575 char *src = strerror(i);
576 if (src != NULL)
577 {
578 strncpy(*dest, src, sizeof *dest);
579 (*dest)[sizeof *dest - 1] = '\0';
580 str->string = *dest;
581 }
582 }
583 if (str->string == NULL)
584 str->string = "unknown";
585 }
586
587 /* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL},
588 * as required by ERR_load_strings. */
589
590 init = 0;
591
592 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
593 }
594#endif
595 125
596#define err_clear_data(p,i) \ 126#define err_clear_data(p,i) \
597 do { \ 127 do { \
@@ -613,68 +143,6 @@ static void build_SYS_str_reasons(void)
613 (p)->err_line[i]= -1; \ 143 (p)->err_line[i]= -1; \
614 } while(0) 144 } while(0)
615 145
616static void ERR_STATE_free(ERR_STATE *s)
617 {
618 int i;
619
620 if (s == NULL)
621 return;
622
623 for (i=0; i<ERR_NUM_ERRORS; i++)
624 {
625 err_clear_data(s,i);
626 }
627 OPENSSL_free(s);
628 }
629
630void ERR_load_ERR_strings(void)
631 {
632 err_fns_check();
633#ifndef OPENSSL_NO_ERR
634 err_load_strings(0,ERR_str_libraries);
635 err_load_strings(0,ERR_str_reasons);
636 err_load_strings(ERR_LIB_SYS,ERR_str_functs);
637 build_SYS_str_reasons();
638 err_load_strings(ERR_LIB_SYS,SYS_str_reasons);
639#endif
640 }
641
642static void err_load_strings(int lib, ERR_STRING_DATA *str)
643 {
644 while (str->error)
645 {
646 if (lib)
647 str->error|=ERR_PACK(lib,0,0);
648 ERRFN(err_set_item)(str);
649 str++;
650 }
651 }
652
653void ERR_load_strings(int lib, ERR_STRING_DATA *str)
654 {
655 ERR_load_ERR_strings();
656 err_load_strings(lib, str);
657 }
658
659void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
660 {
661 while (str->error)
662 {
663 if (lib)
664 str->error|=ERR_PACK(lib,0,0);
665 ERRFN(err_del_item)(str);
666 str++;
667 }
668 }
669
670void ERR_free_strings(void)
671 {
672 err_fns_check();
673 ERRFN(err_del)();
674 }
675
676/********************************************************/
677
678void ERR_put_error(int lib, int func, int reason, const char *file, 146void ERR_put_error(int lib, int func, int reason, const char *file,
679 int line) 147 int line)
680 { 148 {
@@ -829,218 +297,6 @@ static unsigned long get_error_values(int inc, int top, const char **file, int *
829 return ret; 297 return ret;
830 } 298 }
831 299
832void ERR_error_string_n(unsigned long e, char *buf, size_t len)
833 {
834 char lsbuf[64], fsbuf[64], rsbuf[64];
835 const char *ls,*fs,*rs;
836 unsigned long l,f,r;
837
838 l=ERR_GET_LIB(e);
839 f=ERR_GET_FUNC(e);
840 r=ERR_GET_REASON(e);
841
842 ls=ERR_lib_error_string(e);
843 fs=ERR_func_error_string(e);
844 rs=ERR_reason_error_string(e);
845
846 if (ls == NULL)
847 BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
848 if (fs == NULL)
849 BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
850 if (rs == NULL)
851 BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
852
853 BIO_snprintf(buf, len,"error:%08lX:%s:%s:%s", e, ls?ls:lsbuf,
854 fs?fs:fsbuf, rs?rs:rsbuf);
855 if (strlen(buf) == len-1)
856 {
857 /* output may be truncated; make sure we always have 5
858 * colon-separated fields, i.e. 4 colons ... */
859#define NUM_COLONS 4
860 if (len > NUM_COLONS) /* ... if possible */
861 {
862 int i;
863 char *s = buf;
864
865 for (i = 0; i < NUM_COLONS; i++)
866 {
867 char *colon = strchr(s, ':');
868 if (colon == NULL || colon > &buf[len-1] - NUM_COLONS + i)
869 {
870 /* set colon no. i at last possible position
871 * (buf[len-1] is the terminating 0)*/
872 colon = &buf[len-1] - NUM_COLONS + i;
873 *colon = ':';
874 }
875 s = colon + 1;
876 }
877 }
878 }
879 }
880
881/* BAD for multi-threading: uses a local buffer if ret == NULL */
882/* ERR_error_string_n should be used instead for ret != NULL
883 * as ERR_error_string cannot know how large the buffer is */
884char *ERR_error_string(unsigned long e, char *ret)
885 {
886 static char buf[256];
887
888 if (ret == NULL) ret=buf;
889 ERR_error_string_n(e, ret, 256);
890
891 return ret;
892 }
893
894LHASH *ERR_get_string_table(void)
895 {
896 err_fns_check();
897 return ERRFN(err_get)(0);
898 }
899
900LHASH *ERR_get_err_state_table(void)
901 {
902 err_fns_check();
903 return ERRFN(thread_get)(0);
904 }
905
906void ERR_release_err_state_table(LHASH **hash)
907 {
908 err_fns_check();
909 ERRFN(thread_release)(hash);
910 }
911
912const char *ERR_lib_error_string(unsigned long e)
913 {
914 ERR_STRING_DATA d,*p;
915 unsigned long l;
916
917 err_fns_check();
918 l=ERR_GET_LIB(e);
919 d.error=ERR_PACK(l,0,0);
920 p=ERRFN(err_get_item)(&d);
921 return((p == NULL)?NULL:p->string);
922 }
923
924const char *ERR_func_error_string(unsigned long e)
925 {
926 ERR_STRING_DATA d,*p;
927 unsigned long l,f;
928
929 err_fns_check();
930 l=ERR_GET_LIB(e);
931 f=ERR_GET_FUNC(e);
932 d.error=ERR_PACK(l,f,0);
933 p=ERRFN(err_get_item)(&d);
934 return((p == NULL)?NULL:p->string);
935 }
936
937const char *ERR_reason_error_string(unsigned long e)
938 {
939 ERR_STRING_DATA d,*p=NULL;
940 unsigned long l,r;
941
942 err_fns_check();
943 l=ERR_GET_LIB(e);
944 r=ERR_GET_REASON(e);
945 d.error=ERR_PACK(l,0,r);
946 p=ERRFN(err_get_item)(&d);
947 if (!p)
948 {
949 d.error=ERR_PACK(0,0,r);
950 p=ERRFN(err_get_item)(&d);
951 }
952 return((p == NULL)?NULL:p->string);
953 }
954
955/* static unsigned long err_hash(ERR_STRING_DATA *a) */
956static unsigned long err_hash(const void *a_void)
957 {
958 unsigned long ret,l;
959
960 l=((const ERR_STRING_DATA *)a_void)->error;
961 ret=l^ERR_GET_LIB(l)^ERR_GET_FUNC(l);
962 return(ret^ret%19*13);
963 }
964
965/* static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b) */
966static int err_cmp(const void *a_void, const void *b_void)
967 {
968 return((int)(((const ERR_STRING_DATA *)a_void)->error -
969 ((const ERR_STRING_DATA *)b_void)->error));
970 }
971
972/* static unsigned long pid_hash(ERR_STATE *a) */
973static unsigned long pid_hash(const void *a_void)
974 {
975 return(((const ERR_STATE *)a_void)->pid*13);
976 }
977
978/* static int pid_cmp(ERR_STATE *a, ERR_STATE *b) */
979static int pid_cmp(const void *a_void, const void *b_void)
980 {
981 return((int)((long)((const ERR_STATE *)a_void)->pid -
982 (long)((const ERR_STATE *)b_void)->pid));
983 }
984
985void ERR_remove_state(unsigned long pid)
986 {
987 ERR_STATE tmp;
988
989 err_fns_check();
990 if (pid == 0)
991 pid=(unsigned long)CRYPTO_thread_id();
992 tmp.pid=pid;
993 /* thread_del_item automatically destroys the LHASH if the number of
994 * items reaches zero. */
995 ERRFN(thread_del_item)(&tmp);
996 }
997
998ERR_STATE *ERR_get_state(void)
999 {
1000 static ERR_STATE fallback;
1001 ERR_STATE *ret,tmp,*tmpp=NULL;
1002 int i;
1003 unsigned long pid;
1004
1005 err_fns_check();
1006 pid=(unsigned long)CRYPTO_thread_id();
1007 tmp.pid=pid;
1008 ret=ERRFN(thread_get_item)(&tmp);
1009
1010 /* ret == the error state, if NULL, make a new one */
1011 if (ret == NULL)
1012 {
1013 ret=(ERR_STATE *)OPENSSL_malloc(sizeof(ERR_STATE));
1014 if (ret == NULL) return(&fallback);
1015 ret->pid=pid;
1016 ret->top=0;
1017 ret->bottom=0;
1018 for (i=0; i<ERR_NUM_ERRORS; i++)
1019 {
1020 ret->err_data[i]=NULL;
1021 ret->err_data_flags[i]=0;
1022 }
1023 tmpp = ERRFN(thread_set_item)(ret);
1024 /* To check if insertion failed, do a get. */
1025 if (ERRFN(thread_get_item)(ret) != ret)
1026 {
1027 ERR_STATE_free(ret); /* could not insert it */
1028 return(&fallback);
1029 }
1030 /* If a race occured in this function and we came second, tmpp
1031 * is the first one that we just replaced. */
1032 if (tmpp)
1033 ERR_STATE_free(tmpp);
1034 }
1035 return ret;
1036 }
1037
1038int ERR_get_next_error_library(void)
1039 {
1040 err_fns_check();
1041 return ERRFN(get_next_lib)();
1042 }
1043
1044void ERR_set_error_data(char *data, int flags) 300void ERR_set_error_data(char *data, int flags)
1045 { 301 {
1046 ERR_STATE *es; 302 ERR_STATE *es;
@@ -1127,3 +383,34 @@ int ERR_pop_to_mark(void)
1127 es->err_flags[es->top]&=~ERR_FLAG_MARK; 383 es->err_flags[es->top]&=~ERR_FLAG_MARK;
1128 return 1; 384 return 1;
1129 } 385 }
386
387#ifdef OPENSSL_FIPS
388
389static ERR_STATE *fget_state(void)
390 {
391 static ERR_STATE fstate;
392 return &fstate;
393 }
394
395ERR_STATE *(*get_state_func)(void) = fget_state;
396void (*remove_state_func)(unsigned long pid);
397
398ERR_STATE *ERR_get_state(void)
399 {
400 return get_state_func();
401 }
402
403void int_ERR_set_state_func(ERR_STATE *(*get_func)(void),
404 void (*remove_func)(unsigned long pid))
405 {
406 get_state_func = get_func;
407 remove_state_func = remove_func;
408 }
409
410void ERR_remove_state(unsigned long pid)
411 {
412 if (remove_state_func)
413 remove_state_func(pid);
414 }
415
416#endif
diff --git a/src/lib/libcrypto/err/err.h b/src/lib/libcrypto/err/err.h
index bf28fce492..dcac415231 100644
--- a/src/lib/libcrypto/err/err.h
+++ b/src/lib/libcrypto/err/err.h
@@ -140,7 +140,9 @@ typedef struct err_state_st
140#define ERR_LIB_ECDSA 42 140#define ERR_LIB_ECDSA 42
141#define ERR_LIB_ECDH 43 141#define ERR_LIB_ECDH 43
142#define ERR_LIB_STORE 44 142#define ERR_LIB_STORE 44
143#define ERR_LIB_CMS 45 143#define ERR_LIB_FIPS 45
144#define ERR_LIB_CMS 46
145#define ERR_LIB_JPAKE 47
144 146
145#define ERR_LIB_USER 128 147#define ERR_LIB_USER 128
146 148
@@ -172,7 +174,9 @@ typedef struct err_state_st
172#define ECDSAerr(f,r) ERR_PUT_error(ERR_LIB_ECDSA,(f),(r),__FILE__,__LINE__) 174#define ECDSAerr(f,r) ERR_PUT_error(ERR_LIB_ECDSA,(f),(r),__FILE__,__LINE__)
173#define ECDHerr(f,r) ERR_PUT_error(ERR_LIB_ECDH,(f),(r),__FILE__,__LINE__) 175#define ECDHerr(f,r) ERR_PUT_error(ERR_LIB_ECDH,(f),(r),__FILE__,__LINE__)
174#define STOREerr(f,r) ERR_PUT_error(ERR_LIB_STORE,(f),(r),__FILE__,__LINE__) 176#define STOREerr(f,r) ERR_PUT_error(ERR_LIB_STORE,(f),(r),__FILE__,__LINE__)
177#define FIPSerr(f,r) ERR_PUT_error(ERR_LIB_FIPS,(f),(r),__FILE__,__LINE__)
175#define CMSerr(f,r) ERR_PUT_error(ERR_LIB_CMS,(f),(r),__FILE__,__LINE__) 178#define CMSerr(f,r) ERR_PUT_error(ERR_LIB_CMS,(f),(r),__FILE__,__LINE__)
179#define JPAKEerr(f,r) ERR_PUT_error(ERR_LIB_JPAKE,(f),(r),__FILE__,__LINE__)
176 180
177/* Borland C seems too stupid to be able to shift and do longs in 181/* Borland C seems too stupid to be able to shift and do longs in
178 * the pre-processor :-( */ 182 * the pre-processor :-( */
@@ -304,6 +308,12 @@ int ERR_get_next_error_library(void);
304int ERR_set_mark(void); 308int ERR_set_mark(void);
305int ERR_pop_to_mark(void); 309int ERR_pop_to_mark(void);
306 310
311#ifdef OPENSSL_FIPS
312void int_ERR_set_state_func(ERR_STATE *(*get_func)(void),
313 void (*remove_func)(unsigned long pid));
314void int_ERR_lib_init(void);
315#endif
316
307/* Already defined in ossl_typ.h */ 317/* Already defined in ossl_typ.h */
308/* typedef struct st_ERR_FNS ERR_FNS; */ 318/* typedef struct st_ERR_FNS ERR_FNS; */
309/* An application can use this function and provide the return value to loaded 319/* An application can use this function and provide the return value to loaded
diff --git a/src/lib/libcrypto/err/err_all.c b/src/lib/libcrypto/err/err_all.c
index 5813060ce2..f21a5276ed 100644
--- a/src/lib/libcrypto/err/err_all.c
+++ b/src/lib/libcrypto/err/err_all.c
@@ -94,9 +94,16 @@
94#include <openssl/ui.h> 94#include <openssl/ui.h>
95#include <openssl/ocsp.h> 95#include <openssl/ocsp.h>
96#include <openssl/err.h> 96#include <openssl/err.h>
97#ifdef OPENSSL_FIPS
98#include <openssl/fips.h>
99#endif
100
97#ifndef OPENSSL_NO_CMS 101#ifndef OPENSSL_NO_CMS
98#include <openssl/cms.h> 102#include <openssl/cms.h>
99#endif 103#endif
104#ifndef OPENSSL_NO_JPAKE
105#include <openssl/jpake.h>
106#endif
100 107
101void ERR_load_crypto_strings(void) 108void ERR_load_crypto_strings(void)
102 { 109 {
@@ -141,8 +148,14 @@ void ERR_load_crypto_strings(void)
141#endif 148#endif
142 ERR_load_OCSP_strings(); 149 ERR_load_OCSP_strings();
143 ERR_load_UI_strings(); 150 ERR_load_UI_strings();
151#ifdef OPENSSL_FIPS
152 ERR_load_FIPS_strings();
153#endif
144#ifndef OPENSSL_NO_CMS 154#ifndef OPENSSL_NO_CMS
145 ERR_load_CMS_strings(); 155 ERR_load_CMS_strings();
146#endif 156#endif
157#ifndef OPENSSL_NO_JPAKE
158 ERR_load_JPAKE_strings();
159#endif
147#endif 160#endif
148 } 161 }
diff --git a/src/lib/libcrypto/err/err_prn.c b/src/lib/libcrypto/err/err_prn.c
index 2224a901e5..4cdf342fa6 100644
--- a/src/lib/libcrypto/err/err_prn.c
+++ b/src/lib/libcrypto/err/err_prn.c
@@ -86,12 +86,7 @@ void ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u),
86#ifndef OPENSSL_NO_FP_API 86#ifndef OPENSSL_NO_FP_API
87static int print_fp(const char *str, size_t len, void *fp) 87static int print_fp(const char *str, size_t len, void *fp)
88 { 88 {
89 BIO bio; 89 return fwrite(str, 1, len, fp);
90
91 BIO_set(&bio,BIO_s_file());
92 BIO_set_fp(&bio,fp,BIO_NOCLOSE);
93
94 return BIO_printf(&bio, "%s", str);
95 } 90 }
96void ERR_print_errors_fp(FILE *fp) 91void ERR_print_errors_fp(FILE *fp)
97 { 92 {
@@ -99,13 +94,64 @@ void ERR_print_errors_fp(FILE *fp)
99 } 94 }
100#endif 95#endif
101 96
102static int print_bio(const char *str, size_t len, void *bp) 97void ERR_error_string_n(unsigned long e, char *buf, size_t len)
103 { 98 {
104 return BIO_write((BIO *)bp, str, len); 99 char lsbuf[64], fsbuf[64], rsbuf[64];
100 const char *ls,*fs,*rs;
101 unsigned long l,f,r;
102
103 l=ERR_GET_LIB(e);
104 f=ERR_GET_FUNC(e);
105 r=ERR_GET_REASON(e);
106
107 ls=ERR_lib_error_string(e);
108 fs=ERR_func_error_string(e);
109 rs=ERR_reason_error_string(e);
110
111 if (ls == NULL)
112 BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
113 if (fs == NULL)
114 BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
115 if (rs == NULL)
116 BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
117
118 BIO_snprintf(buf, len,"error:%08lX:%s:%s:%s", e, ls?ls:lsbuf,
119 fs?fs:fsbuf, rs?rs:rsbuf);
120 if (strlen(buf) == len-1)
121 {
122 /* output may be truncated; make sure we always have 5
123 * colon-separated fields, i.e. 4 colons ... */
124#define NUM_COLONS 4
125 if (len > NUM_COLONS) /* ... if possible */
126 {
127 int i;
128 char *s = buf;
129
130 for (i = 0; i < NUM_COLONS; i++)
131 {
132 char *colon = strchr(s, ':');
133 if (colon == NULL || colon > &buf[len-1] - NUM_COLONS + i)
134 {
135 /* set colon no. i at last possible position
136 * (buf[len-1] is the terminating 0)*/
137 colon = &buf[len-1] - NUM_COLONS + i;
138 *colon = ':';
139 }
140 s = colon + 1;
141 }
142 }
143 }
105 } 144 }
106void ERR_print_errors(BIO *bp) 145
146/* BAD for multi-threading: uses a local buffer if ret == NULL */
147/* ERR_error_string_n should be used instead for ret != NULL
148 * as ERR_error_string cannot know how large the buffer is */
149char *ERR_error_string(unsigned long e, char *ret)
107 { 150 {
108 ERR_print_errors_cb(print_bio, bp); 151 static char buf[256];
109 } 152
153 if (ret == NULL) ret=buf;
154 ERR_error_string_n(e, ret, 256);
110 155
111 156 return ret;
157 }
diff --git a/src/lib/libcrypto/err/openssl.ec b/src/lib/libcrypto/err/openssl.ec
index 1938f081ac..868826624d 100644
--- a/src/lib/libcrypto/err/openssl.ec
+++ b/src/lib/libcrypto/err/openssl.ec
@@ -31,7 +31,9 @@ L COMP crypto/comp/comp.h crypto/comp/comp_err.c
31L ECDSA crypto/ecdsa/ecdsa.h crypto/ecdsa/ecs_err.c 31L ECDSA crypto/ecdsa/ecdsa.h crypto/ecdsa/ecs_err.c
32L ECDH crypto/ecdh/ecdh.h crypto/ecdh/ech_err.c 32L ECDH crypto/ecdh/ecdh.h crypto/ecdh/ech_err.c
33L STORE crypto/store/store.h crypto/store/str_err.c 33L STORE crypto/store/store.h crypto/store/str_err.c
34L FIPS fips/fips.h crypto/fips_err.h
34L CMS crypto/cms/cms.h crypto/cms/cms_err.c 35L CMS crypto/cms/cms.h crypto/cms/cms_err.c
36L JPAKE crypto/jpake/jpake.h crypto/jpake/jpake_err.c
35 37
36# additional header files to be scanned for function names 38# additional header files to be scanned for function names
37L NONE crypto/x509/x509_vfy.h NONE 39L NONE crypto/x509/x509_vfy.h NONE
diff --git a/src/lib/libcrypto/evp/bio_md.c b/src/lib/libcrypto/evp/bio_md.c
index d648ac6da6..ed5c1135fd 100644
--- a/src/lib/libcrypto/evp/bio_md.c
+++ b/src/lib/libcrypto/evp/bio_md.c
@@ -192,13 +192,8 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
192 ret=0; 192 ret=0;
193 break; 193 break;
194 case BIO_C_GET_MD_CTX: 194 case BIO_C_GET_MD_CTX:
195 if (b->init) 195 pctx=ptr;
196 { 196 *pctx=ctx;
197 pctx=ptr;
198 *pctx=ctx;
199 }
200 else
201 ret=0;
202 break; 197 break;
203 case BIO_C_SET_MD_CTX: 198 case BIO_C_SET_MD_CTX:
204 if (b->init) 199 if (b->init)
diff --git a/src/lib/libcrypto/evp/digest.c b/src/lib/libcrypto/evp/digest.c
index 762e6d3450..3bc2d1295c 100644
--- a/src/lib/libcrypto/evp/digest.c
+++ b/src/lib/libcrypto/evp/digest.c
@@ -116,6 +116,7 @@
116#ifndef OPENSSL_NO_ENGINE 116#ifndef OPENSSL_NO_ENGINE
117#include <openssl/engine.h> 117#include <openssl/engine.h>
118#endif 118#endif
119#include "evp_locl.h"
119 120
120void EVP_MD_CTX_init(EVP_MD_CTX *ctx) 121void EVP_MD_CTX_init(EVP_MD_CTX *ctx)
121 { 122 {
@@ -137,18 +138,77 @@ int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
137 return EVP_DigestInit_ex(ctx, type, NULL); 138 return EVP_DigestInit_ex(ctx, type, NULL);
138 } 139 }
139 140
140int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) 141#ifdef OPENSSL_FIPS
142
143/* The purpose of these is to trap programs that attempt to use non FIPS
144 * algorithms in FIPS mode and ignore the errors.
145 */
146
147static int bad_init(EVP_MD_CTX *ctx)
148 { FIPS_ERROR_IGNORED("Digest init"); return 0;}
149
150static int bad_update(EVP_MD_CTX *ctx,const void *data,size_t count)
151 { FIPS_ERROR_IGNORED("Digest update"); return 0;}
152
153static int bad_final(EVP_MD_CTX *ctx,unsigned char *md)
154 { FIPS_ERROR_IGNORED("Digest Final"); return 0;}
155
156static const EVP_MD bad_md =
141 { 157 {
142 EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); 158 0,
159 0,
160 0,
161 0,
162 bad_init,
163 bad_update,
164 bad_final,
165 NULL,
166 NULL,
167 NULL,
168 0,
169 {0,0,0,0},
170 };
171
172#endif
173
143#ifndef OPENSSL_NO_ENGINE 174#ifndef OPENSSL_NO_ENGINE
144 /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts 175
145 * so this context may already have an ENGINE! Try to avoid releasing 176#ifdef OPENSSL_FIPS
146 * the previous handle, re-querying for an ENGINE, and having a 177
147 * reinitialisation, when it may all be unecessary. */ 178static int do_engine_null(ENGINE *impl) { return 0;}
148 if (ctx->engine && ctx->digest && (!type || 179static int do_evp_md_engine_null(EVP_MD_CTX *ctx,
149 (type && (type->type == ctx->digest->type)))) 180 const EVP_MD **ptype, ENGINE *impl)
150 goto skip_to_init; 181 { return 1; }
151 if (type) 182
183static int (*do_engine_init)(ENGINE *impl)
184 = do_engine_null;
185
186static int (*do_engine_finish)(ENGINE *impl)
187 = do_engine_null;
188
189static int (*do_evp_md_engine)
190 (EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl)
191 = do_evp_md_engine_null;
192
193void int_EVP_MD_set_engine_callbacks(
194 int (*eng_md_init)(ENGINE *impl),
195 int (*eng_md_fin)(ENGINE *impl),
196 int (*eng_md_evp)
197 (EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl))
198 {
199 do_engine_init = eng_md_init;
200 do_engine_finish = eng_md_fin;
201 do_evp_md_engine = eng_md_evp;
202 }
203
204#else
205
206#define do_engine_init ENGINE_init
207#define do_engine_finish ENGINE_finish
208
209static int do_evp_md_engine(EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl)
210 {
211 if (*ptype)
152 { 212 {
153 /* Ensure an ENGINE left lying around from last time is cleared 213 /* Ensure an ENGINE left lying around from last time is cleared
154 * (the previous check attempted to avoid this if the same 214 * (the previous check attempted to avoid this if the same
@@ -159,25 +219,25 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
159 { 219 {
160 if (!ENGINE_init(impl)) 220 if (!ENGINE_init(impl))
161 { 221 {
162 EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR); 222 EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_INITIALIZATION_ERROR);
163 return 0; 223 return 0;
164 } 224 }
165 } 225 }
166 else 226 else
167 /* Ask if an ENGINE is reserved for this job */ 227 /* Ask if an ENGINE is reserved for this job */
168 impl = ENGINE_get_digest_engine(type->type); 228 impl = ENGINE_get_digest_engine((*ptype)->type);
169 if(impl) 229 if(impl)
170 { 230 {
171 /* There's an ENGINE for this job ... (apparently) */ 231 /* There's an ENGINE for this job ... (apparently) */
172 const EVP_MD *d = ENGINE_get_digest(impl, type->type); 232 const EVP_MD *d = ENGINE_get_digest(impl, (*ptype)->type);
173 if(!d) 233 if(!d)
174 { 234 {
175 /* Same comment from evp_enc.c */ 235 /* Same comment from evp_enc.c */
176 EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR); 236 EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_INITIALIZATION_ERROR);
177 return 0; 237 return 0;
178 } 238 }
179 /* We'll use the ENGINE's private digest definition */ 239 /* We'll use the ENGINE's private digest definition */
180 type = d; 240 *ptype = d;
181 /* Store the ENGINE functional reference so we know 241 /* Store the ENGINE functional reference so we know
182 * 'type' came from an ENGINE and we need to release 242 * 'type' came from an ENGINE and we need to release
183 * it when done. */ 243 * it when done. */
@@ -189,12 +249,52 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
189 else 249 else
190 if(!ctx->digest) 250 if(!ctx->digest)
191 { 251 {
192 EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_NO_DIGEST_SET); 252 EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_NO_DIGEST_SET);
193 return 0; 253 return 0;
194 } 254 }
255 return 1;
256 }
257
258#endif
259
260#endif
261
262int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
263 {
264 M_EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED);
265#ifdef OPENSSL_FIPS
266 if(FIPS_selftest_failed())
267 {
268 FIPSerr(FIPS_F_EVP_DIGESTINIT_EX,FIPS_R_FIPS_SELFTEST_FAILED);
269 ctx->digest = &bad_md;
270 return 0;
271 }
272#endif
273#ifndef OPENSSL_NO_ENGINE
274 /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts
275 * so this context may already have an ENGINE! Try to avoid releasing
276 * the previous handle, re-querying for an ENGINE, and having a
277 * reinitialisation, when it may all be unecessary. */
278 if (ctx->engine && ctx->digest && (!type ||
279 (type && (type->type == ctx->digest->type))))
280 goto skip_to_init;
281 if (!do_evp_md_engine(ctx, &type, impl))
282 return 0;
195#endif 283#endif
196 if (ctx->digest != type) 284 if (ctx->digest != type)
197 { 285 {
286#ifdef OPENSSL_FIPS
287 if (FIPS_mode())
288 {
289 if (!(type->flags & EVP_MD_FLAG_FIPS)
290 && !(ctx->flags & EVP_MD_CTX_FLAG_NON_FIPS_ALLOW))
291 {
292 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_DISABLED_FOR_FIPS);
293 ctx->digest = &bad_md;
294 return 0;
295 }
296 }
297#endif
198 if (ctx->digest && ctx->digest->ctx_size) 298 if (ctx->digest && ctx->digest->ctx_size)
199 OPENSSL_free(ctx->md_data); 299 OPENSSL_free(ctx->md_data);
200 ctx->digest=type; 300 ctx->digest=type;
@@ -202,7 +302,7 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
202 ctx->md_data=OPENSSL_malloc(type->ctx_size); 302 ctx->md_data=OPENSSL_malloc(type->ctx_size);
203 } 303 }
204#ifndef OPENSSL_NO_ENGINE 304#ifndef OPENSSL_NO_ENGINE
205skip_to_init: 305 skip_to_init:
206#endif 306#endif
207 return ctx->digest->init(ctx); 307 return ctx->digest->init(ctx);
208 } 308 }
@@ -210,6 +310,9 @@ skip_to_init:
210int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, 310int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data,
211 size_t count) 311 size_t count)
212 { 312 {
313#ifdef OPENSSL_FIPS
314 FIPS_selftest_check();
315#endif
213 return ctx->digest->update(ctx,data,count); 316 return ctx->digest->update(ctx,data,count);
214 } 317 }
215 318
@@ -226,6 +329,9 @@ int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
226int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) 329int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
227 { 330 {
228 int ret; 331 int ret;
332#ifdef OPENSSL_FIPS
333 FIPS_selftest_check();
334#endif
229 335
230 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); 336 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
231 ret=ctx->digest->final(ctx,md); 337 ret=ctx->digest->final(ctx,md);
@@ -234,7 +340,7 @@ int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
234 if (ctx->digest->cleanup) 340 if (ctx->digest->cleanup)
235 { 341 {
236 ctx->digest->cleanup(ctx); 342 ctx->digest->cleanup(ctx);
237 EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); 343 M_EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED);
238 } 344 }
239 memset(ctx->md_data,0,ctx->digest->ctx_size); 345 memset(ctx->md_data,0,ctx->digest->ctx_size);
240 return ret; 346 return ret;
@@ -256,7 +362,7 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
256 } 362 }
257#ifndef OPENSSL_NO_ENGINE 363#ifndef OPENSSL_NO_ENGINE
258 /* Make sure it's safe to copy a digest context using an ENGINE */ 364 /* Make sure it's safe to copy a digest context using an ENGINE */
259 if (in->engine && !ENGINE_init(in->engine)) 365 if (in->engine && !do_engine_init(in->engine))
260 { 366 {
261 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB); 367 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB);
262 return 0; 368 return 0;
@@ -266,7 +372,7 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
266 if (out->digest == in->digest) 372 if (out->digest == in->digest)
267 { 373 {
268 tmp_buf = out->md_data; 374 tmp_buf = out->md_data;
269 EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE); 375 M_EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE);
270 } 376 }
271 else tmp_buf = NULL; 377 else tmp_buf = NULL;
272 EVP_MD_CTX_cleanup(out); 378 EVP_MD_CTX_cleanup(out);
@@ -292,7 +398,7 @@ int EVP_Digest(const void *data, size_t count,
292 int ret; 398 int ret;
293 399
294 EVP_MD_CTX_init(&ctx); 400 EVP_MD_CTX_init(&ctx);
295 EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT); 401 M_EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT);
296 ret=EVP_DigestInit_ex(&ctx, type, impl) 402 ret=EVP_DigestInit_ex(&ctx, type, impl)
297 && EVP_DigestUpdate(&ctx, data, count) 403 && EVP_DigestUpdate(&ctx, data, count)
298 && EVP_DigestFinal_ex(&ctx, md, size); 404 && EVP_DigestFinal_ex(&ctx, md, size);
@@ -314,10 +420,10 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
314 * because sometimes only copies of the context are ever finalised. 420 * because sometimes only copies of the context are ever finalised.
315 */ 421 */
316 if (ctx->digest && ctx->digest->cleanup 422 if (ctx->digest && ctx->digest->cleanup
317 && !EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED)) 423 && !M_EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED))
318 ctx->digest->cleanup(ctx); 424 ctx->digest->cleanup(ctx);
319 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data 425 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
320 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) 426 && !M_EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE))
321 { 427 {
322 OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size); 428 OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size);
323 OPENSSL_free(ctx->md_data); 429 OPENSSL_free(ctx->md_data);
@@ -326,7 +432,7 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
326 if(ctx->engine) 432 if(ctx->engine)
327 /* The EVP_MD we used belongs to an ENGINE, release the 433 /* The EVP_MD we used belongs to an ENGINE, release the
328 * functional reference we held for this reason. */ 434 * functional reference we held for this reason. */
329 ENGINE_finish(ctx->engine); 435 do_engine_finish(ctx->engine);
330#endif 436#endif
331 memset(ctx,'\0',sizeof *ctx); 437 memset(ctx,'\0',sizeof *ctx);
332 438
diff --git a/src/lib/libcrypto/evp/e_aes.c b/src/lib/libcrypto/evp/e_aes.c
index bd6c0a3a62..c9a5ee8d75 100644
--- a/src/lib/libcrypto/evp/e_aes.c
+++ b/src/lib/libcrypto/evp/e_aes.c
@@ -69,32 +69,29 @@ typedef struct
69 69
70IMPLEMENT_BLOCK_CIPHER(aes_128, ks, AES, EVP_AES_KEY, 70IMPLEMENT_BLOCK_CIPHER(aes_128, ks, AES, EVP_AES_KEY,
71 NID_aes_128, 16, 16, 16, 128, 71 NID_aes_128, 16, 16, 16, 128,
72 0, aes_init_key, NULL, 72 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
73 EVP_CIPHER_set_asn1_iv, 73 aes_init_key,
74 EVP_CIPHER_get_asn1_iv, 74 NULL, NULL, NULL, NULL)
75 NULL)
76IMPLEMENT_BLOCK_CIPHER(aes_192, ks, AES, EVP_AES_KEY, 75IMPLEMENT_BLOCK_CIPHER(aes_192, ks, AES, EVP_AES_KEY,
77 NID_aes_192, 16, 24, 16, 128, 76 NID_aes_192, 16, 24, 16, 128,
78 0, aes_init_key, NULL, 77 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
79 EVP_CIPHER_set_asn1_iv, 78 aes_init_key,
80 EVP_CIPHER_get_asn1_iv, 79 NULL, NULL, NULL, NULL)
81 NULL)
82IMPLEMENT_BLOCK_CIPHER(aes_256, ks, AES, EVP_AES_KEY, 80IMPLEMENT_BLOCK_CIPHER(aes_256, ks, AES, EVP_AES_KEY,
83 NID_aes_256, 16, 32, 16, 128, 81 NID_aes_256, 16, 32, 16, 128,
84 0, aes_init_key, NULL, 82 EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
85 EVP_CIPHER_set_asn1_iv, 83 aes_init_key,
86 EVP_CIPHER_get_asn1_iv, 84 NULL, NULL, NULL, NULL)
87 NULL)
88 85
89#define IMPLEMENT_AES_CFBR(ksize,cbits) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16) 86#define IMPLEMENT_AES_CFBR(ksize,cbits,flags) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16,flags)
90 87
91IMPLEMENT_AES_CFBR(128,1) 88IMPLEMENT_AES_CFBR(128,1,EVP_CIPH_FLAG_FIPS)
92IMPLEMENT_AES_CFBR(192,1) 89IMPLEMENT_AES_CFBR(192,1,EVP_CIPH_FLAG_FIPS)
93IMPLEMENT_AES_CFBR(256,1) 90IMPLEMENT_AES_CFBR(256,1,EVP_CIPH_FLAG_FIPS)
94 91
95IMPLEMENT_AES_CFBR(128,8) 92IMPLEMENT_AES_CFBR(128,8,EVP_CIPH_FLAG_FIPS)
96IMPLEMENT_AES_CFBR(192,8) 93IMPLEMENT_AES_CFBR(192,8,EVP_CIPH_FLAG_FIPS)
97IMPLEMENT_AES_CFBR(256,8) 94IMPLEMENT_AES_CFBR(256,8,EVP_CIPH_FLAG_FIPS)
98 95
99static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 96static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
100 const unsigned char *iv, int enc) 97 const unsigned char *iv, int enc)
diff --git a/src/lib/libcrypto/evp/e_camellia.c b/src/lib/libcrypto/evp/e_camellia.c
index a7b40d1c60..365d397164 100644
--- a/src/lib/libcrypto/evp/e_camellia.c
+++ b/src/lib/libcrypto/evp/e_camellia.c
@@ -93,7 +93,7 @@ IMPLEMENT_BLOCK_CIPHER(camellia_256, ks, Camellia, EVP_CAMELLIA_KEY,
93 EVP_CIPHER_get_asn1_iv, 93 EVP_CIPHER_get_asn1_iv,
94 NULL) 94 NULL)
95 95
96#define IMPLEMENT_CAMELLIA_CFBR(ksize,cbits) IMPLEMENT_CFBR(camellia,Camellia,EVP_CAMELLIA_KEY,ks,ksize,cbits,16) 96#define IMPLEMENT_CAMELLIA_CFBR(ksize,cbits) IMPLEMENT_CFBR(camellia,Camellia,EVP_CAMELLIA_KEY,ks,ksize,cbits,16,0)
97 97
98IMPLEMENT_CAMELLIA_CFBR(128,1) 98IMPLEMENT_CAMELLIA_CFBR(128,1)
99IMPLEMENT_CAMELLIA_CFBR(192,1) 99IMPLEMENT_CAMELLIA_CFBR(192,1)
diff --git a/src/lib/libcrypto/evp/e_des.c b/src/lib/libcrypto/evp/e_des.c
index 856323648c..04376df232 100644
--- a/src/lib/libcrypto/evp/e_des.c
+++ b/src/lib/libcrypto/evp/e_des.c
@@ -129,18 +129,21 @@ static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
129 } 129 }
130 130
131BLOCK_CIPHER_defs(des, DES_key_schedule, NID_des, 8, 8, 8, 64, 131BLOCK_CIPHER_defs(des, DES_key_schedule, NID_des, 8, 8, 8, 64,
132 EVP_CIPH_RAND_KEY, des_init_key, NULL, 132 EVP_CIPH_RAND_KEY,
133 des_init_key, NULL,
133 EVP_CIPHER_set_asn1_iv, 134 EVP_CIPHER_set_asn1_iv,
134 EVP_CIPHER_get_asn1_iv, 135 EVP_CIPHER_get_asn1_iv,
135 des_ctrl) 136 des_ctrl)
136 137
137BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,1, 138BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,1,
138 EVP_CIPH_RAND_KEY, des_init_key,NULL, 139 EVP_CIPH_RAND_KEY,
140 des_init_key, NULL,
139 EVP_CIPHER_set_asn1_iv, 141 EVP_CIPHER_set_asn1_iv,
140 EVP_CIPHER_get_asn1_iv,des_ctrl) 142 EVP_CIPHER_get_asn1_iv,des_ctrl)
141 143
142BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,8, 144BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,8,
143 EVP_CIPH_RAND_KEY,des_init_key,NULL, 145 EVP_CIPH_RAND_KEY,
146 des_init_key,NULL,
144 EVP_CIPHER_set_asn1_iv, 147 EVP_CIPHER_set_asn1_iv,
145 EVP_CIPHER_get_asn1_iv,des_ctrl) 148 EVP_CIPHER_get_asn1_iv,des_ctrl)
146 149
diff --git a/src/lib/libcrypto/evp/e_des3.c b/src/lib/libcrypto/evp/e_des3.c
index ac148efab2..f910af19b1 100644
--- a/src/lib/libcrypto/evp/e_des3.c
+++ b/src/lib/libcrypto/evp/e_des3.c
@@ -111,8 +111,7 @@ static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
111#ifdef KSSL_DEBUG 111#ifdef KSSL_DEBUG
112 { 112 {
113 int i; 113 int i;
114 char *cp; 114 printf("des_ede_cbc_cipher(ctx=%lx, buflen=%d)\n", (unsigned long)ctx, ctx->buf_len);
115 printf("des_ede_cbc_cipher(ctx=%lx, buflen=%d)\n", ctx, ctx->buf_len);
116 printf("\t iv= "); 115 printf("\t iv= ");
117 for(i=0;i<8;i++) 116 for(i=0;i<8;i++)
118 printf("%02X",ctx->iv[i]); 117 printf("%02X",ctx->iv[i]);
@@ -164,9 +163,9 @@ static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
164 } 163 }
165 164
166BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64, 165BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64,
167 EVP_CIPH_RAND_KEY, des_ede_init_key, NULL, 166 EVP_CIPH_RAND_KEY|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
168 EVP_CIPHER_set_asn1_iv, 167 des_ede_init_key,
169 EVP_CIPHER_get_asn1_iv, 168 NULL, NULL, NULL,
170 des3_ctrl) 169 des3_ctrl)
171 170
172#define des_ede3_cfb64_cipher des_ede_cfb64_cipher 171#define des_ede3_cfb64_cipher des_ede_cfb64_cipher
@@ -175,21 +174,21 @@ BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64,
175#define des_ede3_ecb_cipher des_ede_ecb_cipher 174#define des_ede3_ecb_cipher des_ede_ecb_cipher
176 175
177BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64, 176BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64,
178 EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, 177 EVP_CIPH_RAND_KEY|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
179 EVP_CIPHER_set_asn1_iv, 178 des_ede3_init_key,
180 EVP_CIPHER_get_asn1_iv, 179 NULL, NULL, NULL,
181 des3_ctrl) 180 des3_ctrl)
182 181
183BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,1, 182BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,1,
184 EVP_CIPH_RAND_KEY, des_ede3_init_key,NULL, 183 EVP_CIPH_RAND_KEY|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
185 EVP_CIPHER_set_asn1_iv, 184 des_ede3_init_key,
186 EVP_CIPHER_get_asn1_iv, 185 NULL, NULL, NULL,
187 des3_ctrl) 186 des3_ctrl)
188 187
189BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,8, 188BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,8,
190 EVP_CIPH_RAND_KEY, des_ede3_init_key,NULL, 189 EVP_CIPH_RAND_KEY|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
191 EVP_CIPHER_set_asn1_iv, 190 des_ede3_init_key,
192 EVP_CIPHER_get_asn1_iv, 191 NULL, NULL, NULL,
193 des3_ctrl) 192 des3_ctrl)
194 193
195static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 194static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
@@ -216,7 +215,7 @@ static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
216#ifdef KSSL_DEBUG 215#ifdef KSSL_DEBUG
217 { 216 {
218 int i; 217 int i;
219 printf("des_ede3_init_key(ctx=%lx)\n", ctx); 218 printf("des_ede3_init_key(ctx=%lx)\n", (unsigned long)ctx);
220 printf("\tKEY= "); 219 printf("\tKEY= ");
221 for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n"); 220 for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n");
222 printf("\t IV= "); 221 printf("\t IV= ");
diff --git a/src/lib/libcrypto/evp/e_null.c b/src/lib/libcrypto/evp/e_null.c
index 5205259f18..0872d733e4 100644
--- a/src/lib/libcrypto/evp/e_null.c
+++ b/src/lib/libcrypto/evp/e_null.c
@@ -69,7 +69,7 @@ static const EVP_CIPHER n_cipher=
69 { 69 {
70 NID_undef, 70 NID_undef,
71 1,0,0, 71 1,0,0,
72 0, 72 EVP_CIPH_FLAG_FIPS,
73 null_init_key, 73 null_init_key,
74 null_cipher, 74 null_cipher,
75 NULL, 75 NULL,
diff --git a/src/lib/libcrypto/evp/e_rc4.c b/src/lib/libcrypto/evp/e_rc4.c
index 67af850bea..55baad7446 100644
--- a/src/lib/libcrypto/evp/e_rc4.c
+++ b/src/lib/libcrypto/evp/e_rc4.c
@@ -64,6 +64,7 @@
64#include <openssl/evp.h> 64#include <openssl/evp.h>
65#include <openssl/objects.h> 65#include <openssl/objects.h>
66#include <openssl/rc4.h> 66#include <openssl/rc4.h>
67#include "evp_locl.h"
67 68
68/* FIXME: surely this is available elsewhere? */ 69/* FIXME: surely this is available elsewhere? */
69#define EVP_RC4_KEY_SIZE 16 70#define EVP_RC4_KEY_SIZE 16
diff --git a/src/lib/libcrypto/evp/evp.h b/src/lib/libcrypto/evp/evp.h
index bdd3b7ecaa..79c097181f 100644
--- a/src/lib/libcrypto/evp/evp.h
+++ b/src/lib/libcrypto/evp/evp.h
@@ -75,6 +75,10 @@
75#include <openssl/bio.h> 75#include <openssl/bio.h>
76#endif 76#endif
77 77
78#ifdef OPENSSL_FIPS
79#include <openssl/fips.h>
80#endif
81
78/* 82/*
79#define EVP_RC2_KEY_SIZE 16 83#define EVP_RC2_KEY_SIZE 16
80#define EVP_RC4_KEY_SIZE 16 84#define EVP_RC4_KEY_SIZE 16
@@ -250,9 +254,19 @@ typedef int evp_verify_method(int type,const unsigned char *m,
250 unsigned int m_length,const unsigned char *sigbuf, 254 unsigned int m_length,const unsigned char *sigbuf,
251 unsigned int siglen, void *key); 255 unsigned int siglen, void *key);
252 256
257typedef struct
258 {
259 EVP_MD_CTX *mctx;
260 void *key;
261 } EVP_MD_SVCTX;
262
253#define EVP_MD_FLAG_ONESHOT 0x0001 /* digest can only handle a single 263#define EVP_MD_FLAG_ONESHOT 0x0001 /* digest can only handle a single
254 * block */ 264 * block */
255 265
266#define EVP_MD_FLAG_FIPS 0x0400 /* Note if suitable for use in FIPS mode */
267
268#define EVP_MD_FLAG_SVCTX 0x0800 /* pass EVP_MD_SVCTX to sign/verify */
269
256#define EVP_PKEY_NULL_method NULL,NULL,{0,0,0,0} 270#define EVP_PKEY_NULL_method NULL,NULL,{0,0,0,0}
257 271
258#ifndef OPENSSL_NO_DSA 272#ifndef OPENSSL_NO_DSA
@@ -303,6 +317,17 @@ struct env_md_ctx_st
303 * cleaned */ 317 * cleaned */
304#define EVP_MD_CTX_FLAG_REUSE 0x0004 /* Don't free up ctx->md_data 318#define EVP_MD_CTX_FLAG_REUSE 0x0004 /* Don't free up ctx->md_data
305 * in EVP_MD_CTX_cleanup */ 319 * in EVP_MD_CTX_cleanup */
320#define EVP_MD_CTX_FLAG_NON_FIPS_ALLOW 0x0008 /* Allow use of non FIPS digest
321 * in FIPS mode */
322
323#define EVP_MD_CTX_FLAG_PAD_MASK 0xF0 /* RSA mode to use */
324#define EVP_MD_CTX_FLAG_PAD_PKCS1 0x00 /* PKCS#1 v1.5 mode */
325#define EVP_MD_CTX_FLAG_PAD_X931 0x10 /* X9.31 mode */
326#define EVP_MD_CTX_FLAG_PAD_PSS 0x20 /* PSS mode */
327#define M_EVP_MD_CTX_FLAG_PSS_SALT(ctx) \
328 ((ctx->flags>>16) &0xFFFF) /* seed length */
329#define EVP_MD_CTX_FLAG_PSS_MDLEN 0xFFFF /* salt len same as digest */
330#define EVP_MD_CTX_FLAG_PSS_MREC 0xFFFE /* salt max or auto recovered */
306 331
307struct evp_cipher_st 332struct evp_cipher_st
308 { 333 {
@@ -347,6 +372,14 @@ struct evp_cipher_st
347#define EVP_CIPH_NO_PADDING 0x100 372#define EVP_CIPH_NO_PADDING 0x100
348/* cipher handles random key generation */ 373/* cipher handles random key generation */
349#define EVP_CIPH_RAND_KEY 0x200 374#define EVP_CIPH_RAND_KEY 0x200
375/* Note if suitable for use in FIPS mode */
376#define EVP_CIPH_FLAG_FIPS 0x400
377/* Allow non FIPS cipher in FIPS mode */
378#define EVP_CIPH_FLAG_NON_FIPS_ALLOW 0x800
379/* Allow use default ASN1 get/set iv */
380#define EVP_CIPH_FLAG_DEFAULT_ASN1 0x1000
381/* Buffer length in bits not bytes: CFB1 mode only */
382#define EVP_CIPH_FLAG_LENGTH_BITS 0x2000
350 383
351/* ctrl() values */ 384/* ctrl() values */
352 385
@@ -429,6 +462,18 @@ typedef int (EVP_PBE_KEYGEN)(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
429#define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a)) 462#define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a))
430#define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a)) 463#define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a))
431 464
465/* Macros to reduce FIPS dependencies: do NOT use in applications */
466#define M_EVP_MD_size(e) ((e)->md_size)
467#define M_EVP_MD_block_size(e) ((e)->block_size)
468#define M_EVP_MD_CTX_set_flags(ctx,flgs) ((ctx)->flags|=(flgs))
469#define M_EVP_MD_CTX_clear_flags(ctx,flgs) ((ctx)->flags&=~(flgs))
470#define M_EVP_MD_CTX_test_flags(ctx,flgs) ((ctx)->flags&(flgs))
471#define M_EVP_MD_type(e) ((e)->type)
472#define M_EVP_MD_CTX_type(e) M_EVP_MD_type(M_EVP_MD_CTX_md(e))
473#define M_EVP_MD_CTX_md(e) ((e)->digest)
474
475#define M_EVP_CIPHER_CTX_set_flags(ctx,flgs) ((ctx)->flags|=(flgs))
476
432int EVP_MD_type(const EVP_MD *md); 477int EVP_MD_type(const EVP_MD *md);
433#define EVP_MD_nid(e) EVP_MD_type(e) 478#define EVP_MD_nid(e) EVP_MD_type(e)
434#define EVP_MD_name(e) OBJ_nid2sn(EVP_MD_nid(e)) 479#define EVP_MD_name(e) OBJ_nid2sn(EVP_MD_nid(e))
@@ -524,6 +569,10 @@ int EVP_BytesToKey(const EVP_CIPHER *type,const EVP_MD *md,
524 const unsigned char *salt, const unsigned char *data, 569 const unsigned char *salt, const unsigned char *data,
525 int datal, int count, unsigned char *key,unsigned char *iv); 570 int datal, int count, unsigned char *key,unsigned char *iv);
526 571
572void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags);
573void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags);
574int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx,int flags);
575
527int EVP_EncryptInit(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, 576int EVP_EncryptInit(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher,
528 const unsigned char *key, const unsigned char *iv); 577 const unsigned char *key, const unsigned char *iv);
529int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl, 578int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl,
@@ -879,6 +928,24 @@ int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
879 EVP_PBE_KEYGEN *keygen); 928 EVP_PBE_KEYGEN *keygen);
880void EVP_PBE_cleanup(void); 929void EVP_PBE_cleanup(void);
881 930
931#ifdef OPENSSL_FIPS
932#ifndef OPENSSL_NO_ENGINE
933void int_EVP_MD_set_engine_callbacks(
934 int (*eng_md_init)(ENGINE *impl),
935 int (*eng_md_fin)(ENGINE *impl),
936 int (*eng_md_evp)
937 (EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl));
938void int_EVP_MD_init_engine_callbacks(void);
939void int_EVP_CIPHER_set_engine_callbacks(
940 int (*eng_ciph_fin)(ENGINE *impl),
941 int (*eng_ciph_evp)
942 (EVP_CIPHER_CTX *ctx, const EVP_CIPHER **pciph, ENGINE *impl));
943void int_EVP_CIPHER_init_engine_callbacks(void);
944#endif
945#endif
946
947void EVP_add_alg_module(void);
948
882/* BEGIN ERROR CODES */ 949/* BEGIN ERROR CODES */
883/* The following lines are auto generated by the script mkerr.pl. Any changes 950/* The following lines are auto generated by the script mkerr.pl. Any changes
884 * made after this point may be overwritten when the script is next run. 951 * made after this point may be overwritten when the script is next run.
@@ -889,16 +956,23 @@ void ERR_load_EVP_strings(void);
889 956
890/* Function codes. */ 957/* Function codes. */
891#define EVP_F_AES_INIT_KEY 133 958#define EVP_F_AES_INIT_KEY 133
959#define EVP_F_ALG_MODULE_INIT 138
892#define EVP_F_CAMELLIA_INIT_KEY 159 960#define EVP_F_CAMELLIA_INIT_KEY 159
893#define EVP_F_D2I_PKEY 100 961#define EVP_F_D2I_PKEY 100
962#define EVP_F_DO_EVP_ENC_ENGINE 140
963#define EVP_F_DO_EVP_ENC_ENGINE_FULL 141
964#define EVP_F_DO_EVP_MD_ENGINE 139
965#define EVP_F_DO_EVP_MD_ENGINE_FULL 142
894#define EVP_F_DSAPKEY2PKCS8 134 966#define EVP_F_DSAPKEY2PKCS8 134
895#define EVP_F_DSA_PKEY2PKCS8 135 967#define EVP_F_DSA_PKEY2PKCS8 135
896#define EVP_F_ECDSA_PKEY2PKCS8 129 968#define EVP_F_ECDSA_PKEY2PKCS8 129
897#define EVP_F_ECKEY_PKEY2PKCS8 132 969#define EVP_F_ECKEY_PKEY2PKCS8 132
970#define EVP_F_EVP_CIPHERINIT 137
898#define EVP_F_EVP_CIPHERINIT_EX 123 971#define EVP_F_EVP_CIPHERINIT_EX 123
899#define EVP_F_EVP_CIPHER_CTX_CTRL 124 972#define EVP_F_EVP_CIPHER_CTX_CTRL 124
900#define EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH 122 973#define EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH 122
901#define EVP_F_EVP_DECRYPTFINAL_EX 101 974#define EVP_F_EVP_DECRYPTFINAL_EX 101
975#define EVP_F_EVP_DIGESTINIT 136
902#define EVP_F_EVP_DIGESTINIT_EX 128 976#define EVP_F_EVP_DIGESTINIT_EX 128
903#define EVP_F_EVP_ENCRYPTFINAL_EX 127 977#define EVP_F_EVP_ENCRYPTFINAL_EX 127
904#define EVP_F_EVP_MD_CTX_COPY_EX 110 978#define EVP_F_EVP_MD_CTX_COPY_EX 110
@@ -940,15 +1014,20 @@ void ERR_load_EVP_strings(void);
940#define EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH 138 1014#define EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH 138
941#define EVP_R_DECODE_ERROR 114 1015#define EVP_R_DECODE_ERROR 114
942#define EVP_R_DIFFERENT_KEY_TYPES 101 1016#define EVP_R_DIFFERENT_KEY_TYPES 101
1017#define EVP_R_DISABLED_FOR_FIPS 144
943#define EVP_R_ENCODE_ERROR 115 1018#define EVP_R_ENCODE_ERROR 115
1019#define EVP_R_ERROR_LOADING_SECTION 145
1020#define EVP_R_ERROR_SETTING_FIPS_MODE 146
944#define EVP_R_EVP_PBE_CIPHERINIT_ERROR 119 1021#define EVP_R_EVP_PBE_CIPHERINIT_ERROR 119
945#define EVP_R_EXPECTING_AN_RSA_KEY 127 1022#define EVP_R_EXPECTING_AN_RSA_KEY 127
946#define EVP_R_EXPECTING_A_DH_KEY 128 1023#define EVP_R_EXPECTING_A_DH_KEY 128
947#define EVP_R_EXPECTING_A_DSA_KEY 129 1024#define EVP_R_EXPECTING_A_DSA_KEY 129
948#define EVP_R_EXPECTING_A_ECDSA_KEY 141 1025#define EVP_R_EXPECTING_A_ECDSA_KEY 141
949#define EVP_R_EXPECTING_A_EC_KEY 142 1026#define EVP_R_EXPECTING_A_EC_KEY 142
1027#define EVP_R_FIPS_MODE_NOT_SUPPORTED 147
950#define EVP_R_INITIALIZATION_ERROR 134 1028#define EVP_R_INITIALIZATION_ERROR 134
951#define EVP_R_INPUT_NOT_INITIALIZED 111 1029#define EVP_R_INPUT_NOT_INITIALIZED 111
1030#define EVP_R_INVALID_FIPS_MODE 148
952#define EVP_R_INVALID_KEY_LENGTH 130 1031#define EVP_R_INVALID_KEY_LENGTH 130
953#define EVP_R_IV_TOO_LARGE 102 1032#define EVP_R_IV_TOO_LARGE 102
954#define EVP_R_KEYGEN_FAILURE 120 1033#define EVP_R_KEYGEN_FAILURE 120
@@ -960,6 +1039,7 @@ void ERR_load_EVP_strings(void);
960#define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105 1039#define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105
961#define EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE 117 1040#define EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE 117
962#define EVP_R_PUBLIC_KEY_NOT_RSA 106 1041#define EVP_R_PUBLIC_KEY_NOT_RSA 106
1042#define EVP_R_UNKNOWN_OPTION 149
963#define EVP_R_UNKNOWN_PBE_ALGORITHM 121 1043#define EVP_R_UNKNOWN_PBE_ALGORITHM 121
964#define EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS 135 1044#define EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS 135
965#define EVP_R_UNSUPPORTED_CIPHER 107 1045#define EVP_R_UNSUPPORTED_CIPHER 107
diff --git a/src/lib/libcrypto/evp/evp_enc.c b/src/lib/libcrypto/evp/evp_enc.c
index a1904993bf..30e0ca4d9f 100644
--- a/src/lib/libcrypto/evp/evp_enc.c
+++ b/src/lib/libcrypto/evp/evp_enc.c
@@ -66,13 +66,15 @@
66#endif 66#endif
67#include "evp_locl.h" 67#include "evp_locl.h"
68 68
69const char EVP_version[]="EVP" OPENSSL_VERSION_PTEXT; 69#ifdef OPENSSL_FIPS
70 #define M_do_cipher(ctx, out, in, inl) \
71 EVP_Cipher(ctx,out,in,inl)
72#else
73 #define M_do_cipher(ctx, out, in, inl) \
74 ctx->cipher->do_cipher(ctx,out,in,inl)
75#endif
70 76
71void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) 77const char EVP_version[]="EVP" OPENSSL_VERSION_PTEXT;
72 {
73 memset(ctx,0,sizeof(EVP_CIPHER_CTX));
74 /* ctx->cipher=NULL; */
75 }
76 78
77EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) 79EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
78 { 80 {
@@ -90,144 +92,6 @@ int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
90 return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc); 92 return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc);
91 } 93 }
92 94
93int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
94 const unsigned char *key, const unsigned char *iv, int enc)
95 {
96 if (enc == -1)
97 enc = ctx->encrypt;
98 else
99 {
100 if (enc)
101 enc = 1;
102 ctx->encrypt = enc;
103 }
104#ifndef OPENSSL_NO_ENGINE
105 /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts
106 * so this context may already have an ENGINE! Try to avoid releasing
107 * the previous handle, re-querying for an ENGINE, and having a
108 * reinitialisation, when it may all be unecessary. */
109 if (ctx->engine && ctx->cipher && (!cipher ||
110 (cipher && (cipher->nid == ctx->cipher->nid))))
111 goto skip_to_init;
112#endif
113 if (cipher)
114 {
115 /* Ensure a context left lying around from last time is cleared
116 * (the previous check attempted to avoid this if the same
117 * ENGINE and EVP_CIPHER could be used). */
118 EVP_CIPHER_CTX_cleanup(ctx);
119
120 /* Restore encrypt field: it is zeroed by cleanup */
121 ctx->encrypt = enc;
122#ifndef OPENSSL_NO_ENGINE
123 if(impl)
124 {
125 if (!ENGINE_init(impl))
126 {
127 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
128 return 0;
129 }
130 }
131 else
132 /* Ask if an ENGINE is reserved for this job */
133 impl = ENGINE_get_cipher_engine(cipher->nid);
134 if(impl)
135 {
136 /* There's an ENGINE for this job ... (apparently) */
137 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
138 if(!c)
139 {
140 /* One positive side-effect of US's export
141 * control history, is that we should at least
142 * be able to avoid using US mispellings of
143 * "initialisation"? */
144 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
145 return 0;
146 }
147 /* We'll use the ENGINE's private cipher definition */
148 cipher = c;
149 /* Store the ENGINE functional reference so we know
150 * 'cipher' came from an ENGINE and we need to release
151 * it when done. */
152 ctx->engine = impl;
153 }
154 else
155 ctx->engine = NULL;
156#endif
157
158 ctx->cipher=cipher;
159 if (ctx->cipher->ctx_size)
160 {
161 ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size);
162 if (!ctx->cipher_data)
163 {
164 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
165 return 0;
166 }
167 }
168 else
169 {
170 ctx->cipher_data = NULL;
171 }
172 ctx->key_len = cipher->key_len;
173 ctx->flags = 0;
174 if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT)
175 {
176 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL))
177 {
178 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
179 return 0;
180 }
181 }
182 }
183 else if(!ctx->cipher)
184 {
185 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
186 return 0;
187 }
188#ifndef OPENSSL_NO_ENGINE
189skip_to_init:
190#endif
191 /* we assume block size is a power of 2 in *cryptUpdate */
192 OPENSSL_assert(ctx->cipher->block_size == 1
193 || ctx->cipher->block_size == 8
194 || ctx->cipher->block_size == 16);
195
196 if(!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
197 switch(EVP_CIPHER_CTX_mode(ctx)) {
198
199 case EVP_CIPH_STREAM_CIPHER:
200 case EVP_CIPH_ECB_MODE:
201 break;
202
203 case EVP_CIPH_CFB_MODE:
204 case EVP_CIPH_OFB_MODE:
205
206 ctx->num = 0;
207
208 case EVP_CIPH_CBC_MODE:
209
210 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
211 (int)sizeof(ctx->iv));
212 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
213 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
214 break;
215
216 default:
217 return 0;
218 break;
219 }
220 }
221
222 if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
223 if(!ctx->cipher->init(ctx,key,iv,enc)) return 0;
224 }
225 ctx->buf_len=0;
226 ctx->final_used=0;
227 ctx->block_mask=ctx->cipher->block_size-1;
228 return 1;
229 }
230
231int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, 95int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
232 const unsigned char *in, int inl) 96 const unsigned char *in, int inl)
233 { 97 {
@@ -279,10 +143,15 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
279 { 143 {
280 int i,j,bl; 144 int i,j,bl;
281 145
282 OPENSSL_assert(inl > 0); 146 if (inl <= 0)
147 {
148 *outl = 0;
149 return inl == 0;
150 }
151
283 if(ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0) 152 if(ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0)
284 { 153 {
285 if(ctx->cipher->do_cipher(ctx,out,in,inl)) 154 if(M_do_cipher(ctx,out,in,inl))
286 { 155 {
287 *outl=inl; 156 *outl=inl;
288 return 1; 157 return 1;
@@ -309,7 +178,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
309 { 178 {
310 j=bl-i; 179 j=bl-i;
311 memcpy(&(ctx->buf[i]),in,j); 180 memcpy(&(ctx->buf[i]),in,j);
312 if(!ctx->cipher->do_cipher(ctx,out,ctx->buf,bl)) return 0; 181 if(!M_do_cipher(ctx,out,ctx->buf,bl)) return 0;
313 inl-=j; 182 inl-=j;
314 in+=j; 183 in+=j;
315 out+=bl; 184 out+=bl;
@@ -322,7 +191,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
322 inl-=i; 191 inl-=i;
323 if (inl > 0) 192 if (inl > 0)
324 { 193 {
325 if(!ctx->cipher->do_cipher(ctx,out,in,inl)) return 0; 194 if(!M_do_cipher(ctx,out,in,inl)) return 0;
326 *outl+=inl; 195 *outl+=inl;
327 } 196 }
328 197
@@ -366,7 +235,7 @@ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
366 n=b-bl; 235 n=b-bl;
367 for (i=bl; i<b; i++) 236 for (i=bl; i<b; i++)
368 ctx->buf[i]=n; 237 ctx->buf[i]=n;
369 ret=ctx->cipher->do_cipher(ctx,out,ctx->buf,b); 238 ret=M_do_cipher(ctx,out,ctx->buf,b);
370 239
371 240
372 if(ret) 241 if(ret)
@@ -381,10 +250,10 @@ int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
381 int fix_len; 250 int fix_len;
382 unsigned int b; 251 unsigned int b;
383 252
384 if (inl == 0) 253 if (inl <= 0)
385 { 254 {
386 *outl=0; 255 *outl = 0;
387 return 1; 256 return inl == 0;
388 } 257 }
389 258
390 if (ctx->flags & EVP_CIPH_NO_PADDING) 259 if (ctx->flags & EVP_CIPH_NO_PADDING)
@@ -488,28 +357,6 @@ void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
488 } 357 }
489 } 358 }
490 359
491int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
492 {
493 if (c->cipher != NULL)
494 {
495 if(c->cipher->cleanup && !c->cipher->cleanup(c))
496 return 0;
497 /* Cleanse cipher context data */
498 if (c->cipher_data)
499 OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
500 }
501 if (c->cipher_data)
502 OPENSSL_free(c->cipher_data);
503#ifndef OPENSSL_NO_ENGINE
504 if (c->engine)
505 /* The EVP_CIPHER we used belongs to an ENGINE, release the
506 * functional reference we held for this reason. */
507 ENGINE_finish(c->engine);
508#endif
509 memset(c,0,sizeof(EVP_CIPHER_CTX));
510 return 1;
511 }
512
513int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) 360int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
514 { 361 {
515 if(c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) 362 if(c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
@@ -531,27 +378,6 @@ int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
531 return 1; 378 return 1;
532 } 379 }
533 380
534int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
535{
536 int ret;
537 if(!ctx->cipher) {
538 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
539 return 0;
540 }
541
542 if(!ctx->cipher->ctrl) {
543 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
544 return 0;
545 }
546
547 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
548 if(ret == -1) {
549 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
550 return 0;
551 }
552 return ret;
553}
554
555int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key) 381int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
556 { 382 {
557 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) 383 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
@@ -561,3 +387,54 @@ int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
561 return 1; 387 return 1;
562 } 388 }
563 389
390#ifndef OPENSSL_NO_ENGINE
391
392#ifdef OPENSSL_FIPS
393
394static int do_evp_enc_engine_full(EVP_CIPHER_CTX *ctx, const EVP_CIPHER **pcipher, ENGINE *impl)
395 {
396 if(impl)
397 {
398 if (!ENGINE_init(impl))
399 {
400 EVPerr(EVP_F_DO_EVP_ENC_ENGINE_FULL, EVP_R_INITIALIZATION_ERROR);
401 return 0;
402 }
403 }
404 else
405 /* Ask if an ENGINE is reserved for this job */
406 impl = ENGINE_get_cipher_engine((*pcipher)->nid);
407 if(impl)
408 {
409 /* There's an ENGINE for this job ... (apparently) */
410 const EVP_CIPHER *c = ENGINE_get_cipher(impl, (*pcipher)->nid);
411 if(!c)
412 {
413 /* One positive side-effect of US's export
414 * control history, is that we should at least
415 * be able to avoid using US mispellings of
416 * "initialisation"? */
417 EVPerr(EVP_F_DO_EVP_ENC_ENGINE_FULL, EVP_R_INITIALIZATION_ERROR);
418 return 0;
419 }
420 /* We'll use the ENGINE's private cipher definition */
421 *pcipher = c;
422 /* Store the ENGINE functional reference so we know
423 * 'cipher' came from an ENGINE and we need to release
424 * it when done. */
425 ctx->engine = impl;
426 }
427 else
428 ctx->engine = NULL;
429 return 1;
430 }
431
432void int_EVP_CIPHER_init_engine_callbacks(void)
433 {
434 int_EVP_CIPHER_set_engine_callbacks(
435 ENGINE_finish, do_evp_enc_engine_full);
436 }
437
438#endif
439
440#endif
diff --git a/src/lib/libcrypto/evp/evp_err.c b/src/lib/libcrypto/evp/evp_err.c
index e8c9e8de9c..b5b900d4fe 100644
--- a/src/lib/libcrypto/evp/evp_err.c
+++ b/src/lib/libcrypto/evp/evp_err.c
@@ -1,6 +1,6 @@
1/* crypto/evp/evp_err.c */ 1/* crypto/evp/evp_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -71,16 +71,23 @@
71static ERR_STRING_DATA EVP_str_functs[]= 71static ERR_STRING_DATA EVP_str_functs[]=
72 { 72 {
73{ERR_FUNC(EVP_F_AES_INIT_KEY), "AES_INIT_KEY"}, 73{ERR_FUNC(EVP_F_AES_INIT_KEY), "AES_INIT_KEY"},
74{ERR_FUNC(EVP_F_ALG_MODULE_INIT), "ALG_MODULE_INIT"},
74{ERR_FUNC(EVP_F_CAMELLIA_INIT_KEY), "CAMELLIA_INIT_KEY"}, 75{ERR_FUNC(EVP_F_CAMELLIA_INIT_KEY), "CAMELLIA_INIT_KEY"},
75{ERR_FUNC(EVP_F_D2I_PKEY), "D2I_PKEY"}, 76{ERR_FUNC(EVP_F_D2I_PKEY), "D2I_PKEY"},
77{ERR_FUNC(EVP_F_DO_EVP_ENC_ENGINE), "DO_EVP_ENC_ENGINE"},
78{ERR_FUNC(EVP_F_DO_EVP_ENC_ENGINE_FULL), "DO_EVP_ENC_ENGINE_FULL"},
79{ERR_FUNC(EVP_F_DO_EVP_MD_ENGINE), "DO_EVP_MD_ENGINE"},
80{ERR_FUNC(EVP_F_DO_EVP_MD_ENGINE_FULL), "DO_EVP_MD_ENGINE_FULL"},
76{ERR_FUNC(EVP_F_DSAPKEY2PKCS8), "DSAPKEY2PKCS8"}, 81{ERR_FUNC(EVP_F_DSAPKEY2PKCS8), "DSAPKEY2PKCS8"},
77{ERR_FUNC(EVP_F_DSA_PKEY2PKCS8), "DSA_PKEY2PKCS8"}, 82{ERR_FUNC(EVP_F_DSA_PKEY2PKCS8), "DSA_PKEY2PKCS8"},
78{ERR_FUNC(EVP_F_ECDSA_PKEY2PKCS8), "ECDSA_PKEY2PKCS8"}, 83{ERR_FUNC(EVP_F_ECDSA_PKEY2PKCS8), "ECDSA_PKEY2PKCS8"},
79{ERR_FUNC(EVP_F_ECKEY_PKEY2PKCS8), "ECKEY_PKEY2PKCS8"}, 84{ERR_FUNC(EVP_F_ECKEY_PKEY2PKCS8), "ECKEY_PKEY2PKCS8"},
85{ERR_FUNC(EVP_F_EVP_CIPHERINIT), "EVP_CipherInit"},
80{ERR_FUNC(EVP_F_EVP_CIPHERINIT_EX), "EVP_CipherInit_ex"}, 86{ERR_FUNC(EVP_F_EVP_CIPHERINIT_EX), "EVP_CipherInit_ex"},
81{ERR_FUNC(EVP_F_EVP_CIPHER_CTX_CTRL), "EVP_CIPHER_CTX_ctrl"}, 87{ERR_FUNC(EVP_F_EVP_CIPHER_CTX_CTRL), "EVP_CIPHER_CTX_ctrl"},
82{ERR_FUNC(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH), "EVP_CIPHER_CTX_set_key_length"}, 88{ERR_FUNC(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH), "EVP_CIPHER_CTX_set_key_length"},
83{ERR_FUNC(EVP_F_EVP_DECRYPTFINAL_EX), "EVP_DecryptFinal_ex"}, 89{ERR_FUNC(EVP_F_EVP_DECRYPTFINAL_EX), "EVP_DecryptFinal_ex"},
90{ERR_FUNC(EVP_F_EVP_DIGESTINIT), "EVP_DigestInit"},
84{ERR_FUNC(EVP_F_EVP_DIGESTINIT_EX), "EVP_DigestInit_ex"}, 91{ERR_FUNC(EVP_F_EVP_DIGESTINIT_EX), "EVP_DigestInit_ex"},
85{ERR_FUNC(EVP_F_EVP_ENCRYPTFINAL_EX), "EVP_EncryptFinal_ex"}, 92{ERR_FUNC(EVP_F_EVP_ENCRYPTFINAL_EX), "EVP_EncryptFinal_ex"},
86{ERR_FUNC(EVP_F_EVP_MD_CTX_COPY_EX), "EVP_MD_CTX_copy_ex"}, 93{ERR_FUNC(EVP_F_EVP_MD_CTX_COPY_EX), "EVP_MD_CTX_copy_ex"},
@@ -125,15 +132,20 @@ static ERR_STRING_DATA EVP_str_reasons[]=
125{ERR_REASON(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH),"data not multiple of block length"}, 132{ERR_REASON(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH),"data not multiple of block length"},
126{ERR_REASON(EVP_R_DECODE_ERROR) ,"decode error"}, 133{ERR_REASON(EVP_R_DECODE_ERROR) ,"decode error"},
127{ERR_REASON(EVP_R_DIFFERENT_KEY_TYPES) ,"different key types"}, 134{ERR_REASON(EVP_R_DIFFERENT_KEY_TYPES) ,"different key types"},
135{ERR_REASON(EVP_R_DISABLED_FOR_FIPS) ,"disabled for fips"},
128{ERR_REASON(EVP_R_ENCODE_ERROR) ,"encode error"}, 136{ERR_REASON(EVP_R_ENCODE_ERROR) ,"encode error"},
137{ERR_REASON(EVP_R_ERROR_LOADING_SECTION) ,"error loading section"},
138{ERR_REASON(EVP_R_ERROR_SETTING_FIPS_MODE),"error setting fips mode"},
129{ERR_REASON(EVP_R_EVP_PBE_CIPHERINIT_ERROR),"evp pbe cipherinit error"}, 139{ERR_REASON(EVP_R_EVP_PBE_CIPHERINIT_ERROR),"evp pbe cipherinit error"},
130{ERR_REASON(EVP_R_EXPECTING_AN_RSA_KEY) ,"expecting an rsa key"}, 140{ERR_REASON(EVP_R_EXPECTING_AN_RSA_KEY) ,"expecting an rsa key"},
131{ERR_REASON(EVP_R_EXPECTING_A_DH_KEY) ,"expecting a dh key"}, 141{ERR_REASON(EVP_R_EXPECTING_A_DH_KEY) ,"expecting a dh key"},
132{ERR_REASON(EVP_R_EXPECTING_A_DSA_KEY) ,"expecting a dsa key"}, 142{ERR_REASON(EVP_R_EXPECTING_A_DSA_KEY) ,"expecting a dsa key"},
133{ERR_REASON(EVP_R_EXPECTING_A_ECDSA_KEY) ,"expecting a ecdsa key"}, 143{ERR_REASON(EVP_R_EXPECTING_A_ECDSA_KEY) ,"expecting a ecdsa key"},
134{ERR_REASON(EVP_R_EXPECTING_A_EC_KEY) ,"expecting a ec key"}, 144{ERR_REASON(EVP_R_EXPECTING_A_EC_KEY) ,"expecting a ec key"},
145{ERR_REASON(EVP_R_FIPS_MODE_NOT_SUPPORTED),"fips mode not supported"},
135{ERR_REASON(EVP_R_INITIALIZATION_ERROR) ,"initialization error"}, 146{ERR_REASON(EVP_R_INITIALIZATION_ERROR) ,"initialization error"},
136{ERR_REASON(EVP_R_INPUT_NOT_INITIALIZED) ,"input not initialized"}, 147{ERR_REASON(EVP_R_INPUT_NOT_INITIALIZED) ,"input not initialized"},
148{ERR_REASON(EVP_R_INVALID_FIPS_MODE) ,"invalid fips mode"},
137{ERR_REASON(EVP_R_INVALID_KEY_LENGTH) ,"invalid key length"}, 149{ERR_REASON(EVP_R_INVALID_KEY_LENGTH) ,"invalid key length"},
138{ERR_REASON(EVP_R_IV_TOO_LARGE) ,"iv too large"}, 150{ERR_REASON(EVP_R_IV_TOO_LARGE) ,"iv too large"},
139{ERR_REASON(EVP_R_KEYGEN_FAILURE) ,"keygen failure"}, 151{ERR_REASON(EVP_R_KEYGEN_FAILURE) ,"keygen failure"},
@@ -145,6 +157,8 @@ static ERR_STRING_DATA EVP_str_reasons[]=
145{ERR_REASON(EVP_R_NO_VERIFY_FUNCTION_CONFIGURED),"no verify function configured"}, 157{ERR_REASON(EVP_R_NO_VERIFY_FUNCTION_CONFIGURED),"no verify function configured"},
146{ERR_REASON(EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE),"pkcs8 unknown broken type"}, 158{ERR_REASON(EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE),"pkcs8 unknown broken type"},
147{ERR_REASON(EVP_R_PUBLIC_KEY_NOT_RSA) ,"public key not rsa"}, 159{ERR_REASON(EVP_R_PUBLIC_KEY_NOT_RSA) ,"public key not rsa"},
160{ERR_REASON(EVP_R_SEED_KEY_SETUP_FAILED) ,"seed key setup failed"},
161{ERR_REASON(EVP_R_UNKNOWN_OPTION) ,"unknown option"},
148{ERR_REASON(EVP_R_UNKNOWN_PBE_ALGORITHM) ,"unknown pbe algorithm"}, 162{ERR_REASON(EVP_R_UNKNOWN_PBE_ALGORITHM) ,"unknown pbe algorithm"},
149{ERR_REASON(EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS),"unsuported number of rounds"}, 163{ERR_REASON(EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS),"unsuported number of rounds"},
150{ERR_REASON(EVP_R_UNSUPPORTED_CIPHER) ,"unsupported cipher"}, 164{ERR_REASON(EVP_R_UNSUPPORTED_CIPHER) ,"unsupported cipher"},
diff --git a/src/lib/libcrypto/evp/evp_lib.c b/src/lib/libcrypto/evp/evp_lib.c
index edb28ef38e..174cf6c594 100644
--- a/src/lib/libcrypto/evp/evp_lib.c
+++ b/src/lib/libcrypto/evp/evp_lib.c
@@ -67,6 +67,8 @@ int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
67 67
68 if (c->cipher->set_asn1_parameters != NULL) 68 if (c->cipher->set_asn1_parameters != NULL)
69 ret=c->cipher->set_asn1_parameters(c,type); 69 ret=c->cipher->set_asn1_parameters(c,type);
70 else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1)
71 ret=EVP_CIPHER_set_asn1_iv(c, type);
70 else 72 else
71 ret=-1; 73 ret=-1;
72 return(ret); 74 return(ret);
@@ -78,6 +80,8 @@ int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
78 80
79 if (c->cipher->get_asn1_parameters != NULL) 81 if (c->cipher->get_asn1_parameters != NULL)
80 ret=c->cipher->get_asn1_parameters(c,type); 82 ret=c->cipher->get_asn1_parameters(c,type);
83 else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1)
84 ret=EVP_CIPHER_get_asn1_iv(c, type);
81 else 85 else
82 ret=-1; 86 ret=-1;
83 return(ret); 87 return(ret);
@@ -178,11 +182,6 @@ int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
178 return ctx->cipher->block_size; 182 return ctx->cipher->block_size;
179 } 183 }
180 184
181int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl)
182 {
183 return ctx->cipher->do_cipher(ctx,out,in,inl);
184 }
185
186const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) 185const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
187 { 186 {
188 return ctx->cipher; 187 return ctx->cipher;
@@ -193,11 +192,6 @@ unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
193 return cipher->flags; 192 return cipher->flags;
194 } 193 }
195 194
196unsigned long EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
197 {
198 return ctx->cipher->flags;
199 }
200
201void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) 195void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
202 { 196 {
203 return ctx->app_data; 197 return ctx->app_data;
@@ -213,11 +207,6 @@ int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
213 return cipher->iv_len; 207 return cipher->iv_len;
214 } 208 }
215 209
216int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
217 {
218 return ctx->cipher->iv_len;
219 }
220
221int EVP_CIPHER_key_length(const EVP_CIPHER *cipher) 210int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
222 { 211 {
223 return cipher->key_len; 212 return cipher->key_len;
@@ -228,11 +217,6 @@ int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
228 return ctx->key_len; 217 return ctx->key_len;
229 } 218 }
230 219
231int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
232 {
233 return cipher->nid;
234 }
235
236int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) 220int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
237 { 221 {
238 return ctx->cipher->nid; 222 return ctx->cipher->nid;
@@ -277,3 +261,18 @@ int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
277 { 261 {
278 return (ctx->flags & flags); 262 return (ctx->flags & flags);
279 } 263 }
264
265void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
266 {
267 ctx->flags |= flags;
268 }
269
270void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
271 {
272 ctx->flags &= ~flags;
273 }
274
275int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
276 {
277 return (ctx->flags & flags);
278 }
diff --git a/src/lib/libcrypto/evp/evp_locl.h b/src/lib/libcrypto/evp/evp_locl.h
index 073b0adcff..eabcc96f30 100644
--- a/src/lib/libcrypto/evp/evp_locl.h
+++ b/src/lib/libcrypto/evp/evp_locl.h
@@ -1,5 +1,5 @@
1/* evp_locl.h */ 1/* evp_locl.h */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -92,7 +92,7 @@ static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const uns
92#define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ 92#define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
93static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \ 93static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \
94{\ 94{\
95 cprefix##_cfb##cbits##_encrypt(in, out, (long)(cbits==1?inl*8:inl), &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num, ctx->encrypt);\ 95 cprefix##_cfb##cbits##_encrypt(in, out, (long)((cbits==1) && !(ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) ?inl*8:inl), &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num, ctx->encrypt);\
96 return 1;\ 96 return 1;\
97} 97}
98 98
@@ -226,11 +226,27 @@ const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
226 226
227#define EVP_C_DATA(kstruct, ctx) ((kstruct *)(ctx)->cipher_data) 227#define EVP_C_DATA(kstruct, ctx) ((kstruct *)(ctx)->cipher_data)
228 228
229#define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len) \ 229#define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
230 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \ 230 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
231 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \ 231 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
232 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \ 232 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
233 0, cipher##_init_key, NULL, \ 233 (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
234 EVP_CIPHER_set_asn1_iv, \ 234 cipher##_init_key, NULL, NULL, NULL, NULL)
235 EVP_CIPHER_get_asn1_iv, \ 235
236 NULL) 236#ifdef OPENSSL_FIPS
237#define RC2_set_key private_RC2_set_key
238#define RC4_set_key private_RC4_set_key
239#define CAST_set_key private_CAST_set_key
240#define RC5_32_set_key private_RC5_32_set_key
241#define BF_set_key private_BF_set_key
242#define Camellia_set_key private_Camellia_set_key
243#define idea_set_encrypt_key private_idea_set_encrypt_key
244
245#define MD5_Init private_MD5_Init
246#define MD4_Init private_MD4_Init
247#define MD2_Init private_MD2_Init
248#define MDC2_Init private_MDC2_Init
249#define SHA_Init private_SHA_Init
250
251#endif
252
diff --git a/src/lib/libcrypto/evp/evp_pbe.c b/src/lib/libcrypto/evp/evp_pbe.c
index c26d2de0f3..5e830be65f 100644
--- a/src/lib/libcrypto/evp/evp_pbe.c
+++ b/src/lib/libcrypto/evp/evp_pbe.c
@@ -1,5 +1,5 @@
1/* evp_pbe.c */ 1/* evp_pbe.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/evp/evp_pkey.c b/src/lib/libcrypto/evp/evp_pkey.c
index 0147f3e02a..10d9e9e772 100644
--- a/src/lib/libcrypto/evp/evp_pkey.c
+++ b/src/lib/libcrypto/evp/evp_pkey.c
@@ -1,5 +1,5 @@
1/* evp_pkey.c */ 1/* evp_pkey.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/evp/m_dss.c b/src/lib/libcrypto/evp/m_dss.c
index a948c77fa4..6b0c0aa7a3 100644
--- a/src/lib/libcrypto/evp/m_dss.c
+++ b/src/lib/libcrypto/evp/m_dss.c
@@ -81,7 +81,7 @@ static const EVP_MD dsa_md=
81 NID_dsaWithSHA, 81 NID_dsaWithSHA,
82 NID_dsaWithSHA, 82 NID_dsaWithSHA,
83 SHA_DIGEST_LENGTH, 83 SHA_DIGEST_LENGTH,
84 0, 84 EVP_MD_FLAG_FIPS,
85 init, 85 init,
86 update, 86 update,
87 final, 87 final,
diff --git a/src/lib/libcrypto/evp/m_dss1.c b/src/lib/libcrypto/evp/m_dss1.c
index c12e13972b..da8babc147 100644
--- a/src/lib/libcrypto/evp/m_dss1.c
+++ b/src/lib/libcrypto/evp/m_dss1.c
@@ -68,6 +68,8 @@
68#include <openssl/dsa.h> 68#include <openssl/dsa.h>
69#endif 69#endif
70 70
71#ifndef OPENSSL_FIPS
72
71static int init(EVP_MD_CTX *ctx) 73static int init(EVP_MD_CTX *ctx)
72 { return SHA1_Init(ctx->md_data); } 74 { return SHA1_Init(ctx->md_data); }
73 75
@@ -98,3 +100,4 @@ const EVP_MD *EVP_dss1(void)
98 return(&dss1_md); 100 return(&dss1_md);
99 } 101 }
100#endif 102#endif
103#endif
diff --git a/src/lib/libcrypto/evp/m_md4.c b/src/lib/libcrypto/evp/m_md4.c
index 1e0b7c5b42..5cd2ab5ade 100644
--- a/src/lib/libcrypto/evp/m_md4.c
+++ b/src/lib/libcrypto/evp/m_md4.c
@@ -58,6 +58,7 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include "evp_locl.h"
61 62
62#ifndef OPENSSL_NO_MD4 63#ifndef OPENSSL_NO_MD4
63 64
diff --git a/src/lib/libcrypto/evp/m_md5.c b/src/lib/libcrypto/evp/m_md5.c
index 63c142119e..6455829671 100644
--- a/src/lib/libcrypto/evp/m_md5.c
+++ b/src/lib/libcrypto/evp/m_md5.c
@@ -62,6 +62,7 @@
62#ifndef OPENSSL_NO_MD5 62#ifndef OPENSSL_NO_MD5
63 63
64#include <openssl/evp.h> 64#include <openssl/evp.h>
65#include "evp_locl.h"
65#include <openssl/objects.h> 66#include <openssl/objects.h>
66#include <openssl/x509.h> 67#include <openssl/x509.h>
67#include <openssl/md5.h> 68#include <openssl/md5.h>
diff --git a/src/lib/libcrypto/evp/m_sha1.c b/src/lib/libcrypto/evp/m_sha1.c
index 4679b1c463..471ec30be0 100644
--- a/src/lib/libcrypto/evp/m_sha1.c
+++ b/src/lib/libcrypto/evp/m_sha1.c
@@ -68,6 +68,8 @@
68#include <openssl/rsa.h> 68#include <openssl/rsa.h>
69#endif 69#endif
70 70
71#ifndef OPENSSL_FIPS
72
71static int init(EVP_MD_CTX *ctx) 73static int init(EVP_MD_CTX *ctx)
72 { return SHA1_Init(ctx->md_data); } 74 { return SHA1_Init(ctx->md_data); }
73 75
@@ -97,7 +99,6 @@ const EVP_MD *EVP_sha1(void)
97 { 99 {
98 return(&sha1_md); 100 return(&sha1_md);
99 } 101 }
100#endif
101 102
102#ifndef OPENSSL_NO_SHA256 103#ifndef OPENSSL_NO_SHA256
103static int init224(EVP_MD_CTX *ctx) 104static int init224(EVP_MD_CTX *ctx)
@@ -202,3 +203,7 @@ static const EVP_MD sha512_md=
202const EVP_MD *EVP_sha512(void) 203const EVP_MD *EVP_sha512(void)
203 { return(&sha512_md); } 204 { return(&sha512_md); }
204#endif /* ifndef OPENSSL_NO_SHA512 */ 205#endif /* ifndef OPENSSL_NO_SHA512 */
206
207#endif
208
209#endif
diff --git a/src/lib/libcrypto/evp/names.c b/src/lib/libcrypto/evp/names.c
index 88c1e780dd..e2e04c3570 100644
--- a/src/lib/libcrypto/evp/names.c
+++ b/src/lib/libcrypto/evp/names.c
@@ -66,6 +66,10 @@ int EVP_add_cipher(const EVP_CIPHER *c)
66 { 66 {
67 int r; 67 int r;
68 68
69#ifdef OPENSSL_FIPS
70 OPENSSL_init();
71#endif
72
69 r=OBJ_NAME_add(OBJ_nid2sn(c->nid),OBJ_NAME_TYPE_CIPHER_METH,(const char *)c); 73 r=OBJ_NAME_add(OBJ_nid2sn(c->nid),OBJ_NAME_TYPE_CIPHER_METH,(const char *)c);
70 if (r == 0) return(0); 74 if (r == 0) return(0);
71 r=OBJ_NAME_add(OBJ_nid2ln(c->nid),OBJ_NAME_TYPE_CIPHER_METH,(const char *)c); 75 r=OBJ_NAME_add(OBJ_nid2ln(c->nid),OBJ_NAME_TYPE_CIPHER_METH,(const char *)c);
@@ -77,6 +81,9 @@ int EVP_add_digest(const EVP_MD *md)
77 int r; 81 int r;
78 const char *name; 82 const char *name;
79 83
84#ifdef OPENSSL_FIPS
85 OPENSSL_init();
86#endif
80 name=OBJ_nid2sn(md->type); 87 name=OBJ_nid2sn(md->type);
81 r=OBJ_NAME_add(name,OBJ_NAME_TYPE_MD_METH,(const char *)md); 88 r=OBJ_NAME_add(name,OBJ_NAME_TYPE_MD_METH,(const char *)md);
82 if (r == 0) return(0); 89 if (r == 0) return(0);
diff --git a/src/lib/libcrypto/evp/p5_crpt.c b/src/lib/libcrypto/evp/p5_crpt.c
index 48d50014a0..2a265fdee2 100644
--- a/src/lib/libcrypto/evp/p5_crpt.c
+++ b/src/lib/libcrypto/evp/p5_crpt.c
@@ -1,5 +1,5 @@
1/* p5_crpt.c */ 1/* p5_crpt.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/evp/p5_crpt2.c b/src/lib/libcrypto/evp/p5_crpt2.c
index c969d5a206..6bec77baf9 100644
--- a/src/lib/libcrypto/evp/p5_crpt2.c
+++ b/src/lib/libcrypto/evp/p5_crpt2.c
@@ -1,5 +1,5 @@
1/* p5_crpt2.c */ 1/* p5_crpt2.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/evp/p_sign.c b/src/lib/libcrypto/evp/p_sign.c
index e4ae5906f5..bf41a0db68 100644
--- a/src/lib/libcrypto/evp/p_sign.c
+++ b/src/lib/libcrypto/evp/p_sign.c
@@ -84,10 +84,6 @@ int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, unsigned int *siglen,
84 MS_STATIC EVP_MD_CTX tmp_ctx; 84 MS_STATIC EVP_MD_CTX tmp_ctx;
85 85
86 *siglen=0; 86 *siglen=0;
87 EVP_MD_CTX_init(&tmp_ctx);
88 EVP_MD_CTX_copy_ex(&tmp_ctx,ctx);
89 EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len);
90 EVP_MD_CTX_cleanup(&tmp_ctx);
91 for (i=0; i<4; i++) 87 for (i=0; i<4; i++)
92 { 88 {
93 v=ctx->digest->required_pkey_type[i]; 89 v=ctx->digest->required_pkey_type[i];
@@ -108,7 +104,23 @@ int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, unsigned int *siglen,
108 EVPerr(EVP_F_EVP_SIGNFINAL,EVP_R_NO_SIGN_FUNCTION_CONFIGURED); 104 EVPerr(EVP_F_EVP_SIGNFINAL,EVP_R_NO_SIGN_FUNCTION_CONFIGURED);
109 return(0); 105 return(0);
110 } 106 }
111 return(ctx->digest->sign(ctx->digest->type,m,m_len,sigret,siglen, 107 EVP_MD_CTX_init(&tmp_ctx);
112 pkey->pkey.ptr)); 108 EVP_MD_CTX_copy_ex(&tmp_ctx,ctx);
109 if (ctx->digest->flags & EVP_MD_FLAG_SVCTX)
110 {
111 EVP_MD_SVCTX sctmp;
112 sctmp.mctx = &tmp_ctx;
113 sctmp.key = pkey->pkey.ptr;
114 i = ctx->digest->sign(ctx->digest->type,
115 NULL, -1, sigret, siglen, &sctmp);
116 }
117 else
118 {
119 EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len);
120 i = ctx->digest->sign(ctx->digest->type,m,m_len,sigret,siglen,
121 pkey->pkey.ptr);
122 }
123 EVP_MD_CTX_cleanup(&tmp_ctx);
124 return i;
113 } 125 }
114 126
diff --git a/src/lib/libcrypto/evp/p_verify.c b/src/lib/libcrypto/evp/p_verify.c
index 21a40a375e..2d46dffe7e 100644
--- a/src/lib/libcrypto/evp/p_verify.c
+++ b/src/lib/libcrypto/evp/p_verify.c
@@ -85,17 +85,29 @@ int EVP_VerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sigbuf,
85 EVPerr(EVP_F_EVP_VERIFYFINAL,EVP_R_WRONG_PUBLIC_KEY_TYPE); 85 EVPerr(EVP_F_EVP_VERIFYFINAL,EVP_R_WRONG_PUBLIC_KEY_TYPE);
86 return(-1); 86 return(-1);
87 } 87 }
88 EVP_MD_CTX_init(&tmp_ctx); 88 if (ctx->digest->verify == NULL)
89 EVP_MD_CTX_copy_ex(&tmp_ctx,ctx);
90 EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len);
91 EVP_MD_CTX_cleanup(&tmp_ctx);
92 if (ctx->digest->verify == NULL)
93 { 89 {
94 EVPerr(EVP_F_EVP_VERIFYFINAL,EVP_R_NO_VERIFY_FUNCTION_CONFIGURED); 90 EVPerr(EVP_F_EVP_VERIFYFINAL,EVP_R_NO_VERIFY_FUNCTION_CONFIGURED);
95 return(0); 91 return(0);
96 } 92 }
97 93
98 return(ctx->digest->verify(ctx->digest->type,m,m_len, 94 EVP_MD_CTX_init(&tmp_ctx);
99 sigbuf,siglen,pkey->pkey.ptr)); 95 EVP_MD_CTX_copy_ex(&tmp_ctx,ctx);
96 if (ctx->digest->flags & EVP_MD_FLAG_SVCTX)
97 {
98 EVP_MD_SVCTX sctmp;
99 sctmp.mctx = &tmp_ctx;
100 sctmp.key = pkey->pkey.ptr;
101 i = ctx->digest->verify(ctx->digest->type,
102 NULL, -1, sigbuf, siglen, &sctmp);
103 }
104 else
105 {
106 EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len);
107 i = ctx->digest->verify(ctx->digest->type,m,m_len,
108 sigbuf,siglen,pkey->pkey.ptr);
109 }
110 EVP_MD_CTX_cleanup(&tmp_ctx);
111 return i;
100 } 112 }
101 113
diff --git a/src/lib/libcrypto/hmac/hmac.c b/src/lib/libcrypto/hmac/hmac.c
index c45e001492..cbc1c76a57 100644
--- a/src/lib/libcrypto/hmac/hmac.c
+++ b/src/lib/libcrypto/hmac/hmac.c
@@ -61,6 +61,8 @@
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include <openssl/hmac.h> 62#include <openssl/hmac.h>
63 63
64#ifndef OPENSSL_FIPS
65
64void HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, 66void HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
65 const EVP_MD *md, ENGINE *impl) 67 const EVP_MD *md, ENGINE *impl)
66 { 68 {
@@ -171,3 +173,11 @@ unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
171 return(md); 173 return(md);
172 } 174 }
173 175
176void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
177 {
178 EVP_MD_CTX_set_flags(&ctx->i_ctx, flags);
179 EVP_MD_CTX_set_flags(&ctx->o_ctx, flags);
180 EVP_MD_CTX_set_flags(&ctx->md_ctx, flags);
181 }
182
183#endif
diff --git a/src/lib/libcrypto/hmac/hmac.h b/src/lib/libcrypto/hmac/hmac.h
index 719fc408ac..fc38ffb52b 100644
--- a/src/lib/libcrypto/hmac/hmac.h
+++ b/src/lib/libcrypto/hmac/hmac.h
@@ -100,6 +100,7 @@ unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
100 const unsigned char *d, size_t n, unsigned char *md, 100 const unsigned char *d, size_t n, unsigned char *md,
101 unsigned int *md_len); 101 unsigned int *md_len);
102 102
103void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags);
103 104
104#ifdef __cplusplus 105#ifdef __cplusplus
105} 106}
diff --git a/src/lib/libcrypto/idea/idea.h b/src/lib/libcrypto/idea/idea.h
index bf97a37e39..a137d4cbce 100644
--- a/src/lib/libcrypto/idea/idea.h
+++ b/src/lib/libcrypto/idea/idea.h
@@ -83,6 +83,9 @@ typedef struct idea_key_st
83const char *idea_options(void); 83const char *idea_options(void);
84void idea_ecb_encrypt(const unsigned char *in, unsigned char *out, 84void idea_ecb_encrypt(const unsigned char *in, unsigned char *out,
85 IDEA_KEY_SCHEDULE *ks); 85 IDEA_KEY_SCHEDULE *ks);
86#ifdef OPENSSL_FIPS
87void private_idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks);
88#endif
86void idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks); 89void idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks);
87void idea_set_decrypt_key(const IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk); 90void idea_set_decrypt_key(const IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk);
88void idea_cbc_encrypt(const unsigned char *in, unsigned char *out, 91void idea_cbc_encrypt(const unsigned char *in, unsigned char *out,
diff --git a/src/lib/libcrypto/md32_common.h b/src/lib/libcrypto/md32_common.h
index 089c450290..61bcd9786f 100644
--- a/src/lib/libcrypto/md32_common.h
+++ b/src/lib/libcrypto/md32_common.h
@@ -301,7 +301,7 @@ int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
301 { 301 {
302 p=(unsigned char *)c->data; 302 p=(unsigned char *)c->data;
303 303
304 if ((n+len) >= HASH_CBLOCK) 304 if (len >= HASH_CBLOCK || len+n >= HASH_CBLOCK)
305 { 305 {
306 memcpy (p+n,data,HASH_CBLOCK-n); 306 memcpy (p+n,data,HASH_CBLOCK-n);
307 HASH_BLOCK_DATA_ORDER (c,p,1); 307 HASH_BLOCK_DATA_ORDER (c,p,1);
diff --git a/src/lib/libcrypto/md4/md4.h b/src/lib/libcrypto/md4/md4.h
index 5598c93a4f..ba1fe4a6ee 100644
--- a/src/lib/libcrypto/md4/md4.h
+++ b/src/lib/libcrypto/md4/md4.h
@@ -105,6 +105,9 @@ typedef struct MD4state_st
105 unsigned int num; 105 unsigned int num;
106 } MD4_CTX; 106 } MD4_CTX;
107 107
108#ifdef OPENSSL_FIPS
109int private_MD4_Init(MD4_CTX *c);
110#endif
108int MD4_Init(MD4_CTX *c); 111int MD4_Init(MD4_CTX *c);
109int MD4_Update(MD4_CTX *c, const void *data, size_t len); 112int MD4_Update(MD4_CTX *c, const void *data, size_t len);
110int MD4_Final(unsigned char *md, MD4_CTX *c); 113int MD4_Final(unsigned char *md, MD4_CTX *c);
diff --git a/src/lib/libcrypto/md4/md4_dgst.c b/src/lib/libcrypto/md4/md4_dgst.c
index cfef94af39..0f5448601d 100644
--- a/src/lib/libcrypto/md4/md4_dgst.c
+++ b/src/lib/libcrypto/md4/md4_dgst.c
@@ -59,6 +59,11 @@
59#include <stdio.h> 59#include <stdio.h>
60#include "md4_locl.h" 60#include "md4_locl.h"
61#include <openssl/opensslv.h> 61#include <openssl/opensslv.h>
62#include <openssl/err.h>
63#ifdef OPENSSL_FIPS
64#include <openssl/fips.h>
65#endif
66
62 67
63const char MD4_version[]="MD4" OPENSSL_VERSION_PTEXT; 68const char MD4_version[]="MD4" OPENSSL_VERSION_PTEXT;
64 69
@@ -70,7 +75,7 @@ const char MD4_version[]="MD4" OPENSSL_VERSION_PTEXT;
70#define INIT_DATA_C (unsigned long)0x98badcfeL 75#define INIT_DATA_C (unsigned long)0x98badcfeL
71#define INIT_DATA_D (unsigned long)0x10325476L 76#define INIT_DATA_D (unsigned long)0x10325476L
72 77
73int MD4_Init(MD4_CTX *c) 78FIPS_NON_FIPS_MD_Init(MD4)
74 { 79 {
75 c->A=INIT_DATA_A; 80 c->A=INIT_DATA_A;
76 c->B=INIT_DATA_B; 81 c->B=INIT_DATA_B;
diff --git a/src/lib/libcrypto/md5/md5.h b/src/lib/libcrypto/md5/md5.h
index dbdc0e1abc..0761f84a27 100644
--- a/src/lib/libcrypto/md5/md5.h
+++ b/src/lib/libcrypto/md5/md5.h
@@ -105,6 +105,9 @@ typedef struct MD5state_st
105 unsigned int num; 105 unsigned int num;
106 } MD5_CTX; 106 } MD5_CTX;
107 107
108#ifdef OPENSSL_FIPS
109int private_MD5_Init(MD5_CTX *c);
110#endif
108int MD5_Init(MD5_CTX *c); 111int MD5_Init(MD5_CTX *c);
109int MD5_Update(MD5_CTX *c, const void *data, size_t len); 112int MD5_Update(MD5_CTX *c, const void *data, size_t len);
110int MD5_Final(unsigned char *md, MD5_CTX *c); 113int MD5_Final(unsigned char *md, MD5_CTX *c);
diff --git a/src/lib/libcrypto/md5/md5_dgst.c b/src/lib/libcrypto/md5/md5_dgst.c
index b96e332ba4..47bb9020ee 100644
--- a/src/lib/libcrypto/md5/md5_dgst.c
+++ b/src/lib/libcrypto/md5/md5_dgst.c
@@ -59,6 +59,11 @@
59#include <stdio.h> 59#include <stdio.h>
60#include "md5_locl.h" 60#include "md5_locl.h"
61#include <openssl/opensslv.h> 61#include <openssl/opensslv.h>
62#include <openssl/err.h>
63#ifdef OPENSSL_FIPS
64#include <openssl/fips.h>
65#endif
66
62 67
63const char MD5_version[]="MD5" OPENSSL_VERSION_PTEXT; 68const char MD5_version[]="MD5" OPENSSL_VERSION_PTEXT;
64 69
@@ -70,7 +75,7 @@ const char MD5_version[]="MD5" OPENSSL_VERSION_PTEXT;
70#define INIT_DATA_C (unsigned long)0x98badcfeL 75#define INIT_DATA_C (unsigned long)0x98badcfeL
71#define INIT_DATA_D (unsigned long)0x10325476L 76#define INIT_DATA_D (unsigned long)0x10325476L
72 77
73int MD5_Init(MD5_CTX *c) 78FIPS_NON_FIPS_MD_Init(MD5)
74 { 79 {
75 c->A=INIT_DATA_A; 80 c->A=INIT_DATA_A;
76 c->B=INIT_DATA_B; 81 c->B=INIT_DATA_B;
diff --git a/src/lib/libcrypto/mem_dbg.c b/src/lib/libcrypto/mem_dbg.c
index 8316485217..dfeb084799 100644
--- a/src/lib/libcrypto/mem_dbg.c
+++ b/src/lib/libcrypto/mem_dbg.c
@@ -330,7 +330,7 @@ static APP_INFO *pop_info(void)
330 return(ret); 330 return(ret);
331 } 331 }
332 332
333int CRYPTO_push_info_(const char *info, const char *file, int line) 333int CRYPTO_dbg_push_info(const char *info, const char *file, int line)
334 { 334 {
335 APP_INFO *ami, *amim; 335 APP_INFO *ami, *amim;
336 int ret=0; 336 int ret=0;
@@ -380,7 +380,7 @@ int CRYPTO_push_info_(const char *info, const char *file, int line)
380 return(ret); 380 return(ret);
381 } 381 }
382 382
383int CRYPTO_pop_info(void) 383int CRYPTO_dbg_pop_info(void)
384 { 384 {
385 int ret=0; 385 int ret=0;
386 386
@@ -395,7 +395,7 @@ int CRYPTO_pop_info(void)
395 return(ret); 395 return(ret);
396 } 396 }
397 397
398int CRYPTO_remove_all_info(void) 398int CRYPTO_dbg_remove_all_info(void)
399 { 399 {
400 int ret=0; 400 int ret=0;
401 401
@@ -793,3 +793,25 @@ void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb)
793 lh_doall_arg(mh, LHASH_DOALL_ARG_FN(cb_leak), &cb); 793 lh_doall_arg(mh, LHASH_DOALL_ARG_FN(cb_leak), &cb);
794 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2); 794 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
795 } 795 }
796
797void CRYPTO_malloc_debug_init(void)
798 {
799 CRYPTO_set_mem_debug_functions(
800 CRYPTO_dbg_malloc,
801 CRYPTO_dbg_realloc,
802 CRYPTO_dbg_free,
803 CRYPTO_dbg_set_options,
804 CRYPTO_dbg_get_options);
805 CRYPTO_set_mem_info_functions(
806 CRYPTO_dbg_push_info,
807 CRYPTO_dbg_pop_info,
808 CRYPTO_dbg_remove_all_info);
809 }
810
811char *CRYPTO_strdup(const char *str, const char *file, int line)
812 {
813 char *ret = CRYPTO_malloc(strlen(str)+1, file, line);
814
815 strcpy(ret, str);
816 return ret;
817 }
diff --git a/src/lib/libcrypto/o_init.c b/src/lib/libcrypto/o_init.c
new file mode 100644
index 0000000000..00ed65a6cf
--- /dev/null
+++ b/src/lib/libcrypto/o_init.c
@@ -0,0 +1,86 @@
1/* o_init.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <e_os.h>
60#include <openssl/err.h>
61
62/* Perform any essential OpenSSL initialization operations.
63 * Currently only sets FIPS callbacks
64 */
65
66void OPENSSL_init(void)
67 {
68#ifdef OPENSSL_FIPS
69 static int done = 0;
70 if (!done)
71 {
72 int_ERR_lib_init();
73#ifdef CRYPTO_MDEBUG
74 CRYPTO_malloc_debug_init();
75#endif
76#ifdef OPENSSL_ENGINE
77 int_EVP_MD_init_engine_callbacks();
78 int_EVP_CIPHER_init_engine_callbacks();
79 int_RAND_init_engine_callbacks();
80#endif
81 done = 1;
82 }
83#endif
84 }
85
86
diff --git a/src/lib/libcrypto/objects/obj_dat.pl b/src/lib/libcrypto/objects/obj_dat.pl
index 8a09a46ee6..7de2f77afd 100644
--- a/src/lib/libcrypto/objects/obj_dat.pl
+++ b/src/lib/libcrypto/objects/obj_dat.pl
@@ -2,7 +2,9 @@
2 2
3# fixes bug in floating point emulation on sparc64 when 3# fixes bug in floating point emulation on sparc64 when
4# this script produces off-by-one output on sparc64 4# this script produces off-by-one output on sparc64
5use integer; 5eval 'use integer;';
6
7print STDERR "Warning: perl module integer not found.\n" if ($@);
6 8
7sub obj_cmp 9sub obj_cmp
8 { 10 {
diff --git a/src/lib/libcrypto/objects/obj_mac.num b/src/lib/libcrypto/objects/obj_mac.num
index 47815b1e4e..e3f56bc52c 100644
--- a/src/lib/libcrypto/objects/obj_mac.num
+++ b/src/lib/libcrypto/objects/obj_mac.num
@@ -788,3 +788,71 @@ id_ct_asciiTextWithCRLF 787
788id_aes128_wrap 788 788id_aes128_wrap 788
789id_aes192_wrap 789 789id_aes192_wrap 789
790id_aes256_wrap 790 790id_aes256_wrap 790
791ecdsa_with_Recommended 791
792ecdsa_with_Specified 792
793ecdsa_with_SHA224 793
794ecdsa_with_SHA256 794
795ecdsa_with_SHA384 795
796ecdsa_with_SHA512 796
797hmacWithMD5 797
798hmacWithSHA224 798
799hmacWithSHA256 799
800hmacWithSHA384 800
801hmacWithSHA512 801
802dsa_with_SHA224 802
803dsa_with_SHA256 803
804whirlpool 804
805cryptopro 805
806cryptocom 806
807id_GostR3411_94_with_GostR3410_2001 807
808id_GostR3411_94_with_GostR3410_94 808
809id_GostR3411_94 809
810id_HMACGostR3411_94 810
811id_GostR3410_2001 811
812id_GostR3410_94 812
813id_Gost28147_89 813
814gost89_cnt 814
815id_Gost28147_89_MAC 815
816id_GostR3411_94_prf 816
817id_GostR3410_2001DH 817
818id_GostR3410_94DH 818
819id_Gost28147_89_CryptoPro_KeyMeshing 819
820id_Gost28147_89_None_KeyMeshing 820
821id_GostR3411_94_TestParamSet 821
822id_GostR3411_94_CryptoProParamSet 822
823id_Gost28147_89_TestParamSet 823
824id_Gost28147_89_CryptoPro_A_ParamSet 824
825id_Gost28147_89_CryptoPro_B_ParamSet 825
826id_Gost28147_89_CryptoPro_C_ParamSet 826
827id_Gost28147_89_CryptoPro_D_ParamSet 827
828id_Gost28147_89_CryptoPro_Oscar_1_1_ParamSet 828
829id_Gost28147_89_CryptoPro_Oscar_1_0_ParamSet 829
830id_Gost28147_89_CryptoPro_RIC_1_ParamSet 830
831id_GostR3410_94_TestParamSet 831
832id_GostR3410_94_CryptoPro_A_ParamSet 832
833id_GostR3410_94_CryptoPro_B_ParamSet 833
834id_GostR3410_94_CryptoPro_C_ParamSet 834
835id_GostR3410_94_CryptoPro_D_ParamSet 835
836id_GostR3410_94_CryptoPro_XchA_ParamSet 836
837id_GostR3410_94_CryptoPro_XchB_ParamSet 837
838id_GostR3410_94_CryptoPro_XchC_ParamSet 838
839id_GostR3410_2001_TestParamSet 839
840id_GostR3410_2001_CryptoPro_A_ParamSet 840
841id_GostR3410_2001_CryptoPro_B_ParamSet 841
842id_GostR3410_2001_CryptoPro_C_ParamSet 842
843id_GostR3410_2001_CryptoPro_XchA_ParamSet 843
844id_GostR3410_2001_CryptoPro_XchB_ParamSet 844
845id_GostR3410_94_a 845
846id_GostR3410_94_aBis 846
847id_GostR3410_94_b 847
848id_GostR3410_94_bBis 848
849id_Gost28147_89_cc 849
850id_GostR3410_94_cc 850
851id_GostR3410_2001_cc 851
852id_GostR3411_94_with_GostR3410_94_cc 852
853id_GostR3411_94_with_GostR3410_2001_cc 853
854id_GostR3410_2001_ParamSet_cc 854
855hmac 855
856LocalKeySet 856
857freshest_crl 857
858id_on_permanentIdentifier 858
diff --git a/src/lib/libcrypto/objects/objects.txt b/src/lib/libcrypto/objects/objects.txt
index 34c8d1d647..a6a811b8e7 100644
--- a/src/lib/libcrypto/objects/objects.txt
+++ b/src/lib/libcrypto/objects/objects.txt
@@ -79,6 +79,12 @@ X9-62_primeCurve 7 : prime256v1
79!Alias id-ecSigType ansi-X9-62 4 79!Alias id-ecSigType ansi-X9-62 4
80!global 80!global
81X9-62_id-ecSigType 1 : ecdsa-with-SHA1 81X9-62_id-ecSigType 1 : ecdsa-with-SHA1
82X9-62_id-ecSigType 2 : ecdsa-with-Recommended
83X9-62_id-ecSigType 3 : ecdsa-with-Specified
84ecdsa-with-Specified 1 : ecdsa-with-SHA224
85ecdsa-with-Specified 2 : ecdsa-with-SHA256
86ecdsa-with-Specified 3 : ecdsa-with-SHA384
87ecdsa-with-Specified 4 : ecdsa-with-SHA512
82 88
83# SECG curve OIDs from "SEC 2: Recommended Elliptic Curve Domain Parameters" 89# SECG curve OIDs from "SEC 2: Recommended Elliptic Curve Domain Parameters"
84# (http://www.secg.org/) 90# (http://www.secg.org/)
@@ -313,6 +319,7 @@ pkcs9 20 : : friendlyName
313pkcs9 21 : : localKeyID 319pkcs9 21 : : localKeyID
314!Cname ms-csp-name 320!Cname ms-csp-name
3151 3 6 1 4 1 311 17 1 : CSPName : Microsoft CSP Name 3211 3 6 1 4 1 311 17 1 : CSPName : Microsoft CSP Name
3221 3 6 1 4 1 311 17 2 : LocalKeySet : Microsoft Local Key set
316!Alias certTypes pkcs9 22 323!Alias certTypes pkcs9 22
317certTypes 1 : : x509Certificate 324certTypes 1 : : x509Certificate
318certTypes 2 : : sdsiCertificate 325certTypes 2 : : sdsiCertificate
@@ -348,7 +355,15 @@ rsadsi 2 2 : MD2 : md2
348rsadsi 2 4 : MD4 : md4 355rsadsi 2 4 : MD4 : md4
349rsadsi 2 5 : MD5 : md5 356rsadsi 2 5 : MD5 : md5
350 : MD5-SHA1 : md5-sha1 357 : MD5-SHA1 : md5-sha1
358rsadsi 2 6 : : hmacWithMD5
351rsadsi 2 7 : : hmacWithSHA1 359rsadsi 2 7 : : hmacWithSHA1
360
361# From RFC4231
362rsadsi 2 8 : : hmacWithSHA224
363rsadsi 2 9 : : hmacWithSHA256
364rsadsi 2 10 : : hmacWithSHA384
365rsadsi 2 11 : : hmacWithSHA512
366
352rsadsi 3 2 : RC2-CBC : rc2-cbc 367rsadsi 3 2 : RC2-CBC : rc2-cbc
353 : RC2-ECB : rc2-ecb 368 : RC2-ECB : rc2-ecb
354!Cname rc2-cfb64 369!Cname rc2-cfb64
@@ -542,6 +557,7 @@ id-cmc 24 : id-cmc-confirmCertAcceptance
542 557
543# other names 558# other names
544id-on 1 : id-on-personalData 559id-on 1 : id-on-personalData
560id-on 3 : id-on-permanentIdentifier : Permanent Identifier
545 561
546# personal data attributes 562# personal data attributes
547id-pda 1 : id-pda-dateOfBirth 563id-pda 1 : id-pda-dateOfBirth
@@ -711,6 +727,8 @@ id-ce 35 : authorityKeyIdentifier : X509v3 Authority Key Identifier
711id-ce 36 : policyConstraints : X509v3 Policy Constraints 727id-ce 36 : policyConstraints : X509v3 Policy Constraints
712!Cname ext-key-usage 728!Cname ext-key-usage
713id-ce 37 : extendedKeyUsage : X509v3 Extended Key Usage 729id-ce 37 : extendedKeyUsage : X509v3 Extended Key Usage
730!Cname freshest-crl
731id-ce 46 : freshestCRL : X509v3 Freshest CRL
714!Cname inhibit-any-policy 732!Cname inhibit-any-policy
715id-ce 54 : inhibitAnyPolicy : X509v3 Inhibit Any Policy 733id-ce 54 : inhibitAnyPolicy : X509v3 Inhibit Any Policy
716!Cname target-information 734!Cname target-information
@@ -833,6 +851,11 @@ nist_hashalgs 2 : SHA384 : sha384
833nist_hashalgs 3 : SHA512 : sha512 851nist_hashalgs 3 : SHA512 : sha512
834nist_hashalgs 4 : SHA224 : sha224 852nist_hashalgs 4 : SHA224 : sha224
835 853
854# OIDs for dsa-with-sha224 and dsa-with-sha256
855!Alias dsa_with_sha2 nistAlgorithms 3
856dsa_with_sha2 1 : dsa_with_SHA224
857dsa_with_sha2 2 : dsa_with_SHA256
858
836# Hold instruction CRL entry extension 859# Hold instruction CRL entry extension
837!Cname hold-instruction-code 860!Cname hold-instruction-code
838id-ce 23 : holdInstructionCode : Hold Instruction Code 861id-ce 23 : holdInstructionCode : Hold Instruction Code
@@ -1070,13 +1093,93 @@ rsadsi 1 1 6 : rsaOAEPEncryptionSET
1070 : Oakley-EC2N-3 : ipsec3 1093 : Oakley-EC2N-3 : ipsec3
1071 : Oakley-EC2N-4 : ipsec4 1094 : Oakley-EC2N-4 : ipsec4
1072 1095
1096iso 0 10118 3 0 55 : whirlpool
1097
1098# GOST OIDs
1099
1100member-body 643 2 2 : cryptopro
1101member-body 643 2 9 : cryptocom
1102
1103cryptopro 3 : id-GostR3411-94-with-GostR3410-2001 : GOST R 34.11-94 with GOST R 34.10-2001
1104cryptopro 4 : id-GostR3411-94-with-GostR3410-94 : GOST R 34.11-94 with GOST R 34.10-94
1105!Cname id-GostR3411-94
1106cryptopro 9 : md_gost94 : GOST R 34.11-94
1107cryptopro 10 : id-HMACGostR3411-94 : HMAC GOST 34.11-94
1108!Cname id-GostR3410-2001
1109cryptopro 19 : gost2001 : GOST R 34.10-2001
1110!Cname id-GostR3410-94
1111cryptopro 20 : gost94 : GOST R 34.10-94
1112!Cname id-Gost28147-89
1113cryptopro 21 : gost89 : GOST 28147-89
1114 : gost89-cnt
1115!Cname id-Gost28147-89-MAC
1116cryptopro 22 : gost-mac : GOST 28147-89 MAC
1117!Cname id-GostR3411-94-prf
1118cryptopro 23 : prf-gostr3411-94 : GOST R 34.11-94 PRF
1119cryptopro 98 : id-GostR3410-2001DH : GOST R 34.10-2001 DH
1120cryptopro 99 : id-GostR3410-94DH : GOST R 34.10-94 DH
1121
1122cryptopro 14 1 : id-Gost28147-89-CryptoPro-KeyMeshing
1123cryptopro 14 0 : id-Gost28147-89-None-KeyMeshing
1124
1125# GOST parameter set OIDs
1126
1127cryptopro 30 0 : id-GostR3411-94-TestParamSet
1128cryptopro 30 1 : id-GostR3411-94-CryptoProParamSet
1129
1130cryptopro 31 0 : id-Gost28147-89-TestParamSet
1131cryptopro 31 1 : id-Gost28147-89-CryptoPro-A-ParamSet
1132cryptopro 31 2 : id-Gost28147-89-CryptoPro-B-ParamSet
1133cryptopro 31 3 : id-Gost28147-89-CryptoPro-C-ParamSet
1134cryptopro 31 4 : id-Gost28147-89-CryptoPro-D-ParamSet
1135cryptopro 31 5 : id-Gost28147-89-CryptoPro-Oscar-1-1-ParamSet
1136cryptopro 31 6 : id-Gost28147-89-CryptoPro-Oscar-1-0-ParamSet
1137cryptopro 31 7 : id-Gost28147-89-CryptoPro-RIC-1-ParamSet
1138
1139cryptopro 32 0 : id-GostR3410-94-TestParamSet
1140cryptopro 32 2 : id-GostR3410-94-CryptoPro-A-ParamSet
1141cryptopro 32 3 : id-GostR3410-94-CryptoPro-B-ParamSet
1142cryptopro 32 4 : id-GostR3410-94-CryptoPro-C-ParamSet
1143cryptopro 32 5 : id-GostR3410-94-CryptoPro-D-ParamSet
1144
1145cryptopro 33 1 : id-GostR3410-94-CryptoPro-XchA-ParamSet
1146cryptopro 33 2 : id-GostR3410-94-CryptoPro-XchB-ParamSet
1147cryptopro 33 3 : id-GostR3410-94-CryptoPro-XchC-ParamSet
1148
1149cryptopro 35 0 : id-GostR3410-2001-TestParamSet
1150cryptopro 35 1 : id-GostR3410-2001-CryptoPro-A-ParamSet
1151cryptopro 35 2 : id-GostR3410-2001-CryptoPro-B-ParamSet
1152cryptopro 35 3 : id-GostR3410-2001-CryptoPro-C-ParamSet
1153
1154cryptopro 36 0 : id-GostR3410-2001-CryptoPro-XchA-ParamSet
1155cryptopro 36 1 : id-GostR3410-2001-CryptoPro-XchB-ParamSet
1156
1157id-GostR3410-94 1 : id-GostR3410-94-a
1158id-GostR3410-94 2 : id-GostR3410-94-aBis
1159id-GostR3410-94 3 : id-GostR3410-94-b
1160id-GostR3410-94 4 : id-GostR3410-94-bBis
1161
1162# Cryptocom LTD GOST OIDs
1163
1164cryptocom 1 6 1 : id-Gost28147-89-cc : GOST 28147-89 Cryptocom ParamSet
1165!Cname id-GostR3410-94-cc
1166cryptocom 1 5 3 : gost94cc : GOST 34.10-94 Cryptocom
1167!Cname id-GostR3410-2001-cc
1168cryptocom 1 5 4 : gost2001cc : GOST 34.10-2001 Cryptocom
1169
1170cryptocom 1 3 3 : id-GostR3411-94-with-GostR3410-94-cc : GOST R 34.11-94 with GOST R 34.10-94 Cryptocom
1171cryptocom 1 3 4 : id-GostR3411-94-with-GostR3410-2001-cc : GOST R 34.11-94 with GOST R 34.10-2001 Cryptocom
1172
1173cryptocom 1 8 1 : id-GostR3410-2001-ParamSet-cc : GOST R 3410-2001 Parameter Set Cryptocom
1073 1174
1074# Definitions for Camellia cipher - CBC MODE 1175# Definitions for Camellia cipher - CBC MODE
1176
10751 2 392 200011 61 1 1 1 2 : CAMELLIA-128-CBC : camellia-128-cbc 11771 2 392 200011 61 1 1 1 2 : CAMELLIA-128-CBC : camellia-128-cbc
10761 2 392 200011 61 1 1 1 3 : CAMELLIA-192-CBC : camellia-192-cbc 11781 2 392 200011 61 1 1 1 3 : CAMELLIA-192-CBC : camellia-192-cbc
10771 2 392 200011 61 1 1 1 4 : CAMELLIA-256-CBC : camellia-256-cbc 11791 2 392 200011 61 1 1 1 4 : CAMELLIA-256-CBC : camellia-256-cbc
1078 1180
1079# Definitions for Camellia cipher - ECB, CFB, OFB MODE 1181# Definitions for Camellia cipher - ECB, CFB, OFB MODE
1182
1080!Alias ntt-ds 0 3 4401 5 1183!Alias ntt-ds 0 3 4401 5
1081!Alias camellia ntt-ds 3 1 9 1184!Alias camellia ntt-ds 3 1 9
1082 1185
@@ -1107,7 +1210,6 @@ camellia 44 : CAMELLIA-256-CFB : camellia-256-cfb
1107 : CAMELLIA-192-CFB8 : camellia-192-cfb8 1210 : CAMELLIA-192-CFB8 : camellia-192-cfb8
1108 : CAMELLIA-256-CFB8 : camellia-256-cfb8 1211 : CAMELLIA-256-CFB8 : camellia-256-cfb8
1109 1212
1110
1111# Definitions for SEED cipher - ECB, CBC, OFB mode 1213# Definitions for SEED cipher - ECB, CBC, OFB mode
1112 1214
1113member-body 410 200004 : KISA : kisa 1215member-body 410 200004 : KISA : kisa
@@ -1117,3 +1219,7 @@ kisa 1 4 : SEED-CBC : seed-cbc
1117kisa 1 5 : SEED-CFB : seed-cfb 1219kisa 1 5 : SEED-CFB : seed-cfb
1118!Cname seed-ofb128 1220!Cname seed-ofb128
1119kisa 1 6 : SEED-OFB : seed-ofb 1221kisa 1 6 : SEED-OFB : seed-ofb
1222
1223# There is no OID that just denotes "HMAC" oddly enough...
1224
1225 : HMAC : hmac
diff --git a/src/lib/libcrypto/ocsp/ocsp_asn.c b/src/lib/libcrypto/ocsp/ocsp_asn.c
index 39b7a1c568..bfe892ac70 100644
--- a/src/lib/libcrypto/ocsp/ocsp_asn.c
+++ b/src/lib/libcrypto/ocsp/ocsp_asn.c
@@ -1,5 +1,5 @@
1/* ocsp_asn.c */ 1/* ocsp_asn.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/ocsp/ocsp_ht.c b/src/lib/libcrypto/ocsp/ocsp_ht.c
index a8e569b74a..6abb30b2c0 100644
--- a/src/lib/libcrypto/ocsp/ocsp_ht.c
+++ b/src/lib/libcrypto/ocsp/ocsp_ht.c
@@ -1,5 +1,5 @@
1/* ocsp_ht.c */ 1/* ocsp_ht.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2006. 3 * project 2006.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -56,11 +56,12 @@
56 * 56 *
57 */ 57 */
58 58
59#include <openssl/asn1.h>
60#include <stdio.h> 59#include <stdio.h>
61#include <stdlib.h> 60#include <stdlib.h>
62#include <ctype.h> 61#include <ctype.h>
63#include <string.h> 62#include <string.h>
63#include "e_os.h"
64#include <openssl/asn1.h>
64#include <openssl/ocsp.h> 65#include <openssl/ocsp.h>
65#include <openssl/err.h> 66#include <openssl/err.h>
66#include <openssl/buffer.h> 67#include <openssl/buffer.h>
diff --git a/src/lib/libcrypto/ocsp/ocsp_srv.c b/src/lib/libcrypto/ocsp/ocsp_srv.c
index fffa134e75..1c606dd0b6 100644
--- a/src/lib/libcrypto/ocsp/ocsp_srv.c
+++ b/src/lib/libcrypto/ocsp/ocsp_srv.c
@@ -1,5 +1,5 @@
1/* ocsp_srv.c */ 1/* ocsp_srv.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/ocsp/ocsp_vfy.c b/src/lib/libcrypto/ocsp/ocsp_vfy.c
index 23ea41c847..4a0c3870d8 100644
--- a/src/lib/libcrypto/ocsp/ocsp_vfy.c
+++ b/src/lib/libcrypto/ocsp/ocsp_vfy.c
@@ -1,5 +1,5 @@
1/* ocsp_vfy.c */ 1/* ocsp_vfy.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/opensslv.h b/src/lib/libcrypto/opensslv.h
index b308894f18..09687b5136 100644
--- a/src/lib/libcrypto/opensslv.h
+++ b/src/lib/libcrypto/opensslv.h
@@ -25,11 +25,11 @@
25 * (Prior to 0.9.5a beta1, a different scheme was used: MMNNFFRBB for 25 * (Prior to 0.9.5a beta1, a different scheme was used: MMNNFFRBB for
26 * major minor fix final patch/beta) 26 * major minor fix final patch/beta)
27 */ 27 */
28#define OPENSSL_VERSION_NUMBER 0x0090808fL 28#define OPENSSL_VERSION_NUMBER 0x009080afL
29#ifdef OPENSSL_FIPS 29#ifdef OPENSSL_FIPS
30#define OPENSSL_VERSION_TEXT "OpenSSL 0.9.8h-fips 28 May 2008" 30#define OPENSSL_VERSION_TEXT "OpenSSL 0.9.8j-fips 07 Jan 2009"
31#else 31#else
32#define OPENSSL_VERSION_TEXT "OpenSSL 0.9.8h 28 May 2008" 32#define OPENSSL_VERSION_TEXT "OpenSSL 0.9.8j 07 Jan 2009"
33#endif 33#endif
34#define OPENSSL_VERSION_PTEXT " part of " OPENSSL_VERSION_TEXT 34#define OPENSSL_VERSION_PTEXT " part of " OPENSSL_VERSION_TEXT
35 35
diff --git a/src/lib/libcrypto/ossl_typ.h b/src/lib/libcrypto/ossl_typ.h
index 345fb1dc4d..0e7a380880 100644
--- a/src/lib/libcrypto/ossl_typ.h
+++ b/src/lib/libcrypto/ossl_typ.h
@@ -100,6 +100,8 @@ typedef int ASN1_NULL;
100#undef X509_EXTENSIONS 100#undef X509_EXTENSIONS
101#undef X509_CERT_PAIR 101#undef X509_CERT_PAIR
102#undef PKCS7_ISSUER_AND_SERIAL 102#undef PKCS7_ISSUER_AND_SERIAL
103#undef OCSP_REQUEST
104#undef OCSP_RESPONSE
103#endif 105#endif
104 106
105#ifdef BIGNUM 107#ifdef BIGNUM
@@ -140,6 +142,8 @@ typedef struct X509_crl_st X509_CRL;
140typedef struct X509_name_st X509_NAME; 142typedef struct X509_name_st X509_NAME;
141typedef struct x509_store_st X509_STORE; 143typedef struct x509_store_st X509_STORE;
142typedef struct x509_store_ctx_st X509_STORE_CTX; 144typedef struct x509_store_ctx_st X509_STORE_CTX;
145typedef struct ssl_st SSL;
146typedef struct ssl_ctx_st SSL_CTX;
143 147
144typedef struct v3_ext_ctx X509V3_CTX; 148typedef struct v3_ext_ctx X509V3_CTX;
145typedef struct conf_st CONF; 149typedef struct conf_st CONF;
diff --git a/src/lib/libcrypto/pem/pem.h b/src/lib/libcrypto/pem/pem.h
index 670afa670b..6f8e01544b 100644
--- a/src/lib/libcrypto/pem/pem.h
+++ b/src/lib/libcrypto/pem/pem.h
@@ -125,6 +125,7 @@ extern "C" {
125#define PEM_STRING_DSA "DSA PRIVATE KEY" 125#define PEM_STRING_DSA "DSA PRIVATE KEY"
126#define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY" 126#define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
127#define PEM_STRING_PKCS7 "PKCS7" 127#define PEM_STRING_PKCS7 "PKCS7"
128#define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
128#define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY" 129#define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
129#define PEM_STRING_PKCS8INF "PRIVATE KEY" 130#define PEM_STRING_PKCS8INF "PRIVATE KEY"
130#define PEM_STRING_DHPARAMS "DH PARAMETERS" 131#define PEM_STRING_DHPARAMS "DH PARAMETERS"
diff --git a/src/lib/libcrypto/pem/pem_all.c b/src/lib/libcrypto/pem/pem_all.c
index 66cbc7eb82..69dd19bf2e 100644
--- a/src/lib/libcrypto/pem/pem_all.c
+++ b/src/lib/libcrypto/pem/pem_all.c
@@ -194,7 +194,49 @@ RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb,
194 194
195#endif 195#endif
196 196
197#ifdef OPENSSL_FIPS
198
199int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
200 unsigned char *kstr, int klen,
201 pem_password_cb *cb, void *u)
202{
203 EVP_PKEY *k;
204 int ret;
205 k = EVP_PKEY_new();
206 if (!k)
207 return 0;
208 EVP_PKEY_set1_RSA(k, x);
209
210 ret = PEM_write_bio_PrivateKey(bp, k, enc, kstr, klen, cb, u);
211 EVP_PKEY_free(k);
212 return ret;
213}
214
215#ifndef OPENSSL_NO_FP_API
216int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
217 unsigned char *kstr, int klen,
218 pem_password_cb *cb, void *u)
219{
220 EVP_PKEY *k;
221 int ret;
222 k = EVP_PKEY_new();
223 if (!k)
224 return 0;
225
226 EVP_PKEY_set1_RSA(k, x);
227
228 ret = PEM_write_PrivateKey(fp, k, enc, kstr, klen, cb, u);
229 EVP_PKEY_free(k);
230 return ret;
231}
232#endif
233
234#else
235
197IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey) 236IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
237
238#endif
239
198IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey) 240IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
199IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY) 241IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
200 242
@@ -224,7 +266,47 @@ DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
224 return pkey_get_dsa(pktmp, dsa); 266 return pkey_get_dsa(pktmp, dsa);
225} 267}
226 268
269#ifdef OPENSSL_FIPS
270
271int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
272 unsigned char *kstr, int klen,
273 pem_password_cb *cb, void *u)
274{
275 EVP_PKEY *k;
276 int ret;
277 k = EVP_PKEY_new();
278 if (!k)
279 return 0;
280 EVP_PKEY_set1_DSA(k, x);
281
282 ret = PEM_write_bio_PrivateKey(bp, k, enc, kstr, klen, cb, u);
283 EVP_PKEY_free(k);
284 return ret;
285}
286
287#ifndef OPENSSL_NO_FP_API
288int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
289 unsigned char *kstr, int klen,
290 pem_password_cb *cb, void *u)
291{
292 EVP_PKEY *k;
293 int ret;
294 k = EVP_PKEY_new();
295 if (!k)
296 return 0;
297 EVP_PKEY_set1_DSA(k, x);
298 ret = PEM_write_PrivateKey(fp, k, enc, kstr, klen, cb, u);
299 EVP_PKEY_free(k);
300 return ret;
301}
302#endif
303
304#else
305
227IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey) 306IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
307
308#endif
309
228IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY) 310IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
229 311
230#ifndef OPENSSL_NO_FP_API 312#ifndef OPENSSL_NO_FP_API
@@ -270,8 +352,49 @@ EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
270 352
271IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS, ECPKParameters) 353IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS, ECPKParameters)
272 354
355
356
357#ifdef OPENSSL_FIPS
358
359int PEM_write_bio_ECPrivateKey(BIO *bp, EC_KEY *x, const EVP_CIPHER *enc,
360 unsigned char *kstr, int klen,
361 pem_password_cb *cb, void *u)
362{
363 EVP_PKEY *k;
364 int ret;
365 k = EVP_PKEY_new();
366 if (!k)
367 return 0;
368 EVP_PKEY_set1_EC_KEY(k, x);
369
370 ret = PEM_write_bio_PrivateKey(bp, k, enc, kstr, klen, cb, u);
371 EVP_PKEY_free(k);
372 return ret;
373}
374
375#ifndef OPENSSL_NO_FP_API
376int PEM_write_ECPrivateKey(FILE *fp, EC_KEY *x, const EVP_CIPHER *enc,
377 unsigned char *kstr, int klen,
378 pem_password_cb *cb, void *u)
379{
380 EVP_PKEY *k;
381 int ret;
382 k = EVP_PKEY_new();
383 if (!k)
384 return 0;
385 EVP_PKEY_set1_EC_KEY(k, x);
386 ret = PEM_write_PrivateKey(fp, k, enc, kstr, klen, cb, u);
387 EVP_PKEY_free(k);
388 return ret;
389}
390#endif
391
392#else
393
273IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY, ECPrivateKey) 394IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY, ECPrivateKey)
274 395
396#endif
397
275IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY) 398IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
276 399
277#ifndef OPENSSL_NO_FP_API 400#ifndef OPENSSL_NO_FP_API
@@ -301,8 +424,59 @@ IMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
301 * (When reading, parameter PEM_STRING_EVP_PKEY is a wildcard for anything 424 * (When reading, parameter PEM_STRING_EVP_PKEY is a wildcard for anything
302 * appropriate.) 425 * appropriate.)
303 */ 426 */
427
428#ifdef OPENSSL_FIPS
429
430static const char *pkey_str(EVP_PKEY *x)
431 {
432 switch (x->type)
433 {
434 case EVP_PKEY_RSA:
435 return PEM_STRING_RSA;
436
437 case EVP_PKEY_DSA:
438 return PEM_STRING_DSA;
439
440 case EVP_PKEY_EC:
441 return PEM_STRING_ECPRIVATEKEY;
442
443 default:
444 return NULL;
445 }
446 }
447
448
449int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
450 unsigned char *kstr, int klen,
451 pem_password_cb *cb, void *u)
452 {
453 if (FIPS_mode())
454 return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
455 (char *)kstr, klen, cb, u);
456 else
457 return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
458 pkey_str(x), bp,(char *)x,enc,kstr,klen,cb,u);
459 }
460
461#ifndef OPENSSL_NO_FP_API
462int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
463 unsigned char *kstr, int klen,
464 pem_password_cb *cb, void *u)
465 {
466 if (FIPS_mode())
467 return PEM_write_PKCS8PrivateKey(fp, x, enc,
468 (char *)kstr, klen, cb, u);
469 else
470 return PEM_ASN1_write((i2d_of_void *)i2d_PrivateKey,
471 pkey_str(x), fp,(char *)x,enc,kstr,klen,cb,u);
472 }
473#endif
474
475#else
304IMPLEMENT_PEM_write_cb(PrivateKey, EVP_PKEY, ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:\ 476IMPLEMENT_PEM_write_cb(PrivateKey, EVP_PKEY, ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:\
305 (x->type == EVP_PKEY_RSA)?PEM_STRING_RSA:PEM_STRING_ECPRIVATEKEY), PrivateKey) 477 (x->type == EVP_PKEY_RSA)?PEM_STRING_RSA:PEM_STRING_ECPRIVATEKEY), PrivateKey)
306 478
479#endif
480
307IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY) 481IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
308 482
diff --git a/src/lib/libcrypto/pem/pem_lib.c b/src/lib/libcrypto/pem/pem_lib.c
index 9bae4c8850..cbafefe416 100644
--- a/src/lib/libcrypto/pem/pem_lib.c
+++ b/src/lib/libcrypto/pem/pem_lib.c
@@ -216,6 +216,9 @@ static int check_pem(const char *nm, const char *name)
216 if(!strcmp(nm, PEM_STRING_X509) && 216 if(!strcmp(nm, PEM_STRING_X509) &&
217 !strcmp(name, PEM_STRING_PKCS7)) return 1; 217 !strcmp(name, PEM_STRING_PKCS7)) return 1;
218 218
219 if(!strcmp(nm, PEM_STRING_PKCS7_SIGNED) &&
220 !strcmp(name, PEM_STRING_PKCS7)) return 1;
221
219 return 0; 222 return 0;
220} 223}
221 224
diff --git a/src/lib/libcrypto/pem/pem_x509.c b/src/lib/libcrypto/pem/pem_x509.c
index 19f88d8d3a..3f709f13e6 100644
--- a/src/lib/libcrypto/pem/pem_x509.c
+++ b/src/lib/libcrypto/pem/pem_x509.c
@@ -1,5 +1,5 @@
1/* pem_x509.c */ 1/* pem_x509.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pem/pem_xaux.c b/src/lib/libcrypto/pem/pem_xaux.c
index 63ce660cf1..7cc7491009 100644
--- a/src/lib/libcrypto/pem/pem_xaux.c
+++ b/src/lib/libcrypto/pem/pem_xaux.c
@@ -1,5 +1,5 @@
1/* pem_xaux.c */ 1/* pem_xaux.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_add.c b/src/lib/libcrypto/pkcs12/p12_add.c
index 41bdc00551..1f3e378f5c 100644
--- a/src/lib/libcrypto/pkcs12/p12_add.c
+++ b/src/lib/libcrypto/pkcs12/p12_add.c
@@ -1,5 +1,5 @@
1/* p12_add.c */ 1/* p12_add.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_asn.c b/src/lib/libcrypto/pkcs12/p12_asn.c
index a3739fee1a..6e27633817 100644
--- a/src/lib/libcrypto/pkcs12/p12_asn.c
+++ b/src/lib/libcrypto/pkcs12/p12_asn.c
@@ -1,5 +1,5 @@
1/* p12_asn.c */ 1/* p12_asn.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_attr.c b/src/lib/libcrypto/pkcs12/p12_attr.c
index 026cf3826a..68d6c5ad15 100644
--- a/src/lib/libcrypto/pkcs12/p12_attr.c
+++ b/src/lib/libcrypto/pkcs12/p12_attr.c
@@ -1,5 +1,5 @@
1/* p12_attr.c */ 1/* p12_attr.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_crpt.c b/src/lib/libcrypto/pkcs12/p12_crpt.c
index 3ad33c49d8..f8b952e27e 100644
--- a/src/lib/libcrypto/pkcs12/p12_crpt.c
+++ b/src/lib/libcrypto/pkcs12/p12_crpt.c
@@ -1,5 +1,5 @@
1/* p12_crpt.c */ 1/* p12_crpt.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_crt.c b/src/lib/libcrypto/pkcs12/p12_crt.c
index dbafda17b6..e863de52ce 100644
--- a/src/lib/libcrypto/pkcs12/p12_crt.c
+++ b/src/lib/libcrypto/pkcs12/p12_crt.c
@@ -1,5 +1,5 @@
1/* p12_crt.c */ 1/* p12_crt.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project. 3 * project.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -59,10 +59,27 @@
59#include <stdio.h> 59#include <stdio.h>
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include <openssl/pkcs12.h> 61#include <openssl/pkcs12.h>
62#ifdef OPENSSL_FIPS
63#include <openssl/fips.h>
64#endif
65
62 66
63 67
64static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag); 68static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag);
65 69
70static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
71 {
72 int idx;
73 X509_ATTRIBUTE *attr;
74 idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
75 if (idx < 0)
76 return 1;
77 attr = EVP_PKEY_get_attr(pkey, idx);
78 if (!X509at_add1_attr(&bag->attrib, attr))
79 return 0;
80 return 1;
81 }
82
66PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert, 83PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,
67 STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter, 84 STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter,
68 int keytype) 85 int keytype)
@@ -77,7 +94,14 @@ PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,
77 94
78 /* Set defaults */ 95 /* Set defaults */
79 if (!nid_cert) 96 if (!nid_cert)
97 {
98#ifdef OPENSSL_FIPS
99 if (FIPS_mode())
100 nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
101 else
102#endif
80 nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC; 103 nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
104 }
81 if (!nid_key) 105 if (!nid_key)
82 nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; 106 nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
83 if (!iter) 107 if (!iter)
@@ -122,20 +146,15 @@ PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,
122 146
123 if (pkey) 147 if (pkey)
124 { 148 {
125 int cspidx;
126 bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass); 149 bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
127 150
128 if (!bag) 151 if (!bag)
129 goto err; 152 goto err;
130 153
131 cspidx = EVP_PKEY_get_attr_by_NID(pkey, NID_ms_csp_name, -1); 154 if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
132 if (cspidx >= 0) 155 goto err;
133 { 156 if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
134 X509_ATTRIBUTE *cspattr; 157 goto err;
135 cspattr = EVP_PKEY_get_attr(pkey, cspidx);
136 if (!X509at_add1_attr(&bag->attrib, cspattr))
137 goto err;
138 }
139 158
140 if(name && !PKCS12_add_friendlyname(bag, name, -1)) 159 if(name && !PKCS12_add_friendlyname(bag, name, -1))
141 goto err; 160 goto err;
diff --git a/src/lib/libcrypto/pkcs12/p12_decr.c b/src/lib/libcrypto/pkcs12/p12_decr.c
index 74c961a92b..ba77dbbe32 100644
--- a/src/lib/libcrypto/pkcs12/p12_decr.c
+++ b/src/lib/libcrypto/pkcs12/p12_decr.c
@@ -1,5 +1,5 @@
1/* p12_decr.c */ 1/* p12_decr.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_init.c b/src/lib/libcrypto/pkcs12/p12_init.c
index 6bdc132631..d4d84b056a 100644
--- a/src/lib/libcrypto/pkcs12/p12_init.c
+++ b/src/lib/libcrypto/pkcs12/p12_init.c
@@ -1,5 +1,5 @@
1/* p12_init.c */ 1/* p12_init.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_key.c b/src/lib/libcrypto/pkcs12/p12_key.c
index 18e72d0a1b..9e57eee4a4 100644
--- a/src/lib/libcrypto/pkcs12/p12_key.c
+++ b/src/lib/libcrypto/pkcs12/p12_key.c
@@ -1,5 +1,5 @@
1/* p12_key.c */ 1/* p12_key.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_kiss.c b/src/lib/libcrypto/pkcs12/p12_kiss.c
index c2ee2cc6f3..5c4c6ec988 100644
--- a/src/lib/libcrypto/pkcs12/p12_kiss.c
+++ b/src/lib/libcrypto/pkcs12/p12_kiss.c
@@ -1,5 +1,5 @@
1/* p12_kiss.c */ 1/* p12_kiss.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_mutl.c b/src/lib/libcrypto/pkcs12/p12_mutl.c
index c408cc8ab8..70bfef6e5d 100644
--- a/src/lib/libcrypto/pkcs12/p12_mutl.c
+++ b/src/lib/libcrypto/pkcs12/p12_mutl.c
@@ -1,5 +1,5 @@
1/* p12_mutl.c */ 1/* p12_mutl.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_npas.c b/src/lib/libcrypto/pkcs12/p12_npas.c
index 48eacc5c49..47e5e9c377 100644
--- a/src/lib/libcrypto/pkcs12/p12_npas.c
+++ b/src/lib/libcrypto/pkcs12/p12_npas.c
@@ -1,5 +1,5 @@
1/* p12_npas.c */ 1/* p12_npas.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_p8d.c b/src/lib/libcrypto/pkcs12/p12_p8d.c
index 3c6f377933..deba81e4a9 100644
--- a/src/lib/libcrypto/pkcs12/p12_p8d.c
+++ b/src/lib/libcrypto/pkcs12/p12_p8d.c
@@ -1,5 +1,5 @@
1/* p12_p8d.c */ 1/* p12_p8d.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_p8e.c b/src/lib/libcrypto/pkcs12/p12_p8e.c
index 3d47956652..bf20a77b4c 100644
--- a/src/lib/libcrypto/pkcs12/p12_p8e.c
+++ b/src/lib/libcrypto/pkcs12/p12_p8e.c
@@ -1,5 +1,5 @@
1/* p12_p8e.c */ 1/* p12_p8e.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/p12_utl.c b/src/lib/libcrypto/pkcs12/p12_utl.c
index 243ec76be9..ca30ac4f6d 100644
--- a/src/lib/libcrypto/pkcs12/p12_utl.c
+++ b/src/lib/libcrypto/pkcs12/p12_utl.c
@@ -1,5 +1,5 @@
1/* p12_utl.c */ 1/* p12_utl.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs12/pkcs12.h b/src/lib/libcrypto/pkcs12/pkcs12.h
index a2d7e359a0..4bee605dc0 100644
--- a/src/lib/libcrypto/pkcs12/pkcs12.h
+++ b/src/lib/libcrypto/pkcs12/pkcs12.h
@@ -1,5 +1,5 @@
1/* pkcs12.h */ 1/* pkcs12.h */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs7/pk7_asn1.c b/src/lib/libcrypto/pkcs7/pk7_asn1.c
index 77931feeb4..1f70d31386 100644
--- a/src/lib/libcrypto/pkcs7/pk7_asn1.c
+++ b/src/lib/libcrypto/pkcs7/pk7_asn1.c
@@ -1,5 +1,5 @@
1/* pk7_asn.c */ 1/* pk7_asn.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs7/pk7_attr.c b/src/lib/libcrypto/pkcs7/pk7_attr.c
index 735c8800e1..d549717169 100644
--- a/src/lib/libcrypto/pkcs7/pk7_attr.c
+++ b/src/lib/libcrypto/pkcs7/pk7_attr.c
@@ -1,5 +1,5 @@
1/* pk7_attr.c */ 1/* pk7_attr.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs7/pk7_mime.c b/src/lib/libcrypto/pkcs7/pk7_mime.c
index 17b68992f7..bf190360d7 100644
--- a/src/lib/libcrypto/pkcs7/pk7_mime.c
+++ b/src/lib/libcrypto/pkcs7/pk7_mime.c
@@ -1,5 +1,5 @@
1/* pk7_mime.c */ 1/* pk7_mime.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project. 3 * project.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/pkcs7/pk7_smime.c b/src/lib/libcrypto/pkcs7/pk7_smime.c
index 5c6b0fe24b..c34db1d6fe 100644
--- a/src/lib/libcrypto/pkcs7/pk7_smime.c
+++ b/src/lib/libcrypto/pkcs7/pk7_smime.c
@@ -1,5 +1,5 @@
1/* pk7_smime.c */ 1/* pk7_smime.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project. 3 * project.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -282,6 +282,7 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
282 PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE); 282 PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE);
283 goto err; 283 goto err;
284 } 284 }
285 BIO_set_mem_eof_return(tmpout, 0);
285 } else tmpout = out; 286 } else tmpout = out;
286 287
287 /* We now have to 'read' from p7bio to calculate digests etc. */ 288 /* We now have to 'read' from p7bio to calculate digests etc. */
diff --git a/src/lib/libcrypto/rand/rand.h b/src/lib/libcrypto/rand/rand.h
index ac6c021763..ea89153cba 100644
--- a/src/lib/libcrypto/rand/rand.h
+++ b/src/lib/libcrypto/rand/rand.h
@@ -72,7 +72,7 @@ extern "C" {
72#endif 72#endif
73 73
74#if defined(OPENSSL_FIPS) 74#if defined(OPENSSL_FIPS)
75#define FIPS_RAND_SIZE_T size_t 75#define FIPS_RAND_SIZE_T int
76#endif 76#endif
77 77
78/* Already defined in ossl_typ.h */ 78/* Already defined in ossl_typ.h */
@@ -111,6 +111,15 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes);
111int RAND_egd(const char *path); 111int RAND_egd(const char *path);
112int RAND_egd_bytes(const char *path,int bytes); 112int RAND_egd_bytes(const char *path,int bytes);
113int RAND_poll(void); 113int RAND_poll(void);
114#ifndef OPENSSL_NO_ENGINE
115#ifdef OPENSSL_FIPS
116void int_RAND_init_engine_callbacks(void);
117void int_RAND_set_callbacks(
118 int (*set_rand_func)(const RAND_METHOD *meth,
119 const RAND_METHOD **pmeth),
120 const RAND_METHOD *(*get_rand_func)(const RAND_METHOD **pmeth));
121#endif
122#endif
114 123
115#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) 124#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
116 125
@@ -128,11 +137,29 @@ void ERR_load_RAND_strings(void);
128/* Error codes for the RAND functions. */ 137/* Error codes for the RAND functions. */
129 138
130/* Function codes. */ 139/* Function codes. */
140#define RAND_F_ENG_RAND_GET_RAND_METHOD 108
141#define RAND_F_FIPS_RAND 103
142#define RAND_F_FIPS_RAND_BYTES 102
143#define RAND_F_FIPS_RAND_GET_RAND_METHOD 109
144#define RAND_F_FIPS_RAND_SET_DT 106
145#define RAND_F_FIPS_SET_DT 104
146#define RAND_F_FIPS_SET_PRNG_SEED 107
147#define RAND_F_FIPS_SET_TEST_MODE 105
131#define RAND_F_RAND_GET_RAND_METHOD 101 148#define RAND_F_RAND_GET_RAND_METHOD 101
132#define RAND_F_SSLEAY_RAND_BYTES 100 149#define RAND_F_SSLEAY_RAND_BYTES 100
133 150
134/* Reason codes. */ 151/* Reason codes. */
152#define RAND_R_NON_FIPS_METHOD 105
153#define RAND_R_NOT_IN_TEST_MODE 106
154#define RAND_R_NO_KEY_SET 107
155#define RAND_R_PRNG_ASKING_FOR_TOO_MUCH 101
156#define RAND_R_PRNG_ERROR 108
157#define RAND_R_PRNG_KEYED 109
158#define RAND_R_PRNG_NOT_REKEYED 102
159#define RAND_R_PRNG_NOT_RESEEDED 103
135#define RAND_R_PRNG_NOT_SEEDED 100 160#define RAND_R_PRNG_NOT_SEEDED 100
161#define RAND_R_PRNG_SEED_MUST_NOT_MATCH_KEY 110
162#define RAND_R_PRNG_STUCK 104
136 163
137#ifdef __cplusplus 164#ifdef __cplusplus
138} 165}
diff --git a/src/lib/libcrypto/rand/rand_err.c b/src/lib/libcrypto/rand/rand_err.c
index 386934dcd1..829fb44d77 100644
--- a/src/lib/libcrypto/rand/rand_err.c
+++ b/src/lib/libcrypto/rand/rand_err.c
@@ -1,6 +1,6 @@
1/* crypto/rand/rand_err.c */ 1/* crypto/rand/rand_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -70,6 +70,14 @@
70 70
71static ERR_STRING_DATA RAND_str_functs[]= 71static ERR_STRING_DATA RAND_str_functs[]=
72 { 72 {
73{ERR_FUNC(RAND_F_ENG_RAND_GET_RAND_METHOD), "ENG_RAND_GET_RAND_METHOD"},
74{ERR_FUNC(RAND_F_FIPS_RAND), "FIPS_RAND"},
75{ERR_FUNC(RAND_F_FIPS_RAND_BYTES), "FIPS_RAND_BYTES"},
76{ERR_FUNC(RAND_F_FIPS_RAND_GET_RAND_METHOD), "FIPS_RAND_GET_RAND_METHOD"},
77{ERR_FUNC(RAND_F_FIPS_RAND_SET_DT), "FIPS_RAND_SET_DT"},
78{ERR_FUNC(RAND_F_FIPS_SET_DT), "FIPS_SET_DT"},
79{ERR_FUNC(RAND_F_FIPS_SET_PRNG_SEED), "FIPS_SET_PRNG_SEED"},
80{ERR_FUNC(RAND_F_FIPS_SET_TEST_MODE), "FIPS_SET_TEST_MODE"},
73{ERR_FUNC(RAND_F_RAND_GET_RAND_METHOD), "RAND_get_rand_method"}, 81{ERR_FUNC(RAND_F_RAND_GET_RAND_METHOD), "RAND_get_rand_method"},
74{ERR_FUNC(RAND_F_SSLEAY_RAND_BYTES), "SSLEAY_RAND_BYTES"}, 82{ERR_FUNC(RAND_F_SSLEAY_RAND_BYTES), "SSLEAY_RAND_BYTES"},
75{0,NULL} 83{0,NULL}
@@ -77,7 +85,17 @@ static ERR_STRING_DATA RAND_str_functs[]=
77 85
78static ERR_STRING_DATA RAND_str_reasons[]= 86static ERR_STRING_DATA RAND_str_reasons[]=
79 { 87 {
88{ERR_REASON(RAND_R_NON_FIPS_METHOD) ,"non fips method"},
89{ERR_REASON(RAND_R_NOT_IN_TEST_MODE) ,"not in test mode"},
90{ERR_REASON(RAND_R_NO_KEY_SET) ,"no key set"},
91{ERR_REASON(RAND_R_PRNG_ASKING_FOR_TOO_MUCH),"prng asking for too much"},
92{ERR_REASON(RAND_R_PRNG_ERROR) ,"prng error"},
93{ERR_REASON(RAND_R_PRNG_KEYED) ,"prng keyed"},
94{ERR_REASON(RAND_R_PRNG_NOT_REKEYED) ,"prng not rekeyed"},
95{ERR_REASON(RAND_R_PRNG_NOT_RESEEDED) ,"prng not reseeded"},
80{ERR_REASON(RAND_R_PRNG_NOT_SEEDED) ,"PRNG not seeded"}, 96{ERR_REASON(RAND_R_PRNG_NOT_SEEDED) ,"PRNG not seeded"},
97{ERR_REASON(RAND_R_PRNG_SEED_MUST_NOT_MATCH_KEY),"prng seed must not match key"},
98{ERR_REASON(RAND_R_PRNG_STUCK) ,"prng stuck"},
81{0,NULL} 99{0,NULL}
82 }; 100 };
83 101
diff --git a/src/lib/libcrypto/rand/rand_lib.c b/src/lib/libcrypto/rand/rand_lib.c
index 513e338985..da6b4e0e86 100644
--- a/src/lib/libcrypto/rand/rand_lib.c
+++ b/src/lib/libcrypto/rand/rand_lib.c
@@ -60,15 +60,82 @@
60#include <time.h> 60#include <time.h>
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include <openssl/rand.h> 62#include <openssl/rand.h>
63#include "rand_lcl.h"
64#ifdef OPENSSL_FIPS
65#include <openssl/fips.h>
66#include <openssl/fips_rand.h>
67#endif
68
63#ifndef OPENSSL_NO_ENGINE 69#ifndef OPENSSL_NO_ENGINE
64#include <openssl/engine.h> 70#include <openssl/engine.h>
65#endif 71#endif
66 72
73static const RAND_METHOD *default_RAND_meth = NULL;
74
75#ifdef OPENSSL_FIPS
76
77static int fips_RAND_set_rand_method(const RAND_METHOD *meth,
78 const RAND_METHOD **pmeth)
79 {
80 *pmeth = meth;
81 return 1;
82 }
83
84static const RAND_METHOD *fips_RAND_get_rand_method(const RAND_METHOD **pmeth)
85 {
86 if (!*pmeth)
87 {
88 if(FIPS_mode())
89 *pmeth=FIPS_rand_method();
90 else
91 *pmeth = RAND_SSLeay();
92 }
93
94 if(FIPS_mode()
95 && *pmeth != FIPS_rand_check())
96 {
97 RANDerr(RAND_F_FIPS_RAND_GET_RAND_METHOD,RAND_R_NON_FIPS_METHOD);
98 return 0;
99 }
100
101 return *pmeth;
102 }
103
104static int (*RAND_set_rand_method_func)(const RAND_METHOD *meth,
105 const RAND_METHOD **pmeth)
106 = fips_RAND_set_rand_method;
107static const RAND_METHOD *(*RAND_get_rand_method_func)
108 (const RAND_METHOD **pmeth)
109 = fips_RAND_get_rand_method;
110
111#ifndef OPENSSL_NO_ENGINE
112void int_RAND_set_callbacks(
113 int (*set_rand_func)(const RAND_METHOD *meth,
114 const RAND_METHOD **pmeth),
115 const RAND_METHOD *(*get_rand_func)
116 (const RAND_METHOD **pmeth))
117 {
118 RAND_set_rand_method_func = set_rand_func;
119 RAND_get_rand_method_func = get_rand_func;
120 }
121#endif
122
123int RAND_set_rand_method(const RAND_METHOD *meth)
124 {
125 return RAND_set_rand_method_func(meth, &default_RAND_meth);
126 }
127
128const RAND_METHOD *RAND_get_rand_method(void)
129 {
130 return RAND_get_rand_method_func(&default_RAND_meth);
131 }
132
133#else
134
67#ifndef OPENSSL_NO_ENGINE 135#ifndef OPENSSL_NO_ENGINE
68/* non-NULL if default_RAND_meth is ENGINE-provided */ 136/* non-NULL if default_RAND_meth is ENGINE-provided */
69static ENGINE *funct_ref =NULL; 137static ENGINE *funct_ref =NULL;
70#endif 138#endif
71static const RAND_METHOD *default_RAND_meth = NULL;
72 139
73int RAND_set_rand_method(const RAND_METHOD *meth) 140int RAND_set_rand_method(const RAND_METHOD *meth)
74 { 141 {
@@ -129,6 +196,8 @@ int RAND_set_rand_engine(ENGINE *engine)
129 } 196 }
130#endif 197#endif
131 198
199#endif
200
132void RAND_cleanup(void) 201void RAND_cleanup(void)
133 { 202 {
134 const RAND_METHOD *meth = RAND_get_rand_method(); 203 const RAND_METHOD *meth = RAND_get_rand_method();
diff --git a/src/lib/libcrypto/rand/randfile.c b/src/lib/libcrypto/rand/randfile.c
index 6c0ec9a41c..d108353bbc 100644
--- a/src/lib/libcrypto/rand/randfile.c
+++ b/src/lib/libcrypto/rand/randfile.c
@@ -81,10 +81,25 @@
81# include <sys/stat.h> 81# include <sys/stat.h>
82#endif 82#endif
83 83
84#ifdef _WIN32
85#define stat _stat
86#define chmod _chmod
87#define open _open
88#define fdopen _fdopen
89#endif
90
84#undef BUFSIZE 91#undef BUFSIZE
85#define BUFSIZE 1024 92#define BUFSIZE 1024
86#define RAND_DATA 1024 93#define RAND_DATA 1024
87 94
95#ifdef OPENSSL_SYS_VMS
96/* This declaration is a nasty hack to get around vms' extension to fopen
97 * for passing in sharing options being disabled by our /STANDARD=ANSI89 */
98static FILE *(*const vms_fopen)(const char *, const char *, ...) =
99 (FILE *(*)(const char *, const char *, ...))fopen;
100#define VMS_OPEN_ATTRS "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
101#endif
102
88/* #define RFILE ".rnd" - defined in ../../e_os.h */ 103/* #define RFILE ".rnd" - defined in ../../e_os.h */
89 104
90/* Note that these functions are intended for seed files only. 105/* Note that these functions are intended for seed files only.
@@ -106,7 +121,11 @@ int RAND_load_file(const char *file, long bytes)
106 RAND_add(&sb,sizeof(sb),0.0); 121 RAND_add(&sb,sizeof(sb),0.0);
107 if (bytes == 0) return(ret); 122 if (bytes == 0) return(ret);
108 123
124#ifdef OPENSSL_SYS_VMS
125 in=vms_fopen(file,"rb",VMS_OPEN_ATTRS);
126#else
109 in=fopen(file,"rb"); 127 in=fopen(file,"rb");
128#endif
110 if (in == NULL) goto err; 129 if (in == NULL) goto err;
111#if defined(S_IFBLK) && defined(S_IFCHR) 130#if defined(S_IFBLK) && defined(S_IFCHR)
112 if (sb.st_mode & (S_IFBLK | S_IFCHR)) { 131 if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
@@ -167,7 +186,7 @@ int RAND_write_file(const char *file)
167#endif 186#endif
168 } 187 }
169 188
170#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32) 189#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_VMS)
171 { 190 {
172 /* For some reason Win32 can't write to files created this way */ 191 /* For some reason Win32 can't write to files created this way */
173 192
@@ -178,8 +197,34 @@ int RAND_write_file(const char *file)
178 out = fdopen(fd, "wb"); 197 out = fdopen(fd, "wb");
179 } 198 }
180#endif 199#endif
200
201#ifdef OPENSSL_SYS_VMS
202 /* VMS NOTE: Prior versions of this routine created a _new_
203 * version of the rand file for each call into this routine, then
204 * deleted all existing versions named ;-1, and finally renamed
205 * the current version as ';1'. Under concurrent usage, this
206 * resulted in an RMS race condition in rename() which could
207 * orphan files (see vms message help for RMS$_REENT). With the
208 * fopen() calls below, openssl/VMS now shares the top-level
209 * version of the rand file. Note that there may still be
210 * conditions where the top-level rand file is locked. If so, this
211 * code will then create a new version of the rand file. Without
212 * the delete and rename code, this can result in ascending file
213 * versions that stop at version 32767, and this routine will then
214 * return an error. The remedy for this is to recode the calling
215 * application to avoid concurrent use of the rand file, or
216 * synchronize usage at the application level. Also consider
217 * whether or not you NEED a persistent rand file in a concurrent
218 * use situation.
219 */
220
221 out = vms_fopen(file,"rb+",VMS_OPEN_ATTRS);
222 if (out == NULL)
223 out = vms_fopen(file,"wb",VMS_OPEN_ATTRS);
224#else
181 if (out == NULL) 225 if (out == NULL)
182 out = fopen(file,"wb"); 226 out = fopen(file,"wb");
227#endif
183 if (out == NULL) goto err; 228 if (out == NULL) goto err;
184 229
185#ifndef NO_CHMOD 230#ifndef NO_CHMOD
@@ -201,25 +246,6 @@ int RAND_write_file(const char *file)
201 ret+=i; 246 ret+=i;
202 if (n <= 0) break; 247 if (n <= 0) break;
203 } 248 }
204#ifdef OPENSSL_SYS_VMS
205 /* Try to delete older versions of the file, until there aren't
206 any */
207 {
208 char *tmpf;
209
210 tmpf = OPENSSL_malloc(strlen(file) + 4); /* to add ";-1" and a nul */
211 if (tmpf)
212 {
213 strcpy(tmpf, file);
214 strcat(tmpf, ";-1");
215 while(delete(tmpf) == 0)
216 ;
217 rename(file,";1"); /* Make sure it's version 1, or we
218 will reach the limit (32767) at
219 some point... */
220 }
221 }
222#endif /* OPENSSL_SYS_VMS */
223 249
224 fclose(out); 250 fclose(out);
225 OPENSSL_cleanse(buf,BUFSIZE); 251 OPENSSL_cleanse(buf,BUFSIZE);
diff --git a/src/lib/libcrypto/rc2/rc2.h b/src/lib/libcrypto/rc2/rc2.h
index 34c8362317..e542ec94ff 100644
--- a/src/lib/libcrypto/rc2/rc2.h
+++ b/src/lib/libcrypto/rc2/rc2.h
@@ -79,7 +79,9 @@ typedef struct rc2_key_st
79 RC2_INT data[64]; 79 RC2_INT data[64];
80 } RC2_KEY; 80 } RC2_KEY;
81 81
82 82#ifdef OPENSSL_FIPS
83void private_RC2_set_key(RC2_KEY *key, int len, const unsigned char *data,int bits);
84#endif
83void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data,int bits); 85void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data,int bits);
84void RC2_ecb_encrypt(const unsigned char *in,unsigned char *out,RC2_KEY *key, 86void RC2_ecb_encrypt(const unsigned char *in,unsigned char *out,RC2_KEY *key,
85 int enc); 87 int enc);
diff --git a/src/lib/libcrypto/rc2/rc2_skey.c b/src/lib/libcrypto/rc2/rc2_skey.c
index 4953642056..4e000e5b99 100644
--- a/src/lib/libcrypto/rc2/rc2_skey.c
+++ b/src/lib/libcrypto/rc2/rc2_skey.c
@@ -57,6 +57,11 @@
57 */ 57 */
58 58
59#include <openssl/rc2.h> 59#include <openssl/rc2.h>
60#include <openssl/crypto.h>
61#ifdef OPENSSL_FIPS
62#include <openssl/fips.h>
63#endif
64
60#include "rc2_locl.h" 65#include "rc2_locl.h"
61 66
62static unsigned char key_table[256]={ 67static unsigned char key_table[256]={
@@ -94,8 +99,20 @@ static unsigned char key_table[256]={
94 * BSAFE uses the 'retarded' version. What I previously shipped is 99 * BSAFE uses the 'retarded' version. What I previously shipped is
95 * the same as specifying 1024 for the 'bits' parameter. Bsafe uses 100 * the same as specifying 1024 for the 'bits' parameter. Bsafe uses
96 * a version where the bits parameter is the same as len*8 */ 101 * a version where the bits parameter is the same as len*8 */
102
103#ifdef OPENSSL_FIPS
97void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits) 104void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits)
98 { 105 {
106 if (FIPS_mode())
107 FIPS_BAD_ABORT(RC2)
108 private_RC2_set_key(key, len, data, bits);
109 }
110void private_RC2_set_key(RC2_KEY *key, int len, const unsigned char *data,
111 int bits)
112#else
113void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits)
114#endif
115 {
99 int i,j; 116 int i,j;
100 unsigned char *k; 117 unsigned char *k;
101 RC2_INT *ki; 118 RC2_INT *ki;
diff --git a/src/lib/libcrypto/rc4/asm/rc4-x86_64.pl b/src/lib/libcrypto/rc4/asm/rc4-x86_64.pl
index 2d47320485..00c6fa28aa 100755
--- a/src/lib/libcrypto/rc4/asm/rc4-x86_64.pl
+++ b/src/lib/libcrypto/rc4/asm/rc4-x86_64.pl
@@ -359,6 +359,8 @@ ___
359 359
360$code =~ s/#([bwd])/$1/gm; 360$code =~ s/#([bwd])/$1/gm;
361 361
362$code =~ s/RC4_set_key/private_RC4_set_key/g if ($ENV{FIPSCANLIB} ne "");
363
362print $code; 364print $code;
363 365
364close STDOUT; 366close STDOUT;
diff --git a/src/lib/libcrypto/rc4/rc4.h b/src/lib/libcrypto/rc4/rc4.h
index 7aec04fe93..2d8620d33b 100644
--- a/src/lib/libcrypto/rc4/rc4.h
+++ b/src/lib/libcrypto/rc4/rc4.h
@@ -76,6 +76,9 @@ typedef struct rc4_key_st
76 76
77 77
78const char *RC4_options(void); 78const char *RC4_options(void);
79#ifdef OPENSSL_FIPS
80void private_RC4_set_key(RC4_KEY *key, int len, const unsigned char *data);
81#endif
79void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data); 82void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data);
80void RC4(RC4_KEY *key, unsigned long len, const unsigned char *indata, 83void RC4(RC4_KEY *key, unsigned long len, const unsigned char *indata,
81 unsigned char *outdata); 84 unsigned char *outdata);
diff --git a/src/lib/libcrypto/rc4/rc4_skey.c b/src/lib/libcrypto/rc4/rc4_skey.c
index 46b77ec321..4478d1a4b3 100644
--- a/src/lib/libcrypto/rc4/rc4_skey.c
+++ b/src/lib/libcrypto/rc4/rc4_skey.c
@@ -59,6 +59,11 @@
59#include <openssl/rc4.h> 59#include <openssl/rc4.h>
60#include "rc4_locl.h" 60#include "rc4_locl.h"
61#include <openssl/opensslv.h> 61#include <openssl/opensslv.h>
62#include <openssl/crypto.h>
63#ifdef OPENSSL_FIPS
64#include <openssl/fips.h>
65#endif
66
62 67
63const char RC4_version[]="RC4" OPENSSL_VERSION_PTEXT; 68const char RC4_version[]="RC4" OPENSSL_VERSION_PTEXT;
64 69
@@ -85,7 +90,11 @@ const char *RC4_options(void)
85 * Date: Wed, 14 Sep 1994 06:35:31 GMT 90 * Date: Wed, 14 Sep 1994 06:35:31 GMT
86 */ 91 */
87 92
93#ifdef OPENSSL_FIPS
94void private_RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
95#else
88void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data) 96void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
97#endif
89 { 98 {
90 register RC4_INT tmp; 99 register RC4_INT tmp;
91 register int id1,id2; 100 register int id1,id2;
@@ -127,7 +136,12 @@ void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
127 * 136 *
128 * <appro@fy.chalmers.se> 137 * <appro@fy.chalmers.se>
129 */ 138 */
130 if (OPENSSL_ia32cap_P & (1<<20)) { 139#ifdef OPENSSL_FIPS
140 unsigned long *ia32cap_ptr = OPENSSL_ia32cap_loc();
141 if (ia32cap_ptr && (*ia32cap_ptr & (1<<28))) {
142#else
143 if (OPENSSL_ia32cap_P & (1<<28)) {
144#endif
131 unsigned char *cp=(unsigned char *)d; 145 unsigned char *cp=(unsigned char *)d;
132 146
133 for (i=0;i<256;i++) cp[i]=i; 147 for (i=0;i<256;i++) cp[i]=i;
diff --git a/src/lib/libcrypto/ripemd/ripemd.h b/src/lib/libcrypto/ripemd/ripemd.h
index 033a5965b5..3b6d04386d 100644
--- a/src/lib/libcrypto/ripemd/ripemd.h
+++ b/src/lib/libcrypto/ripemd/ripemd.h
@@ -90,7 +90,9 @@ typedef struct RIPEMD160state_st
90 RIPEMD160_LONG data[RIPEMD160_LBLOCK]; 90 RIPEMD160_LONG data[RIPEMD160_LBLOCK];
91 unsigned int num; 91 unsigned int num;
92 } RIPEMD160_CTX; 92 } RIPEMD160_CTX;
93 93#ifdef OPENSSL_FIPS
94int private_RIPEMD160_Init(RIPEMD160_CTX *c);
95#endif
94int RIPEMD160_Init(RIPEMD160_CTX *c); 96int RIPEMD160_Init(RIPEMD160_CTX *c);
95int RIPEMD160_Update(RIPEMD160_CTX *c, const void *data, size_t len); 97int RIPEMD160_Update(RIPEMD160_CTX *c, const void *data, size_t len);
96int RIPEMD160_Final(unsigned char *md, RIPEMD160_CTX *c); 98int RIPEMD160_Final(unsigned char *md, RIPEMD160_CTX *c);
diff --git a/src/lib/libcrypto/ripemd/rmd_dgst.c b/src/lib/libcrypto/ripemd/rmd_dgst.c
index 61626284b8..ead11d075a 100644
--- a/src/lib/libcrypto/ripemd/rmd_dgst.c
+++ b/src/lib/libcrypto/ripemd/rmd_dgst.c
@@ -59,6 +59,11 @@
59#include <stdio.h> 59#include <stdio.h>
60#include "rmd_locl.h" 60#include "rmd_locl.h"
61#include <openssl/opensslv.h> 61#include <openssl/opensslv.h>
62#include <openssl/err.h>
63#ifdef OPENSSL_FIPS
64#include <openssl/fips.h>
65#endif
66
62 67
63const char RMD160_version[]="RIPE-MD160" OPENSSL_VERSION_PTEXT; 68const char RMD160_version[]="RIPE-MD160" OPENSSL_VERSION_PTEXT;
64 69
@@ -69,7 +74,7 @@ const char RMD160_version[]="RIPE-MD160" OPENSSL_VERSION_PTEXT;
69 void ripemd160_block(RIPEMD160_CTX *c, unsigned long *p,size_t num); 74 void ripemd160_block(RIPEMD160_CTX *c, unsigned long *p,size_t num);
70# endif 75# endif
71 76
72int RIPEMD160_Init(RIPEMD160_CTX *c) 77FIPS_NON_FIPS_MD_Init(RIPEMD160)
73 { 78 {
74 c->A=RIPEMD160_A; 79 c->A=RIPEMD160_A;
75 c->B=RIPEMD160_B; 80 c->B=RIPEMD160_B;
diff --git a/src/lib/libcrypto/ripemd/rmd_locl.h b/src/lib/libcrypto/ripemd/rmd_locl.h
index f14b346e66..ce12a8000e 100644
--- a/src/lib/libcrypto/ripemd/rmd_locl.h
+++ b/src/lib/libcrypto/ripemd/rmd_locl.h
@@ -72,7 +72,7 @@
72 */ 72 */
73#ifdef RMD160_ASM 73#ifdef RMD160_ASM
74# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__INTEL__) 74# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__INTEL__)
75# define ripemd160_block_data_order ripemd160_block_asm_data_order 75# define ripemd160_block_host_order ripemd160_block_asm_data_order
76# endif 76# endif
77#endif 77#endif
78 78
diff --git a/src/lib/libcrypto/rsa/rsa.h b/src/lib/libcrypto/rsa/rsa.h
index 6b5e4f8a9a..5bb932ae15 100644
--- a/src/lib/libcrypto/rsa/rsa.h
+++ b/src/lib/libcrypto/rsa/rsa.h
@@ -74,6 +74,25 @@
74#error RSA is disabled. 74#error RSA is disabled.
75#endif 75#endif
76 76
77/* If this flag is set the RSA method is FIPS compliant and can be used
78 * in FIPS mode. This is set in the validated module method. If an
79 * application sets this flag in its own methods it is its reposibility
80 * to ensure the result is compliant.
81 */
82
83#define RSA_FLAG_FIPS_METHOD 0x0400
84
85/* If this flag is set the operations normally disabled in FIPS mode are
86 * permitted it is then the applications responsibility to ensure that the
87 * usage is compliant.
88 */
89
90#define RSA_FLAG_NON_FIPS_ALLOW 0x0400
91
92#ifdef OPENSSL_FIPS
93#define FIPS_RSA_SIZE_T int
94#endif
95
77#ifdef __cplusplus 96#ifdef __cplusplus
78extern "C" { 97extern "C" {
79#endif 98#endif
@@ -163,6 +182,8 @@ struct rsa_st
163# define OPENSSL_RSA_MAX_MODULUS_BITS 16384 182# define OPENSSL_RSA_MAX_MODULUS_BITS 16384
164#endif 183#endif
165 184
185#define OPENSSL_RSA_FIPS_MIN_MODULUS_BITS 1024
186
166#ifndef OPENSSL_RSA_SMALL_MODULUS_BITS 187#ifndef OPENSSL_RSA_SMALL_MODULUS_BITS
167# define OPENSSL_RSA_SMALL_MODULUS_BITS 3072 188# define OPENSSL_RSA_SMALL_MODULUS_BITS 3072
168#endif 189#endif
@@ -240,6 +261,11 @@ RSA * RSA_generate_key(int bits, unsigned long e,void
240 261
241/* New version */ 262/* New version */
242int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb); 263int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
264int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, BIGNUM *q2,
265 const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *Xp,
266 const BIGNUM *Xq1, const BIGNUM *Xq2, const BIGNUM *Xq,
267 const BIGNUM *e, BN_GENCB *cb);
268int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e, BN_GENCB *cb);
243 269
244int RSA_check_key(const RSA *); 270int RSA_check_key(const RSA *);
245 /* next 4 return -1 on error */ 271 /* next 4 return -1 on error */
@@ -257,6 +283,11 @@ int RSA_up_ref(RSA *r);
257 283
258int RSA_flags(const RSA *r); 284int RSA_flags(const RSA *r);
259 285
286#ifdef OPENSSL_FIPS
287RSA *FIPS_rsa_new(void);
288void FIPS_rsa_free(RSA *r);
289#endif
290
260void RSA_set_default_method(const RSA_METHOD *meth); 291void RSA_set_default_method(const RSA_METHOD *meth);
261const RSA_METHOD *RSA_get_default_method(void); 292const RSA_METHOD *RSA_get_default_method(void);
262const RSA_METHOD *RSA_get_method(const RSA *rsa); 293const RSA_METHOD *RSA_get_method(const RSA *rsa);
@@ -281,6 +312,7 @@ int RSA_print_fp(FILE *fp, const RSA *r,int offset);
281int RSA_print(BIO *bp, const RSA *r,int offset); 312int RSA_print(BIO *bp, const RSA *r,int offset);
282#endif 313#endif
283 314
315#ifndef OPENSSL_NO_RC4
284int i2d_RSA_NET(const RSA *a, unsigned char **pp, 316int i2d_RSA_NET(const RSA *a, unsigned char **pp,
285 int (*cb)(char *buf, int len, const char *prompt, int verify), 317 int (*cb)(char *buf, int len, const char *prompt, int verify),
286 int sgckey); 318 int sgckey);
@@ -294,6 +326,7 @@ int i2d_Netscape_RSA(const RSA *a, unsigned char **pp,
294RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, 326RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length,
295 int (*cb)(char *buf, int len, const char *prompt, 327 int (*cb)(char *buf, int len, const char *prompt,
296 int verify)); 328 int verify));
329#endif
297 330
298/* The following 2 functions sign and verify a X509_SIG ASN1 object 331/* The following 2 functions sign and verify a X509_SIG ASN1 object
299 * inside PKCS#1 padded RSA encryption */ 332 * inside PKCS#1 padded RSA encryption */
@@ -368,6 +401,8 @@ void ERR_load_RSA_strings(void);
368/* Error codes for the RSA functions. */ 401/* Error codes for the RSA functions. */
369 402
370/* Function codes. */ 403/* Function codes. */
404#define RSA_F_FIPS_RSA_SIGN 140
405#define RSA_F_FIPS_RSA_VERIFY 141
371#define RSA_F_MEMORY_LOCK 100 406#define RSA_F_MEMORY_LOCK 100
372#define RSA_F_RSA_BUILTIN_KEYGEN 129 407#define RSA_F_RSA_BUILTIN_KEYGEN 129
373#define RSA_F_RSA_CHECK_KEY 123 408#define RSA_F_RSA_CHECK_KEY 123
@@ -399,7 +434,11 @@ void ERR_load_RSA_strings(void);
399#define RSA_F_RSA_PADDING_CHECK_X931 128 434#define RSA_F_RSA_PADDING_CHECK_X931 128
400#define RSA_F_RSA_PRINT 115 435#define RSA_F_RSA_PRINT 115
401#define RSA_F_RSA_PRINT_FP 116 436#define RSA_F_RSA_PRINT_FP 116
437#define RSA_F_RSA_PRIVATE_ENCRYPT 137
438#define RSA_F_RSA_PUBLIC_DECRYPT 138
402#define RSA_F_RSA_SETUP_BLINDING 136 439#define RSA_F_RSA_SETUP_BLINDING 136
440#define RSA_F_RSA_SET_DEFAULT_METHOD 139
441#define RSA_F_RSA_SET_METHOD 142
403#define RSA_F_RSA_SIGN 117 442#define RSA_F_RSA_SIGN 117
404#define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118 443#define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118
405#define RSA_F_RSA_VERIFY 119 444#define RSA_F_RSA_VERIFY 119
@@ -433,10 +472,12 @@ void ERR_load_RSA_strings(void);
433#define RSA_R_KEY_SIZE_TOO_SMALL 120 472#define RSA_R_KEY_SIZE_TOO_SMALL 120
434#define RSA_R_LAST_OCTET_INVALID 134 473#define RSA_R_LAST_OCTET_INVALID 134
435#define RSA_R_MODULUS_TOO_LARGE 105 474#define RSA_R_MODULUS_TOO_LARGE 105
475#define RSA_R_NON_FIPS_METHOD 141
436#define RSA_R_NO_PUBLIC_EXPONENT 140 476#define RSA_R_NO_PUBLIC_EXPONENT 140
437#define RSA_R_NULL_BEFORE_BLOCK_MISSING 113 477#define RSA_R_NULL_BEFORE_BLOCK_MISSING 113
438#define RSA_R_N_DOES_NOT_EQUAL_P_Q 127 478#define RSA_R_N_DOES_NOT_EQUAL_P_Q 127
439#define RSA_R_OAEP_DECODING_ERROR 121 479#define RSA_R_OAEP_DECODING_ERROR 121
480#define RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE 142
440#define RSA_R_PADDING_CHECK_FAILED 114 481#define RSA_R_PADDING_CHECK_FAILED 114
441#define RSA_R_P_NOT_PRIME 128 482#define RSA_R_P_NOT_PRIME 128
442#define RSA_R_Q_NOT_PRIME 129 483#define RSA_R_Q_NOT_PRIME 129
diff --git a/src/lib/libcrypto/rsa/rsa_asn1.c b/src/lib/libcrypto/rsa/rsa_asn1.c
index bbbf26d50e..6e8a803e81 100644
--- a/src/lib/libcrypto/rsa/rsa_asn1.c
+++ b/src/lib/libcrypto/rsa/rsa_asn1.c
@@ -1,5 +1,5 @@
1/* rsa_asn1.c */ 1/* rsa_asn1.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c
index ffadaab9a4..0ac6418449 100644
--- a/src/lib/libcrypto/rsa/rsa_eay.c
+++ b/src/lib/libcrypto/rsa/rsa_eay.c
@@ -115,7 +115,7 @@
115#include <openssl/rsa.h> 115#include <openssl/rsa.h>
116#include <openssl/rand.h> 116#include <openssl/rand.h>
117 117
118#ifndef RSA_NULL 118#if !defined(RSA_NULL) && !defined(OPENSSL_FIPS)
119 119
120static int RSA_eay_public_encrypt(int flen, const unsigned char *from, 120static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
121 unsigned char *to, RSA *rsa,int padding); 121 unsigned char *to, RSA *rsa,int padding);
@@ -150,16 +150,6 @@ const RSA_METHOD *RSA_PKCS1_SSLeay(void)
150 return(&rsa_pkcs1_eay_meth); 150 return(&rsa_pkcs1_eay_meth);
151 } 151 }
152 152
153/* Usage example;
154 * MONT_HELPER(rsa->_method_mod_p, bn_ctx, rsa->p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
155 */
156#define MONT_HELPER(method_mod, ctx, m, pre_cond, err_instr) \
157 if ((pre_cond) && ((method_mod) == NULL) && \
158 !BN_MONT_CTX_set_locked(&(method_mod), \
159 CRYPTO_LOCK_RSA, \
160 (m), (ctx))) \
161 err_instr
162
163static int RSA_eay_public_encrypt(int flen, const unsigned char *from, 153static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
164 unsigned char *to, RSA *rsa, int padding) 154 unsigned char *to, RSA *rsa, int padding)
165 { 155 {
@@ -233,7 +223,9 @@ static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
233 goto err; 223 goto err;
234 } 224 }
235 225
236 MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); 226 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
227 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
228 goto err;
237 229
238 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, 230 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
239 rsa->_method_mod_n)) goto err; 231 rsa->_method_mod_n)) goto err;
@@ -438,7 +430,9 @@ static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
438 else 430 else
439 d= rsa->d; 431 d= rsa->d;
440 432
441 MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); 433 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
434 if(!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
435 goto err;
442 436
443 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, 437 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
444 rsa->_method_mod_n)) goto err; 438 rsa->_method_mod_n)) goto err;
@@ -559,7 +553,9 @@ static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
559 else 553 else
560 d = rsa->d; 554 d = rsa->d;
561 555
562 MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); 556 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
557 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
558 goto err;
563 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, 559 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
564 rsa->_method_mod_n)) 560 rsa->_method_mod_n))
565 goto err; 561 goto err;
@@ -669,7 +665,9 @@ static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
669 goto err; 665 goto err;
670 } 666 }
671 667
672 MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); 668 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
669 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
670 goto err;
673 671
674 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, 672 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
675 rsa->_method_mod_n)) goto err; 673 rsa->_method_mod_n)) goto err;
@@ -747,11 +745,18 @@ static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
747 q = rsa->q; 745 q = rsa->q;
748 } 746 }
749 747
750 MONT_HELPER(rsa->_method_mod_p, ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err); 748 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
751 MONT_HELPER(rsa->_method_mod_q, ctx, q, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err); 749 {
750 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx))
751 goto err;
752 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx))
753 goto err;
754 }
752 } 755 }
753 756
754 MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err); 757 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
758 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
759 goto err;
755 760
756 /* compute I mod q */ 761 /* compute I mod q */
757 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) 762 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
diff --git a/src/lib/libcrypto/rsa/rsa_err.c b/src/lib/libcrypto/rsa/rsa_err.c
index fe3ba1b44b..501f5ea389 100644
--- a/src/lib/libcrypto/rsa/rsa_err.c
+++ b/src/lib/libcrypto/rsa/rsa_err.c
@@ -1,6 +1,6 @@
1/* crypto/rsa/rsa_err.c */ 1/* crypto/rsa/rsa_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -70,6 +70,8 @@
70 70
71static ERR_STRING_DATA RSA_str_functs[]= 71static ERR_STRING_DATA RSA_str_functs[]=
72 { 72 {
73{ERR_FUNC(RSA_F_FIPS_RSA_SIGN), "FIPS_RSA_SIGN"},
74{ERR_FUNC(RSA_F_FIPS_RSA_VERIFY), "FIPS_RSA_VERIFY"},
73{ERR_FUNC(RSA_F_MEMORY_LOCK), "MEMORY_LOCK"}, 75{ERR_FUNC(RSA_F_MEMORY_LOCK), "MEMORY_LOCK"},
74{ERR_FUNC(RSA_F_RSA_BUILTIN_KEYGEN), "RSA_BUILTIN_KEYGEN"}, 76{ERR_FUNC(RSA_F_RSA_BUILTIN_KEYGEN), "RSA_BUILTIN_KEYGEN"},
75{ERR_FUNC(RSA_F_RSA_CHECK_KEY), "RSA_check_key"}, 77{ERR_FUNC(RSA_F_RSA_CHECK_KEY), "RSA_check_key"},
@@ -101,7 +103,11 @@ static ERR_STRING_DATA RSA_str_functs[]=
101{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_X931), "RSA_padding_check_X931"}, 103{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_X931), "RSA_padding_check_X931"},
102{ERR_FUNC(RSA_F_RSA_PRINT), "RSA_print"}, 104{ERR_FUNC(RSA_F_RSA_PRINT), "RSA_print"},
103{ERR_FUNC(RSA_F_RSA_PRINT_FP), "RSA_print_fp"}, 105{ERR_FUNC(RSA_F_RSA_PRINT_FP), "RSA_print_fp"},
106{ERR_FUNC(RSA_F_RSA_PRIVATE_ENCRYPT), "RSA_private_encrypt"},
107{ERR_FUNC(RSA_F_RSA_PUBLIC_DECRYPT), "RSA_public_decrypt"},
104{ERR_FUNC(RSA_F_RSA_SETUP_BLINDING), "RSA_setup_blinding"}, 108{ERR_FUNC(RSA_F_RSA_SETUP_BLINDING), "RSA_setup_blinding"},
109{ERR_FUNC(RSA_F_RSA_SET_DEFAULT_METHOD), "RSA_set_default_method"},
110{ERR_FUNC(RSA_F_RSA_SET_METHOD), "RSA_set_method"},
105{ERR_FUNC(RSA_F_RSA_SIGN), "RSA_sign"}, 111{ERR_FUNC(RSA_F_RSA_SIGN), "RSA_sign"},
106{ERR_FUNC(RSA_F_RSA_SIGN_ASN1_OCTET_STRING), "RSA_sign_ASN1_OCTET_STRING"}, 112{ERR_FUNC(RSA_F_RSA_SIGN_ASN1_OCTET_STRING), "RSA_sign_ASN1_OCTET_STRING"},
107{ERR_FUNC(RSA_F_RSA_VERIFY), "RSA_verify"}, 113{ERR_FUNC(RSA_F_RSA_VERIFY), "RSA_verify"},
@@ -138,10 +144,12 @@ static ERR_STRING_DATA RSA_str_reasons[]=
138{ERR_REASON(RSA_R_KEY_SIZE_TOO_SMALL) ,"key size too small"}, 144{ERR_REASON(RSA_R_KEY_SIZE_TOO_SMALL) ,"key size too small"},
139{ERR_REASON(RSA_R_LAST_OCTET_INVALID) ,"last octet invalid"}, 145{ERR_REASON(RSA_R_LAST_OCTET_INVALID) ,"last octet invalid"},
140{ERR_REASON(RSA_R_MODULUS_TOO_LARGE) ,"modulus too large"}, 146{ERR_REASON(RSA_R_MODULUS_TOO_LARGE) ,"modulus too large"},
147{ERR_REASON(RSA_R_NON_FIPS_METHOD) ,"non fips method"},
141{ERR_REASON(RSA_R_NO_PUBLIC_EXPONENT) ,"no public exponent"}, 148{ERR_REASON(RSA_R_NO_PUBLIC_EXPONENT) ,"no public exponent"},
142{ERR_REASON(RSA_R_NULL_BEFORE_BLOCK_MISSING),"null before block missing"}, 149{ERR_REASON(RSA_R_NULL_BEFORE_BLOCK_MISSING),"null before block missing"},
143{ERR_REASON(RSA_R_N_DOES_NOT_EQUAL_P_Q) ,"n does not equal p q"}, 150{ERR_REASON(RSA_R_N_DOES_NOT_EQUAL_P_Q) ,"n does not equal p q"},
144{ERR_REASON(RSA_R_OAEP_DECODING_ERROR) ,"oaep decoding error"}, 151{ERR_REASON(RSA_R_OAEP_DECODING_ERROR) ,"oaep decoding error"},
152{ERR_REASON(RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE),"operation not allowed in fips mode"},
145{ERR_REASON(RSA_R_PADDING_CHECK_FAILED) ,"padding check failed"}, 153{ERR_REASON(RSA_R_PADDING_CHECK_FAILED) ,"padding check failed"},
146{ERR_REASON(RSA_R_P_NOT_PRIME) ,"p not prime"}, 154{ERR_REASON(RSA_R_P_NOT_PRIME) ,"p not prime"},
147{ERR_REASON(RSA_R_Q_NOT_PRIME) ,"q not prime"}, 155{ERR_REASON(RSA_R_Q_NOT_PRIME) ,"q not prime"},
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c
index 767f7ab682..41278f83c6 100644
--- a/src/lib/libcrypto/rsa/rsa_gen.c
+++ b/src/lib/libcrypto/rsa/rsa_gen.c
@@ -68,6 +68,8 @@
68#include <openssl/bn.h> 68#include <openssl/bn.h>
69#include <openssl/rsa.h> 69#include <openssl/rsa.h>
70 70
71#ifndef OPENSSL_FIPS
72
71static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb); 73static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb);
72 74
73/* NB: this wrapper would normally be placed in rsa_lib.c and the static 75/* NB: this wrapper would normally be placed in rsa_lib.c and the static
@@ -217,3 +219,4 @@ err:
217 return ok; 219 return ok;
218 } 220 }
219 221
222#endif
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c
index 104aa4c1f2..5714841f4c 100644
--- a/src/lib/libcrypto/rsa/rsa_lib.c
+++ b/src/lib/libcrypto/rsa/rsa_lib.c
@@ -67,215 +67,6 @@
67#include <openssl/engine.h> 67#include <openssl/engine.h>
68#endif 68#endif
69 69
70const char RSA_version[]="RSA" OPENSSL_VERSION_PTEXT;
71
72static const RSA_METHOD *default_RSA_meth=NULL;
73
74RSA *RSA_new(void)
75 {
76 RSA *r=RSA_new_method(NULL);
77
78 return r;
79 }
80
81void RSA_set_default_method(const RSA_METHOD *meth)
82 {
83 default_RSA_meth = meth;
84 }
85
86const RSA_METHOD *RSA_get_default_method(void)
87 {
88 if (default_RSA_meth == NULL)
89 {
90#ifdef RSA_NULL
91 default_RSA_meth=RSA_null_method();
92#else
93#if 0 /* was: #ifdef RSAref */
94 default_RSA_meth=RSA_PKCS1_RSAref();
95#else
96 default_RSA_meth=RSA_PKCS1_SSLeay();
97#endif
98#endif
99 }
100
101 return default_RSA_meth;
102 }
103
104const RSA_METHOD *RSA_get_method(const RSA *rsa)
105 {
106 return rsa->meth;
107 }
108
109int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
110 {
111 /* NB: The caller is specifically setting a method, so it's not up to us
112 * to deal with which ENGINE it comes from. */
113 const RSA_METHOD *mtmp;
114 mtmp = rsa->meth;
115 if (mtmp->finish) mtmp->finish(rsa);
116#ifndef OPENSSL_NO_ENGINE
117 if (rsa->engine)
118 {
119 ENGINE_finish(rsa->engine);
120 rsa->engine = NULL;
121 }
122#endif
123 rsa->meth = meth;
124 if (meth->init) meth->init(rsa);
125 return 1;
126 }
127
128RSA *RSA_new_method(ENGINE *engine)
129 {
130 RSA *ret;
131
132 ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
133 if (ret == NULL)
134 {
135 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
136 return NULL;
137 }
138
139 ret->meth = RSA_get_default_method();
140#ifndef OPENSSL_NO_ENGINE
141 if (engine)
142 {
143 if (!ENGINE_init(engine))
144 {
145 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
146 OPENSSL_free(ret);
147 return NULL;
148 }
149 ret->engine = engine;
150 }
151 else
152 ret->engine = ENGINE_get_default_RSA();
153 if(ret->engine)
154 {
155 ret->meth = ENGINE_get_RSA(ret->engine);
156 if(!ret->meth)
157 {
158 RSAerr(RSA_F_RSA_NEW_METHOD,
159 ERR_R_ENGINE_LIB);
160 ENGINE_finish(ret->engine);
161 OPENSSL_free(ret);
162 return NULL;
163 }
164 }
165#endif
166
167 ret->pad=0;
168 ret->version=0;
169 ret->n=NULL;
170 ret->e=NULL;
171 ret->d=NULL;
172 ret->p=NULL;
173 ret->q=NULL;
174 ret->dmp1=NULL;
175 ret->dmq1=NULL;
176 ret->iqmp=NULL;
177 ret->references=1;
178 ret->_method_mod_n=NULL;
179 ret->_method_mod_p=NULL;
180 ret->_method_mod_q=NULL;
181 ret->blinding=NULL;
182 ret->mt_blinding=NULL;
183 ret->bignum_data=NULL;
184 ret->flags=ret->meth->flags;
185 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
186 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
187 {
188#ifndef OPENSSL_NO_ENGINE
189 if (ret->engine)
190 ENGINE_finish(ret->engine);
191#endif
192 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
193 OPENSSL_free(ret);
194 ret=NULL;
195 }
196 return(ret);
197 }
198
199void RSA_free(RSA *r)
200 {
201 int i;
202
203 if (r == NULL) return;
204
205 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
206#ifdef REF_PRINT
207 REF_PRINT("RSA",r);
208#endif
209 if (i > 0) return;
210#ifdef REF_CHECK
211 if (i < 0)
212 {
213 fprintf(stderr,"RSA_free, bad reference count\n");
214 abort();
215 }
216#endif
217
218 if (r->meth->finish)
219 r->meth->finish(r);
220#ifndef OPENSSL_NO_ENGINE
221 if (r->engine)
222 ENGINE_finish(r->engine);
223#endif
224
225 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
226
227 if (r->n != NULL) BN_clear_free(r->n);
228 if (r->e != NULL) BN_clear_free(r->e);
229 if (r->d != NULL) BN_clear_free(r->d);
230 if (r->p != NULL) BN_clear_free(r->p);
231 if (r->q != NULL) BN_clear_free(r->q);
232 if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
233 if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
234 if (r->iqmp != NULL) BN_clear_free(r->iqmp);
235 if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
236 if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding);
237 if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
238 OPENSSL_free(r);
239 }
240
241int RSA_up_ref(RSA *r)
242 {
243 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
244#ifdef REF_PRINT
245 REF_PRINT("RSA",r);
246#endif
247#ifdef REF_CHECK
248 if (i < 2)
249 {
250 fprintf(stderr, "RSA_up_ref, bad reference count\n");
251 abort();
252 }
253#endif
254 return ((i > 1) ? 1 : 0);
255 }
256
257int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
258 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
259 {
260 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
261 new_func, dup_func, free_func);
262 }
263
264int RSA_set_ex_data(RSA *r, int idx, void *arg)
265 {
266 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
267 }
268
269void *RSA_get_ex_data(const RSA *r, int idx)
270 {
271 return(CRYPTO_get_ex_data(&r->ex_data,idx));
272 }
273
274int RSA_size(const RSA *r)
275 {
276 return(BN_num_bytes(r->n));
277 }
278
279int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, 70int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
280 RSA *rsa, int padding) 71 RSA *rsa, int padding)
281 { 72 {
@@ -285,6 +76,13 @@ int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
285int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, 76int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
286 RSA *rsa, int padding) 77 RSA *rsa, int padding)
287 { 78 {
79#ifdef OPENSSL_FIPS
80 if(FIPS_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW))
81 {
82 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
83 return 0;
84 }
85#endif
288 return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); 86 return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
289 } 87 }
290 88
@@ -297,12 +95,19 @@ int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
297int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, 95int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
298 RSA *rsa, int padding) 96 RSA *rsa, int padding)
299 { 97 {
98#ifdef OPENSSL_FIPS
99 if(FIPS_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW))
100 {
101 RSAerr(RSA_F_RSA_PUBLIC_DECRYPT, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
102 return 0;
103 }
104#endif
300 return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); 105 return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
301 } 106 }
302 107
303int RSA_flags(const RSA *r) 108int RSA_size(const RSA *r)
304 { 109 {
305 return((r == NULL)?0:r->meth->flags); 110 return(BN_num_bytes(r->n));
306 } 111 }
307 112
308void RSA_blinding_off(RSA *rsa) 113void RSA_blinding_off(RSA *rsa)
@@ -427,48 +232,3 @@ err:
427 232
428 return ret; 233 return ret;
429} 234}
430
431int RSA_memory_lock(RSA *r)
432 {
433 int i,j,k,off;
434 char *p;
435 BIGNUM *bn,**t[6],*b;
436 BN_ULONG *ul;
437
438 if (r->d == NULL) return(1);
439 t[0]= &r->d;
440 t[1]= &r->p;
441 t[2]= &r->q;
442 t[3]= &r->dmp1;
443 t[4]= &r->dmq1;
444 t[5]= &r->iqmp;
445 k=sizeof(BIGNUM)*6;
446 off=k/sizeof(BN_ULONG)+1;
447 j=1;
448 for (i=0; i<6; i++)
449 j+= (*t[i])->top;
450 if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
451 {
452 RSAerr(RSA_F_RSA_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
453 return(0);
454 }
455 bn=(BIGNUM *)p;
456 ul=(BN_ULONG *)&(p[off]);
457 for (i=0; i<6; i++)
458 {
459 b= *(t[i]);
460 *(t[i])= &(bn[i]);
461 memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
462 bn[i].flags=BN_FLG_STATIC_DATA;
463 bn[i].d=ul;
464 memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
465 ul+=b->top;
466 BN_clear_free(b);
467 }
468
469 /* I should fix this so it can still be done */
470 r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
471
472 r->bignum_data=p;
473 return(1);
474 }
diff --git a/src/lib/libcrypto/rsa/rsa_oaep.c b/src/lib/libcrypto/rsa/rsa_oaep.c
index 3652677a99..4d30c9d2d3 100644
--- a/src/lib/libcrypto/rsa/rsa_oaep.c
+++ b/src/lib/libcrypto/rsa/rsa_oaep.c
@@ -187,7 +187,7 @@ int PKCS1_MGF1(unsigned char *mask, long len,
187 int mdlen; 187 int mdlen;
188 188
189 EVP_MD_CTX_init(&c); 189 EVP_MD_CTX_init(&c);
190 mdlen = EVP_MD_size(dgst); 190 mdlen = M_EVP_MD_size(dgst);
191 for (i = 0; outlen < len; i++) 191 for (i = 0; outlen < len; i++)
192 { 192 {
193 cnt[0] = (unsigned char)((i >> 24) & 255); 193 cnt[0] = (unsigned char)((i >> 24) & 255);
diff --git a/src/lib/libcrypto/rsa/rsa_pss.c b/src/lib/libcrypto/rsa/rsa_pss.c
index e19d18c5b9..9b993aca49 100644
--- a/src/lib/libcrypto/rsa/rsa_pss.c
+++ b/src/lib/libcrypto/rsa/rsa_pss.c
@@ -1,5 +1,5 @@
1/* rsa_pss.c */ 1/* rsa_pss.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2005. 3 * project 2005.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -81,7 +81,7 @@ int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
81 EVP_MD_CTX ctx; 81 EVP_MD_CTX ctx;
82 unsigned char H_[EVP_MAX_MD_SIZE]; 82 unsigned char H_[EVP_MAX_MD_SIZE];
83 83
84 hLen = EVP_MD_size(Hash); 84 hLen = M_EVP_MD_size(Hash);
85 /* 85 /*
86 * Negative sLen has special meanings: 86 * Negative sLen has special meanings:
87 * -1 sLen == hLen 87 * -1 sLen == hLen
@@ -176,7 +176,7 @@ int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
176 unsigned char *H, *salt = NULL, *p; 176 unsigned char *H, *salt = NULL, *p;
177 EVP_MD_CTX ctx; 177 EVP_MD_CTX ctx;
178 178
179 hLen = EVP_MD_size(Hash); 179 hLen = M_EVP_MD_size(Hash);
180 /* 180 /*
181 * Negative sLen has special meanings: 181 * Negative sLen has special meanings:
182 * -1 sLen == hLen 182 * -1 sLen == hLen
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c
index 71aabeea1b..5488c06f6d 100644
--- a/src/lib/libcrypto/rsa/rsa_sign.c
+++ b/src/lib/libcrypto/rsa/rsa_sign.c
@@ -90,6 +90,14 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
90 i = SSL_SIG_LENGTH; 90 i = SSL_SIG_LENGTH;
91 s = m; 91 s = m;
92 } else { 92 } else {
93 /* NB: in FIPS mode block anything that isn't a TLS signature */
94#ifdef OPENSSL_FIPS
95 if(FIPS_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW))
96 {
97 RSAerr(RSA_F_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
98 return 0;
99 }
100#endif
93 sig.algor= &algor; 101 sig.algor= &algor;
94 sig.algor->algorithm=OBJ_nid2obj(type); 102 sig.algor->algorithm=OBJ_nid2obj(type);
95 if (sig.algor->algorithm == NULL) 103 if (sig.algor->algorithm == NULL)
@@ -167,10 +175,22 @@ int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
167 RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE); 175 RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE);
168 goto err; 176 goto err;
169 } 177 }
170 if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) { 178 if(dtype == NID_md5_sha1)
179 {
180 if (m_len != SSL_SIG_LENGTH)
181 {
171 RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH); 182 RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH);
172 goto err; 183 goto err;
173 } 184 }
185 }
186 /* NB: in FIPS mode block anything that isn't a TLS signature */
187#ifdef OPENSSL_FIPS
188 else if(FIPS_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW))
189 {
190 RSAerr(RSA_F_RSA_VERIFY, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
191 return 0;
192 }
193#endif
174 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); 194 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
175 195
176 if (i <= 0) goto err; 196 if (i <= 0) goto err;
diff --git a/src/lib/libcrypto/rsa/rsa_ssl.c b/src/lib/libcrypto/rsa/rsa_ssl.c
index ea72629494..cfeff15bc9 100644
--- a/src/lib/libcrypto/rsa/rsa_ssl.c
+++ b/src/lib/libcrypto/rsa/rsa_ssl.c
@@ -130,7 +130,7 @@ int RSA_padding_check_SSLv23(unsigned char *to, int tlen,
130 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_NULL_BEFORE_BLOCK_MISSING); 130 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_NULL_BEFORE_BLOCK_MISSING);
131 return(-1); 131 return(-1);
132 } 132 }
133 for (k= -8; k<0; k++) 133 for (k = -9; k<-1; k++)
134 { 134 {
135 if (p[k] != 0x03) break; 135 if (p[k] != 0x03) break;
136 } 136 }
diff --git a/src/lib/libcrypto/rsa/rsa_x931.c b/src/lib/libcrypto/rsa/rsa_x931.c
index e918654176..21548e37ed 100644
--- a/src/lib/libcrypto/rsa/rsa_x931.c
+++ b/src/lib/libcrypto/rsa/rsa_x931.c
@@ -1,5 +1,5 @@
1/* rsa_x931.c */ 1/* rsa_x931.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2005. 3 * project 2005.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/sha/asm/sha1-586.pl b/src/lib/libcrypto/sha/asm/sha1-586.pl
index 0b4dab2bd5..a787dd37da 100644
--- a/src/lib/libcrypto/sha/asm/sha1-586.pl
+++ b/src/lib/libcrypto/sha/asm/sha1-586.pl
@@ -149,7 +149,7 @@ sub BODY_40_59
149 &add($f,$e); # f+=ROTATE(a,5) 149 &add($f,$e); # f+=ROTATE(a,5)
150 } 150 }
151 151
152&function_begin("sha1_block_data_order",16); 152&function_begin("sha1_block_data_order");
153 &mov($tmp1,&wparam(0)); # SHA_CTX *c 153 &mov($tmp1,&wparam(0)); # SHA_CTX *c
154 &mov($T,&wparam(1)); # const void *input 154 &mov($T,&wparam(1)); # const void *input
155 &mov($A,&wparam(2)); # size_t num 155 &mov($A,&wparam(2)); # size_t num
diff --git a/src/lib/libcrypto/sha/sha.h b/src/lib/libcrypto/sha/sha.h
index eed44d7f94..47a2c29f66 100644
--- a/src/lib/libcrypto/sha/sha.h
+++ b/src/lib/libcrypto/sha/sha.h
@@ -106,6 +106,9 @@ typedef struct SHAstate_st
106 } SHA_CTX; 106 } SHA_CTX;
107 107
108#ifndef OPENSSL_NO_SHA0 108#ifndef OPENSSL_NO_SHA0
109#ifdef OPENSSL_FIPS
110int private_SHA_Init(SHA_CTX *c);
111#endif
109int SHA_Init(SHA_CTX *c); 112int SHA_Init(SHA_CTX *c);
110int SHA_Update(SHA_CTX *c, const void *data, size_t len); 113int SHA_Update(SHA_CTX *c, const void *data, size_t len);
111int SHA_Final(unsigned char *md, SHA_CTX *c); 114int SHA_Final(unsigned char *md, SHA_CTX *c);
diff --git a/src/lib/libcrypto/sha/sha1_one.c b/src/lib/libcrypto/sha/sha1_one.c
index 7c65b60276..4831174198 100644
--- a/src/lib/libcrypto/sha/sha1_one.c
+++ b/src/lib/libcrypto/sha/sha1_one.c
@@ -61,7 +61,7 @@
61#include <openssl/sha.h> 61#include <openssl/sha.h>
62#include <openssl/crypto.h> 62#include <openssl/crypto.h>
63 63
64#ifndef OPENSSL_NO_SHA1 64#if !defined(OPENSSL_NO_SHA1)
65unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md) 65unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md)
66 { 66 {
67 SHA_CTX c; 67 SHA_CTX c;
diff --git a/src/lib/libcrypto/sha/sha1dgst.c b/src/lib/libcrypto/sha/sha1dgst.c
index 50d1925cde..d31f0781a0 100644
--- a/src/lib/libcrypto/sha/sha1dgst.c
+++ b/src/lib/libcrypto/sha/sha1dgst.c
@@ -63,6 +63,10 @@
63#define SHA_1 63#define SHA_1
64 64
65#include <openssl/opensslv.h> 65#include <openssl/opensslv.h>
66#ifdef OPENSSL_FIPS
67#include <openssl/fips.h>
68#endif
69
66 70
67const char SHA1_version[]="SHA1" OPENSSL_VERSION_PTEXT; 71const char SHA1_version[]="SHA1" OPENSSL_VERSION_PTEXT;
68 72
diff --git a/src/lib/libcrypto/sha/sha256.c b/src/lib/libcrypto/sha/sha256.c
index 867f90cc97..3256a83e98 100644
--- a/src/lib/libcrypto/sha/sha256.c
+++ b/src/lib/libcrypto/sha/sha256.c
@@ -12,12 +12,19 @@
12 12
13#include <openssl/crypto.h> 13#include <openssl/crypto.h>
14#include <openssl/sha.h> 14#include <openssl/sha.h>
15#ifdef OPENSSL_FIPS
16#include <openssl/fips.h>
17#endif
18
15#include <openssl/opensslv.h> 19#include <openssl/opensslv.h>
16 20
17const char SHA256_version[]="SHA-256" OPENSSL_VERSION_PTEXT; 21const char SHA256_version[]="SHA-256" OPENSSL_VERSION_PTEXT;
18 22
19int SHA224_Init (SHA256_CTX *c) 23int SHA224_Init (SHA256_CTX *c)
20 { 24 {
25#ifdef OPENSSL_FIPS
26 FIPS_selftest_check();
27#endif
21 c->h[0]=0xc1059ed8UL; c->h[1]=0x367cd507UL; 28 c->h[0]=0xc1059ed8UL; c->h[1]=0x367cd507UL;
22 c->h[2]=0x3070dd17UL; c->h[3]=0xf70e5939UL; 29 c->h[2]=0x3070dd17UL; c->h[3]=0xf70e5939UL;
23 c->h[4]=0xffc00b31UL; c->h[5]=0x68581511UL; 30 c->h[4]=0xffc00b31UL; c->h[5]=0x68581511UL;
@@ -29,6 +36,9 @@ int SHA224_Init (SHA256_CTX *c)
29 36
30int SHA256_Init (SHA256_CTX *c) 37int SHA256_Init (SHA256_CTX *c)
31 { 38 {
39#ifdef OPENSSL_FIPS
40 FIPS_selftest_check();
41#endif
32 c->h[0]=0x6a09e667UL; c->h[1]=0xbb67ae85UL; 42 c->h[0]=0x6a09e667UL; c->h[1]=0xbb67ae85UL;
33 c->h[2]=0x3c6ef372UL; c->h[3]=0xa54ff53aUL; 43 c->h[2]=0x3c6ef372UL; c->h[3]=0xa54ff53aUL;
34 c->h[4]=0x510e527fUL; c->h[5]=0x9b05688cUL; 44 c->h[4]=0x510e527fUL; c->h[5]=0x9b05688cUL;
diff --git a/src/lib/libcrypto/sha/sha512.c b/src/lib/libcrypto/sha/sha512.c
index 987fc07c99..f5ed468b85 100644
--- a/src/lib/libcrypto/sha/sha512.c
+++ b/src/lib/libcrypto/sha/sha512.c
@@ -5,6 +5,10 @@
5 * ==================================================================== 5 * ====================================================================
6 */ 6 */
7#include <openssl/opensslconf.h> 7#include <openssl/opensslconf.h>
8#ifdef OPENSSL_FIPS
9#include <openssl/fips.h>
10#endif
11
8#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA512) 12#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA512)
9/* 13/*
10 * IMPLEMENTATION NOTES. 14 * IMPLEMENTATION NOTES.
@@ -61,6 +65,9 @@ const char SHA512_version[]="SHA-512" OPENSSL_VERSION_PTEXT;
61 65
62int SHA384_Init (SHA512_CTX *c) 66int SHA384_Init (SHA512_CTX *c)
63 { 67 {
68#ifdef OPENSSL_FIPS
69 FIPS_selftest_check();
70#endif
64 c->h[0]=U64(0xcbbb9d5dc1059ed8); 71 c->h[0]=U64(0xcbbb9d5dc1059ed8);
65 c->h[1]=U64(0x629a292a367cd507); 72 c->h[1]=U64(0x629a292a367cd507);
66 c->h[2]=U64(0x9159015a3070dd17); 73 c->h[2]=U64(0x9159015a3070dd17);
@@ -76,6 +83,9 @@ int SHA384_Init (SHA512_CTX *c)
76 83
77int SHA512_Init (SHA512_CTX *c) 84int SHA512_Init (SHA512_CTX *c)
78 { 85 {
86#ifdef OPENSSL_FIPS
87 FIPS_selftest_check();
88#endif
79 c->h[0]=U64(0x6a09e667f3bcc908); 89 c->h[0]=U64(0x6a09e667f3bcc908);
80 c->h[1]=U64(0xbb67ae8584caa73b); 90 c->h[1]=U64(0xbb67ae8584caa73b);
81 c->h[2]=U64(0x3c6ef372fe94f82b); 91 c->h[2]=U64(0x3c6ef372fe94f82b);
@@ -327,7 +337,7 @@ static const SHA_LONG64 K512[80] = {
327 ((SHA_LONG64)hi)<<32|lo; }) 337 ((SHA_LONG64)hi)<<32|lo; })
328# else 338# else
329# define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\ 339# define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\
330 unsigned int hi=p[0],lo=p[1]; \ 340 unsigned int hi=p[0],lo=p[1]; \
331 asm ("bswapl %0; bswapl %1;" \ 341 asm ("bswapl %0; bswapl %1;" \
332 : "=r"(lo),"=r"(hi) \ 342 : "=r"(lo),"=r"(hi) \
333 : "0"(lo),"1"(hi)); \ 343 : "0"(lo),"1"(hi)); \
diff --git a/src/lib/libcrypto/sha/sha_locl.h b/src/lib/libcrypto/sha/sha_locl.h
index e37e5726e3..da46ddfe79 100644
--- a/src/lib/libcrypto/sha/sha_locl.h
+++ b/src/lib/libcrypto/sha/sha_locl.h
@@ -122,8 +122,15 @@ void sha1_block_data_order (SHA_CTX *c, const void *p,size_t num);
122#define INIT_DATA_h3 0x10325476UL 122#define INIT_DATA_h3 0x10325476UL
123#define INIT_DATA_h4 0xc3d2e1f0UL 123#define INIT_DATA_h4 0xc3d2e1f0UL
124 124
125#if defined(SHA_0) && defined(OPENSSL_FIPS)
126FIPS_NON_FIPS_MD_Init(SHA)
127#else
125int HASH_INIT (SHA_CTX *c) 128int HASH_INIT (SHA_CTX *c)
129#endif
126 { 130 {
131#if defined(SHA_1) && defined(OPENSSL_FIPS)
132 FIPS_selftest_check();
133#endif
127 c->h0=INIT_DATA_h0; 134 c->h0=INIT_DATA_h0;
128 c->h1=INIT_DATA_h1; 135 c->h1=INIT_DATA_h1;
129 c->h2=INIT_DATA_h2; 136 c->h2=INIT_DATA_h2;
diff --git a/src/lib/libcrypto/stack/safestack.h b/src/lib/libcrypto/stack/safestack.h
index 78cc485e6d..40b17902e0 100644
--- a/src/lib/libcrypto/stack/safestack.h
+++ b/src/lib/libcrypto/stack/safestack.h
@@ -986,6 +986,50 @@ STACK_OF(type) \
986#define sk_MIME_HEADER_sort(st) SKM_sk_sort(MIME_HEADER, (st)) 986#define sk_MIME_HEADER_sort(st) SKM_sk_sort(MIME_HEADER, (st))
987#define sk_MIME_HEADER_is_sorted(st) SKM_sk_is_sorted(MIME_HEADER, (st)) 987#define sk_MIME_HEADER_is_sorted(st) SKM_sk_is_sorted(MIME_HEADER, (st))
988 988
989#define sk_MIME_HEADER_new(st) SKM_sk_new(MIME_HEADER, (st))
990#define sk_MIME_HEADER_new_null() SKM_sk_new_null(MIME_HEADER)
991#define sk_MIME_HEADER_free(st) SKM_sk_free(MIME_HEADER, (st))
992#define sk_MIME_HEADER_num(st) SKM_sk_num(MIME_HEADER, (st))
993#define sk_MIME_HEADER_value(st, i) SKM_sk_value(MIME_HEADER, (st), (i))
994#define sk_MIME_HEADER_set(st, i, val) SKM_sk_set(MIME_HEADER, (st), (i), (val))
995#define sk_MIME_HEADER_zero(st) SKM_sk_zero(MIME_HEADER, (st))
996#define sk_MIME_HEADER_push(st, val) SKM_sk_push(MIME_HEADER, (st), (val))
997#define sk_MIME_HEADER_unshift(st, val) SKM_sk_unshift(MIME_HEADER, (st), (val))
998#define sk_MIME_HEADER_find(st, val) SKM_sk_find(MIME_HEADER, (st), (val))
999#define sk_MIME_HEADER_find_ex(st, val) SKM_sk_find_ex(MIME_HEADER, (st), (val))
1000#define sk_MIME_HEADER_delete(st, i) SKM_sk_delete(MIME_HEADER, (st), (i))
1001#define sk_MIME_HEADER_delete_ptr(st, ptr) SKM_sk_delete_ptr(MIME_HEADER, (st), (ptr))
1002#define sk_MIME_HEADER_insert(st, val, i) SKM_sk_insert(MIME_HEADER, (st), (val), (i))
1003#define sk_MIME_HEADER_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(MIME_HEADER, (st), (cmp))
1004#define sk_MIME_HEADER_dup(st) SKM_sk_dup(MIME_HEADER, st)
1005#define sk_MIME_HEADER_pop_free(st, free_func) SKM_sk_pop_free(MIME_HEADER, (st), (free_func))
1006#define sk_MIME_HEADER_shift(st) SKM_sk_shift(MIME_HEADER, (st))
1007#define sk_MIME_HEADER_pop(st) SKM_sk_pop(MIME_HEADER, (st))
1008#define sk_MIME_HEADER_sort(st) SKM_sk_sort(MIME_HEADER, (st))
1009#define sk_MIME_HEADER_is_sorted(st) SKM_sk_is_sorted(MIME_HEADER, (st))
1010
1011#define sk_MIME_PARAM_new(st) SKM_sk_new(MIME_PARAM, (st))
1012#define sk_MIME_PARAM_new_null() SKM_sk_new_null(MIME_PARAM)
1013#define sk_MIME_PARAM_free(st) SKM_sk_free(MIME_PARAM, (st))
1014#define sk_MIME_PARAM_num(st) SKM_sk_num(MIME_PARAM, (st))
1015#define sk_MIME_PARAM_value(st, i) SKM_sk_value(MIME_PARAM, (st), (i))
1016#define sk_MIME_PARAM_set(st, i, val) SKM_sk_set(MIME_PARAM, (st), (i), (val))
1017#define sk_MIME_PARAM_zero(st) SKM_sk_zero(MIME_PARAM, (st))
1018#define sk_MIME_PARAM_push(st, val) SKM_sk_push(MIME_PARAM, (st), (val))
1019#define sk_MIME_PARAM_unshift(st, val) SKM_sk_unshift(MIME_PARAM, (st), (val))
1020#define sk_MIME_PARAM_find(st, val) SKM_sk_find(MIME_PARAM, (st), (val))
1021#define sk_MIME_PARAM_find_ex(st, val) SKM_sk_find_ex(MIME_PARAM, (st), (val))
1022#define sk_MIME_PARAM_delete(st, i) SKM_sk_delete(MIME_PARAM, (st), (i))
1023#define sk_MIME_PARAM_delete_ptr(st, ptr) SKM_sk_delete_ptr(MIME_PARAM, (st), (ptr))
1024#define sk_MIME_PARAM_insert(st, val, i) SKM_sk_insert(MIME_PARAM, (st), (val), (i))
1025#define sk_MIME_PARAM_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(MIME_PARAM, (st), (cmp))
1026#define sk_MIME_PARAM_dup(st) SKM_sk_dup(MIME_PARAM, st)
1027#define sk_MIME_PARAM_pop_free(st, free_func) SKM_sk_pop_free(MIME_PARAM, (st), (free_func))
1028#define sk_MIME_PARAM_shift(st) SKM_sk_shift(MIME_PARAM, (st))
1029#define sk_MIME_PARAM_pop(st) SKM_sk_pop(MIME_PARAM, (st))
1030#define sk_MIME_PARAM_sort(st) SKM_sk_sort(MIME_PARAM, (st))
1031#define sk_MIME_PARAM_is_sorted(st) SKM_sk_is_sorted(MIME_PARAM, (st))
1032
989#define sk_MIME_PARAM_new(st) SKM_sk_new(MIME_PARAM, (st)) 1033#define sk_MIME_PARAM_new(st) SKM_sk_new(MIME_PARAM, (st))
990#define sk_MIME_PARAM_new_null() SKM_sk_new_null(MIME_PARAM) 1034#define sk_MIME_PARAM_new_null() SKM_sk_new_null(MIME_PARAM)
991#define sk_MIME_PARAM_free(st) SKM_sk_free(MIME_PARAM, (st)) 1035#define sk_MIME_PARAM_free(st) SKM_sk_free(MIME_PARAM, (st))
diff --git a/src/lib/libcrypto/ui/ui_openssl.c b/src/lib/libcrypto/ui/ui_openssl.c
index 1f23a45a33..ef930bf247 100644
--- a/src/lib/libcrypto/ui/ui_openssl.c
+++ b/src/lib/libcrypto/ui/ui_openssl.c
@@ -677,6 +677,8 @@ static int noecho_fgets(char *buf, int size, FILE *tty)
677 size--; 677 size--;
678#ifdef WIN16TTY 678#ifdef WIN16TTY
679 i=_inchar(); 679 i=_inchar();
680#elif defined(_WIN32)
681 i=_getch();
680#else 682#else
681 i=getch(); 683 i=getch();
682#endif 684#endif
diff --git a/src/lib/libcrypto/util/mkerr.pl b/src/lib/libcrypto/util/mkerr.pl
index 53e14ab4df..554bebb159 100644
--- a/src/lib/libcrypto/util/mkerr.pl
+++ b/src/lib/libcrypto/util/mkerr.pl
@@ -44,7 +44,8 @@ while (@ARGV) {
44} 44}
45 45
46if($recurse) { 46if($recurse) {
47 @source = (<crypto/*.c>, <crypto/*/*.c>, <ssl/*.c>); 47 @source = ( <crypto/*.c>, <crypto/*/*.c>, <ssl/*.c>,
48 <fips/*.c>, <fips/*/*.c>);
48} else { 49} else {
49 @source = @ARGV; 50 @source = @ARGV;
50} 51}
diff --git a/src/lib/libcrypto/x509/by_dir.c b/src/lib/libcrypto/x509/by_dir.c
index 37f9a48206..341e0ba6a4 100644
--- a/src/lib/libcrypto/x509/by_dir.c
+++ b/src/lib/libcrypto/x509/by_dir.c
@@ -74,6 +74,10 @@
74#include <openssl/lhash.h> 74#include <openssl/lhash.h>
75#include <openssl/x509.h> 75#include <openssl/x509.h>
76 76
77#ifdef _WIN32
78#define stat _stat
79#endif
80
77typedef struct lookup_dir_st 81typedef struct lookup_dir_st
78 { 82 {
79 BUF_MEM *buffer; 83 BUF_MEM *buffer;
diff --git a/src/lib/libcrypto/x509/x509_att.c b/src/lib/libcrypto/x509/x509_att.c
index 511b49d589..98460e8921 100644
--- a/src/lib/libcrypto/x509/x509_att.c
+++ b/src/lib/libcrypto/x509/x509_att.c
@@ -245,7 +245,7 @@ X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
245 goto err; 245 goto err;
246 if (!X509_ATTRIBUTE_set1_data(ret,atrtype,data,len)) 246 if (!X509_ATTRIBUTE_set1_data(ret,atrtype,data,len))
247 goto err; 247 goto err;
248 248
249 if ((attr != NULL) && (*attr == NULL)) *attr=ret; 249 if ((attr != NULL) && (*attr == NULL)) *attr=ret;
250 return(ret); 250 return(ret);
251err: 251err:
@@ -302,8 +302,15 @@ int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *dat
302 atype = attrtype; 302 atype = attrtype;
303 } 303 }
304 if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err; 304 if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err;
305 attr->single = 0;
306 /* This is a bit naughty because the attribute should really have
307 * at least one value but some types use and zero length SET and
308 * require this.
309 */
310 if (attrtype == 0)
311 return 1;
305 if(!(ttmp = ASN1_TYPE_new())) goto err; 312 if(!(ttmp = ASN1_TYPE_new())) goto err;
306 if (len == -1) 313 if ((len == -1) && !(attrtype & MBSTRING_FLAG))
307 { 314 {
308 if (!ASN1_TYPE_set1(ttmp, attrtype, data)) 315 if (!ASN1_TYPE_set1(ttmp, attrtype, data))
309 goto err; 316 goto err;
@@ -311,7 +318,6 @@ int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *dat
311 else 318 else
312 ASN1_TYPE_set(ttmp, atype, stmp); 319 ASN1_TYPE_set(ttmp, atype, stmp);
313 if(!sk_ASN1_TYPE_push(attr->value.set, ttmp)) goto err; 320 if(!sk_ASN1_TYPE_push(attr->value.set, ttmp)) goto err;
314 attr->single = 0;
315 return 1; 321 return 1;
316 err: 322 err:
317 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE); 323 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
diff --git a/src/lib/libcrypto/x509/x509_cmp.c b/src/lib/libcrypto/x509/x509_cmp.c
index 0d6bc653b2..e4c682fc44 100644
--- a/src/lib/libcrypto/x509/x509_cmp.c
+++ b/src/lib/libcrypto/x509/x509_cmp.c
@@ -322,10 +322,16 @@ unsigned long X509_NAME_hash(X509_NAME *x)
322 { 322 {
323 unsigned long ret=0; 323 unsigned long ret=0;
324 unsigned char md[16]; 324 unsigned char md[16];
325 EVP_MD_CTX md_ctx;
325 326
326 /* Make sure X509_NAME structure contains valid cached encoding */ 327 /* Make sure X509_NAME structure contains valid cached encoding */
327 i2d_X509_NAME(x,NULL); 328 i2d_X509_NAME(x,NULL);
328 EVP_Digest(x->bytes->data, x->bytes->length, md, NULL, EVP_md5(), NULL); 329 EVP_MD_CTX_init(&md_ctx);
330 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
331 EVP_DigestInit_ex(&md_ctx, EVP_md5(), NULL);
332 EVP_DigestUpdate(&md_ctx, x->bytes->data, x->bytes->length);
333 EVP_DigestFinal_ex(&md_ctx,md,NULL);
334 EVP_MD_CTX_cleanup(&md_ctx);
329 335
330 ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| 336 ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)|
331 ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) 337 ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
diff --git a/src/lib/libcrypto/x509/x509_trs.c b/src/lib/libcrypto/x509/x509_trs.c
index 9c84a59d52..ed18700585 100644
--- a/src/lib/libcrypto/x509/x509_trs.c
+++ b/src/lib/libcrypto/x509/x509_trs.c
@@ -1,5 +1,5 @@
1/* x509_trs.c */ 1/* x509_trs.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c
index 9a62ebcf67..336c40ddd7 100644
--- a/src/lib/libcrypto/x509/x509_vfy.c
+++ b/src/lib/libcrypto/x509/x509_vfy.c
@@ -394,7 +394,7 @@ static int check_chain_extensions(X509_STORE_CTX *ctx)
394#ifdef OPENSSL_NO_CHAIN_VERIFY 394#ifdef OPENSSL_NO_CHAIN_VERIFY
395 return 1; 395 return 1;
396#else 396#else
397 int i, ok=0, must_be_ca; 397 int i, ok=0, must_be_ca, plen = 0;
398 X509 *x; 398 X509 *x;
399 int (*cb)(int xok,X509_STORE_CTX *xctx); 399 int (*cb)(int xok,X509_STORE_CTX *xctx);
400 int proxy_path_length = 0; 400 int proxy_path_length = 0;
@@ -495,9 +495,10 @@ static int check_chain_extensions(X509_STORE_CTX *ctx)
495 if (!ok) goto end; 495 if (!ok) goto end;
496 } 496 }
497 } 497 }
498 /* Check pathlen */ 498 /* Check pathlen if not self issued */
499 if ((i > 1) && (x->ex_pathlen != -1) 499 if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
500 && (i > (x->ex_pathlen + proxy_path_length + 1))) 500 && (x->ex_pathlen != -1)
501 && (plen > (x->ex_pathlen + proxy_path_length + 1)))
501 { 502 {
502 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; 503 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
503 ctx->error_depth = i; 504 ctx->error_depth = i;
@@ -505,6 +506,9 @@ static int check_chain_extensions(X509_STORE_CTX *ctx)
505 ok=cb(0,ctx); 506 ok=cb(0,ctx);
506 if (!ok) goto end; 507 if (!ok) goto end;
507 } 508 }
509 /* Increment path length if not self issued */
510 if (!(x->ex_flags & EXFLAG_SI))
511 plen++;
508 /* If this certificate is a proxy certificate, the next 512 /* If this certificate is a proxy certificate, the next
509 certificate must be another proxy certificate or a EE 513 certificate must be another proxy certificate or a EE
510 certificate. If not, the next certificate must be a 514 certificate. If not, the next certificate must be a
diff --git a/src/lib/libcrypto/x509/x509_vpm.c b/src/lib/libcrypto/x509/x509_vpm.c
index e9db6d62a7..c92e65936f 100644
--- a/src/lib/libcrypto/x509/x509_vpm.c
+++ b/src/lib/libcrypto/x509/x509_vpm.c
@@ -1,5 +1,5 @@
1/* x509_vpm.c */ 1/* x509_vpm.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004. 3 * project 2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509/x509cset.c b/src/lib/libcrypto/x509/x509cset.c
index 9d1646d5c8..7f4004b291 100644
--- a/src/lib/libcrypto/x509/x509cset.c
+++ b/src/lib/libcrypto/x509/x509cset.c
@@ -1,5 +1,5 @@
1/* crypto/x509/x509cset.c */ 1/* crypto/x509/x509cset.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509/x509spki.c b/src/lib/libcrypto/x509/x509spki.c
index ed868b838e..02a203d72c 100644
--- a/src/lib/libcrypto/x509/x509spki.c
+++ b/src/lib/libcrypto/x509/x509spki.c
@@ -1,5 +1,5 @@
1/* x509spki.c */ 1/* x509spki.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/ext_dat.h b/src/lib/libcrypto/x509v3/ext_dat.h
index 5c063ac65d..3eaec46f8a 100644
--- a/src/lib/libcrypto/x509v3/ext_dat.h
+++ b/src/lib/libcrypto/x509v3/ext_dat.h
@@ -1,5 +1,5 @@
1/* ext_dat.h */ 1/* ext_dat.h */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/pcy_cache.c b/src/lib/libcrypto/x509v3/pcy_cache.c
index c18beb89f5..1030931b71 100644
--- a/src/lib/libcrypto/x509v3/pcy_cache.c
+++ b/src/lib/libcrypto/x509v3/pcy_cache.c
@@ -1,5 +1,5 @@
1/* pcy_cache.c */ 1/* pcy_cache.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004. 3 * project 2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/pcy_data.c b/src/lib/libcrypto/x509v3/pcy_data.c
index 614d2b4935..fb392b901f 100644
--- a/src/lib/libcrypto/x509v3/pcy_data.c
+++ b/src/lib/libcrypto/x509v3/pcy_data.c
@@ -1,5 +1,5 @@
1/* pcy_data.c */ 1/* pcy_data.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004. 3 * project 2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -87,6 +87,12 @@ X509_POLICY_DATA *policy_data_new(POLICYINFO *policy, ASN1_OBJECT *id, int crit)
87 X509_POLICY_DATA *ret; 87 X509_POLICY_DATA *ret;
88 if (!policy && !id) 88 if (!policy && !id)
89 return NULL; 89 return NULL;
90 if (id)
91 {
92 id = OBJ_dup(id);
93 if (!id)
94 return NULL;
95 }
90 ret = OPENSSL_malloc(sizeof(X509_POLICY_DATA)); 96 ret = OPENSSL_malloc(sizeof(X509_POLICY_DATA));
91 if (!ret) 97 if (!ret)
92 return NULL; 98 return NULL;
@@ -94,6 +100,8 @@ X509_POLICY_DATA *policy_data_new(POLICYINFO *policy, ASN1_OBJECT *id, int crit)
94 if (!ret->expected_policy_set) 100 if (!ret->expected_policy_set)
95 { 101 {
96 OPENSSL_free(ret); 102 OPENSSL_free(ret);
103 if (id)
104 ASN1_OBJECT_free(id);
97 return NULL; 105 return NULL;
98 } 106 }
99 107
diff --git a/src/lib/libcrypto/x509v3/pcy_int.h b/src/lib/libcrypto/x509v3/pcy_int.h
index ba62a209da..3780de4fcd 100644
--- a/src/lib/libcrypto/x509v3/pcy_int.h
+++ b/src/lib/libcrypto/x509v3/pcy_int.h
@@ -1,5 +1,5 @@
1/* pcy_int.h */ 1/* pcy_int.h */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004. 3 * project 2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/pcy_lib.c b/src/lib/libcrypto/x509v3/pcy_lib.c
index dae4840bc5..93bfd92703 100644
--- a/src/lib/libcrypto/x509v3/pcy_lib.c
+++ b/src/lib/libcrypto/x509v3/pcy_lib.c
@@ -1,5 +1,5 @@
1/* pcy_lib.c */ 1/* pcy_lib.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004. 3 * project 2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/pcy_map.c b/src/lib/libcrypto/x509v3/pcy_map.c
index 35221e8ba8..f28796e6d4 100644
--- a/src/lib/libcrypto/x509v3/pcy_map.c
+++ b/src/lib/libcrypto/x509v3/pcy_map.c
@@ -1,5 +1,5 @@
1/* pcy_map.c */ 1/* pcy_map.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004. 3 * project 2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/pcy_node.c b/src/lib/libcrypto/x509v3/pcy_node.c
index dcc1554e29..6587cb05ab 100644
--- a/src/lib/libcrypto/x509v3/pcy_node.c
+++ b/src/lib/libcrypto/x509v3/pcy_node.c
@@ -1,5 +1,5 @@
1/* pcy_node.c */ 1/* pcy_node.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004. 3 * project 2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/pcy_tree.c b/src/lib/libcrypto/x509v3/pcy_tree.c
index 4fda1d419a..6c87a7f506 100644
--- a/src/lib/libcrypto/x509v3/pcy_tree.c
+++ b/src/lib/libcrypto/x509v3/pcy_tree.c
@@ -1,5 +1,5 @@
1/* pcy_tree.c */ 1/* pcy_tree.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2004. 3 * project 2004.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -130,9 +130,9 @@ static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
130 ret = 2; 130 ret = 2;
131 if (explicit_policy > 0) 131 if (explicit_policy > 0)
132 { 132 {
133 explicit_policy--; 133 if (!(x->ex_flags & EXFLAG_SI))
134 if (!(x->ex_flags & EXFLAG_SS) 134 explicit_policy--;
135 && (cache->explicit_skip != -1) 135 if ((cache->explicit_skip != -1)
136 && (cache->explicit_skip < explicit_policy)) 136 && (cache->explicit_skip < explicit_policy))
137 explicit_policy = cache->explicit_skip; 137 explicit_policy = cache->explicit_skip;
138 } 138 }
@@ -197,13 +197,14 @@ static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
197 /* Any matching allowed if certificate is self 197 /* Any matching allowed if certificate is self
198 * issued and not the last in the chain. 198 * issued and not the last in the chain.
199 */ 199 */
200 if (!(x->ex_flags & EXFLAG_SS) || (i == 0)) 200 if (!(x->ex_flags & EXFLAG_SI) || (i == 0))
201 level->flags |= X509_V_FLAG_INHIBIT_ANY; 201 level->flags |= X509_V_FLAG_INHIBIT_ANY;
202 } 202 }
203 else 203 else
204 { 204 {
205 any_skip--; 205 if (!(x->ex_flags & EXFLAG_SI))
206 if ((cache->any_skip > 0) 206 any_skip--;
207 if ((cache->any_skip >= 0)
207 && (cache->any_skip < any_skip)) 208 && (cache->any_skip < any_skip))
208 any_skip = cache->any_skip; 209 any_skip = cache->any_skip;
209 } 210 }
@@ -213,7 +214,7 @@ static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
213 else 214 else
214 { 215 {
215 map_skip--; 216 map_skip--;
216 if ((cache->map_skip > 0) 217 if ((cache->map_skip >= 0)
217 && (cache->map_skip < map_skip)) 218 && (cache->map_skip < map_skip))
218 map_skip = cache->map_skip; 219 map_skip = cache->map_skip;
219 } 220 }
@@ -310,7 +311,8 @@ static int tree_link_any(X509_POLICY_LEVEL *curr,
310 311
311 if (data == NULL) 312 if (data == NULL)
312 return 0; 313 return 0;
313 data->qualifier_set = curr->anyPolicy->data->qualifier_set; 314 /* Curr may not have anyPolicy */
315 data->qualifier_set = cache->anyPolicy->qualifier_set;
314 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS; 316 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
315 if (!level_add_node(curr, data, node, tree)) 317 if (!level_add_node(curr, data, node, tree))
316 { 318 {
diff --git a/src/lib/libcrypto/x509v3/v3_akey.c b/src/lib/libcrypto/x509v3/v3_akey.c
index ac0548b775..c6b68ee221 100644
--- a/src/lib/libcrypto/x509v3/v3_akey.c
+++ b/src/lib/libcrypto/x509v3/v3_akey.c
@@ -1,5 +1,5 @@
1/* v3_akey.c */ 1/* v3_akey.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_akeya.c b/src/lib/libcrypto/x509v3/v3_akeya.c
index 2aafa26ba7..2c50f7360e 100644
--- a/src/lib/libcrypto/x509v3/v3_akeya.c
+++ b/src/lib/libcrypto/x509v3/v3_akeya.c
@@ -1,5 +1,5 @@
1/* v3_akey_asn1.c */ 1/* v3_akey_asn1.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_alt.c b/src/lib/libcrypto/x509v3/v3_alt.c
index bb2f5bc54e..58b2952478 100644
--- a/src/lib/libcrypto/x509v3/v3_alt.c
+++ b/src/lib/libcrypto/x509v3/v3_alt.c
@@ -1,5 +1,5 @@
1/* v3_alt.c */ 1/* v3_alt.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project. 3 * project.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -527,7 +527,8 @@ GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
527 return gen; 527 return gen;
528 528
529 err: 529 err:
530 GENERAL_NAME_free(gen); 530 if (!out)
531 GENERAL_NAME_free(gen);
531 return NULL; 532 return NULL;
532 } 533 }
533 534
diff --git a/src/lib/libcrypto/x509v3/v3_bcons.c b/src/lib/libcrypto/x509v3/v3_bcons.c
index 74b1233071..82aa488f75 100644
--- a/src/lib/libcrypto/x509v3/v3_bcons.c
+++ b/src/lib/libcrypto/x509v3/v3_bcons.c
@@ -1,5 +1,5 @@
1/* v3_bcons.c */ 1/* v3_bcons.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_bitst.c b/src/lib/libcrypto/x509v3/v3_bitst.c
index cf31f0816e..058d0d4dce 100644
--- a/src/lib/libcrypto/x509v3/v3_bitst.c
+++ b/src/lib/libcrypto/x509v3/v3_bitst.c
@@ -1,5 +1,5 @@
1/* v3_bitst.c */ 1/* v3_bitst.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_conf.c b/src/lib/libcrypto/x509v3/v3_conf.c
index 2b867305fb..11eb6b7fd5 100644
--- a/src/lib/libcrypto/x509v3/v3_conf.c
+++ b/src/lib/libcrypto/x509v3/v3_conf.c
@@ -1,5 +1,5 @@
1/* v3_conf.c */ 1/* v3_conf.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_cpols.c b/src/lib/libcrypto/x509v3/v3_cpols.c
index a40f490aa9..95596055ab 100644
--- a/src/lib/libcrypto/x509v3/v3_cpols.c
+++ b/src/lib/libcrypto/x509v3/v3_cpols.c
@@ -1,5 +1,5 @@
1/* v3_cpols.c */ 1/* v3_cpols.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_crld.c b/src/lib/libcrypto/x509v3/v3_crld.c
index c6e3ebae7b..181a8977b1 100644
--- a/src/lib/libcrypto/x509v3/v3_crld.c
+++ b/src/lib/libcrypto/x509v3/v3_crld.c
@@ -1,5 +1,5 @@
1/* v3_crld.c */ 1/* v3_crld.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_enum.c b/src/lib/libcrypto/x509v3/v3_enum.c
index a236cb22e1..36576eaa4d 100644
--- a/src/lib/libcrypto/x509v3/v3_enum.c
+++ b/src/lib/libcrypto/x509v3/v3_enum.c
@@ -1,5 +1,5 @@
1/* v3_enum.c */ 1/* v3_enum.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_extku.c b/src/lib/libcrypto/x509v3/v3_extku.c
index a4efe0031e..c0d14500ed 100644
--- a/src/lib/libcrypto/x509v3/v3_extku.c
+++ b/src/lib/libcrypto/x509v3/v3_extku.c
@@ -1,5 +1,5 @@
1/* v3_extku.c */ 1/* v3_extku.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_genn.c b/src/lib/libcrypto/x509v3/v3_genn.c
index 650b510980..84b4b1c881 100644
--- a/src/lib/libcrypto/x509v3/v3_genn.c
+++ b/src/lib/libcrypto/x509v3/v3_genn.c
@@ -1,5 +1,5 @@
1/* v3_genn.c */ 1/* v3_genn.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_ia5.c b/src/lib/libcrypto/x509v3/v3_ia5.c
index b739ccd036..4ff12b52b5 100644
--- a/src/lib/libcrypto/x509v3/v3_ia5.c
+++ b/src/lib/libcrypto/x509v3/v3_ia5.c
@@ -1,5 +1,5 @@
1/* v3_ia5.c */ 1/* v3_ia5.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_info.c b/src/lib/libcrypto/x509v3/v3_info.c
index e0ef69de42..e1b8699f92 100644
--- a/src/lib/libcrypto/x509v3/v3_info.c
+++ b/src/lib/libcrypto/x509v3/v3_info.c
@@ -1,5 +1,5 @@
1/* v3_info.c */ 1/* v3_info.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_int.c b/src/lib/libcrypto/x509v3/v3_int.c
index 9a48dc1508..4bfd14cf46 100644
--- a/src/lib/libcrypto/x509v3/v3_int.c
+++ b/src/lib/libcrypto/x509v3/v3_int.c
@@ -1,5 +1,5 @@
1/* v3_int.c */ 1/* v3_int.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_lib.c b/src/lib/libcrypto/x509v3/v3_lib.c
index f3015ea610..df3a48f43e 100644
--- a/src/lib/libcrypto/x509v3/v3_lib.c
+++ b/src/lib/libcrypto/x509v3/v3_lib.c
@@ -1,5 +1,5 @@
1/* v3_lib.c */ 1/* v3_lib.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_ncons.c b/src/lib/libcrypto/x509v3/v3_ncons.c
index 42e7f5a879..4e706be3e1 100644
--- a/src/lib/libcrypto/x509v3/v3_ncons.c
+++ b/src/lib/libcrypto/x509v3/v3_ncons.c
@@ -1,5 +1,5 @@
1/* v3_ncons.c */ 1/* v3_ncons.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project. 3 * project.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_ocsp.c b/src/lib/libcrypto/x509v3/v3_ocsp.c
index 62aac06335..e426ea930c 100644
--- a/src/lib/libcrypto/x509v3/v3_ocsp.c
+++ b/src/lib/libcrypto/x509v3/v3_ocsp.c
@@ -1,5 +1,5 @@
1/* v3_ocsp.c */ 1/* v3_ocsp.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_pcons.c b/src/lib/libcrypto/x509v3/v3_pcons.c
index 13248c2ada..86c0ff70e6 100644
--- a/src/lib/libcrypto/x509v3/v3_pcons.c
+++ b/src/lib/libcrypto/x509v3/v3_pcons.c
@@ -1,5 +1,5 @@
1/* v3_pcons.c */ 1/* v3_pcons.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project. 3 * project.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_pku.c b/src/lib/libcrypto/x509v3/v3_pku.c
index 5c4626e89b..076f3ff48e 100644
--- a/src/lib/libcrypto/x509v3/v3_pku.c
+++ b/src/lib/libcrypto/x509v3/v3_pku.c
@@ -1,5 +1,5 @@
1/* v3_pku.c */ 1/* v3_pku.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_pmaps.c b/src/lib/libcrypto/x509v3/v3_pmaps.c
index 626303264f..da03bbc35d 100644
--- a/src/lib/libcrypto/x509v3/v3_pmaps.c
+++ b/src/lib/libcrypto/x509v3/v3_pmaps.c
@@ -1,5 +1,5 @@
1/* v3_pmaps.c */ 1/* v3_pmaps.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project. 3 * project.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_prn.c b/src/lib/libcrypto/x509v3/v3_prn.c
index 20bd9bda19..c1bb17f105 100644
--- a/src/lib/libcrypto/x509v3/v3_prn.c
+++ b/src/lib/libcrypto/x509v3/v3_prn.c
@@ -1,5 +1,5 @@
1/* v3_prn.c */ 1/* v3_prn.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_purp.c b/src/lib/libcrypto/x509v3/v3_purp.c
index b2f5cdfa05..e18751e01c 100644
--- a/src/lib/libcrypto/x509v3/v3_purp.c
+++ b/src/lib/libcrypto/x509v3/v3_purp.c
@@ -1,5 +1,5 @@
1/* v3_purp.c */ 1/* v3_purp.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -291,7 +291,9 @@ int X509_supported_extension(X509_EXTENSION *ex)
291 NID_sbgp_ipAddrBlock, /* 290 */ 291 NID_sbgp_ipAddrBlock, /* 290 */
292 NID_sbgp_autonomousSysNum, /* 291 */ 292 NID_sbgp_autonomousSysNum, /* 291 */
293#endif 293#endif
294 NID_proxyCertInfo /* 661 */ 294 NID_policy_constraints, /* 401 */
295 NID_proxyCertInfo, /* 661 */
296 NID_inhibit_any_policy /* 748 */
295 }; 297 };
296 298
297 int ex_nid; 299 int ex_nid;
@@ -325,7 +327,7 @@ static void x509v3_cache_extensions(X509 *x)
325#endif 327#endif
326 /* Does subject name match issuer ? */ 328 /* Does subject name match issuer ? */
327 if(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) 329 if(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)))
328 x->ex_flags |= EXFLAG_SS; 330 x->ex_flags |= EXFLAG_SI;
329 /* V1 should mean no extensions ... */ 331 /* V1 should mean no extensions ... */
330 if(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1; 332 if(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1;
331 /* Handle basic constraints */ 333 /* Handle basic constraints */
diff --git a/src/lib/libcrypto/x509v3/v3_skey.c b/src/lib/libcrypto/x509v3/v3_skey.c
index da0a3558f6..202c9e4896 100644
--- a/src/lib/libcrypto/x509v3/v3_skey.c
+++ b/src/lib/libcrypto/x509v3/v3_skey.c
@@ -1,5 +1,5 @@
1/* v3_skey.c */ 1/* v3_skey.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_sxnet.c b/src/lib/libcrypto/x509v3/v3_sxnet.c
index eaea9ea01b..2a6bf11b65 100644
--- a/src/lib/libcrypto/x509v3/v3_sxnet.c
+++ b/src/lib/libcrypto/x509v3/v3_sxnet.c
@@ -1,5 +1,5 @@
1/* v3_sxnet.c */ 1/* v3_sxnet.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/x509v3/v3_utl.c b/src/lib/libcrypto/x509v3/v3_utl.c
index ac171ca940..a4236bbb6d 100644
--- a/src/lib/libcrypto/x509v3/v3_utl.c
+++ b/src/lib/libcrypto/x509v3/v3_utl.c
@@ -1,5 +1,5 @@
1/* v3_utl.c */ 1/* v3_utl.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project. 3 * project.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -736,17 +736,20 @@ static int ipv6_from_asc(unsigned char *v6, const char *in)
736 736
737 /* Format result */ 737 /* Format result */
738 738
739 /* Copy initial part */ 739 if (v6stat.zero_pos >= 0)
740 if (v6stat.zero_pos > 0) 740 {
741 /* Copy initial part */
741 memcpy(v6, v6stat.tmp, v6stat.zero_pos); 742 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
742 /* Zero middle */ 743 /* Zero middle */
743 if (v6stat.total != 16)
744 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); 744 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
745 /* Copy final part */ 745 /* Copy final part */
746 if (v6stat.total != v6stat.zero_pos) 746 if (v6stat.total != v6stat.zero_pos)
747 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, 747 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
748 v6stat.tmp + v6stat.zero_pos, 748 v6stat.tmp + v6stat.zero_pos,
749 v6stat.total - v6stat.zero_pos); 749 v6stat.total - v6stat.zero_pos);
750 }
751 else
752 memcpy(v6, v6stat.tmp, 16);
750 753
751 return 1; 754 return 1;
752 } 755 }
diff --git a/src/lib/libcrypto/x509v3/x509v3.h b/src/lib/libcrypto/x509v3/x509v3.h
index db2b0482c1..9ef83da755 100644
--- a/src/lib/libcrypto/x509v3/x509v3.h
+++ b/src/lib/libcrypto/x509v3/x509v3.h
@@ -1,5 +1,5 @@
1/* x509v3.h */ 1/* x509v3.h */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
@@ -363,6 +363,8 @@ DECLARE_ASN1_FUNCTIONS(PROXY_CERT_INFO_EXTENSION)
363#define EXFLAG_NSCERT 0x8 363#define EXFLAG_NSCERT 0x8
364 364
365#define EXFLAG_CA 0x10 365#define EXFLAG_CA 0x10
366/* Really self issued not necessarily self signed */
367#define EXFLAG_SI 0x20
366#define EXFLAG_SS 0x20 368#define EXFLAG_SS 0x20
367#define EXFLAG_V1 0x40 369#define EXFLAG_V1 0x40
368#define EXFLAG_INVALID 0x80 370#define EXFLAG_INVALID 0x80
@@ -370,7 +372,7 @@ DECLARE_ASN1_FUNCTIONS(PROXY_CERT_INFO_EXTENSION)
370#define EXFLAG_CRITICAL 0x200 372#define EXFLAG_CRITICAL 0x200
371#define EXFLAG_PROXY 0x400 373#define EXFLAG_PROXY 0x400
372 374
373#define EXFLAG_INVALID_POLICY 0x400 375#define EXFLAG_INVALID_POLICY 0x800
374 376
375#define KU_DIGITAL_SIGNATURE 0x0080 377#define KU_DIGITAL_SIGNATURE 0x0080
376#define KU_NON_REPUDIATION 0x0040 378#define KU_NON_REPUDIATION 0x0040
diff --git a/src/lib/libssl/d1_clnt.c b/src/lib/libssl/d1_clnt.c
index 5e59dc845a..49c6760d19 100644
--- a/src/lib/libssl/d1_clnt.c
+++ b/src/lib/libssl/d1_clnt.c
@@ -1095,8 +1095,7 @@ int dtls1_send_client_certificate(SSL *s)
1095 * ssl->rwstate=SSL_X509_LOOKUP; return(-1); 1095 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1096 * We then get retied later */ 1096 * We then get retied later */
1097 i=0; 1097 i=0;
1098 if (s->ctx->client_cert_cb != NULL) 1098 i = ssl_do_client_cert_cb(s, &x509, &pkey);
1099 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1100 if (i < 0) 1099 if (i < 0)
1101 { 1100 {
1102 s->rwstate=SSL_X509_LOOKUP; 1101 s->rwstate=SSL_X509_LOOKUP;
diff --git a/src/lib/libssl/d1_enc.c b/src/lib/libssl/d1_enc.c
index cbff7495c5..cf3332e4e4 100644
--- a/src/lib/libssl/d1_enc.c
+++ b/src/lib/libssl/d1_enc.c
@@ -115,12 +115,16 @@
115 115
116#include <stdio.h> 116#include <stdio.h>
117#include "ssl_locl.h" 117#include "ssl_locl.h"
118#ifndef OPENSSL_NO_COMP
118#include <openssl/comp.h> 119#include <openssl/comp.h>
120#endif
119#include <openssl/evp.h> 121#include <openssl/evp.h>
120#include <openssl/hmac.h> 122#include <openssl/hmac.h>
121#include <openssl/md5.h> 123#include <openssl/md5.h>
122#include <openssl/rand.h> 124#include <openssl/rand.h>
123 125#ifdef KSSL_DEBUG
126#include <openssl/des.h>
127#endif
124 128
125int dtls1_enc(SSL *s, int send) 129int dtls1_enc(SSL *s, int send)
126 { 130 {
@@ -202,10 +206,11 @@ int dtls1_enc(SSL *s, int send)
202 { 206 {
203 unsigned long ui; 207 unsigned long ui;
204 printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", 208 printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
205 ds,rec->data,rec->input,l); 209 (void *)ds,rec->data,rec->input,l);
206 printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n", 210 printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%ld %ld], %d iv_len\n",
207 ds->buf_len, ds->cipher->key_len, 211 ds->buf_len, ds->cipher->key_len,
208 DES_KEY_SZ, DES_SCHEDULE_SZ, 212 (unsigned long)DES_KEY_SZ,
213 (unsigned long)DES_SCHEDULE_SZ,
209 ds->cipher->iv_len); 214 ds->cipher->iv_len);
210 printf("\t\tIV: "); 215 printf("\t\tIV: ");
211 for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]); 216 for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]);
@@ -230,10 +235,10 @@ int dtls1_enc(SSL *s, int send)
230 235
231#ifdef KSSL_DEBUG 236#ifdef KSSL_DEBUG
232 { 237 {
233 unsigned long i; 238 unsigned long ki;
234 printf("\trec->data="); 239 printf("\trec->data=");
235 for (i=0; i<l; i++) 240 for (ki=0; ki<l; ki++)
236 printf(" %02x", rec->data[i]); printf("\n"); 241 printf(" %02x", rec->data[ki]); printf("\n");
237 } 242 }
238#endif /* KSSL_DEBUG */ 243#endif /* KSSL_DEBUG */
239 244
diff --git a/src/lib/libssl/d1_lib.c b/src/lib/libssl/d1_lib.c
index fc088b4148..3568e97a87 100644
--- a/src/lib/libssl/d1_lib.c
+++ b/src/lib/libssl/d1_lib.c
@@ -106,6 +106,7 @@ int dtls1_new(SSL *s)
106 pq_64bit_init(&(d1->bitmap.map)); 106 pq_64bit_init(&(d1->bitmap.map));
107 pq_64bit_init(&(d1->bitmap.max_seq_num)); 107 pq_64bit_init(&(d1->bitmap.max_seq_num));
108 108
109 d1->next_bitmap.length = d1->bitmap.length;
109 pq_64bit_init(&(d1->next_bitmap.map)); 110 pq_64bit_init(&(d1->next_bitmap.map));
110 pq_64bit_init(&(d1->next_bitmap.max_seq_num)); 111 pq_64bit_init(&(d1->next_bitmap.max_seq_num));
111 112
diff --git a/src/lib/libssl/d1_pkt.c b/src/lib/libssl/d1_pkt.c
index 377696deac..eb56cf987b 100644
--- a/src/lib/libssl/d1_pkt.c
+++ b/src/lib/libssl/d1_pkt.c
@@ -597,6 +597,7 @@ again:
597 /* check whether this is a repeat, or aged record */ 597 /* check whether this is a repeat, or aged record */
598 if ( ! dtls1_record_replay_check(s, bitmap, &(rr->seq_num))) 598 if ( ! dtls1_record_replay_check(s, bitmap, &(rr->seq_num)))
599 { 599 {
600 rr->length = 0;
600 s->packet_length=0; /* dump this record */ 601 s->packet_length=0; /* dump this record */
601 goto again; /* get another record */ 602 goto again; /* get another record */
602 } 603 }
@@ -811,6 +812,14 @@ start:
811 * may be fragmented--don't always expect dest_maxlen bytes */ 812 * may be fragmented--don't always expect dest_maxlen bytes */
812 if ( rr->length < dest_maxlen) 813 if ( rr->length < dest_maxlen)
813 { 814 {
815#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
816 /*
817 * for normal alerts rr->length is 2, while
818 * dest_maxlen is 7 if we were to handle this
819 * non-existing alert...
820 */
821 FIX ME
822#endif
814 s->rstate=SSL_ST_READ_HEADER; 823 s->rstate=SSL_ST_READ_HEADER;
815 rr->length = 0; 824 rr->length = 0;
816 goto start; 825 goto start;
@@ -1251,7 +1260,7 @@ int dtls1_write_bytes(SSL *s, int type, const void *buf_, int len)
1251 else 1260 else
1252 s->s3->wnum += i; 1261 s->s3->wnum += i;
1253 1262
1254 return tot + i; 1263 return i;
1255 } 1264 }
1256 1265
1257int do_dtls1_write(SSL *s, int type, const unsigned char *buf, unsigned int len, int create_empty_fragment) 1266int do_dtls1_write(SSL *s, int type, const unsigned char *buf, unsigned int len, int create_empty_fragment)
@@ -1576,7 +1585,7 @@ int dtls1_dispatch_alert(SSL *s)
1576 { 1585 {
1577 int i,j; 1586 int i,j;
1578 void (*cb)(const SSL *ssl,int type,int val)=NULL; 1587 void (*cb)(const SSL *ssl,int type,int val)=NULL;
1579 unsigned char buf[2 + 2 + 3]; /* alert level + alert desc + message seq +frag_off */ 1588 unsigned char buf[DTLS1_AL_HEADER_LENGTH];
1580 unsigned char *ptr = &buf[0]; 1589 unsigned char *ptr = &buf[0];
1581 1590
1582 s->s3->alert_dispatch=0; 1591 s->s3->alert_dispatch=0;
@@ -1585,6 +1594,7 @@ int dtls1_dispatch_alert(SSL *s)
1585 *ptr++ = s->s3->send_alert[0]; 1594 *ptr++ = s->s3->send_alert[0];
1586 *ptr++ = s->s3->send_alert[1]; 1595 *ptr++ = s->s3->send_alert[1];
1587 1596
1597#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1588 if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) 1598 if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE)
1589 { 1599 {
1590 s2n(s->d1->handshake_read_seq, ptr); 1600 s2n(s->d1->handshake_read_seq, ptr);
@@ -1600,6 +1610,7 @@ int dtls1_dispatch_alert(SSL *s)
1600#endif 1610#endif
1601 l2n3(s->d1->r_msg_hdr.frag_off, ptr); 1611 l2n3(s->d1->r_msg_hdr.frag_off, ptr);
1602 } 1612 }
1613#endif
1603 1614
1604 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0); 1615 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0);
1605 if (i <= 0) 1616 if (i <= 0)
@@ -1609,8 +1620,11 @@ int dtls1_dispatch_alert(SSL *s)
1609 } 1620 }
1610 else 1621 else
1611 { 1622 {
1612 if ( s->s3->send_alert[0] == SSL3_AL_FATAL || 1623 if (s->s3->send_alert[0] == SSL3_AL_FATAL
1613 s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) 1624#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1625 || s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1626#endif
1627 )
1614 (void)BIO_flush(s->wbio); 1628 (void)BIO_flush(s->wbio);
1615 1629
1616 if (s->msg_callback) 1630 if (s->msg_callback)
diff --git a/src/lib/libssl/d1_srvr.c b/src/lib/libssl/d1_srvr.c
index 927b01f3c4..0bbf8ae7f3 100644
--- a/src/lib/libssl/d1_srvr.c
+++ b/src/lib/libssl/d1_srvr.c
@@ -732,7 +732,7 @@ int dtls1_send_server_hello(SSL *s)
732 732
733 d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l); 733 d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
734 734
735 s->state=SSL3_ST_CW_CLNT_HELLO_B; 735 s->state=SSL3_ST_SW_SRVR_HELLO_B;
736 /* number of bytes to write */ 736 /* number of bytes to write */
737 s->init_num=p-buf; 737 s->init_num=p-buf;
738 s->init_off=0; 738 s->init_off=0;
@@ -741,7 +741,7 @@ int dtls1_send_server_hello(SSL *s)
741 dtls1_buffer_message(s, 0); 741 dtls1_buffer_message(s, 0);
742 } 742 }
743 743
744 /* SSL3_ST_CW_CLNT_HELLO_B */ 744 /* SSL3_ST_SW_SRVR_HELLO_B */
745 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 745 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
746 } 746 }
747 747
@@ -765,7 +765,7 @@ int dtls1_send_server_done(SSL *s)
765 dtls1_buffer_message(s, 0); 765 dtls1_buffer_message(s, 0);
766 } 766 }
767 767
768 /* SSL3_ST_CW_CLNT_HELLO_B */ 768 /* SSL3_ST_SW_SRVR_DONE_B */
769 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 769 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
770 } 770 }
771 771
diff --git a/src/lib/libssl/dtls1.h b/src/lib/libssl/dtls1.h
index a663cf85f2..f159d37110 100644
--- a/src/lib/libssl/dtls1.h
+++ b/src/lib/libssl/dtls1.h
@@ -70,7 +70,10 @@ extern "C" {
70#define DTLS1_VERSION 0xFEFF 70#define DTLS1_VERSION 0xFEFF
71#define DTLS1_BAD_VER 0x0100 71#define DTLS1_BAD_VER 0x0100
72 72
73#if 0
74/* this alert description is not specified anywhere... */
73#define DTLS1_AD_MISSING_HANDSHAKE_MESSAGE 110 75#define DTLS1_AD_MISSING_HANDSHAKE_MESSAGE 110
76#endif
74 77
75/* lengths of messages */ 78/* lengths of messages */
76#define DTLS1_COOKIE_LENGTH 32 79#define DTLS1_COOKIE_LENGTH 32
@@ -84,7 +87,11 @@ extern "C" {
84 87
85#define DTLS1_CCS_HEADER_LENGTH 1 88#define DTLS1_CCS_HEADER_LENGTH 1
86 89
90#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
87#define DTLS1_AL_HEADER_LENGTH 7 91#define DTLS1_AL_HEADER_LENGTH 7
92#else
93#define DTLS1_AL_HEADER_LENGTH 2
94#endif
88 95
89 96
90typedef struct dtls1_bitmap_st 97typedef struct dtls1_bitmap_st
diff --git a/src/lib/libssl/s23_clnt.c b/src/lib/libssl/s23_clnt.c
index c45a8e0a04..bc918170e1 100644
--- a/src/lib/libssl/s23_clnt.c
+++ b/src/lib/libssl/s23_clnt.c
@@ -257,6 +257,14 @@ static int ssl23_client_hello(SSL *s)
257 version_major = TLS1_VERSION_MAJOR; 257 version_major = TLS1_VERSION_MAJOR;
258 version_minor = TLS1_VERSION_MINOR; 258 version_minor = TLS1_VERSION_MINOR;
259 } 259 }
260#ifdef OPENSSL_FIPS
261 else if(FIPS_mode())
262 {
263 SSLerr(SSL_F_SSL23_CLIENT_HELLO,
264 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
265 return -1;
266 }
267#endif
260 else if (version == SSL3_VERSION) 268 else if (version == SSL3_VERSION)
261 { 269 {
262 version_major = SSL3_VERSION_MAJOR; 270 version_major = SSL3_VERSION_MAJOR;
@@ -536,6 +544,14 @@ static int ssl23_get_server_hello(SSL *s)
536 if ((p[2] == SSL3_VERSION_MINOR) && 544 if ((p[2] == SSL3_VERSION_MINOR) &&
537 !(s->options & SSL_OP_NO_SSLv3)) 545 !(s->options & SSL_OP_NO_SSLv3))
538 { 546 {
547#ifdef OPENSSL_FIPS
548 if(FIPS_mode())
549 {
550 SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,
551 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
552 goto err;
553 }
554#endif
539 s->version=SSL3_VERSION; 555 s->version=SSL3_VERSION;
540 s->method=SSLv3_client_method(); 556 s->method=SSLv3_client_method();
541 } 557 }
diff --git a/src/lib/libssl/s23_srvr.c b/src/lib/libssl/s23_srvr.c
index 6637bb9549..ba06e7ae2e 100644
--- a/src/lib/libssl/s23_srvr.c
+++ b/src/lib/libssl/s23_srvr.c
@@ -386,6 +386,15 @@ int ssl23_get_client_hello(SSL *s)
386 } 386 }
387 } 387 }
388 388
389#ifdef OPENSSL_FIPS
390 if (FIPS_mode() && (s->version < TLS1_VERSION))
391 {
392 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,
393 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
394 goto err;
395 }
396#endif
397
389 if (s->state == SSL23_ST_SR_CLNT_HELLO_B) 398 if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
390 { 399 {
391 /* we have SSLv3/TLSv1 in an SSLv2 header 400 /* we have SSLv3/TLSv1 in an SSLv2 header
diff --git a/src/lib/libssl/s3_clnt.c b/src/lib/libssl/s3_clnt.c
index f6864cdc50..5fd3520caf 100644
--- a/src/lib/libssl/s3_clnt.c
+++ b/src/lib/libssl/s3_clnt.c
@@ -130,10 +130,17 @@
130#include <openssl/objects.h> 130#include <openssl/objects.h>
131#include <openssl/evp.h> 131#include <openssl/evp.h>
132#include <openssl/md5.h> 132#include <openssl/md5.h>
133#ifdef OPENSSL_FIPS
134#include <openssl/fips.h>
135#endif
136
133#ifndef OPENSSL_NO_DH 137#ifndef OPENSSL_NO_DH
134#include <openssl/dh.h> 138#include <openssl/dh.h>
135#endif 139#endif
136#include <openssl/bn.h> 140#include <openssl/bn.h>
141#ifndef OPENSSL_NO_ENGINE
142#include <openssl/engine.h>
143#endif
137 144
138static SSL_METHOD *ssl3_get_client_method(int ver); 145static SSL_METHOD *ssl3_get_client_method(int ver);
139static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b); 146static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
@@ -965,7 +972,7 @@ int ssl3_get_server_certificate(SSL *s)
965 } 972 }
966 973
967 i=ssl_verify_cert_chain(s,sk); 974 i=ssl_verify_cert_chain(s,sk);
968 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i) 975 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
969#ifndef OPENSSL_NO_KRB5 976#ifndef OPENSSL_NO_KRB5
970 && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK)) 977 && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
971 != (SSL_aKRB5|SSL_kKRB5) 978 != (SSL_aKRB5|SSL_kKRB5)
@@ -999,7 +1006,7 @@ int ssl3_get_server_certificate(SSL *s)
999 == (SSL_aKRB5|SSL_kKRB5))? 0: 1; 1006 == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
1000 1007
1001#ifdef KSSL_DEBUG 1008#ifdef KSSL_DEBUG
1002 printf("pkey,x = %p, %p\n", pkey,x); 1009 printf("pkey,x = %p, %p\n", (void *)pkey,(void *)x);
1003 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey)); 1010 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
1004 printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name, 1011 printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
1005 s->s3->tmp.new_cipher->algorithms, need_cert); 1012 s->s3->tmp.new_cipher->algorithms, need_cert);
@@ -1415,6 +1422,8 @@ int ssl3_get_key_exchange(SSL *s)
1415 q=md_buf; 1422 q=md_buf;
1416 for (num=2; num > 0; num--) 1423 for (num=2; num > 0; num--)
1417 { 1424 {
1425 EVP_MD_CTX_set_flags(&md_ctx,
1426 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1418 EVP_DigestInit_ex(&md_ctx,(num == 2) 1427 EVP_DigestInit_ex(&md_ctx,(num == 2)
1419 ?s->ctx->md5:s->ctx->sha1, NULL); 1428 ?s->ctx->md5:s->ctx->sha1, NULL);
1420 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1429 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
@@ -1450,7 +1459,7 @@ int ssl3_get_key_exchange(SSL *s)
1450 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1459 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1451 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1460 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1452 EVP_VerifyUpdate(&md_ctx,param,param_len); 1461 EVP_VerifyUpdate(&md_ctx,param,param_len);
1453 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey)) 1462 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1454 { 1463 {
1455 /* bad signature */ 1464 /* bad signature */
1456 al=SSL_AD_DECRYPT_ERROR; 1465 al=SSL_AD_DECRYPT_ERROR;
@@ -1468,7 +1477,7 @@ int ssl3_get_key_exchange(SSL *s)
1468 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1477 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1469 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1478 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1470 EVP_VerifyUpdate(&md_ctx,param,param_len); 1479 EVP_VerifyUpdate(&md_ctx,param,param_len);
1471 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey)) 1480 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1472 { 1481 {
1473 /* bad signature */ 1482 /* bad signature */
1474 al=SSL_AD_DECRYPT_ERROR; 1483 al=SSL_AD_DECRYPT_ERROR;
@@ -1768,7 +1777,7 @@ int ssl3_get_cert_status(SSL *s)
1768 goto f_err; 1777 goto f_err;
1769 } 1778 }
1770 n2l3(p, resplen); 1779 n2l3(p, resplen);
1771 if (resplen + 4 != n) 1780 if (resplen + 4 != (unsigned long)n)
1772 { 1781 {
1773 al = SSL_AD_DECODE_ERROR; 1782 al = SSL_AD_DECODE_ERROR;
1774 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH); 1783 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
@@ -2061,12 +2070,12 @@ int ssl3_send_client_key_exchange(SSL *s)
2061 { 2070 {
2062 DH *dh_srvr,*dh_clnt; 2071 DH *dh_srvr,*dh_clnt;
2063 2072
2064 if (s->session->sess_cert == NULL) 2073 if (s->session->sess_cert == NULL)
2065 { 2074 {
2066 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); 2075 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2067 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE); 2076 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
2068 goto err; 2077 goto err;
2069 } 2078 }
2070 2079
2071 if (s->session->sess_cert->peer_dh_tmp != NULL) 2080 if (s->session->sess_cert->peer_dh_tmp != NULL)
2072 dh_srvr=s->session->sess_cert->peer_dh_tmp; 2081 dh_srvr=s->session->sess_cert->peer_dh_tmp;
@@ -2448,8 +2457,7 @@ int ssl3_send_client_certificate(SSL *s)
2448 * ssl->rwstate=SSL_X509_LOOKUP; return(-1); 2457 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2449 * We then get retied later */ 2458 * We then get retied later */
2450 i=0; 2459 i=0;
2451 if (s->ctx->client_cert_cb != NULL) 2460 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2452 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
2453 if (i < 0) 2461 if (i < 0)
2454 { 2462 {
2455 s->rwstate=SSL_X509_LOOKUP; 2463 s->rwstate=SSL_X509_LOOKUP;
@@ -2716,3 +2724,21 @@ static int ssl3_check_finished(SSL *s)
2716 return 1; 2724 return 1;
2717 } 2725 }
2718#endif 2726#endif
2727
2728int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2729 {
2730 int i = 0;
2731#ifndef OPENSSL_NO_ENGINE
2732 if (s->ctx->client_cert_engine)
2733 {
2734 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2735 SSL_get_client_CA_list(s),
2736 px509, ppkey, NULL, NULL, NULL);
2737 if (i != 0)
2738 return i;
2739 }
2740#endif
2741 if (s->ctx->client_cert_cb)
2742 i = s->ctx->client_cert_cb(s,px509,ppkey);
2743 return i;
2744 }
diff --git a/src/lib/libssl/s3_lib.c b/src/lib/libssl/s3_lib.c
index bdbcd44f27..8916a0b1b3 100644
--- a/src/lib/libssl/s3_lib.c
+++ b/src/lib/libssl/s3_lib.c
@@ -158,7 +158,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
158 SSL3_TXT_RSA_NULL_SHA, 158 SSL3_TXT_RSA_NULL_SHA,
159 SSL3_CK_RSA_NULL_SHA, 159 SSL3_CK_RSA_NULL_SHA,
160 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_SSLV3, 160 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_SSLV3,
161 SSL_NOT_EXP|SSL_STRONG_NONE, 161 SSL_NOT_EXP|SSL_STRONG_NONE|SSL_FIPS,
162 0, 162 0,
163 0, 163 0,
164 0, 164 0,
@@ -264,7 +264,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
264 SSL3_TXT_RSA_DES_192_CBC3_SHA, 264 SSL3_TXT_RSA_DES_192_CBC3_SHA,
265 SSL3_CK_RSA_DES_192_CBC3_SHA, 265 SSL3_CK_RSA_DES_192_CBC3_SHA,
266 SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3, 266 SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
267 SSL_NOT_EXP|SSL_HIGH, 267 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
268 0, 268 0,
269 168, 269 168,
270 168, 270 168,
@@ -304,7 +304,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
304 SSL3_TXT_DH_DSS_DES_192_CBC3_SHA, 304 SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
305 SSL3_CK_DH_DSS_DES_192_CBC3_SHA, 305 SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
306 SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3, 306 SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
307 SSL_NOT_EXP|SSL_HIGH, 307 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
308 0, 308 0,
309 168, 309 168,
310 168, 310 168,
@@ -343,7 +343,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
343 SSL3_TXT_DH_RSA_DES_192_CBC3_SHA, 343 SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
344 SSL3_CK_DH_RSA_DES_192_CBC3_SHA, 344 SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
345 SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3, 345 SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
346 SSL_NOT_EXP|SSL_HIGH, 346 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
347 0, 347 0,
348 168, 348 168,
349 168, 349 168,
@@ -384,7 +384,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
384 SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA, 384 SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA,
385 SSL3_CK_EDH_DSS_DES_192_CBC3_SHA, 385 SSL3_CK_EDH_DSS_DES_192_CBC3_SHA,
386 SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_SSLV3, 386 SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_SSLV3,
387 SSL_NOT_EXP|SSL_HIGH, 387 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
388 0, 388 0,
389 168, 389 168,
390 168, 390 168,
@@ -423,7 +423,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
423 SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA, 423 SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
424 SSL3_CK_EDH_RSA_DES_192_CBC3_SHA, 424 SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
425 SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3, 425 SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
426 SSL_NOT_EXP|SSL_HIGH, 426 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
427 0, 427 0,
428 168, 428 168,
429 168, 429 168,
@@ -488,7 +488,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
488 SSL3_TXT_ADH_DES_192_CBC_SHA, 488 SSL3_TXT_ADH_DES_192_CBC_SHA,
489 SSL3_CK_ADH_DES_192_CBC_SHA, 489 SSL3_CK_ADH_DES_192_CBC_SHA,
490 SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_SSLV3, 490 SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_SSLV3,
491 SSL_NOT_EXP|SSL_HIGH, 491 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
492 0, 492 0,
493 168, 493 168,
494 168, 494 168,
@@ -563,7 +563,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
563 SSL3_TXT_KRB5_DES_192_CBC3_SHA, 563 SSL3_TXT_KRB5_DES_192_CBC3_SHA,
564 SSL3_CK_KRB5_DES_192_CBC3_SHA, 564 SSL3_CK_KRB5_DES_192_CBC3_SHA,
565 SSL_kKRB5|SSL_aKRB5| SSL_3DES|SSL_SHA1 |SSL_SSLV3, 565 SSL_kKRB5|SSL_aKRB5| SSL_3DES|SSL_SHA1 |SSL_SSLV3,
566 SSL_NOT_EXP|SSL_HIGH, 566 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
567 0, 567 0,
568 168, 568 168,
569 168, 569 168,
@@ -747,7 +747,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
747 TLS1_TXT_RSA_WITH_AES_128_SHA, 747 TLS1_TXT_RSA_WITH_AES_128_SHA,
748 TLS1_CK_RSA_WITH_AES_128_SHA, 748 TLS1_CK_RSA_WITH_AES_128_SHA,
749 SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA |SSL_TLSV1, 749 SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA |SSL_TLSV1,
750 SSL_NOT_EXP|SSL_HIGH, 750 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
751 0, 751 0,
752 128, 752 128,
753 128, 753 128,
@@ -760,7 +760,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
760 TLS1_TXT_DH_DSS_WITH_AES_128_SHA, 760 TLS1_TXT_DH_DSS_WITH_AES_128_SHA,
761 TLS1_CK_DH_DSS_WITH_AES_128_SHA, 761 TLS1_CK_DH_DSS_WITH_AES_128_SHA,
762 SSL_kDHd|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1, 762 SSL_kDHd|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1,
763 SSL_NOT_EXP|SSL_HIGH, 763 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
764 0, 764 0,
765 128, 765 128,
766 128, 766 128,
@@ -773,7 +773,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
773 TLS1_TXT_DH_RSA_WITH_AES_128_SHA, 773 TLS1_TXT_DH_RSA_WITH_AES_128_SHA,
774 TLS1_CK_DH_RSA_WITH_AES_128_SHA, 774 TLS1_CK_DH_RSA_WITH_AES_128_SHA,
775 SSL_kDHr|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1, 775 SSL_kDHr|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1,
776 SSL_NOT_EXP|SSL_HIGH, 776 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
777 0, 777 0,
778 128, 778 128,
779 128, 779 128,
@@ -786,7 +786,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
786 TLS1_TXT_DHE_DSS_WITH_AES_128_SHA, 786 TLS1_TXT_DHE_DSS_WITH_AES_128_SHA,
787 TLS1_CK_DHE_DSS_WITH_AES_128_SHA, 787 TLS1_CK_DHE_DSS_WITH_AES_128_SHA,
788 SSL_kEDH|SSL_aDSS|SSL_AES|SSL_SHA|SSL_TLSV1, 788 SSL_kEDH|SSL_aDSS|SSL_AES|SSL_SHA|SSL_TLSV1,
789 SSL_NOT_EXP|SSL_HIGH, 789 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
790 0, 790 0,
791 128, 791 128,
792 128, 792 128,
@@ -799,7 +799,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
799 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, 799 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA,
800 TLS1_CK_DHE_RSA_WITH_AES_128_SHA, 800 TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
801 SSL_kEDH|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1, 801 SSL_kEDH|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1,
802 SSL_NOT_EXP|SSL_HIGH, 802 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
803 0, 803 0,
804 128, 804 128,
805 128, 805 128,
@@ -812,7 +812,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
812 TLS1_TXT_ADH_WITH_AES_128_SHA, 812 TLS1_TXT_ADH_WITH_AES_128_SHA,
813 TLS1_CK_ADH_WITH_AES_128_SHA, 813 TLS1_CK_ADH_WITH_AES_128_SHA,
814 SSL_kEDH|SSL_aNULL|SSL_AES|SSL_SHA|SSL_TLSV1, 814 SSL_kEDH|SSL_aNULL|SSL_AES|SSL_SHA|SSL_TLSV1,
815 SSL_NOT_EXP|SSL_HIGH, 815 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
816 0, 816 0,
817 128, 817 128,
818 128, 818 128,
@@ -826,7 +826,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
826 TLS1_TXT_RSA_WITH_AES_256_SHA, 826 TLS1_TXT_RSA_WITH_AES_256_SHA,
827 TLS1_CK_RSA_WITH_AES_256_SHA, 827 TLS1_CK_RSA_WITH_AES_256_SHA,
828 SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA |SSL_TLSV1, 828 SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA |SSL_TLSV1,
829 SSL_NOT_EXP|SSL_HIGH, 829 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
830 0, 830 0,
831 256, 831 256,
832 256, 832 256,
@@ -839,7 +839,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
839 TLS1_TXT_DH_DSS_WITH_AES_256_SHA, 839 TLS1_TXT_DH_DSS_WITH_AES_256_SHA,
840 TLS1_CK_DH_DSS_WITH_AES_256_SHA, 840 TLS1_CK_DH_DSS_WITH_AES_256_SHA,
841 SSL_kDHd|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1, 841 SSL_kDHd|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1,
842 SSL_NOT_EXP|SSL_HIGH, 842 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
843 0, 843 0,
844 256, 844 256,
845 256, 845 256,
@@ -852,7 +852,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
852 TLS1_TXT_DH_RSA_WITH_AES_256_SHA, 852 TLS1_TXT_DH_RSA_WITH_AES_256_SHA,
853 TLS1_CK_DH_RSA_WITH_AES_256_SHA, 853 TLS1_CK_DH_RSA_WITH_AES_256_SHA,
854 SSL_kDHr|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1, 854 SSL_kDHr|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1,
855 SSL_NOT_EXP|SSL_HIGH, 855 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
856 0, 856 0,
857 256, 857 256,
858 256, 858 256,
@@ -865,7 +865,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
865 TLS1_TXT_DHE_DSS_WITH_AES_256_SHA, 865 TLS1_TXT_DHE_DSS_WITH_AES_256_SHA,
866 TLS1_CK_DHE_DSS_WITH_AES_256_SHA, 866 TLS1_CK_DHE_DSS_WITH_AES_256_SHA,
867 SSL_kEDH|SSL_aDSS|SSL_AES|SSL_SHA|SSL_TLSV1, 867 SSL_kEDH|SSL_aDSS|SSL_AES|SSL_SHA|SSL_TLSV1,
868 SSL_NOT_EXP|SSL_HIGH, 868 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
869 0, 869 0,
870 256, 870 256,
871 256, 871 256,
@@ -878,7 +878,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
878 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, 878 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA,
879 TLS1_CK_DHE_RSA_WITH_AES_256_SHA, 879 TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
880 SSL_kEDH|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1, 880 SSL_kEDH|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1,
881 SSL_NOT_EXP|SSL_HIGH, 881 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
882 0, 882 0,
883 256, 883 256,
884 256, 884 256,
@@ -891,7 +891,7 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
891 TLS1_TXT_ADH_WITH_AES_256_SHA, 891 TLS1_TXT_ADH_WITH_AES_256_SHA,
892 TLS1_CK_ADH_WITH_AES_256_SHA, 892 TLS1_CK_ADH_WITH_AES_256_SHA,
893 SSL_kEDH|SSL_aNULL|SSL_AES|SSL_SHA|SSL_TLSV1, 893 SSL_kEDH|SSL_aNULL|SSL_AES|SSL_SHA|SSL_TLSV1,
894 SSL_NOT_EXP|SSL_HIGH, 894 SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
895 0, 895 0,
896 256, 896 256,
897 256, 897 256,
diff --git a/src/lib/libssl/s3_pkt.c b/src/lib/libssl/s3_pkt.c
index 44c7c143fe..9476dcddf6 100644
--- a/src/lib/libssl/s3_pkt.c
+++ b/src/lib/libssl/s3_pkt.c
@@ -753,8 +753,15 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
753 s->rwstate=SSL_NOTHING; 753 s->rwstate=SSL_NOTHING;
754 return(s->s3->wpend_ret); 754 return(s->s3->wpend_ret);
755 } 755 }
756 else if (i <= 0) 756 else if (i <= 0) {
757 if (s->version == DTLS1_VERSION ||
758 s->version == DTLS1_BAD_VER) {
759 /* For DTLS, just drop it. That's kind of the whole
760 point in using a datagram service */
761 s->s3->wbuf.left = 0;
762 }
757 return(i); 763 return(i);
764 }
758 s->s3->wbuf.offset+=i; 765 s->s3->wbuf.offset+=i;
759 s->s3->wbuf.left-=i; 766 s->s3->wbuf.left-=i;
760 } 767 }
@@ -1225,6 +1232,13 @@ int ssl3_do_change_cipher_spec(SSL *s)
1225 1232
1226 if (s->s3->tmp.key_block == NULL) 1233 if (s->s3->tmp.key_block == NULL)
1227 { 1234 {
1235 if (s->session == NULL)
1236 {
1237 /* might happen if dtls1_read_bytes() calls this */
1238 SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC,SSL_R_CCS_RECEIVED_EARLY);
1239 return (0);
1240 }
1241
1228 s->session->cipher=s->s3->tmp.new_cipher; 1242 s->session->cipher=s->s3->tmp.new_cipher;
1229 if (!s->method->ssl3_enc->setup_key_block(s)) return(0); 1243 if (!s->method->ssl3_enc->setup_key_block(s)) return(0);
1230 } 1244 }
diff --git a/src/lib/libssl/s3_srvr.c b/src/lib/libssl/s3_srvr.c
index 903522ab59..80b45eb86f 100644
--- a/src/lib/libssl/s3_srvr.c
+++ b/src/lib/libssl/s3_srvr.c
@@ -902,22 +902,28 @@ int ssl3_get_client_hello(SSL *s)
902 break; 902 break;
903 } 903 }
904 } 904 }
905 if (j == 0) 905 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
906 { 906 {
907 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) 907 /* Special case as client bug workaround: the previously used cipher may
908 { 908 * not be in the current list, the client instead might be trying to
909 /* Very bad for multi-threading.... */ 909 * continue using a cipher that before wasn't chosen due to server
910 s->session->cipher=sk_SSL_CIPHER_value(ciphers, 0); 910 * preferences. We'll have to reject the connection if the cipher is not
911 } 911 * enabled, though. */
912 else 912 c = sk_SSL_CIPHER_value(ciphers, 0);
913 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0)
913 { 914 {
914 /* we need to have the cipher in the cipher 915 s->session->cipher = c;
915 * list if we are asked to reuse it */ 916 j = 1;
916 al=SSL_AD_ILLEGAL_PARAMETER;
917 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
918 goto f_err;
919 } 917 }
920 } 918 }
919 if (j == 0)
920 {
921 /* we need to have the cipher in the cipher
922 * list if we are asked to reuse it */
923 al=SSL_AD_ILLEGAL_PARAMETER;
924 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
925 goto f_err;
926 }
921 } 927 }
922 928
923 /* compression */ 929 /* compression */
@@ -1172,13 +1178,13 @@ int ssl3_send_server_hello(SSL *s)
1172 *(d++)=SSL3_MT_SERVER_HELLO; 1178 *(d++)=SSL3_MT_SERVER_HELLO;
1173 l2n3(l,d); 1179 l2n3(l,d);
1174 1180
1175 s->state=SSL3_ST_CW_CLNT_HELLO_B; 1181 s->state=SSL3_ST_SW_SRVR_HELLO_B;
1176 /* number of bytes to write */ 1182 /* number of bytes to write */
1177 s->init_num=p-buf; 1183 s->init_num=p-buf;
1178 s->init_off=0; 1184 s->init_off=0;
1179 } 1185 }
1180 1186
1181 /* SSL3_ST_CW_CLNT_HELLO_B */ 1187 /* SSL3_ST_SW_SRVR_HELLO_B */
1182 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1188 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1183 } 1189 }
1184 1190
@@ -1202,7 +1208,7 @@ int ssl3_send_server_done(SSL *s)
1202 s->init_off=0; 1208 s->init_off=0;
1203 } 1209 }
1204 1210
1205 /* SSL3_ST_CW_CLNT_HELLO_B */ 1211 /* SSL3_ST_SW_SRVR_DONE_B */
1206 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1212 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1207 } 1213 }
1208 1214
@@ -1540,6 +1546,8 @@ int ssl3_send_server_key_exchange(SSL *s)
1540 j=0; 1546 j=0;
1541 for (num=2; num > 0; num--) 1547 for (num=2; num > 0; num--)
1542 { 1548 {
1549 EVP_MD_CTX_set_flags(&md_ctx,
1550 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1543 EVP_DigestInit_ex(&md_ctx,(num == 2) 1551 EVP_DigestInit_ex(&md_ctx,(num == 2)
1544 ?s->ctx->md5:s->ctx->sha1, NULL); 1552 ?s->ctx->md5:s->ctx->sha1, NULL);
1545 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1553 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
@@ -2558,7 +2566,7 @@ int ssl3_get_client_certificate(SSL *s)
2558 else 2566 else
2559 { 2567 {
2560 i=ssl_verify_cert_chain(s,sk); 2568 i=ssl_verify_cert_chain(s,sk);
2561 if (!i) 2569 if (i <= 0)
2562 { 2570 {
2563 al=ssl_verify_alarm_type(s->verify_result); 2571 al=ssl_verify_alarm_type(s->verify_result);
2564 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED); 2572 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
diff --git a/src/lib/libssl/ssl.h b/src/lib/libssl/ssl.h
index 6df921f3c1..ff8a128d3c 100644
--- a/src/lib/libssl/ssl.h
+++ b/src/lib/libssl/ssl.h
@@ -252,6 +252,7 @@ extern "C" {
252#define SSL_TXT_LOW "LOW" 252#define SSL_TXT_LOW "LOW"
253#define SSL_TXT_MEDIUM "MEDIUM" 253#define SSL_TXT_MEDIUM "MEDIUM"
254#define SSL_TXT_HIGH "HIGH" 254#define SSL_TXT_HIGH "HIGH"
255#define SSL_TXT_FIPS "FIPS"
255#define SSL_TXT_kFZA "kFZA" 256#define SSL_TXT_kFZA "kFZA"
256#define SSL_TXT_aFZA "aFZA" 257#define SSL_TXT_aFZA "aFZA"
257#define SSL_TXT_eFZA "eFZA" 258#define SSL_TXT_eFZA "eFZA"
@@ -361,9 +362,6 @@ typedef struct ssl_cipher_st
361 362
362DECLARE_STACK_OF(SSL_CIPHER) 363DECLARE_STACK_OF(SSL_CIPHER)
363 364
364typedef struct ssl_st SSL;
365typedef struct ssl_ctx_st SSL_CTX;
366
367/* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */ 365/* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
368typedef struct ssl_method_st 366typedef struct ssl_method_st
369 { 367 {
@@ -760,6 +758,12 @@ struct ssl_ctx_st
760 758
761 int quiet_shutdown; 759 int quiet_shutdown;
762 760
761#ifndef OPENSSL_ENGINE
762 /* Engine to pass requests for client certs to
763 */
764 ENGINE *client_cert_engine;
765#endif
766
763#ifndef OPENSSL_NO_TLSEXT 767#ifndef OPENSSL_NO_TLSEXT
764 /* TLS extensions servername callback */ 768 /* TLS extensions servername callback */
765 int (*tlsext_servername_callback)(SSL*, int *, void *); 769 int (*tlsext_servername_callback)(SSL*, int *, void *);
@@ -829,6 +833,9 @@ void SSL_CTX_set_info_callback(SSL_CTX *ctx, void (*cb)(const SSL *ssl,int type,
829void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val); 833void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val);
830void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*client_cert_cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey)); 834void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*client_cert_cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey));
831int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509, EVP_PKEY **pkey); 835int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509, EVP_PKEY **pkey);
836#ifndef OPENSSL_NO_ENGINE
837int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e);
838#endif
832void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, int (*app_gen_cookie_cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len)); 839void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, int (*app_gen_cookie_cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len));
833void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, int (*app_verify_cookie_cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len)); 840void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, int (*app_verify_cookie_cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len));
834 841
@@ -1702,6 +1709,7 @@ void ERR_load_SSL_strings(void);
1702#define SSL_F_SSL3_CONNECT 132 1709#define SSL_F_SSL3_CONNECT 132
1703#define SSL_F_SSL3_CTRL 213 1710#define SSL_F_SSL3_CTRL 213
1704#define SSL_F_SSL3_CTX_CTRL 133 1711#define SSL_F_SSL3_CTX_CTRL 133
1712#define SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC 279
1705#define SSL_F_SSL3_ENC 134 1713#define SSL_F_SSL3_ENC 134
1706#define SSL_F_SSL3_GENERATE_KEY_BLOCK 238 1714#define SSL_F_SSL3_GENERATE_KEY_BLOCK 238
1707#define SSL_F_SSL3_GET_CERTIFICATE_REQUEST 135 1715#define SSL_F_SSL3_GET_CERTIFICATE_REQUEST 135
@@ -1755,6 +1763,7 @@ void ERR_load_SSL_strings(void);
1755#define SSL_F_SSL_CTX_CHECK_PRIVATE_KEY 168 1763#define SSL_F_SSL_CTX_CHECK_PRIVATE_KEY 168
1756#define SSL_F_SSL_CTX_NEW 169 1764#define SSL_F_SSL_CTX_NEW 169
1757#define SSL_F_SSL_CTX_SET_CIPHER_LIST 269 1765#define SSL_F_SSL_CTX_SET_CIPHER_LIST 269
1766#define SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE 278
1758#define SSL_F_SSL_CTX_SET_PURPOSE 226 1767#define SSL_F_SSL_CTX_SET_PURPOSE 226
1759#define SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT 219 1768#define SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT 219
1760#define SSL_F_SSL_CTX_SET_SSL_VERSION 170 1769#define SSL_F_SSL_CTX_SET_SSL_VERSION 170
@@ -1935,6 +1944,7 @@ void ERR_load_SSL_strings(void);
1935#define SSL_R_NO_CIPHERS_SPECIFIED 183 1944#define SSL_R_NO_CIPHERS_SPECIFIED 183
1936#define SSL_R_NO_CIPHER_LIST 184 1945#define SSL_R_NO_CIPHER_LIST 184
1937#define SSL_R_NO_CIPHER_MATCH 185 1946#define SSL_R_NO_CIPHER_MATCH 185
1947#define SSL_R_NO_CLIENT_CERT_METHOD 317
1938#define SSL_R_NO_CLIENT_CERT_RECEIVED 186 1948#define SSL_R_NO_CLIENT_CERT_RECEIVED 186
1939#define SSL_R_NO_COMPRESSION_SPECIFIED 187 1949#define SSL_R_NO_COMPRESSION_SPECIFIED 187
1940#define SSL_R_NO_METHOD_SPECIFIED 188 1950#define SSL_R_NO_METHOD_SPECIFIED 188
diff --git a/src/lib/libssl/ssl_asn1.c b/src/lib/libssl/ssl_asn1.c
index 6e14f4d834..0f9a3489dd 100644
--- a/src/lib/libssl/ssl_asn1.c
+++ b/src/lib/libssl/ssl_asn1.c
@@ -353,7 +353,7 @@ SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
353 memcpy(ret->session_id,os.data,os.length); 353 memcpy(ret->session_id,os.data,os.length);
354 354
355 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING); 355 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
356 if (ret->master_key_length > SSL_MAX_MASTER_KEY_LENGTH) 356 if (os.length > SSL_MAX_MASTER_KEY_LENGTH)
357 ret->master_key_length=SSL_MAX_MASTER_KEY_LENGTH; 357 ret->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
358 else 358 else
359 ret->master_key_length=os.length; 359 ret->master_key_length=os.length;
diff --git a/src/lib/libssl/ssl_ciph.c b/src/lib/libssl/ssl_ciph.c
index 725f7f3c1f..0c2aa249b4 100644
--- a/src/lib/libssl/ssl_ciph.c
+++ b/src/lib/libssl/ssl_ciph.c
@@ -115,7 +115,10 @@
115 */ 115 */
116#include <stdio.h> 116#include <stdio.h>
117#include <openssl/objects.h> 117#include <openssl/objects.h>
118#ifndef OPENSSL_NO_COMP
118#include <openssl/comp.h> 119#include <openssl/comp.h>
120#endif
121
119#include "ssl_locl.h" 122#include "ssl_locl.h"
120 123
121#define SSL_ENC_DES_IDX 0 124#define SSL_ENC_DES_IDX 0
@@ -222,6 +225,7 @@ static const SSL_CIPHER cipher_aliases[]={
222 {0,SSL_TXT_LOW, 0, 0, SSL_LOW, 0,0,0,0,SSL_STRONG_MASK}, 225 {0,SSL_TXT_LOW, 0, 0, SSL_LOW, 0,0,0,0,SSL_STRONG_MASK},
223 {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK}, 226 {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK},
224 {0,SSL_TXT_HIGH, 0, 0, SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK}, 227 {0,SSL_TXT_HIGH, 0, 0, SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK},
228 {0,SSL_TXT_FIPS, 0, 0, SSL_FIPS, 0,0,0,0,SSL_FIPS|SSL_STRONG_NONE},
225 }; 229 };
226 230
227void ssl_load_ciphers(void) 231void ssl_load_ciphers(void)
@@ -515,7 +519,12 @@ static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
515 c = ssl_method->get_cipher(i); 519 c = ssl_method->get_cipher(i);
516#define IS_MASKED(c) ((c)->algorithms & (((c)->alg_bits == 256) ? m256 : mask)) 520#define IS_MASKED(c) ((c)->algorithms & (((c)->alg_bits == 256) ? m256 : mask))
517 /* drop those that use any of that is not available */ 521 /* drop those that use any of that is not available */
522#ifdef OPENSSL_FIPS
523 if ((c != NULL) && c->valid && !IS_MASKED(c)
524 && (!FIPS_mode() || (c->algo_strength & SSL_FIPS)))
525#else
518 if ((c != NULL) && c->valid && !IS_MASKED(c)) 526 if ((c != NULL) && c->valid && !IS_MASKED(c))
527#endif
519 { 528 {
520 co_list[co_list_num].cipher = c; 529 co_list[co_list_num].cipher = c;
521 co_list[co_list_num].next = NULL; 530 co_list[co_list_num].next = NULL;
@@ -1054,7 +1063,11 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
1054 */ 1063 */
1055 for (curr = head; curr != NULL; curr = curr->next) 1064 for (curr = head; curr != NULL; curr = curr->next)
1056 { 1065 {
1066#ifdef OPENSSL_FIPS
1067 if (curr->active && (!FIPS_mode() || curr->cipher->algo_strength & SSL_FIPS))
1068#else
1057 if (curr->active) 1069 if (curr->active)
1070#endif
1058 { 1071 {
1059 sk_SSL_CIPHER_push(cipherstack, curr->cipher); 1072 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
1060#ifdef CIPHER_DEBUG 1073#ifdef CIPHER_DEBUG
diff --git a/src/lib/libssl/ssl_err.c b/src/lib/libssl/ssl_err.c
index 50779c1632..24a994fe01 100644
--- a/src/lib/libssl/ssl_err.c
+++ b/src/lib/libssl/ssl_err.c
@@ -1,6 +1,6 @@
1/* ssl/ssl_err.c */ 1/* ssl/ssl_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2008 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -138,6 +138,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
138{ERR_FUNC(SSL_F_SSL3_CONNECT), "SSL3_CONNECT"}, 138{ERR_FUNC(SSL_F_SSL3_CONNECT), "SSL3_CONNECT"},
139{ERR_FUNC(SSL_F_SSL3_CTRL), "SSL3_CTRL"}, 139{ERR_FUNC(SSL_F_SSL3_CTRL), "SSL3_CTRL"},
140{ERR_FUNC(SSL_F_SSL3_CTX_CTRL), "SSL3_CTX_CTRL"}, 140{ERR_FUNC(SSL_F_SSL3_CTX_CTRL), "SSL3_CTX_CTRL"},
141{ERR_FUNC(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC), "SSL3_DO_CHANGE_CIPHER_SPEC"},
141{ERR_FUNC(SSL_F_SSL3_ENC), "SSL3_ENC"}, 142{ERR_FUNC(SSL_F_SSL3_ENC), "SSL3_ENC"},
142{ERR_FUNC(SSL_F_SSL3_GENERATE_KEY_BLOCK), "SSL3_GENERATE_KEY_BLOCK"}, 143{ERR_FUNC(SSL_F_SSL3_GENERATE_KEY_BLOCK), "SSL3_GENERATE_KEY_BLOCK"},
143{ERR_FUNC(SSL_F_SSL3_GET_CERTIFICATE_REQUEST), "SSL3_GET_CERTIFICATE_REQUEST"}, 144{ERR_FUNC(SSL_F_SSL3_GET_CERTIFICATE_REQUEST), "SSL3_GET_CERTIFICATE_REQUEST"},
@@ -191,6 +192,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
191{ERR_FUNC(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY), "SSL_CTX_check_private_key"}, 192{ERR_FUNC(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY), "SSL_CTX_check_private_key"},
192{ERR_FUNC(SSL_F_SSL_CTX_NEW), "SSL_CTX_new"}, 193{ERR_FUNC(SSL_F_SSL_CTX_NEW), "SSL_CTX_new"},
193{ERR_FUNC(SSL_F_SSL_CTX_SET_CIPHER_LIST), "SSL_CTX_set_cipher_list"}, 194{ERR_FUNC(SSL_F_SSL_CTX_SET_CIPHER_LIST), "SSL_CTX_set_cipher_list"},
195{ERR_FUNC(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE), "SSL_CTX_set_client_cert_engine"},
194{ERR_FUNC(SSL_F_SSL_CTX_SET_PURPOSE), "SSL_CTX_set_purpose"}, 196{ERR_FUNC(SSL_F_SSL_CTX_SET_PURPOSE), "SSL_CTX_set_purpose"},
195{ERR_FUNC(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT), "SSL_CTX_set_session_id_context"}, 197{ERR_FUNC(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT), "SSL_CTX_set_session_id_context"},
196{ERR_FUNC(SSL_F_SSL_CTX_SET_SSL_VERSION), "SSL_CTX_set_ssl_version"}, 198{ERR_FUNC(SSL_F_SSL_CTX_SET_SSL_VERSION), "SSL_CTX_set_ssl_version"},
@@ -374,6 +376,7 @@ static ERR_STRING_DATA SSL_str_reasons[]=
374{ERR_REASON(SSL_R_NO_CIPHERS_SPECIFIED) ,"no ciphers specified"}, 376{ERR_REASON(SSL_R_NO_CIPHERS_SPECIFIED) ,"no ciphers specified"},
375{ERR_REASON(SSL_R_NO_CIPHER_LIST) ,"no cipher list"}, 377{ERR_REASON(SSL_R_NO_CIPHER_LIST) ,"no cipher list"},
376{ERR_REASON(SSL_R_NO_CIPHER_MATCH) ,"no cipher match"}, 378{ERR_REASON(SSL_R_NO_CIPHER_MATCH) ,"no cipher match"},
379{ERR_REASON(SSL_R_NO_CLIENT_CERT_METHOD) ,"no client cert method"},
377{ERR_REASON(SSL_R_NO_CLIENT_CERT_RECEIVED),"no client cert received"}, 380{ERR_REASON(SSL_R_NO_CLIENT_CERT_RECEIVED),"no client cert received"},
378{ERR_REASON(SSL_R_NO_COMPRESSION_SPECIFIED),"no compression specified"}, 381{ERR_REASON(SSL_R_NO_COMPRESSION_SPECIFIED),"no compression specified"},
379{ERR_REASON(SSL_R_NO_METHOD_SPECIFIED) ,"no method specified"}, 382{ERR_REASON(SSL_R_NO_METHOD_SPECIFIED) ,"no method specified"},
diff --git a/src/lib/libssl/ssl_lib.c b/src/lib/libssl/ssl_lib.c
index 065411aea8..68eee77e6f 100644
--- a/src/lib/libssl/ssl_lib.c
+++ b/src/lib/libssl/ssl_lib.c
@@ -130,6 +130,9 @@
130#ifndef OPENSSL_NO_DH 130#ifndef OPENSSL_NO_DH
131#include <openssl/dh.h> 131#include <openssl/dh.h>
132#endif 132#endif
133#ifndef OPENSSL_NO_ENGINE
134#include <openssl/engine.h>
135#endif
133 136
134const char *SSL_version_str=OPENSSL_VERSION_TEXT; 137const char *SSL_version_str=OPENSSL_VERSION_TEXT;
135 138
@@ -1393,6 +1396,14 @@ SSL_CTX *SSL_CTX_new(SSL_METHOD *meth)
1393 return(NULL); 1396 return(NULL);
1394 } 1397 }
1395 1398
1399#ifdef OPENSSL_FIPS
1400 if (FIPS_mode() && (meth->version < TLS1_VERSION))
1401 {
1402 SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1403 return NULL;
1404 }
1405#endif
1406
1396 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) 1407 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1397 { 1408 {
1398 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); 1409 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
@@ -1513,6 +1524,27 @@ SSL_CTX *SSL_CTX_new(SSL_METHOD *meth)
1513 1524
1514#endif 1525#endif
1515 1526
1527#ifndef OPENSSL_NO_ENGINE
1528 ret->client_cert_engine = NULL;
1529#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
1530#define eng_strx(x) #x
1531#define eng_str(x) eng_strx(x)
1532 /* Use specific client engine automatically... ignore errors */
1533 {
1534 ENGINE *eng;
1535 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
1536 if (!eng)
1537 {
1538 ERR_clear_error();
1539 ENGINE_load_builtin_engines();
1540 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
1541 }
1542 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
1543 ERR_clear_error();
1544 }
1545#endif
1546#endif
1547
1516 return(ret); 1548 return(ret);
1517err: 1549err:
1518 SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE); 1550 SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
@@ -1583,6 +1615,10 @@ void SSL_CTX_free(SSL_CTX *a)
1583#else 1615#else
1584 a->comp_methods = NULL; 1616 a->comp_methods = NULL;
1585#endif 1617#endif
1618#ifndef OPENSSL_NO_ENGINE
1619 if (a->client_cert_engine)
1620 ENGINE_finish(a->client_cert_engine);
1621#endif
1586 OPENSSL_free(a); 1622 OPENSSL_free(a);
1587 } 1623 }
1588 1624
diff --git a/src/lib/libssl/ssl_locl.h b/src/lib/libssl/ssl_locl.h
index de94c0d0c7..ed4ddbbae6 100644
--- a/src/lib/libssl/ssl_locl.h
+++ b/src/lib/libssl/ssl_locl.h
@@ -124,7 +124,9 @@
124#include "e_os.h" 124#include "e_os.h"
125 125
126#include <openssl/buffer.h> 126#include <openssl/buffer.h>
127#ifndef OPENSSL_NO_COMP
127#include <openssl/comp.h> 128#include <openssl/comp.h>
129#endif
128#include <openssl/bio.h> 130#include <openssl/bio.h>
129#include <openssl/stack.h> 131#include <openssl/stack.h>
130#ifndef OPENSSL_NO_RSA 132#ifndef OPENSSL_NO_RSA
@@ -330,8 +332,9 @@
330#define SSL_LOW 0x00000020L 332#define SSL_LOW 0x00000020L
331#define SSL_MEDIUM 0x00000040L 333#define SSL_MEDIUM 0x00000040L
332#define SSL_HIGH 0x00000080L 334#define SSL_HIGH 0x00000080L
335#define SSL_FIPS 0x00000100L
333 336
334/* we have used 000000ff - 24 bits left to go */ 337/* we have used 000001ff - 23 bits left to go */
335 338
336/* 339/*
337 * Macros to check the export status and cipher strength for export ciphers. 340 * Macros to check the export status and cipher strength for export ciphers.
@@ -499,6 +502,7 @@ typedef struct ssl3_enc_method
499 int (*alert_value)(int); 502 int (*alert_value)(int);
500 } SSL3_ENC_METHOD; 503 } SSL3_ENC_METHOD;
501 504
505#ifndef OPENSSL_NO_COMP
502/* Used for holding the relevant compression methods loaded into SSL_CTX */ 506/* Used for holding the relevant compression methods loaded into SSL_CTX */
503typedef struct ssl3_comp_st 507typedef struct ssl3_comp_st
504 { 508 {
@@ -506,6 +510,7 @@ typedef struct ssl3_comp_st
506 char *name; /* Text name used for the compression type */ 510 char *name; /* Text name used for the compression type */
507 COMP_METHOD *method; /* The method :-) */ 511 COMP_METHOD *method; /* The method :-) */
508 } SSL3_COMP; 512 } SSL3_COMP;
513#endif
509 514
510extern SSL3_ENC_METHOD ssl3_undef_enc_method; 515extern SSL3_ENC_METHOD ssl3_undef_enc_method;
511OPENSSL_EXTERN SSL_CIPHER ssl2_ciphers[]; 516OPENSSL_EXTERN SSL_CIPHER ssl2_ciphers[];
@@ -874,6 +879,7 @@ int ssl3_get_new_session_ticket(SSL *s);
874int ssl3_get_cert_status(SSL *s); 879int ssl3_get_cert_status(SSL *s);
875int ssl3_get_server_done(SSL *s); 880int ssl3_get_server_done(SSL *s);
876int ssl3_send_client_verify(SSL *s); 881int ssl3_send_client_verify(SSL *s);
882int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
877int ssl3_send_client_certificate(SSL *s); 883int ssl3_send_client_certificate(SSL *s);
878int ssl3_send_client_key_exchange(SSL *s); 884int ssl3_send_client_key_exchange(SSL *s);
879int ssl3_get_key_exchange(SSL *s); 885int ssl3_get_key_exchange(SSL *s);
diff --git a/src/lib/libssl/ssl_sess.c b/src/lib/libssl/ssl_sess.c
index ee88be2b88..8391d62212 100644
--- a/src/lib/libssl/ssl_sess.c
+++ b/src/lib/libssl/ssl_sess.c
@@ -59,6 +59,9 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <openssl/lhash.h> 60#include <openssl/lhash.h>
61#include <openssl/rand.h> 61#include <openssl/rand.h>
62#ifndef OPENSSL_NO_ENGINE
63#include <openssl/engine.h>
64#endif
62#include "ssl_locl.h" 65#include "ssl_locl.h"
63 66
64static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); 67static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
@@ -870,6 +873,25 @@ int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PK
870 return ctx->client_cert_cb; 873 return ctx->client_cert_cb;
871 } 874 }
872 875
876#ifndef OPENSSL_NO_ENGINE
877int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
878 {
879 if (!ENGINE_init(e))
880 {
881 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
882 return 0;
883 }
884 if(!ENGINE_get_ssl_client_cert_function(e))
885 {
886 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, SSL_R_NO_CLIENT_CERT_METHOD);
887 ENGINE_finish(e);
888 return 0;
889 }
890 ctx->client_cert_engine = e;
891 return 1;
892 }
893#endif
894
873void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, 895void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
874 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len)) 896 int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
875 { 897 {
diff --git a/src/lib/libssl/t1_enc.c b/src/lib/libssl/t1_enc.c
index ed5a4a7255..7cb3e29a41 100644
--- a/src/lib/libssl/t1_enc.c
+++ b/src/lib/libssl/t1_enc.c
@@ -111,10 +111,15 @@
111 111
112#include <stdio.h> 112#include <stdio.h>
113#include "ssl_locl.h" 113#include "ssl_locl.h"
114#ifndef OPENSSL_NO_COMP
114#include <openssl/comp.h> 115#include <openssl/comp.h>
116#endif
115#include <openssl/evp.h> 117#include <openssl/evp.h>
116#include <openssl/hmac.h> 118#include <openssl/hmac.h>
117#include <openssl/md5.h> 119#include <openssl/md5.h>
120#ifdef KSSL_DEBUG
121#include <openssl/des.h>
122#endif
118 123
119static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec, 124static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
120 int sec_len, unsigned char *seed, int seed_len, 125 int sec_len, unsigned char *seed, int seed_len,
@@ -131,6 +136,8 @@ static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
131 136
132 HMAC_CTX_init(&ctx); 137 HMAC_CTX_init(&ctx);
133 HMAC_CTX_init(&ctx_tmp); 138 HMAC_CTX_init(&ctx_tmp);
139 HMAC_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
140 HMAC_CTX_set_flags(&ctx_tmp, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
134 HMAC_Init_ex(&ctx,sec,sec_len,md, NULL); 141 HMAC_Init_ex(&ctx,sec,sec_len,md, NULL);
135 HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL); 142 HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL);
136 HMAC_Update(&ctx,seed,seed_len); 143 HMAC_Update(&ctx,seed,seed_len);
@@ -249,15 +256,15 @@ int tls1_change_cipher_state(SSL *s, int which)
249#ifdef KSSL_DEBUG 256#ifdef KSSL_DEBUG
250 printf("tls1_change_cipher_state(which= %d) w/\n", which); 257 printf("tls1_change_cipher_state(which= %d) w/\n", which);
251 printf("\talg= %ld, comp= %p\n", s->s3->tmp.new_cipher->algorithms, 258 printf("\talg= %ld, comp= %p\n", s->s3->tmp.new_cipher->algorithms,
252 comp); 259 (void *)comp);
253 printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c); 260 printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", (void *)c);
254 printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n", 261 printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n",
255 c->nid,c->block_size,c->key_len,c->iv_len); 262 c->nid,c->block_size,c->key_len,c->iv_len);
256 printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length); 263 printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length);
257 { 264 {
258 int i; 265 int ki;
259 for (i=0; i<s->s3->tmp.key_block_length; i++) 266 for (ki=0; ki<s->s3->tmp.key_block_length; ki++)
260 printf("%02x", key_block[i]); printf("\n"); 267 printf("%02x", key_block[ki]); printf("\n");
261 } 268 }
262#endif /* KSSL_DEBUG */ 269#endif /* KSSL_DEBUG */
263 270
@@ -413,11 +420,13 @@ printf("which = %04X\nmac key=",which);
413 s->session->key_arg_length=0; 420 s->session->key_arg_length=0;
414#ifdef KSSL_DEBUG 421#ifdef KSSL_DEBUG
415 { 422 {
416 int i; 423 int ki;
417 printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n"); 424 printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n");
418 printf("\tkey= "); for (i=0; i<c->key_len; i++) printf("%02x", key[i]); 425 printf("\tkey= ");
426 for (ki=0; ki<c->key_len; ki++) printf("%02x", key[ki]);
419 printf("\n"); 427 printf("\n");
420 printf("\t iv= "); for (i=0; i<c->iv_len; i++) printf("%02x", iv[i]); 428 printf("\t iv= ");
429 for (ki=0; ki<c->iv_len; ki++) printf("%02x", iv[ki]);
421 printf("\n"); 430 printf("\n");
422 } 431 }
423#endif /* KSSL_DEBUG */ 432#endif /* KSSL_DEBUG */
@@ -590,10 +599,11 @@ int tls1_enc(SSL *s, int send)
590 { 599 {
591 unsigned long ui; 600 unsigned long ui;
592 printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", 601 printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
593 ds,rec->data,rec->input,l); 602 (void *)ds,rec->data,rec->input,l);
594 printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n", 603 printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%ld %ld], %d iv_len\n",
595 ds->buf_len, ds->cipher->key_len, 604 ds->buf_len, ds->cipher->key_len,
596 DES_KEY_SZ, DES_SCHEDULE_SZ, 605 (unsigned long)DES_KEY_SZ,
606 (unsigned long)DES_SCHEDULE_SZ,
597 ds->cipher->iv_len); 607 ds->cipher->iv_len);
598 printf("\t\tIV: "); 608 printf("\t\tIV: ");
599 for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]); 609 for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]);
@@ -618,10 +628,10 @@ int tls1_enc(SSL *s, int send)
618 628
619#ifdef KSSL_DEBUG 629#ifdef KSSL_DEBUG
620 { 630 {
621 unsigned long i; 631 unsigned long ki;
622 printf("\trec->data="); 632 printf("\trec->data=");
623 for (i=0; i<l; i++) 633 for (ki=0; ki<l; i++)
624 printf(" %02x", rec->data[i]); printf("\n"); 634 printf(" %02x", rec->data[ki]); printf("\n");
625 } 635 }
626#endif /* KSSL_DEBUG */ 636#endif /* KSSL_DEBUG */
627 637
@@ -805,7 +815,7 @@ int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
805 unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH]; 815 unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
806 816
807#ifdef KSSL_DEBUG 817#ifdef KSSL_DEBUG
808 printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len); 818 printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", (void *)s,out, p,len);
809#endif /* KSSL_DEBUG */ 819#endif /* KSSL_DEBUG */
810 820
811 /* Setup the stuff to munge */ 821 /* Setup the stuff to munge */
@@ -852,8 +862,10 @@ int tls1_alert_code(int code)
852 case SSL_AD_INTERNAL_ERROR: return(TLS1_AD_INTERNAL_ERROR); 862 case SSL_AD_INTERNAL_ERROR: return(TLS1_AD_INTERNAL_ERROR);
853 case SSL_AD_USER_CANCELLED: return(TLS1_AD_USER_CANCELLED); 863 case SSL_AD_USER_CANCELLED: return(TLS1_AD_USER_CANCELLED);
854 case SSL_AD_NO_RENEGOTIATION: return(TLS1_AD_NO_RENEGOTIATION); 864 case SSL_AD_NO_RENEGOTIATION: return(TLS1_AD_NO_RENEGOTIATION);
865#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
855 case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: return 866 case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: return
856 (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); 867 (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
868#endif
857 default: return(-1); 869 default: return(-1);
858 } 870 }
859 } 871 }
diff --git a/src/lib/libssl/t1_lib.c b/src/lib/libssl/t1_lib.c
index 35f04afa4a..9ce726996d 100644
--- a/src/lib/libssl/t1_lib.c
+++ b/src/lib/libssl/t1_lib.c
@@ -734,6 +734,13 @@ int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
734 /* Point after session ID in client hello */ 734 /* Point after session ID in client hello */
735 const unsigned char *p = session_id + len; 735 const unsigned char *p = session_id + len;
736 unsigned short i; 736 unsigned short i;
737
738 /* If tickets disabled behave as if no ticket present
739 * to permit stateful resumption.
740 */
741 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
742 return 1;
743
737 if ((s->version <= SSL3_VERSION) || !limit) 744 if ((s->version <= SSL3_VERSION) || !limit)
738 return 1; 745 return 1;
739 if (p >= limit) 746 if (p >= limit)
@@ -761,12 +768,7 @@ int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
761 return 1; 768 return 1;
762 if (type == TLSEXT_TYPE_session_ticket) 769 if (type == TLSEXT_TYPE_session_ticket)
763 { 770 {
764 /* If tickets disabled indicate cache miss which will 771 /* If zero length note client will accept a ticket
765 * trigger a full handshake
766 */
767 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
768 return 0;
769 /* If zero length not client will accept a ticket
770 * and indicate cache miss to trigger full handshake 772 * and indicate cache miss to trigger full handshake
771 */ 773 */
772 if (size == 0) 774 if (size == 0)
diff --git a/src/lib/libssl/test/CAss.cnf b/src/lib/libssl/test/CAss.cnf
index 20f8f05e3d..546e660626 100644
--- a/src/lib/libssl/test/CAss.cnf
+++ b/src/lib/libssl/test/CAss.cnf
@@ -7,7 +7,7 @@ RANDFILE = ./.rnd
7 7
8#################################################################### 8####################################################################
9[ req ] 9[ req ]
10default_bits = 512 10default_bits = 1024
11default_keyfile = keySS.pem 11default_keyfile = keySS.pem
12distinguished_name = req_distinguished_name 12distinguished_name = req_distinguished_name
13encrypt_rsa_key = no 13encrypt_rsa_key = no
diff --git a/src/lib/libssl/test/Uss.cnf b/src/lib/libssl/test/Uss.cnf
index 0c0ebb5f67..98b2e054b7 100644
--- a/src/lib/libssl/test/Uss.cnf
+++ b/src/lib/libssl/test/Uss.cnf
@@ -7,7 +7,7 @@ RANDFILE = ./.rnd
7 7
8#################################################################### 8####################################################################
9[ req ] 9[ req ]
10default_bits = 512 10default_bits = 1024
11default_keyfile = keySS.pem 11default_keyfile = keySS.pem
12distinguished_name = req_distinguished_name 12distinguished_name = req_distinguished_name
13encrypt_rsa_key = no 13encrypt_rsa_key = no