summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto')
-rw-r--r--src/lib/libcrypto/asn1/a_bitstr.c204
-rw-r--r--src/lib/libcrypto/asn1/a_bool.c121
-rw-r--r--src/lib/libcrypto/asn1/a_bytes.c346
-rw-r--r--src/lib/libcrypto/asn1/a_d2i_fp.c201
-rw-r--r--src/lib/libcrypto/asn1/a_digest.c91
-rw-r--r--src/lib/libcrypto/asn1/a_dup.c86
-rw-r--r--src/lib/libcrypto/asn1/a_i2d_fp.c119
-rw-r--r--src/lib/libcrypto/asn1/a_int.c305
-rw-r--r--src/lib/libcrypto/asn1/a_object.c389
-rw-r--r--src/lib/libcrypto/asn1/a_octet.c90
-rw-r--r--src/lib/libcrypto/asn1/a_print.c161
-rw-r--r--src/lib/libcrypto/asn1/a_set.c149
-rw-r--r--src/lib/libcrypto/asn1/a_sign.c147
-rw-r--r--src/lib/libcrypto/asn1/a_type.c325
-rw-r--r--src/lib/libcrypto/asn1/a_verify.c121
-rw-r--r--src/lib/libcrypto/asn1/asn1.h859
-rw-r--r--src/lib/libcrypto/asn1/asn1_err.c266
-rw-r--r--src/lib/libcrypto/asn1/asn1_lib.c444
-rw-r--r--src/lib/libcrypto/asn1/asn1_mac.h321
-rw-r--r--src/lib/libcrypto/asn1/asn1_par.c393
-rw-r--r--src/lib/libcrypto/asn1/d2i_pr.c117
-rw-r--r--src/lib/libcrypto/asn1/d2i_pu.c117
-rw-r--r--src/lib/libcrypto/asn1/evp_asn1.c193
-rw-r--r--src/lib/libcrypto/asn1/f_int.c211
-rw-r--r--src/lib/libcrypto/asn1/f_string.c210
-rw-r--r--src/lib/libcrypto/asn1/i2d_pr.c86
-rw-r--r--src/lib/libcrypto/asn1/i2d_pu.c84
-rw-r--r--src/lib/libcrypto/asn1/n_pkey.c365
-rw-r--r--src/lib/libcrypto/asn1/t_pkey.c392
-rw-r--r--src/lib/libcrypto/asn1/t_req.c226
-rw-r--r--src/lib/libcrypto/asn1/t_x509.c386
-rw-r--r--src/lib/libcrypto/asn1/x_algor.c126
-rw-r--r--src/lib/libcrypto/asn1/x_attrib.c152
-rw-r--r--src/lib/libcrypto/asn1/x_crl.c353
-rw-r--r--src/lib/libcrypto/asn1/x_exten.c156
-rw-r--r--src/lib/libcrypto/asn1/x_info.c111
-rw-r--r--src/lib/libcrypto/asn1/x_name.c295
-rw-r--r--src/lib/libcrypto/asn1/x_pkey.c156
-rw-r--r--src/lib/libcrypto/asn1/x_pubkey.c256
-rw-r--r--src/lib/libcrypto/asn1/x_req.c247
-rw-r--r--src/lib/libcrypto/asn1/x_sig.c119
-rw-r--r--src/lib/libcrypto/asn1/x_spki.c181
-rw-r--r--src/lib/libcrypto/asn1/x_val.c118
-rw-r--r--src/lib/libcrypto/asn1/x_x509.c158
-rw-r--r--src/lib/libcrypto/bf/COPYRIGHT46
-rw-r--r--src/lib/libcrypto/bf/INSTALL14
-rw-r--r--src/lib/libcrypto/bf/README8
-rw-r--r--src/lib/libcrypto/bf/VERSION6
-rw-r--r--src/lib/libcrypto/bf/asm/bf-586.pl136
-rw-r--r--src/lib/libcrypto/bf/bf_cbc.c148
-rw-r--r--src/lib/libcrypto/bf/bf_cfb64.c127
-rw-r--r--src/lib/libcrypto/bf/bf_ecb.c98
-rw-r--r--src/lib/libcrypto/bf/bf_enc.c241
-rw-r--r--src/lib/libcrypto/bf/bf_ofb64.c115
-rw-r--r--src/lib/libcrypto/bf/bf_pi.h325
-rw-r--r--src/lib/libcrypto/bf/bf_skey.c119
-rw-r--r--src/lib/libcrypto/bf/blowfish.h116
-rw-r--r--src/lib/libcrypto/bio/b_dump.c125
-rw-r--r--src/lib/libcrypto/bio/b_print.c92
-rw-r--r--src/lib/libcrypto/bio/b_sock.c628
-rw-r--r--src/lib/libcrypto/bio/bf_buff.c512
-rw-r--r--src/lib/libcrypto/bio/bf_nbio.c268
-rw-r--r--src/lib/libcrypto/bio/bf_null.c196
-rw-r--r--src/lib/libcrypto/bio/bio.h688
-rw-r--r--src/lib/libcrypto/bio/bio_cb.c138
-rw-r--r--src/lib/libcrypto/bio/bio_err.c130
-rw-r--r--src/lib/libcrypto/bio/bio_lib.c519
-rw-r--r--src/lib/libcrypto/bio/bss_acpt.c500
-rw-r--r--src/lib/libcrypto/bio/bss_conn.c648
-rw-r--r--src/lib/libcrypto/bio/bss_fd.c62
-rw-r--r--src/lib/libcrypto/bio/bss_file.c339
-rw-r--r--src/lib/libcrypto/bio/bss_mem.c297
-rw-r--r--src/lib/libcrypto/bio/bss_null.c177
-rw-r--r--src/lib/libcrypto/bio/bss_sock.c461
-rw-r--r--src/lib/libcrypto/bn/asm/bn-586.pl314
-rw-r--r--src/lib/libcrypto/bn/asm/pa-risc2.s416
-rw-r--r--src/lib/libcrypto/bn/bn_add.c167
-rw-r--r--src/lib/libcrypto/bn/bn_blind.c143
-rw-r--r--src/lib/libcrypto/bn/bn_div.c286
-rw-r--r--src/lib/libcrypto/bn/bn_err.c111
-rw-r--r--src/lib/libcrypto/bn/bn_exp.c553
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c203
-rw-r--r--src/lib/libcrypto/bn/bn_lcl.h199
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c611
-rw-r--r--src/lib/libcrypto/bn/bn_mod.c97
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c306
-rw-r--r--src/lib/libcrypto/bn/bn_mpi.c134
-rw-r--r--src/lib/libcrypto/bn/bn_mul.c209
-rw-r--r--src/lib/libcrypto/bn/bn_prime.c473
-rw-r--r--src/lib/libcrypto/bn/bn_prime.h325
-rw-r--r--src/lib/libcrypto/bn/bn_prime.pl56
-rw-r--r--src/lib/libcrypto/bn/bn_print.c333
-rw-r--r--src/lib/libcrypto/bn/bn_rand.c121
-rw-r--r--src/lib/libcrypto/bn/bn_recp.c125
-rw-r--r--src/lib/libcrypto/bn/bn_shift.c210
-rw-r--r--src/lib/libcrypto/bn/bn_sqr.c122
-rw-r--r--src/lib/libcrypto/bn/bn_word.c204
-rw-r--r--src/lib/libcrypto/buffer/buf_err.c87
-rw-r--r--src/lib/libcrypto/buffer/buffer.c145
-rw-r--r--src/lib/libcrypto/buffer/buffer.h107
-rw-r--r--src/lib/libcrypto/cast/asm/cast-586.pl167
-rw-r--r--src/lib/libcrypto/cast/c_cfb64.c127
-rw-r--r--src/lib/libcrypto/cast/c_ecb.c82
-rw-r--r--src/lib/libcrypto/cast/c_enc.c210
-rw-r--r--src/lib/libcrypto/cast/c_ofb64.c115
-rw-r--r--src/lib/libcrypto/cast/c_skey.c165
-rw-r--r--src/lib/libcrypto/cast/cast.h109
-rw-r--r--src/lib/libcrypto/cast/cast_lcl.h224
-rw-r--r--src/lib/libcrypto/cast/cast_s.h585
-rw-r--r--src/lib/libcrypto/conf/conf.h114
-rw-r--r--src/lib/libcrypto/conf/conf_err.c96
-rw-r--r--src/lib/libcrypto/conf/keysets.pl61
-rw-r--r--src/lib/libcrypto/conf/ssleay.cnf78
-rw-r--r--src/lib/libcrypto/cpt_err.c86
-rw-r--r--src/lib/libcrypto/cryptlib.c307
-rw-r--r--src/lib/libcrypto/cryptlib.h100
-rw-r--r--src/lib/libcrypto/crypto.h319
-rw-r--r--src/lib/libcrypto/cversion.c99
-rw-r--r--src/lib/libcrypto/des/COPYRIGHT50
-rw-r--r--src/lib/libcrypto/des/asm/crypt586.pl204
-rw-r--r--src/lib/libcrypto/des/asm/des-586.pl251
-rw-r--r--src/lib/libcrypto/des/asm/desboth.pl79
-rw-r--r--src/lib/libcrypto/des/cbc_cksm.c103
-rw-r--r--src/lib/libcrypto/des/cbc_enc.c135
-rw-r--r--src/lib/libcrypto/des/cfb64ede.c151
-rw-r--r--src/lib/libcrypto/des/cfb64enc.c128
-rw-r--r--src/lib/libcrypto/des/cfb_enc.c171
-rw-r--r--src/lib/libcrypto/des/des_enc.c502
-rw-r--r--src/lib/libcrypto/des/ecb3_enc.c87
-rw-r--r--src/lib/libcrypto/des/ecb_enc.c124
-rw-r--r--src/lib/libcrypto/des/enc_read.c218
-rw-r--r--src/lib/libcrypto/des/enc_writ.c160
-rw-r--r--src/lib/libcrypto/des/fcrypt.c153
-rw-r--r--src/lib/libcrypto/des/fcrypt_b.c148
-rw-r--r--src/lib/libcrypto/des/ncbc_enc.c130
-rw-r--r--src/lib/libcrypto/des/ofb64ede.c131
-rw-r--r--src/lib/libcrypto/des/ofb64enc.c114
-rw-r--r--src/lib/libcrypto/des/ofb_enc.c139
-rw-r--r--src/lib/libcrypto/des/pcbc_enc.c126
-rw-r--r--src/lib/libcrypto/des/qud_cksm.c144
-rw-r--r--src/lib/libcrypto/des/rand_key.c118
-rw-r--r--src/lib/libcrypto/des/set_key.c246
-rw-r--r--src/lib/libcrypto/des/spr.h204
-rw-r--r--src/lib/libcrypto/des/str2key.c171
-rw-r--r--src/lib/libcrypto/des/xcbc_enc.c206
-rw-r--r--src/lib/libcrypto/dh/dh.h162
-rw-r--r--src/lib/libcrypto/dh/dh_check.c120
-rw-r--r--src/lib/libcrypto/dh/dh_err.c96
-rw-r--r--src/lib/libcrypto/dh/dh_gen.c150
-rw-r--r--src/lib/libcrypto/dh/dh_key.c142
-rw-r--r--src/lib/libcrypto/dh/dh_lib.c100
-rw-r--r--src/lib/libcrypto/dsa/dsa.h194
-rw-r--r--src/lib/libcrypto/dsa/dsa_err.c99
-rw-r--r--src/lib/libcrypto/dsa/dsa_gen.c328
-rw-r--r--src/lib/libcrypto/dsa/dsa_key.c112
-rw-r--r--src/lib/libcrypto/dsa/dsa_lib.c145
-rw-r--r--src/lib/libcrypto/dsa/dsa_sign.c215
-rw-r--r--src/lib/libcrypto/dsa/dsa_vrf.c152
-rw-r--r--src/lib/libcrypto/err/err.c642
-rw-r--r--src/lib/libcrypto/err/err.h287
-rw-r--r--src/lib/libcrypto/err/err_all.c116
-rw-r--r--src/lib/libcrypto/err/err_prn.c107
-rw-r--r--src/lib/libcrypto/evp/bio_b64.c547
-rw-r--r--src/lib/libcrypto/evp/bio_enc.c423
-rw-r--r--src/lib/libcrypto/evp/bio_md.c270
-rw-r--r--src/lib/libcrypto/evp/c_all.c190
-rw-r--r--src/lib/libcrypto/evp/digest.c89
-rw-r--r--src/lib/libcrypto/evp/e_null.c109
-rw-r--r--src/lib/libcrypto/evp/e_rc4.c127
-rw-r--r--src/lib/libcrypto/evp/e_xcbc_d.c122
-rw-r--r--src/lib/libcrypto/evp/encode.c438
-rw-r--r--src/lib/libcrypto/evp/evp.h793
-rw-r--r--src/lib/libcrypto/evp/evp_enc.c303
-rw-r--r--src/lib/libcrypto/evp/evp_err.c108
-rw-r--r--src/lib/libcrypto/evp/evp_key.c167
-rw-r--r--src/lib/libcrypto/evp/evp_lib.c117
-rw-r--r--src/lib/libcrypto/evp/m_dss.c82
-rw-r--r--src/lib/libcrypto/evp/m_dss1.c81
-rw-r--r--src/lib/libcrypto/evp/m_md5.c81
-rw-r--r--src/lib/libcrypto/evp/m_null.c88
-rw-r--r--src/lib/libcrypto/evp/m_ripemd.c81
-rw-r--r--src/lib/libcrypto/evp/m_sha1.c81
-rw-r--r--src/lib/libcrypto/evp/names.c285
-rw-r--r--src/lib/libcrypto/evp/p_dec.c84
-rw-r--r--src/lib/libcrypto/evp/p_enc.c83
-rw-r--r--src/lib/libcrypto/evp/p_lib.c294
-rw-r--r--src/lib/libcrypto/evp/p_open.c119
-rw-r--r--src/lib/libcrypto/evp/p_seal.c115
-rw-r--r--src/lib/libcrypto/evp/p_sign.c119
-rw-r--r--src/lib/libcrypto/evp/p_verify.c102
-rw-r--r--src/lib/libcrypto/ex_data.c236
-rw-r--r--src/lib/libcrypto/hmac/hmac.c165
-rw-r--r--src/lib/libcrypto/hmac/hmac.h106
-rw-r--r--src/lib/libcrypto/lhash/lh_stats.c289
-rw-r--r--src/lib/libcrypto/lhash/lhash.c489
-rw-r--r--src/lib/libcrypto/lhash/lhash.h155
-rw-r--r--src/lib/libcrypto/md5/asm/md5-586.pl304
-rw-r--r--src/lib/libcrypto/md5/md5.h99
-rw-r--r--src/lib/libcrypto/md5/md5_dgst.c440
-rw-r--r--src/lib/libcrypto/md5/md5_locl.h195
-rw-r--r--src/lib/libcrypto/md5/md5_one.c77
-rw-r--r--src/lib/libcrypto/objects/obj_dat.c578
-rw-r--r--src/lib/libcrypto/objects/obj_dat.pl269
-rw-r--r--src/lib/libcrypto/objects/obj_err.c96
-rw-r--r--src/lib/libcrypto/objects/obj_lib.c126
-rw-r--r--src/lib/libcrypto/objects/objects.h724
-rw-r--r--src/lib/libcrypto/objects/objects.txt40
-rw-r--r--src/lib/libcrypto/pem/message16
-rw-r--r--src/lib/libcrypto/pem/pem.h562
-rw-r--r--src/lib/libcrypto/pem/pem_all.c488
-rw-r--r--src/lib/libcrypto/pem/pem_err.c122
-rw-r--r--src/lib/libcrypto/pem/pem_info.c365
-rw-r--r--src/lib/libcrypto/pem/pem_lib.c762
-rw-r--r--src/lib/libcrypto/pem/pem_seal.c191
-rw-r--r--src/lib/libcrypto/pem/pem_sign.c109
-rw-r--r--src/lib/libcrypto/pem/pkcs7.lis22
-rw-r--r--src/lib/libcrypto/perlasm/cbc.pl342
-rw-r--r--src/lib/libcrypto/perlasm/readme124
-rw-r--r--src/lib/libcrypto/perlasm/x86asm.pl113
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_doit.c408
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_lib.c449
-rw-r--r--src/lib/libcrypto/pkcs7/pkcs7.h449
-rw-r--r--src/lib/libcrypto/pkcs7/pkcs7err.c110
-rw-r--r--src/lib/libcrypto/rand/rand.h92
-rw-r--r--src/lib/libcrypto/rand/randfile.c166
-rw-r--r--src/lib/libcrypto/rc2/rc2_cbc.c235
-rw-r--r--src/lib/libcrypto/rc2/rc2_ecb.c90
-rw-r--r--src/lib/libcrypto/rc2/rc2_locl.h156
-rw-r--r--src/lib/libcrypto/rc2/rc2_skey.c142
-rw-r--r--src/lib/libcrypto/rc2/rc2cfb64.c127
-rw-r--r--src/lib/libcrypto/rc2/rc2ofb64.c115
-rw-r--r--src/lib/libcrypto/rc2/rrc2.doc219
-rw-r--r--src/lib/libcrypto/rc2/version22
-rw-r--r--src/lib/libcrypto/rc4/asm/rc4-586.pl173
-rw-r--r--src/lib/libcrypto/rc4/rc4_enc.c135
-rw-r--r--src/lib/libcrypto/rc4/rc4_skey.c119
-rw-r--r--src/lib/libcrypto/ripemd/README15
-rw-r--r--src/lib/libcrypto/ripemd/asm/rmd-586.pl582
-rw-r--r--src/lib/libcrypto/ripemd/ripemd.h99
-rw-r--r--src/lib/libcrypto/ripemd/rmd_dgst.c535
-rw-r--r--src/lib/libcrypto/ripemd/rmd_locl.h226
-rw-r--r--src/lib/libcrypto/ripemd/rmd_one.c77
-rw-r--r--src/lib/libcrypto/ripemd/rmdconst.h399
-rw-r--r--src/lib/libcrypto/rsa/rsa.h324
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c274
-rw-r--r--src/lib/libcrypto/rsa/rsa_err.c129
-rw-r--r--src/lib/libcrypto/rsa/rsa_gen.c101
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c294
-rw-r--r--src/lib/libcrypto/rsa/rsa_none.c109
-rw-r--r--src/lib/libcrypto/rsa/rsa_pk1.c233
-rw-r--r--src/lib/libcrypto/rsa/rsa_saos.c153
-rw-r--r--src/lib/libcrypto/rsa/rsa_sign.c196
-rw-r--r--src/lib/libcrypto/rsa/rsa_ssl.c153
-rw-r--r--src/lib/libcrypto/sha/asm/sha1-586.pl491
-rw-r--r--src/lib/libcrypto/sha/sha.h109
-rw-r--r--src/lib/libcrypto/sha/sha1_one.c77
-rw-r--r--src/lib/libcrypto/sha/sha1dgst.c468
-rw-r--r--src/lib/libcrypto/sha/sha_locl.h246
-rw-r--r--src/lib/libcrypto/stack/stack.c307
-rw-r--r--src/lib/libcrypto/stack/stack.h120
-rw-r--r--src/lib/libcrypto/txt_db/txt_db.c394
-rw-r--r--src/lib/libcrypto/txt_db/txt_db.h117
-rw-r--r--src/lib/libcrypto/x509/by_dir.c359
-rw-r--r--src/lib/libcrypto/x509/by_file.c282
-rw-r--r--src/lib/libcrypto/x509/x509.h1152
-rw-r--r--src/lib/libcrypto/x509/x509_cmp.c257
-rw-r--r--src/lib/libcrypto/x509/x509_d2.c110
-rw-r--r--src/lib/libcrypto/x509/x509_def.c83
-rw-r--r--src/lib/libcrypto/x509/x509_err.c130
-rw-r--r--src/lib/libcrypto/x509/x509_ext.c222
-rw-r--r--src/lib/libcrypto/x509/x509_lu.c446
-rw-r--r--src/lib/libcrypto/x509/x509_obj.c179
-rw-r--r--src/lib/libcrypto/x509/x509_r2x.c122
-rw-r--r--src/lib/libcrypto/x509/x509_req.c116
-rw-r--r--src/lib/libcrypto/x509/x509_set.c164
-rw-r--r--src/lib/libcrypto/x509/x509_txt.c132
-rw-r--r--src/lib/libcrypto/x509/x509_v3.c409
-rw-r--r--src/lib/libcrypto/x509/x509_vfy.c704
-rw-r--r--src/lib/libcrypto/x509/x509_vfy.h378
-rw-r--r--src/lib/libcrypto/x509/x509name.c358
-rw-r--r--src/lib/libcrypto/x509/x509rset.c89
-rw-r--r--src/lib/libcrypto/x509/x509type.c115
-rw-r--r--src/lib/libcrypto/x509/x_all.c465
-rw-r--r--src/lib/libcrypto/x509v3/x509v3.h87
284 files changed, 62789 insertions, 0 deletions
diff --git a/src/lib/libcrypto/asn1/a_bitstr.c b/src/lib/libcrypto/asn1/a_bitstr.c
new file mode 100644
index 0000000000..2c10120651
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_bitstr.c
@@ -0,0 +1,204 @@
1/* crypto/asn1/a_bitstr.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62
63/* ASN1err(ASN1_F_ASN1_STRING_NEW,ASN1_R_STRING_TOO_SHORT);
64 * ASN1err(ASN1_F_D2I_ASN1_BIT_STRING,ASN1_R_EXPECTING_A_BIT_STRING);
65 */
66
67int i2d_ASN1_BIT_STRING(a,pp)
68ASN1_BIT_STRING *a;
69unsigned char **pp;
70 {
71 int ret,j,r,bits;
72 unsigned char *p,*d;
73
74 if (a == NULL) return(0);
75
76 /* our bit strings are always a multiple of 8 :-) */
77 bits=0;
78 ret=1+a->length;
79 r=ASN1_object_size(0,ret,V_ASN1_BIT_STRING);
80 if (pp == NULL) return(r);
81 p= *pp;
82
83 ASN1_put_object(&p,0,ret,V_ASN1_BIT_STRING,V_ASN1_UNIVERSAL);
84 if (bits == 0)
85 j=0;
86 else j=8-bits;
87 *(p++)=(unsigned char)j;
88 d=a->data;
89 memcpy(p,d,a->length);
90 p+=a->length;
91 if (a->length > 0) p[-1]&=(0xff<<j);
92 *pp=p;
93 return(r);
94 }
95
96ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(a, pp, length)
97ASN1_BIT_STRING **a;
98unsigned char **pp;
99long length;
100 {
101 ASN1_BIT_STRING *ret=NULL;
102 unsigned char *p,*s;
103 long len;
104 int inf,tag,xclass;
105 int i;
106
107 if ((a == NULL) || ((*a) == NULL))
108 {
109 if ((ret=ASN1_BIT_STRING_new()) == NULL) return(NULL);
110 }
111 else
112 ret=(*a);
113
114 p= *pp;
115 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
116 if (inf & 0x80)
117 {
118 i=ASN1_R_BAD_OBJECT_HEADER;
119 goto err;
120 }
121
122 if (tag != V_ASN1_BIT_STRING)
123 {
124 i=ASN1_R_EXPECTING_A_BIT_STRING;
125 goto err;
126 }
127 if (len < 1) { i=ASN1_R_STRING_TOO_SHORT; goto err; }
128
129 i= *(p++);
130 if (len-- > 1) /* using one because of the bits left byte */
131 {
132 s=(unsigned char *)Malloc((int)len);
133 if (s == NULL)
134 {
135 i=ERR_R_MALLOC_FAILURE;
136 goto err;
137 }
138 memcpy(s,p,(int)len);
139 s[len-1]&=(0xff<<i);
140 p+=len;
141 }
142 else
143 s=NULL;
144
145 ret->length=(int)len;
146 if (ret->data != NULL) Free((char *)ret->data);
147 ret->data=s;
148 ret->type=V_ASN1_BIT_STRING;
149 if (a != NULL) (*a)=ret;
150 *pp=p;
151 return(ret);
152err:
153 ASN1err(ASN1_F_D2I_ASN1_BIT_STRING,i);
154 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
155 ASN1_BIT_STRING_free(ret);
156 return(NULL);
157 }
158
159/* These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
160 */
161int ASN1_BIT_STRING_set_bit(a,n,value)
162ASN1_BIT_STRING *a;
163int n;
164int value;
165 {
166 int w,v,iv;
167 unsigned char *c;
168
169 w=n/8;
170 v=1<<(7-(n&0x07));
171 iv= ~v;
172
173 if (a == NULL) return(0);
174 if ((a->length < (w+1)) || (a->data == NULL))
175 {
176 if (!value) return(1); /* Don't need to set */
177 if (a->data == NULL)
178 c=(unsigned char *)Malloc(w+1);
179 else
180 c=(unsigned char *)Realloc(a->data,w+1);
181 if (c == NULL) return(0);
182 a->data=c;
183 a->length=w+1;
184 c[w]=0;
185 }
186 a->data[w]=((a->data[w])&iv)|v;
187 while ((a->length > 0) && (a->data[a->length-1] == 0))
188 a->length--;
189 return(1);
190 }
191
192int ASN1_BIT_STRING_get_bit(a,n)
193ASN1_BIT_STRING *a;
194int n;
195 {
196 int w,v;
197
198 w=n/8;
199 v=1<<(7-(n&0x07));
200 if ((a == NULL) || (a->length < (w+1)) || (a->data == NULL))
201 return(0);
202 return((a->data[w]&v) != 0);
203 }
204
diff --git a/src/lib/libcrypto/asn1/a_bool.c b/src/lib/libcrypto/asn1/a_bool.c
new file mode 100644
index 0000000000..41a95aa278
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_bool.c
@@ -0,0 +1,121 @@
1/* crypto/asn1/a_bool.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62
63/* ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,ASN1_R_EXPECTING_A_BOOLEAN);
64 * ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
65 */
66
67int i2d_ASN1_BOOLEAN(a,pp)
68int a;
69unsigned char **pp;
70 {
71 int r;
72 unsigned char *p;
73
74 r=ASN1_object_size(0,1,V_ASN1_BOOLEAN);
75 if (pp == NULL) return(r);
76 p= *pp;
77
78 ASN1_put_object(&p,0,1,V_ASN1_BOOLEAN,V_ASN1_UNIVERSAL);
79 *(p++)= (unsigned char)a;
80 *pp=p;
81 return(r);
82 }
83
84int d2i_ASN1_BOOLEAN(a, pp, length)
85int *a;
86unsigned char **pp;
87long length;
88 {
89 int ret= -1;
90 unsigned char *p;
91 long len;
92 int inf,tag,xclass;
93 int i=0;
94
95 p= *pp;
96 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
97 if (inf & 0x80)
98 {
99 i=ASN1_R_BAD_OBJECT_HEADER;
100 goto err;
101 }
102
103 if (tag != V_ASN1_BOOLEAN)
104 {
105 i=ASN1_R_EXPECTING_A_BOOLEAN;
106 goto err;
107 }
108
109 if (len != 1)
110 {
111 i=ASN1_R_BOOLEAN_IS_WRONG_LENGTH;
112 goto err;
113 }
114 ret= (int)*(p++);
115 if (a != NULL) (*a)=ret;
116 *pp=p;
117 return(ret);
118err:
119 ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,i);
120 return(ret);
121 }
diff --git a/src/lib/libcrypto/asn1/a_bytes.c b/src/lib/libcrypto/asn1/a_bytes.c
new file mode 100644
index 0000000000..14168d61ad
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_bytes.c
@@ -0,0 +1,346 @@
1/* crypto/asn1/a_bytes.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ASN1_R_ERROR_STACK);
64 * ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_ERROR_STACK);
65 * ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_WRONG_TYPE);
66 * ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,ASN1_R_WRONG_TAG);
67 */
68
69static unsigned long tag2bit[32]={
700, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
71B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
72B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
73B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 12-15 */
740, 0, B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING,
75B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING,0,
760,B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING,
77B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN,
78 };
79
80#ifndef NOPROTO
81static int asn1_collate_primative(ASN1_STRING *a, ASN1_CTX *c);
82#else
83static int asn1_collate_primative();
84#endif
85
86/* type is a 'bitmap' of acceptable string types to be accepted.
87 */
88ASN1_STRING *d2i_ASN1_type_bytes(a, pp, length, type)
89ASN1_STRING **a;
90unsigned char **pp;
91long length;
92int type;
93 {
94 ASN1_STRING *ret=NULL;
95 unsigned char *p,*s;
96 long len;
97 int inf,tag,xclass;
98 int i=0;
99
100 p= *pp;
101 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
102 if (inf & 0x80) goto err;
103
104 if (tag >= 32)
105 {
106 i=ASN1_R_TAG_VALUE_TOO_HIGH;;
107 goto err;
108 }
109 if (!(tag2bit[tag] & type))
110 {
111 i=ASN1_R_WRONG_TYPE;
112 goto err;
113 }
114
115 /* If a bit-string, exit early */
116 if (tag == V_ASN1_BIT_STRING)
117 return(d2i_ASN1_BIT_STRING(a,pp,length));
118
119 if ((a == NULL) || ((*a) == NULL))
120 {
121 if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
122 }
123 else
124 ret=(*a);
125
126 if (len != 0)
127 {
128 s=(unsigned char *)Malloc((int)len+1);
129 if (s == NULL)
130 {
131 i=ERR_R_MALLOC_FAILURE;
132 goto err;
133 }
134 memcpy(s,p,(int)len);
135 s[len]='\0';
136 p+=len;
137 }
138 else
139 s=NULL;
140
141 if (ret->data != NULL) Free((char *)ret->data);
142 ret->length=(int)len;
143 ret->data=s;
144 ret->type=tag;
145 if (a != NULL) (*a)=ret;
146 *pp=p;
147 return(ret);
148err:
149 ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,i);
150 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
151 ASN1_STRING_free(ret);
152 return(NULL);
153 }
154
155int i2d_ASN1_bytes(a, pp, tag, xclass)
156ASN1_STRING *a;
157unsigned char **pp;
158int tag;
159int xclass;
160 {
161 int ret,r,constructed;
162 unsigned char *p;
163
164 if (a == NULL) return(0);
165
166 if (tag == V_ASN1_BIT_STRING)
167 return(i2d_ASN1_BIT_STRING(a,pp));
168
169 ret=a->length;
170 r=ASN1_object_size(0,ret,tag);
171 if (pp == NULL) return(r);
172 p= *pp;
173
174 if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
175 constructed=1;
176 else
177 constructed=0;
178 ASN1_put_object(&p,constructed,ret,tag,xclass);
179 memcpy(p,a->data,a->length);
180 p+=a->length;
181 *pp= p;
182 return(r);
183 }
184
185ASN1_STRING *d2i_ASN1_bytes(a, pp, length, Ptag, Pclass)
186ASN1_STRING **a;
187unsigned char **pp;
188long length;
189int Ptag;
190int Pclass;
191 {
192 ASN1_STRING *ret=NULL;
193 unsigned char *p,*s;
194 long len;
195 int inf,tag,xclass;
196 int i=0;
197
198 if ((a == NULL) || ((*a) == NULL))
199 {
200 if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
201 }
202 else
203 ret=(*a);
204
205 p= *pp;
206 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
207 if (inf & 0x80)
208 {
209 i=ASN1_R_BAD_OBJECT_HEADER;
210 goto err;
211 }
212
213 if (tag != Ptag)
214 {
215 i=ASN1_R_WRONG_TAG;
216 goto err;
217 }
218
219 if (inf & V_ASN1_CONSTRUCTED)
220 {
221 ASN1_CTX c;
222
223 c.pp=pp;
224 c.p=p;
225 c.inf=inf;
226 c.slen=len;
227 c.tag=Ptag;
228 c.xclass=Pclass;
229 c.max=(length == 0)?0:(p+length);
230 if (!asn1_collate_primative(ret,&c))
231 goto err;
232 else
233 {
234 p=c.p;
235 }
236 }
237 else
238 {
239 if (len != 0)
240 {
241 if ((ret->length < len) || (ret->data == NULL))
242 {
243 if (ret->data != NULL) Free((char *)ret->data);
244 s=(unsigned char *)Malloc((int)len);
245 if (s == NULL)
246 {
247 i=ERR_R_MALLOC_FAILURE;
248 goto err;
249 }
250 }
251 else
252 s=ret->data;
253 memcpy(s,p,(int)len);
254 p+=len;
255 }
256 else
257 {
258 s=NULL;
259 if (ret->data != NULL) Free((char *)ret->data);
260 }
261
262 ret->length=(int)len;
263 ret->data=s;
264 ret->type=Ptag;
265 }
266
267 if (a != NULL) (*a)=ret;
268 *pp=p;
269 return(ret);
270err:
271 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
272 ASN1_STRING_free(ret);
273 ASN1err(ASN1_F_D2I_ASN1_BYTES,i);
274 return(NULL);
275 }
276
277
278/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapes
279 * them into the one struture that is then returned */
280/* There have been a few bug fixes for this function from
281 * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */
282static int asn1_collate_primative(a,c)
283ASN1_STRING *a;
284ASN1_CTX *c;
285 {
286 ASN1_STRING *os=NULL;
287 BUF_MEM b;
288 int num;
289
290 b.length=0;
291 b.max=0;
292 b.data=NULL;
293
294 if (a == NULL)
295 {
296 c->error=ERR_R_PASSED_NULL_PARAMETER;
297 goto err;
298 }
299
300 num=0;
301 for (;;)
302 {
303 if (c->inf & 1)
304 {
305 c->eos=ASN1_check_infinite_end(&c->p,
306 (long)(c->max-c->p));
307 if (c->eos) break;
308 }
309 else
310 {
311 if (c->slen <= 0) break;
312 }
313
314 c->q=c->p;
315 if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass)
316 == NULL)
317 {
318 c->error=ERR_R_ASN1_LIB;
319 goto err;
320 }
321
322 if (!BUF_MEM_grow(&b,num+os->length))
323 {
324 c->error=ERR_R_BUF_LIB;
325 goto err;
326 }
327 memcpy(&(b.data[num]),os->data,os->length);
328 if (!(c->inf & 1))
329 c->slen-=(c->p-c->q);
330 num+=os->length;
331 }
332
333 if (!asn1_Finish(c)) goto err;
334
335 a->length=num;
336 if (a->data != NULL) Free(a->data);
337 a->data=(unsigned char *)b.data;
338 if (os != NULL) ASN1_STRING_free(os);
339 return(1);
340err:
341 ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,c->error);
342 if (os != NULL) ASN1_STRING_free(os);
343 if (b.data != NULL) Free(b.data);
344 return(0);
345 }
346
diff --git a/src/lib/libcrypto/asn1/a_d2i_fp.c b/src/lib/libcrypto/asn1/a_d2i_fp.c
new file mode 100644
index 0000000000..d952836a91
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_d2i_fp.c
@@ -0,0 +1,201 @@
1/* crypto/asn1/a_d2i_fp.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "asn1_mac.h"
63
64#define HEADER_SIZE 8
65
66#ifndef NO_FP_API
67char *ASN1_d2i_fp(xnew,d2i,in,x)
68char *(*xnew)();
69char *(*d2i)();
70FILE *in;
71unsigned char **x;
72 {
73 BIO *b;
74 char *ret;
75
76 if ((b=BIO_new(BIO_s_file())) == NULL)
77 {
78 ASN1err(ASN1_F_ASN1_D2I_FP,ERR_R_BUF_LIB);
79 return(NULL);
80 }
81 BIO_set_fp(b,in,BIO_NOCLOSE);
82 ret=ASN1_d2i_bio(xnew,d2i,b,x);
83 BIO_free(b);
84 return(ret);
85 }
86#endif
87
88char *ASN1_d2i_bio(xnew,d2i,in,x)
89char *(*xnew)();
90char *(*d2i)();
91BIO *in;
92unsigned char **x;
93 {
94 BUF_MEM *b;
95 unsigned char *p;
96 int i;
97 char *ret=NULL;
98 ASN1_CTX c;
99 int want=HEADER_SIZE;
100 int eos=0;
101 int off=0;
102 int len=0;
103
104 b=BUF_MEM_new();
105 if (b == NULL)
106 {
107 ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE);
108 return(NULL);
109 }
110
111 ERR_clear_error();
112 for (;;)
113 {
114 if (want >= (len-off))
115 {
116 want-=(len-off);
117
118 if (!BUF_MEM_grow(b,len+want))
119 {
120 ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE);
121 goto err;
122 }
123 i=BIO_read(in,&(b->data[len]),want);
124 if ((i < 0) && ((len-off) == 0))
125 {
126 ASN1err(ASN1_F_ASN1_D2I_BIO,ASN1_R_NOT_ENOUGH_DATA);
127 goto err;
128 }
129 if (i > 0)
130 len+=i;
131 }
132 /* else data already loaded */
133
134 p=(unsigned char *)&(b->data[off]);
135 c.p=p;
136 c.inf=ASN1_get_object(&(c.p),&(c.slen),&(c.tag),&(c.xclass),
137 len-off);
138 if (c.inf & 0x80)
139 {
140 unsigned long e;
141
142 e=ERR_GET_REASON(ERR_peek_error());
143 if (e != ASN1_R_TOO_LONG)
144 goto err;
145 else
146 ERR_get_error(); /* clear error */
147 }
148 i=c.p-p;/* header length */
149 off+=i; /* end of data */
150
151 if (c.inf & 1)
152 {
153 /* no data body so go round again */
154 eos++;
155 want=HEADER_SIZE;
156 }
157 else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC))
158 {
159 /* eos value, so go back and read another header */
160 eos--;
161 if (eos <= 0)
162 break;
163 else
164 want=HEADER_SIZE;
165 }
166 else
167 {
168 /* suck in c.slen bytes of data */
169 want=(int)c.slen;
170 if (want > (len-off))
171 {
172 want-=(len-off);
173 if (!BUF_MEM_grow(b,len+want))
174 {
175 ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE);
176 goto err;
177 }
178 i=BIO_read(in,&(b->data[len]),want);
179 if (i <= 0)
180 {
181 ASN1err(ASN1_F_ASN1_D2I_BIO,ASN1_R_NOT_ENOUGH_DATA);
182 goto err;
183 }
184 len+=i;
185 }
186 off+=(int)c.slen;
187 if (eos <= 0)
188 {
189 break;
190 }
191 else
192 want=HEADER_SIZE;
193 }
194 }
195
196 p=(unsigned char *)b->data;
197 ret=d2i(x,&p,off);
198err:
199 if (b != NULL) BUF_MEM_free(b);
200 return(ret);
201 }
diff --git a/src/lib/libcrypto/asn1/a_digest.c b/src/lib/libcrypto/asn1/a_digest.c
new file mode 100644
index 0000000000..8ddb65b0dc
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_digest.c
@@ -0,0 +1,91 @@
1/* crypto/asn1/a_digest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include <sys/types.h>
62#include <sys/stat.h>
63
64#include "cryptlib.h"
65#include "evp.h"
66#include "x509.h"
67#include "buffer.h"
68
69int ASN1_digest(i2d,type,data,md,len)
70int (*i2d)();
71EVP_MD *type;
72char *data;
73unsigned char *md;
74unsigned int *len;
75 {
76 EVP_MD_CTX ctx;
77 int i;
78 unsigned char *str,*p;
79
80 i=i2d(data,NULL);
81 if ((str=(unsigned char *)Malloc(i)) == NULL) return(0);
82 p=str;
83 i2d(data,&p);
84
85 EVP_DigestInit(&ctx,type);
86 EVP_DigestUpdate(&ctx,str,i);
87 EVP_DigestFinal(&ctx,md,len);
88 Free(str);
89 return(1);
90 }
91
diff --git a/src/lib/libcrypto/asn1/a_dup.c b/src/lib/libcrypto/asn1/a_dup.c
new file mode 100644
index 0000000000..961b4cb069
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_dup.c
@@ -0,0 +1,86 @@
1/* crypto/asn1/a_dup.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63#define READ_CHUNK 2048
64
65char *ASN1_dup(i2d,d2i,x)
66int (*i2d)();
67char *(*d2i)();
68char *x;
69 {
70 unsigned char *b,*p;
71 long i;
72 char *ret;
73
74 if (x == NULL) return(NULL);
75
76 i=(long)i2d(x,NULL);
77 b=(unsigned char *)Malloc((unsigned int)i+10);
78 if (b == NULL)
79 { ASN1err(ASN1_F_ASN1_DUP,ERR_R_MALLOC_FAILURE); return(NULL); }
80 p= b;
81 i=i2d(x,&p);
82 p= b;
83 ret=d2i(NULL,&p,i);
84 Free((char *)b);
85 return(ret);
86 }
diff --git a/src/lib/libcrypto/asn1/a_i2d_fp.c b/src/lib/libcrypto/asn1/a_i2d_fp.c
new file mode 100644
index 0000000000..66c3df68d5
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_i2d_fp.c
@@ -0,0 +1,119 @@
1/* crypto/asn1/a_i2d_fp.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "asn1_mac.h"
63
64#ifndef NO_FP_API
65int ASN1_i2d_fp(i2d,out,x)
66int (*i2d)();
67FILE *out;
68unsigned char *x;
69 {
70 BIO *b;
71 int ret;
72
73 if ((b=BIO_new(BIO_s_file())) == NULL)
74 {
75 ASN1err(ASN1_F_ASN1_I2D_FP,ERR_R_BUF_LIB);
76 return(0);
77 }
78 BIO_set_fp(b,out,BIO_NOCLOSE);
79 ret=ASN1_i2d_bio(i2d,b,x);
80 BIO_free(b);
81 return(ret);
82 }
83#endif
84
85int ASN1_i2d_bio(i2d,out,x)
86int (*i2d)();
87BIO *out;
88unsigned char *x;
89 {
90 char *b;
91 unsigned char *p;
92 int i,j=0,n,ret=1;
93
94 n=i2d(x,NULL);
95 b=(char *)Malloc(n);
96 if (b == NULL)
97 {
98 ASN1err(ASN1_F_ASN1_I2D_BIO,ERR_R_MALLOC_FAILURE);
99 return(0);
100 }
101
102 p=(unsigned char *)b;
103 i2d(x,&p);
104
105 for (;;)
106 {
107 i=BIO_write(out,&(b[j]),n);
108 if (i == n) break;
109 if (i <= 0)
110 {
111 ret=0;
112 break;
113 }
114 j+=i;
115 n-=i;
116 }
117 Free((char *)b);
118 return(ret);
119 }
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c
new file mode 100644
index 0000000000..df79cf99bb
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_int.c
@@ -0,0 +1,305 @@
1/* crypto/asn1/a_int.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62
63/* ASN1err(ASN1_F_D2I_ASN1_INTEGER,ASN1_R_EXPECTING_AN_INTEGER);
64 */
65
66int i2d_ASN1_INTEGER(a,pp)
67ASN1_INTEGER *a;
68unsigned char **pp;
69 {
70 int pad=0,ret,r,i,t;
71 unsigned char *p,*pt,*n,pb=0;
72
73 if ((a == NULL) || (a->data == NULL)) return(0);
74 t=a->type;
75 if (a->length == 0)
76 ret=1;
77 else
78 {
79 ret=a->length;
80 i=a->data[0];
81 if ((t == V_ASN1_INTEGER) && (i > 127))
82 {
83 pad=1;
84 pb=0;
85 }
86 else if ((t == V_ASN1_NEG_INTEGER) && (i>128))
87 {
88 pad=1;
89 pb=0xFF;
90 }
91 ret+=pad;
92 }
93 r=ASN1_object_size(0,ret,V_ASN1_INTEGER);
94 if (pp == NULL) return(r);
95 p= *pp;
96
97 ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);
98 if (pad) *(p++)=pb;
99 if (a->length == 0)
100 *(p++)=0;
101 else if (t == V_ASN1_INTEGER)
102 {
103 memcpy(p,a->data,(unsigned int)a->length);
104 p+=a->length;
105 }
106 else
107 {
108 n=a->data;
109 pt=p;
110 for (i=a->length; i>0; i--)
111 *(p++)= (*(n++)^0xFF)+1;
112 if (!pad) *pt|=0x80;
113 }
114
115 *pp=p;
116 return(r);
117 }
118
119ASN1_INTEGER *d2i_ASN1_INTEGER(a, pp, length)
120ASN1_INTEGER **a;
121unsigned char **pp;
122long length;
123 {
124 ASN1_INTEGER *ret=NULL;
125 unsigned char *p,*to,*s;
126 long len;
127 int inf,tag,xclass;
128 int i;
129
130 if ((a == NULL) || ((*a) == NULL))
131 {
132 if ((ret=ASN1_INTEGER_new()) == NULL) return(NULL);
133 ret->type=V_ASN1_INTEGER;
134 }
135 else
136 ret=(*a);
137
138 p= *pp;
139 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
140 if (inf & 0x80)
141 {
142 i=ASN1_R_BAD_OBJECT_HEADER;
143 goto err;
144 }
145
146 if (tag != V_ASN1_INTEGER)
147 {
148 i=ASN1_R_EXPECTING_AN_INTEGER;
149 goto err;
150 }
151
152 /* We must Malloc stuff, even for 0 bytes otherwise it
153 * signifies a missing NULL parameter. */
154 s=(unsigned char *)Malloc((int)len+1);
155 if (s == NULL)
156 {
157 i=ERR_R_MALLOC_FAILURE;
158 goto err;
159 }
160 to=s;
161 if (*p & 0x80) /* a negative number */
162 {
163 ret->type=V_ASN1_NEG_INTEGER;
164 if (*p == 0xff)
165 {
166 p++;
167 len--;
168 }
169 for (i=(int)len; i>0; i--)
170 *(to++)= (*(p++)^0xFF)+1;
171 }
172 else
173 {
174 ret->type=V_ASN1_INTEGER;
175 if ((*p == 0) && (len != 1))
176 {
177 p++;
178 len--;
179 }
180 memcpy(s,p,(int)len);
181 p+=len;
182 }
183
184 if (ret->data != NULL) Free((char *)ret->data);
185 ret->data=s;
186 ret->length=(int)len;
187 if (a != NULL) (*a)=ret;
188 *pp=p;
189 return(ret);
190err:
191 ASN1err(ASN1_F_D2I_ASN1_INTEGER,i);
192 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
193 ASN1_INTEGER_free(ret);
194 return(NULL);
195 }
196
197int ASN1_INTEGER_set(a,v)
198ASN1_INTEGER *a;
199long v;
200 {
201 int i,j,k;
202 unsigned char buf[sizeof(long)+1];
203 long d;
204
205 a->type=V_ASN1_INTEGER;
206 if (a->length < (sizeof(long)+1))
207 {
208 if (a->data != NULL)
209 Free((char *)a->data);
210 if ((a->data=(unsigned char *)Malloc(sizeof(long)+1)) != NULL)
211 memset((char *)a->data,0,sizeof(long)+1);
212 }
213 if (a->data == NULL)
214 {
215 ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE);
216 return(0);
217 }
218 d=v;
219 if (d < 0)
220 {
221 d= -d;
222 a->type=V_ASN1_NEG_INTEGER;
223 }
224
225 for (i=0; i<sizeof(long); i++)
226 {
227 if (d == 0) break;
228 buf[i]=(int)d&0xff;
229 d>>=8;
230 }
231 j=0;
232 if (v < 0) a->data[j++]=0;
233 for (k=i-1; k >=0; k--)
234 a->data[j++]=buf[k];
235 a->length=j;
236 return(1);
237 }
238
239long ASN1_INTEGER_get(a)
240ASN1_INTEGER *a;
241 {
242 int neg=0,i;
243 long r=0;
244
245 if (a == NULL) return(0L);
246 i=a->type;
247 if (i == V_ASN1_NEG_INTEGER)
248 neg=1;
249 else if (i != V_ASN1_INTEGER)
250 return(0);
251
252 if (a->length > sizeof(long))
253 {
254 /* hmm... a bit ugly */
255 return(0xffffffffL);
256 }
257 if (a->data == NULL)
258 return(0);
259
260 for (i=0; i<a->length; i++)
261 {
262 r<<=8;
263 r|=(unsigned char)a->data[i];
264 }
265 if (neg) r= -r;
266 return(r);
267 }
268
269ASN1_INTEGER *BN_to_ASN1_INTEGER(bn,ai)
270BIGNUM *bn;
271ASN1_INTEGER *ai;
272 {
273 ASN1_INTEGER *ret;
274 int len,j;
275
276 if (ai == NULL)
277 ret=ASN1_INTEGER_new();
278 else
279 ret=ai;
280 if (ret == NULL)
281 {
282 ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ASN1_R_ERROR_STACK);
283 goto err;
284 }
285 ret->type=V_ASN1_INTEGER;
286 j=BN_num_bits(bn);
287 len=((j == 0)?0:((j/8)+1));
288 ret->data=(unsigned char *)Malloc(len+4);
289 ret->length=BN_bn2bin(bn,ret->data);
290 return(ret);
291err:
292 if (ret != ai) ASN1_INTEGER_free(ret);
293 return(NULL);
294 }
295
296BIGNUM *ASN1_INTEGER_to_BN(ai,bn)
297ASN1_INTEGER *ai;
298BIGNUM *bn;
299 {
300 BIGNUM *ret;
301
302 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
303 ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);
304 return(ret);
305 }
diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c
new file mode 100644
index 0000000000..5a7eeef8d8
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_object.c
@@ -0,0 +1,389 @@
1/* crypto/asn1/a_object.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "asn1.h"
63#include "objects.h"
64
65/* ASN1err(ASN1_F_ASN1_OBJECT_NEW,ASN1_R_EXPECTING_AN_OBJECT);
66 * ASN1err(ASN1_F_D2I_ASN1_OBJECT,ASN1_R_BAD_OBJECT_HEADER);
67 * ASN1err(ASN1_F_I2T_ASN1_OBJECT,ASN1_R_BAD_OBJECT_HEADER);
68 */
69
70int i2d_ASN1_OBJECT(a, pp)
71ASN1_OBJECT *a;
72unsigned char **pp;
73 {
74 unsigned char *p;
75
76 if ((a == NULL) || (a->data == NULL)) return(0);
77
78 if (pp == NULL)
79 return(ASN1_object_size(0,a->length,V_ASN1_OBJECT));
80
81 p= *pp;
82 ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
83 memcpy(p,a->data,a->length);
84 p+=a->length;
85
86 *pp=p;
87 return(a->length);
88 }
89
90int a2d_ASN1_OBJECT(out,olen,buf,num)
91unsigned char *out;
92int olen;
93char *buf;
94int num;
95 {
96 int i,first,len=0,c;
97 char tmp[24],*p;
98 unsigned long l;
99
100 if (num == 0)
101 return(0);
102 else if (num == -1)
103 num=strlen(buf);
104
105 p=buf;
106 c= *(p++);
107 num--;
108 if ((c >= '0') && (c <= '2'))
109 {
110 first=(c-'0')*40;
111 }
112 else
113 {
114 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE);
115 goto err;
116 }
117
118 if (num <= 0)
119 {
120 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER);
121 goto err;
122 }
123 c= *(p++);
124 num--;
125 for (;;)
126 {
127 if (num <= 0) break;
128 if ((c != '.') && (c != ' '))
129 {
130 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR);
131 goto err;
132 }
133 l=0;
134 for (;;)
135 {
136 if (num <= 0) break;
137 num--;
138 c= *(p++);
139 if ((c == ' ') || (c == '.'))
140 break;
141 if ((c < '0') || (c > '9'))
142 {
143 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);
144 goto err;
145 }
146 l=l*10L+(long)(c-'0');
147 }
148 if (len == 0)
149 {
150 if ((first < 2) && (l >= 40))
151 {
152 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE);
153 goto err;
154 }
155 l+=(long)first;
156 }
157 i=0;
158 for (;;)
159 {
160 tmp[i++]=(unsigned char)l&0x7f;
161 l>>=7L;
162 if (l == 0L) break;
163 }
164 if (out != NULL)
165 {
166 if (len+i > olen)
167 {
168 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL);
169 goto err;
170 }
171 while (--i > 0)
172 out[len++]=tmp[i]|0x80;
173 out[len++]=tmp[0];
174 }
175 else
176 len+=i;
177 }
178 return(len);
179err:
180 return(0);
181 }
182
183int i2t_ASN1_OBJECT(buf,buf_len,a)
184char *buf;
185int buf_len;
186ASN1_OBJECT *a;
187 {
188 int i,idx=0,n=0,len,nid;
189 unsigned long l;
190 unsigned char *p;
191 char *s;
192 char tbuf[32];
193
194 if (buf_len <= 0) return(0);
195
196 if ((a == NULL) || (a->data == NULL))
197 {
198 buf[0]='\0';
199 return(0);
200 }
201
202 nid=OBJ_obj2nid(a);
203 if (nid == NID_undef)
204 {
205 len=a->length;
206 p=a->data;
207
208 idx=0;
209 l=0;
210 while (idx < a->length)
211 {
212 l|=(p[idx]&0x7f);
213 if (!(p[idx] & 0x80)) break;
214 l<<=7L;
215 idx++;
216 }
217 idx++;
218 i=(int)(l/40);
219 if (i > 2) i=2;
220 l-=(long)(i*40);
221
222 sprintf(tbuf,"%d.%ld",i,l);
223 i=strlen(tbuf);
224 strncpy(buf,tbuf,buf_len);
225 buf_len-=i;
226 buf+=i;
227 n+=i;
228
229 l=0;
230 for (; idx<len; idx++)
231 {
232 l|=p[idx]&0x7f;
233 if (!(p[idx] & 0x80))
234 {
235 sprintf(tbuf,".%ld",l);
236 i=strlen(tbuf);
237 if (buf_len > 0)
238 strncpy(buf,tbuf,buf_len);
239 buf_len-=i;
240 buf+=i;
241 n+=i;
242 l=0;
243 }
244 l<<=7L;
245 }
246 }
247 else
248 {
249 s=(char *)OBJ_nid2ln(nid);
250 if (s == NULL)
251 s=(char *)OBJ_nid2sn(nid);
252 strncpy(buf,s,buf_len);
253 n=strlen(s);
254 }
255 buf[buf_len-1]='\0';
256 return(n);
257 }
258
259int i2a_ASN1_OBJECT(bp,a)
260BIO *bp;
261ASN1_OBJECT *a;
262 {
263 char buf[80];
264 int i;
265
266 if ((a == NULL) || (a->data == NULL))
267 return(BIO_write(bp,"NULL",4));
268 i=i2t_ASN1_OBJECT(buf,80,a);
269 if (i > 80) i=80;
270 BIO_write(bp,buf,i);
271 return(i);
272 }
273
274ASN1_OBJECT *d2i_ASN1_OBJECT(a, pp, length)
275ASN1_OBJECT **a;
276unsigned char **pp;
277long length;
278 {
279 ASN1_OBJECT *ret=NULL;
280 unsigned char *p;
281 long len;
282 int tag,xclass;
283 int inf,i;
284
285 /* only the ASN1_OBJECTs from the 'table' will have values
286 * for ->sn or ->ln */
287 if ((a == NULL) || ((*a) == NULL) ||
288 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
289 {
290 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
291 }
292 else ret=(*a);
293
294 p= *pp;
295
296 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
297 if (inf & 0x80)
298 {
299 i=ASN1_R_BAD_OBJECT_HEADER;
300 goto err;
301 }
302
303 if (tag != V_ASN1_OBJECT)
304 {
305 i=ASN1_R_EXPECTING_AN_OBJECT;
306 goto err;
307 }
308 if ((ret->data == NULL) || (ret->length < len))
309 {
310 if (ret->data != NULL) Free((char *)ret->data);
311 ret->data=(unsigned char *)Malloc((int)len);
312 ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
313 if (ret->data == NULL)
314 { i=ERR_R_MALLOC_FAILURE; goto err; }
315 }
316 memcpy(ret->data,p,(int)len);
317 ret->length=(int)len;
318 ret->sn=NULL;
319 ret->ln=NULL;
320 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
321 p+=len;
322
323 if (a != NULL) (*a)=ret;
324 *pp=p;
325 return(ret);
326err:
327 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
328 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
329 ASN1_OBJECT_free(ret);
330 return(NULL);
331 }
332
333ASN1_OBJECT *ASN1_OBJECT_new()
334 {
335 ASN1_OBJECT *ret;
336
337 ret=(ASN1_OBJECT *)Malloc(sizeof(ASN1_OBJECT));
338 if (ret == NULL)
339 {
340 ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE);
341 return(NULL);
342 }
343 ret->length=0;
344 ret->data=NULL;
345 ret->nid=0;
346 ret->sn=NULL;
347 ret->ln=NULL;
348 ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
349 return(ret);
350 }
351
352void ASN1_OBJECT_free(a)
353ASN1_OBJECT *a;
354 {
355 if (a == NULL) return;
356 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
357 {
358 if (a->sn != NULL) Free(a->sn);
359 if (a->ln != NULL) Free(a->ln);
360 a->sn=a->ln=NULL;
361 }
362 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
363 {
364 if (a->data != NULL) Free(a->data);
365 a->data=NULL;
366 a->length=0;
367 }
368 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
369 Free((char *)a);
370 }
371
372ASN1_OBJECT *ASN1_OBJECT_create(nid,data,len,sn,ln)
373int nid;
374unsigned char *data;
375int len;
376char *sn,*ln;
377 {
378 ASN1_OBJECT o;
379
380 o.sn=sn;
381 o.ln=ln;
382 o.data=data;
383 o.nid=nid;
384 o.length=len;
385 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|
386 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA;
387 return(OBJ_dup(&o));
388 }
389
diff --git a/src/lib/libcrypto/asn1/a_octet.c b/src/lib/libcrypto/asn1/a_octet.c
new file mode 100644
index 0000000000..be3f172a8c
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_octet.c
@@ -0,0 +1,90 @@
1/* crypto/asn1/a_octet.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62
63/* ASN1err(ASN1_F_D2I_ASN1_OCTET_STRING,ASN1_R_EXPECTING_AN_OCTET_STRING);
64 */
65
66int i2d_ASN1_OCTET_STRING(a, pp)
67ASN1_OCTET_STRING *a;
68unsigned char **pp;
69 {
70 return(i2d_ASN1_bytes((ASN1_STRING *)a,pp,
71 V_ASN1_OCTET_STRING,V_ASN1_UNIVERSAL));
72 }
73
74ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(a, pp, length)
75ASN1_OCTET_STRING **a;
76unsigned char **pp;
77long length;
78 {
79 ASN1_OCTET_STRING *ret=NULL;
80
81 ret=(ASN1_OCTET_STRING *)d2i_ASN1_bytes((ASN1_STRING **)a,
82 pp,length,V_ASN1_OCTET_STRING,V_ASN1_UNIVERSAL);
83 if (ret == NULL)
84 {
85 ASN1err(ASN1_F_D2I_ASN1_OCTET_STRING,ASN1_R_ERROR_STACK);
86 return(NULL);
87 }
88 return(ret);
89 }
90
diff --git a/src/lib/libcrypto/asn1/a_print.c b/src/lib/libcrypto/asn1/a_print.c
new file mode 100644
index 0000000000..3023361dee
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_print.c
@@ -0,0 +1,161 @@
1/* crypto/asn1/a_print.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62
63/* ASN1err(ASN1_F_D2I_ASN1_PRINT_TYPE,ASN1_R_WRONG_PRINTABLE_TYPE);
64 * ASN1err(ASN1_F_D2I_ASN1_PRINT_TYPE,ASN1_R_TAG_VALUE_TOO_HIGH);
65 */
66
67int i2d_ASN1_IA5STRING(a,pp)
68ASN1_IA5STRING *a;
69unsigned char **pp;
70 { return(M_i2d_ASN1_IA5STRING(a,pp)); }
71
72ASN1_IA5STRING *d2i_ASN1_IA5STRING(a,pp,l)
73ASN1_IA5STRING **a;
74unsigned char **pp;
75long l;
76 { return(M_d2i_ASN1_IA5STRING(a,pp,l)); }
77
78ASN1_T61STRING *d2i_ASN1_T61STRING(a,pp,l)
79ASN1_T61STRING **a;
80unsigned char **pp;
81long l;
82 { return(M_d2i_ASN1_T61STRING(a,pp,l)); }
83
84ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(a,pp,l)
85ASN1_PRINTABLESTRING **a;
86unsigned char **pp;
87long l;
88 { return(M_d2i_ASN1_PRINTABLESTRING(a,pp,l)); }
89
90int i2d_ASN1_PRINTABLE(a,pp)
91ASN1_STRING *a;
92unsigned char **pp;
93 { return(M_i2d_ASN1_PRINTABLE(a,pp)); }
94
95ASN1_STRING *d2i_ASN1_PRINTABLE(a,pp,l)
96ASN1_STRING **a;
97unsigned char **pp;
98long l;
99 { return(M_d2i_ASN1_PRINTABLE(a,pp,l)); }
100
101int ASN1_PRINTABLE_type(s,len)
102unsigned char *s;
103int len;
104 {
105 int c;
106 int ia5=0;
107 int t61=0;
108
109 if (len <= 0) len= -1;
110 if (s == NULL) return(V_ASN1_PRINTABLESTRING);
111
112 while ((*s) && (len-- != 0))
113 {
114 c= *(s++);
115 if (!( ((c >= 'a') && (c <= 'z')) ||
116 ((c >= 'A') && (c <= 'Z')) ||
117 (c == ' ') ||
118 ((c >= '0') && (c <= '9')) ||
119 (c == ' ') || (c == '\'') ||
120 (c == '(') || (c == ')') ||
121 (c == '+') || (c == ',') ||
122 (c == '-') || (c == '.') ||
123 (c == '/') || (c == ':') ||
124 (c == '=') || (c == '?')))
125 ia5=1;
126 if (c&0x80)
127 t61=1;
128 }
129 if (t61) return(V_ASN1_T61STRING);
130 if (ia5) return(V_ASN1_IA5STRING);
131 return(V_ASN1_PRINTABLESTRING);
132 }
133
134int ASN1_UNIVERSALSTRING_to_string(s)
135ASN1_UNIVERSALSTRING *s;
136 {
137 int i;
138 unsigned char *p;
139
140 if (s->type != V_ASN1_UNIVERSALSTRING) return(0);
141 if ((s->length%4) != 0) return(0);
142 p=s->data;
143 for (i=0; i<s->length; i+=4)
144 {
145 if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
146 break;
147 else
148 p+=4;
149 }
150 if (i < s->length) return(0);
151 p=s->data;
152 for (i=3; i<s->length; i+=4)
153 {
154 *(p++)=s->data[i];
155 }
156 *(p)='\0';
157 s->length/=4;
158 s->type=ASN1_PRINTABLE_type(s->data,s->length);
159 return(1);
160 }
161
diff --git a/src/lib/libcrypto/asn1/a_set.c b/src/lib/libcrypto/asn1/a_set.c
new file mode 100644
index 0000000000..17c49946cf
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_set.c
@@ -0,0 +1,149 @@
1/* crypto/asn1/a_set.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ERR_R_MALLOC_FAILURE);
64 */
65
66int i2d_ASN1_SET(a,pp,func,ex_tag,ex_class)
67STACK *a;
68unsigned char **pp;
69int (*func)();
70int ex_tag;
71int ex_class;
72 {
73 int ret=0,r;
74 int i;
75 unsigned char *p;
76
77 if (a == NULL) return(0);
78 for (i=sk_num(a)-1; i>=0; i--)
79 ret+=func(sk_value(a,i),NULL);
80 r=ASN1_object_size(1,ret,ex_tag);
81 if (pp == NULL) return(r);
82
83 p= *pp;
84 ASN1_put_object(&p,1,ret,ex_tag,ex_class);
85 for (i=0; i<sk_num(a); i++)
86 func(sk_value(a,i),&p);
87
88 *pp=p;
89 return(r);
90 }
91
92STACK *d2i_ASN1_SET(a,pp,length,func,ex_tag,ex_class)
93STACK **a;
94unsigned char **pp;
95long length;
96char *(*func)();
97int ex_tag;
98int ex_class;
99 {
100 ASN1_CTX c;
101 STACK *ret=NULL;
102
103 if ((a == NULL) || ((*a) == NULL))
104 { if ((ret=sk_new(NULL)) == NULL) goto err; }
105 else
106 ret=(*a);
107
108 c.p= *pp;
109 c.max=(length == 0)?0:(c.p+length);
110
111 c.inf=ASN1_get_object(&c.p,&c.slen,&c.tag,&c.xclass,c.max-c.p);
112 if (c.inf & 0x80) goto err;
113 if (ex_class != c.xclass)
114 {
115 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_CLASS);
116 goto err;
117 }
118 if (ex_tag != c.tag)
119 {
120 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_TAG);
121 goto err;
122 }
123 if ((c.slen+c.p) > c.max)
124 {
125 ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_LENGTH_ERROR);
126 goto err;
127 }
128 /* check for infinite constructed - it can be as long
129 * as the amount of data passed to us */
130 if (c.inf == (V_ASN1_CONSTRUCTED+1))
131 c.slen=length+ *pp-c.p;
132 c.max=c.p+c.slen;
133
134 while (c.p < c.max)
135 {
136 char *s;
137
138 if (M_ASN1_D2I_end_sequence()) break;
139 if ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL) goto err;
140 if (!sk_push(ret,s)) goto err;
141 }
142 if (a != NULL) (*a)=ret;
143 *pp=c.p;
144 return(ret);
145err:
146 if ((ret != NULL) && ((a == NULL) || (*a != ret))) sk_free(ret);
147 return(NULL);
148 }
149
diff --git a/src/lib/libcrypto/asn1/a_sign.c b/src/lib/libcrypto/asn1/a_sign.c
new file mode 100644
index 0000000000..02188e68c4
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_sign.c
@@ -0,0 +1,147 @@
1/* crypto/asn1/a_sign.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include <sys/types.h>
62#include <sys/stat.h>
63
64#include "cryptlib.h"
65#include "bn.h"
66#include "evp.h"
67#include "x509.h"
68#include "objects.h"
69#include "buffer.h"
70#include "pem.h"
71
72int ASN1_sign(i2d,algor1,algor2,signature,data,pkey,type)
73int (*i2d)();
74X509_ALGOR *algor1;
75X509_ALGOR *algor2;
76ASN1_BIT_STRING *signature;
77char *data;
78EVP_PKEY *pkey;
79EVP_MD *type;
80 {
81 EVP_MD_CTX ctx;
82 unsigned char *p,*buf_in=NULL,*buf_out=NULL;
83 int i,inl=0,outl=0,outll=0;
84 X509_ALGOR *a;
85
86 for (i=0; i<2; i++)
87 {
88 if (i == 0)
89 a=algor1;
90 else
91 a=algor2;
92 if (a == NULL) continue;
93 if ( (a->parameter == NULL) ||
94 (a->parameter->type != V_ASN1_NULL))
95 {
96 ASN1_TYPE_free(a->parameter);
97 if ((a->parameter=ASN1_TYPE_new()) == NULL) goto err;
98 a->parameter->type=V_ASN1_NULL;
99 }
100 ASN1_OBJECT_free(a->algorithm);
101 a->algorithm=OBJ_nid2obj(type->pkey_type);
102 if (a->algorithm == NULL)
103 {
104 ASN1err(ASN1_F_ASN1_SIGN,ASN1_R_UNKNOWN_OBJECT_TYPE);
105 goto err;
106 }
107 if (a->algorithm->length == 0)
108 {
109 ASN1err(ASN1_F_ASN1_SIGN,ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
110 goto err;
111 }
112 }
113 inl=i2d(data,NULL);
114 buf_in=(unsigned char *)Malloc((unsigned int)inl);
115 outll=outl=EVP_PKEY_size(pkey);
116 buf_out=(unsigned char *)Malloc((unsigned int)outl);
117 if ((buf_in == NULL) || (buf_out == NULL))
118 {
119 outl=0;
120 ASN1err(ASN1_F_ASN1_SIGN,ERR_R_MALLOC_FAILURE);
121 goto err;
122 }
123 p=buf_in;
124
125 i2d(data,&p);
126 EVP_SignInit(&ctx,type);
127 EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl);
128 if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out,
129 (unsigned int *)&outl,pkey))
130 {
131 outl=0;
132 ASN1err(ASN1_F_ASN1_SIGN,ERR_R_EVP_LIB);
133 goto err;
134 }
135 if (signature->data != NULL) Free((char *)signature->data);
136 signature->data=buf_out;
137 buf_out=NULL;
138 signature->length=outl;
139
140err:
141 memset(&ctx,0,sizeof(ctx));
142 if (buf_in != NULL)
143 { memset((char *)buf_in,0,(unsigned int)inl); Free((char *)buf_in); }
144 if (buf_out != NULL)
145 { memset((char *)buf_out,0,outll); Free((char *)buf_out); }
146 return(outl);
147 }
diff --git a/src/lib/libcrypto/asn1/a_type.c b/src/lib/libcrypto/asn1/a_type.c
new file mode 100644
index 0000000000..7c0004084c
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_type.c
@@ -0,0 +1,325 @@
1/* crypto/asn1/a_type.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ASN1_R_ERROR_STACK);
64 * ASN1err(ASN1_F_D2I_ASN1_BYTES,ASN1_R_ERROR_STACK);
65 * ASN1err(ASN1_F_D2I_ASN1_BYTES,ASN1_R_WRONG_TAG);
66 * ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,ASN1_R_WRONG_TAG);
67 */
68
69#ifndef NOPROTO
70static void ASN1_TYPE_component_free(ASN1_TYPE *a);
71#else
72static void ASN1_TYPE_component_free();
73#endif
74
75int i2d_ASN1_TYPE(a,pp)
76ASN1_TYPE *a;
77unsigned char **pp;
78 {
79 int r=0;
80
81 if (a == NULL) return(0);
82
83 switch (a->type)
84 {
85 case V_ASN1_NULL:
86 if (pp != NULL)
87 ASN1_put_object(pp,0,0,V_ASN1_NULL,V_ASN1_UNIVERSAL);
88 r=2;
89 break;
90 case V_ASN1_INTEGER:
91 case V_ASN1_NEG_INTEGER:
92 r=i2d_ASN1_INTEGER(a->value.integer,pp);
93 break;
94 case V_ASN1_BIT_STRING:
95 r=i2d_ASN1_BIT_STRING(a->value.bit_string,pp);
96 break;
97 case V_ASN1_OCTET_STRING:
98 r=i2d_ASN1_OCTET_STRING(a->value.octet_string,pp);
99 break;
100 case V_ASN1_OBJECT:
101 r=i2d_ASN1_OBJECT(a->value.object,pp);
102 break;
103 case V_ASN1_PRINTABLESTRING:
104 r=M_i2d_ASN1_PRINTABLESTRING(a->value.printablestring,pp);
105 break;
106 case V_ASN1_T61STRING:
107 r=M_i2d_ASN1_T61STRING(a->value.t61string,pp);
108 break;
109 case V_ASN1_IA5STRING:
110 r=M_i2d_ASN1_IA5STRING(a->value.ia5string,pp);
111 break;
112 case V_ASN1_GENERALSTRING:
113 r=M_i2d_ASN1_GENERALSTRING(a->value.generalstring,pp);
114 break;
115 case V_ASN1_UNIVERSALSTRING:
116 r=M_i2d_ASN1_UNIVERSALSTRING(a->value.universalstring,pp);
117 break;
118 case V_ASN1_BMPSTRING:
119 r=M_i2d_ASN1_BMPSTRING(a->value.bmpstring,pp);
120 break;
121 case V_ASN1_UTCTIME:
122 r=i2d_ASN1_UTCTIME(a->value.utctime,pp);
123 break;
124 case V_ASN1_SET:
125 case V_ASN1_SEQUENCE:
126 if (a->value.set == NULL)
127 r=0;
128 else
129 {
130 r=a->value.set->length;
131 if (pp != NULL)
132 {
133 memcpy(*pp,a->value.set->data,r);
134 *pp+=r;
135 }
136 }
137 break;
138 }
139 return(r);
140 }
141
142ASN1_TYPE *d2i_ASN1_TYPE(a,pp,length)
143ASN1_TYPE **a;
144unsigned char **pp;
145long length;
146 {
147 ASN1_TYPE *ret=NULL;
148 unsigned char *q,*p,*max;
149 int inf,tag,xclass;
150 long len;
151
152 if ((a == NULL) || ((*a) == NULL))
153 {
154 if ((ret=ASN1_TYPE_new()) == NULL) goto err;
155 }
156 else
157 ret=(*a);
158
159 p= *pp;
160 q=p;
161 max=(p+length);
162
163 inf=ASN1_get_object(&q,&len,&tag,&xclass,length);
164 if (inf & 0x80) goto err;
165
166 ASN1_TYPE_component_free(ret);
167
168 switch (tag)
169 {
170 case V_ASN1_NULL:
171 p=q;
172 ret->value.ptr=NULL;
173 break;
174 case V_ASN1_INTEGER:
175 if ((ret->value.integer=
176 d2i_ASN1_INTEGER(NULL,&p,max-p)) == NULL)
177 goto err;
178 break;
179 case V_ASN1_BIT_STRING:
180 if ((ret->value.bit_string=
181 d2i_ASN1_BIT_STRING(NULL,&p,max-p)) == NULL)
182 goto err;
183 break;
184 case V_ASN1_OCTET_STRING:
185 if ((ret->value.octet_string=
186 d2i_ASN1_OCTET_STRING(NULL,&p,max-p)) == NULL)
187 goto err;
188 break;
189 case V_ASN1_OBJECT:
190 if ((ret->value.object=
191 d2i_ASN1_OBJECT(NULL,&p,max-p)) == NULL)
192 goto err;
193 break;
194 case V_ASN1_PRINTABLESTRING:
195 if ((ret->value.printablestring=
196 d2i_ASN1_PRINTABLESTRING(NULL,&p,max-p)) == NULL)
197 goto err;
198 break;
199 case V_ASN1_T61STRING:
200 if ((ret->value.t61string=
201 M_d2i_ASN1_T61STRING(NULL,&p,max-p)) == NULL)
202 goto err;
203 break;
204 case V_ASN1_IA5STRING:
205 if ((ret->value.ia5string=
206 M_d2i_ASN1_IA5STRING(NULL,&p,max-p)) == NULL)
207 goto err;
208 break;
209 case V_ASN1_GENERALSTRING:
210 if ((ret->value.generalstring=
211 M_d2i_ASN1_GENERALSTRING(NULL,&p,max-p)) == NULL)
212 goto err;
213 break;
214 case V_ASN1_UNIVERSALSTRING:
215 if ((ret->value.universalstring=
216 M_d2i_ASN1_UNIVERSALSTRING(NULL,&p,max-p)) == NULL)
217 goto err;
218 break;
219 case V_ASN1_BMPSTRING:
220 if ((ret->value.bmpstring=
221 M_d2i_ASN1_BMPSTRING(NULL,&p,max-p)) == NULL)
222 goto err;
223 break;
224 case V_ASN1_UTCTIME:
225 if ((ret->value.utctime=
226 d2i_ASN1_UTCTIME(NULL,&p,max-p)) == NULL)
227 goto err;
228 break;
229 case V_ASN1_SET:
230 case V_ASN1_SEQUENCE:
231 /* Sets and sequences are left complete */
232 if ((ret->value.set=ASN1_STRING_new()) == NULL) goto err;
233 ret->value.set->type=tag;
234 len+=(q-p);
235 if (!ASN1_STRING_set(ret->value.set,p,(int)len)) goto err;
236 p+=len;
237 break;
238 default:
239 ASN1err(ASN1_F_D2I_ASN1_TYPE,ASN1_R_BAD_TYPE);
240 goto err;
241 }
242
243 ret->type=tag;
244 if (a != NULL) (*a)=ret;
245 *pp=p;
246 return(ret);
247err:
248 if ((ret != NULL) && ((a == NULL) || (*a != ret))) ASN1_TYPE_free(ret);
249 return(NULL);
250 }
251
252ASN1_TYPE *ASN1_TYPE_new()
253 {
254 ASN1_TYPE *ret=NULL;
255
256 M_ASN1_New_Malloc(ret,ASN1_TYPE);
257 ret->type= -1;
258 ret->value.ptr=NULL;
259 return(ret);
260 M_ASN1_New_Error(ASN1_F_ASN1_TYPE_NEW);
261 }
262
263void ASN1_TYPE_free(a)
264ASN1_TYPE *a;
265 {
266 if (a == NULL) return;
267 ASN1_TYPE_component_free(a);
268 Free((char *)(char *)a);
269 }
270
271int ASN1_TYPE_get(a)
272ASN1_TYPE *a;
273 {
274 if (a->value.ptr != NULL)
275 return(a->type);
276 else
277 return(0);
278 }
279
280void ASN1_TYPE_set(a,type,value)
281ASN1_TYPE *a;
282int type;
283char *value;
284 {
285 if (a->value.ptr != NULL)
286 ASN1_TYPE_component_free(a);
287 a->type=type;
288 a->value.ptr=value;
289 }
290
291static void ASN1_TYPE_component_free(a)
292ASN1_TYPE *a;
293 {
294 if (a == NULL) return;
295
296 if (a->value.ptr != NULL)
297 {
298 switch (a->type)
299 {
300 case V_ASN1_OBJECT:
301 ASN1_OBJECT_free(a->value.object);
302 break;
303 case V_ASN1_INTEGER:
304 case V_ASN1_NEG_INTEGER:
305 case V_ASN1_BIT_STRING:
306 case V_ASN1_OCTET_STRING:
307 case V_ASN1_PRINTABLESTRING:
308 case V_ASN1_T61STRING:
309 case V_ASN1_IA5STRING:
310 case V_ASN1_UNIVERSALSTRING:
311 case V_ASN1_GENERALSTRING:
312 case V_ASN1_UTCTIME:
313 case V_ASN1_SET:
314 case V_ASN1_SEQUENCE:
315 ASN1_STRING_free((ASN1_STRING *)a->value.ptr);
316 break;
317 default:
318 /* MEMORY LEAK */
319 break;
320 }
321 a->type=0;
322 a->value.ptr=NULL;
323 }
324 }
325
diff --git a/src/lib/libcrypto/asn1/a_verify.c b/src/lib/libcrypto/asn1/a_verify.c
new file mode 100644
index 0000000000..03fc63dbef
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_verify.c
@@ -0,0 +1,121 @@
1/* crypto/asn1/a_verify.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include <sys/types.h>
62#include <sys/stat.h>
63
64#include "cryptlib.h"
65#include "bn.h"
66#include "x509.h"
67#include "objects.h"
68#include "buffer.h"
69#include "evp.h"
70#include "pem.h"
71
72int ASN1_verify(i2d,a,signature,data,pkey)
73int (*i2d)();
74X509_ALGOR *a;
75ASN1_BIT_STRING *signature;
76char *data;
77EVP_PKEY *pkey;
78 {
79 EVP_MD_CTX ctx;
80 EVP_MD *type;
81 unsigned char *p,*buf_in=NULL;
82 int ret= -1,i,inl;
83
84 i=OBJ_obj2nid(a->algorithm);
85 type=EVP_get_digestbyname(OBJ_nid2sn(i));
86 if (type == NULL)
87 {
88 ASN1err(ASN1_F_ASN1_VERIFY,ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
89 goto err;
90 }
91
92 inl=i2d(data,NULL);
93 buf_in=(unsigned char *)Malloc((unsigned int)inl);
94 if (buf_in == NULL)
95 {
96 ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_MALLOC_FAILURE);
97 goto err;
98 }
99 p=buf_in;
100
101 i2d(data,&p);
102 EVP_VerifyInit(&ctx,type);
103 EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl);
104
105 memset(buf_in,0,(unsigned int)inl);
106 Free((char *)buf_in);
107
108 if (EVP_VerifyFinal(&ctx,(unsigned char *)signature->data,
109 (unsigned int)signature->length,pkey) <= 0)
110 {
111 ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_EVP_LIB);
112 ret=0;
113 goto err;
114 }
115 /* we don't need to zero the 'ctx' because we just checked
116 * public information */
117 /* memset(&ctx,0,sizeof(ctx)); */
118 ret=1;
119err:
120 return(ret);
121 }
diff --git a/src/lib/libcrypto/asn1/asn1.h b/src/lib/libcrypto/asn1/asn1.h
new file mode 100644
index 0000000000..9793db365d
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1.h
@@ -0,0 +1,859 @@
1/* crypto/asn1/asn1.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_ASN1_H
60#define HEADER_ASN1_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include <time.h>
67#include "bn.h"
68#include "stack.h"
69
70#define V_ASN1_UNIVERSAL 0x00
71#define V_ASN1_APPLICATION 0x40
72#define V_ASN1_CONTEXT_SPECIFIC 0x80
73#define V_ASN1_PRIVATE 0xc0
74
75#define V_ASN1_CONSTRUCTED 0x20
76#define V_ASN1_PRIMATIVE_TAG 0x1f
77
78#define V_ASN1_APP_CHOOSE -2 /* let the recipent choose */
79
80#define V_ASN1_UNDEF -1
81#define V_ASN1_EOC 0
82#define V_ASN1_BOOLEAN 1 /**/
83#define V_ASN1_INTEGER 2
84#define V_ASN1_NEG_INTEGER (2+0x100)
85#define V_ASN1_BIT_STRING 3
86#define V_ASN1_OCTET_STRING 4
87#define V_ASN1_NULL 5
88#define V_ASN1_OBJECT 6
89#define V_ASN1_OBJECT_DESCRIPTOR 7
90#define V_ASN1_EXTERNAL 8
91#define V_ASN1_REAL 9
92#define V_ASN1_ENUMERATED 10 /* microsoft weirdness */
93#define V_ASN1_SEQUENCE 16
94#define V_ASN1_SET 17
95#define V_ASN1_NUMERICSTRING 18 /**/
96#define V_ASN1_PRINTABLESTRING 19
97#define V_ASN1_T61STRING 20
98#define V_ASN1_TELETEXSTRING 20 /* alias */
99#define V_ASN1_VIDEOTEXSTRING 21 /**/
100#define V_ASN1_IA5STRING 22
101#define V_ASN1_UTCTIME 23
102#define V_ASN1_GENERALIZEDTIME 24 /**/
103#define V_ASN1_GRAPHICSTRING 25 /**/
104#define V_ASN1_ISO64STRING 26 /**/
105#define V_ASN1_VISIBLESTRING 26 /* alias */
106#define V_ASN1_GENERALSTRING 27 /**/
107#define V_ASN1_UNIVERSALSTRING 28 /**/
108#define V_ASN1_BMPSTRING 30
109
110/* For use with d2i_ASN1_type_bytes() */
111#define B_ASN1_NUMERICSTRING 0x0001
112#define B_ASN1_PRINTABLESTRING 0x0002
113#define B_ASN1_T61STRING 0x0004
114#define B_ASN1_VIDEOTEXSTRING 0x0008
115#define B_ASN1_IA5STRING 0x0010
116#define B_ASN1_GRAPHICSTRING 0x0020
117#define B_ASN1_ISO64STRING 0x0040
118#define B_ASN1_GENERALSTRING 0x0080
119#define B_ASN1_UNIVERSALSTRING 0x0100
120#define B_ASN1_OCTET_STRING 0x0200
121#define B_ASN1_BIT_STRING 0x0400
122#define B_ASN1_BMPSTRING 0x0800
123#define B_ASN1_UNKNOWN 0x1000
124
125#ifndef DEBUG
126
127#define ASN1_INTEGER ASN1_STRING
128#define ASN1_BIT_STRING ASN1_STRING
129#define ASN1_OCTET_STRING ASN1_STRING
130#define ASN1_PRINTABLESTRING ASN1_STRING
131#define ASN1_T61STRING ASN1_STRING
132#define ASN1_IA5STRING ASN1_STRING
133#define ASN1_UTCTIME ASN1_STRING
134#define ASN1_GENERALIZEDTIME ASN1_STRING
135#define ASN1_GENERALSTRING ASN1_STRING
136#define ASN1_UNIVERSALSTRING ASN1_STRING
137#define ASN1_BMPSTRING ASN1_STRING
138
139#else
140
141typedef struct asn1_integer_st
142 {
143 int length;
144 int type;
145 unsigned char *data;
146 } ASN1_INTEGER;
147
148typedef struct asn1_bit_string_st
149 {
150 int length;
151 int type;
152 unsigned char *data;
153 } ASN1_BIT_STRING;
154
155typedef struct asn1_octet_string_st
156 {
157 int length;
158 int type;
159 unsigned char *data;
160 } ASN1_OCTET_STRING;
161
162typedef struct asn1_printablestring_st
163 {
164 int length;
165 int type;
166 unsigned char *data;
167 } ASN1_PRINTABLESTRING;
168
169typedef struct asn1_t61string_st
170 {
171 int length;
172 int type;
173 unsigned char *data;
174 } ASN1_T61STRING;
175
176typedef struct asn1_ia5string_st
177 {
178 int length;
179 int type;
180 unsigned char *data;
181 } ASN1_IA5STRING;
182
183typedef struct asn1_generalstring_st
184 {
185 int length;
186 int type;
187 unsigned char *data;
188 } ASN1_GENERALSTRING;
189
190typedef struct asn1_universalstring_st
191 {
192 int length;
193 int type;
194 unsigned char *data;
195 } ASN1_UNIVERSALSTRING;
196
197typedef struct asn1_bmpstring_st
198 {
199 int length;
200 int type;
201 unsigned char *data;
202 } ASN1_BMPSTRING;
203
204typedef struct asn1_utctime_st
205 {
206 int length;
207 int type;
208 unsigned char *data;
209 } ASN1_UTCTIME;
210
211typedef struct asn1_generalizedtime_st
212 {
213 int length;
214 int type;
215 unsigned char *data;
216 } ASN1_GENERALIZEDTIME;
217
218#endif
219
220typedef struct asn1_ctx_st
221 {
222 unsigned char *p;/* work char pointer */
223 int eos; /* end of sequence read for indefinite encoding */
224 int error; /* error code to use when returning an error */
225 int inf; /* constructed if 0x20, indefinite is 0x21 */
226 int tag; /* tag from last 'get object' */
227 int xclass; /* class from last 'get object' */
228 long slen; /* length of last 'get object' */
229 unsigned char *max; /* largest value of p alowed */
230 unsigned char *q;/* temporary variable */
231 unsigned char **pp;/* variable */
232 } ASN1_CTX;
233
234/* These are used internally in the ASN1_OBJECT to keep track of
235 * whether the names and data need to be free()ed */
236#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 /* internal use */
237#define ASN1_OBJECT_FLAG_CRITICAL 0x02 /* critical x509v3 object id */
238#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */
239#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 /* internal use */
240typedef struct asn1_object_st
241 {
242 char *sn,*ln;
243 int nid;
244 int length;
245 unsigned char *data;
246 int flags; /* Should we free this one */
247 } ASN1_OBJECT;
248
249/* This is the base type that holds just about everything :-) */
250typedef struct asn1_string_st
251 {
252 int length;
253 int type;
254 unsigned char *data;
255 } ASN1_STRING;
256
257typedef struct asn1_type_st
258 {
259 int type;
260 union {
261 char *ptr;
262 ASN1_STRING * asn1_string;
263 ASN1_OBJECT * object;
264 ASN1_INTEGER * integer;
265 ASN1_BIT_STRING * bit_string;
266 ASN1_OCTET_STRING * octet_string;
267 ASN1_PRINTABLESTRING * printablestring;
268 ASN1_T61STRING * t61string;
269 ASN1_IA5STRING * ia5string;
270 ASN1_GENERALSTRING * generalstring;
271 ASN1_BMPSTRING * bmpstring;
272 ASN1_UNIVERSALSTRING * universalstring;
273 ASN1_UTCTIME * utctime;
274 ASN1_GENERALIZEDTIME * generalizedtime;
275 /* set and sequence are left complete and still
276 * contain the set or sequence bytes */
277 ASN1_STRING * set;
278 ASN1_STRING * sequence;
279 } value;
280 } ASN1_TYPE;
281
282typedef struct asn1_method_st
283 {
284 int (*i2d)();
285 char *(*d2i)();
286 char *(*create)();
287 void (*destroy)();
288 } ASN1_METHOD;
289
290/* This is used when parsing some Netscape objects */
291typedef struct asn1_header_st
292 {
293 ASN1_OCTET_STRING *header;
294 char *data;
295 ASN1_METHOD *meth;
296 } ASN1_HEADER;
297
298#define ASN1_STRING_length(x) ((x)->length)
299#define ASN1_STRING_type(x) ((x)->type)
300#define ASN1_STRING_data(x) ((x)->data)
301
302/* Macros for string operations */
303#define ASN1_BIT_STRING_new() (ASN1_BIT_STRING *)\
304 ASN1_STRING_type_new(V_ASN1_BIT_STRING)
305#define ASN1_BIT_STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
306#define ASN1_BIT_STRING_dup(a) (ASN1_BIT_STRING *)\
307 ASN1_STRING_dup((ASN1_STRING *)a)
308#define ASN1_BIT_STRING_cmp(a,b) ASN1_STRING_cmp(\
309 (ASN1_STRING *)a,(ASN1_STRING *)b)
310#define ASN1_BIT_STRING_set(a,b,c) ASN1_STRING_set((ASN1_STRING *)a,b,c)
311/* i2d_ASN1_BIT_STRING() is a function */
312/* d2i_ASN1_BIT_STRING() is a function */
313
314#define ASN1_INTEGER_new() (ASN1_INTEGER *)\
315 ASN1_STRING_type_new(V_ASN1_INTEGER)
316#define ASN1_INTEGER_free(a) ASN1_STRING_free((ASN1_STRING *)a)
317#define ASN1_INTEGER_dup(a) (ASN1_INTEGER *)ASN1_STRING_dup((ASN1_STRING *)a)
318#define ASN1_INTEGER_cmp(a,b) ASN1_STRING_cmp(\
319 (ASN1_STRING *)a,(ASN1_STRING *)b)
320/* ASN1_INTEGER_set() is a function, also see BN_to_ASN1_INTEGER() */
321/* ASN1_INTEGER_get() is a function, also see ASN1_INTEGER_to_BN() */
322/* i2d_ASN1_INTEGER() is a function */
323/* d2i_ASN1_INTEGER() is a function */
324
325#define ASN1_OCTET_STRING_new() (ASN1_OCTET_STRING *)\
326 ASN1_STRING_type_new(V_ASN1_OCTET_STRING)
327#define ASN1_OCTET_STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
328#define ASN1_OCTET_STRING_dup(a) (ASN1_OCTET_STRING *)\
329 ASN1_STRING_dup((ASN1_STRING *)a)
330#define ASN1_OCTET_STRING_cmp(a,b) ASN1_STRING_cmp(\
331 (ASN1_STRING *)a,(ASN1_STRING *)b)
332#define ASN1_OCTET_STRING_set(a,b,c) ASN1_STRING_set((ASN1_STRING *)a,b,c)
333#define ASN1_OCTET_STRING_print(a,b) ASN1_STRING_print(a,(ASN1_STRING *)b)
334#define M_i2d_ASN1_OCTET_STRING(a,pp) \
335 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_OCTET_STRING,\
336 V_ASN1_OCTET_STRING)
337/* d2i_ASN1_OCTET_STRING() is a function */
338
339#define ASN1_PRINTABLE_new() ASN1_STRING_type_new(V_ASN1_T61STRING)
340#define ASN1_PRINTABLE_free(a) ASN1_STRING_free((ASN1_STRING *)a)
341#define M_i2d_ASN1_PRINTABLE(a,pp) i2d_ASN1_bytes((ASN1_STRING *)a,\
342 pp,a->type,V_ASN1_UNIVERSAL)
343#define M_d2i_ASN1_PRINTABLE(a,pp,l) \
344 d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l, \
345 B_ASN1_PRINTABLESTRING| \
346 B_ASN1_T61STRING| \
347 B_ASN1_IA5STRING| \
348 B_ASN1_BIT_STRING| \
349 B_ASN1_UNIVERSALSTRING|\
350 B_ASN1_BMPSTRING|\
351 B_ASN1_UNKNOWN)
352
353#define ASN1_PRINTABLESTRING_new() (ASN1_PRINTABLESTRING_STRING *)\
354 ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING)
355#define ASN1_PRINTABLESTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
356#define M_i2d_ASN1_PRINTABLESTRING(a,pp) \
357 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_PRINTABLESTRING,\
358 V_ASN1_UNIVERSAL)
359#define M_d2i_ASN1_PRINTABLESTRING(a,pp,l) \
360 (ASN1_PRINTABLESTRING *)d2i_ASN1_type_bytes\
361 ((ASN1_STRING **)a,pp,l,B_ASN1_PRINTABLESTRING)
362
363#define ASN1_T61STRING_new() (ASN1_T61STRING_STRING *)\
364 ASN1_STRING_type_new(V_ASN1_T61STRING)
365#define ASN1_T61STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
366#define M_i2d_ASN1_T61STRING(a,pp) \
367 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_T61STRING,\
368 V_ASN1_UNIVERSAL)
369#define M_d2i_ASN1_T61STRING(a,pp,l) \
370 (ASN1_T61STRING *)d2i_ASN1_type_bytes\
371 ((ASN1_STRING **)a,pp,l,B_ASN1_T61STRING)
372
373#define ASN1_IA5STRING_new() (ASN1_IA5STRING *)\
374 ASN1_STRING_type_new(V_ASN1_IA5STRING)
375#define ASN1_IA5STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
376#define M_i2d_ASN1_IA5STRING(a,pp) \
377 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_IA5STRING,\
378 V_ASN1_UNIVERSAL)
379#define M_d2i_ASN1_IA5STRING(a,pp,l) \
380 (ASN1_IA5STRING *)d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l,\
381 B_ASN1_IA5STRING)
382
383#define ASN1_UTCTIME_new() (ASN1_UTCTIME *)\
384 ASN1_STRING_type_new(V_ASN1_UTCTIME)
385#define ASN1_UTCTIME_free(a) ASN1_STRING_free((ASN1_STRING *)a)
386#define ASN1_UTCTIME_dup(a) (ASN1_UTCTIME *)ASN1_STRING_dup((ASN1_STRING *)a)
387/* i2d_ASN1_UTCTIME() is a function */
388/* d2i_ASN1_UTCTIME() is a function */
389/* ASN1_UTCTIME_set() is a function */
390/* ASN1_UTCTIME_check() is a function */
391
392#define ASN1_GENERALIZEDTIME_new() (ASN1_GENERALIZEDTIME *)\
393 ASN1_STRING_type_new(V_ASN1_GENERALIZEDTIME)
394#define ASN1_GENERALIZEDTIME_free(a) ASN1_STRING_free((ASN1_STRING *)a)
395#define ASN1_GENERALIZEDTIME_dup(a) (ASN1_UTCTIME *)ASN1_STRING_dup(\
396 (ASN1_STRING *)a)
397/* DOES NOT EXIST YET i2d_ASN1_GENERALIZEDTIME() is a function */
398/* DOES NOT EXIST YET d2i_ASN1_GENERALIZEDTIME() is a function */
399/* DOES NOT EXIST YET ASN1_GENERALIZEDTIME_set() is a function */
400/* DOES NOT EXIST YET ASN1_GENERALIZEDTIME_check() is a function */
401
402#define ASN1_GENERALSTRING_new() (ASN1_GENERALSTRING *)\
403 ASN1_STRING_type_new(V_ASN1_GENERALSTRING)
404#define ASN1_GENERALSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
405#define M_i2d_ASN1_GENERALSTRING(a,pp) \
406 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_GENERALSTRING,\
407 V_ASN1_UNIVERSAL)
408#define M_d2i_ASN1_GENERALSTRING(a,pp,l) \
409 (ASN1_GENERALSTRING *)d2i_ASN1_type_bytes\
410 ((ASN1_STRING **)a,pp,l,B_ASN1_GENERALSTRING)
411
412#define ASN1_UNIVERSALSTRING_new() (ASN1_UNIVERSALSTRING *)\
413 ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING)
414#define ASN1_UNIVERSALSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
415#define M_i2d_ASN1_UNIVERSALSTRING(a,pp) \
416 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_UNIVERSALSTRING,\
417 V_ASN1_UNIVERSAL)
418#define M_d2i_ASN1_UNIVERSALSTRING(a,pp,l) \
419 (ASN1_UNIVERSALSTRING *)d2i_ASN1_type_bytes\
420 ((ASN1_STRING **)a,pp,l,B_ASN1_UNIVERSALSTRING)
421
422#define ASN1_BMPSTRING_new() (ASN1_BMPSTRING *)\
423 ASN1_STRING_type_new(V_ASN1_BMPSTRING)
424#define ASN1_BMPSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a)
425#define M_i2d_ASN1_BMPSTRING(a,pp) \
426 i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_BMPSTRING,\
427 V_ASN1_UNIVERSAL)
428#define M_d2i_ASN1_BMPSTRING(a,pp,l) \
429 (ASN1_BMPSTRING *)d2i_ASN1_type_bytes\
430 ((ASN1_STRING **)a,pp,l,B_ASN1_BMPSTRING)
431
432#ifndef NOPROTO
433ASN1_TYPE * ASN1_TYPE_new(void );
434void ASN1_TYPE_free(ASN1_TYPE *a);
435int i2d_ASN1_TYPE(ASN1_TYPE *a,unsigned char **pp);
436ASN1_TYPE * d2i_ASN1_TYPE(ASN1_TYPE **a,unsigned char **pp,long length);
437int ASN1_TYPE_get(ASN1_TYPE *a);
438void ASN1_TYPE_set(ASN1_TYPE *a, int type, char *value);
439
440ASN1_OBJECT * ASN1_OBJECT_new(void );
441void ASN1_OBJECT_free(ASN1_OBJECT *a);
442int i2d_ASN1_OBJECT(ASN1_OBJECT *a,unsigned char **pp);
443ASN1_OBJECT * d2i_ASN1_OBJECT(ASN1_OBJECT **a,unsigned char **pp,
444 long length);
445
446ASN1_STRING * ASN1_STRING_new(void );
447void ASN1_STRING_free(ASN1_STRING *a);
448ASN1_STRING * ASN1_STRING_dup(ASN1_STRING *a);
449ASN1_STRING * ASN1_STRING_type_new(int type );
450int ASN1_STRING_cmp(ASN1_STRING *a, ASN1_STRING *b);
451int ASN1_STRING_set(ASN1_STRING *str,unsigned char *data, int len);
452
453int i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a,unsigned char **pp);
454ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,unsigned char **pp,
455 long length);
456int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value);
457int ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *a, int n);
458
459
460int i2d_ASN1_BOOLEAN(int a,unsigned char **pp);
461int d2i_ASN1_BOOLEAN(int *a,unsigned char **pp,long length);
462
463int i2d_ASN1_INTEGER(ASN1_INTEGER *a,unsigned char **pp);
464ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a,unsigned char **pp,
465 long length);
466
467int ASN1_UTCTIME_check(ASN1_UTCTIME *a);
468ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s,time_t t);
469int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, char *str);
470
471int i2d_ASN1_OCTET_STRING(ASN1_OCTET_STRING *a,unsigned char **pp);
472ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(ASN1_OCTET_STRING **a,
473 unsigned char **pp,long length);
474
475int i2d_ASN1_PRINTABLE(ASN1_STRING *a,unsigned char **pp);
476ASN1_STRING *d2i_ASN1_PRINTABLE(ASN1_STRING **a,
477 unsigned char **pp, long l);
478ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(ASN1_PRINTABLESTRING **a,
479 unsigned char **pp, long l);
480
481ASN1_T61STRING *d2i_ASN1_T61STRING(ASN1_T61STRING **a,
482 unsigned char **pp, long l);
483int i2d_ASN1_IA5STRING(ASN1_IA5STRING *a,unsigned char **pp);
484ASN1_IA5STRING *d2i_ASN1_IA5STRING(ASN1_IA5STRING **a,
485 unsigned char **pp, long l);
486
487int i2d_ASN1_UTCTIME(ASN1_UTCTIME *a,unsigned char **pp);
488ASN1_UTCTIME * d2i_ASN1_UTCTIME(ASN1_UTCTIME **a,unsigned char **pp,
489 long length);
490
491int i2d_ASN1_SET(STACK *a, unsigned char **pp,
492 int (*func)(), int ex_tag, int ex_class);
493STACK * d2i_ASN1_SET(STACK **a, unsigned char **pp, long length,
494 char *(*func)(), int ex_tag, int ex_class);
495
496#ifdef HEADER_BIO_H
497int i2a_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *a);
498int a2i_ASN1_INTEGER(BIO *bp,ASN1_INTEGER *bs,char *buf,int size);
499int i2a_ASN1_OBJECT(BIO *bp,ASN1_OBJECT *a);
500int a2i_ASN1_STRING(BIO *bp,ASN1_STRING *bs,char *buf,int size);
501int i2a_ASN1_STRING(BIO *bp, ASN1_STRING *a, int type);
502#endif
503int i2t_ASN1_OBJECT(char *buf,int buf_len,ASN1_OBJECT *a);
504
505int a2d_ASN1_OBJECT(unsigned char *out,int olen, char *buf, int num);
506ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data,int len,
507 char *sn, char *ln);
508
509int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);
510long ASN1_INTEGER_get(ASN1_INTEGER *a);
511ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai);
512BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai,BIGNUM *bn);
513
514/* General */
515/* given a string, return the correct type, max is the maximum length */
516int ASN1_PRINTABLE_type(unsigned char *s, int max);
517
518int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass);
519ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, unsigned char **pp,
520 long length, int Ptag, int Pclass);
521/* type is one or more of the B_ASN1_ values. */
522ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a,unsigned char **pp,
523 long length,int type);
524
525/* PARSING */
526int asn1_Finish(ASN1_CTX *c);
527
528/* SPECIALS */
529int ASN1_get_object(unsigned char **pp, long *plength, int *ptag,
530 int *pclass, long omax);
531int ASN1_check_infinite_end(unsigned char **p,long len);
532void ASN1_put_object(unsigned char **pp, int constructed, int length,
533 int tag, int xclass);
534int ASN1_object_size(int constructed, int length, int tag);
535
536/* Used to implement other functions */
537char *ASN1_dup(int (*i2d)(),char *(*d2i)(),char *x);
538
539#ifndef NO_FP_API
540char *ASN1_d2i_fp(char *(*xnew)(),char *(*d2i)(),FILE *fp,unsigned char **x);
541int ASN1_i2d_fp(int (*i2d)(),FILE *out,unsigned char *x);
542#endif
543
544#ifdef HEADER_BIO_H
545char *ASN1_d2i_bio(char *(*xnew)(),char *(*d2i)(),BIO *bp,unsigned char **x);
546int ASN1_i2d_bio(int (*i2d)(),BIO *out,unsigned char *x);
547int ASN1_UTCTIME_print(BIO *fp,ASN1_UTCTIME *a);
548int ASN1_STRING_print(BIO *bp,ASN1_STRING *v);
549int ASN1_parse(BIO *bp,unsigned char *pp,long len,int indent);
550#endif
551
552/* Used to load and write netscape format cert/key */
553int i2d_ASN1_HEADER(ASN1_HEADER *a,unsigned char **pp);
554ASN1_HEADER *d2i_ASN1_HEADER(ASN1_HEADER **a,unsigned char **pp, long length);
555ASN1_HEADER *ASN1_HEADER_new(void );
556void ASN1_HEADER_free(ASN1_HEADER *a);
557
558int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s);
559
560void ERR_load_ASN1_strings(void);
561
562/* Not used that much at this point, except for the first two */
563ASN1_METHOD *X509_asn1_meth(void);
564ASN1_METHOD *RSAPrivateKey_asn1_meth(void);
565ASN1_METHOD *ASN1_IA5STRING_asn1_meth(void);
566ASN1_METHOD *ASN1_BIT_STRING_asn1_meth(void);
567
568int ASN1_TYPE_set_octetstring(ASN1_TYPE *a,
569 unsigned char *data, int len);
570int ASN1_TYPE_get_octetstring(ASN1_TYPE *a,
571 unsigned char *data, int max_len);
572int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num,
573 unsigned char *data, int len);
574int ASN1_TYPE_get_int_octetstring(ASN1_TYPE *a,long *num,
575 unsigned char *data, int max_len);
576
577#else
578
579ASN1_TYPE * ASN1_TYPE_new();
580void ASN1_TYPE_free();
581int i2d_ASN1_TYPE();
582ASN1_TYPE * d2i_ASN1_TYPE();
583int ASN1_TYPE_get();
584void ASN1_TYPE_set();
585
586ASN1_OBJECT * ASN1_OBJECT_new();
587void ASN1_OBJECT_free();
588int i2d_ASN1_OBJECT();
589ASN1_OBJECT * d2i_ASN1_OBJECT();
590ASN1_STRING * ASN1_STRING_new();
591void ASN1_STRING_free();
592ASN1_STRING * ASN1_STRING_dup();
593ASN1_STRING * ASN1_STRING_type_new();
594int ASN1_STRING_cmp();
595int ASN1_STRING_set();
596int i2d_ASN1_BIT_STRING();
597ASN1_BIT_STRING *d2i_ASN1_BIT_STRING();
598int ASN1_BIT_STRING_set_bit();
599int ASN1_BIT_STRING_get_bit();
600int i2d_ASN1_BOOLEAN();
601int d2i_ASN1_BOOLEAN();
602int i2d_ASN1_INTEGER();
603ASN1_INTEGER *d2i_ASN1_INTEGER();
604int ASN1_UTCTIME_check();
605ASN1_UTCTIME *ASN1_UTCTIME_set();
606int ASN1_UTCTIME_set_string();
607int i2d_ASN1_OCTET_STRING();
608ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING();
609int i2d_ASN1_PRINTABLE();
610ASN1_STRING *d2i_ASN1_PRINTABLE();
611ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING();
612ASN1_T61STRING *d2i_ASN1_T61STRING();
613int i2d_ASN1_IA5STRING();
614ASN1_IA5STRING *d2i_ASN1_IA5STRING();
615int i2d_ASN1_UTCTIME();
616ASN1_UTCTIME * d2i_ASN1_UTCTIME();
617int i2d_ASN1_SET();
618STACK * d2i_ASN1_SET();
619int a2d_ASN1_OBJECT();
620ASN1_OBJECT *ASN1_OBJECT_create();
621int ASN1_INTEGER_set();
622long ASN1_INTEGER_get();
623ASN1_INTEGER *BN_to_ASN1_INTEGER();
624BIGNUM *ASN1_INTEGER_to_BN();
625int ASN1_PRINTABLE_type();
626int i2d_ASN1_bytes();
627ASN1_STRING *d2i_ASN1_bytes();
628ASN1_STRING *d2i_ASN1_type_bytes();
629int asn1_Finish();
630int ASN1_get_object();
631int ASN1_check_infinite_end();
632void ASN1_put_object();
633int ASN1_object_size();
634char *ASN1_dup();
635#ifndef NO_FP_API
636char *ASN1_d2i_fp();
637int ASN1_i2d_fp();
638#endif
639
640char *ASN1_d2i_bio();
641int ASN1_i2d_bio();
642int ASN1_UTCTIME_print();
643int ASN1_STRING_print();
644int ASN1_parse();
645int i2a_ASN1_INTEGER();
646int a2i_ASN1_INTEGER();
647int i2a_ASN1_OBJECT();
648int i2t_ASN1_OBJECT();
649int a2i_ASN1_STRING();
650int i2a_ASN1_STRING();
651
652int i2d_ASN1_HEADER();
653ASN1_HEADER *d2i_ASN1_HEADER();
654ASN1_HEADER *ASN1_HEADER_new();
655void ASN1_HEADER_free();
656void ERR_load_ASN1_strings();
657ASN1_METHOD *X509_asn1_meth();
658ASN1_METHOD *RSAPrivateKey_asn1_meth();
659ASN1_METHOD *ASN1_IA5STRING_asn1_meth();
660ASN1_METHOD *ASN1_BIT_STRING_asn1_meth();
661
662int ASN1_UNIVERSALSTRING_to_string();
663
664int ASN1_TYPE_set_octetstring();
665int ASN1_TYPE_get_octetstring();
666int ASN1_TYPE_set_int_octetstring();
667int ASN1_TYPE_get_int_octetstring();
668
669#endif
670
671/* BEGIN ERROR CODES */
672/* Error codes for the ASN1 functions. */
673
674/* Function codes. */
675#define ASN1_F_A2D_ASN1_OBJECT 100
676#define ASN1_F_A2I_ASN1_INTEGER 101
677#define ASN1_F_A2I_ASN1_STRING 102
678#define ASN1_F_ASN1_COLLATE_PRIMATIVE 103
679#define ASN1_F_ASN1_D2I_BIO 104
680#define ASN1_F_ASN1_D2I_FP 105
681#define ASN1_F_ASN1_DUP 106
682#define ASN1_F_ASN1_GET_OBJECT 107
683#define ASN1_F_ASN1_HEADER_NEW 108
684#define ASN1_F_ASN1_I2D_BIO 109
685#define ASN1_F_ASN1_I2D_FP 110
686#define ASN1_F_ASN1_INTEGER_SET 111
687#define ASN1_F_ASN1_INTEGER_TO_BN 112
688#define ASN1_F_ASN1_OBJECT_NEW 113
689#define ASN1_F_ASN1_SIGN 114
690#define ASN1_F_ASN1_STRING_NEW 115
691#define ASN1_F_ASN1_STRING_TYPE_NEW 116
692#define ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING 117
693#define ASN1_F_ASN1_TYPE_GET_OCTETSTRING 118
694#define ASN1_F_ASN1_TYPE_NEW 119
695#define ASN1_F_ASN1_UTCTIME_NEW 120
696#define ASN1_F_ASN1_VERIFY 121
697#define ASN1_F_BN_TO_ASN1_INTEGER 122
698#define ASN1_F_D2I_ASN1_BIT_STRING 123
699#define ASN1_F_D2I_ASN1_BMPSTRING 124
700#define ASN1_F_D2I_ASN1_BOOLEAN 125
701#define ASN1_F_D2I_ASN1_BYTES 126
702#define ASN1_F_D2I_ASN1_HEADER 127
703#define ASN1_F_D2I_ASN1_INTEGER 128
704#define ASN1_F_D2I_ASN1_OBJECT 129
705#define ASN1_F_D2I_ASN1_OCTET_STRING 130
706#define ASN1_F_D2I_ASN1_PRINT_TYPE 131
707#define ASN1_F_D2I_ASN1_SET 132
708#define ASN1_F_D2I_ASN1_TYPE 133
709#define ASN1_F_D2I_ASN1_TYPE_BYTES 134
710#define ASN1_F_D2I_ASN1_UTCTIME 135
711#define ASN1_F_D2I_DHPARAMS 136
712#define ASN1_F_D2I_DSAPARAMS 137
713#define ASN1_F_D2I_DSAPRIVATEKEY 138
714#define ASN1_F_D2I_DSAPUBLICKEY 139
715#define ASN1_F_D2I_NETSCAPE_PKEY 140
716#define ASN1_F_D2I_NETSCAPE_RSA 141
717#define ASN1_F_D2I_NETSCAPE_RSA_2 142
718#define ASN1_F_D2I_NETSCAPE_SPKAC 143
719#define ASN1_F_D2I_NETSCAPE_SPKI 144
720#define ASN1_F_D2I_PKCS7 145
721#define ASN1_F_D2I_PKCS7_DIGEST 146
722#define ASN1_F_D2I_PKCS7_ENCRYPT 147
723#define ASN1_F_D2I_PKCS7_ENC_CONTENT 148
724#define ASN1_F_D2I_PKCS7_ENVELOPE 149
725#define ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL 150
726#define ASN1_F_D2I_PKCS7_RECIP_INFO 151
727#define ASN1_F_D2I_PKCS7_SIGNED 152
728#define ASN1_F_D2I_PKCS7_SIGNER_INFO 153
729#define ASN1_F_D2I_PKCS7_SIGN_ENVELOPE 154
730#define ASN1_F_D2I_PRIVATEKEY 155
731#define ASN1_F_D2I_PUBLICKEY 156
732#define ASN1_F_D2I_RSAPRIVATEKEY 157
733#define ASN1_F_D2I_RSAPUBLICKEY 158
734#define ASN1_F_D2I_X509 159
735#define ASN1_F_D2I_X509_ALGOR 160
736#define ASN1_F_D2I_X509_ATTRIBUTE 161
737#define ASN1_F_D2I_X509_CINF 162
738#define ASN1_F_D2I_X509_CRL 163
739#define ASN1_F_D2I_X509_CRL_INFO 164
740#define ASN1_F_D2I_X509_EXTENSION 165
741#define ASN1_F_D2I_X509_KEY 166
742#define ASN1_F_D2I_X509_NAME 167
743#define ASN1_F_D2I_X509_NAME_ENTRY 168
744#define ASN1_F_D2I_X509_PKEY 169
745#define ASN1_F_D2I_X509_PUBKEY 170
746#define ASN1_F_D2I_X509_REQ 171
747#define ASN1_F_D2I_X509_REQ_INFO 172
748#define ASN1_F_D2I_X509_REVOKED 173
749#define ASN1_F_D2I_X509_SIG 174
750#define ASN1_F_D2I_X509_VAL 175
751#define ASN1_F_I2D_ASN1_HEADER 176
752#define ASN1_F_I2D_DHPARAMS 177
753#define ASN1_F_I2D_DSAPARAMS 178
754#define ASN1_F_I2D_DSAPRIVATEKEY 179
755#define ASN1_F_I2D_DSAPUBLICKEY 180
756#define ASN1_F_I2D_NETSCAPE_RSA 181
757#define ASN1_F_I2D_PKCS7 182
758#define ASN1_F_I2D_PRIVATEKEY 183
759#define ASN1_F_I2D_PUBLICKEY 184
760#define ASN1_F_I2D_RSAPRIVATEKEY 185
761#define ASN1_F_I2D_RSAPUBLICKEY 186
762#define ASN1_F_I2D_X509_ATTRIBUTE 187
763#define ASN1_F_I2T_ASN1_OBJECT 188
764#define ASN1_F_NETSCAPE_PKEY_NEW 189
765#define ASN1_F_NETSCAPE_SPKAC_NEW 190
766#define ASN1_F_NETSCAPE_SPKI_NEW 191
767#define ASN1_F_PKCS7_DIGEST_NEW 192
768#define ASN1_F_PKCS7_ENCRYPT_NEW 193
769#define ASN1_F_PKCS7_ENC_CONTENT_NEW 194
770#define ASN1_F_PKCS7_ENVELOPE_NEW 195
771#define ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW 196
772#define ASN1_F_PKCS7_NEW 197
773#define ASN1_F_PKCS7_RECIP_INFO_NEW 198
774#define ASN1_F_PKCS7_SIGNED_NEW 199
775#define ASN1_F_PKCS7_SIGNER_INFO_NEW 200
776#define ASN1_F_PKCS7_SIGN_ENVELOPE_NEW 201
777#define ASN1_F_X509_ALGOR_NEW 202
778#define ASN1_F_X509_ATTRIBUTE_NEW 203
779#define ASN1_F_X509_CINF_NEW 204
780#define ASN1_F_X509_CRL_INFO_NEW 205
781#define ASN1_F_X509_CRL_NEW 206
782#define ASN1_F_X509_DHPARAMS_NEW 207
783#define ASN1_F_X509_EXTENSION_NEW 208
784#define ASN1_F_X509_INFO_NEW 209
785#define ASN1_F_X509_KEY_NEW 210
786#define ASN1_F_X509_NAME_ENTRY_NEW 211
787#define ASN1_F_X509_NAME_NEW 212
788#define ASN1_F_X509_NEW 213
789#define ASN1_F_X509_PKEY_NEW 214
790#define ASN1_F_X509_PUBKEY_NEW 215
791#define ASN1_F_X509_REQ_INFO_NEW 216
792#define ASN1_F_X509_REQ_NEW 217
793#define ASN1_F_X509_REVOKED_NEW 218
794#define ASN1_F_X509_SIG_NEW 219
795#define ASN1_F_X509_VAL_FREE 220
796#define ASN1_F_X509_VAL_NEW 221
797
798/* Reason codes. */
799#define ASN1_R_BAD_CLASS 100
800#define ASN1_R_BAD_GET_OBJECT 101
801#define ASN1_R_BAD_OBJECT_HEADER 102
802#define ASN1_R_BAD_PASSWORD_READ 103
803#define ASN1_R_BAD_PKCS7_CONTENT 104
804#define ASN1_R_BAD_PKCS7_TYPE 105
805#define ASN1_R_BAD_TAG 106
806#define ASN1_R_BAD_TYPE 107
807#define ASN1_R_BN_LIB 108
808#define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 109
809#define ASN1_R_BUFFER_TOO_SMALL 110
810#define ASN1_R_DATA_IS_WRONG 111
811#define ASN1_R_DECODING_ERROR 112
812#define ASN1_R_ERROR_STACK 113
813#define ASN1_R_EXPECTING_AN_INTEGER 114
814#define ASN1_R_EXPECTING_AN_OBJECT 115
815#define ASN1_R_EXPECTING_AN_OCTET_STRING 116
816#define ASN1_R_EXPECTING_A_BIT_STRING 117
817#define ASN1_R_EXPECTING_A_BOOLEAN 118
818#define ASN1_R_EXPECTING_A_SEQUENCE 119
819#define ASN1_R_EXPECTING_A_UTCTIME 120
820#define ASN1_R_FIRST_NUM_TOO_LARGE 121
821#define ASN1_R_HEADER_TOO_LONG 122
822#define ASN1_R_INVALID_DIGIT 123
823#define ASN1_R_INVALID_SEPARATOR 124
824#define ASN1_R_INVALID_TIME_FORMAT 125
825#define ASN1_R_IV_TOO_LARGE 126
826#define ASN1_R_LENGTH_ERROR 127
827#define ASN1_R_LENGTH_MISMATCH 128
828#define ASN1_R_MISSING_EOS 129
829#define ASN1_R_MISSING_SECOND_NUMBER 130
830#define ASN1_R_NON_HEX_CHARACTERS 131
831#define ASN1_R_NOT_ENOUGH_DATA 132
832#define ASN1_R_ODD_NUMBER_OF_CHARS 133
833#define ASN1_R_PARSING 134
834#define ASN1_R_PRIVATE_KEY_HEADER_MISSING 135
835#define ASN1_R_SECOND_NUMBER_TOO_LARGE 136
836#define ASN1_R_SHORT_LINE 137
837#define ASN1_R_STRING_TOO_SHORT 138
838#define ASN1_R_TAG_VALUE_TOO_HIGH 139
839#define ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 140
840#define ASN1_R_TOO_LONG 141
841#define ASN1_R_UNABLE_TO_DECODE_RSA_KEY 142
842#define ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY 143
843#define ASN1_R_UNKNOWN_ATTRIBUTE_TYPE 144
844#define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 145
845#define ASN1_R_UNKNOWN_OBJECT_TYPE 146
846#define ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE 147
847#define ASN1_R_UNSUPPORTED_CIPHER 148
848#define ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM 149
849#define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 150
850#define ASN1_R_UTCTIME_TOO_LONG 151
851#define ASN1_R_WRONG_PRINTABLE_TYPE 152
852#define ASN1_R_WRONG_TAG 153
853#define ASN1_R_WRONG_TYPE 154
854
855#ifdef __cplusplus
856}
857#endif
858#endif
859
diff --git a/src/lib/libcrypto/asn1/asn1_err.c b/src/lib/libcrypto/asn1/asn1_err.c
new file mode 100644
index 0000000000..03c2858e7d
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_err.c
@@ -0,0 +1,266 @@
1/* lib/asn1/asn1_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "asn1.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA ASN1_str_functs[]=
65 {
66{ERR_PACK(0,ASN1_F_A2D_ASN1_OBJECT,0), "a2d_ASN1_OBJECT"},
67{ERR_PACK(0,ASN1_F_A2I_ASN1_INTEGER,0), "a2i_ASN1_INTEGER"},
68{ERR_PACK(0,ASN1_F_A2I_ASN1_STRING,0), "a2i_ASN1_STRING"},
69{ERR_PACK(0,ASN1_F_ASN1_COLLATE_PRIMATIVE,0), "ASN1_COLLATE_PRIMATIVE"},
70{ERR_PACK(0,ASN1_F_ASN1_D2I_BIO,0), "ASN1_d2i_bio"},
71{ERR_PACK(0,ASN1_F_ASN1_D2I_FP,0), "ASN1_d2i_fp"},
72{ERR_PACK(0,ASN1_F_ASN1_DUP,0), "ASN1_dup"},
73{ERR_PACK(0,ASN1_F_ASN1_GET_OBJECT,0), "ASN1_get_object"},
74{ERR_PACK(0,ASN1_F_ASN1_HEADER_NEW,0), "ASN1_HEADER_new"},
75{ERR_PACK(0,ASN1_F_ASN1_I2D_BIO,0), "ASN1_i2d_bio"},
76{ERR_PACK(0,ASN1_F_ASN1_I2D_FP,0), "ASN1_i2d_fp"},
77{ERR_PACK(0,ASN1_F_ASN1_INTEGER_SET,0), "ASN1_INTEGER_set"},
78{ERR_PACK(0,ASN1_F_ASN1_INTEGER_TO_BN,0), "ASN1_INTEGER_to_BN"},
79{ERR_PACK(0,ASN1_F_ASN1_OBJECT_NEW,0), "ASN1_OBJECT_new"},
80{ERR_PACK(0,ASN1_F_ASN1_SIGN,0), "ASN1_SIGN"},
81{ERR_PACK(0,ASN1_F_ASN1_STRING_NEW,0), "ASN1_STRING_new"},
82{ERR_PACK(0,ASN1_F_ASN1_STRING_TYPE_NEW,0), "ASN1_STRING_type_new"},
83{ERR_PACK(0,ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING,0), "ASN1_TYPE_get_int_octetstring"},
84{ERR_PACK(0,ASN1_F_ASN1_TYPE_GET_OCTETSTRING,0), "ASN1_TYPE_get_octetstring"},
85{ERR_PACK(0,ASN1_F_ASN1_TYPE_NEW,0), "ASN1_TYPE_new"},
86{ERR_PACK(0,ASN1_F_ASN1_UTCTIME_NEW,0), "ASN1_UTCTIME_NEW"},
87{ERR_PACK(0,ASN1_F_ASN1_VERIFY,0), "ASN1_VERIFY"},
88{ERR_PACK(0,ASN1_F_BN_TO_ASN1_INTEGER,0), "BN_to_ASN1_INTEGER"},
89{ERR_PACK(0,ASN1_F_D2I_ASN1_BIT_STRING,0), "d2i_ASN1_BIT_STRING"},
90{ERR_PACK(0,ASN1_F_D2I_ASN1_BMPSTRING,0), "D2I_ASN1_BMPSTRING"},
91{ERR_PACK(0,ASN1_F_D2I_ASN1_BOOLEAN,0), "d2i_ASN1_BOOLEAN"},
92{ERR_PACK(0,ASN1_F_D2I_ASN1_BYTES,0), "d2i_ASN1_bytes"},
93{ERR_PACK(0,ASN1_F_D2I_ASN1_HEADER,0), "d2i_ASN1_HEADER"},
94{ERR_PACK(0,ASN1_F_D2I_ASN1_INTEGER,0), "d2i_ASN1_INTEGER"},
95{ERR_PACK(0,ASN1_F_D2I_ASN1_OBJECT,0), "d2i_ASN1_OBJECT"},
96{ERR_PACK(0,ASN1_F_D2I_ASN1_OCTET_STRING,0), "d2i_ASN1_OCTET_STRING"},
97{ERR_PACK(0,ASN1_F_D2I_ASN1_PRINT_TYPE,0), "D2I_ASN1_PRINT_TYPE"},
98{ERR_PACK(0,ASN1_F_D2I_ASN1_SET,0), "d2i_ASN1_SET"},
99{ERR_PACK(0,ASN1_F_D2I_ASN1_TYPE,0), "d2i_ASN1_TYPE"},
100{ERR_PACK(0,ASN1_F_D2I_ASN1_TYPE_BYTES,0), "d2i_ASN1_type_bytes"},
101{ERR_PACK(0,ASN1_F_D2I_ASN1_UTCTIME,0), "d2i_ASN1_UTCTIME"},
102{ERR_PACK(0,ASN1_F_D2I_DHPARAMS,0), "D2I_DHPARAMS"},
103{ERR_PACK(0,ASN1_F_D2I_DSAPARAMS,0), "D2I_DSAPARAMS"},
104{ERR_PACK(0,ASN1_F_D2I_DSAPRIVATEKEY,0), "D2I_DSAPRIVATEKEY"},
105{ERR_PACK(0,ASN1_F_D2I_DSAPUBLICKEY,0), "D2I_DSAPUBLICKEY"},
106{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_PKEY,0), "D2I_NETSCAPE_PKEY"},
107{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_RSA,0), "D2I_NETSCAPE_RSA"},
108{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_RSA_2,0), "D2I_NETSCAPE_RSA_2"},
109{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_SPKAC,0), "D2I_NETSCAPE_SPKAC"},
110{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_SPKI,0), "D2I_NETSCAPE_SPKI"},
111{ERR_PACK(0,ASN1_F_D2I_PKCS7,0), "D2I_PKCS7"},
112{ERR_PACK(0,ASN1_F_D2I_PKCS7_DIGEST,0), "D2I_PKCS7_DIGEST"},
113{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENCRYPT,0), "D2I_PKCS7_ENCRYPT"},
114{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENC_CONTENT,0), "D2I_PKCS7_ENC_CONTENT"},
115{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENVELOPE,0), "D2I_PKCS7_ENVELOPE"},
116{ERR_PACK(0,ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL,0), "D2I_PKCS7_ISSUER_AND_SERIAL"},
117{ERR_PACK(0,ASN1_F_D2I_PKCS7_RECIP_INFO,0), "D2I_PKCS7_RECIP_INFO"},
118{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGNED,0), "D2I_PKCS7_SIGNED"},
119{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGNER_INFO,0), "D2I_PKCS7_SIGNER_INFO"},
120{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGN_ENVELOPE,0), "D2I_PKCS7_SIGN_ENVELOPE"},
121{ERR_PACK(0,ASN1_F_D2I_PRIVATEKEY,0), "D2I_PRIVATEKEY"},
122{ERR_PACK(0,ASN1_F_D2I_PUBLICKEY,0), "D2I_PUBLICKEY"},
123{ERR_PACK(0,ASN1_F_D2I_RSAPRIVATEKEY,0), "D2I_RSAPRIVATEKEY"},
124{ERR_PACK(0,ASN1_F_D2I_RSAPUBLICKEY,0), "D2I_RSAPUBLICKEY"},
125{ERR_PACK(0,ASN1_F_D2I_X509,0), "D2I_X509"},
126{ERR_PACK(0,ASN1_F_D2I_X509_ALGOR,0), "D2I_X509_ALGOR"},
127{ERR_PACK(0,ASN1_F_D2I_X509_ATTRIBUTE,0), "D2I_X509_ATTRIBUTE"},
128{ERR_PACK(0,ASN1_F_D2I_X509_CINF,0), "D2I_X509_CINF"},
129{ERR_PACK(0,ASN1_F_D2I_X509_CRL,0), "D2I_X509_CRL"},
130{ERR_PACK(0,ASN1_F_D2I_X509_CRL_INFO,0), "D2I_X509_CRL_INFO"},
131{ERR_PACK(0,ASN1_F_D2I_X509_EXTENSION,0), "D2I_X509_EXTENSION"},
132{ERR_PACK(0,ASN1_F_D2I_X509_KEY,0), "D2I_X509_KEY"},
133{ERR_PACK(0,ASN1_F_D2I_X509_NAME,0), "D2I_X509_NAME"},
134{ERR_PACK(0,ASN1_F_D2I_X509_NAME_ENTRY,0), "D2I_X509_NAME_ENTRY"},
135{ERR_PACK(0,ASN1_F_D2I_X509_PKEY,0), "D2I_X509_PKEY"},
136{ERR_PACK(0,ASN1_F_D2I_X509_PUBKEY,0), "D2I_X509_PUBKEY"},
137{ERR_PACK(0,ASN1_F_D2I_X509_REQ,0), "D2I_X509_REQ"},
138{ERR_PACK(0,ASN1_F_D2I_X509_REQ_INFO,0), "D2I_X509_REQ_INFO"},
139{ERR_PACK(0,ASN1_F_D2I_X509_REVOKED,0), "D2I_X509_REVOKED"},
140{ERR_PACK(0,ASN1_F_D2I_X509_SIG,0), "D2I_X509_SIG"},
141{ERR_PACK(0,ASN1_F_D2I_X509_VAL,0), "D2I_X509_VAL"},
142{ERR_PACK(0,ASN1_F_I2D_ASN1_HEADER,0), "i2d_ASN1_HEADER"},
143{ERR_PACK(0,ASN1_F_I2D_DHPARAMS,0), "I2D_DHPARAMS"},
144{ERR_PACK(0,ASN1_F_I2D_DSAPARAMS,0), "I2D_DSAPARAMS"},
145{ERR_PACK(0,ASN1_F_I2D_DSAPRIVATEKEY,0), "I2D_DSAPRIVATEKEY"},
146{ERR_PACK(0,ASN1_F_I2D_DSAPUBLICKEY,0), "I2D_DSAPUBLICKEY"},
147{ERR_PACK(0,ASN1_F_I2D_NETSCAPE_RSA,0), "I2D_NETSCAPE_RSA"},
148{ERR_PACK(0,ASN1_F_I2D_PKCS7,0), "I2D_PKCS7"},
149{ERR_PACK(0,ASN1_F_I2D_PRIVATEKEY,0), "I2D_PRIVATEKEY"},
150{ERR_PACK(0,ASN1_F_I2D_PUBLICKEY,0), "I2D_PUBLICKEY"},
151{ERR_PACK(0,ASN1_F_I2D_RSAPRIVATEKEY,0), "I2D_RSAPRIVATEKEY"},
152{ERR_PACK(0,ASN1_F_I2D_RSAPUBLICKEY,0), "I2D_RSAPUBLICKEY"},
153{ERR_PACK(0,ASN1_F_I2D_X509_ATTRIBUTE,0), "I2D_X509_ATTRIBUTE"},
154{ERR_PACK(0,ASN1_F_I2T_ASN1_OBJECT,0), "i2t_ASN1_OBJECT"},
155{ERR_PACK(0,ASN1_F_NETSCAPE_PKEY_NEW,0), "NETSCAPE_PKEY_NEW"},
156{ERR_PACK(0,ASN1_F_NETSCAPE_SPKAC_NEW,0), "NETSCAPE_SPKAC_NEW"},
157{ERR_PACK(0,ASN1_F_NETSCAPE_SPKI_NEW,0), "NETSCAPE_SPKI_NEW"},
158{ERR_PACK(0,ASN1_F_PKCS7_DIGEST_NEW,0), "PKCS7_DIGEST_NEW"},
159{ERR_PACK(0,ASN1_F_PKCS7_ENCRYPT_NEW,0), "PKCS7_ENCRYPT_NEW"},
160{ERR_PACK(0,ASN1_F_PKCS7_ENC_CONTENT_NEW,0), "PKCS7_ENC_CONTENT_NEW"},
161{ERR_PACK(0,ASN1_F_PKCS7_ENVELOPE_NEW,0), "PKCS7_ENVELOPE_NEW"},
162{ERR_PACK(0,ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW,0), "PKCS7_ISSUER_AND_SERIAL_NEW"},
163{ERR_PACK(0,ASN1_F_PKCS7_NEW,0), "PKCS7_NEW"},
164{ERR_PACK(0,ASN1_F_PKCS7_RECIP_INFO_NEW,0), "PKCS7_RECIP_INFO_NEW"},
165{ERR_PACK(0,ASN1_F_PKCS7_SIGNED_NEW,0), "PKCS7_SIGNED_NEW"},
166{ERR_PACK(0,ASN1_F_PKCS7_SIGNER_INFO_NEW,0), "PKCS7_SIGNER_INFO_NEW"},
167{ERR_PACK(0,ASN1_F_PKCS7_SIGN_ENVELOPE_NEW,0), "PKCS7_SIGN_ENVELOPE_NEW"},
168{ERR_PACK(0,ASN1_F_X509_ALGOR_NEW,0), "X509_ALGOR_NEW"},
169{ERR_PACK(0,ASN1_F_X509_ATTRIBUTE_NEW,0), "X509_ATTRIBUTE_NEW"},
170{ERR_PACK(0,ASN1_F_X509_CINF_NEW,0), "X509_CINF_NEW"},
171{ERR_PACK(0,ASN1_F_X509_CRL_INFO_NEW,0), "X509_CRL_INFO_NEW"},
172{ERR_PACK(0,ASN1_F_X509_CRL_NEW,0), "X509_CRL_NEW"},
173{ERR_PACK(0,ASN1_F_X509_DHPARAMS_NEW,0), "X509_DHPARAMS_NEW"},
174{ERR_PACK(0,ASN1_F_X509_EXTENSION_NEW,0), "X509_EXTENSION_NEW"},
175{ERR_PACK(0,ASN1_F_X509_INFO_NEW,0), "X509_INFO_NEW"},
176{ERR_PACK(0,ASN1_F_X509_KEY_NEW,0), "X509_KEY_NEW"},
177{ERR_PACK(0,ASN1_F_X509_NAME_ENTRY_NEW,0), "X509_NAME_ENTRY_NEW"},
178{ERR_PACK(0,ASN1_F_X509_NAME_NEW,0), "X509_NAME_NEW"},
179{ERR_PACK(0,ASN1_F_X509_NEW,0), "X509_NEW"},
180{ERR_PACK(0,ASN1_F_X509_PKEY_NEW,0), "X509_PKEY_NEW"},
181{ERR_PACK(0,ASN1_F_X509_PUBKEY_NEW,0), "X509_PUBKEY_NEW"},
182{ERR_PACK(0,ASN1_F_X509_REQ_INFO_NEW,0), "X509_REQ_INFO_NEW"},
183{ERR_PACK(0,ASN1_F_X509_REQ_NEW,0), "X509_REQ_NEW"},
184{ERR_PACK(0,ASN1_F_X509_REVOKED_NEW,0), "X509_REVOKED_NEW"},
185{ERR_PACK(0,ASN1_F_X509_SIG_NEW,0), "X509_SIG_NEW"},
186{ERR_PACK(0,ASN1_F_X509_VAL_FREE,0), "X509_VAL_FREE"},
187{ERR_PACK(0,ASN1_F_X509_VAL_NEW,0), "X509_VAL_NEW"},
188{0,NULL},
189 };
190
191static ERR_STRING_DATA ASN1_str_reasons[]=
192 {
193{ASN1_R_BAD_CLASS ,"bad class"},
194{ASN1_R_BAD_GET_OBJECT ,"bad get object"},
195{ASN1_R_BAD_OBJECT_HEADER ,"bad object header"},
196{ASN1_R_BAD_PASSWORD_READ ,"bad password read"},
197{ASN1_R_BAD_PKCS7_CONTENT ,"bad pkcs7 content"},
198{ASN1_R_BAD_PKCS7_TYPE ,"bad pkcs7 type"},
199{ASN1_R_BAD_TAG ,"bad tag"},
200{ASN1_R_BAD_TYPE ,"bad type"},
201{ASN1_R_BN_LIB ,"bn lib"},
202{ASN1_R_BOOLEAN_IS_WRONG_LENGTH ,"boolean is wrong length"},
203{ASN1_R_BUFFER_TOO_SMALL ,"buffer too small"},
204{ASN1_R_DATA_IS_WRONG ,"data is wrong"},
205{ASN1_R_DECODING_ERROR ,"decoding error"},
206{ASN1_R_ERROR_STACK ,"error stack"},
207{ASN1_R_EXPECTING_AN_INTEGER ,"expecting an integer"},
208{ASN1_R_EXPECTING_AN_OBJECT ,"expecting an object"},
209{ASN1_R_EXPECTING_AN_OCTET_STRING ,"expecting an octet string"},
210{ASN1_R_EXPECTING_A_BIT_STRING ,"expecting a bit string"},
211{ASN1_R_EXPECTING_A_BOOLEAN ,"expecting a boolean"},
212{ASN1_R_EXPECTING_A_SEQUENCE ,"expecting a sequence"},
213{ASN1_R_EXPECTING_A_UTCTIME ,"expecting a utctime"},
214{ASN1_R_FIRST_NUM_TOO_LARGE ,"first num too large"},
215{ASN1_R_HEADER_TOO_LONG ,"header too long"},
216{ASN1_R_INVALID_DIGIT ,"invalid digit"},
217{ASN1_R_INVALID_SEPARATOR ,"invalid separator"},
218{ASN1_R_INVALID_TIME_FORMAT ,"invalid time format"},
219{ASN1_R_IV_TOO_LARGE ,"iv too large"},
220{ASN1_R_LENGTH_ERROR ,"length error"},
221{ASN1_R_LENGTH_MISMATCH ,"length mismatch"},
222{ASN1_R_MISSING_EOS ,"missing eos"},
223{ASN1_R_MISSING_SECOND_NUMBER ,"missing second number"},
224{ASN1_R_NON_HEX_CHARACTERS ,"non hex characters"},
225{ASN1_R_NOT_ENOUGH_DATA ,"not enough data"},
226{ASN1_R_ODD_NUMBER_OF_CHARS ,"odd number of chars"},
227{ASN1_R_PARSING ,"parsing"},
228{ASN1_R_PRIVATE_KEY_HEADER_MISSING ,"private key header missing"},
229{ASN1_R_SECOND_NUMBER_TOO_LARGE ,"second number too large"},
230{ASN1_R_SHORT_LINE ,"short line"},
231{ASN1_R_STRING_TOO_SHORT ,"string too short"},
232{ASN1_R_TAG_VALUE_TOO_HIGH ,"tag value too high"},
233{ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD,"the asn1 object identifier is not known for this md"},
234{ASN1_R_TOO_LONG ,"too long"},
235{ASN1_R_UNABLE_TO_DECODE_RSA_KEY ,"unable to decode rsa key"},
236{ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY ,"unable to decode rsa private key"},
237{ASN1_R_UNKNOWN_ATTRIBUTE_TYPE ,"unknown attribute type"},
238{ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM ,"unknown message digest algorithm"},
239{ASN1_R_UNKNOWN_OBJECT_TYPE ,"unknown object type"},
240{ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE ,"unknown public key type"},
241{ASN1_R_UNSUPPORTED_CIPHER ,"unsupported cipher"},
242{ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM ,"unsupported encryption algorithm"},
243{ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE ,"unsupported public key type"},
244{ASN1_R_UTCTIME_TOO_LONG ,"utctime too long"},
245{ASN1_R_WRONG_PRINTABLE_TYPE ,"wrong printable type"},
246{ASN1_R_WRONG_TAG ,"wrong tag"},
247{ASN1_R_WRONG_TYPE ,"wrong type"},
248{0,NULL},
249 };
250
251#endif
252
253void ERR_load_ASN1_strings()
254 {
255 static int init=1;
256
257 if (init);
258 {;
259 init=0;
260#ifndef NO_ERR
261 ERR_load_strings(ERR_LIB_ASN1,ASN1_str_functs);
262 ERR_load_strings(ERR_LIB_ASN1,ASN1_str_reasons);
263#endif
264
265 }
266 }
diff --git a/src/lib/libcrypto/asn1/asn1_lib.c b/src/lib/libcrypto/asn1/asn1_lib.c
new file mode 100644
index 0000000000..ff30b25836
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_lib.c
@@ -0,0 +1,444 @@
1/* crypto/asn1/asn1_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62#include "asn1_mac.h"
63
64#ifndef NOPROTO
65static int asn1_get_length(unsigned char **pp,int *inf,long *rl,int max);
66static void asn1_put_length(unsigned char **pp, int length);
67#else
68static int asn1_get_length();
69static void asn1_put_length();
70#endif
71
72char *ASN1_version="ASN1 part of SSLeay 0.9.0b 29-Jun-1998";
73
74int ASN1_check_infinite_end(p,len)
75unsigned char **p;
76long len;
77 {
78 /* If there is 0 or 1 byte left, the length check should pick
79 * things up */
80 if (len <= 0)
81 return(1);
82 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0))
83 {
84 (*p)+=2;
85 return(1);
86 }
87 return(0);
88 }
89
90
91int ASN1_get_object(pp, plength, ptag, pclass, omax)
92unsigned char **pp;
93long *plength;
94int *ptag;
95int *pclass;
96long omax;
97 {
98 int i,ret;
99 long l;
100 unsigned char *p= *pp;
101 int tag,xclass,inf;
102 long max=omax;
103
104 if (!max) goto err;
105 ret=(*p&V_ASN1_CONSTRUCTED);
106 xclass=(*p&V_ASN1_PRIVATE);
107 i= *p&V_ASN1_PRIMATIVE_TAG;
108 if (i == V_ASN1_PRIMATIVE_TAG)
109 { /* high-tag */
110 p++;
111 if (--max == 0) goto err;
112 l=0;
113 while (*p&0x80)
114 {
115 l<<=7L;
116 l|= *(p++)&0x7f;
117 if (--max == 0) goto err;
118 }
119 l<<=7L;
120 l|= *(p++)&0x7f;
121 tag=(int)l;
122 }
123 else
124 {
125 tag=i;
126 p++;
127 if (--max == 0) goto err;
128 }
129 *ptag=tag;
130 *pclass=xclass;
131 if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err;
132
133#ifdef undef
134 fprintf(stderr,"p=%d + *plength=%d > omax=%d + *pp=%d (%d > %d)\n",
135 p,*plength,omax,*pp,(p+ *plength),omax+ *pp);
136
137#endif
138 if ((p+ *plength) > (omax+ *pp))
139 {
140 ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG);
141 /* Set this so that even if things are not long enough
142 * the values are set correctly */
143 ret|=0x80;
144 }
145 *pp=p;
146 return(ret+inf);
147err:
148 ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_HEADER_TOO_LONG);
149 return(0x80);
150 }
151
152static int asn1_get_length(pp,inf,rl,max)
153unsigned char **pp;
154int *inf;
155long *rl;
156int max;
157 {
158 unsigned char *p= *pp;
159 long ret=0;
160 int i;
161
162 if (max-- < 1) return(0);
163 if (*p == 0x80)
164 {
165 *inf=1;
166 ret=0;
167 p++;
168 }
169 else
170 {
171 *inf=0;
172 i= *p&0x7f;
173 if (*(p++) & 0x80)
174 {
175 if (max-- == 0) return(0);
176 while (i-- > 0)
177 {
178 ret<<=8L;
179 ret|= *(p++);
180 if (max-- == 0) return(0);
181 }
182 }
183 else
184 ret=i;
185 }
186 *pp=p;
187 *rl=ret;
188 return(1);
189 }
190
191/* class 0 is constructed
192 * constructed == 2 for indefinitle length constructed */
193void ASN1_put_object(pp,constructed,length,tag,xclass)
194unsigned char **pp;
195int constructed;
196int length;
197int tag;
198int xclass;
199 {
200 unsigned char *p= *pp;
201 int i;
202
203 i=(constructed)?V_ASN1_CONSTRUCTED:0;
204 i|=(xclass&V_ASN1_PRIVATE);
205 if (tag < 31)
206 *(p++)=i|(tag&V_ASN1_PRIMATIVE_TAG);
207 else
208 {
209 *(p++)=i|V_ASN1_PRIMATIVE_TAG;
210 while (tag > 0x7f)
211 {
212 *(p++)=(tag&0x7f)|0x80;
213 tag>>=7;
214 }
215 *(p++)=(tag&0x7f);
216 }
217 if ((constructed == 2) && (length == 0))
218 *(p++)=0x80; /* der_put_length would output 0 instead */
219 else
220 asn1_put_length(&p,length);
221 *pp=p;
222 }
223
224static void asn1_put_length(pp, length)
225unsigned char **pp;
226int length;
227 {
228 unsigned char *p= *pp;
229 int i,l;
230 if (length <= 127)
231 *(p++)=(unsigned char)length;
232 else
233 {
234 l=length;
235 for (i=0; l > 0; i++)
236 l>>=8;
237 *(p++)=i|0x80;
238 l=i;
239 while (i-- > 0)
240 {
241 p[i]=length&0xff;
242 length>>=8;
243 }
244 p+=l;
245 }
246 *pp=p;
247 }
248
249int ASN1_object_size(constructed, length, tag)
250int constructed;
251int length;
252int tag;
253 {
254 int ret;
255
256 ret=length;
257 ret++;
258 if (tag >= 31)
259 {
260 while (tag > 0)
261 {
262 tag>>=7;
263 ret++;
264 }
265 }
266 if ((length == 0) && (constructed == 2))
267 ret+=2;
268 ret++;
269 if (length > 127)
270 {
271 while (length > 0)
272 {
273 length>>=8;
274 ret++;
275 }
276 }
277 return(ret);
278 }
279
280int asn1_Finish(c)
281ASN1_CTX *c;
282 {
283 if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos))
284 {
285 if (!ASN1_check_infinite_end(&c->p,c->slen))
286 {
287 c->error=ASN1_R_MISSING_EOS;
288 return(0);
289 }
290 }
291 if ( ((c->slen != 0) && !(c->inf & 1)) ||
292 ((c->slen < 0) && (c->inf & 1)))
293 {
294 c->error=ASN1_R_LENGTH_MISMATCH;
295 return(0);
296 }
297 return(1);
298 }
299
300int asn1_GetSequence(c,length)
301ASN1_CTX *c;
302long *length;
303 {
304 unsigned char *q;
305
306 q=c->p;
307 c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass),
308 *length);
309 if (c->inf & 0x80)
310 {
311 c->error=ASN1_R_BAD_GET_OBJECT;
312 return(0);
313 }
314 if (c->tag != V_ASN1_SEQUENCE)
315 {
316 c->error=ASN1_R_EXPECTING_A_SEQUENCE;
317 return(0);
318 }
319 (*length)-=(c->p-q);
320 if (c->max && (*length < 0))
321 {
322 c->error=ASN1_R_LENGTH_MISMATCH;
323 return(0);
324 }
325 if (c->inf == (1|V_ASN1_CONSTRUCTED))
326 c->slen= *length+ *(c->pp)-c->p;
327 c->eos=0;
328 return(1);
329 }
330
331ASN1_STRING *ASN1_STRING_dup(str)
332ASN1_STRING *str;
333 {
334 ASN1_STRING *ret;
335
336 if (str == NULL) return(NULL);
337 if ((ret=ASN1_STRING_type_new(str->type)) == NULL)
338 return(NULL);
339 if (!ASN1_STRING_set(ret,str->data,str->length))
340 {
341 ASN1_STRING_free(ret);
342 return(NULL);
343 }
344 return(ret);
345 }
346
347int ASN1_STRING_set(str,data,len)
348ASN1_STRING *str;
349unsigned char *data;
350int len;
351 {
352 char *c;
353
354 if (len < 0)
355 {
356 if (data == NULL)
357 return(0);
358 else
359 len=strlen((char *)data);
360 }
361 if ((str->length < len) || (str->data == NULL))
362 {
363 c=(char *)str->data;
364 if (c == NULL)
365 str->data=(unsigned char *)Malloc(len+1);
366 else
367 str->data=(unsigned char *)Realloc(c,len+1);
368
369 if (str->data == NULL)
370 {
371 str->data=(unsigned char *)c;
372 return(0);
373 }
374 }
375 str->length=len;
376 if (data != NULL)
377 {
378 memcpy(str->data,data,len);
379 /* an alowance for strings :-) */
380 str->data[len]='\0';
381 }
382 return(1);
383 }
384
385ASN1_STRING *ASN1_STRING_new()
386 {
387 return(ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
388 }
389
390
391ASN1_STRING *ASN1_STRING_type_new(type)
392int type;
393 {
394 ASN1_STRING *ret;
395
396 ret=(ASN1_STRING *)Malloc(sizeof(ASN1_STRING));
397 if (ret == NULL)
398 {
399 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);
400 return(NULL);
401 }
402 ret->length=0;
403 ret->type=type;
404 ret->data=NULL;
405 return(ret);
406 }
407
408void ASN1_STRING_free(a)
409ASN1_STRING *a;
410 {
411 if (a == NULL) return;
412 if (a->data != NULL) Free((char *)a->data);
413 Free((char *)a);
414 }
415
416int ASN1_STRING_cmp(a,b)
417ASN1_STRING *a,*b;
418 {
419 int i;
420
421 i=(a->length-b->length);
422 if (i == 0)
423 {
424 i=memcmp(a->data,b->data,a->length);
425 if (i == 0)
426 return(a->type-b->type);
427 else
428 return(i);
429 }
430 else
431 return(i);
432 }
433
434void asn1_add_error(address,offset)
435unsigned char *address;
436int offset;
437 {
438 char buf1[16],buf2[16];
439
440 sprintf(buf1,"%lu",(unsigned long)address);
441 sprintf(buf2,"%d",offset);
442 ERR_add_error_data(4,"address=",buf1," offset=",buf2);
443 }
444
diff --git a/src/lib/libcrypto/asn1/asn1_mac.h b/src/lib/libcrypto/asn1/asn1_mac.h
new file mode 100644
index 0000000000..4fba70e4bb
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_mac.h
@@ -0,0 +1,321 @@
1/* crypto/asn1/asn1_mac.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_ASN1_MAC_H
60#define HEADER_ASN1_MAC_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "asn1.h"
67#include "x509.h"
68#include "pkcs7.h"
69
70#define M_ASN1_D2I_vars(a,type,func) \
71 ASN1_CTX c; \
72 type ret=NULL; \
73 \
74 c.pp=pp; \
75 c.error=ASN1_R_ERROR_STACK; \
76 if ((a == NULL) || ((*a) == NULL)) \
77 { if ((ret=(type)func()) == NULL) goto err; } \
78 else ret=(*a);
79
80#define M_ASN1_D2I_Init() \
81 c.p= *pp; \
82 c.max=(length == 0)?0:(c.p+length);
83
84#define M_ASN1_D2I_Finish_2(a) \
85 if (!asn1_Finish(&c)) goto err; \
86 *pp=c.p; \
87 if (a != NULL) (*a)=ret; \
88 return(ret);
89
90#define M_ASN1_D2I_Finish(a,func,e) \
91 M_ASN1_D2I_Finish_2(a); \
92err:\
93 ASN1err((e),c.error); \
94 asn1_add_error(*pp,(int)(c.q- *pp)); \
95 if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
96 return(NULL)
97
98#define M_ASN1_D2I_start_sequence() \
99 if (!asn1_GetSequence(&c,&length)) goto err;
100
101#define M_ASN1_D2I_end_sequence() \
102 (((c.inf&1) == 0)?(c.slen <= 0): \
103 (c.eos=ASN1_check_infinite_end(&c.p,c.slen)))
104
105#define M_ASN1_D2I_get(b,func) \
106 c.q=c.p; \
107 if (func(&(b),&c.p,c.slen) == NULL) goto err; \
108 c.slen-=(c.p-c.q);
109
110#define M_ASN1_D2I_get_opt(b,func,type) \
111 if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
112 == (V_ASN1_UNIVERSAL|(type)))) \
113 { \
114 M_ASN1_D2I_get(b,func); \
115 }
116
117#define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \
118 if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \
119 (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \
120 { \
121 unsigned char tmp; \
122 tmp=M_ASN1_next; \
123 M_ASN1_next=(tmp& ~V_ASN1_PRIMATIVE_TAG)|type; \
124 M_ASN1_D2I_get(b,func); \
125 M_ASN1_next_prev=tmp; \
126 }
127
128#define M_ASN1_D2I_get_set(r,func) \
129 M_ASN1_D2I_get_imp_set(r,func,V_ASN1_SET,V_ASN1_UNIVERSAL);
130
131#define M_ASN1_D2I_get_IMP_set_opt(b,func,tag) \
132 if ((c.slen != 0) && \
133 (M_ASN1_next == \
134 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
135 { \
136 M_ASN1_D2I_get_imp_set(b,func,tag,V_ASN1_CONTEXT_SPECIFIC); \
137 }
138
139#define M_ASN1_D2I_get_seq(r,func) \
140 M_ASN1_D2I_get_imp_set(r,func,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
141
142#define M_ASN1_D2I_get_seq_opt(r,func) \
143 if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
144 V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
145 { M_ASN1_D2I_get_seq(r,func); }
146
147#define M_ASN1_D2I_get_IMP_set(r,func,x) \
148 M_ASN1_D2I_get_imp_set(r,func,x,V_ASN1_CONTEXT_SPECIFIC);
149
150#define M_ASN1_D2I_get_imp_set(r,func,a,b) \
151 c.q=c.p; \
152 if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,a,b) == NULL) \
153 goto err; \
154 c.slen-=(c.p-c.q);
155
156#define M_ASN1_D2I_get_set_strings(r,func,a,b) \
157 c.q=c.p; \
158 if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
159 goto err; \
160 c.slen-=(c.p-c.q);
161
162#define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
163 if ((c.slen != 0L) && (M_ASN1_next == \
164 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
165 { \
166 int Tinf,Ttag,Tclass; \
167 long Tlen; \
168 \
169 c.q=c.p; \
170 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
171 if (Tinf & 0x80) \
172 { c.error=ASN1_R_BAD_OBJECT_HEADER; goto err; } \
173 if (func(&(r),&c.p,Tlen) == NULL) \
174 goto err; \
175 c.slen-=(c.p-c.q); \
176 }
177
178#define M_ASN1_D2I_get_EXP_set_opt(r,func,tag,b) \
179 if ((c.slen != 0) && (M_ASN1_next == \
180 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
181 { \
182 int Tinf,Ttag,Tclass; \
183 long Tlen; \
184 \
185 c.q=c.p; \
186 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
187 if (Tinf & 0x80) \
188 { c.error=ASN1_R_BAD_OBJECT_HEADER; goto err; } \
189 if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \
190 b,V_ASN1_UNIVERSAL) == NULL) \
191 goto err; \
192 c.slen-=(c.p-c.q); \
193 }
194
195/* New macros */
196#define M_ASN1_New_Malloc(ret,type) \
197 if ((ret=(type *)Malloc(sizeof(type))) == NULL) goto err2;
198
199#define M_ASN1_New(arg,func) \
200 if (((arg)=func()) == NULL) return(NULL)
201
202#define M_ASN1_New_Error(a) \
203/* err: ASN1err((a),ASN1_R_ERROR_STACK); \
204 return(NULL);*/ \
205 err2: ASN1err((a),ERR_R_MALLOC_FAILURE); \
206 return(NULL)
207
208
209#define M_ASN1_next (*c.p)
210#define M_ASN1_next_prev (*c.q)
211
212/*************************************************/
213
214#define M_ASN1_I2D_vars(a) int r=0,ret=0; \
215 unsigned char *p; \
216 if (a == NULL) return(0)
217
218/* Length Macros */
219#define M_ASN1_I2D_len(a,f) ret+=f(a,NULL)
220#define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f)
221
222#define M_ASN1_I2D_len_SET(a,f) \
223 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL);
224
225#define M_ASN1_I2D_len_SEQ(a,f) \
226 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
227
228#define M_ASN1_I2D_len_SEQ_opt(a,f) \
229 if ((a != NULL) && (sk_num(a) != 0)) \
230 M_ASN1_I2D_len_SEQ(a,f);
231
232#define M_ASN1_I2D_len_IMP_set(a,f,x) \
233 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC);
234
235#define M_ASN1_I2D_len_IMP_set_opt(a,f,x) \
236 if ((a != NULL) && (sk_num(a) != 0)) \
237 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC);
238
239#define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
240 if (a != NULL)\
241 { \
242 v=f(a,NULL); \
243 ret+=ASN1_object_size(1,v,mtag); \
244 }
245
246#define M_ASN1_I2D_len_EXP_set_opt(a,f,mtag,tag,v) \
247 if ((a != NULL) && (sk_num(a) != 0))\
248 { \
249 v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL); \
250 ret+=ASN1_object_size(1,v,mtag); \
251 }
252
253/* Put Macros */
254#define M_ASN1_I2D_put(a,f) f(a,&p)
255
256#define M_ASN1_I2D_put_IMP_opt(a,f,t) \
257 if (a != NULL) \
258 { \
259 unsigned char *q=p; \
260 f(a,&p); \
261 *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
262 }
263
264#define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
265 V_ASN1_UNIVERSAL)
266#define M_ASN1_I2D_put_IMP_set(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
267 V_ASN1_CONTEXT_SPECIFIC)
268
269#define M_ASN1_I2D_put_SEQ(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
270 V_ASN1_UNIVERSAL)
271
272#define M_ASN1_I2D_put_SEQ_opt(a,f) \
273 if ((a != NULL) && (sk_num(a) != 0)) \
274 M_ASN1_I2D_put_SEQ(a,f);
275
276#define M_ASN1_I2D_put_IMP_set_opt(a,f,x) \
277 if ((a != NULL) && (sk_num(a) != 0)) \
278 { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC); }
279
280#define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
281 if (a != NULL) \
282 { \
283 ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
284 f(a,&p); \
285 }
286
287#define M_ASN1_I2D_put_EXP_set_opt(a,f,mtag,tag,v) \
288 if ((a != NULL) && (sk_num(a) != 0)) \
289 { \
290 ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
291 i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL); \
292 }
293
294#define M_ASN1_I2D_seq_total() \
295 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
296 if (pp == NULL) return(r); \
297 p= *pp; \
298 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
299
300#define M_ASN1_I2D_INF_seq_start(tag,ctx) \
301 *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
302 *(p++)=0x80
303
304#define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
305
306#define M_ASN1_I2D_finish() *pp=p; \
307 return(r);
308
309#ifndef NOPROTO
310int asn1_GetSequence(ASN1_CTX *c, long *length);
311void asn1_add_error(unsigned char *address,int offset);
312#else
313int asn1_GetSequence();
314void asn1_add_error();
315#endif
316
317#ifdef __cplusplus
318}
319#endif
320
321#endif
diff --git a/src/lib/libcrypto/asn1/asn1_par.c b/src/lib/libcrypto/asn1/asn1_par.c
new file mode 100644
index 0000000000..3906227d21
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_par.c
@@ -0,0 +1,393 @@
1/* crypto/asn1/asn1_par.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "objects.h"
63#include "x509.h"
64
65#ifndef NOPROTO
66static int asn1_print_info(BIO *bp, int tag, int xclass,int constructed,
67 int indent);
68static int asn1_parse2(BIO *bp, unsigned char **pp, long length,
69 int offset, int depth, int indent);
70#else
71static int asn1_print_info();
72static int asn1_parse2();
73#endif
74
75static int asn1_print_info(bp, tag, xclass, constructed,indent)
76BIO *bp;
77int tag;
78int xclass;
79int constructed;
80int indent;
81 {
82 static char *fmt="%-18s";
83 static char *fmt2="%2d %-15s";
84 char *p,str[128],*p2=NULL;
85
86 if (constructed & V_ASN1_CONSTRUCTED)
87 p="cons: ";
88 else
89 p="prim: ";
90 if (BIO_write(bp,p,6) < 6) goto err;
91 if (indent)
92 {
93 if (indent > 128) indent=128;
94 memset(str,' ',indent);
95 if (BIO_write(bp,str,indent) < indent) goto err;
96 }
97
98 p=str;
99 if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
100 sprintf(str,"priv [ %d ] ",tag);
101 else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
102 sprintf(str,"cont [ %d ]",tag);
103 else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
104 sprintf(str,"appl [ %d ]",tag);
105 else if ((tag == V_ASN1_EOC) /* && (xclass == V_ASN1_UNIVERSAL) */)
106 p="EOC";
107 else if (tag == V_ASN1_BOOLEAN)
108 p="BOOLEAN";
109 else if (tag == V_ASN1_INTEGER)
110 p="INTEGER";
111 else if (tag == V_ASN1_BIT_STRING)
112 p="BIT STRING";
113 else if (tag == V_ASN1_OCTET_STRING)
114 p="OCTET STRING";
115 else if (tag == V_ASN1_NULL)
116 p="NULL";
117 else if (tag == V_ASN1_OBJECT)
118 p="OBJECT";
119 else if (tag == V_ASN1_SEQUENCE)
120 p="SEQUENCE";
121 else if (tag == V_ASN1_SET)
122 p="SET";
123 else if (tag == V_ASN1_PRINTABLESTRING)
124 p="PRINTABLESTRING";
125 else if (tag == V_ASN1_T61STRING)
126 p="T61STRING";
127 else if (tag == V_ASN1_IA5STRING)
128 p="IA5STRING";
129 else if (tag == V_ASN1_UTCTIME)
130 p="UTCTIME";
131
132 /* extras */
133 else if (tag == V_ASN1_NUMERICSTRING)
134 p="NUMERICSTRING";
135 else if (tag == V_ASN1_VIDEOTEXSTRING)
136 p="VIDEOTEXSTRING";
137 else if (tag == V_ASN1_GENERALIZEDTIME)
138 p="GENERALIZEDTIME";
139 else if (tag == V_ASN1_GRAPHICSTRING)
140 p="GRAPHICSTRING";
141 else if (tag == V_ASN1_ISO64STRING)
142 p="ISO64STRING";
143 else if (tag == V_ASN1_GENERALSTRING)
144 p="GENERALSTRING";
145 else if (tag == V_ASN1_UNIVERSALSTRING)
146 p="UNIVERSALSTRING";
147 else if (tag == V_ASN1_BMPSTRING)
148 p="BMPSTRING";
149 else
150 p2="(unknown)";
151
152 if (p2 != NULL)
153 {
154 if (BIO_printf(bp,fmt2,tag,p2) <= 0) goto err;
155 }
156 else
157 {
158 if (BIO_printf(bp,fmt,p) <= 0) goto err;
159 }
160 return(1);
161err:
162 return(0);
163 }
164
165int ASN1_parse(bp, pp, len, indent)
166BIO *bp;
167unsigned char *pp;
168long len;
169int indent;
170 {
171 return(asn1_parse2(bp,&pp,len,0,0,indent));
172 }
173
174static int asn1_parse2(bp, pp, length, offset, depth, indent)
175BIO *bp;
176unsigned char **pp;
177long length;
178int offset;
179int depth;
180int indent;
181 {
182 unsigned char *p,*ep,*tot,*op,*opp;
183 long len;
184 int tag,xclass,ret=0;
185 int nl,hl,j,r;
186 ASN1_OBJECT *o=NULL;
187 ASN1_OCTET_STRING *os=NULL;
188 /* ASN1_BMPSTRING *bmp=NULL;*/
189
190 p= *pp;
191 tot=p+length;
192 op=p-1;
193 while ((p < tot) && (op < p))
194 {
195 op=p;
196 j=ASN1_get_object(&p,&len,&tag,&xclass,length);
197#ifdef LINT
198 j=j;
199#endif
200 if (j & 0x80)
201 {
202 if (BIO_write(bp,"Error in encoding\n",18) <= 0)
203 goto end;
204 ret=0;
205 goto end;
206 }
207 hl=(p-op);
208 length-=hl;
209 /* if j == 0x21 it is a constructed indefinite length object */
210 if (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp))
211 <= 0) goto end;
212
213 if (j != (V_ASN1_CONSTRUCTED | 1))
214 {
215 if (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ",
216 depth,(long)hl,len) <= 0)
217 goto end;
218 }
219 else
220 {
221 if (BIO_printf(bp,"d=%-2d hl=%ld l=inf ",
222 depth,(long)hl) <= 0)
223 goto end;
224 }
225 if (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0))
226 goto end;
227 if (j & V_ASN1_CONSTRUCTED)
228 {
229 ep=p+len;
230 if (BIO_write(bp,"\n",1) <= 0) goto end;
231 if (len > length)
232 {
233 BIO_printf(bp,
234 "length is greater than %ld\n",length);
235 ret=0;
236 goto end;
237 }
238 if ((j == 0x21) && (len == 0))
239 {
240 for (;;)
241 {
242 r=asn1_parse2(bp,&p,(long)(tot-p),
243 offset+(p - *pp),depth+1,
244 indent);
245 if (r == 0) { ret=0; goto end; }
246 if ((r == 2) || (p >= tot)) break;
247 }
248 }
249 else
250 while (p < ep)
251 {
252 r=asn1_parse2(bp,&p,(long)len,
253 offset+(p - *pp),depth+1,
254 indent);
255 if (r == 0) { ret=0; goto end; }
256 }
257 }
258 else if (xclass != 0)
259 {
260 p+=len;
261 if (BIO_write(bp,"\n",1) <= 0) goto end;
262 }
263 else
264 {
265 nl=0;
266 if ( (tag == V_ASN1_PRINTABLESTRING) ||
267 (tag == V_ASN1_T61STRING) ||
268 (tag == V_ASN1_IA5STRING) ||
269 (tag == V_ASN1_UTCTIME))
270 {
271 if (BIO_write(bp,":",1) <= 0) goto end;
272 if ((len > 0) &&
273 BIO_write(bp,(char *)p,(int)len)
274 != (int)len)
275 goto end;
276 }
277 else if (tag == V_ASN1_OBJECT)
278 {
279 opp=op;
280 if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL)
281 {
282 if (BIO_write(bp,":",1) <= 0) goto end;
283 i2a_ASN1_OBJECT(bp,o);
284 }
285 else
286 {
287 if (BIO_write(bp,":BAD OBJECT",11) <= 0)
288 goto end;
289 }
290 }
291 else if (tag == V_ASN1_BOOLEAN)
292 {
293 int ii;
294
295 opp=op;
296 ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl);
297 if (ii < 0)
298 {
299 if (BIO_write(bp,"Bad boolean\n",12))
300 goto end;
301 }
302 BIO_printf(bp,":%d",ii);
303 }
304 else if (tag == V_ASN1_BMPSTRING)
305 {
306 /* do the BMP thang */
307 }
308 else if (tag == V_ASN1_OCTET_STRING)
309 {
310 int i,printable=1;
311
312 opp=op;
313 os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl);
314 if (os != NULL)
315 {
316 opp=os->data;
317 for (i=0; i<os->length; i++)
318 {
319 if (( (opp[i] < ' ') &&
320 (opp[i] != '\n') &&
321 (opp[i] != '\r') &&
322 (opp[i] != '\t')) ||
323 (opp[i] > '~'))
324 {
325 printable=0;
326 break;
327 }
328 }
329 if (printable && (os->length > 0))
330 {
331 if (BIO_write(bp,":",1) <= 0)
332 goto end;
333 if (BIO_write(bp,(char *)opp,
334 os->length) <= 0)
335 goto end;
336 }
337 ASN1_OCTET_STRING_free(os);
338 os=NULL;
339 }
340 }
341 else if (tag == V_ASN1_INTEGER)
342 {
343 ASN1_INTEGER *bs;
344 int i;
345
346 opp=op;
347 bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl);
348 if (bs != NULL)
349 {
350 if (BIO_write(bp,":",1) <= 0) goto end;
351 if (bs->type == V_ASN1_NEG_INTEGER)
352 if (BIO_write(bp,"-",1) <= 0)
353 goto end;
354 for (i=0; i<bs->length; i++)
355 {
356 if (BIO_printf(bp,"%02X",
357 bs->data[i]) <= 0)
358 goto end;
359 }
360 if (bs->length == 0)
361 {
362 if (BIO_write(bp,"00",2) <= 0)
363 goto end;
364 }
365 }
366 else
367 {
368 if (BIO_write(bp,"BAD INTEGER",11) <= 0)
369 goto end;
370 }
371 ASN1_INTEGER_free(bs);
372 }
373
374 if (!nl)
375 {
376 if (BIO_write(bp,"\n",1) <= 0) goto end;
377 }
378 p+=len;
379 if ((tag == V_ASN1_EOC) && (xclass == 0))
380 {
381 ret=2; /* End of sequence */
382 goto end;
383 }
384 }
385 length-=len;
386 }
387 ret=1;
388end:
389 if (o != NULL) ASN1_OBJECT_free(o);
390 if (os != NULL) ASN1_OCTET_STRING_free(os);
391 *pp=p;
392 return(ret);
393 }
diff --git a/src/lib/libcrypto/asn1/d2i_pr.c b/src/lib/libcrypto/asn1/d2i_pr.c
new file mode 100644
index 0000000000..b9eaa9629b
--- /dev/null
+++ b/src/lib/libcrypto/asn1/d2i_pr.c
@@ -0,0 +1,117 @@
1/* crypto/asn1/d2i_pr.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "evp.h"
63#include "objects.h"
64#include "x509.h"
65
66EVP_PKEY *d2i_PrivateKey(type,a,pp,length)
67int type;
68EVP_PKEY **a;
69unsigned char **pp;
70long length;
71 {
72 EVP_PKEY *ret;
73
74 if ((a == NULL) || (*a == NULL))
75 {
76 if ((ret=EVP_PKEY_new()) == NULL)
77 {
78 ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_EVP_LIB);
79 return(NULL);
80 }
81 }
82 else ret= *a;
83
84 ret->save_type=type;
85 ret->type=EVP_PKEY_type(type);
86 switch (ret->type)
87 {
88#ifndef NO_RSA
89 case EVP_PKEY_RSA:
90 if ((ret->pkey.rsa=d2i_RSAPrivateKey(NULL,pp,length)) == NULL)
91 {
92 ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB);
93 goto err;
94 }
95 break;
96#endif
97#ifndef NO_DSA
98 case EVP_PKEY_DSA:
99 if ((ret->pkey.dsa=d2i_DSAPrivateKey(NULL,pp,length)) == NULL)
100 {
101 ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB);
102 goto err;
103 }
104 break;
105#endif
106 default:
107 ASN1err(ASN1_F_D2I_PRIVATEKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
108 goto err;
109 break;
110 }
111 if (a != NULL) (*a)=ret;
112 return(ret);
113err:
114 if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);
115 return(NULL);
116 }
117
diff --git a/src/lib/libcrypto/asn1/d2i_pu.c b/src/lib/libcrypto/asn1/d2i_pu.c
new file mode 100644
index 0000000000..5d6192f1e5
--- /dev/null
+++ b/src/lib/libcrypto/asn1/d2i_pu.c
@@ -0,0 +1,117 @@
1/* crypto/asn1/d2i_pu.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "evp.h"
63#include "objects.h"
64#include "x509.h"
65
66EVP_PKEY *d2i_PublicKey(type,a,pp,length)
67int type;
68EVP_PKEY **a;
69unsigned char **pp;
70long length;
71 {
72 EVP_PKEY *ret;
73
74 if ((a == NULL) || (*a == NULL))
75 {
76 if ((ret=EVP_PKEY_new()) == NULL)
77 {
78 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB);
79 return(NULL);
80 }
81 }
82 else ret= *a;
83
84 ret->save_type=type;
85 ret->type=EVP_PKEY_type(type);
86 switch (ret->type)
87 {
88#ifndef NO_RSA
89 case EVP_PKEY_RSA:
90 if ((ret->pkey.rsa=d2i_RSAPublicKey(NULL,pp,length)) == NULL)
91 {
92 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
93 goto err;
94 }
95 break;
96#endif
97#ifndef NO_DSA
98 case EVP_PKEY_DSA:
99 if ((ret->pkey.dsa=d2i_DSAPublicKey(NULL,pp,length)) == NULL)
100 {
101 ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
102 goto err;
103 }
104 break;
105#endif
106 default:
107 ASN1err(ASN1_F_D2I_PUBLICKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
108 goto err;
109 break;
110 }
111 if (a != NULL) (*a)=ret;
112 return(ret);
113err:
114 if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);
115 return(NULL);
116 }
117
diff --git a/src/lib/libcrypto/asn1/evp_asn1.c b/src/lib/libcrypto/asn1/evp_asn1.c
new file mode 100644
index 0000000000..ebe34a3362
--- /dev/null
+++ b/src/lib/libcrypto/asn1/evp_asn1.c
@@ -0,0 +1,193 @@
1/* crypto/asn1/evp_asn1.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62#include "asn1_mac.h"
63
64int ASN1_TYPE_set_octetstring(a,data,len)
65ASN1_TYPE *a;
66unsigned char *data;
67int len;
68 {
69 ASN1_STRING *os;
70
71 if ((os=ASN1_OCTET_STRING_new()) == NULL) return(0);
72 if (!ASN1_OCTET_STRING_set(os,data,len)) return(0);
73 ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,(char *)os);
74 return(1);
75 }
76
77int ASN1_TYPE_get_octetstring(a,data,max_len)
78ASN1_TYPE *a;
79unsigned char *data;
80int max_len; /* for returned value */
81 {
82 int ret,num;
83 unsigned char *p;
84
85 if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL))
86 {
87 ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING,ASN1_R_DATA_IS_WRONG);
88 return(-1);
89 }
90 p=ASN1_STRING_data(a->value.octet_string);
91 ret=ASN1_STRING_length(a->value.octet_string);
92 if (ret < max_len)
93 num=ret;
94 else
95 num=max_len;
96 memcpy(data,p,num);
97 return(ret);
98 }
99
100int ASN1_TYPE_set_int_octetstring(a,num,data,len)
101ASN1_TYPE *a;
102long num;
103unsigned char *data;
104int len;
105 {
106 int n,size;
107 ASN1_OCTET_STRING os,*osp;
108 ASN1_INTEGER in;
109 unsigned char *p;
110 unsigned char buf[32]; /* when they have 256bit longs,
111 * I'll be in trouble */
112 in.data=buf;
113 in.length=32;
114 os.data=data;
115 os.type=V_ASN1_OCTET_STRING;
116 os.length=len;
117 ASN1_INTEGER_set(&in,num);
118 n = i2d_ASN1_INTEGER(&in,NULL);
119 n+=M_i2d_ASN1_OCTET_STRING(&os,NULL);
120
121 size=ASN1_object_size(1,n,V_ASN1_SEQUENCE);
122
123 if ((osp=ASN1_STRING_new()) == NULL) return(0);
124 /* Grow the 'string' */
125 ASN1_STRING_set(osp,NULL,size);
126
127 ASN1_STRING_length(osp)=size;
128 p=ASN1_STRING_data(osp);
129
130 ASN1_put_object(&p,1,n,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
131 i2d_ASN1_INTEGER(&in,&p);
132 M_i2d_ASN1_OCTET_STRING(&os,&p);
133
134 ASN1_TYPE_set(a,V_ASN1_SEQUENCE,(char *)osp);
135 return(1);
136 }
137
138/* we return the actual length... */
139int ASN1_TYPE_get_int_octetstring(a,num,data,max_len)
140ASN1_TYPE *a;
141long *num;
142unsigned char *data;
143int max_len; /* for returned value */
144 {
145 int ret= -1,n;
146 ASN1_INTEGER *ai=NULL;
147 ASN1_OCTET_STRING *os=NULL;
148 unsigned char *p;
149 long length;
150 ASN1_CTX c;
151
152 if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL))
153 {
154 goto err;
155 }
156 p=ASN1_STRING_data(a->value.sequence);
157 length=ASN1_STRING_length(a->value.sequence);
158
159 c.pp= &p;
160 c.p=p;
161 c.max=p+length;
162 c.error=ASN1_R_DATA_IS_WRONG;
163
164 M_ASN1_D2I_start_sequence();
165 c.q=c.p;
166 if ((ai=d2i_ASN1_INTEGER(NULL,&c.p,c.slen)) == NULL) goto err;
167 c.slen-=(c.p-c.q);
168 c.q=c.p;
169 if ((os=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL) goto err;
170 c.slen-=(c.p-c.q);
171 if (!M_ASN1_D2I_end_sequence()) goto err;
172
173 if (num != NULL)
174 *num=ASN1_INTEGER_get(ai);
175
176 ret=ASN1_STRING_length(os);
177 if (max_len > ret)
178 n=ret;
179 else
180 n=max_len;
181
182 if (data != NULL)
183 memcpy(data,ASN1_STRING_data(os),n);
184 if (0)
185 {
186err:
187 ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING,ASN1_R_DATA_IS_WRONG);
188 }
189 if (os != NULL) ASN1_OCTET_STRING_free(os);
190 if (ai != NULL) ASN1_INTEGER_free(ai);
191 return(ret);
192 }
193
diff --git a/src/lib/libcrypto/asn1/f_int.c b/src/lib/libcrypto/asn1/f_int.c
new file mode 100644
index 0000000000..4817c45cb7
--- /dev/null
+++ b/src/lib/libcrypto/asn1/f_int.c
@@ -0,0 +1,211 @@
1/* crypto/asn1/f_int.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "x509.h"
63
64int i2a_ASN1_INTEGER(bp, a)
65BIO *bp;
66ASN1_INTEGER *a;
67 {
68 int i,n=0;
69 static char *h="0123456789ABCDEF";
70 char buf[2];
71
72 if (a == NULL) return(0);
73
74 if (a->length == 0)
75 {
76 if (BIO_write(bp,"00",2) != 2) goto err;
77 n=2;
78 }
79 else
80 {
81 for (i=0; i<a->length; i++)
82 {
83 if ((i != 0) && (i%35 == 0))
84 {
85 if (BIO_write(bp,"\\\n",2) != 2) goto err;
86 n+=2;
87 }
88 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f];
89 buf[1]=h[((unsigned char)a->data[i] )&0x0f];
90 if (BIO_write(bp,buf,2) != 2) goto err;
91 n+=2;
92 }
93 }
94 return(n);
95err:
96 return(-1);
97 }
98
99int a2i_ASN1_INTEGER(bp,bs,buf,size)
100BIO *bp;
101ASN1_INTEGER *bs;
102char *buf;
103int size;
104 {
105 int ret=0;
106 int i,j,k,m,n,again,bufsize;
107 unsigned char *s=NULL,*sp;
108 unsigned char *bufp;
109 int num=0,slen=0,first=1;
110
111 bs->type=V_ASN1_INTEGER;
112
113 bufsize=BIO_gets(bp,buf,size);
114 for (;;)
115 {
116 if (bufsize < 1) goto err_sl;
117 i=bufsize;
118 if (buf[i-1] == '\n') buf[--i]='\0';
119 if (i == 0) goto err_sl;
120 if (buf[i-1] == '\r') buf[--i]='\0';
121 if (i == 0) goto err_sl;
122 again=(buf[i-1] == '\\');
123
124 for (j=0; j<i; j++)
125 {
126 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) ||
127 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
128 ((buf[j] >= 'A') && (buf[j] <= 'F'))))
129 {
130 i=j;
131 break;
132 }
133 }
134 buf[i]='\0';
135 /* We have now cleared all the crap off the end of the
136 * line */
137 if (i < 2) goto err_sl;
138
139 bufp=(unsigned char *)buf;
140 if (first)
141 {
142 first=0;
143 if ((bufp[0] == '0') && (buf[1] == '0'))
144 {
145 bufp+=2;
146 i-=2;
147 }
148 }
149 k=0;
150 i-=again;
151 if (i%2 != 0)
152 {
153 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_ODD_NUMBER_OF_CHARS);
154 goto err;
155 }
156 i/=2;
157 if (num+i > slen)
158 {
159 if (s == NULL)
160 sp=(unsigned char *)Malloc(
161 (unsigned int)num+i*2);
162 else
163 sp=(unsigned char *)Realloc(s,
164 (unsigned int)num+i*2);
165 if (sp == NULL)
166 {
167 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
168 if (s != NULL) Free((char *)s);
169 goto err;
170 }
171 s=sp;
172 slen=num+i*2;
173 }
174 for (j=0; j<i; j++,k+=2)
175 {
176 for (n=0; n<2; n++)
177 {
178 m=bufp[k+n];
179 if ((m >= '0') && (m <= '9'))
180 m-='0';
181 else if ((m >= 'a') && (m <= 'f'))
182 m=m-'a'+10;
183 else if ((m >= 'A') && (m <= 'F'))
184 m=m-'A'+10;
185 else
186 {
187 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_NON_HEX_CHARACTERS);
188 goto err;
189 }
190 s[num+j]<<=4;
191 s[num+j]|=m;
192 }
193 }
194 num+=i;
195 if (again)
196 bufsize=BIO_gets(bp,buf,size);
197 else
198 break;
199 }
200 bs->length=num;
201 bs->data=s;
202 ret=1;
203err:
204 if (0)
205 {
206err_sl:
207 ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_SHORT_LINE);
208 }
209 return(ret);
210 }
211
diff --git a/src/lib/libcrypto/asn1/f_string.c b/src/lib/libcrypto/asn1/f_string.c
new file mode 100644
index 0000000000..ab2837824e
--- /dev/null
+++ b/src/lib/libcrypto/asn1/f_string.c
@@ -0,0 +1,210 @@
1/* crypto/asn1/f_string.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "x509.h"
63
64int i2a_ASN1_STRING(bp, a, type)
65BIO *bp;
66ASN1_STRING *a;
67int type;
68 {
69 int i,n=0;
70 static char *h="0123456789ABCDEF";
71 char buf[2];
72
73 if (a == NULL) return(0);
74
75 if (a->length == 0)
76 {
77 if (BIO_write(bp,"0",1) != 1) goto err;
78 n=1;
79 }
80 else
81 {
82 for (i=0; i<a->length; i++)
83 {
84 if ((i != 0) && (i%35 == 0))
85 {
86 if (BIO_write(bp,"\\\n",2) != 2) goto err;
87 n+=2;
88 }
89 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f];
90 buf[1]=h[((unsigned char)a->data[i] )&0x0f];
91 if (BIO_write(bp,buf,2) != 2) goto err;
92 n+=2;
93 }
94 }
95 return(n);
96err:
97 return(-1);
98 }
99
100int a2i_ASN1_STRING(bp,bs,buf,size)
101BIO *bp;
102ASN1_STRING *bs;
103char *buf;
104int size;
105 {
106 int ret=0;
107 int i,j,k,m,n,again,bufsize;
108 unsigned char *s=NULL,*sp;
109 unsigned char *bufp;
110 int num=0,slen=0,first=1;
111
112 bufsize=BIO_gets(bp,buf,size);
113 for (;;)
114 {
115 if (bufsize < 1)
116 {
117 if (first)
118 break;
119 else
120 goto err_sl;
121 }
122 first=0;
123
124 i=bufsize;
125 if (buf[i-1] == '\n') buf[--i]='\0';
126 if (i == 0) goto err_sl;
127 if (buf[i-1] == '\r') buf[--i]='\0';
128 if (i == 0) goto err_sl;
129 again=(buf[i-1] == '\\');
130
131 for (j=i-1; j>0; j--)
132 {
133 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) ||
134 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
135 ((buf[j] >= 'A') && (buf[j] <= 'F'))))
136 {
137 i=j;
138 break;
139 }
140 }
141 buf[i]='\0';
142 /* We have now cleared all the crap off the end of the
143 * line */
144 if (i < 2) goto err_sl;
145
146 bufp=(unsigned char *)buf;
147
148 k=0;
149 i-=again;
150 if (i%2 != 0)
151 {
152 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_ODD_NUMBER_OF_CHARS);
153 goto err;
154 }
155 i/=2;
156 if (num+i > slen)
157 {
158 if (s == NULL)
159 sp=(unsigned char *)Malloc(
160 (unsigned int)num+i*2);
161 else
162 sp=(unsigned char *)Realloc(s,
163 (unsigned int)num+i*2);
164 if (sp == NULL)
165 {
166 ASN1err(ASN1_F_A2I_ASN1_STRING,ERR_R_MALLOC_FAILURE);
167 if (s != NULL) Free((char *)s);
168 goto err;
169 }
170 s=sp;
171 slen=num+i*2;
172 }
173 for (j=0; j<i; j++,k+=2)
174 {
175 for (n=0; n<2; n++)
176 {
177 m=bufp[k+n];
178 if ((m >= '0') && (m <= '9'))
179 m-='0';
180 else if ((m >= 'a') && (m <= 'f'))
181 m=m-'a'+10;
182 else if ((m >= 'A') && (m <= 'F'))
183 m=m-'A'+10;
184 else
185 {
186 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_NON_HEX_CHARACTERS);
187 goto err;
188 }
189 s[num+j]<<=4;
190 s[num+j]|=m;
191 }
192 }
193 num+=i;
194 if (again)
195 bufsize=BIO_gets(bp,buf,size);
196 else
197 break;
198 }
199 bs->length=num;
200 bs->data=s;
201 ret=1;
202err:
203 if (0)
204 {
205err_sl:
206 ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_SHORT_LINE);
207 }
208 return(ret);
209 }
210
diff --git a/src/lib/libcrypto/asn1/i2d_pr.c b/src/lib/libcrypto/asn1/i2d_pr.c
new file mode 100644
index 0000000000..b6b821d73c
--- /dev/null
+++ b/src/lib/libcrypto/asn1/i2d_pr.c
@@ -0,0 +1,86 @@
1/* crypto/asn1/i2d_pr.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "evp.h"
63#include "objects.h"
64
65int i2d_PrivateKey(a,pp)
66EVP_PKEY *a;
67unsigned char **pp;
68 {
69#ifndef NO_RSA
70 if (a->type == EVP_PKEY_RSA)
71 {
72 return(i2d_RSAPrivateKey(a->pkey.rsa,pp));
73 }
74 else
75#endif
76#ifndef NO_DSA
77 if (a->type == EVP_PKEY_DSA)
78 {
79 return(i2d_DSAPrivateKey(a->pkey.dsa,pp));
80 }
81#endif
82
83 ASN1err(ASN1_F_I2D_PRIVATEKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
84 return(-1);
85 }
86
diff --git a/src/lib/libcrypto/asn1/i2d_pu.c b/src/lib/libcrypto/asn1/i2d_pu.c
new file mode 100644
index 0000000000..1b854252b7
--- /dev/null
+++ b/src/lib/libcrypto/asn1/i2d_pu.c
@@ -0,0 +1,84 @@
1/* crypto/asn1/i2d_pu.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "evp.h"
63#include "objects.h"
64
65int i2d_PublicKey(a,pp)
66EVP_PKEY *a;
67unsigned char **pp;
68 {
69 switch (a->type)
70 {
71#ifndef NO_RSA
72 case EVP_PKEY_RSA:
73 return(i2d_RSAPublicKey(a->pkey.rsa,pp));
74#endif
75#ifndef NO_DSA
76 case EVP_PKEY_DSA:
77 return(i2d_DSAPublicKey(a->pkey.dsa,pp));
78#endif
79 default:
80 ASN1err(ASN1_F_I2D_PUBLICKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
81 return(-1);
82 }
83 }
84
diff --git a/src/lib/libcrypto/asn1/n_pkey.c b/src/lib/libcrypto/asn1/n_pkey.c
new file mode 100644
index 0000000000..5110c91bec
--- /dev/null
+++ b/src/lib/libcrypto/asn1/n_pkey.c
@@ -0,0 +1,365 @@
1/* crypto/asn1/n_pkey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "rsa.h"
62#include "objects.h"
63#include "asn1_mac.h"
64#include "evp.h"
65#include "x509.h"
66
67
68#ifndef NO_RC4
69
70typedef struct netscape_pkey_st
71 {
72 ASN1_INTEGER *version;
73 X509_ALGOR *algor;
74 ASN1_OCTET_STRING *private_key;
75 } NETSCAPE_PKEY;
76
77/*
78 * ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_LENGTH_MISMATCH);
79 * ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_DECODING_ERROR);
80 * ASN1err(ASN1_F_D2I_NETSCAPE_PKEY,ASN1_R_DECODING_ERROR);
81 * ASN1err(ASN1_F_NETSCAPE_PKEY_NEW,ASN1_R_DECODING_ERROR);
82 */
83#ifndef NOPROTO
84static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp);
85static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a,unsigned char **pp, long length);
86static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void);
87static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *);
88#else
89static int i2d_NETSCAPE_PKEY();
90static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY();
91static NETSCAPE_PKEY *NETSCAPE_PKEY_new();
92static void NETSCAPE_PKEY_free();
93#endif
94
95int i2d_Netscape_RSA(a,pp,cb)
96RSA *a;
97unsigned char **pp;
98int (*cb)();
99 {
100 int i,j,l[6];
101 NETSCAPE_PKEY *pkey;
102 unsigned char buf[256],*zz;
103 unsigned char key[EVP_MAX_KEY_LENGTH];
104 EVP_CIPHER_CTX ctx;
105 X509_ALGOR *alg=NULL;
106 ASN1_OCTET_STRING os,os2;
107 M_ASN1_I2D_vars(a);
108
109 if (a == NULL) return(0);
110
111#ifdef WIN32
112 r=r; /* shut the damn compiler up :-) */
113#endif
114
115 os.data=os2.data=NULL;
116 if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
117 if (!ASN1_INTEGER_set(pkey->version,0)) goto err;
118
119 if (pkey->algor->algorithm != NULL)
120 ASN1_OBJECT_free(pkey->algor->algorithm);
121 pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
122 if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
123 pkey->algor->parameter->type=V_ASN1_NULL;
124
125 l[0]=i2d_RSAPrivateKey(a,NULL);
126 pkey->private_key->length=l[0];
127
128 os2.length=i2d_NETSCAPE_PKEY(pkey,NULL);
129 l[1]=i2d_ASN1_OCTET_STRING(&os2,NULL);
130
131 if ((alg=X509_ALGOR_new()) == NULL) goto err;
132 if (alg->algorithm != NULL)
133 ASN1_OBJECT_free(alg->algorithm);
134 alg->algorithm=OBJ_nid2obj(NID_rc4);
135 if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
136 alg->parameter->type=V_ASN1_NULL;
137
138 l[2]=i2d_X509_ALGOR(alg,NULL);
139 l[3]=ASN1_object_size(1,l[2]+l[1],V_ASN1_SEQUENCE);
140
141 os.data=(unsigned char *)"private-key";
142 os.length=11;
143 l[4]=i2d_ASN1_OCTET_STRING(&os,NULL);
144
145 l[5]=ASN1_object_size(1,l[4]+l[3],V_ASN1_SEQUENCE);
146
147 if (pp == NULL)
148 {
149 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
150 if (alg != NULL) X509_ALGOR_free(alg);
151 return(l[5]);
152 }
153
154 if (pkey->private_key->data != NULL)
155 Free((char *)pkey->private_key->data);
156 if ((pkey->private_key->data=(unsigned char *)Malloc(l[0])) == NULL)
157 {
158 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
159 goto err;
160 }
161 zz=pkey->private_key->data;
162 i2d_RSAPrivateKey(a,&zz);
163
164 if ((os2.data=(unsigned char *)Malloc(os2.length)) == NULL)
165 {
166 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
167 goto err;
168 }
169 zz=os2.data;
170 i2d_NETSCAPE_PKEY(pkey,&zz);
171
172 if (cb == NULL)
173 cb=EVP_read_pw_string;
174 i=cb(buf,256,"Enter Private Key password:",1);
175 if (i != 0)
176 {
177 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ASN1_R_BAD_PASSWORD_READ);
178 goto err;
179 }
180 EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
181 strlen((char *)buf),1,key,NULL);
182 memset(buf,0,256);
183
184 EVP_CIPHER_CTX_init(&ctx);
185 EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL);
186 EVP_EncryptUpdate(&ctx,os2.data,&i,os2.data,os2.length);
187 EVP_EncryptFinal(&ctx,&(os2.data[i]),&j);
188 EVP_CIPHER_CTX_cleanup(&ctx);
189
190 p= *pp;
191 ASN1_put_object(&p,1,l[4]+l[3],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
192 i2d_ASN1_OCTET_STRING(&os,&p);
193 ASN1_put_object(&p,1,l[2]+l[1],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
194 i2d_X509_ALGOR(alg,&p);
195 i2d_ASN1_OCTET_STRING(&os2,&p);
196 ret=l[5];
197err:
198 if (os2.data != NULL) Free((char *)os2.data);
199 if (alg != NULL) X509_ALGOR_free(alg);
200 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
201 r=r;
202 return(ret);
203 }
204
205RSA *d2i_Netscape_RSA(a,pp,length,cb)
206RSA **a;
207unsigned char **pp;
208long length;
209int (*cb)();
210 {
211 RSA *ret=NULL;
212 ASN1_OCTET_STRING *os=NULL;
213 ASN1_CTX c;
214
215 c.pp=pp;
216 c.error=ASN1_R_DECODING_ERROR;
217
218 M_ASN1_D2I_Init();
219 M_ASN1_D2I_start_sequence();
220 M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
221 if ((os->length != 11) || (strncmp("private-key",
222 (char *)os->data,os->length) != 0))
223 {
224 ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
225 ASN1_BIT_STRING_free(os);
226 goto err;
227 }
228 ASN1_BIT_STRING_free(os);
229 c.q=c.p;
230 if ((ret=d2i_Netscape_RSA_2(a,&c.p,c.slen,cb)) == NULL) goto err;
231 c.slen-=(c.p-c.q);
232
233 M_ASN1_D2I_Finish(a,RSA_free,ASN1_F_D2I_NETSCAPE_RSA);
234 }
235
236RSA *d2i_Netscape_RSA_2(a,pp,length,cb)
237RSA **a;
238unsigned char **pp;
239long length;
240int (*cb)();
241 {
242 NETSCAPE_PKEY *pkey=NULL;
243 RSA *ret=NULL;
244 int i,j;
245 unsigned char buf[256],*zz;
246 unsigned char key[EVP_MAX_KEY_LENGTH];
247 EVP_CIPHER_CTX ctx;
248 X509_ALGOR *alg=NULL;
249 ASN1_OCTET_STRING *os=NULL;
250 ASN1_CTX c;
251
252 c.error=ASN1_R_ERROR_STACK;
253 c.pp=pp;
254
255 M_ASN1_D2I_Init();
256 M_ASN1_D2I_start_sequence();
257 M_ASN1_D2I_get(alg,d2i_X509_ALGOR);
258 if (OBJ_obj2nid(alg->algorithm) != NID_rc4)
259 {
260 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
261 goto err;
262 }
263 M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
264 if (cb == NULL)
265 cb=EVP_read_pw_string;
266 i=cb(buf,256,"Enter Private Key password:",0);
267 if (i != 0)
268 {
269 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_BAD_PASSWORD_READ);
270 goto err;
271 }
272
273 EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
274 strlen((char *)buf),1,key,NULL);
275 memset(buf,0,256);
276
277 EVP_CIPHER_CTX_init(&ctx);
278 EVP_DecryptInit(&ctx,EVP_rc4(),key,NULL);
279 EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
280 EVP_DecryptFinal(&ctx,&(os->data[i]),&j);
281 EVP_CIPHER_CTX_cleanup(&ctx);
282 os->length=i+j;
283
284 zz=os->data;
285
286 if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
287 {
288 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
289 goto err;
290 }
291
292 zz=pkey->private_key->data;
293 if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL)
294 {
295 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
296 goto err;
297 }
298 if (!asn1_Finish(&c)) goto err;
299 *pp=c.p;
300err:
301 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
302 if (os != NULL) ASN1_BIT_STRING_free(os);
303 if (alg != NULL) X509_ALGOR_free(alg);
304 return(ret);
305 }
306
307static int i2d_NETSCAPE_PKEY(a,pp)
308NETSCAPE_PKEY *a;
309unsigned char **pp;
310 {
311 M_ASN1_I2D_vars(a);
312
313
314 M_ASN1_I2D_len(a->version, i2d_ASN1_INTEGER);
315 M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR);
316 M_ASN1_I2D_len(a->private_key, i2d_ASN1_OCTET_STRING);
317
318 M_ASN1_I2D_seq_total();
319
320 M_ASN1_I2D_put(a->version, i2d_ASN1_INTEGER);
321 M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR);
322 M_ASN1_I2D_put(a->private_key, i2d_ASN1_OCTET_STRING);
323
324 M_ASN1_I2D_finish();
325 }
326
327static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(a,pp,length)
328NETSCAPE_PKEY **a;
329unsigned char **pp;
330long length;
331 {
332 M_ASN1_D2I_vars(a,NETSCAPE_PKEY *,NETSCAPE_PKEY_new);
333
334 M_ASN1_D2I_Init();
335 M_ASN1_D2I_start_sequence();
336 M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER);
337 M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
338 M_ASN1_D2I_get(ret->private_key,d2i_ASN1_OCTET_STRING);
339 M_ASN1_D2I_Finish(a,NETSCAPE_PKEY_free,ASN1_F_D2I_NETSCAPE_PKEY);
340 }
341
342static NETSCAPE_PKEY *NETSCAPE_PKEY_new()
343 {
344 NETSCAPE_PKEY *ret=NULL;
345
346 M_ASN1_New_Malloc(ret,NETSCAPE_PKEY);
347 M_ASN1_New(ret->version,ASN1_INTEGER_new);
348 M_ASN1_New(ret->algor,X509_ALGOR_new);
349 M_ASN1_New(ret->private_key,ASN1_OCTET_STRING_new);
350 return(ret);
351 M_ASN1_New_Error(ASN1_F_NETSCAPE_PKEY_NEW);
352 }
353
354static void NETSCAPE_PKEY_free(a)
355NETSCAPE_PKEY *a;
356 {
357 if (a == NULL) return;
358 ASN1_INTEGER_free(a->version);
359 X509_ALGOR_free(a->algor);
360 ASN1_OCTET_STRING_free(a->private_key);
361 Free((char *)a);
362 }
363
364#endif /* NO_RC4 */
365
diff --git a/src/lib/libcrypto/asn1/t_pkey.c b/src/lib/libcrypto/asn1/t_pkey.c
new file mode 100644
index 0000000000..bc518d59a2
--- /dev/null
+++ b/src/lib/libcrypto/asn1/t_pkey.c
@@ -0,0 +1,392 @@
1/* crypto/asn1/t_pkey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "bn.h"
63#ifndef NO_RSA
64#include "rsa.h"
65#endif
66#ifndef NO_DH
67#include "dh.h"
68#endif
69#ifndef NO_DSA
70#include "dsa.h"
71#endif
72
73/* DHerr(DH_F_DHPARAMS_PRINT,ERR_R_MALLOC_FAILURE);
74 * DSAerr(DSA_F_DSAPARAMS_PRINT,ERR_R_MALLOC_FAILURE);
75 */
76
77#ifndef NOPROTO
78static int print(BIO *fp,char *str,BIGNUM *num,
79 unsigned char *buf,int off);
80#else
81static int print();
82#endif
83
84#ifndef NO_RSA
85#ifndef NO_FP_API
86int RSA_print_fp(fp,x,off)
87FILE *fp;
88RSA *x;
89int off;
90 {
91 BIO *b;
92 int ret;
93
94 if ((b=BIO_new(BIO_s_file())) == NULL)
95 {
96 RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
97 return(0);
98 }
99 BIO_set_fp(b,fp,BIO_NOCLOSE);
100 ret=RSA_print(b,x,off);
101 BIO_free(b);
102 return(ret);
103 }
104#endif
105
106int RSA_print(bp,x,off)
107BIO *bp;
108RSA *x;
109int off;
110 {
111 char str[128],*s;
112 unsigned char *m=NULL;
113 int i,ret=0;
114
115 i=RSA_size(x);
116 m=(unsigned char *)Malloc((unsigned int)i+10);
117 if (m == NULL)
118 {
119 RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
120 goto err;
121 }
122
123 if (off)
124 {
125 if (off > 128) off=128;
126 memset(str,' ',off);
127 }
128 if (x->d != NULL)
129 {
130 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
131 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
132 <= 0) goto err;
133 }
134
135 if (x->d == NULL)
136 sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n));
137 else
138 strcpy(str,"modulus:");
139 if (!print(bp,str,x->n,m,off)) goto err;
140 s=(x->d == NULL)?"Exponent:":"publicExponent:";
141 if (!print(bp,s,x->e,m,off)) goto err;
142 if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
143 if (!print(bp,"prime1:",x->p,m,off)) goto err;
144 if (!print(bp,"prime2:",x->q,m,off)) goto err;
145 if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
146 if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
147 if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
148 ret=1;
149err:
150 if (m != NULL) Free((char *)m);
151 return(ret);
152 }
153#endif /* NO_RSA */
154
155#ifndef NO_DSA
156#ifndef NO_FP_API
157int DSA_print_fp(fp,x,off)
158FILE *fp;
159DSA *x;
160int off;
161 {
162 BIO *b;
163 int ret;
164
165 if ((b=BIO_new(BIO_s_file())) == NULL)
166 {
167 DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
168 return(0);
169 }
170 BIO_set_fp(b,fp,BIO_NOCLOSE);
171 ret=DSA_print(b,x,off);
172 BIO_free(b);
173 return(ret);
174 }
175#endif
176
177int DSA_print(bp,x,off)
178BIO *bp;
179DSA *x;
180int off;
181 {
182 char str[128];
183 unsigned char *m=NULL;
184 int i,ret=0;
185 BIGNUM *bn=NULL;
186
187 if (x->p != NULL)
188 bn=x->p;
189 else if (x->priv_key != NULL)
190 bn=x->priv_key;
191 else if (x->pub_key != NULL)
192 bn=x->pub_key;
193
194 /* larger than needed but what the hell :-) */
195 if (bn != NULL)
196 i=BN_num_bytes(bn)*2;
197 else
198 i=256;
199 m=(unsigned char *)Malloc((unsigned int)i+10);
200 if (m == NULL)
201 {
202 DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
203 goto err;
204 }
205
206 if (off)
207 {
208 if (off > 128) off=128;
209 memset(str,' ',off);
210 }
211 if (x->priv_key != NULL)
212 {
213 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
214 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
215 <= 0) goto err;
216 }
217
218 if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
219 goto err;
220 if ((x->pub_key != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
221 goto err;
222 if ((x->p != NULL) && !print(bp,"P: ",x->p,m,off)) goto err;
223 if ((x->q != NULL) && !print(bp,"Q: ",x->q,m,off)) goto err;
224 if ((x->g != NULL) && !print(bp,"G: ",x->g,m,off)) goto err;
225 ret=1;
226err:
227 if (m != NULL) Free((char *)m);
228 return(ret);
229 }
230#endif /* !NO_DSA */
231
232static int print(bp,number,num,buf,off)
233BIO *bp;
234char *number;
235BIGNUM *num;
236unsigned char *buf;
237int off;
238 {
239 int n,i;
240 char str[128],*neg;
241
242 if (num == NULL) return(1);
243 neg=(num->neg)?"-":"";
244 if (off)
245 {
246 if (off > 128) off=128;
247 memset(str,' ',off);
248 if (BIO_write(bp,str,off) <= 0) return(0);
249 }
250
251 if (BN_num_bytes(num) <= BN_BYTES)
252 {
253 if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
254 (unsigned long)num->d[0],neg,(unsigned long)num->d[0])
255 <= 0) return(0);
256 }
257 else
258 {
259 buf[0]=0;
260 if (BIO_printf(bp,"%s%s",number,
261 (neg[0] == '-')?" (Negative)":"") <= 0)
262 return(0);
263 n=BN_bn2bin(num,&buf[1]);
264
265 if (buf[1] & 0x80)
266 n++;
267 else buf++;
268
269 for (i=0; i<n; i++)
270 {
271 if ((i%15) == 0)
272 {
273 str[0]='\n';
274 memset(&(str[1]),' ',off+4);
275 if (BIO_write(bp,str,off+1+4) <= 0) return(0);
276 }
277 if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
278 <= 0) return(0);
279 }
280 if (BIO_write(bp,"\n",1) <= 0) return(0);
281 }
282 return(1);
283 }
284
285#ifndef NO_DH
286#ifndef NO_FP_API
287int DHparams_print_fp(fp,x)
288FILE *fp;
289DH *x;
290 {
291 BIO *b;
292 int ret;
293
294 if ((b=BIO_new(BIO_s_file())) == NULL)
295 {
296 DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
297 return(0);
298 }
299 BIO_set_fp(b,fp,BIO_NOCLOSE);
300 ret=DHparams_print(b, x);
301 BIO_free(b);
302 return(ret);
303 }
304#endif
305
306int DHparams_print(bp,x)
307BIO *bp;
308DH *x;
309 {
310 unsigned char *m=NULL;
311 int reason=ERR_R_BUF_LIB,i,ret=0;
312
313 i=BN_num_bytes(x->p);
314 m=(unsigned char *)Malloc((unsigned int)i+10);
315 if (m == NULL)
316 {
317 reason=ERR_R_MALLOC_FAILURE;
318 goto err;
319 }
320
321 if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
322 BN_num_bits(x->p)) <= 0)
323 goto err;
324 if (!print(bp,"prime:",x->p,m,4)) goto err;
325 if (!print(bp,"generator:",x->g,m,4)) goto err;
326 if (x->length != 0)
327 {
328 if (BIO_printf(bp," recomented-private-length: %d bits\n",
329 (int)x->length) <= 0) goto err;
330 }
331 ret=1;
332 if (0)
333 {
334err:
335 DHerr(DH_F_DHPARAMS_PRINT,reason);
336 }
337 if (m != NULL) Free((char *)m);
338 return(ret);
339 }
340#endif
341
342#ifndef NO_DSA
343#ifndef NO_FP_API
344int DSAparams_print_fp(fp,x)
345FILE *fp;
346DSA *x;
347 {
348 BIO *b;
349 int ret;
350
351 if ((b=BIO_new(BIO_s_file())) == NULL)
352 {
353 DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
354 return(0);
355 }
356 BIO_set_fp(b,fp,BIO_NOCLOSE);
357 ret=DSAparams_print(b, x);
358 BIO_free(b);
359 return(ret);
360 }
361#endif
362
363int DSAparams_print(bp,x)
364BIO *bp;
365DSA *x;
366 {
367 unsigned char *m=NULL;
368 int reason=ERR_R_BUF_LIB,i,ret=0;
369
370 i=BN_num_bytes(x->p);
371 m=(unsigned char *)Malloc((unsigned int)i+10);
372 if (m == NULL)
373 {
374 reason=ERR_R_MALLOC_FAILURE;
375 goto err;
376 }
377
378 if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
379 BN_num_bits(x->p)) <= 0)
380 goto err;
381 if (!print(bp,"p:",x->p,m,4)) goto err;
382 if (!print(bp,"q:",x->q,m,4)) goto err;
383 if (!print(bp,"g:",x->g,m,4)) goto err;
384 ret=1;
385err:
386 if (m != NULL) Free((char *)m);
387 DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
388 return(ret);
389 }
390
391#endif /* !NO_DSA */
392
diff --git a/src/lib/libcrypto/asn1/t_req.c b/src/lib/libcrypto/asn1/t_req.c
new file mode 100644
index 0000000000..7df749a48f
--- /dev/null
+++ b/src/lib/libcrypto/asn1/t_req.c
@@ -0,0 +1,226 @@
1/* crypto/asn1/t_req.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "bn.h"
63#include "objects.h"
64#include "x509.h"
65
66#ifndef NO_FP_API
67int X509_REQ_print_fp(fp,x)
68FILE *fp;
69X509_REQ *x;
70 {
71 BIO *b;
72 int ret;
73
74 if ((b=BIO_new(BIO_s_file())) == NULL)
75 {
76 X509err(X509_F_X509_REQ_PRINT_FP,ERR_R_BUF_LIB);
77 return(0);
78 }
79 BIO_set_fp(b,fp,BIO_NOCLOSE);
80 ret=X509_REQ_print(b, x);
81 BIO_free(b);
82 return(ret);
83 }
84#endif
85
86int X509_REQ_print(bp,x)
87BIO *bp;
88X509_REQ *x;
89 {
90 unsigned long l;
91 int i,n;
92 char *s,*neg;
93 X509_REQ_INFO *ri;
94 EVP_PKEY *pkey;
95 STACK *sk;
96 char str[128];
97
98 ri=x->req_info;
99 sprintf(str,"Certificate Request:\n");
100 if (BIO_puts(bp,str) <= 0) goto err;
101 sprintf(str,"%4sData:\n","");
102 if (BIO_puts(bp,str) <= 0) goto err;
103
104 neg=(ri->version->type == V_ASN1_NEG_INTEGER)?"-":"";
105 l=0;
106 for (i=0; i<ri->version->length; i++)
107 { l<<=8; l+=ri->version->data[i]; }
108 sprintf(str,"%8sVersion: %s%lu (%s0x%lx)\n","",neg,l,neg,l);
109 if (BIO_puts(bp,str) <= 0) goto err;
110 sprintf(str,"%8sSubject: ","");
111 if (BIO_puts(bp,str) <= 0) goto err;
112
113 X509_NAME_print(bp,ri->subject,16);
114 sprintf(str,"\n%8sSubject Public Key Info:\n","");
115 if (BIO_puts(bp,str) <= 0) goto err;
116 i=OBJ_obj2nid(ri->pubkey->algor->algorithm);
117 sprintf(str,"%12sPublic Key Algorithm: %s\n","",
118 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i));
119 if (BIO_puts(bp,str) <= 0) goto err;
120
121 pkey=X509_REQ_get_pubkey(x);
122#ifndef NO_RSA
123 if (pkey->type == EVP_PKEY_RSA)
124 {
125 BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
126 BN_num_bits(pkey->pkey.rsa->n));
127 RSA_print(bp,pkey->pkey.rsa,16);
128 }
129 else
130#endif
131#ifndef NO_DSA
132 if (pkey->type == EVP_PKEY_DSA)
133 {
134 BIO_printf(bp,"%12sDSA Public Key:\n","");
135 DSA_print(bp,pkey->pkey.dsa,16);
136 }
137 else
138#endif
139 BIO_printf(bp,"%12sUnknown Public Key:\n","");
140
141 /* may not be */
142 sprintf(str,"%8sAttributes:\n","");
143 if (BIO_puts(bp,str) <= 0) goto err;
144
145 sk=x->req_info->attributes;
146 if ((sk == NULL) || (sk_num(sk) == 0))
147 {
148 if (!x->req_info->req_kludge)
149 {
150 sprintf(str,"%12sa0:00\n","");
151 if (BIO_puts(bp,str) <= 0) goto err;
152 }
153 }
154 else
155 {
156 for (i=0; i<sk_num(sk); i++)
157 {
158 ASN1_TYPE *at;
159 X509_ATTRIBUTE *a;
160 ASN1_BIT_STRING *bs=NULL;
161 ASN1_TYPE *t;
162 int j,type=0,count=1,ii=0;
163
164 a=(X509_ATTRIBUTE *)sk_value(sk,i);
165 sprintf(str,"%12s","");
166 if (BIO_puts(bp,str) <= 0) goto err;
167 if ((j=i2a_ASN1_OBJECT(bp,a->object)) > 0)
168
169 if (a->set)
170 {
171 ii=0;
172 count=sk_num(a->value.set);
173get_next:
174 at=(ASN1_TYPE *)sk_value(a->value.set,ii);
175 type=at->type;
176 bs=at->value.asn1_string;
177 }
178 else
179 {
180 t=a->value.single;
181 type=t->type;
182 bs=t->value.bit_string;
183 }
184 for (j=25-j; j>0; j--)
185 if (BIO_write(bp," ",1) != 1) goto err;
186 if (BIO_puts(bp,":") <= 0) goto err;
187 if ( (type == V_ASN1_PRINTABLESTRING) ||
188 (type == V_ASN1_T61STRING) ||
189 (type == V_ASN1_IA5STRING))
190 {
191 if (BIO_write(bp,(char *)bs->data,bs->length)
192 != bs->length)
193 goto err;
194 BIO_puts(bp,"\n");
195 }
196 else
197 {
198 BIO_puts(bp,"unable to print attribute\n");
199 }
200 if (++ii < count) goto get_next;
201 }
202 }
203
204 i=OBJ_obj2nid(x->sig_alg->algorithm);
205 sprintf(str,"%4sSignature Algorithm: %s","",
206 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i));
207 if (BIO_puts(bp,str) <= 0) goto err;
208
209 n=x->signature->length;
210 s=(char *)x->signature->data;
211 for (i=0; i<n; i++)
212 {
213 if ((i%18) == 0)
214 {
215 sprintf(str,"\n%8s","");
216 if (BIO_puts(bp,str) <= 0) goto err;
217 }
218 sprintf(str,"%02x%s",(unsigned char)s[i],((i+1) == n)?"":":");
219 if (BIO_puts(bp,str) <= 0) goto err;
220 }
221 if (BIO_puts(bp,"\n") <= 0) goto err;
222 return(1);
223err:
224 X509err(X509_F_X509_REQ_PRINT,ERR_R_BUF_LIB);
225 return(0);
226 }
diff --git a/src/lib/libcrypto/asn1/t_x509.c b/src/lib/libcrypto/asn1/t_x509.c
new file mode 100644
index 0000000000..b10fbbb992
--- /dev/null
+++ b/src/lib/libcrypto/asn1/t_x509.c
@@ -0,0 +1,386 @@
1/* crypto/asn1/t_x509.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "bn.h"
63#ifndef NO_RSA
64#include "rsa.h"
65#endif
66#ifndef NO_DSA
67#include "dsa.h"
68#endif
69#include "objects.h"
70#include "x509.h"
71
72#ifndef NO_FP_API
73int X509_print_fp(fp,x)
74FILE *fp;
75X509 *x;
76 {
77 BIO *b;
78 int ret;
79
80 if ((b=BIO_new(BIO_s_file())) == NULL)
81 {
82 X509err(X509_F_X509_PRINT_FP,ERR_R_BUF_LIB);
83 return(0);
84 }
85 BIO_set_fp(b,fp,BIO_NOCLOSE);
86 ret=X509_print(b, x);
87 BIO_free(b);
88 return(ret);
89 }
90#endif
91
92int X509_print(bp,x)
93BIO *bp;
94X509 *x;
95 {
96 long l;
97 int ret=0,i,j,n;
98 char *m=NULL,*s;
99 X509_CINF *ci;
100 ASN1_INTEGER *bs;
101 EVP_PKEY *pkey=NULL;
102 char *neg;
103 X509_EXTENSION *ex;
104 ASN1_STRING *str=NULL;
105
106 ci=x->cert_info;
107 if (BIO_write(bp,"Certificate:\n",13) <= 0) goto err;
108 if (BIO_write(bp," Data:\n",10) <= 0) goto err;
109 l=X509_get_version(x);
110 if (BIO_printf(bp,"%8sVersion: %lu (0x%lx)\n","",l+1,l) <= 0) goto err;
111 if (BIO_write(bp," Serial Number:",22) <= 0) goto err;
112
113 bs=X509_get_serialNumber(x);
114 if (bs->length <= 4)
115 {
116 l=ASN1_INTEGER_get(bs);
117 if (l < 0)
118 {
119 l= -l;
120 neg="-";
121 }
122 else
123 neg="";
124 if (BIO_printf(bp," %s%lu (%s0x%lx)\n",neg,l,neg,l) <= 0)
125 goto err;
126 }
127 else
128 {
129 neg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":"";
130 if (BIO_printf(bp,"\n%12s%s","",neg) <= 0) goto err;
131
132 for (i=0; i<bs->length; i++)
133 {
134 if (BIO_printf(bp,"%02x%c",bs->data[i],
135 ((i+1 == bs->length)?'\n':':')) <= 0)
136 goto err;
137 }
138 }
139
140 i=OBJ_obj2nid(ci->signature->algorithm);
141 if (BIO_printf(bp,"%8sSignature Algorithm: %s\n","",
142 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0)
143 goto err;
144
145 if (BIO_write(bp," Issuer: ",16) <= 0) goto err;
146 if (!X509_NAME_print(bp,X509_get_issuer_name(x),16)) goto err;
147 if (BIO_write(bp,"\n Validity\n",18) <= 0) goto err;
148 if (BIO_write(bp," Not Before: ",24) <= 0) goto err;
149 if (!ASN1_UTCTIME_print(bp,X509_get_notBefore(x))) goto err;
150 if (BIO_write(bp,"\n Not After : ",25) <= 0) goto err;
151 if (!ASN1_UTCTIME_print(bp,X509_get_notAfter(x))) goto err;
152 if (BIO_write(bp,"\n Subject: ",18) <= 0) goto err;
153 if (!X509_NAME_print(bp,X509_get_subject_name(x),16)) goto err;
154 if (BIO_write(bp,"\n Subject Public Key Info:\n",34) <= 0)
155 goto err;
156 i=OBJ_obj2nid(ci->key->algor->algorithm);
157 if (BIO_printf(bp,"%12sPublic Key Algorithm: %s\n","",
158 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err;
159
160 pkey=X509_get_pubkey(x);
161#ifndef NO_RSA
162 if (pkey->type == EVP_PKEY_RSA)
163 {
164 BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
165 BN_num_bits(pkey->pkey.rsa->n));
166 RSA_print(bp,pkey->pkey.rsa,16);
167 }
168 else
169#endif
170#ifndef NO_DSA
171 if (pkey->type == EVP_PKEY_DSA)
172 {
173 BIO_printf(bp,"%12sDSA Public Key:\n","");
174 DSA_print(bp,pkey->pkey.dsa,16);
175 }
176 else
177#endif
178 BIO_printf(bp,"%12sDSA Public Key:\n","");
179
180 n=X509_get_ext_count(x);
181 if (n > 0)
182 {
183 BIO_printf(bp,"%8sX509v3 extensions:\n","");
184 for (i=0; i<n; i++)
185 {
186 int data_type,pack_type;
187 ASN1_OBJECT *obj;
188
189 ex=X509_get_ext(x,i);
190 if (BIO_printf(bp,"%12s","") <= 0) goto err;
191 obj=X509_EXTENSION_get_object(ex);
192 i2a_ASN1_OBJECT(bp,obj);
193 j=X509_EXTENSION_get_critical(ex);
194 if (BIO_printf(bp,": %s\n%16s",j?"critical":"","") <= 0)
195 goto err;
196
197 pack_type=X509v3_pack_type_by_OBJ(obj);
198 data_type=X509v3_data_type_by_OBJ(obj);
199
200 if (pack_type == X509_EXT_PACK_STRING)
201 {
202 if (X509v3_unpack_string(
203 &str,data_type,
204 X509_EXTENSION_get_data(ex)) == NULL)
205 {
206 /* hmm... */
207 goto err;
208 }
209 if ( (data_type == V_ASN1_IA5STRING) ||
210 (data_type == V_ASN1_PRINTABLESTRING) ||
211 (data_type == V_ASN1_T61STRING))
212 {
213 if (BIO_write(bp,(char *)str->data,
214 str->length) <= 0)
215 goto err;
216 }
217 else if (data_type == V_ASN1_BIT_STRING)
218 {
219 BIO_printf(bp,"0x");
220 for (j=0; j<str->length; j++)
221 {
222 BIO_printf(bp,"%02X",
223 str->data[j]);
224 }
225 }
226 }
227 else
228 {
229 ASN1_OCTET_STRING_print(bp,ex->value);
230 }
231 if (BIO_write(bp,"\n",1) <= 0) goto err;
232 }
233 }
234
235 i=OBJ_obj2nid(x->sig_alg->algorithm);
236 if (BIO_printf(bp,"%4sSignature Algorithm: %s","",
237 (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err;
238
239 n=x->signature->length;
240 s=(char *)x->signature->data;
241 for (i=0; i<n; i++)
242 {
243 if ((i%18) == 0)
244 if (BIO_write(bp,"\n ",9) <= 0) goto err;
245 if (BIO_printf(bp,"%02x%s",(unsigned char)s[i],
246 ((i+1) == n)?"":":") <= 0) goto err;
247 }
248 if (BIO_write(bp,"\n",1) != 1) goto err;
249 ret=1;
250err:
251 if (str != NULL) ASN1_STRING_free(str);
252 if (m != NULL) Free((char *)m);
253 return(ret);
254 }
255
256int ASN1_STRING_print(bp,v)
257BIO *bp;
258ASN1_STRING *v;
259 {
260 int i,n;
261 char buf[80],*p;;
262
263 if (v == NULL) return(0);
264 n=0;
265 p=(char *)v->data;
266 for (i=0; i<v->length; i++)
267 {
268 if ((p[i] > '~') || ((p[i] < ' ') &&
269 (p[i] != '\n') && (p[i] != '\r')))
270 buf[n]='.';
271 else
272 buf[n]=p[i];
273 n++;
274 if (n >= 80)
275 {
276 if (BIO_write(bp,buf,n) <= 0)
277 return(0);
278 n=0;
279 }
280 }
281 if (n > 0)
282 if (BIO_write(bp,buf,n) <= 0)
283 return(0);
284 return(1);
285 }
286
287int ASN1_UTCTIME_print(bp,tm)
288BIO *bp;
289ASN1_UTCTIME *tm;
290 {
291 char *v;
292 int gmt=0;
293 static char *mon[12]={
294 "Jan","Feb","Mar","Apr","May","Jun",
295 "Jul","Aug","Sep","Oct","Nov","Dec"};
296 int i;
297 int y=0,M=0,d=0,h=0,m=0,s=0;
298
299 i=tm->length;
300 v=(char *)tm->data;
301
302 if (i < 10) goto err;
303 if (v[i-1] == 'Z') gmt=1;
304 for (i=0; i<10; i++)
305 if ((v[i] > '9') || (v[i] < '0')) goto err;
306 y= (v[0]-'0')*10+(v[1]-'0');
307 if (y < 50) y+=100;
308 M= (v[2]-'0')*10+(v[3]-'0');
309 if ((M > 12) || (M < 1)) goto err;
310 d= (v[4]-'0')*10+(v[5]-'0');
311 h= (v[6]-'0')*10+(v[7]-'0');
312 m= (v[8]-'0')*10+(v[9]-'0');
313 if ( (v[10] >= '0') && (v[10] <= '9') &&
314 (v[11] >= '0') && (v[11] <= '9'))
315 s= (v[10]-'0')*10+(v[11]-'0');
316
317 if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
318 mon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0)
319 return(0);
320 else
321 return(1);
322err:
323 BIO_write(bp,"Bad time value",14);
324 return(0);
325 }
326
327int X509_NAME_print(bp,name,obase)
328BIO *bp;
329X509_NAME *name;
330int obase;
331 {
332 char *s,*c;
333 int ret=0,l,ll,i,first=1;
334 char buf[256];
335
336 ll=80-2-obase;
337
338 s=X509_NAME_oneline(name,buf,256);
339 s++; /* skip the first slash */
340
341 l=ll;
342 c=s;
343 for (;;)
344 {
345 if ( ((*s == '/') &&
346 ((s[1] >= 'A') && (s[1] <= 'Z') && (
347 (s[2] == '=') ||
348 ((s[2] >= 'A') && (s[2] <= 'Z') &&
349 (s[3] == '='))
350 ))) ||
351 (*s == '\0'))
352 {
353 if ((l <= 0) && !first)
354 {
355 first=0;
356 if (BIO_write(bp,"\n",1) != 1) goto err;
357 for (i=0; i<obase; i++)
358 {
359 if (BIO_write(bp," ",1) != 1) goto err;
360 }
361 l=ll;
362 }
363 i=s-c;
364 if (BIO_write(bp,c,i) != i) goto err;
365 c+=i;
366 c++;
367 if (*s != '\0')
368 {
369 if (BIO_write(bp,", ",2) != 2) goto err;
370 }
371 l--;
372 }
373 if (*s == '\0') break;
374 s++;
375 l--;
376 }
377
378 ret=1;
379 if (0)
380 {
381err:
382 X509err(X509_F_X509_NAME_PRINT,ERR_R_BUF_LIB);
383 }
384 return(ret);
385 }
386
diff --git a/src/lib/libcrypto/asn1/x_algor.c b/src/lib/libcrypto/asn1/x_algor.c
new file mode 100644
index 0000000000..0ed2c87b64
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_algor.c
@@ -0,0 +1,126 @@
1/* crypto/asn1/x_algor.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/*
64 * ASN1err(ASN1_F_D2I_X509_ALGOR,ASN1_R_LENGTH_MISMATCH);
65 * ASN1err(ASN1_F_X509_ALGOR_NEW,ASN1_R_EXPECTING_A_SEQUENCE);
66 * ASN1err(ASN1_F_D2I_X509_ALGOR,ASN1_R_LENGTH_MISMATCH);
67 */
68
69int i2d_X509_ALGOR(a,pp)
70X509_ALGOR *a;
71unsigned char **pp;
72 {
73 M_ASN1_I2D_vars(a);
74
75 M_ASN1_I2D_len(a->algorithm,i2d_ASN1_OBJECT);
76 if (a->parameter != NULL)
77 { M_ASN1_I2D_len(a->parameter,i2d_ASN1_TYPE); }
78
79 M_ASN1_I2D_seq_total();
80 M_ASN1_I2D_put(a->algorithm,i2d_ASN1_OBJECT);
81 if (a->parameter != NULL)
82 { M_ASN1_I2D_put(a->parameter,i2d_ASN1_TYPE); }
83
84 M_ASN1_I2D_finish();
85 }
86
87X509_ALGOR *d2i_X509_ALGOR(a,pp,length)
88X509_ALGOR **a;
89unsigned char **pp;
90long length;
91 {
92 M_ASN1_D2I_vars(a,X509_ALGOR *,X509_ALGOR_new);
93
94 M_ASN1_D2I_Init();
95 M_ASN1_D2I_start_sequence();
96 M_ASN1_D2I_get(ret->algorithm,d2i_ASN1_OBJECT);
97 if (!M_ASN1_D2I_end_sequence())
98 { M_ASN1_D2I_get(ret->parameter,d2i_ASN1_TYPE); }
99 else
100 {
101 ASN1_TYPE_free(ret->parameter);
102 ret->parameter=NULL;
103 }
104 M_ASN1_D2I_Finish(a,X509_ALGOR_free,ASN1_F_D2I_X509_ALGOR);
105 }
106
107X509_ALGOR *X509_ALGOR_new()
108 {
109 X509_ALGOR *ret=NULL;
110
111 M_ASN1_New_Malloc(ret,X509_ALGOR);
112 M_ASN1_New(ret->algorithm,ASN1_OBJECT_new);
113 ret->parameter=NULL;
114 return(ret);
115 M_ASN1_New_Error(ASN1_F_X509_ALGOR_NEW);
116 }
117
118void X509_ALGOR_free(a)
119X509_ALGOR *a;
120 {
121 if (a == NULL) return;
122 ASN1_OBJECT_free(a->algorithm);
123 ASN1_TYPE_free(a->parameter);
124 Free((char *)a);
125 }
126
diff --git a/src/lib/libcrypto/asn1/x_attrib.c b/src/lib/libcrypto/asn1/x_attrib.c
new file mode 100644
index 0000000000..e52ced8627
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_attrib.c
@@ -0,0 +1,152 @@
1/* crypto/asn1/x_attrib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "objects.h"
62#include "asn1_mac.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_ATTRIBUTE,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_X509_ATTRIBUTE_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
67 * ASN1err(ASN1_F_I2D_X509_ATTRIBUTE,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
68 */
69
70/* sequence */
71int i2d_X509_ATTRIBUTE(a,pp)
72X509_ATTRIBUTE *a;
73unsigned char **pp;
74 {
75 int k=0;
76 int r=0,ret=0;
77 unsigned char **p=NULL;
78
79 if (a == NULL) return(0);
80
81 p=NULL;
82 for (;;)
83 {
84 if (k)
85 {
86 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE);
87 if (pp == NULL) return(r);
88 p=pp;
89 ASN1_put_object(p,1,ret,V_ASN1_SEQUENCE,
90 V_ASN1_UNIVERSAL);
91 }
92
93 ret+=i2d_ASN1_OBJECT(a->object,p);
94 if (a->set)
95 ret+=i2d_ASN1_SET(a->value.set,p,i2d_ASN1_TYPE,
96 V_ASN1_SET,V_ASN1_UNIVERSAL);
97 else
98 ret+=i2d_ASN1_TYPE(a->value.single,p);
99 if (k++) return(r);
100 }
101 }
102
103X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(a,pp,length)
104X509_ATTRIBUTE **a;
105unsigned char **pp;
106long length;
107 {
108 M_ASN1_D2I_vars(a,X509_ATTRIBUTE *,X509_ATTRIBUTE_new);
109
110 M_ASN1_D2I_Init();
111 M_ASN1_D2I_start_sequence();
112 M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT);
113
114 if ((c.slen != 0) &&
115 (M_ASN1_next == (V_ASN1_CONSTRUCTED|V_ASN1_UNIVERSAL|V_ASN1_SET)))
116 {
117 ret->set=1;
118 M_ASN1_D2I_get_set(ret->value.set,d2i_ASN1_TYPE);
119 }
120 else
121 {
122 ret->set=0;
123 M_ASN1_D2I_get(ret->value.single,d2i_ASN1_TYPE);
124 }
125
126 M_ASN1_D2I_Finish(a,X509_ATTRIBUTE_free,ASN1_F_D2I_X509_ATTRIBUTE);
127 }
128
129X509_ATTRIBUTE *X509_ATTRIBUTE_new()
130 {
131 X509_ATTRIBUTE *ret=NULL;
132
133 M_ASN1_New_Malloc(ret,X509_ATTRIBUTE);
134 M_ASN1_New(ret->object,ASN1_OBJECT_new);
135 ret->set=0;
136 ret->value.ptr=NULL;
137 return(ret);
138 M_ASN1_New_Error(ASN1_F_X509_ATTRIBUTE_NEW);
139 }
140
141void X509_ATTRIBUTE_free(a)
142X509_ATTRIBUTE *a;
143 {
144 if (a == NULL) return;
145 ASN1_OBJECT_free(a->object);
146 if (a->set)
147 sk_pop_free(a->value.set,ASN1_TYPE_free);
148 else
149 ASN1_TYPE_free(a->value.single);
150 Free((char *)a);
151 }
152
diff --git a/src/lib/libcrypto/asn1/x_crl.c b/src/lib/libcrypto/asn1/x_crl.c
new file mode 100644
index 0000000000..13acdab427
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_crl.c
@@ -0,0 +1,353 @@
1/* crypto/asn1/x_crl.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62#include "x509.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_CRL,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_D2I_X509_CRL_INFO,ASN1_R_EXPECTING_A_SEQUENCE);
67 * ASN1err(ASN1_F_D2I_X509_REVOKED,ASN1_R_LENGTH_MISMATCH);
68 * ASN1err(ASN1_F_X509_CRL_NEW,ASN1_R_LENGTH_MISMATCH);
69 * ASN1err(ASN1_F_X509_CRL_INFO_NEW,ASN1_R_EXPECTING_A_SEQUENCE);
70 * ASN1err(ASN1_F_X509_REVOKED_NEW,ASN1_R_LENGTH_MISMATCH);
71 */
72
73#ifndef NOPROTO
74static int X509_REVOKED_cmp(X509_REVOKED **a,X509_REVOKED **b);
75static int X509_REVOKED_seq_cmp(X509_REVOKED **a,X509_REVOKED **b);
76#else
77static int X509_REVOKED_cmp();
78static int X509_REVOKED_seq_cmp();
79#endif
80
81int i2d_X509_REVOKED(a,pp)
82X509_REVOKED *a;
83unsigned char **pp;
84 {
85 M_ASN1_I2D_vars(a);
86
87 M_ASN1_I2D_len(a->serialNumber,i2d_ASN1_INTEGER);
88 M_ASN1_I2D_len(a->revocationDate,i2d_ASN1_UTCTIME);
89 M_ASN1_I2D_len_SEQ_opt(a->extensions,i2d_X509_EXTENSION);
90
91 M_ASN1_I2D_seq_total();
92
93 M_ASN1_I2D_put(a->serialNumber,i2d_ASN1_INTEGER);
94 M_ASN1_I2D_put(a->revocationDate,i2d_ASN1_UTCTIME);
95 M_ASN1_I2D_put_SEQ_opt(a->extensions,i2d_X509_EXTENSION);
96
97 M_ASN1_I2D_finish();
98 }
99
100X509_REVOKED *d2i_X509_REVOKED(a,pp,length)
101X509_REVOKED **a;
102unsigned char **pp;
103long length;
104 {
105 M_ASN1_D2I_vars(a,X509_REVOKED *,X509_REVOKED_new);
106
107 M_ASN1_D2I_Init();
108 M_ASN1_D2I_start_sequence();
109 M_ASN1_D2I_get(ret->serialNumber,d2i_ASN1_INTEGER);
110 M_ASN1_D2I_get(ret->revocationDate,d2i_ASN1_UTCTIME);
111 M_ASN1_D2I_get_seq_opt(ret->extensions,d2i_X509_EXTENSION);
112 M_ASN1_D2I_Finish(a,X509_REVOKED_free,ASN1_F_D2I_X509_REVOKED);
113 }
114
115int i2d_X509_CRL_INFO(a,pp)
116X509_CRL_INFO *a;
117unsigned char **pp;
118 {
119 int v1=0;
120 long l=0;
121 M_ASN1_I2D_vars(a);
122
123 if (sk_num(a->revoked) != 0)
124 qsort((char *)a->revoked->data,sk_num(a->revoked),
125 sizeof(X509_REVOKED *),(int (*)(P_CC_CC))X509_REVOKED_seq_cmp);
126 if ((a->version != NULL) && ((l=ASN1_INTEGER_get(a->version)) != 0))
127 {
128 M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER);
129 }
130 M_ASN1_I2D_len(a->sig_alg,i2d_X509_ALGOR);
131 M_ASN1_I2D_len(a->issuer,i2d_X509_NAME);
132 M_ASN1_I2D_len(a->lastUpdate,i2d_ASN1_UTCTIME);
133 if (a->nextUpdate != NULL)
134 { M_ASN1_I2D_len(a->nextUpdate,i2d_ASN1_UTCTIME); }
135 M_ASN1_I2D_len_SEQ_opt(a->revoked,i2d_X509_REVOKED);
136 M_ASN1_I2D_len_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,0,
137 V_ASN1_SEQUENCE,v1);
138
139 M_ASN1_I2D_seq_total();
140
141 if ((a->version != NULL) && (l != 0))
142 {
143 M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER);
144 }
145 M_ASN1_I2D_put(a->sig_alg,i2d_X509_ALGOR);
146 M_ASN1_I2D_put(a->issuer,i2d_X509_NAME);
147 M_ASN1_I2D_put(a->lastUpdate,i2d_ASN1_UTCTIME);
148 if (a->nextUpdate != NULL)
149 { M_ASN1_I2D_put(a->nextUpdate,i2d_ASN1_UTCTIME); }
150 M_ASN1_I2D_put_SEQ_opt(a->revoked,i2d_X509_REVOKED);
151 M_ASN1_I2D_put_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,0,
152 V_ASN1_SEQUENCE,v1);
153
154 M_ASN1_I2D_finish();
155 }
156
157X509_CRL_INFO *d2i_X509_CRL_INFO(a,pp,length)
158X509_CRL_INFO **a;
159unsigned char **pp;
160long length;
161 {
162 int i,ver=0;
163 M_ASN1_D2I_vars(a,X509_CRL_INFO *,X509_CRL_INFO_new);
164
165
166 M_ASN1_D2I_Init();
167 M_ASN1_D2I_start_sequence();
168 M_ASN1_D2I_get_opt(ret->version,d2i_ASN1_INTEGER,V_ASN1_INTEGER);
169 if (ret->version != NULL)
170 ver=ret->version->data[0];
171
172 if ((ver == 0) && (ret->version != NULL))
173 {
174 ASN1_INTEGER_free(ret->version);
175 ret->version=NULL;
176 }
177 M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);
178 M_ASN1_D2I_get(ret->issuer,d2i_X509_NAME);
179 M_ASN1_D2I_get(ret->lastUpdate,d2i_ASN1_UTCTIME);
180 M_ASN1_D2I_get_opt(ret->nextUpdate,d2i_ASN1_UTCTIME,V_ASN1_UTCTIME);
181 if (ret->revoked != NULL)
182 {
183 while (sk_num(ret->revoked))
184 X509_REVOKED_free((X509_REVOKED *)sk_pop(ret->revoked));
185 }
186 M_ASN1_D2I_get_seq_opt(ret->revoked,d2i_X509_REVOKED);
187
188 if (ret->revoked != NULL)
189 {
190 for (i=0; i<sk_num(ret->revoked); i++)
191 {
192 ((X509_REVOKED *)sk_value(ret->revoked,i))->sequence=i;
193 }
194 }
195
196 if (ver >= 1)
197 {
198 if (ret->extensions != NULL)
199 {
200 while (sk_num(ret->extensions))
201 X509_EXTENSION_free((X509_EXTENSION *)
202 sk_pop(ret->extensions));
203 }
204
205 M_ASN1_D2I_get_EXP_set_opt(ret->extensions,d2i_X509_EXTENSION,
206 0,V_ASN1_SEQUENCE);
207 }
208
209 M_ASN1_D2I_Finish(a,X509_CRL_INFO_free,ASN1_F_D2I_X509_CRL_INFO);
210 }
211
212int i2d_X509_CRL(a,pp)
213X509_CRL *a;
214unsigned char **pp;
215 {
216 M_ASN1_I2D_vars(a);
217
218 M_ASN1_I2D_len(a->crl,i2d_X509_CRL_INFO);
219 M_ASN1_I2D_len(a->sig_alg,i2d_X509_ALGOR);
220 M_ASN1_I2D_len(a->signature,i2d_ASN1_BIT_STRING);
221
222 M_ASN1_I2D_seq_total();
223
224 M_ASN1_I2D_put(a->crl,i2d_X509_CRL_INFO);
225 M_ASN1_I2D_put(a->sig_alg,i2d_X509_ALGOR);
226 M_ASN1_I2D_put(a->signature,i2d_ASN1_BIT_STRING);
227
228 M_ASN1_I2D_finish();
229 }
230
231X509_CRL *d2i_X509_CRL(a,pp,length)
232X509_CRL **a;
233unsigned char **pp;
234long length;
235 {
236 M_ASN1_D2I_vars(a,X509_CRL *,X509_CRL_new);
237
238 M_ASN1_D2I_Init();
239 M_ASN1_D2I_start_sequence();
240 M_ASN1_D2I_get(ret->crl,d2i_X509_CRL_INFO);
241 M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);
242 M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);
243
244 M_ASN1_D2I_Finish(a,X509_CRL_free,ASN1_F_D2I_X509_CRL);
245 }
246
247
248X509_REVOKED *X509_REVOKED_new()
249 {
250 X509_REVOKED *ret=NULL;
251
252 M_ASN1_New_Malloc(ret,X509_REVOKED);
253 M_ASN1_New(ret->serialNumber,ASN1_INTEGER_new);
254 M_ASN1_New(ret->revocationDate,ASN1_UTCTIME_new);
255 ret->extensions=NULL;
256 return(ret);
257 M_ASN1_New_Error(ASN1_F_X509_REVOKED_NEW);
258 }
259
260X509_CRL_INFO *X509_CRL_INFO_new()
261 {
262 X509_CRL_INFO *ret=NULL;
263
264 M_ASN1_New_Malloc(ret,X509_CRL_INFO);
265 ret->version=NULL;
266 M_ASN1_New(ret->sig_alg,X509_ALGOR_new);
267 M_ASN1_New(ret->issuer,X509_NAME_new);
268 M_ASN1_New(ret->lastUpdate,ASN1_UTCTIME_new);
269 ret->nextUpdate=NULL;
270 M_ASN1_New(ret->revoked,sk_new_null);
271 M_ASN1_New(ret->extensions,sk_new_null);
272 ret->revoked->comp=(int (*)())X509_REVOKED_cmp;
273 return(ret);
274 M_ASN1_New_Error(ASN1_F_X509_CRL_INFO_NEW);
275 }
276
277X509_CRL *X509_CRL_new()
278 {
279 X509_CRL *ret=NULL;
280
281 M_ASN1_New_Malloc(ret,X509_CRL);
282 ret->references=1;
283 M_ASN1_New(ret->crl,X509_CRL_INFO_new);
284 M_ASN1_New(ret->sig_alg,X509_ALGOR_new);
285 M_ASN1_New(ret->signature,ASN1_BIT_STRING_new);
286 return(ret);
287 M_ASN1_New_Error(ASN1_F_X509_CRL_NEW);
288 }
289
290void X509_REVOKED_free(a)
291X509_REVOKED *a;
292 {
293 if (a == NULL) return;
294 ASN1_INTEGER_free(a->serialNumber);
295 ASN1_UTCTIME_free(a->revocationDate);
296 sk_pop_free(a->extensions,X509_EXTENSION_free);
297 Free((char *)a);
298 }
299
300void X509_CRL_INFO_free(a)
301X509_CRL_INFO *a;
302 {
303 if (a == NULL) return;
304 ASN1_INTEGER_free(a->version);
305 X509_ALGOR_free(a->sig_alg);
306 X509_NAME_free(a->issuer);
307 ASN1_UTCTIME_free(a->lastUpdate);
308 if (a->nextUpdate)
309 ASN1_UTCTIME_free(a->nextUpdate);
310 sk_pop_free(a->revoked,X509_REVOKED_free);
311 sk_pop_free(a->extensions,X509_EXTENSION_free);
312 Free((char *)a);
313 }
314
315void X509_CRL_free(a)
316X509_CRL *a;
317 {
318 int i;
319
320 if (a == NULL) return;
321
322 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509_CRL);
323#ifdef REF_PRINT
324 REF_PRINT("X509_CRL",a);
325#endif
326 if (i > 0) return;
327#ifdef REF_CHECK
328 if (i < 0)
329 {
330 fprintf(stderr,"X509_CRL_free, bad reference count\n");
331 abort();
332 }
333#endif
334
335 X509_CRL_INFO_free(a->crl);
336 X509_ALGOR_free(a->sig_alg);
337 ASN1_BIT_STRING_free(a->signature);
338 Free((char *)a);
339 }
340
341static int X509_REVOKED_cmp(a,b)
342X509_REVOKED **a,**b;
343 {
344 return(ASN1_STRING_cmp(
345 (ASN1_STRING *)(*a)->serialNumber,
346 (ASN1_STRING *)(*b)->serialNumber));
347 }
348
349static int X509_REVOKED_seq_cmp(a,b)
350X509_REVOKED **a,**b;
351 {
352 return((*a)->sequence-(*b)->sequence);
353 }
diff --git a/src/lib/libcrypto/asn1/x_exten.c b/src/lib/libcrypto/asn1/x_exten.c
new file mode 100644
index 0000000000..54ffe2f00b
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_exten.c
@@ -0,0 +1,156 @@
1/* crypto/asn1/x_exten.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "objects.h"
62#include "asn1_mac.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_EXTENSION,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_X509_EXTENSION_NEW,ASN1_R_LENGTH_MISMATCH);
67 */
68
69int i2d_X509_EXTENSION(a,pp)
70X509_EXTENSION *a;
71unsigned char **pp;
72 {
73 int k=0;
74 int r=0,ret=0;
75 unsigned char **p=NULL;
76
77 if (a == NULL) return(0);
78
79 p=NULL;
80 for (;;)
81 {
82 if (k)
83 {
84 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE);
85 if (pp == NULL) return(r);
86 p=pp;
87 ASN1_put_object(p,1,ret,V_ASN1_SEQUENCE,
88 V_ASN1_UNIVERSAL);
89 }
90
91 ret+=i2d_ASN1_OBJECT(a->object,p);
92 if ((a->critical) || a->netscape_hack)
93 ret+=i2d_ASN1_BOOLEAN(a->critical,p);
94 ret+=i2d_ASN1_OCTET_STRING(a->value,p);
95 if (k++) return(r);
96 }
97 }
98
99X509_EXTENSION *d2i_X509_EXTENSION(a,pp,length)
100X509_EXTENSION **a;
101unsigned char **pp;
102long length;
103 {
104 int i;
105 M_ASN1_D2I_vars(a,X509_EXTENSION *,X509_EXTENSION_new);
106
107 M_ASN1_D2I_Init();
108 M_ASN1_D2I_start_sequence();
109 M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT);
110
111 if ((ret->argp != NULL) && (ret->ex_free != NULL))
112 ret->ex_free(ret);
113 ret->argl=0;
114 ret->argp=NULL;
115 ret->netscape_hack=0;
116 if ((c.slen != 0) &&
117 (M_ASN1_next == (V_ASN1_UNIVERSAL|V_ASN1_BOOLEAN)))
118 {
119 c.q=c.p;
120 if (d2i_ASN1_BOOLEAN(&i,&c.p,c.slen) < 0) goto err;
121 ret->critical=i;
122 c.slen-=(c.p-c.q);
123 if (ret->critical == 0) ret->netscape_hack=1;
124 }
125 M_ASN1_D2I_get(ret->value,d2i_ASN1_OCTET_STRING);
126
127 M_ASN1_D2I_Finish(a,X509_EXTENSION_free,ASN1_F_D2I_X509_EXTENSION);
128 }
129
130X509_EXTENSION *X509_EXTENSION_new()
131 {
132 X509_EXTENSION *ret=NULL;
133
134 M_ASN1_New_Malloc(ret,X509_EXTENSION);
135 M_ASN1_New(ret->object,ASN1_OBJECT_new);
136 M_ASN1_New(ret->value,ASN1_OCTET_STRING_new);
137 ret->critical=0;
138 ret->netscape_hack=0;
139 ret->argl=0L;
140 ret->argp=NULL;
141 ret->ex_free=NULL;
142 return(ret);
143 M_ASN1_New_Error(ASN1_F_X509_EXTENSION_NEW);
144 }
145
146void X509_EXTENSION_free(a)
147X509_EXTENSION *a;
148 {
149 if (a == NULL) return;
150 if ((a->argp != NULL) && (a->ex_free != NULL))
151 a->ex_free(a);
152 ASN1_OBJECT_free(a->object);
153 ASN1_OCTET_STRING_free(a->value);
154 Free((char *)a);
155 }
156
diff --git a/src/lib/libcrypto/asn1/x_info.c b/src/lib/libcrypto/asn1/x_info.c
new file mode 100644
index 0000000000..b55f0ce77a
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_info.c
@@ -0,0 +1,111 @@
1/* crypto/asn1/x_info.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "asn1_mac.h"
63#include "x509.h"
64
65X509_INFO *X509_INFO_new()
66 {
67 X509_INFO *ret=NULL;
68
69 ret=(X509_INFO *)Malloc(sizeof(X509_INFO));
70 if (ret == NULL)
71 {
72 ASN1err(ASN1_F_X509_INFO_NEW,ERR_R_MALLOC_FAILURE);
73 return(NULL);
74 }
75
76 ret->enc_cipher.cipher=NULL;
77 ret->enc_len=0;
78 ret->enc_data=NULL;
79
80 ret->references=1;
81 ret->x509=NULL;
82 ret->crl=NULL;
83 ret->x_pkey=NULL;
84 return(ret);
85 }
86
87void X509_INFO_free(x)
88X509_INFO *x;
89 {
90 int i;
91
92 if (x == NULL) return;
93
94 i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_X509_INFO);
95#ifdef REF_PRINT
96 REF_PRINT("X509_INFO",x);
97#endif
98 if (i > 0) return;
99#ifdef REF_CHECK
100 if (i < 0)
101 {
102 fprintf(stderr,"X509_INFO_free, bad reference count\n");
103 abort();
104 }
105#endif
106
107 if (x->x509 != NULL) X509_free(x->x509);
108 if (x->crl != NULL) X509_CRL_free(x->crl);
109 if (x->x_pkey != NULL) X509_PKEY_free(x->x_pkey);
110 Free((char *)x);
111 }
diff --git a/src/lib/libcrypto/asn1/x_name.c b/src/lib/libcrypto/asn1/x_name.c
new file mode 100644
index 0000000000..28b9c34b58
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_name.c
@@ -0,0 +1,295 @@
1/* crypto/asn1/x_name.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "objects.h"
62#include "asn1_mac.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_NAME,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_X509_NAME_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
67 * ASN1err(ASN1_F_D2I_X509_NAME_ENTRY,ASN1_R_LENGTH_MISMATCH);
68 * ASN1err(ASN1_F_X509_NAME_ENTRY_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
69 */
70
71#ifndef NOPROTO
72static int i2d_X509_NAME_entries(X509_NAME *a);
73#else
74static int i2d_X509_NAME_entries();
75#endif
76
77int i2d_X509_NAME_ENTRY(a,pp)
78X509_NAME_ENTRY *a;
79unsigned char **pp;
80 {
81 M_ASN1_I2D_vars(a);
82
83 M_ASN1_I2D_len(a->object,i2d_ASN1_OBJECT);
84 M_ASN1_I2D_len(a->value,i2d_ASN1_PRINTABLE);
85
86 M_ASN1_I2D_seq_total();
87
88 M_ASN1_I2D_put(a->object,i2d_ASN1_OBJECT);
89 M_ASN1_I2D_put(a->value,i2d_ASN1_PRINTABLE);
90
91 M_ASN1_I2D_finish();
92 }
93
94X509_NAME_ENTRY *d2i_X509_NAME_ENTRY(a,pp,length)
95X509_NAME_ENTRY **a;
96unsigned char **pp;
97long length;
98 {
99 M_ASN1_D2I_vars(a,X509_NAME_ENTRY *,X509_NAME_ENTRY_new);
100
101 M_ASN1_D2I_Init();
102 M_ASN1_D2I_start_sequence();
103 M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT);
104 M_ASN1_D2I_get(ret->value,d2i_ASN1_PRINTABLE);
105 ret->set=0;
106 M_ASN1_D2I_Finish(a,X509_NAME_ENTRY_free,ASN1_F_D2I_X509_NAME_ENTRY);
107 }
108
109int i2d_X509_NAME(a,pp)
110X509_NAME *a;
111unsigned char **pp;
112 {
113 int ret;
114
115 if (a == NULL) return(0);
116 if (a->modified)
117 {
118 ret=i2d_X509_NAME_entries(a);
119 if (ret < 0) return(ret);
120 }
121
122 ret=a->bytes->length;
123 if (pp != NULL)
124 {
125 memcpy(*pp,a->bytes->data,ret);
126 *pp+=ret;
127 }
128 return(ret);
129 }
130
131static int i2d_X509_NAME_entries(a)
132X509_NAME *a;
133 {
134 X509_NAME_ENTRY *ne,*fe=NULL;
135 STACK *sk;
136 BUF_MEM *buf=NULL;
137 int set=0,r,ret=0;
138 int i;
139 unsigned char *p;
140 int size=0;
141
142 sk=a->entries;
143 for (i=0; i<sk_num(sk); i++)
144 {
145 ne=(X509_NAME_ENTRY *)sk_value(sk,i);
146 if (fe == NULL)
147 {
148 fe=ne;
149 size=0;
150 }
151
152 if (ne->set != set)
153 {
154 ret+=ASN1_object_size(1,size,V_ASN1_SET);
155 fe->size=size;
156 fe=ne;
157 size=0;
158 set=ne->set;
159 }
160 size+=i2d_X509_NAME_ENTRY(ne,NULL);
161 }
162
163 ret+=ASN1_object_size(1,size,V_ASN1_SET);
164 if (fe != NULL)
165 fe->size=size;
166
167 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE);
168
169 buf=a->bytes;
170 if (!BUF_MEM_grow(buf,r)) goto err;
171 p=(unsigned char *)buf->data;
172
173 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
174
175 set= -1;
176 for (i=0; i<sk_num(sk); i++)
177 {
178 ne=(X509_NAME_ENTRY *)sk_value(sk,i);
179 if (set != ne->set)
180 {
181 set=ne->set;
182 ASN1_put_object(&p,1,ne->size,
183 V_ASN1_SET,V_ASN1_UNIVERSAL);
184 }
185 i2d_X509_NAME_ENTRY(ne,&p);
186 }
187 a->modified=0;
188 return(r);
189err:
190 return(-1);
191 }
192
193X509_NAME *d2i_X509_NAME(a,pp,length)
194X509_NAME **a;
195unsigned char **pp;
196long length;
197 {
198 int set=0,i;
199 int idx=0;
200 unsigned char *orig;
201 M_ASN1_D2I_vars(a,X509_NAME *,X509_NAME_new);
202
203 orig= *pp;
204 if (sk_num(ret->entries) > 0)
205 {
206 while (sk_num(ret->entries) > 0)
207 X509_NAME_ENTRY_free((X509_NAME_ENTRY *)
208 sk_pop(ret->entries));
209 }
210
211 M_ASN1_D2I_Init();
212 M_ASN1_D2I_start_sequence();
213 for (;;)
214 {
215 if (M_ASN1_D2I_end_sequence()) break;
216 M_ASN1_D2I_get_set(ret->entries,d2i_X509_NAME_ENTRY);
217 for (; idx < sk_num(ret->entries); idx++)
218 {
219 ((X509_NAME_ENTRY *)sk_value(ret->entries,idx))->set=
220 set;
221 }
222 set++;
223 }
224
225 i=(int)(c.p-orig);
226 if (!BUF_MEM_grow(ret->bytes,i)) goto err;
227 memcpy(ret->bytes->data,orig,i);
228 ret->bytes->length=i;
229 ret->modified=0;
230
231 M_ASN1_D2I_Finish(a,X509_NAME_free,ASN1_F_D2I_X509_NAME);
232 }
233
234X509_NAME *X509_NAME_new()
235 {
236 X509_NAME *ret=NULL;
237
238 M_ASN1_New_Malloc(ret,X509_NAME);
239 if ((ret->entries=sk_new(NULL)) == NULL) goto err2;
240 M_ASN1_New(ret->bytes,BUF_MEM_new);
241 ret->modified=1;
242 return(ret);
243 M_ASN1_New_Error(ASN1_F_X509_NAME_NEW);
244 }
245
246X509_NAME_ENTRY *X509_NAME_ENTRY_new()
247 {
248 X509_NAME_ENTRY *ret=NULL;
249
250 M_ASN1_New_Malloc(ret,X509_NAME_ENTRY);
251/* M_ASN1_New(ret->object,ASN1_OBJECT_new);*/
252 ret->object=NULL;
253 ret->set=0;
254 M_ASN1_New(ret->value,ASN1_STRING_new);
255 return(ret);
256 M_ASN1_New_Error(ASN1_F_X509_NAME_ENTRY_NEW);
257 }
258
259void X509_NAME_free(a)
260X509_NAME *a;
261 {
262 BUF_MEM_free(a->bytes);
263 sk_pop_free(a->entries,X509_NAME_ENTRY_free);
264 Free((char *)a);
265 }
266
267void X509_NAME_ENTRY_free(a)
268X509_NAME_ENTRY *a;
269 {
270 if (a == NULL) return;
271 ASN1_OBJECT_free(a->object);
272 ASN1_BIT_STRING_free(a->value);
273 Free((char *)a);
274 }
275
276int X509_NAME_set(xn,name)
277X509_NAME **xn;
278X509_NAME *name;
279 {
280 X509_NAME *in;
281
282 if (*xn == NULL) return(0);
283
284 if (*xn != name)
285 {
286 in=X509_NAME_dup(name);
287 if (in != NULL)
288 {
289 X509_NAME_free(*xn);
290 *xn=in;
291 }
292 }
293 return(*xn != NULL);
294 }
295
diff --git a/src/lib/libcrypto/asn1/x_pkey.c b/src/lib/libcrypto/asn1/x_pkey.c
new file mode 100644
index 0000000000..1d4d926129
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_pkey.c
@@ -0,0 +1,156 @@
1/* crypto/asn1/x_pkey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "asn1_mac.h"
64
65/* ASN1err(ASN1_F_D2I_X509_PKEY,ASN1_R_UNSUPPORTED_CIPHER); */
66/* ASN1err(ASN1_F_X509_PKEY_NEW,ASN1_R_IV_TOO_LARGE); */
67
68/* need to implement */
69int i2d_X509_PKEY(a,pp)
70X509_PKEY *a;
71unsigned char **pp;
72 {
73 return(0);
74 }
75
76X509_PKEY *d2i_X509_PKEY(a,pp,length)
77X509_PKEY **a;
78unsigned char **pp;
79long length;
80 {
81 int i;
82 M_ASN1_D2I_vars(a,X509_PKEY *,X509_PKEY_new);
83
84 M_ASN1_D2I_Init();
85 M_ASN1_D2I_start_sequence();
86 M_ASN1_D2I_get(ret->enc_algor,d2i_X509_ALGOR);
87 M_ASN1_D2I_get(ret->enc_pkey,d2i_ASN1_OCTET_STRING);
88
89 ret->cipher.cipher=EVP_get_cipherbyname(
90 OBJ_nid2ln(OBJ_obj2nid(ret->enc_algor->algorithm)));
91 if (ret->cipher.cipher == NULL)
92 {
93 c.error=ASN1_R_UNSUPPORTED_CIPHER;
94 goto err;
95 }
96 if (ret->enc_algor->parameter->type == V_ASN1_OCTET_STRING)
97 {
98 i=ret->enc_algor->parameter->value.octet_string->length;
99 if (i > EVP_MAX_IV_LENGTH)
100 {
101 c.error=ASN1_R_IV_TOO_LARGE;
102 goto err;
103 }
104 memcpy(ret->cipher.iv,
105 ret->enc_algor->parameter->value.octet_string->data,i);
106 }
107 else
108 memset(ret->cipher.iv,0,EVP_MAX_IV_LENGTH);
109 M_ASN1_D2I_Finish(a,X509_PKEY_free,ASN1_F_D2I_X509_PKEY);
110 }
111
112X509_PKEY *X509_PKEY_new()
113 {
114 X509_PKEY *ret=NULL;
115
116 M_ASN1_New_Malloc(ret,X509_PKEY);
117 ret->version=0;
118 M_ASN1_New(ret->enc_algor,X509_ALGOR_new);
119 M_ASN1_New(ret->enc_pkey,ASN1_OCTET_STRING_new);
120 ret->dec_pkey=NULL;
121 ret->key_length=0;
122 ret->key_data=NULL;
123 ret->key_free=0;
124 ret->cipher.cipher=NULL;
125 memset(ret->cipher.iv,0,EVP_MAX_IV_LENGTH);
126 ret->references=1;
127 return(ret);
128 M_ASN1_New_Error(ASN1_F_X509_PKEY_NEW);
129 }
130
131void X509_PKEY_free(x)
132X509_PKEY *x;
133 {
134 int i;
135
136 if (x == NULL) return;
137
138 i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_X509_PKEY);
139#ifdef REF_PRINT
140 REF_PRINT("X509_PKEY",x);
141#endif
142 if (i > 0) return;
143#ifdef REF_CHECK
144 if (i < 0)
145 {
146 fprintf(stderr,"X509_PKEY_free, bad reference count\n");
147 abort();
148 }
149#endif
150
151 if (x->enc_algor != NULL) X509_ALGOR_free(x->enc_algor);
152 if (x->enc_pkey != NULL) ASN1_OCTET_STRING_free(x->enc_pkey);
153 if (x->dec_pkey != NULL)EVP_PKEY_free(x->dec_pkey);
154 if ((x->key_data != NULL) && (x->key_free)) Free((char *)x->key_data);
155 Free((char *)(char *)x);
156 }
diff --git a/src/lib/libcrypto/asn1/x_pubkey.c b/src/lib/libcrypto/asn1/x_pubkey.c
new file mode 100644
index 0000000000..a309cf74a7
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_pubkey.c
@@ -0,0 +1,256 @@
1/* crypto/asn1/x_pubkey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/*
64 * ASN1err(ASN1_F_D2I_X509_PUBKEY,ASN1_R_LENGTH_MISMATCH);
65 * ASN1err(ASN1_F_X509_PUBKEY_NEW,ASN1_R_LENGTH_MISMATCH);
66 */
67
68int i2d_X509_PUBKEY(a,pp)
69X509_PUBKEY *a;
70unsigned char **pp;
71 {
72 M_ASN1_I2D_vars(a);
73
74 M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR);
75 M_ASN1_I2D_len(a->public_key, i2d_ASN1_BIT_STRING);
76
77 M_ASN1_I2D_seq_total();
78
79 M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR);
80 M_ASN1_I2D_put(a->public_key, i2d_ASN1_BIT_STRING);
81
82 M_ASN1_I2D_finish();
83 }
84
85X509_PUBKEY *d2i_X509_PUBKEY(a,pp,length)
86X509_PUBKEY **a;
87unsigned char **pp;
88long length;
89 {
90 M_ASN1_D2I_vars(a,X509_PUBKEY *,X509_PUBKEY_new);
91
92 M_ASN1_D2I_Init();
93 M_ASN1_D2I_start_sequence();
94 M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
95 M_ASN1_D2I_get(ret->public_key,d2i_ASN1_BIT_STRING);
96 if (ret->pkey != NULL)
97 {
98 EVP_PKEY_free(ret->pkey);
99 ret->pkey=NULL;
100 }
101 M_ASN1_D2I_Finish(a,X509_PUBKEY_free,ASN1_F_D2I_X509_PUBKEY);
102 }
103
104X509_PUBKEY *X509_PUBKEY_new()
105 {
106 X509_PUBKEY *ret=NULL;
107
108 M_ASN1_New_Malloc(ret,X509_PUBKEY);
109 M_ASN1_New(ret->algor,X509_ALGOR_new);
110 M_ASN1_New(ret->public_key,ASN1_BIT_STRING_new);
111 ret->pkey=NULL;
112 return(ret);
113 M_ASN1_New_Error(ASN1_F_X509_PUBKEY_NEW);
114 }
115
116void X509_PUBKEY_free(a)
117X509_PUBKEY *a;
118 {
119 if (a == NULL) return;
120 X509_ALGOR_free(a->algor);
121 ASN1_BIT_STRING_free(a->public_key);
122 if (a->pkey != NULL) EVP_PKEY_free(a->pkey);
123 Free((char *)a);
124 }
125
126int X509_PUBKEY_set(x,pkey)
127X509_PUBKEY **x;
128EVP_PKEY *pkey;
129 {
130 int ok=0;
131 X509_PUBKEY *pk;
132 X509_ALGOR *a;
133 ASN1_OBJECT *o;
134 unsigned char *s,*p;
135 int i;
136
137 if (x == NULL) return(0);
138
139 if ((pk=X509_PUBKEY_new()) == NULL) goto err;
140 a=pk->algor;
141
142 /* set the algorithm id */
143 if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err;
144 ASN1_OBJECT_free(a->algorithm);
145 a->algorithm=o;
146
147 /* Set the parameter list */
148 if (!pkey->save_parameters || (pkey->type == EVP_PKEY_RSA))
149 {
150 if ((a->parameter == NULL) ||
151 (a->parameter->type != V_ASN1_NULL))
152 {
153 ASN1_TYPE_free(a->parameter);
154 a->parameter=ASN1_TYPE_new();
155 a->parameter->type=V_ASN1_NULL;
156 }
157 }
158 else
159#ifndef NO_DSA
160 if (pkey->type == EVP_PKEY_DSA)
161 {
162 unsigned char *pp;
163 DSA *dsa;
164
165 dsa=pkey->pkey.dsa;
166 dsa->write_params=0;
167 ASN1_TYPE_free(a->parameter);
168 i=i2d_DSAparams(dsa,NULL);
169 p=(unsigned char *)Malloc(i);
170 pp=p;
171 i2d_DSAparams(dsa,&pp);
172 a->parameter=ASN1_TYPE_new();
173 a->parameter->type=V_ASN1_SEQUENCE;
174 a->parameter->value.sequence=ASN1_STRING_new();
175 ASN1_STRING_set(a->parameter->value.sequence,p,i);
176 Free(p);
177 }
178 else
179#endif
180 {
181 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
182 goto err;
183 }
184
185 i=i2d_PublicKey(pkey,NULL);
186 if ((s=(unsigned char *)Malloc(i+1)) == NULL) goto err;
187 p=s;
188 i2d_PublicKey(pkey,&p);
189 if (!ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
190 Free(s);
191
192 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
193 pk->pkey=pkey;
194
195 if (*x != NULL)
196 X509_PUBKEY_free(*x);
197
198 *x=pk;
199 pk=NULL;
200
201 ok=1;
202err:
203 if (pk != NULL) X509_PUBKEY_free(pk);
204 return(ok);
205 }
206
207EVP_PKEY *X509_PUBKEY_get(key)
208X509_PUBKEY *key;
209 {
210 EVP_PKEY *ret=NULL;
211 long j;
212 int type;
213 unsigned char *p;
214#ifndef NO_DSA
215 X509_ALGOR *a;
216#endif
217
218 if (key == NULL) goto err;
219
220 if (key->pkey != NULL) return(key->pkey);
221
222 if (key->public_key == NULL) goto err;
223
224 type=OBJ_obj2nid(key->algor->algorithm);
225 p=key->public_key->data;
226 j=key->public_key->length;
227 if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL)
228 {
229 X509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB);
230 goto err;
231 }
232 ret->save_parameters=0;
233
234#ifndef NO_DSA
235 a=key->algor;
236 if (ret->type == EVP_PKEY_DSA)
237 {
238 if (a->parameter->type == V_ASN1_SEQUENCE)
239 {
240 ret->pkey.dsa->write_params=0;
241 p=a->parameter->value.sequence->data;
242 j=a->parameter->value.sequence->length;
243 if (!d2i_DSAparams(&ret->pkey.dsa,&p,(long)j))
244 goto err;
245 }
246 ret->save_parameters=1;
247 }
248#endif
249 key->pkey=ret;
250 return(ret);
251err:
252 if (ret != NULL)
253 EVP_PKEY_free(ret);
254 return(NULL);
255 }
256
diff --git a/src/lib/libcrypto/asn1/x_req.c b/src/lib/libcrypto/asn1/x_req.c
new file mode 100644
index 0000000000..ff0be13d37
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_req.c
@@ -0,0 +1,247 @@
1/* crypto/asn1/x_req.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62#include "x509.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509_REQ,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_D2I_X509_REQ_INFO,ASN1_R_LENGTH_MISMATCH);
67 * ASN1err(ASN1_F_X509_REQ_NEW,ASN1_R_LENGTH_MISMATCH);
68 * ASN1err(ASN1_F_X509_REQ_INFO_NEW,ASN1_R_LENGTH_MISMATCH);
69 */
70
71int i2d_X509_REQ_INFO(a,pp)
72X509_REQ_INFO *a;
73unsigned char **pp;
74 {
75 M_ASN1_I2D_vars(a);
76
77 M_ASN1_I2D_len(a->version, i2d_ASN1_INTEGER);
78 M_ASN1_I2D_len(a->subject, i2d_X509_NAME);
79 M_ASN1_I2D_len(a->pubkey, i2d_X509_PUBKEY);
80
81 /* this is a *nasty* hack reported to be required to
82 * allow some CA Software to accept the cert request.
83 * It is not following the PKCS standards ...
84 * PKCS#10 pg 5
85 * attributes [0] IMPLICIT Attibutes
86 * NOTE: no OPTIONAL ... so it *must* be there
87 */
88 if (a->req_kludge)
89 {
90 M_ASN1_I2D_len_IMP_set_opt(a->attributes,i2d_X509_ATTRIBUTE,0);
91 }
92 else
93 {
94 M_ASN1_I2D_len_IMP_set(a->attributes, i2d_X509_ATTRIBUTE,0);
95 }
96
97 M_ASN1_I2D_seq_total();
98 M_ASN1_I2D_put(a->version, i2d_ASN1_INTEGER);
99 M_ASN1_I2D_put(a->subject, i2d_X509_NAME);
100 M_ASN1_I2D_put(a->pubkey, i2d_X509_PUBKEY);
101
102 /* this is a *nasty* hack reported to be required by some CA's.
103 * It is not following the PKCS standards ...
104 * PKCS#10 pg 5
105 * attributes [0] IMPLICIT Attibutes
106 * NOTE: no OPTIONAL ... so it *must* be there
107 */
108 if (a->req_kludge)
109 {
110 M_ASN1_I2D_put_IMP_set_opt(a->attributes,i2d_X509_ATTRIBUTE,0);
111 }
112 else
113 {
114 M_ASN1_I2D_put_IMP_set(a->attributes,i2d_X509_ATTRIBUTE,0);
115 }
116
117 M_ASN1_I2D_finish();
118 }
119
120X509_REQ_INFO *d2i_X509_REQ_INFO(a,pp,length)
121X509_REQ_INFO **a;
122unsigned char **pp;
123long length;
124 {
125 M_ASN1_D2I_vars(a,X509_REQ_INFO *,X509_REQ_INFO_new);
126
127 M_ASN1_D2I_Init();
128 M_ASN1_D2I_start_sequence();
129 M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER);
130 M_ASN1_D2I_get(ret->subject,d2i_X509_NAME);
131 M_ASN1_D2I_get(ret->pubkey,d2i_X509_PUBKEY);
132
133 /* this is a *nasty* hack to allow for some CA's that
134 * have been reported as requiring it.
135 * It is not following the PKCS standards ...
136 * PKCS#10 pg 5
137 * attributes [0] IMPLICIT Attibutes
138 * NOTE: no OPTIONAL ... so it *must* be there
139 */
140 if (asn1_Finish(&c))
141 ret->req_kludge=1;
142 else
143 {
144 M_ASN1_D2I_get_IMP_set(ret->attributes,d2i_X509_ATTRIBUTE,0);
145 }
146
147 M_ASN1_D2I_Finish(a,X509_REQ_INFO_free,ASN1_F_D2I_X509_REQ_INFO);
148 }
149
150X509_REQ_INFO *X509_REQ_INFO_new()
151 {
152 X509_REQ_INFO *ret=NULL;
153
154 M_ASN1_New_Malloc(ret,X509_REQ_INFO);
155 M_ASN1_New(ret->version,ASN1_INTEGER_new);
156 M_ASN1_New(ret->subject,X509_NAME_new);
157 M_ASN1_New(ret->pubkey,X509_PUBKEY_new);
158 M_ASN1_New(ret->attributes,sk_new_null);
159 ret->req_kludge=0;
160 return(ret);
161 M_ASN1_New_Error(ASN1_F_X509_REQ_INFO_NEW);
162 }
163
164void X509_REQ_INFO_free(a)
165X509_REQ_INFO *a;
166 {
167 if (a == NULL) return;
168 ASN1_INTEGER_free(a->version);
169 X509_NAME_free(a->subject);
170 X509_PUBKEY_free(a->pubkey);
171 sk_pop_free(a->attributes,X509_ATTRIBUTE_free);
172 Free((char *)a);
173 }
174
175int i2d_X509_REQ(a,pp)
176X509_REQ *a;
177unsigned char **pp;
178 {
179 M_ASN1_I2D_vars(a);
180 M_ASN1_I2D_len(a->req_info, i2d_X509_REQ_INFO);
181 M_ASN1_I2D_len(a->sig_alg, i2d_X509_ALGOR);
182 M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING);
183
184 M_ASN1_I2D_seq_total();
185
186 M_ASN1_I2D_put(a->req_info, i2d_X509_REQ_INFO);
187 M_ASN1_I2D_put(a->sig_alg, i2d_X509_ALGOR);
188 M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING);
189
190 M_ASN1_I2D_finish();
191 }
192
193X509_REQ *d2i_X509_REQ(a,pp,length)
194X509_REQ **a;
195unsigned char **pp;
196long length;
197 {
198 M_ASN1_D2I_vars(a,X509_REQ *,X509_REQ_new);
199
200 M_ASN1_D2I_Init();
201 M_ASN1_D2I_start_sequence();
202 M_ASN1_D2I_get(ret->req_info,d2i_X509_REQ_INFO);
203 M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);
204 M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);
205 M_ASN1_D2I_Finish(a,X509_REQ_free,ASN1_F_D2I_X509_REQ);
206 }
207
208X509_REQ *X509_REQ_new()
209 {
210 X509_REQ *ret=NULL;
211
212 M_ASN1_New_Malloc(ret,X509_REQ);
213 ret->references=1;
214 M_ASN1_New(ret->req_info,X509_REQ_INFO_new);
215 M_ASN1_New(ret->sig_alg,X509_ALGOR_new);
216 M_ASN1_New(ret->signature,ASN1_BIT_STRING_new);
217 return(ret);
218 M_ASN1_New_Error(ASN1_F_X509_REQ_NEW);
219 }
220
221void X509_REQ_free(a)
222X509_REQ *a;
223 {
224 int i;
225
226 if (a == NULL) return;
227
228 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509_REQ);
229#ifdef REF_PRINT
230 REF_PRINT("X509_REQ",a);
231#endif
232 if (i > 0) return;
233#ifdef REF_CHECK
234 if (i < 0)
235 {
236 fprintf(stderr,"X509_REQ_free, bad reference count\n");
237 abort();
238 }
239#endif
240
241 X509_REQ_INFO_free(a->req_info);
242 X509_ALGOR_free(a->sig_alg);
243 ASN1_BIT_STRING_free(a->signature);
244 Free((char *)a);
245 }
246
247
diff --git a/src/lib/libcrypto/asn1/x_sig.c b/src/lib/libcrypto/asn1/x_sig.c
new file mode 100644
index 0000000000..f0a2e4c27a
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_sig.c
@@ -0,0 +1,119 @@
1/* crypto/asn1/x_sig.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/*
64 * ASN1err(ASN1_F_D2I_X509_SIG,ASN1_R_LENGTH_MISMATCH);
65 * ASN1err(ASN1_F_X509_SIG_NEW,ASN1_R_LENGTH_MISMATCH);
66 */
67
68int i2d_X509_SIG(a,pp)
69X509_SIG *a;
70unsigned char **pp;
71 {
72 M_ASN1_I2D_vars(a);
73
74 M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR);
75 M_ASN1_I2D_len(a->digest, i2d_ASN1_OCTET_STRING);
76
77 M_ASN1_I2D_seq_total();
78
79 M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR);
80 M_ASN1_I2D_put(a->digest, i2d_ASN1_OCTET_STRING);
81
82 M_ASN1_I2D_finish();
83 }
84
85X509_SIG *d2i_X509_SIG(a,pp,length)
86X509_SIG **a;
87unsigned char **pp;
88long length;
89 {
90 M_ASN1_D2I_vars(a,X509_SIG *,X509_SIG_new);
91
92 M_ASN1_D2I_Init();
93 M_ASN1_D2I_start_sequence();
94 M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
95 M_ASN1_D2I_get(ret->digest,d2i_ASN1_OCTET_STRING);
96 M_ASN1_D2I_Finish(a,X509_SIG_free,ASN1_F_D2I_X509_SIG);
97 }
98
99X509_SIG *X509_SIG_new()
100 {
101 X509_SIG *ret=NULL;
102
103 M_ASN1_New_Malloc(ret,X509_SIG);
104 M_ASN1_New(ret->algor,X509_ALGOR_new);
105 M_ASN1_New(ret->digest,ASN1_OCTET_STRING_new);
106 return(ret);
107 M_ASN1_New_Error(ASN1_F_X509_SIG_NEW);
108 }
109
110void X509_SIG_free(a)
111X509_SIG *a;
112 {
113 if (a == NULL) return;
114 X509_ALGOR_free(a->algor);
115 ASN1_OCTET_STRING_free(a->digest);
116 Free((char *)a);
117 }
118
119
diff --git a/src/lib/libcrypto/asn1/x_spki.c b/src/lib/libcrypto/asn1/x_spki.c
new file mode 100644
index 0000000000..4a80df44b8
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_spki.c
@@ -0,0 +1,181 @@
1/* crypto/asn1/x_spki.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59 /* This module was send to me my Pat Richards <patr@x509.com> who
60 * wrote it. It is under my Copyright with his permision
61 */
62
63#include <stdio.h>
64#include "cryptlib.h"
65#include "x509.h"
66#include "asn1_mac.h"
67
68/*
69 * ASN1err(ASN1_F_D2I_NETSCAPE_SPKAC,ASN1_R_LENGTH_MISMATCH);
70 * ASN1err(ASN1_F_NETSCAPE_SPKAC_NEW,ASN1_R_LENGTH_MISMATCH);
71 * ASN1err(ASN1_F_D2I_NETSCAPE_SPKI,ASN1_R_LENGTH_MISMATCH);
72 * ASN1err(ASN1_F_NETSCAPE_SPKI_NEW,ASN1_R_LENGTH_MISMATCH);
73 */
74
75int i2d_NETSCAPE_SPKAC(a,pp)
76NETSCAPE_SPKAC *a;
77unsigned char **pp;
78 {
79 M_ASN1_I2D_vars(a);
80
81 M_ASN1_I2D_len(a->pubkey, i2d_X509_PUBKEY);
82 M_ASN1_I2D_len(a->challenge, i2d_ASN1_IA5STRING);
83
84 M_ASN1_I2D_seq_total();
85
86 M_ASN1_I2D_put(a->pubkey, i2d_X509_PUBKEY);
87 M_ASN1_I2D_put(a->challenge, i2d_ASN1_IA5STRING);
88
89 M_ASN1_I2D_finish();
90 }
91
92NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(a,pp,length)
93NETSCAPE_SPKAC **a;
94unsigned char **pp;
95long length;
96 {
97 M_ASN1_D2I_vars(a,NETSCAPE_SPKAC *,NETSCAPE_SPKAC_new);
98
99 M_ASN1_D2I_Init();
100 M_ASN1_D2I_start_sequence();
101 M_ASN1_D2I_get(ret->pubkey,d2i_X509_PUBKEY);
102 M_ASN1_D2I_get(ret->challenge,d2i_ASN1_IA5STRING);
103 M_ASN1_D2I_Finish(a,NETSCAPE_SPKAC_free,ASN1_F_D2I_NETSCAPE_SPKAC);
104 }
105
106NETSCAPE_SPKAC *NETSCAPE_SPKAC_new()
107 {
108 NETSCAPE_SPKAC *ret=NULL;
109
110 M_ASN1_New_Malloc(ret,NETSCAPE_SPKAC);
111 M_ASN1_New(ret->pubkey,X509_PUBKEY_new);
112 M_ASN1_New(ret->challenge,ASN1_IA5STRING_new);
113 return(ret);
114 M_ASN1_New_Error(ASN1_F_NETSCAPE_SPKAC_NEW);
115 }
116
117void NETSCAPE_SPKAC_free(a)
118NETSCAPE_SPKAC *a;
119 {
120 if (a == NULL) return;
121 X509_PUBKEY_free(a->pubkey);
122 ASN1_IA5STRING_free(a->challenge);
123 Free((char *)a);
124 }
125
126int i2d_NETSCAPE_SPKI(a,pp)
127NETSCAPE_SPKI *a;
128unsigned char **pp;
129 {
130 M_ASN1_I2D_vars(a);
131
132 M_ASN1_I2D_len(a->spkac, i2d_NETSCAPE_SPKAC);
133 M_ASN1_I2D_len(a->sig_algor, i2d_X509_ALGOR);
134 M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING);
135
136 M_ASN1_I2D_seq_total();
137
138 M_ASN1_I2D_put(a->spkac, i2d_NETSCAPE_SPKAC);
139 M_ASN1_I2D_put(a->sig_algor, i2d_X509_ALGOR);
140 M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING);
141
142 M_ASN1_I2D_finish();
143 }
144
145NETSCAPE_SPKI *d2i_NETSCAPE_SPKI(a,pp,length)
146NETSCAPE_SPKI **a;
147unsigned char **pp;
148long length;
149 {
150 M_ASN1_D2I_vars(a,NETSCAPE_SPKI *,NETSCAPE_SPKI_new);
151
152 M_ASN1_D2I_Init();
153 M_ASN1_D2I_start_sequence();
154 M_ASN1_D2I_get(ret->spkac,d2i_NETSCAPE_SPKAC);
155 M_ASN1_D2I_get(ret->sig_algor,d2i_X509_ALGOR);
156 M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);
157 M_ASN1_D2I_Finish(a,NETSCAPE_SPKI_free,ASN1_F_D2I_NETSCAPE_SPKI);
158 }
159
160NETSCAPE_SPKI *NETSCAPE_SPKI_new()
161 {
162 NETSCAPE_SPKI *ret=NULL;
163
164 M_ASN1_New_Malloc(ret,NETSCAPE_SPKI);
165 M_ASN1_New(ret->spkac,NETSCAPE_SPKAC_new);
166 M_ASN1_New(ret->sig_algor,X509_ALGOR_new);
167 M_ASN1_New(ret->signature,ASN1_BIT_STRING_new);
168 return(ret);
169 M_ASN1_New_Error(ASN1_F_NETSCAPE_SPKI_NEW);
170 }
171
172void NETSCAPE_SPKI_free(a)
173NETSCAPE_SPKI *a;
174 {
175 if (a == NULL) return;
176 NETSCAPE_SPKAC_free(a->spkac);
177 X509_ALGOR_free(a->sig_algor);
178 ASN1_BIT_STRING_free(a->signature);
179 Free((char *)a);
180 }
181
diff --git a/src/lib/libcrypto/asn1/x_val.c b/src/lib/libcrypto/asn1/x_val.c
new file mode 100644
index 0000000000..a9c390f88c
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_val.c
@@ -0,0 +1,118 @@
1/* crypto/asn1/x_val.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/* ASN1err(ASN1_F_X509_VAL_NEW,ERR_R_MALLOC_FAILURE);
64 * ASN1err(ASN1_F_X509_VAL_FREE,ERR_R_MALLOC_FAILURE);
65 * ASN1err(ASN1_F_D2I_X509_VAL,ERR_R_MALLOC_FAILURE);
66 */
67
68int i2d_X509_VAL(a,pp)
69X509_VAL *a;
70unsigned char **pp;
71 {
72 M_ASN1_I2D_vars(a);
73
74 M_ASN1_I2D_len(a->notBefore,i2d_ASN1_UTCTIME);
75 M_ASN1_I2D_len(a->notAfter,i2d_ASN1_UTCTIME);
76
77 M_ASN1_I2D_seq_total();
78
79 M_ASN1_I2D_put(a->notBefore,i2d_ASN1_UTCTIME);
80 M_ASN1_I2D_put(a->notAfter,i2d_ASN1_UTCTIME);
81
82 M_ASN1_I2D_finish();
83 }
84
85X509_VAL *d2i_X509_VAL(a,pp,length)
86X509_VAL **a;
87unsigned char **pp;
88long length;
89 {
90 M_ASN1_D2I_vars(a,X509_VAL *,X509_VAL_new);
91
92 M_ASN1_D2I_Init();
93 M_ASN1_D2I_start_sequence();
94 M_ASN1_D2I_get(ret->notBefore,d2i_ASN1_UTCTIME);
95 M_ASN1_D2I_get(ret->notAfter,d2i_ASN1_UTCTIME);
96 M_ASN1_D2I_Finish(a,X509_VAL_free,ASN1_F_D2I_X509_VAL);
97 }
98
99X509_VAL *X509_VAL_new()
100 {
101 X509_VAL *ret=NULL;
102
103 M_ASN1_New_Malloc(ret,X509_VAL);
104 M_ASN1_New(ret->notBefore,ASN1_UTCTIME_new);
105 M_ASN1_New(ret->notAfter,ASN1_UTCTIME_new);
106 return(ret);
107 M_ASN1_New_Error(ASN1_F_X509_VAL_NEW);
108 }
109
110void X509_VAL_free(a)
111X509_VAL *a;
112 {
113 if (a == NULL) return;
114 ASN1_UTCTIME_free(a->notBefore);
115 ASN1_UTCTIME_free(a->notAfter);
116 Free((char *)a);
117 }
118
diff --git a/src/lib/libcrypto/asn1/x_x509.c b/src/lib/libcrypto/asn1/x_x509.c
new file mode 100644
index 0000000000..bc466ce0f6
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_x509.c
@@ -0,0 +1,158 @@
1/* crypto/asn1/x_x509.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "asn1_mac.h"
63
64/*
65 * ASN1err(ASN1_F_D2I_X509,ASN1_R_LENGTH_MISMATCH);
66 * ASN1err(ASN1_F_X509_NEW,ASN1_R_BAD_GET_OBJECT);
67 */
68
69static ASN1_METHOD meth={
70 (int (*)()) i2d_X509,
71 (char *(*)())d2i_X509,
72 (char *(*)())X509_new,
73 (void (*)()) X509_free};
74
75ASN1_METHOD *X509_asn1_meth()
76 {
77 return(&meth);
78 }
79
80int i2d_X509(a,pp)
81X509 *a;
82unsigned char **pp;
83 {
84 M_ASN1_I2D_vars(a);
85
86 M_ASN1_I2D_len(a->cert_info, i2d_X509_CINF);
87 M_ASN1_I2D_len(a->sig_alg, i2d_X509_ALGOR);
88 M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING);
89
90 M_ASN1_I2D_seq_total();
91
92 M_ASN1_I2D_put(a->cert_info, i2d_X509_CINF);
93 M_ASN1_I2D_put(a->sig_alg, i2d_X509_ALGOR);
94 M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING);
95
96 M_ASN1_I2D_finish();
97 }
98
99X509 *d2i_X509(a,pp,length)
100X509 **a;
101unsigned char **pp;
102long length;
103 {
104 M_ASN1_D2I_vars(a,X509 *,X509_new);
105
106 M_ASN1_D2I_Init();
107 M_ASN1_D2I_start_sequence();
108 M_ASN1_D2I_get(ret->cert_info,d2i_X509_CINF);
109 M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);
110 M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);
111if (ret->name != NULL) Free(ret->name);
112ret->name=X509_NAME_oneline(ret->cert_info->subject,NULL,0);
113
114 M_ASN1_D2I_Finish(a,X509_free,ASN1_F_D2I_X509);
115 }
116
117X509 *X509_new()
118 {
119 X509 *ret=NULL;
120
121 M_ASN1_New_Malloc(ret,X509);
122 ret->references=1;
123 ret->valid=0;
124 ret->name=NULL;
125 M_ASN1_New(ret->cert_info,X509_CINF_new);
126 M_ASN1_New(ret->sig_alg,X509_ALGOR_new);
127 M_ASN1_New(ret->signature,ASN1_BIT_STRING_new);
128 return(ret);
129 M_ASN1_New_Error(ASN1_F_X509_NEW);
130 }
131
132void X509_free(a)
133X509 *a;
134 {
135 int i;
136
137 if (a == NULL) return;
138
139 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509);
140#ifdef REF_PRINT
141 REF_PRINT("X509",a);
142#endif
143 if (i > 0) return;
144#ifdef REF_CHECK
145 if (i < 0)
146 {
147 fprintf(stderr,"X509_free, bad reference count\n");
148 abort();
149 }
150#endif
151
152 X509_CINF_free(a->cert_info);
153 X509_ALGOR_free(a->sig_alg);
154 ASN1_BIT_STRING_free(a->signature);
155 if (a->name != NULL) Free(a->name);
156 Free((char *)a);
157 }
158
diff --git a/src/lib/libcrypto/bf/COPYRIGHT b/src/lib/libcrypto/bf/COPYRIGHT
new file mode 100644
index 0000000000..6857223506
--- /dev/null
+++ b/src/lib/libcrypto/bf/COPYRIGHT
@@ -0,0 +1,46 @@
1Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
2All rights reserved.
3
4This package is an Blowfish implementation written
5by Eric Young (eay@cryptsoft.com).
6
7This library is free for commercial and non-commercial use as long as
8the following conditions are aheared to. The following conditions
9apply to all code found in this distribution.
10
11Copyright remains Eric Young's, and as such any Copyright notices in
12the code are not to be removed.
13
14Redistribution and use in source and binary forms, with or without
15modification, are permitted provided that the following conditions
16are met:
171. Redistributions of source code must retain the copyright
18 notice, this list of conditions and the following disclaimer.
192. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
223. All advertising materials mentioning features or use of this software
23 must display the following acknowledgement:
24 This product includes software developed by Eric Young (eay@cryptsoft.com)
25
26THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
27ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36SUCH DAMAGE.
37
38The license and distribution terms for any publically available version or
39derivative of this code cannot be changed. i.e. this code cannot simply be
40copied and put under another distrubution license
41[including the GNU Public License.]
42
43The reason behind this being stated in this direct manner is past
44experience in code simply being copied and the attribution removed
45from it and then being distributed as part of other packages. This
46implementation was a non-trivial and unpaid effort.
diff --git a/src/lib/libcrypto/bf/INSTALL b/src/lib/libcrypto/bf/INSTALL
new file mode 100644
index 0000000000..3b25923532
--- /dev/null
+++ b/src/lib/libcrypto/bf/INSTALL
@@ -0,0 +1,14 @@
1This Eric Young's blowfish implementation, taken from his SSLeay library
2and made available as a separate library.
3
4The version number (0.7.2m) is the SSLeay version that this library was
5taken from.
6
7To build, just unpack and type make.
8If you are not using gcc, edit the Makefile.
9If you are compiling for an x86 box, try the assembler (it needs improving).
10There are also some compile time options that can improve performance,
11these are documented in the Makefile.
12
13eric 15-Apr-1997
14
diff --git a/src/lib/libcrypto/bf/README b/src/lib/libcrypto/bf/README
new file mode 100644
index 0000000000..f2712fd0e7
--- /dev/null
+++ b/src/lib/libcrypto/bf/README
@@ -0,0 +1,8 @@
1This is a quick packaging up of my blowfish code into a library.
2It has been lifted from SSLeay.
3The copyright notices seem a little harsh because I have not spent the
4time to rewrite the conditions from the normal SSLeay ones.
5
6Basically if you just want to play with the library, not a problem.
7
8eric 15-Apr-1997
diff --git a/src/lib/libcrypto/bf/VERSION b/src/lib/libcrypto/bf/VERSION
new file mode 100644
index 0000000000..be995855e4
--- /dev/null
+++ b/src/lib/libcrypto/bf/VERSION
@@ -0,0 +1,6 @@
1The version numbers will follow my SSL implementation
2
30.7.2r - Some reasonable default compiler options from
4 Peter Gutman <pgut001@cs.auckland.ac.nz>
5
60.7.2m - the first release
diff --git a/src/lib/libcrypto/bf/asm/bf-586.pl b/src/lib/libcrypto/bf/asm/bf-586.pl
new file mode 100644
index 0000000000..5c7ab14ab0
--- /dev/null
+++ b/src/lib/libcrypto/bf/asm/bf-586.pl
@@ -0,0 +1,136 @@
1#!/usr/bin/perl
2
3push(@INC,"perlasm","../../perlasm");
4require "x86asm.pl";
5require "cbc.pl";
6
7&asm_init($ARGV[0],"bf-586.pl");
8
9$BF_ROUNDS=16;
10$BF_OFF=($BF_ROUNDS+2)*4;
11$L="edi";
12$R="esi";
13$P="ebp";
14$tmp1="eax";
15$tmp2="ebx";
16$tmp3="ecx";
17$tmp4="edx";
18
19&BF_encrypt("BF_encrypt",1);
20&BF_encrypt("BF_decrypt",0);
21&cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
22&asm_finish();
23
24sub BF_encrypt
25 {
26 local($name,$enc)=@_;
27
28 &function_begin_B($name,"");
29
30 &comment("");
31
32 &push("ebp");
33 &push("ebx");
34 &mov($tmp2,&wparam(0));
35 &mov($P,&wparam(1));
36 &push("esi");
37 &push("edi");
38
39 &comment("Load the 2 words");
40 &mov($L,&DWP(0,$tmp2,"",0));
41 &mov($R,&DWP(4,$tmp2,"",0));
42
43 &xor( $tmp1, $tmp1);
44
45 # encrypting part
46
47 if ($enc)
48 {
49 &mov($tmp2,&DWP(0,$P,"",0));
50 &xor( $tmp3, $tmp3);
51
52 &xor($L,$tmp2);
53 for ($i=0; $i<$BF_ROUNDS; $i+=2)
54 {
55 &comment("");
56 &comment("Round $i");
57 &BF_ENCRYPT($i+1,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
58
59 &comment("");
60 &comment("Round ".sprintf("%d",$i+1));
61 &BF_ENCRYPT($i+2,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
62 }
63 # &mov($tmp1,&wparam(0)); In last loop
64 &mov($tmp4,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
65 }
66 else
67 {
68 &mov($tmp2,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
69 &xor( $tmp3, $tmp3);
70
71 &xor($L,$tmp2);
72 for ($i=$BF_ROUNDS; $i>0; $i-=2)
73 {
74 &comment("");
75 &comment("Round $i");
76 &BF_ENCRYPT($i,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
77 &comment("");
78 &comment("Round ".sprintf("%d",$i-1));
79 &BF_ENCRYPT($i-1,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
80 }
81 # &mov($tmp1,&wparam(0)); In last loop
82 &mov($tmp4,&DWP(0,$P,"",0));
83 }
84
85 &xor($R,$tmp4);
86 &mov(&DWP(4,$tmp1,"",0),$L);
87
88 &mov(&DWP(0,$tmp1,"",0),$R);
89 &function_end($name);
90 }
91
92sub BF_ENCRYPT
93 {
94 local($i,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,$enc)=@_;
95
96 &mov( $tmp4, &DWP(&n2a($i*4),$P,"",0)); # for next round
97
98 &mov( $tmp2, $R);
99 &xor( $L, $tmp4);
100
101 &shr( $tmp2, 16);
102 &mov( $tmp4, $R);
103
104 &movb( &LB($tmp1), &HB($tmp2)); # A
105 &and( $tmp2, 0xff); # B
106
107 &movb( &LB($tmp3), &HB($tmp4)); # C
108 &and( $tmp4, 0xff); # D
109
110 &mov( $tmp1, &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
111 &mov( $tmp2, &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
112
113 &add( $tmp2, $tmp1);
114 &mov( $tmp1, &DWP(&n2a($BF_OFF+0x0800),$P,$tmp3,4));
115
116 &xor( $tmp2, $tmp1);
117 &mov( $tmp4, &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp4,4));
118
119 &add( $tmp2, $tmp4);
120 if (($enc && ($i != 16)) || ((!$enc) && ($i != 1)))
121 { &xor( $tmp1, $tmp1); }
122 else
123 {
124 &comment("Load parameter 0 ($i) enc=$enc");
125 &mov($tmp1,&wparam(0));
126 } # In last loop
127
128 &xor( $L, $tmp2);
129 # delay
130 }
131
132sub n2a
133 {
134 sprintf("%d",$_[0]);
135 }
136
diff --git a/src/lib/libcrypto/bf/bf_cbc.c b/src/lib/libcrypto/bf/bf_cbc.c
new file mode 100644
index 0000000000..e0fa9ad763
--- /dev/null
+++ b/src/lib/libcrypto/bf/bf_cbc.c
@@ -0,0 +1,148 @@
1/* crypto/bf/bf_cbc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "blowfish.h"
60#include "bf_locl.h"
61
62void BF_cbc_encrypt(in, out, length, ks, iv, encrypt)
63unsigned char *in;
64unsigned char *out;
65long length;
66BF_KEY *ks;
67unsigned char *iv;
68int encrypt;
69 {
70 register BF_LONG tin0,tin1;
71 register BF_LONG tout0,tout1,xor0,xor1;
72 register long l=length;
73 BF_LONG tin[2];
74
75 if (encrypt)
76 {
77 n2l(iv,tout0);
78 n2l(iv,tout1);
79 iv-=8;
80 for (l-=8; l>=0; l-=8)
81 {
82 n2l(in,tin0);
83 n2l(in,tin1);
84 tin0^=tout0;
85 tin1^=tout1;
86 tin[0]=tin0;
87 tin[1]=tin1;
88 BF_encrypt(tin,ks);
89 tout0=tin[0];
90 tout1=tin[1];
91 l2n(tout0,out);
92 l2n(tout1,out);
93 }
94 if (l != -8)
95 {
96 n2ln(in,tin0,tin1,l+8);
97 tin0^=tout0;
98 tin1^=tout1;
99 tin[0]=tin0;
100 tin[1]=tin1;
101 BF_encrypt(tin,ks);
102 tout0=tin[0];
103 tout1=tin[1];
104 l2n(tout0,out);
105 l2n(tout1,out);
106 }
107 l2n(tout0,iv);
108 l2n(tout1,iv);
109 }
110 else
111 {
112 n2l(iv,xor0);
113 n2l(iv,xor1);
114 iv-=8;
115 for (l-=8; l>=0; l-=8)
116 {
117 n2l(in,tin0);
118 n2l(in,tin1);
119 tin[0]=tin0;
120 tin[1]=tin1;
121 BF_decrypt(tin,ks);
122 tout0=tin[0]^xor0;
123 tout1=tin[1]^xor1;
124 l2n(tout0,out);
125 l2n(tout1,out);
126 xor0=tin0;
127 xor1=tin1;
128 }
129 if (l != -8)
130 {
131 n2l(in,tin0);
132 n2l(in,tin1);
133 tin[0]=tin0;
134 tin[1]=tin1;
135 BF_decrypt(tin,ks);
136 tout0=tin[0]^xor0;
137 tout1=tin[1]^xor1;
138 l2nn(tout0,tout1,out,l+8);
139 xor0=tin0;
140 xor1=tin1;
141 }
142 l2n(xor0,iv);
143 l2n(xor1,iv);
144 }
145 tin0=tin1=tout0=tout1=xor0=xor1=0;
146 tin[0]=tin[1]=0;
147 }
148
diff --git a/src/lib/libcrypto/bf/bf_cfb64.c b/src/lib/libcrypto/bf/bf_cfb64.c
new file mode 100644
index 0000000000..f9c66e7ced
--- /dev/null
+++ b/src/lib/libcrypto/bf/bf_cfb64.c
@@ -0,0 +1,127 @@
1/* crypto/bf/bf_cfb64.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "blowfish.h"
60#include "bf_locl.h"
61
62/* The input and output encrypted as though 64bit cfb mode is being
63 * used. The extra state information to record how much of the
64 * 64bit block we have used is contained in *num;
65 */
66
67void BF_cfb64_encrypt(in, out, length, schedule, ivec, num, encrypt)
68unsigned char *in;
69unsigned char *out;
70long length;
71BF_KEY *schedule;
72unsigned char *ivec;
73int *num;
74int encrypt;
75 {
76 register BF_LONG v0,v1,t;
77 register int n= *num;
78 register long l=length;
79 BF_LONG ti[2];
80 unsigned char *iv,c,cc;
81
82 iv=(unsigned char *)ivec;
83 if (encrypt)
84 {
85 while (l--)
86 {
87 if (n == 0)
88 {
89 n2l(iv,v0); ti[0]=v0;
90 n2l(iv,v1); ti[1]=v1;
91 BF_encrypt((BF_LONG *)ti,schedule);
92 iv=(unsigned char *)ivec;
93 t=ti[0]; l2n(t,iv);
94 t=ti[1]; l2n(t,iv);
95 iv=(unsigned char *)ivec;
96 }
97 c= *(in++)^iv[n];
98 *(out++)=c;
99 iv[n]=c;
100 n=(n+1)&0x07;
101 }
102 }
103 else
104 {
105 while (l--)
106 {
107 if (n == 0)
108 {
109 n2l(iv,v0); ti[0]=v0;
110 n2l(iv,v1); ti[1]=v1;
111 BF_encrypt((BF_LONG *)ti,schedule);
112 iv=(unsigned char *)ivec;
113 t=ti[0]; l2n(t,iv);
114 t=ti[1]; l2n(t,iv);
115 iv=(unsigned char *)ivec;
116 }
117 cc= *(in++);
118 c=iv[n];
119 iv[n]=cc;
120 *(out++)=c^cc;
121 n=(n+1)&0x07;
122 }
123 }
124 v0=v1=ti[0]=ti[1]=t=c=cc=0;
125 *num=n;
126 }
127
diff --git a/src/lib/libcrypto/bf/bf_ecb.c b/src/lib/libcrypto/bf/bf_ecb.c
new file mode 100644
index 0000000000..6d16360bd9
--- /dev/null
+++ b/src/lib/libcrypto/bf/bf_ecb.c
@@ -0,0 +1,98 @@
1/* crypto/bf/bf_ecb.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "blowfish.h"
60#include "bf_locl.h"
61
62/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
63 * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
64 * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
65 */
66
67char *BF_version="BlowFish part of SSLeay 0.9.0b 29-Jun-1998";
68
69char *BF_options()
70 {
71#ifdef BF_PTR
72 return("blowfish(ptr)");
73#elif defined(BF_PTR2)
74 return("blowfish(ptr2)");
75#else
76 return("blowfish(idx)");
77#endif
78 }
79
80void BF_ecb_encrypt(in, out, ks, encrypt)
81unsigned char *in;
82unsigned char *out;
83BF_KEY *ks;
84int encrypt;
85 {
86 BF_LONG l,d[2];
87
88 n2l(in,l); d[0]=l;
89 n2l(in,l); d[1]=l;
90 if (encrypt)
91 BF_encrypt(d,ks);
92 else
93 BF_decrypt(d,ks);
94 l=d[0]; l2n(l,out);
95 l=d[1]; l2n(l,out);
96 l=d[0]=d[1]=0;
97 }
98
diff --git a/src/lib/libcrypto/bf/bf_enc.c b/src/lib/libcrypto/bf/bf_enc.c
new file mode 100644
index 0000000000..66a8604c59
--- /dev/null
+++ b/src/lib/libcrypto/bf/bf_enc.c
@@ -0,0 +1,241 @@
1/* crypto/bf/bf_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "blowfish.h"
60#include "bf_locl.h"
61
62/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
63 * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
64 * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
65 */
66
67#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
68If you set BF_ROUNDS to some value other than 16 or 20, you will have
69to modify the code.
70#endif
71
72void BF_encrypt(data,key)
73BF_LONG *data;
74BF_KEY *key;
75 {
76 register BF_LONG l,r,*p,*s;
77
78 p=key->P;
79 s= &(key->S[0]);
80 l=data[0];
81 r=data[1];
82
83 l^=p[0];
84 BF_ENC(r,l,s,p[ 1]);
85 BF_ENC(l,r,s,p[ 2]);
86 BF_ENC(r,l,s,p[ 3]);
87 BF_ENC(l,r,s,p[ 4]);
88 BF_ENC(r,l,s,p[ 5]);
89 BF_ENC(l,r,s,p[ 6]);
90 BF_ENC(r,l,s,p[ 7]);
91 BF_ENC(l,r,s,p[ 8]);
92 BF_ENC(r,l,s,p[ 9]);
93 BF_ENC(l,r,s,p[10]);
94 BF_ENC(r,l,s,p[11]);
95 BF_ENC(l,r,s,p[12]);
96 BF_ENC(r,l,s,p[13]);
97 BF_ENC(l,r,s,p[14]);
98 BF_ENC(r,l,s,p[15]);
99 BF_ENC(l,r,s,p[16]);
100#if BF_ROUNDS == 20
101 BF_ENC(r,l,s,p[17]);
102 BF_ENC(l,r,s,p[18]);
103 BF_ENC(r,l,s,p[19]);
104 BF_ENC(l,r,s,p[20]);
105#endif
106 r^=p[BF_ROUNDS+1];
107
108 data[1]=l&0xffffffffL;
109 data[0]=r&0xffffffffL;
110 }
111
112#ifndef BF_DEFAULT_OPTIONS
113
114void BF_decrypt(data,key)
115BF_LONG *data;
116BF_KEY *key;
117 {
118 register BF_LONG l,r,*p,*s;
119
120 p=key->P;
121 s= &(key->S[0]);
122 l=data[0];
123 r=data[1];
124
125 l^=p[BF_ROUNDS+1];
126#if BF_ROUNDS == 20
127 BF_ENC(r,l,s,p[20]);
128 BF_ENC(l,r,s,p[19]);
129 BF_ENC(r,l,s,p[18]);
130 BF_ENC(l,r,s,p[17]);
131#endif
132 BF_ENC(r,l,s,p[16]);
133 BF_ENC(l,r,s,p[15]);
134 BF_ENC(r,l,s,p[14]);
135 BF_ENC(l,r,s,p[13]);
136 BF_ENC(r,l,s,p[12]);
137 BF_ENC(l,r,s,p[11]);
138 BF_ENC(r,l,s,p[10]);
139 BF_ENC(l,r,s,p[ 9]);
140 BF_ENC(r,l,s,p[ 8]);
141 BF_ENC(l,r,s,p[ 7]);
142 BF_ENC(r,l,s,p[ 6]);
143 BF_ENC(l,r,s,p[ 5]);
144 BF_ENC(r,l,s,p[ 4]);
145 BF_ENC(l,r,s,p[ 3]);
146 BF_ENC(r,l,s,p[ 2]);
147 BF_ENC(l,r,s,p[ 1]);
148 r^=p[0];
149
150 data[1]=l&0xffffffffL;
151 data[0]=r&0xffffffffL;
152 }
153
154void BF_cbc_encrypt(in, out, length, ks, iv, encrypt)
155unsigned char *in;
156unsigned char *out;
157long length;
158BF_KEY *ks;
159unsigned char *iv;
160int encrypt;
161 {
162 register BF_LONG tin0,tin1;
163 register BF_LONG tout0,tout1,xor0,xor1;
164 register long l=length;
165 BF_LONG tin[2];
166
167 if (encrypt)
168 {
169 n2l(iv,tout0);
170 n2l(iv,tout1);
171 iv-=8;
172 for (l-=8; l>=0; l-=8)
173 {
174 n2l(in,tin0);
175 n2l(in,tin1);
176 tin0^=tout0;
177 tin1^=tout1;
178 tin[0]=tin0;
179 tin[1]=tin1;
180 BF_encrypt(tin,ks);
181 tout0=tin[0];
182 tout1=tin[1];
183 l2n(tout0,out);
184 l2n(tout1,out);
185 }
186 if (l != -8)
187 {
188 n2ln(in,tin0,tin1,l+8);
189 tin0^=tout0;
190 tin1^=tout1;
191 tin[0]=tin0;
192 tin[1]=tin1;
193 BF_encrypt(tin,ks);
194 tout0=tin[0];
195 tout1=tin[1];
196 l2n(tout0,out);
197 l2n(tout1,out);
198 }
199 l2n(tout0,iv);
200 l2n(tout1,iv);
201 }
202 else
203 {
204 n2l(iv,xor0);
205 n2l(iv,xor1);
206 iv-=8;
207 for (l-=8; l>=0; l-=8)
208 {
209 n2l(in,tin0);
210 n2l(in,tin1);
211 tin[0]=tin0;
212 tin[1]=tin1;
213 BF_decrypt(tin,ks);
214 tout0=tin[0]^xor0;
215 tout1=tin[1]^xor1;
216 l2n(tout0,out);
217 l2n(tout1,out);
218 xor0=tin0;
219 xor1=tin1;
220 }
221 if (l != -8)
222 {
223 n2l(in,tin0);
224 n2l(in,tin1);
225 tin[0]=tin0;
226 tin[1]=tin1;
227 BF_decrypt(tin,ks);
228 tout0=tin[0]^xor0;
229 tout1=tin[1]^xor1;
230 l2nn(tout0,tout1,out,l+8);
231 xor0=tin0;
232 xor1=tin1;
233 }
234 l2n(xor0,iv);
235 l2n(xor1,iv);
236 }
237 tin0=tin1=tout0=tout1=xor0=xor1=0;
238 tin[0]=tin[1]=0;
239 }
240
241#endif
diff --git a/src/lib/libcrypto/bf/bf_ofb64.c b/src/lib/libcrypto/bf/bf_ofb64.c
new file mode 100644
index 0000000000..5d844ac760
--- /dev/null
+++ b/src/lib/libcrypto/bf/bf_ofb64.c
@@ -0,0 +1,115 @@
1/* crypto/bf/bf_ofb64.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "blowfish.h"
60#include "bf_locl.h"
61
62/* The input and output encrypted as though 64bit ofb mode is being
63 * used. The extra state information to record how much of the
64 * 64bit block we have used is contained in *num;
65 */
66void BF_ofb64_encrypt(in, out, length, schedule, ivec, num)
67unsigned char *in;
68unsigned char *out;
69long length;
70BF_KEY *schedule;
71unsigned char *ivec;
72int *num;
73 {
74 register BF_LONG v0,v1,t;
75 register int n= *num;
76 register long l=length;
77 unsigned char d[8];
78 register char *dp;
79 BF_LONG ti[2];
80 unsigned char *iv;
81 int save=0;
82
83 iv=(unsigned char *)ivec;
84 n2l(iv,v0);
85 n2l(iv,v1);
86 ti[0]=v0;
87 ti[1]=v1;
88 dp=(char *)d;
89 l2n(v0,dp);
90 l2n(v1,dp);
91 while (l--)
92 {
93 if (n == 0)
94 {
95 BF_encrypt((BF_LONG *)ti,schedule);
96 dp=(char *)d;
97 t=ti[0]; l2n(t,dp);
98 t=ti[1]; l2n(t,dp);
99 save++;
100 }
101 *(out++)= *(in++)^d[n];
102 n=(n+1)&0x07;
103 }
104 if (save)
105 {
106 v0=ti[0];
107 v1=ti[1];
108 iv=(unsigned char *)ivec;
109 l2n(v0,iv);
110 l2n(v1,iv);
111 }
112 t=v0=v1=ti[0]=ti[1]=0;
113 *num=n;
114 }
115
diff --git a/src/lib/libcrypto/bf/bf_pi.h b/src/lib/libcrypto/bf/bf_pi.h
new file mode 100644
index 0000000000..417b935538
--- /dev/null
+++ b/src/lib/libcrypto/bf/bf_pi.h
@@ -0,0 +1,325 @@
1/* crypto/bf/bf_pi.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59static BF_KEY bf_init= {
60 {
61 0x243f6a88L, 0x85a308d3L, 0x13198a2eL, 0x03707344L,
62 0xa4093822L, 0x299f31d0L, 0x082efa98L, 0xec4e6c89L,
63 0x452821e6L, 0x38d01377L, 0xbe5466cfL, 0x34e90c6cL,
64 0xc0ac29b7L, 0xc97c50ddL, 0x3f84d5b5L, 0xb5470917L,
65 0x9216d5d9L, 0x8979fb1b
66 },{
67 0xd1310ba6L, 0x98dfb5acL, 0x2ffd72dbL, 0xd01adfb7L,
68 0xb8e1afedL, 0x6a267e96L, 0xba7c9045L, 0xf12c7f99L,
69 0x24a19947L, 0xb3916cf7L, 0x0801f2e2L, 0x858efc16L,
70 0x636920d8L, 0x71574e69L, 0xa458fea3L, 0xf4933d7eL,
71 0x0d95748fL, 0x728eb658L, 0x718bcd58L, 0x82154aeeL,
72 0x7b54a41dL, 0xc25a59b5L, 0x9c30d539L, 0x2af26013L,
73 0xc5d1b023L, 0x286085f0L, 0xca417918L, 0xb8db38efL,
74 0x8e79dcb0L, 0x603a180eL, 0x6c9e0e8bL, 0xb01e8a3eL,
75 0xd71577c1L, 0xbd314b27L, 0x78af2fdaL, 0x55605c60L,
76 0xe65525f3L, 0xaa55ab94L, 0x57489862L, 0x63e81440L,
77 0x55ca396aL, 0x2aab10b6L, 0xb4cc5c34L, 0x1141e8ceL,
78 0xa15486afL, 0x7c72e993L, 0xb3ee1411L, 0x636fbc2aL,
79 0x2ba9c55dL, 0x741831f6L, 0xce5c3e16L, 0x9b87931eL,
80 0xafd6ba33L, 0x6c24cf5cL, 0x7a325381L, 0x28958677L,
81 0x3b8f4898L, 0x6b4bb9afL, 0xc4bfe81bL, 0x66282193L,
82 0x61d809ccL, 0xfb21a991L, 0x487cac60L, 0x5dec8032L,
83 0xef845d5dL, 0xe98575b1L, 0xdc262302L, 0xeb651b88L,
84 0x23893e81L, 0xd396acc5L, 0x0f6d6ff3L, 0x83f44239L,
85 0x2e0b4482L, 0xa4842004L, 0x69c8f04aL, 0x9e1f9b5eL,
86 0x21c66842L, 0xf6e96c9aL, 0x670c9c61L, 0xabd388f0L,
87 0x6a51a0d2L, 0xd8542f68L, 0x960fa728L, 0xab5133a3L,
88 0x6eef0b6cL, 0x137a3be4L, 0xba3bf050L, 0x7efb2a98L,
89 0xa1f1651dL, 0x39af0176L, 0x66ca593eL, 0x82430e88L,
90 0x8cee8619L, 0x456f9fb4L, 0x7d84a5c3L, 0x3b8b5ebeL,
91 0xe06f75d8L, 0x85c12073L, 0x401a449fL, 0x56c16aa6L,
92 0x4ed3aa62L, 0x363f7706L, 0x1bfedf72L, 0x429b023dL,
93 0x37d0d724L, 0xd00a1248L, 0xdb0fead3L, 0x49f1c09bL,
94 0x075372c9L, 0x80991b7bL, 0x25d479d8L, 0xf6e8def7L,
95 0xe3fe501aL, 0xb6794c3bL, 0x976ce0bdL, 0x04c006baL,
96 0xc1a94fb6L, 0x409f60c4L, 0x5e5c9ec2L, 0x196a2463L,
97 0x68fb6fafL, 0x3e6c53b5L, 0x1339b2ebL, 0x3b52ec6fL,
98 0x6dfc511fL, 0x9b30952cL, 0xcc814544L, 0xaf5ebd09L,
99 0xbee3d004L, 0xde334afdL, 0x660f2807L, 0x192e4bb3L,
100 0xc0cba857L, 0x45c8740fL, 0xd20b5f39L, 0xb9d3fbdbL,
101 0x5579c0bdL, 0x1a60320aL, 0xd6a100c6L, 0x402c7279L,
102 0x679f25feL, 0xfb1fa3ccL, 0x8ea5e9f8L, 0xdb3222f8L,
103 0x3c7516dfL, 0xfd616b15L, 0x2f501ec8L, 0xad0552abL,
104 0x323db5faL, 0xfd238760L, 0x53317b48L, 0x3e00df82L,
105 0x9e5c57bbL, 0xca6f8ca0L, 0x1a87562eL, 0xdf1769dbL,
106 0xd542a8f6L, 0x287effc3L, 0xac6732c6L, 0x8c4f5573L,
107 0x695b27b0L, 0xbbca58c8L, 0xe1ffa35dL, 0xb8f011a0L,
108 0x10fa3d98L, 0xfd2183b8L, 0x4afcb56cL, 0x2dd1d35bL,
109 0x9a53e479L, 0xb6f84565L, 0xd28e49bcL, 0x4bfb9790L,
110 0xe1ddf2daL, 0xa4cb7e33L, 0x62fb1341L, 0xcee4c6e8L,
111 0xef20cadaL, 0x36774c01L, 0xd07e9efeL, 0x2bf11fb4L,
112 0x95dbda4dL, 0xae909198L, 0xeaad8e71L, 0x6b93d5a0L,
113 0xd08ed1d0L, 0xafc725e0L, 0x8e3c5b2fL, 0x8e7594b7L,
114 0x8ff6e2fbL, 0xf2122b64L, 0x8888b812L, 0x900df01cL,
115 0x4fad5ea0L, 0x688fc31cL, 0xd1cff191L, 0xb3a8c1adL,
116 0x2f2f2218L, 0xbe0e1777L, 0xea752dfeL, 0x8b021fa1L,
117 0xe5a0cc0fL, 0xb56f74e8L, 0x18acf3d6L, 0xce89e299L,
118 0xb4a84fe0L, 0xfd13e0b7L, 0x7cc43b81L, 0xd2ada8d9L,
119 0x165fa266L, 0x80957705L, 0x93cc7314L, 0x211a1477L,
120 0xe6ad2065L, 0x77b5fa86L, 0xc75442f5L, 0xfb9d35cfL,
121 0xebcdaf0cL, 0x7b3e89a0L, 0xd6411bd3L, 0xae1e7e49L,
122 0x00250e2dL, 0x2071b35eL, 0x226800bbL, 0x57b8e0afL,
123 0x2464369bL, 0xf009b91eL, 0x5563911dL, 0x59dfa6aaL,
124 0x78c14389L, 0xd95a537fL, 0x207d5ba2L, 0x02e5b9c5L,
125 0x83260376L, 0x6295cfa9L, 0x11c81968L, 0x4e734a41L,
126 0xb3472dcaL, 0x7b14a94aL, 0x1b510052L, 0x9a532915L,
127 0xd60f573fL, 0xbc9bc6e4L, 0x2b60a476L, 0x81e67400L,
128 0x08ba6fb5L, 0x571be91fL, 0xf296ec6bL, 0x2a0dd915L,
129 0xb6636521L, 0xe7b9f9b6L, 0xff34052eL, 0xc5855664L,
130 0x53b02d5dL, 0xa99f8fa1L, 0x08ba4799L, 0x6e85076aL,
131 0x4b7a70e9L, 0xb5b32944L, 0xdb75092eL, 0xc4192623L,
132 0xad6ea6b0L, 0x49a7df7dL, 0x9cee60b8L, 0x8fedb266L,
133 0xecaa8c71L, 0x699a17ffL, 0x5664526cL, 0xc2b19ee1L,
134 0x193602a5L, 0x75094c29L, 0xa0591340L, 0xe4183a3eL,
135 0x3f54989aL, 0x5b429d65L, 0x6b8fe4d6L, 0x99f73fd6L,
136 0xa1d29c07L, 0xefe830f5L, 0x4d2d38e6L, 0xf0255dc1L,
137 0x4cdd2086L, 0x8470eb26L, 0x6382e9c6L, 0x021ecc5eL,
138 0x09686b3fL, 0x3ebaefc9L, 0x3c971814L, 0x6b6a70a1L,
139 0x687f3584L, 0x52a0e286L, 0xb79c5305L, 0xaa500737L,
140 0x3e07841cL, 0x7fdeae5cL, 0x8e7d44ecL, 0x5716f2b8L,
141 0xb03ada37L, 0xf0500c0dL, 0xf01c1f04L, 0x0200b3ffL,
142 0xae0cf51aL, 0x3cb574b2L, 0x25837a58L, 0xdc0921bdL,
143 0xd19113f9L, 0x7ca92ff6L, 0x94324773L, 0x22f54701L,
144 0x3ae5e581L, 0x37c2dadcL, 0xc8b57634L, 0x9af3dda7L,
145 0xa9446146L, 0x0fd0030eL, 0xecc8c73eL, 0xa4751e41L,
146 0xe238cd99L, 0x3bea0e2fL, 0x3280bba1L, 0x183eb331L,
147 0x4e548b38L, 0x4f6db908L, 0x6f420d03L, 0xf60a04bfL,
148 0x2cb81290L, 0x24977c79L, 0x5679b072L, 0xbcaf89afL,
149 0xde9a771fL, 0xd9930810L, 0xb38bae12L, 0xdccf3f2eL,
150 0x5512721fL, 0x2e6b7124L, 0x501adde6L, 0x9f84cd87L,
151 0x7a584718L, 0x7408da17L, 0xbc9f9abcL, 0xe94b7d8cL,
152 0xec7aec3aL, 0xdb851dfaL, 0x63094366L, 0xc464c3d2L,
153 0xef1c1847L, 0x3215d908L, 0xdd433b37L, 0x24c2ba16L,
154 0x12a14d43L, 0x2a65c451L, 0x50940002L, 0x133ae4ddL,
155 0x71dff89eL, 0x10314e55L, 0x81ac77d6L, 0x5f11199bL,
156 0x043556f1L, 0xd7a3c76bL, 0x3c11183bL, 0x5924a509L,
157 0xf28fe6edL, 0x97f1fbfaL, 0x9ebabf2cL, 0x1e153c6eL,
158 0x86e34570L, 0xeae96fb1L, 0x860e5e0aL, 0x5a3e2ab3L,
159 0x771fe71cL, 0x4e3d06faL, 0x2965dcb9L, 0x99e71d0fL,
160 0x803e89d6L, 0x5266c825L, 0x2e4cc978L, 0x9c10b36aL,
161 0xc6150ebaL, 0x94e2ea78L, 0xa5fc3c53L, 0x1e0a2df4L,
162 0xf2f74ea7L, 0x361d2b3dL, 0x1939260fL, 0x19c27960L,
163 0x5223a708L, 0xf71312b6L, 0xebadfe6eL, 0xeac31f66L,
164 0xe3bc4595L, 0xa67bc883L, 0xb17f37d1L, 0x018cff28L,
165 0xc332ddefL, 0xbe6c5aa5L, 0x65582185L, 0x68ab9802L,
166 0xeecea50fL, 0xdb2f953bL, 0x2aef7dadL, 0x5b6e2f84L,
167 0x1521b628L, 0x29076170L, 0xecdd4775L, 0x619f1510L,
168 0x13cca830L, 0xeb61bd96L, 0x0334fe1eL, 0xaa0363cfL,
169 0xb5735c90L, 0x4c70a239L, 0xd59e9e0bL, 0xcbaade14L,
170 0xeecc86bcL, 0x60622ca7L, 0x9cab5cabL, 0xb2f3846eL,
171 0x648b1eafL, 0x19bdf0caL, 0xa02369b9L, 0x655abb50L,
172 0x40685a32L, 0x3c2ab4b3L, 0x319ee9d5L, 0xc021b8f7L,
173 0x9b540b19L, 0x875fa099L, 0x95f7997eL, 0x623d7da8L,
174 0xf837889aL, 0x97e32d77L, 0x11ed935fL, 0x16681281L,
175 0x0e358829L, 0xc7e61fd6L, 0x96dedfa1L, 0x7858ba99L,
176 0x57f584a5L, 0x1b227263L, 0x9b83c3ffL, 0x1ac24696L,
177 0xcdb30aebL, 0x532e3054L, 0x8fd948e4L, 0x6dbc3128L,
178 0x58ebf2efL, 0x34c6ffeaL, 0xfe28ed61L, 0xee7c3c73L,
179 0x5d4a14d9L, 0xe864b7e3L, 0x42105d14L, 0x203e13e0L,
180 0x45eee2b6L, 0xa3aaabeaL, 0xdb6c4f15L, 0xfacb4fd0L,
181 0xc742f442L, 0xef6abbb5L, 0x654f3b1dL, 0x41cd2105L,
182 0xd81e799eL, 0x86854dc7L, 0xe44b476aL, 0x3d816250L,
183 0xcf62a1f2L, 0x5b8d2646L, 0xfc8883a0L, 0xc1c7b6a3L,
184 0x7f1524c3L, 0x69cb7492L, 0x47848a0bL, 0x5692b285L,
185 0x095bbf00L, 0xad19489dL, 0x1462b174L, 0x23820e00L,
186 0x58428d2aL, 0x0c55f5eaL, 0x1dadf43eL, 0x233f7061L,
187 0x3372f092L, 0x8d937e41L, 0xd65fecf1L, 0x6c223bdbL,
188 0x7cde3759L, 0xcbee7460L, 0x4085f2a7L, 0xce77326eL,
189 0xa6078084L, 0x19f8509eL, 0xe8efd855L, 0x61d99735L,
190 0xa969a7aaL, 0xc50c06c2L, 0x5a04abfcL, 0x800bcadcL,
191 0x9e447a2eL, 0xc3453484L, 0xfdd56705L, 0x0e1e9ec9L,
192 0xdb73dbd3L, 0x105588cdL, 0x675fda79L, 0xe3674340L,
193 0xc5c43465L, 0x713e38d8L, 0x3d28f89eL, 0xf16dff20L,
194 0x153e21e7L, 0x8fb03d4aL, 0xe6e39f2bL, 0xdb83adf7L,
195 0xe93d5a68L, 0x948140f7L, 0xf64c261cL, 0x94692934L,
196 0x411520f7L, 0x7602d4f7L, 0xbcf46b2eL, 0xd4a20068L,
197 0xd4082471L, 0x3320f46aL, 0x43b7d4b7L, 0x500061afL,
198 0x1e39f62eL, 0x97244546L, 0x14214f74L, 0xbf8b8840L,
199 0x4d95fc1dL, 0x96b591afL, 0x70f4ddd3L, 0x66a02f45L,
200 0xbfbc09ecL, 0x03bd9785L, 0x7fac6dd0L, 0x31cb8504L,
201 0x96eb27b3L, 0x55fd3941L, 0xda2547e6L, 0xabca0a9aL,
202 0x28507825L, 0x530429f4L, 0x0a2c86daL, 0xe9b66dfbL,
203 0x68dc1462L, 0xd7486900L, 0x680ec0a4L, 0x27a18deeL,
204 0x4f3ffea2L, 0xe887ad8cL, 0xb58ce006L, 0x7af4d6b6L,
205 0xaace1e7cL, 0xd3375fecL, 0xce78a399L, 0x406b2a42L,
206 0x20fe9e35L, 0xd9f385b9L, 0xee39d7abL, 0x3b124e8bL,
207 0x1dc9faf7L, 0x4b6d1856L, 0x26a36631L, 0xeae397b2L,
208 0x3a6efa74L, 0xdd5b4332L, 0x6841e7f7L, 0xca7820fbL,
209 0xfb0af54eL, 0xd8feb397L, 0x454056acL, 0xba489527L,
210 0x55533a3aL, 0x20838d87L, 0xfe6ba9b7L, 0xd096954bL,
211 0x55a867bcL, 0xa1159a58L, 0xcca92963L, 0x99e1db33L,
212 0xa62a4a56L, 0x3f3125f9L, 0x5ef47e1cL, 0x9029317cL,
213 0xfdf8e802L, 0x04272f70L, 0x80bb155cL, 0x05282ce3L,
214 0x95c11548L, 0xe4c66d22L, 0x48c1133fL, 0xc70f86dcL,
215 0x07f9c9eeL, 0x41041f0fL, 0x404779a4L, 0x5d886e17L,
216 0x325f51ebL, 0xd59bc0d1L, 0xf2bcc18fL, 0x41113564L,
217 0x257b7834L, 0x602a9c60L, 0xdff8e8a3L, 0x1f636c1bL,
218 0x0e12b4c2L, 0x02e1329eL, 0xaf664fd1L, 0xcad18115L,
219 0x6b2395e0L, 0x333e92e1L, 0x3b240b62L, 0xeebeb922L,
220 0x85b2a20eL, 0xe6ba0d99L, 0xde720c8cL, 0x2da2f728L,
221 0xd0127845L, 0x95b794fdL, 0x647d0862L, 0xe7ccf5f0L,
222 0x5449a36fL, 0x877d48faL, 0xc39dfd27L, 0xf33e8d1eL,
223 0x0a476341L, 0x992eff74L, 0x3a6f6eabL, 0xf4f8fd37L,
224 0xa812dc60L, 0xa1ebddf8L, 0x991be14cL, 0xdb6e6b0dL,
225 0xc67b5510L, 0x6d672c37L, 0x2765d43bL, 0xdcd0e804L,
226 0xf1290dc7L, 0xcc00ffa3L, 0xb5390f92L, 0x690fed0bL,
227 0x667b9ffbL, 0xcedb7d9cL, 0xa091cf0bL, 0xd9155ea3L,
228 0xbb132f88L, 0x515bad24L, 0x7b9479bfL, 0x763bd6ebL,
229 0x37392eb3L, 0xcc115979L, 0x8026e297L, 0xf42e312dL,
230 0x6842ada7L, 0xc66a2b3bL, 0x12754cccL, 0x782ef11cL,
231 0x6a124237L, 0xb79251e7L, 0x06a1bbe6L, 0x4bfb6350L,
232 0x1a6b1018L, 0x11caedfaL, 0x3d25bdd8L, 0xe2e1c3c9L,
233 0x44421659L, 0x0a121386L, 0xd90cec6eL, 0xd5abea2aL,
234 0x64af674eL, 0xda86a85fL, 0xbebfe988L, 0x64e4c3feL,
235 0x9dbc8057L, 0xf0f7c086L, 0x60787bf8L, 0x6003604dL,
236 0xd1fd8346L, 0xf6381fb0L, 0x7745ae04L, 0xd736fcccL,
237 0x83426b33L, 0xf01eab71L, 0xb0804187L, 0x3c005e5fL,
238 0x77a057beL, 0xbde8ae24L, 0x55464299L, 0xbf582e61L,
239 0x4e58f48fL, 0xf2ddfda2L, 0xf474ef38L, 0x8789bdc2L,
240 0x5366f9c3L, 0xc8b38e74L, 0xb475f255L, 0x46fcd9b9L,
241 0x7aeb2661L, 0x8b1ddf84L, 0x846a0e79L, 0x915f95e2L,
242 0x466e598eL, 0x20b45770L, 0x8cd55591L, 0xc902de4cL,
243 0xb90bace1L, 0xbb8205d0L, 0x11a86248L, 0x7574a99eL,
244 0xb77f19b6L, 0xe0a9dc09L, 0x662d09a1L, 0xc4324633L,
245 0xe85a1f02L, 0x09f0be8cL, 0x4a99a025L, 0x1d6efe10L,
246 0x1ab93d1dL, 0x0ba5a4dfL, 0xa186f20fL, 0x2868f169L,
247 0xdcb7da83L, 0x573906feL, 0xa1e2ce9bL, 0x4fcd7f52L,
248 0x50115e01L, 0xa70683faL, 0xa002b5c4L, 0x0de6d027L,
249 0x9af88c27L, 0x773f8641L, 0xc3604c06L, 0x61a806b5L,
250 0xf0177a28L, 0xc0f586e0L, 0x006058aaL, 0x30dc7d62L,
251 0x11e69ed7L, 0x2338ea63L, 0x53c2dd94L, 0xc2c21634L,
252 0xbbcbee56L, 0x90bcb6deL, 0xebfc7da1L, 0xce591d76L,
253 0x6f05e409L, 0x4b7c0188L, 0x39720a3dL, 0x7c927c24L,
254 0x86e3725fL, 0x724d9db9L, 0x1ac15bb4L, 0xd39eb8fcL,
255 0xed545578L, 0x08fca5b5L, 0xd83d7cd3L, 0x4dad0fc4L,
256 0x1e50ef5eL, 0xb161e6f8L, 0xa28514d9L, 0x6c51133cL,
257 0x6fd5c7e7L, 0x56e14ec4L, 0x362abfceL, 0xddc6c837L,
258 0xd79a3234L, 0x92638212L, 0x670efa8eL, 0x406000e0L,
259 0x3a39ce37L, 0xd3faf5cfL, 0xabc27737L, 0x5ac52d1bL,
260 0x5cb0679eL, 0x4fa33742L, 0xd3822740L, 0x99bc9bbeL,
261 0xd5118e9dL, 0xbf0f7315L, 0xd62d1c7eL, 0xc700c47bL,
262 0xb78c1b6bL, 0x21a19045L, 0xb26eb1beL, 0x6a366eb4L,
263 0x5748ab2fL, 0xbc946e79L, 0xc6a376d2L, 0x6549c2c8L,
264 0x530ff8eeL, 0x468dde7dL, 0xd5730a1dL, 0x4cd04dc6L,
265 0x2939bbdbL, 0xa9ba4650L, 0xac9526e8L, 0xbe5ee304L,
266 0xa1fad5f0L, 0x6a2d519aL, 0x63ef8ce2L, 0x9a86ee22L,
267 0xc089c2b8L, 0x43242ef6L, 0xa51e03aaL, 0x9cf2d0a4L,
268 0x83c061baL, 0x9be96a4dL, 0x8fe51550L, 0xba645bd6L,
269 0x2826a2f9L, 0xa73a3ae1L, 0x4ba99586L, 0xef5562e9L,
270 0xc72fefd3L, 0xf752f7daL, 0x3f046f69L, 0x77fa0a59L,
271 0x80e4a915L, 0x87b08601L, 0x9b09e6adL, 0x3b3ee593L,
272 0xe990fd5aL, 0x9e34d797L, 0x2cf0b7d9L, 0x022b8b51L,
273 0x96d5ac3aL, 0x017da67dL, 0xd1cf3ed6L, 0x7c7d2d28L,
274 0x1f9f25cfL, 0xadf2b89bL, 0x5ad6b472L, 0x5a88f54cL,
275 0xe029ac71L, 0xe019a5e6L, 0x47b0acfdL, 0xed93fa9bL,
276 0xe8d3c48dL, 0x283b57ccL, 0xf8d56629L, 0x79132e28L,
277 0x785f0191L, 0xed756055L, 0xf7960e44L, 0xe3d35e8cL,
278 0x15056dd4L, 0x88f46dbaL, 0x03a16125L, 0x0564f0bdL,
279 0xc3eb9e15L, 0x3c9057a2L, 0x97271aecL, 0xa93a072aL,
280 0x1b3f6d9bL, 0x1e6321f5L, 0xf59c66fbL, 0x26dcf319L,
281 0x7533d928L, 0xb155fdf5L, 0x03563482L, 0x8aba3cbbL,
282 0x28517711L, 0xc20ad9f8L, 0xabcc5167L, 0xccad925fL,
283 0x4de81751L, 0x3830dc8eL, 0x379d5862L, 0x9320f991L,
284 0xea7a90c2L, 0xfb3e7bceL, 0x5121ce64L, 0x774fbe32L,
285 0xa8b6e37eL, 0xc3293d46L, 0x48de5369L, 0x6413e680L,
286 0xa2ae0810L, 0xdd6db224L, 0x69852dfdL, 0x09072166L,
287 0xb39a460aL, 0x6445c0ddL, 0x586cdecfL, 0x1c20c8aeL,
288 0x5bbef7ddL, 0x1b588d40L, 0xccd2017fL, 0x6bb4e3bbL,
289 0xdda26a7eL, 0x3a59ff45L, 0x3e350a44L, 0xbcb4cdd5L,
290 0x72eacea8L, 0xfa6484bbL, 0x8d6612aeL, 0xbf3c6f47L,
291 0xd29be463L, 0x542f5d9eL, 0xaec2771bL, 0xf64e6370L,
292 0x740e0d8dL, 0xe75b1357L, 0xf8721671L, 0xaf537d5dL,
293 0x4040cb08L, 0x4eb4e2ccL, 0x34d2466aL, 0x0115af84L,
294 0xe1b00428L, 0x95983a1dL, 0x06b89fb4L, 0xce6ea048L,
295 0x6f3f3b82L, 0x3520ab82L, 0x011a1d4bL, 0x277227f8L,
296 0x611560b1L, 0xe7933fdcL, 0xbb3a792bL, 0x344525bdL,
297 0xa08839e1L, 0x51ce794bL, 0x2f32c9b7L, 0xa01fbac9L,
298 0xe01cc87eL, 0xbcc7d1f6L, 0xcf0111c3L, 0xa1e8aac7L,
299 0x1a908749L, 0xd44fbd9aL, 0xd0dadecbL, 0xd50ada38L,
300 0x0339c32aL, 0xc6913667L, 0x8df9317cL, 0xe0b12b4fL,
301 0xf79e59b7L, 0x43f5bb3aL, 0xf2d519ffL, 0x27d9459cL,
302 0xbf97222cL, 0x15e6fc2aL, 0x0f91fc71L, 0x9b941525L,
303 0xfae59361L, 0xceb69cebL, 0xc2a86459L, 0x12baa8d1L,
304 0xb6c1075eL, 0xe3056a0cL, 0x10d25065L, 0xcb03a442L,
305 0xe0ec6e0eL, 0x1698db3bL, 0x4c98a0beL, 0x3278e964L,
306 0x9f1f9532L, 0xe0d392dfL, 0xd3a0342bL, 0x8971f21eL,
307 0x1b0a7441L, 0x4ba3348cL, 0xc5be7120L, 0xc37632d8L,
308 0xdf359f8dL, 0x9b992f2eL, 0xe60b6f47L, 0x0fe3f11dL,
309 0xe54cda54L, 0x1edad891L, 0xce6279cfL, 0xcd3e7e6fL,
310 0x1618b166L, 0xfd2c1d05L, 0x848fd2c5L, 0xf6fb2299L,
311 0xf523f357L, 0xa6327623L, 0x93a83531L, 0x56cccd02L,
312 0xacf08162L, 0x5a75ebb5L, 0x6e163697L, 0x88d273ccL,
313 0xde966292L, 0x81b949d0L, 0x4c50901bL, 0x71c65614L,
314 0xe6c6c7bdL, 0x327a140aL, 0x45e1d006L, 0xc3f27b9aL,
315 0xc9aa53fdL, 0x62a80f00L, 0xbb25bfe2L, 0x35bdd2f6L,
316 0x71126905L, 0xb2040222L, 0xb6cbcf7cL, 0xcd769c2bL,
317 0x53113ec0L, 0x1640e3d3L, 0x38abbd60L, 0x2547adf0L,
318 0xba38209cL, 0xf746ce76L, 0x77afa1c5L, 0x20756060L,
319 0x85cbfe4eL, 0x8ae88dd8L, 0x7aaaf9b0L, 0x4cf9aa7eL,
320 0x1948c25cL, 0x02fb8a8cL, 0x01c36ae4L, 0xd6ebe1f9L,
321 0x90d4f869L, 0xa65cdea0L, 0x3f09252dL, 0xc208e69fL,
322 0xb74e6132L, 0xce77e25bL, 0x578fdfe3L, 0x3ac372e6L,
323 }
324 };
325
diff --git a/src/lib/libcrypto/bf/bf_skey.c b/src/lib/libcrypto/bf/bf_skey.c
new file mode 100644
index 0000000000..86574c0acc
--- /dev/null
+++ b/src/lib/libcrypto/bf/bf_skey.c
@@ -0,0 +1,119 @@
1/* crypto/bf/bf_skey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include "blowfish.h"
62#include "bf_locl.h"
63#include "bf_pi.h"
64
65void BF_set_key(key,len,data)
66BF_KEY *key;
67int len;
68unsigned char *data;
69 {
70 int i;
71 BF_LONG *p,ri,in[2];
72 unsigned char *d,*end;
73
74
75 memcpy((char *)key,(char *)&bf_init,sizeof(BF_KEY));
76 p=key->P;
77
78 if (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4;
79
80 d=data;
81 end= &(data[len]);
82 for (i=0; i<(BF_ROUNDS+2); i++)
83 {
84 ri= *(d++);
85 if (d >= end) d=data;
86
87 ri<<=8;
88 ri|= *(d++);
89 if (d >= end) d=data;
90
91 ri<<=8;
92 ri|= *(d++);
93 if (d >= end) d=data;
94
95 ri<<=8;
96 ri|= *(d++);
97 if (d >= end) d=data;
98
99 p[i]^=ri;
100 }
101
102 in[0]=0L;
103 in[1]=0L;
104 for (i=0; i<(BF_ROUNDS+2); i+=2)
105 {
106 BF_encrypt(in,key);
107 p[i ]=in[0];
108 p[i+1]=in[1];
109 }
110
111 p=key->S;
112 for (i=0; i<4*256; i+=2)
113 {
114 BF_encrypt(in,key);
115 p[i ]=in[0];
116 p[i+1]=in[1];
117 }
118 }
119
diff --git a/src/lib/libcrypto/bf/blowfish.h b/src/lib/libcrypto/bf/blowfish.h
new file mode 100644
index 0000000000..c4a8085a29
--- /dev/null
+++ b/src/lib/libcrypto/bf/blowfish.h
@@ -0,0 +1,116 @@
1/* crypto/bf/blowfish.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_BLOWFISH_H
60#define HEADER_BLOWFISH_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#define BF_ENCRYPT 1
67#define BF_DECRYPT 0
68
69/* If you make this 'unsigned int' the pointer variants will work on
70 * the Alpha, otherwise they will not. Strangly using the '8 byte'
71 * BF_LONG and the default 'non-pointer' inner loop is the best configuration
72 * for the Alpha */
73#define BF_LONG unsigned long
74
75#define BF_ROUNDS 16
76#define BF_BLOCK 8
77
78typedef struct bf_key_st
79 {
80 BF_LONG P[BF_ROUNDS+2];
81 BF_LONG S[4*256];
82 } BF_KEY;
83
84#ifndef NOPROTO
85
86void BF_set_key(BF_KEY *key, int len, unsigned char *data);
87void BF_ecb_encrypt(unsigned char *in,unsigned char *out,BF_KEY *key,
88 int enc);
89void BF_encrypt(BF_LONG *data,BF_KEY *key);
90void BF_decrypt(BF_LONG *data,BF_KEY *key);
91void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length,
92 BF_KEY *ks, unsigned char *iv, int enc);
93void BF_cfb64_encrypt(unsigned char *in, unsigned char *out, long length,
94 BF_KEY *schedule, unsigned char *ivec, int *num, int enc);
95void BF_ofb64_encrypt(unsigned char *in, unsigned char *out, long length,
96 BF_KEY *schedule, unsigned char *ivec, int *num);
97char *BF_options(void);
98
99#else
100
101void BF_set_key();
102void BF_ecb_encrypt();
103void BF_encrypt();
104void BF_decrypt();
105void BF_cbc_encrypt();
106void BF_cfb64_encrypt();
107void BF_ofb64_encrypt();
108char *BF_options();
109
110#endif
111
112#ifdef __cplusplus
113}
114#endif
115
116#endif
diff --git a/src/lib/libcrypto/bio/b_dump.c b/src/lib/libcrypto/bio/b_dump.c
new file mode 100644
index 0000000000..db84ad3d47
--- /dev/null
+++ b/src/lib/libcrypto/bio/b_dump.c
@@ -0,0 +1,125 @@
1/* crypto/bio/b_dump.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/*
60 * Stolen from tjh's ssl/ssl_trc.c stuff.
61 */
62
63#include <stdio.h>
64#include "cryptlib.h"
65#include "bio.h"
66
67#define TRUNCATE
68#define DUMP_WIDTH 16
69
70int BIO_dump(bio,s,len)
71BIO *bio;
72char *s;
73int len;
74{
75 int ret=0;
76 char buf[160+1],tmp[20];
77 int i,j,rows,trunc;
78 unsigned char ch;
79
80 trunc=0;
81
82#ifdef TRUNCATE
83 for(; (len > 0) && ((s[len-1] == ' ') || (s[len-1] == '\0')); len--)
84 trunc++;
85#endif
86
87 rows=(len/DUMP_WIDTH);
88 if ((rows*DUMP_WIDTH)<len)
89 rows++;
90 for(i=0;i<rows;i++) {
91 buf[0]='\0'; /* start with empty string */
92 sprintf(tmp,"%04x - ",i*DUMP_WIDTH);
93 strcpy(buf,tmp);
94 for(j=0;j<DUMP_WIDTH;j++) {
95 if (((i*DUMP_WIDTH)+j)>=len) {
96 strcat(buf," ");
97 } else {
98 ch=((unsigned char)*((char *)(s)+i*DUMP_WIDTH+j)) & 0xff;
99 sprintf(tmp,"%02x%c",ch,j==7?'-':' ');
100 strcat(buf,tmp);
101 }
102 }
103 strcat(buf," ");
104 for(j=0;j<DUMP_WIDTH;j++) {
105 if (((i*DUMP_WIDTH)+j)>=len)
106 break;
107 ch=((unsigned char)*((char *)(s)+i*DUMP_WIDTH+j)) & 0xff;
108 sprintf(tmp,"%c",((ch>=' ')&&(ch<='~'))?ch:'.');
109 strcat(buf,tmp);
110 }
111 strcat(buf,"\n");
112 /* if this is the last call then update the ddt_dump thing so that
113 * we will move the selection point in the debug window
114 */
115 ret+=BIO_write(bio,(char *)buf,strlen(buf));
116 }
117#ifdef TRUNCATE
118 if (trunc > 0) {
119 sprintf(buf,"%04x - <SPACES/NULS>\n",len+trunc);
120 ret+=BIO_write(bio,(char *)buf,strlen(buf));
121 }
122#endif
123 return(ret);
124}
125
diff --git a/src/lib/libcrypto/bio/b_print.c b/src/lib/libcrypto/bio/b_print.c
new file mode 100644
index 0000000000..cdadeb839a
--- /dev/null
+++ b/src/lib/libcrypto/bio/b_print.c
@@ -0,0 +1,92 @@
1/* crypto/bio/b_print.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/*
60 * Stolen from tjh's ssl/ssl_trc.c stuff.
61 */
62
63#include <stdio.h>
64#include "cryptlib.h"
65#include "bio.h"
66
67int BIO_printf ( VAR_PLIST( BIO *, bio ) )
68VAR_ALIST
69 {
70 VAR_BDEFN(args, BIO *, bio);
71 char *format;
72 int ret;
73 MS_STATIC char hugebuf[1024*2]; /* 10k in one chunk is the limit */
74
75 VAR_INIT(args, BIO *, bio);
76 VAR_ARG(args, char *, format);
77
78 hugebuf[0]='\0';
79
80/* no-one uses _doprnt anymore and it appears to be broken under SunOS 4.1.4 */
81#if 0 && defined(sun) && !defined(VAR_ANSI) /**/
82 _doprnt(hugebuf,format,args);
83#else /* !sun */
84 vsprintf(hugebuf,format,args);
85#endif /* sun */
86
87 ret=BIO_write(bio,hugebuf,strlen(hugebuf));
88
89 VAR_END( args );
90 return(ret);
91 }
92
diff --git a/src/lib/libcrypto/bio/b_sock.c b/src/lib/libcrypto/bio/b_sock.c
new file mode 100644
index 0000000000..a45909527c
--- /dev/null
+++ b/src/lib/libcrypto/bio/b_sock.c
@@ -0,0 +1,628 @@
1/* crypto/bio/b_sock.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef NO_SOCK
60
61#include <stdio.h>
62#include <stdlib.h>
63#include <errno.h>
64#define USE_SOCKETS
65#include "cryptlib.h"
66#include "bio.h"
67
68/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */
69
70#ifdef WIN16
71#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
72#else
73#define SOCKET_PROTOCOL IPPROTO_TCP
74#endif
75
76#ifdef SO_MAXCONN
77#define MAX_LISTEN SOMAXCONN
78#elif defined(SO_MAXCONN)
79#define MAX_LISTEN SO_MAXCONN
80#else
81#define MAX_LISTEN 32
82#endif
83
84#ifdef WINDOWS
85static int wsa_init_done=0;
86#endif
87
88static unsigned long BIO_ghbn_hits=0L;
89static unsigned long BIO_ghbn_miss=0L;
90
91#define GHBN_NUM 4
92static struct ghbn_cache_st
93 {
94 char name[129];
95 struct hostent *ent;
96 unsigned long order;
97 } ghbn_cache[GHBN_NUM];
98
99#ifndef NOPROTO
100static int get_ip(char *str,unsigned char *ip);
101static void ghbn_free(struct hostent *a);
102static struct hostent *ghbn_dup(struct hostent *a);
103#else
104static int get_ip();
105static void ghbn_free();
106static struct hostent *ghbn_dup();
107#endif
108
109int BIO_get_host_ip(str,ip)
110char *str;
111unsigned char *ip;
112 {
113 int i;
114 struct hostent *he;
115
116 i=get_ip(str,ip);
117 if (i > 0) return(1);
118 if (i < 0)
119 {
120 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS);
121 ERR_add_error_data(2,"host=",str);
122 return(0);
123 }
124 else
125 { /* do a gethostbyname */
126 if (!BIO_sock_init()) return(0);
127
128 he=BIO_gethostbyname(str);
129 if (he == NULL)
130 {
131 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP);
132 ERR_add_error_data(2,"host=",str);
133 return(0);
134 }
135
136 /* cast to short because of win16 winsock definition */
137 if ((short)he->h_addrtype != AF_INET)
138 {
139 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
140 ERR_add_error_data(2,"host=",str);
141 return(0);
142 }
143 for (i=0; i<4; i++)
144 ip[i]=he->h_addr_list[0][i];
145 }
146 return(1);
147 }
148
149int BIO_get_port(str,port_ptr)
150char *str;
151short *port_ptr;
152 {
153 int i;
154 struct servent *s;
155
156 if (str == NULL)
157 {
158 BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED);
159 return(0);
160 }
161 i=atoi(str);
162 if (i != 0)
163 *port_ptr=(unsigned short)i;
164 else
165 {
166 s=getservbyname(str,"tcp");
167 if (s == NULL)
168 {
169 if (strcmp(str,"http") == 0)
170 *port_ptr=80;
171 else if (strcmp(str,"telnet") == 0)
172 *port_ptr=23;
173 else if (strcmp(str,"socks") == 0)
174 *port_ptr=1080;
175 else if (strcmp(str,"https") == 0)
176 *port_ptr=443;
177 else if (strcmp(str,"ssl") == 0)
178 *port_ptr=443;
179 else if (strcmp(str,"ftp") == 0)
180 *port_ptr=21;
181 else if (strcmp(str,"gopher") == 0)
182 *port_ptr=70;
183#if 0
184 else if (strcmp(str,"wais") == 0)
185 *port_ptr=21;
186#endif
187 else
188 {
189 SYSerr(SYS_F_GETSERVBYNAME,get_last_socket_error());
190 ERR_add_error_data(3,"service='",str,"'");
191 return(0);
192 }
193 return(1);
194 }
195 *port_ptr=htons((unsigned short)s->s_port);
196 }
197 return(1);
198 }
199
200int BIO_sock_error(sock)
201int sock;
202 {
203 int j,i,size;
204
205 size=sizeof(int);
206
207 i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(char *)&j,&size);
208 if (i < 0)
209 return(1);
210 else
211 return(j);
212 }
213
214long BIO_ghbn_ctrl(cmd,iarg,parg)
215int cmd;
216int iarg;
217char *parg;
218 {
219 int i;
220 char **p;
221
222 switch (cmd)
223 {
224 case BIO_GHBN_CTRL_HITS:
225 return(BIO_ghbn_hits);
226 break;
227 case BIO_GHBN_CTRL_MISSES:
228 return(BIO_ghbn_miss);
229 break;
230 case BIO_GHBN_CTRL_CACHE_SIZE:
231 return(GHBN_NUM);
232 break;
233 case BIO_GHBN_CTRL_GET_ENTRY:
234 if ((iarg >= 0) && (iarg <GHBN_NUM) &&
235 (ghbn_cache[iarg].order > 0))
236 {
237 p=(char **)parg;
238 if (p == NULL) return(0);
239 *p=ghbn_cache[iarg].name;
240 ghbn_cache[iarg].name[128]='\0';
241 return(1);
242 }
243 return(0);
244 break;
245 case BIO_GHBN_CTRL_FLUSH:
246 for (i=0; i<GHBN_NUM; i++)
247 ghbn_cache[i].order=0;
248 break;
249 default:
250 return(0);
251 }
252 return(1);
253 }
254
255static struct hostent *ghbn_dup(a)
256struct hostent *a;
257 {
258 struct hostent *ret;
259 int i,j;
260
261 ret=(struct hostent *)malloc(sizeof(struct hostent));
262 if (ret == NULL) return(NULL);
263 memset(ret,0,sizeof(struct hostent));
264
265 for (i=0; a->h_aliases[i] != NULL; i++)
266 ;
267 i++;
268 ret->h_aliases=(char **)malloc(sizeof(char *)*i);
269 memset(ret->h_aliases,0,sizeof(char *)*i);
270 if (ret == NULL) goto err;
271
272 for (i=0; a->h_addr_list[i] != NULL; i++)
273 ;
274 i++;
275 ret->h_addr_list=(char **)malloc(sizeof(char *)*i);
276 memset(ret->h_addr_list,0,sizeof(char *)*i);
277 if (ret->h_addr_list == NULL) goto err;
278
279 j=strlen(a->h_name)+1;
280 if ((ret->h_name=malloc(j)) == NULL) goto err;
281 memcpy((char *)ret->h_name,a->h_name,j);
282 for (i=0; a->h_aliases[i] != NULL; i++)
283 {
284 j=strlen(a->h_aliases[i])+1;
285 if ((ret->h_aliases[i]=malloc(j)) == NULL) goto err;
286 memcpy(ret->h_aliases[i],a->h_aliases[i],j);
287 }
288 ret->h_length=a->h_length;
289 ret->h_addrtype=a->h_addrtype;
290 for (i=0; a->h_addr_list[i] != NULL; i++)
291 {
292 if ((ret->h_addr_list[i]=malloc(a->h_length)) == NULL)
293 goto err;
294 memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length);
295 }
296 return(ret);
297err:
298 if (ret != NULL)
299 ghbn_free(ret);
300 return(NULL);
301 }
302
303static void ghbn_free(a)
304struct hostent *a;
305 {
306 int i;
307
308 if (a->h_aliases != NULL)
309 {
310 for (i=0; a->h_aliases[i] != NULL; i++)
311 free(a->h_aliases[i]);
312 free(a->h_aliases);
313 }
314 if (a->h_addr_list != NULL)
315 {
316 for (i=0; a->h_addr_list[i] != NULL; i++)
317 free(a->h_addr_list[i]);
318 free(a->h_addr_list);
319 }
320 if (a->h_name != NULL) free((char *)a->h_name);
321 free(a);
322 }
323
324struct hostent *BIO_gethostbyname(name)
325char *name;
326 {
327 struct hostent *ret;
328 int i,lowi=0,j;
329 unsigned long low= (unsigned long)-1;
330
331/* return(gethostbyname(name)); */
332
333 CRYPTO_w_lock(CRYPTO_LOCK_BIO_GETHOSTBYNAME);
334 j=strlen(name);
335 if (j < 128)
336 {
337 for (i=0; i<GHBN_NUM; i++)
338 {
339 if (low > ghbn_cache[i].order)
340 {
341 low=ghbn_cache[i].order;
342 lowi=i;
343 }
344 if (ghbn_cache[i].order > 0)
345 {
346 if (strncmp(name,ghbn_cache[i].name,128) == 0)
347 break;
348 }
349 }
350 }
351 else
352 i=GHBN_NUM;
353
354 if (i == GHBN_NUM) /* no hit*/
355 {
356 BIO_ghbn_miss++;
357 ret=gethostbyname(name);
358
359 if (ret == NULL) return(NULL);
360 if (j > 128) return(ret); /* too big to cache */
361
362 /* else add to cache */
363 if (ghbn_cache[lowi].ent != NULL)
364 ghbn_free(ghbn_cache[lowi].ent);
365
366 strncpy(ghbn_cache[lowi].name,name,128);
367 ghbn_cache[lowi].ent=ghbn_dup(ret);
368 ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits;
369 }
370 else
371 {
372 BIO_ghbn_hits++;
373 ret= ghbn_cache[i].ent;
374 ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits;
375 }
376 CRYPTO_w_unlock(CRYPTO_LOCK_BIO_GETHOSTBYNAME);
377 return(ret);
378 }
379
380int BIO_sock_init()
381 {
382#ifdef WINDOWS
383 static struct WSAData wsa_state;
384
385 if (!wsa_init_done)
386 {
387 int err;
388
389#ifdef SIGINT
390 signal(SIGINT,(void (*)(int))BIO_sock_cleanup);
391#endif
392 wsa_init_done=1;
393 memset(&wsa_state,0,sizeof(wsa_state));
394 if (WSAStartup(0x0101,&wsa_state)!=0)
395 {
396 err=WSAGetLastError();
397 SYSerr(SYS_F_WSASTARTUP,err);
398 BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
399 return(-1);
400 }
401 }
402#endif /* WINDOWS */
403 return(1);
404 }
405
406void BIO_sock_cleanup()
407 {
408#ifdef WINDOWS
409 if (wsa_init_done)
410 {
411 wsa_init_done=0;
412 WSACancelBlockingCall();
413 WSACleanup();
414 }
415#endif
416 }
417
418int BIO_socket_ioctl(fd,type,arg)
419int fd;
420long type;
421unsigned long *arg;
422 {
423 int i;
424
425 i=ioctlsocket(fd,type,arg);
426 if (i < 0)
427 SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error());
428 return(i);
429 }
430
431/* The reason I have implemented this instead of using sscanf is because
432 * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
433static int get_ip(str,ip)
434char *str;
435unsigned char ip[4];
436 {
437 unsigned int tmp[4];
438 int num=0,c,ok=0;
439
440 tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
441
442 for (;;)
443 {
444 c= *(str++);
445 if ((c >= '0') && (c <= '9'))
446 {
447 ok=1;
448 tmp[num]=tmp[num]*10+c-'0';
449 if (tmp[num] > 255) return(-1);
450 }
451 else if (c == '.')
452 {
453 if (!ok) return(-1);
454 if (num == 3) break;
455 num++;
456 ok=0;
457 }
458 else if ((num == 3) && ok)
459 break;
460 else
461 return(0);
462 }
463 ip[0]=tmp[0];
464 ip[1]=tmp[1];
465 ip[2]=tmp[2];
466 ip[3]=tmp[3];
467 return(1);
468 }
469
470int BIO_get_accept_socket(host)
471char *host;
472 {
473 int ret=0;
474 struct sockaddr_in server;
475 int s= -1;
476 unsigned char ip[4];
477 short port;
478 char *str,*h,*p,*e;
479 unsigned long l;
480
481 if (!BIO_sock_init()) return(INVALID_SOCKET);
482
483 if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET);
484
485 h=p=NULL;
486 h=str;
487 for (e=str; *e; e++)
488 {
489 if (*e == ':')
490 {
491 p= &(e[1]);
492 *e='\0';
493 }
494 else if (*e == '/')
495 {
496 *e='\0';
497 break;
498 }
499 }
500
501 if (p == NULL)
502 {
503 p=h;
504 h="*";
505 }
506
507 if (!BIO_get_port(p,&port)) return(INVALID_SOCKET);
508
509 memset((char *)&server,0,sizeof(server));
510 server.sin_family=AF_INET;
511 server.sin_port=htons((unsigned short)port);
512
513 if (strcmp(h,"*") == 0)
514 server.sin_addr.s_addr=INADDR_ANY;
515 else
516 {
517 if (!BIO_get_host_ip(h,&(ip[0]))) return(INVALID_SOCKET);
518 l=(unsigned long)
519 ((unsigned long)ip[0]<<24L)|
520 ((unsigned long)ip[0]<<16L)|
521 ((unsigned long)ip[0]<< 8L)|
522 ((unsigned long)ip[0]);
523 server.sin_addr.s_addr=htonl(l);
524 }
525
526 s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
527 if (s == INVALID_SOCKET)
528 {
529 SYSerr(SYS_F_SOCKET,get_last_socket_error());
530 ERR_add_error_data(3,"port='",host,"'");
531 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET);
532 goto err;
533 }
534 if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
535 {
536 SYSerr(SYS_F_BIND,get_last_socket_error());
537 ERR_add_error_data(3,"port='",host,"'");
538 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET);
539 goto err;
540 }
541 if (listen(s,MAX_LISTEN) == -1)
542 {
543 SYSerr(SYS_F_BIND,get_last_socket_error());
544 ERR_add_error_data(3,"port='",host,"'");
545 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET);
546 goto err;
547 }
548 ret=1;
549err:
550 if (str != NULL) Free(str);
551 if ((ret == 0) && (s != INVALID_SOCKET))
552 {
553#ifdef WINDOWS
554 closesocket(s);
555#else
556 close(s);
557#endif
558 s= INVALID_SOCKET;
559 }
560 return(s);
561 }
562
563int BIO_accept(sock,addr)
564int sock;
565char **addr;
566 {
567 int ret=INVALID_SOCKET;
568 static struct sockaddr_in from;
569 unsigned long l;
570 short port;
571 int len;
572 char *p;
573
574 memset((char *)&from,0,sizeof(from));
575 len=sizeof(from);
576 ret=accept(sock,(struct sockaddr *)&from,&len);
577 if (ret == INVALID_SOCKET)
578 {
579 SYSerr(SYS_F_ACCEPT,get_last_socket_error());
580 BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR);
581 goto end;
582 }
583
584 if (addr == NULL) goto end;
585
586 l=ntohl(from.sin_addr.s_addr);
587 port=ntohs(from.sin_port);
588 if (*addr == NULL)
589 {
590 if ((p=Malloc(24)) == NULL)
591 {
592 BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
593 goto end;
594 }
595 *addr=p;
596 }
597 sprintf(*addr,"%d.%d.%d.%d:%d",
598 (unsigned char)(l>>24L)&0xff,
599 (unsigned char)(l>>16L)&0xff,
600 (unsigned char)(l>> 8L)&0xff,
601 (unsigned char)(l )&0xff,
602 port);
603end:
604 return(ret);
605 }
606
607int BIO_set_tcp_ndelay(s,on)
608int s;
609int on;
610 {
611 int ret=0;
612#if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
613 int opt;
614
615#ifdef SOL_TCP
616 opt=SOL_TCP;
617#else
618#ifdef IPPROTO_TCP
619 opt=IPPROTO_TCP;
620#endif
621#endif
622
623 ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on));
624#endif
625 return(ret == 0);
626 }
627#endif
628
diff --git a/src/lib/libcrypto/bio/bf_buff.c b/src/lib/libcrypto/bio/bf_buff.c
new file mode 100644
index 0000000000..7912b88473
--- /dev/null
+++ b/src/lib/libcrypto/bio/bf_buff.c
@@ -0,0 +1,512 @@
1/* crypto/bio/bf_buff.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include "bio.h"
63#include "evp.h"
64
65#ifndef NOPROTO
66static int buffer_write(BIO *h,char *buf,int num);
67static int buffer_read(BIO *h,char *buf,int size);
68static int buffer_puts(BIO *h,char *str);
69static int buffer_gets(BIO *h,char *str,int size);
70static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2);
71static int buffer_new(BIO *h);
72static int buffer_free(BIO *data);
73#else
74static int buffer_write();
75static int buffer_read();
76static int buffer_puts();
77static int buffer_gets();
78static long buffer_ctrl();
79static int buffer_new();
80static int buffer_free();
81#endif
82
83#define DEFAULT_BUFFER_SIZE 1024
84
85static BIO_METHOD methods_buffer=
86 {
87 BIO_TYPE_BUFFER,
88 "buffer",
89 buffer_write,
90 buffer_read,
91 buffer_puts,
92 buffer_gets,
93 buffer_ctrl,
94 buffer_new,
95 buffer_free,
96 };
97
98BIO_METHOD *BIO_f_buffer()
99 {
100 return(&methods_buffer);
101 }
102
103static int buffer_new(bi)
104BIO *bi;
105 {
106 BIO_F_BUFFER_CTX *ctx;
107
108 ctx=(BIO_F_BUFFER_CTX *)Malloc(sizeof(BIO_F_BUFFER_CTX));
109 if (ctx == NULL) return(0);
110 ctx->ibuf=(char *)Malloc(DEFAULT_BUFFER_SIZE);
111 if (ctx->ibuf == NULL) { Free(ctx); return(0); }
112 ctx->obuf=(char *)Malloc(DEFAULT_BUFFER_SIZE);
113 if (ctx->obuf == NULL) { Free(ctx->ibuf); Free(ctx); return(0); }
114 ctx->ibuf_size=DEFAULT_BUFFER_SIZE;
115 ctx->obuf_size=DEFAULT_BUFFER_SIZE;
116 ctx->ibuf_len=0;
117 ctx->ibuf_off=0;
118 ctx->obuf_len=0;
119 ctx->obuf_off=0;
120
121 bi->init=1;
122 bi->ptr=(char *)ctx;
123 bi->flags=0;
124 return(1);
125 }
126
127static int buffer_free(a)
128BIO *a;
129 {
130 BIO_F_BUFFER_CTX *b;
131
132 if (a == NULL) return(0);
133 b=(BIO_F_BUFFER_CTX *)a->ptr;
134 if (b->ibuf != NULL) Free(b->ibuf);
135 if (b->obuf != NULL) Free(b->obuf);
136 Free(a->ptr);
137 a->ptr=NULL;
138 a->init=0;
139 a->flags=0;
140 return(1);
141 }
142
143static int buffer_read(b,out,outl)
144BIO *b;
145char *out;
146int outl;
147 {
148 int i,num=0;
149 BIO_F_BUFFER_CTX *ctx;
150
151 if (out == NULL) return(0);
152 ctx=(BIO_F_BUFFER_CTX *)b->ptr;
153
154 if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
155 num=0;
156 BIO_clear_retry_flags(b);
157
158start:
159 i=ctx->ibuf_len;
160 /* If there is stuff left over, grab it */
161 if (i != 0)
162 {
163 if (i > outl) i=outl;
164 memcpy(out,&(ctx->ibuf[ctx->ibuf_off]),i);
165 ctx->ibuf_off+=i;
166 ctx->ibuf_len-=i;
167 num+=i;
168 if (outl == i) return(num);
169 outl-=i;
170 out+=i;
171 }
172
173 /* We may have done a partial read. try to do more.
174 * We have nothing in the buffer.
175 * If we get an error and have read some data, just return it
176 * and let them retry to get the error again.
177 * copy direct to parent address space */
178 if (outl > ctx->ibuf_size)
179 {
180 for (;;)
181 {
182 i=BIO_read(b->next_bio,out,outl);
183 if (i <= 0)
184 {
185 BIO_copy_next_retry(b);
186 if (i < 0) return((num > 0)?num:i);
187 if (i == 0) return(num);
188 }
189 num+=i;
190 if (outl == i) return(num);
191 out+=i;
192 outl-=i;
193 }
194 }
195 /* else */
196
197 /* we are going to be doing some buffering */
198 i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
199 if (i <= 0)
200 {
201 BIO_copy_next_retry(b);
202 if (i < 0) return((num > 0)?num:i);
203 if (i == 0) return(num);
204 }
205 ctx->ibuf_off=0;
206 ctx->ibuf_len=i;
207
208 /* Lets re-read using ourselves :-) */
209 goto start;
210 }
211
212static int buffer_write(b,in,inl)
213BIO *b;
214char *in;
215int inl;
216 {
217 int i,num=0;
218 BIO_F_BUFFER_CTX *ctx;
219
220 if ((in == NULL) || (inl <= 0)) return(0);
221 ctx=(BIO_F_BUFFER_CTX *)b->ptr;
222 if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
223
224 BIO_clear_retry_flags(b);
225start:
226 i=ctx->obuf_size-(ctx->obuf_len+ctx->obuf_off);
227 /* add to buffer and return */
228 if (i >= inl)
229 {
230 memcpy(&(ctx->obuf[ctx->obuf_len]),in,inl);
231 ctx->obuf_len+=inl;
232 return(num+inl);
233 }
234 /* else */
235 /* stuff already in buffer, so add to it first, then flush */
236 if (ctx->obuf_len != 0)
237 {
238 if (i > 0) /* lets fill it up if we can */
239 {
240 memcpy(&(ctx->obuf[ctx->obuf_len]),in,i);
241 in+=i;
242 inl-=i;
243 num+=i;
244 ctx->obuf_len+=i;
245 }
246 /* we now have a full buffer needing flushing */
247 for (;;)
248 {
249 i=BIO_write(b->next_bio,&(ctx->obuf[ctx->obuf_off]),
250 ctx->obuf_len);
251 if (i <= 0)
252 {
253 BIO_copy_next_retry(b);
254
255 if (i < 0) return((num > 0)?num:i);
256 if (i == 0) return(num);
257 }
258 ctx->obuf_off+=i;
259 ctx->obuf_len-=i;
260 if (ctx->obuf_len == 0) break;
261 }
262 }
263 /* we only get here if the buffer has been flushed and we
264 * still have stuff to write */
265 ctx->obuf_off=0;
266
267 /* we now have inl bytes to write */
268 while (inl >= ctx->obuf_size)
269 {
270 i=BIO_write(b->next_bio,in,inl);
271 if (i <= 0)
272 {
273 BIO_copy_next_retry(b);
274 if (i < 0) return((num > 0)?num:i);
275 if (i == 0) return(num);
276 }
277 num+=i;
278 in+=i;
279 inl-=i;
280 if (inl == 0) return(num);
281 }
282
283 /* copy the rest into the buffer since we have only a small
284 * amount left */
285 goto start;
286 }
287
288static long buffer_ctrl(b,cmd,num,ptr)
289BIO *b;
290int cmd;
291long num;
292char *ptr;
293 {
294 BIO *dbio;
295 BIO_F_BUFFER_CTX *ctx;
296 long ret=1;
297 char *p1,*p2;
298 int r,i,*ip;
299 int ibs,obs;
300
301 ctx=(BIO_F_BUFFER_CTX *)b->ptr;
302
303 switch (cmd)
304 {
305 case BIO_CTRL_RESET:
306 ctx->ibuf_off=0;
307 ctx->ibuf_len=0;
308 ctx->obuf_off=0;
309 ctx->obuf_len=0;
310 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
311 break;
312 case BIO_CTRL_INFO:
313 ret=(long)ctx->obuf_len;
314 break;
315 case BIO_C_GET_BUFF_NUM_LINES:
316 ret=0;
317 p1=ctx->ibuf;
318 for (i=ctx->ibuf_off; i<ctx->ibuf_len; i++)
319 {
320 if (p1[i] == '\n') ret++;
321 }
322 break;
323 case BIO_CTRL_WPENDING:
324 ret=(long)ctx->obuf_len;
325 if (ret == 0)
326 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
327 break;
328 case BIO_CTRL_PENDING:
329 ret=(long)ctx->ibuf_len;
330 if (ret == 0)
331 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
332 break;
333 case BIO_C_SET_BUFF_READ_DATA:
334 if (num > ctx->ibuf_size)
335 {
336 p1=Malloc((int)num);
337 if (p1 == NULL) goto malloc_error;
338 if (ctx->ibuf != NULL) Free(ctx->ibuf);
339 ctx->ibuf=p1;
340 }
341 ctx->ibuf_off=0;
342 ctx->ibuf_len=(int)num;
343 memcpy(ctx->ibuf,ptr,(int)num);
344 ret=1;
345 break;
346 case BIO_C_SET_BUFF_SIZE:
347 if (ptr != NULL)
348 {
349 ip=(int *)ptr;
350 if (*ip == 0)
351 {
352 ibs=(int)num;
353 obs=ctx->obuf_size;
354 }
355 else /* if (*ip == 1) */
356 {
357 ibs=ctx->ibuf_size;
358 obs=(int)num;
359 }
360 }
361 else
362 {
363 ibs=(int)num;
364 obs=(int)num;
365 }
366 p1=ctx->ibuf;
367 p2=ctx->obuf;
368 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
369 {
370 p1=(char *)Malloc((int)num);
371 if (p1 == NULL) goto malloc_error;
372 }
373 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size))
374 {
375 p2=(char *)Malloc((int)num);
376 if (p2 == NULL)
377 {
378 if (p1 != ctx->ibuf) Free(p1);
379 goto malloc_error;
380 }
381 }
382 if (ctx->ibuf != p1)
383 {
384 Free(ctx->ibuf);
385 ctx->ibuf=p1;
386 ctx->ibuf_off=0;
387 ctx->ibuf_len=0;
388 ctx->ibuf_size=ibs;
389 }
390 if (ctx->obuf != p2)
391 {
392 Free(ctx->obuf);
393 ctx->obuf=p2;
394 ctx->obuf_off=0;
395 ctx->obuf_len=0;
396 ctx->obuf_size=obs;
397 }
398 break;
399 case BIO_C_DO_STATE_MACHINE:
400 BIO_clear_retry_flags(b);
401 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
402 BIO_copy_next_retry(b);
403 break;
404
405 case BIO_CTRL_FLUSH:
406 if (ctx->obuf_len <= 0)
407 {
408 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
409 break;
410 }
411
412 for (;;)
413 {
414 BIO_clear_retry_flags(b);
415 if (ctx->obuf_len > ctx->obuf_off)
416 {
417 r=BIO_write(b->next_bio,
418 &(ctx->obuf[ctx->obuf_off]),
419 ctx->obuf_len-ctx->obuf_off);
420#if 0
421fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_off,r);
422#endif
423 BIO_copy_next_retry(b);
424 if (r <= 0) return((long)r);
425 ctx->obuf_off+=r;
426 }
427 else
428 {
429 ctx->obuf_len=0;
430 ctx->obuf_off=0;
431 ret=1;
432 break;
433 }
434 }
435 break;
436 case BIO_CTRL_DUP:
437 dbio=(BIO *)ptr;
438 if ( !BIO_set_read_buffer_size(dbio,ctx->ibuf_size) ||
439 !BIO_set_write_buffer_size(dbio,ctx->obuf_size))
440 ret=0;
441 break;
442 default:
443 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
444 break;
445 }
446 return(ret);
447malloc_error:
448 BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE);
449 return(0);
450 }
451
452static int buffer_gets(b,buf,size)
453BIO *b;
454char *buf;
455int size;
456 {
457 BIO_F_BUFFER_CTX *ctx;
458 int num=0,i,flag;
459 char *p;
460
461 ctx=(BIO_F_BUFFER_CTX *)b->ptr;
462 size--; /* reserve space for a '\0' */
463 BIO_clear_retry_flags(b);
464
465 for (;;)
466 {
467 if (ctx->ibuf_len > 0)
468 {
469 p= &(ctx->ibuf[ctx->ibuf_off]);
470 flag=0;
471 for (i=0; (i<ctx->ibuf_len) && (i<size); i++)
472 {
473 *(buf++)=p[i];
474 if (p[i] == '\n')
475 {
476 flag=1;
477 i++;
478 break;
479 }
480 }
481 num+=i;
482 size-=i;
483 ctx->ibuf_len-=i;
484 ctx->ibuf_off+=i;
485 if ((flag) || (i == size))
486 {
487 *buf='\0';
488 return(num);
489 }
490 }
491 else /* read another chunk */
492 {
493 i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
494 if (i <= 0)
495 {
496 BIO_copy_next_retry(b);
497 if (i < 0) return((num > 0)?num:i);
498 if (i == 0) return(num);
499 }
500 ctx->ibuf_len=i;
501 ctx->ibuf_off=0;
502 }
503 }
504 }
505
506static int buffer_puts(b,str)
507BIO *b;
508char *str;
509 {
510 return(BIO_write(b,str,strlen(str)));
511 }
512
diff --git a/src/lib/libcrypto/bio/bf_nbio.c b/src/lib/libcrypto/bio/bf_nbio.c
new file mode 100644
index 0000000000..034b3024df
--- /dev/null
+++ b/src/lib/libcrypto/bio/bf_nbio.c
@@ -0,0 +1,268 @@
1/* crypto/bio/bf_nbio.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include "rand.h"
63#include "bio.h"
64#include "evp.h"
65
66/* BIO_put and BIO_get both add to the digest,
67 * BIO_gets returns the digest */
68
69#ifndef NOPROTO
70static int nbiof_write(BIO *h,char *buf,int num);
71static int nbiof_read(BIO *h,char *buf,int size);
72static int nbiof_puts(BIO *h,char *str);
73static int nbiof_gets(BIO *h,char *str,int size);
74static long nbiof_ctrl(BIO *h,int cmd,long arg1,char *arg2);
75static int nbiof_new(BIO *h);
76static int nbiof_free(BIO *data);
77#else
78static int nbiof_write();
79static int nbiof_read();
80static int nbiof_puts();
81static int nbiof_gets();
82static long nbiof_ctrl();
83static int nbiof_new();
84static int nbiof_free();
85#endif
86
87typedef struct nbio_test_st
88 {
89 /* only set if we sent a 'should retry' error */
90 int lrn;
91 int lwn;
92 } NBIO_TEST;
93
94static BIO_METHOD methods_nbiof=
95 {
96 BIO_TYPE_NBIO_TEST,
97 "non-blocking IO test filter",
98 nbiof_write,
99 nbiof_read,
100 nbiof_puts,
101 nbiof_gets,
102 nbiof_ctrl,
103 nbiof_new,
104 nbiof_free,
105 };
106
107BIO_METHOD *BIO_f_nbio_test()
108 {
109 return(&methods_nbiof);
110 }
111
112static int nbiof_new(bi)
113BIO *bi;
114 {
115 NBIO_TEST *nt;
116
117 nt=(NBIO_TEST *)Malloc(sizeof(NBIO_TEST));
118 nt->lrn= -1;
119 nt->lwn= -1;
120 bi->ptr=(char *)nt;
121 bi->init=1;
122 bi->flags=0;
123 return(1);
124 }
125
126static int nbiof_free(a)
127BIO *a;
128 {
129 if (a == NULL) return(0);
130 if (a->ptr != NULL)
131 Free(a->ptr);
132 a->ptr=NULL;
133 a->init=0;
134 a->flags=0;
135 return(1);
136 }
137
138static int nbiof_read(b,out,outl)
139BIO *b;
140char *out;
141int outl;
142 {
143 NBIO_TEST *nt;
144 int ret=0;
145#if 0
146 int num;
147 unsigned char n;
148#endif
149
150 if (out == NULL) return(0);
151 if (b->next_bio == NULL) return(0);
152 nt=(NBIO_TEST *)b->ptr;
153
154 BIO_clear_retry_flags(b);
155#if 0
156 RAND_bytes(&n,1);
157 num=(n&0x07);
158
159 if (outl > num) outl=num;
160
161 if (num == 0)
162 {
163 ret= -1;
164 BIO_set_retry_read(b);
165 }
166 else
167#endif
168 {
169 ret=BIO_read(b->next_bio,out,outl);
170 if (ret < 0)
171 BIO_copy_next_retry(b);
172 }
173 return(ret);
174 }
175
176static int nbiof_write(b,in,inl)
177BIO *b;
178char *in;
179int inl;
180 {
181 NBIO_TEST *nt;
182 int ret=0;
183 int num;
184 unsigned char n;
185
186 if ((in == NULL) || (inl <= 0)) return(0);
187 if (b->next_bio == NULL) return(0);
188 nt=(NBIO_TEST *)b->ptr;
189
190 BIO_clear_retry_flags(b);
191
192#if 1
193 if (nt->lwn > 0)
194 {
195 num=nt->lwn;
196 nt->lwn=0;
197 }
198 else
199 {
200 RAND_bytes(&n,1);
201 num=(n&7);
202 }
203
204 if (inl > num) inl=num;
205
206 if (num == 0)
207 {
208 ret= -1;
209 BIO_set_retry_write(b);
210 }
211 else
212#endif
213 {
214 ret=BIO_write(b->next_bio,in,inl);
215 if (ret < 0)
216 {
217 BIO_copy_next_retry(b);
218 nt->lwn=inl;
219 }
220 }
221 return(ret);
222 }
223
224static long nbiof_ctrl(b,cmd,num,ptr)
225BIO *b;
226int cmd;
227long num;
228char *ptr;
229 {
230 long ret;
231
232 if (b->next_bio == NULL) return(0);
233 switch (cmd)
234 {
235 case BIO_C_DO_STATE_MACHINE:
236 BIO_clear_retry_flags(b);
237 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
238 BIO_copy_next_retry(b);
239 break;
240 case BIO_CTRL_DUP:
241 ret=0L;
242 break;
243 default:
244 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
245 break;
246 }
247 return(ret);
248 }
249
250static int nbiof_gets(bp,buf,size)
251BIO *bp;
252char *buf;
253int size;
254 {
255 if (bp->next_bio == NULL) return(0);
256 return(BIO_gets(bp->next_bio,buf,size));
257 }
258
259
260static int nbiof_puts(bp,str)
261BIO *bp;
262char *str;
263 {
264 if (bp->next_bio == NULL) return(0);
265 return(BIO_puts(bp->next_bio,str));
266 }
267
268
diff --git a/src/lib/libcrypto/bio/bf_null.c b/src/lib/libcrypto/bio/bf_null.c
new file mode 100644
index 0000000000..a47a65741a
--- /dev/null
+++ b/src/lib/libcrypto/bio/bf_null.c
@@ -0,0 +1,196 @@
1/* crypto/bio/bf_null.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include "bio.h"
63#include "evp.h"
64
65/* BIO_put and BIO_get both add to the digest,
66 * BIO_gets returns the digest */
67
68#ifndef NOPROTO
69static int nullf_write(BIO *h,char *buf,int num);
70static int nullf_read(BIO *h,char *buf,int size);
71static int nullf_puts(BIO *h,char *str);
72static int nullf_gets(BIO *h,char *str,int size);
73static long nullf_ctrl(BIO *h,int cmd,long arg1,char *arg2);
74static int nullf_new(BIO *h);
75static int nullf_free(BIO *data);
76#else
77static int nullf_write();
78static int nullf_read();
79static int nullf_puts();
80static int nullf_gets();
81static long nullf_ctrl();
82static int nullf_new();
83static int nullf_free();
84#endif
85
86static BIO_METHOD methods_nullf=
87 {
88 BIO_TYPE_NULL_FILTER,
89 "NULL filter",
90 nullf_write,
91 nullf_read,
92 nullf_puts,
93 nullf_gets,
94 nullf_ctrl,
95 nullf_new,
96 nullf_free,
97 };
98
99BIO_METHOD *BIO_f_null()
100 {
101 return(&methods_nullf);
102 }
103
104static int nullf_new(bi)
105BIO *bi;
106 {
107 bi->init=1;
108 bi->ptr=NULL;
109 bi->flags=0;
110 return(1);
111 }
112
113static int nullf_free(a)
114BIO *a;
115 {
116 if (a == NULL) return(0);
117/* a->ptr=NULL;
118 a->init=0;
119 a->flags=0;*/
120 return(1);
121 }
122
123static int nullf_read(b,out,outl)
124BIO *b;
125char *out;
126int outl;
127 {
128 int ret=0;
129
130 if (out == NULL) return(0);
131 if (b->next_bio == NULL) return(0);
132 ret=BIO_read(b->next_bio,out,outl);
133 BIO_clear_retry_flags(b);
134 BIO_copy_next_retry(b);
135 return(ret);
136 }
137
138static int nullf_write(b,in,inl)
139BIO *b;
140char *in;
141int inl;
142 {
143 int ret=0;
144
145 if ((in == NULL) || (inl <= 0)) return(0);
146 if (b->next_bio == NULL) return(0);
147 ret=BIO_write(b->next_bio,in,inl);
148 BIO_clear_retry_flags(b);
149 BIO_copy_next_retry(b);
150 return(ret);
151 }
152
153static long nullf_ctrl(b,cmd,num,ptr)
154BIO *b;
155int cmd;
156long num;
157char *ptr;
158 {
159 long ret;
160
161 if (b->next_bio == NULL) return(0);
162 switch(cmd)
163 {
164 case BIO_C_DO_STATE_MACHINE:
165 BIO_clear_retry_flags(b);
166 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
167 BIO_copy_next_retry(b);
168 break;
169 case BIO_CTRL_DUP:
170 ret=0L;
171 break;
172 default:
173 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
174 }
175 return(ret);
176 }
177
178static int nullf_gets(bp,buf,size)
179BIO *bp;
180char *buf;
181int size;
182 {
183 if (bp->next_bio == NULL) return(0);
184 return(BIO_gets(bp->next_bio,buf,size));
185 }
186
187
188static int nullf_puts(bp,str)
189BIO *bp;
190char *str;
191 {
192 if (bp->next_bio == NULL) return(0);
193 return(BIO_puts(bp->next_bio,str));
194 }
195
196
diff --git a/src/lib/libcrypto/bio/bio.h b/src/lib/libcrypto/bio/bio.h
new file mode 100644
index 0000000000..300b330e00
--- /dev/null
+++ b/src/lib/libcrypto/bio/bio.h
@@ -0,0 +1,688 @@
1/* crypto/bio/bio.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_BIO_H
60#define HEADER_BIO_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "crypto.h"
67
68/* These are the 'types' of BIOs */
69#define BIO_TYPE_NONE 0
70#define BIO_TYPE_MEM (1|0x0400)
71#define BIO_TYPE_FILE (2|0x0400)
72
73#define BIO_TYPE_FD (4|0x0400|0x0100)
74#define BIO_TYPE_SOCKET (5|0x0400|0x0100)
75#define BIO_TYPE_NULL (6|0x0400)
76#define BIO_TYPE_SSL (7|0x0200)
77#define BIO_TYPE_MD (8|0x0200) /* pasive filter */
78#define BIO_TYPE_BUFFER (9|0x0200) /* filter */
79#define BIO_TYPE_CIPHER (10|0x0200) /* filter */
80#define BIO_TYPE_BASE64 (11|0x0200) /* filter */
81#define BIO_TYPE_CONNECT (12|0x0400|0x0100) /* socket - connect */
82#define BIO_TYPE_ACCEPT (13|0x0400|0x0100) /* socket for accept */
83#define BIO_TYPE_PROXY_CLIENT (14|0x0200) /* client proxy BIO */
84#define BIO_TYPE_PROXY_SERVER (15|0x0200) /* server proxy BIO */
85#define BIO_TYPE_NBIO_TEST (16|0x0200) /* server proxy BIO */
86#define BIO_TYPE_NULL_FILTER (17|0x0200)
87
88#define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */
89#define BIO_TYPE_FILTER 0x0200
90#define BIO_TYPE_SOURCE_SINK 0x0400
91
92/* BIO_FILENAME_READ|BIO_CLOSE to open or close on free.
93 * BIO_set_fp(in,stdin,BIO_NOCLOSE); */
94#define BIO_NOCLOSE 0x00
95#define BIO_CLOSE 0x01
96
97/* These are used in the following macros and are passed to
98 * BIO_ctrl() */
99#define BIO_CTRL_RESET 1 /* opt - rewind/zero etc */
100#define BIO_CTRL_EOF 2 /* opt - are we at the eof */
101#define BIO_CTRL_INFO 3 /* opt - extra tit-bits */
102#define BIO_CTRL_SET 4 /* man - set the 'IO' type */
103#define BIO_CTRL_GET 5 /* man - get the 'IO' type */
104#define BIO_CTRL_PUSH 6 /* opt - internal, used to signify change */
105#define BIO_CTRL_POP 7 /* opt - internal, used to signify change */
106#define BIO_CTRL_GET_CLOSE 8 /* man - set the 'close' on free */
107#define BIO_CTRL_SET_CLOSE 9 /* man - set the 'close' on free */
108#define BIO_CTRL_PENDING 10 /* opt - is their more data buffered */
109#define BIO_CTRL_FLUSH 11 /* opt - 'flush' buffered output */
110#define BIO_CTRL_DUP 12 /* man - extra stuff for 'duped' BIO */
111#define BIO_CTRL_WPENDING 13 /* opt - number of bytes still to write */
112/* callback is int cb(BIO *bio,state,ret); */
113#define BIO_CTRL_SET_CALLBACK 14 /* opt - set callback function */
114#define BIO_CTRL_GET_CALLBACK 15 /* opt - set callback function */
115
116#define BIO_CTRL_SET_FILENAME 30 /* BIO_s_file special */
117
118/* modifiers */
119#define BIO_FP_READ 0x02
120#define BIO_FP_WRITE 0x04
121#define BIO_FP_APPEND 0x08
122#define BIO_FP_TEXT 0x10
123
124#define BIO_FLAGS_READ 0x01
125#define BIO_FLAGS_WRITE 0x02
126#define BIO_FLAGS_IO_SPECIAL 0x04
127#define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL)
128#define BIO_FLAGS_SHOULD_RETRY 0x08
129
130/* Used in BIO_gethostbyname() */
131#define BIO_GHBN_CTRL_HITS 1
132#define BIO_GHBN_CTRL_MISSES 2
133#define BIO_GHBN_CTRL_CACHE_SIZE 3
134#define BIO_GHBN_CTRL_GET_ENTRY 4
135#define BIO_GHBN_CTRL_FLUSH 5
136
137/* Mostly used in the SSL BIO */
138/* Not used anymore
139 * #define BIO_FLAGS_PROTOCOL_DELAYED_READ 0x10
140 * #define BIO_FLAGS_PROTOCOL_DELAYED_WRITE 0x20
141 * #define BIO_FLAGS_PROTOCOL_STARTUP 0x40
142 */
143
144#define BIO_FLAGS_BASE64_NO_NL 0x100
145
146#define BIO_set_flags(b,f) ((b)->flags|=(f))
147#define BIO_get_flags(b) ((b)->flags)
148#define BIO_set_retry_special(b) \
149 ((b)->flags|=(BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY))
150#define BIO_set_retry_read(b) \
151 ((b)->flags|=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY))
152#define BIO_set_retry_write(b) \
153 ((b)->flags|=(BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY))
154
155/* These are normally used internally in BIOs */
156#define BIO_clear_flags(b,f) ((b)->flags&= ~(f))
157#define BIO_clear_retry_flags(b) \
158 ((b)->flags&= ~(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
159#define BIO_get_retry_flags(b) \
160 ((b)->flags&(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
161
162/* These shouldbe used by the application to tell why we should retry */
163#define BIO_should_read(a) ((a)->flags & BIO_FLAGS_READ)
164#define BIO_should_write(a) ((a)->flags & BIO_FLAGS_WRITE)
165#define BIO_should_io_special(a) ((a)->flags & BIO_FLAGS_IO_SPECIAL)
166#define BIO_retry_type(a) ((a)->flags & BIO_FLAGS_RWS)
167#define BIO_should_retry(a) ((a)->flags & BIO_FLAGS_SHOULD_RETRY)
168
169/* The next two are used in conjunction with the
170 * BIO_should_io_special() condition. After this returns true,
171 * BIO *BIO_get_retry_BIO(BIO *bio, int *reason); will walk the BIO
172 * stack and return the 'reason' for the special and the offending BIO.
173 * Given a BIO, BIO_get_retry_reason(bio) will return the code. */
174/* Returned from the SSL bio when the certificate retrieval code had an error */
175#define BIO_RR_SSL_X509_LOOKUP 0x01
176/* Returned from the connect BIO when a connect would have blocked */
177#define BIO_RR_CONNECT 0x02
178
179/* These are passed by the BIO callback */
180#define BIO_CB_FREE 0x01
181#define BIO_CB_READ 0x02
182#define BIO_CB_WRITE 0x03
183#define BIO_CB_PUTS 0x04
184#define BIO_CB_GETS 0x05
185#define BIO_CB_CTRL 0x06
186
187/* The callback is called before and after the underling operation,
188 * The BIO_CB_RETURN flag indicates if it is after the call */
189#define BIO_CB_RETURN 0x80
190#define BIO_CB_return(a) ((a)|BIO_CB_RETURN))
191#define BIO_cb_pre(a) (!((a)&BIO_CB_RETURN))
192#define BIO_cb_post(a) ((a)&BIO_CB_RETURN)
193
194#define BIO_set_callback(b,cb) ((b)->callback=(cb))
195#define BIO_set_callback_arg(b,arg) ((b)->cb_arg=(char *)(arg))
196#define BIO_get_callback_arg(b) ((b)->cb_arg)
197#define BIO_get_callback(b) ((b)->callback)
198#define BIO_method_name(b) ((b)->method->name)
199#define BIO_method_type(b) ((b)->method->type)
200
201#ifndef WIN16
202typedef struct bio_method_st
203 {
204 int type;
205 char *name;
206 int (*bwrite)();
207 int (*bread)();
208 int (*bputs)();
209 int (*bgets)();
210 long (*ctrl)();
211 int (*create)();
212 int (*destroy)();
213 } BIO_METHOD;
214#else
215typedef struct bio_method_st
216 {
217 int type;
218 char *name;
219 int (_far *bwrite)();
220 int (_far *bread)();
221 int (_far *bputs)();
222 int (_far *bgets)();
223 long (_far *ctrl)();
224 int (_far *create)();
225 int (_far *destroy)();
226 } BIO_METHOD;
227#endif
228
229typedef struct bio_st
230 {
231 BIO_METHOD *method;
232#ifndef NOPROTO
233 /* bio, mode, argp, argi, argl, ret */
234 long (*callback)(struct bio_st *,int,char *,int, long,long);
235#else
236 long (*callback)();
237#endif
238 char *cb_arg; /* first argument for the callback */
239
240 int init;
241 int shutdown;
242 int flags; /* extra storage */
243 int retry_reason;
244 int num;
245 char *ptr;
246 struct bio_st *next_bio; /* used by filter BIOs */
247 struct bio_st *prev_bio; /* used by filter BIOs */
248 int references;
249 unsigned long num_read;
250 unsigned long num_write;
251
252 CRYPTO_EX_DATA ex_data;
253 } BIO;
254
255typedef struct bio_f_buffer_ctx_struct
256 {
257 /* BIO *bio; */ /* this is now in the BIO struct */
258 int ibuf_size; /* how big is the input buffer */
259 int obuf_size; /* how big is the output buffer */
260
261 char *ibuf; /* the char array */
262 int ibuf_len; /* how many bytes are in it */
263 int ibuf_off; /* write/read offset */
264
265 char *obuf; /* the char array */
266 int obuf_len; /* how many bytes are in it */
267 int obuf_off; /* write/read offset */
268 } BIO_F_BUFFER_CTX;
269
270/* connect BIO stuff */
271#define BIO_CONN_S_BEFORE 1
272#define BIO_CONN_S_GET_IP 2
273#define BIO_CONN_S_GET_PORT 3
274#define BIO_CONN_S_CREATE_SOCKET 4
275#define BIO_CONN_S_CONNECT 5
276#define BIO_CONN_S_OK 6
277#define BIO_CONN_S_BLOCKED_CONNECT 7
278#define BIO_CONN_S_NBIO 8
279#define BIO_CONN_get_param_hostname BIO_ctrl
280
281#define BIO_number_read(b) ((b)->num_read)
282#define BIO_number_written(b) ((b)->num_write)
283
284#define BIO_C_SET_CONNECT 100
285#define BIO_C_DO_STATE_MACHINE 101
286#define BIO_C_SET_NBIO 102
287#define BIO_C_SET_PROXY_PARAM 103
288#define BIO_C_SET_FD 104
289#define BIO_C_GET_FD 105
290#define BIO_C_SET_FILE_PTR 106
291#define BIO_C_GET_FILE_PTR 107
292#define BIO_C_SET_FILENAME 108
293#define BIO_C_SET_SSL 109
294#define BIO_C_GET_SSL 110
295#define BIO_C_SET_MD 111
296#define BIO_C_GET_MD 112
297#define BIO_C_GET_CIPHER_STATUS 113
298#define BIO_C_SET_BUF_MEM 114
299#define BIO_C_GET_BUF_MEM_PTR 115
300#define BIO_C_GET_BUFF_NUM_LINES 116
301#define BIO_C_SET_BUFF_SIZE 117
302#define BIO_C_SET_ACCEPT 118
303#define BIO_C_SSL_MODE 119
304#define BIO_C_GET_MD_CTX 120
305#define BIO_C_GET_PROXY_PARAM 121
306#define BIO_C_SET_BUFF_READ_DATA 122 /* data to read first */
307#define BIO_C_GET_CONNECT 123
308#define BIO_C_GET_ACCEPT 124
309#define BIO_C_SET_SSL_RENEGOTIATE_BYTES 125
310#define BIO_C_GET_SSL_NUM_RENEGOTIATES 126
311#define BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT 127
312
313#define BIO_set_app_data(s,arg) BIO_set_ex_data(s,0,(char *)arg)
314#define BIO_get_app_data(s) BIO_get_ex_data(s,0)
315
316int BIO_get_ex_num(BIO *bio);
317int BIO_set_ex_data(BIO *bio,int idx,char *data);
318char *BIO_get_ex_data(BIO *bio,int idx);
319void BIO_set_ex_free_func(BIO *bio,int idx,void (*cb)());
320int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(),
321 int (*dup_func)(), void (*free_func)());
322
323/* BIO_s_connect_socket() */
324#define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name)
325#define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port)
326#define BIO_set_conn_ip(b,ip) BIO_ctrl(b,BIO_C_SET_CONNECT,2,(char *)ip)
327#define BIO_set_conn_int_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,3,(char *)port)
328#define BIO_get_conn_hostname(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0)
329#define BIO_get_conn_port(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1)
330#define BIO_get_conn_ip(b,ip) BIO_ptr_ctrl(b,BIO_C_SET_CONNECT,2)
331#define BIO_get_conn_int port(b,port) BIO_int_ctrl(b,BIO_C_SET_CONNECT,3,port)
332
333#define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL)
334
335/* BIO_s_accept_socket() */
336#define BIO_set_accept_port(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0,(char *)name)
337#define BIO_get_accept_port(b) BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,0)
338/* #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) */
339#define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?"a":NULL)
340#define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio)
341
342#define BIO_do_connect(b) BIO_do_handshake(b)
343#define BIO_do_accept(b) BIO_do_handshake(b)
344#define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
345
346/* BIO_s_proxy_client() */
347#define BIO_set_url(b,url) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,0,(char *)(url))
348#define BIO_set_proxies(b,p) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,1,(char *)(p))
349/* BIO_set_nbio(b,n) */
350#define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s))
351/* BIO *BIO_get_filter_bio(BIO *bio); */
352#define BIO_set_proxy_cb(b,cb) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(char *)(cb))
353#define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk)
354#define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool)
355
356#define BIO_get_proxy_header(b,skp) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,0,(char *)skp)
357#define BIO_get_proxies(b,pxy_p) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,1,(char *)(pxy_p))
358#define BIO_get_url(b,url) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,2,(char *)(url))
359#define BIO_get_no_connect_return(b) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,5,NULL)
360
361#define BIO_set_fd(b,fd,c) BIO_int_ctrl(b,BIO_C_SET_FD,c,fd)
362#define BIO_get_fd(b,c) BIO_ctrl(b,BIO_C_GET_FD,0,(char *)c)
363
364#define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp)
365#define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp)
366
367#define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
368 BIO_CLOSE|BIO_FP_READ,name)
369#define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
370 BIO_CLOSE|BIO_FP_WRITE,name)
371#define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
372 BIO_CLOSE|BIO_FP_APPEND,name)
373
374/* WARNING WARNING, this ups the reference count on the read bio of the
375 * SSL structure. This is because the ssl read BIO is now pointed to by
376 * the next_bio field in the bio. So when you free the BIO, make sure
377 * you are doing a BIO_free_all() to catch the underlying BIO. */
378#define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl)
379#define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp)
380#define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL)
381#define BIO_set_ssl_renegotiate_bytes(b,num) \
382 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL);
383#define BIO_get_num_renegotiates(b) \
384 BIO_ctrl(b,BIO_C_SET_SSL_NUM_RENEGOTIATES,0,NULL);
385#define BIO_set_ssl_renegotiate_timeout(b,seconds) \
386 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL);
387
388/* defined in evp.h */
389/* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */
390
391#define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm)
392#define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp)
393
394/* For the BIO_f_buffer() type */
395#define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL)
396#define BIO_set_buffer_size(b,size) BIO_ctrl(b,BIO_C_SET_BUFF_SIZE,size,NULL)
397#define BIO_set_read_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,0)
398#define BIO_set_write_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,1)
399#define BIO_set_buffer_read_data(b,buf,num) BIO_ctrl(b,BIO_C_SET_BUFF_READ_DATA,num,buf)
400
401/* Don't use the next one unless you know what you are doing :-) */
402#define BIO_dup_state(b,ret) BIO_ctrl(b,BIO_CTRL_DUP,0,(char *)(ret))
403
404#define BIO_reset(b) (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL)
405#define BIO_eof(b) (int)BIO_ctrl(b,BIO_CTRL_EOF,0,NULL)
406#define BIO_set_close(b,c) (int)BIO_ctrl(b,BIO_CTRL_SET_CLOSE,(c),NULL)
407#define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL)
408#define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL)
409#define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL)
410#define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL)
411#define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0,(char *)cbp)
412#define BIO_set_info_callback(b,cb) (int)BIO_ctrl(b,BIO_CTRL_SET_CALLBACK,0,(char *)cb)
413
414/* For the BIO_f_buffer() type */
415#define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL)
416
417#ifdef NO_STDIO
418#define NO_FP_API
419#endif
420
421#ifndef NOPROTO
422# if defined(WIN16) && defined(_WINDLL)
423BIO_METHOD *BIO_s_file_internal(void);
424BIO *BIO_new_file_internal(char *filename, char *mode);
425BIO *BIO_new_fp_internal(FILE *stream, int close_flag);
426# define BIO_s_file BIO_s_file_internal
427# define BIO_new_file BIO_new_file_internal
428# define BIO_new_fp BIO_new_fp_internal
429# else /* FP_API */
430BIO_METHOD *BIO_s_file(void );
431BIO *BIO_new_file(char *filename, char *mode);
432BIO *BIO_new_fp(FILE *stream, int close_flag);
433# define BIO_s_file_internal BIO_s_file
434# define BIO_new_file_internal BIO_new_file
435# define BIO_new_fp_internal BIO_s_file
436# endif /* FP_API */
437#else
438# if defined(WIN16) && defined(_WINDLL)
439BIO_METHOD *BIO_s_file_internal();
440BIO *BIO_new_file_internal();
441BIO *BIO_new_fp_internal();
442# define BIO_s_file BIO_s_file_internal
443# define BIO_new_file BIO_new_file_internal
444# define BIO_new_fp BIO_new_fp_internal
445# else /* FP_API */
446BIO_METHOD *BIO_s_file();
447BIO *BIO_new_file();
448BIO *BIO_new_fp();
449# define BIO_s_file_internal BIO_s_file
450# define BIO_new_file_internal BIO_new_file
451# define BIO_new_fp_internal BIO_s_file
452# endif /* FP_API */
453#endif
454
455#ifndef NOPROTO
456BIO * BIO_new(BIO_METHOD *type);
457int BIO_set(BIO *a,BIO_METHOD *type);
458int BIO_free(BIO *a);
459int BIO_read(BIO *b, char *data, int len);
460int BIO_gets(BIO *bp,char *buf, int size);
461int BIO_write(BIO *b, char *data, int len);
462int BIO_puts(BIO *bp,char *buf);
463long BIO_ctrl(BIO *bp,int cmd,long larg,char *parg);
464char * BIO_ptr_ctrl(BIO *bp,int cmd,long larg);
465long BIO_int_ctrl(BIO *bp,int cmd,long larg,int iarg);
466BIO * BIO_push(BIO *b,BIO *append);
467BIO * BIO_pop(BIO *b);
468void BIO_free_all(BIO *a);
469BIO * BIO_find_type(BIO *b,int bio_type);
470BIO * BIO_get_retry_BIO(BIO *bio, int *reason);
471int BIO_get_retry_reason(BIO *bio);
472BIO * BIO_dup_chain(BIO *in);
473
474#ifndef WIN16
475long BIO_debug_callback(BIO *bio,int cmd,char *argp,int argi,
476 long argl,long ret);
477#else
478long _far _loadds BIO_debug_callback(BIO *bio,int cmd,char *argp,int argi,
479 long argl,long ret);
480#endif
481
482BIO_METHOD *BIO_s_mem(void);
483BIO_METHOD *BIO_s_socket(void);
484BIO_METHOD *BIO_s_connect(void);
485BIO_METHOD *BIO_s_accept(void);
486BIO_METHOD *BIO_s_fd(void);
487BIO_METHOD *BIO_s_null(void);
488BIO_METHOD *BIO_f_null(void);
489BIO_METHOD *BIO_f_nbio_test(void);
490BIO_METHOD *BIO_f_buffer(void);
491
492int BIO_sock_should_retry(int i);
493int BIO_sock_non_fatal_error(int error);
494int BIO_fd_should_retry(int i);
495int BIO_fd_non_fatal_error(int error);
496int BIO_dump(BIO *b,char *bytes,int len);
497
498struct hostent *BIO_gethostbyname(char *name);
499int BIO_sock_error(int sock);
500int BIO_socket_ioctl(int fd, long type, unsigned long *arg);
501int BIO_get_port(char *str, short *port_ptr);
502int BIO_get_host_ip(char *str, unsigned char *ip);
503int BIO_get_accept_socket(char *host_port);
504int BIO_accept(int sock,char **ip_port);
505int BIO_sock_init(void );
506void BIO_sock_cleanup(void);
507int BIO_set_tcp_ndelay(int sock,int turn_on);
508
509void ERR_load_BIO_strings(void );
510
511BIO *BIO_new_socket(int sock, int close_flag);
512BIO *BIO_new_fd(int fd, int close_flag);
513BIO *BIO_new_connect(char *host_port);
514BIO *BIO_new_accept(char *host_port);
515
516void BIO_copy_next_retry(BIO *b);
517
518long BIO_ghbn_ctrl(int cmd,int iarg,char *parg);
519
520#else
521
522BIO * BIO_new();
523int BIO_set();
524int BIO_free();
525int BIO_read();
526int BIO_gets();
527int BIO_write();
528int BIO_puts();
529char * BIO_ptr_ctrl();
530long BIO_ctrl();
531long BIO_int_ctrl();
532BIO * BIO_push();
533BIO * BIO_pop();
534void BIO_free_all();
535BIO * BIO_find_type();
536BIO * BIO_get_retry_BIO();
537int BIO_get_retry_reason();
538BIO * BIO_dup_chain();
539
540#ifndef WIN16
541long BIO_debug_callback();
542#else
543long _far _loadds BIO_debug_callback();
544#endif
545
546BIO_METHOD *BIO_s_mem();
547BIO_METHOD *BIO_s_socket();
548BIO_METHOD *BIO_s_connect();
549BIO_METHOD *BIO_s_accept();
550BIO_METHOD *BIO_s_fd();
551BIO_METHOD *BIO_s_null();
552BIO_METHOD *BIO_f_null();
553BIO_METHOD *BIO_f_buffer();
554BIO_METHOD *BIO_f_nbio_test();
555
556int BIO_sock_should_retry();
557int BIO_sock_non_fatal_error();
558int BIO_fd_should_retry();
559int BIO_fd_non_fatal_error();
560int BIO_dump();
561
562struct hostent *BIO_gethostbyname();
563int BIO_sock_error();
564int BIO_socket_ioctl();
565int BIO_get_port();
566int BIO_get_host_ip();
567int BIO_get_accept_socket();
568int BIO_accept();
569int BIO_sock_init();
570void BIO_sock_cleanup();
571int BIO_set_tcp_ndelay();
572
573void ERR_load_BIO_strings();
574
575BIO *BIO_new_socket();
576BIO *BIO_new_fd();
577BIO *BIO_new_connect();
578BIO *BIO_new_accept();
579
580void BIO_copy_next_retry();
581
582int BIO_ghbn_ctrl();
583
584#endif
585
586/* Tim Hudson's portable varargs stuff */
587
588#ifndef NOPROTO
589#define VAR_ANSI /* select ANSI version by default */
590#endif
591
592#ifdef VAR_ANSI
593/* ANSI version of a "portable" macro set for variable length args */
594#ifndef __STDARG_H__ /**/
595#include <stdarg.h>
596#endif /**/
597
598#define VAR_PLIST(arg1type,arg1) arg1type arg1, ...
599#define VAR_PLIST2(arg1type,arg1,arg2type,arg2) arg1type arg1,arg2type arg2,...
600#define VAR_ALIST
601#define VAR_BDEFN(args,arg1type,arg1) va_list args
602#define VAR_BDEFN2(args,arg1type,arg1,arg2type,arg2) va_list args
603#define VAR_INIT(args,arg1type,arg1) va_start(args,arg1);
604#define VAR_INIT2(args,arg1type,arg1,arg2type,arg2) va_start(args,arg2);
605#define VAR_ARG(args,type,arg) arg=va_arg(args,type)
606#define VAR_END(args) va_end(args);
607
608#else
609
610/* K&R version of a "portable" macro set for variable length args */
611#ifndef __VARARGS_H__
612#include <varargs.h>
613#endif
614
615#define VAR_PLIST(arg1type,arg1) va_alist
616#define VAR_PLIST2(arg1type,arg1,arg2type,arg2) va_alist
617#define VAR_ALIST va_dcl
618#define VAR_BDEFN(args,arg1type,arg1) va_list args; arg1type arg1
619#define VAR_BDEFN2(args,arg1type,arg1,arg2type,arg2) va_list args; \
620 arg1type arg1; arg2type arg2
621#define VAR_INIT(args,arg1type,arg1) va_start(args); \
622 arg1=va_arg(args,arg1type);
623#define VAR_INIT2(args,arg1type,arg1,arg2type,arg2) va_start(args); \
624 arg1=va_arg(args,arg1type); arg2=va_arg(args,arg2type);
625#define VAR_ARG(args,type,arg) arg=va_arg(args,type)
626#define VAR_END(args) va_end(args);
627
628#endif
629
630#ifndef NOPROTO
631int BIO_printf( VAR_PLIST( BIO *, bio ) );
632#else
633int BIO_printf();
634#endif
635
636/* BEGIN ERROR CODES */
637/* Error codes for the BIO functions. */
638
639/* Function codes. */
640#define BIO_F_ACPT_STATE 100
641#define BIO_F_BIO_ACCEPT 101
642#define BIO_F_BIO_CTRL 102
643#define BIO_F_BIO_GETS 103
644#define BIO_F_BIO_GET_ACCEPT_SOCKET 104
645#define BIO_F_BIO_GET_HOST_IP 105
646#define BIO_F_BIO_GET_PORT 106
647#define BIO_F_BIO_NEW 107
648#define BIO_F_BIO_NEW_FILE 108
649#define BIO_F_BIO_PUTS 109
650#define BIO_F_BIO_READ 110
651#define BIO_F_BIO_SOCK_INIT 111
652#define BIO_F_BIO_WRITE 112
653#define BIO_F_BUFFER_CTRL 113
654#define BIO_F_CONN_STATE 114
655#define BIO_F_FILE_CTRL 115
656#define BIO_F_MEM_WRITE 116
657#define BIO_F_SSL_NEW 117
658#define BIO_F_WSASTARTUP 118
659
660/* Reason codes. */
661#define BIO_R_ACCEPT_ERROR 100
662#define BIO_R_BAD_FOPEN_MODE 101
663#define BIO_R_BAD_HOSTNAME_LOOKUP 102
664#define BIO_R_CONNECT_ERROR 103
665#define BIO_R_ERROR_SETTING_NBIO 104
666#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105
667#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106
668#define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107
669#define BIO_R_INVALID_IP_ADDRESS 108
670#define BIO_R_KEEPALIVE 109
671#define BIO_R_NBIO_CONNECT_ERROR 110
672#define BIO_R_NO_ACCEPT_PORT_SPECIFIED 111
673#define BIO_R_NO_HOSTHNAME_SPECIFIED 112
674#define BIO_R_NO_PORT_DEFINED 113
675#define BIO_R_NO_PORT_SPECIFIED 114
676#define BIO_R_NULL_PARAMETER 115
677#define BIO_R_UNABLE_TO_BIND_SOCKET 116
678#define BIO_R_UNABLE_TO_CREATE_SOCKET 117
679#define BIO_R_UNABLE_TO_LISTEN_SOCKET 118
680#define BIO_R_UNINITALISED 119
681#define BIO_R_UNSUPPORTED_METHOD 120
682#define BIO_R_WSASTARTUP 121
683
684#ifdef __cplusplus
685}
686#endif
687#endif
688
diff --git a/src/lib/libcrypto/bio/bio_cb.c b/src/lib/libcrypto/bio/bio_cb.c
new file mode 100644
index 0000000000..bc6ed9eda1
--- /dev/null
+++ b/src/lib/libcrypto/bio/bio_cb.c
@@ -0,0 +1,138 @@
1/* crypto/bio/bio_cb.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62#include "cryptlib.h"
63#include "bio.h"
64#include "err.h"
65
66long MS_CALLBACK BIO_debug_callback(bio,cmd,argp,argi,argl,ret)
67BIO *bio;
68int cmd;
69char *argp;
70int argi;
71long argl;
72long ret;
73 {
74 BIO *b;
75 MS_STATIC char buf[256];
76 char *p;
77 long r=1;
78
79 if (BIO_CB_RETURN & cmd)
80 r=ret;
81
82 sprintf(buf,"BIO[%08lX]:",(unsigned long)bio);
83 p= &(buf[14]);
84 switch (cmd)
85 {
86 case BIO_CB_FREE:
87 sprintf(p,"Free - %s\n",bio->method->name);
88 break;
89 case BIO_CB_READ:
90 if (bio->method->type & BIO_TYPE_DESCRIPTOR)
91 sprintf(p,"read(%d,%d) - %s fd=%d\n",bio->num,argi,bio->method->name,bio->num);
92 else
93 sprintf(p,"read(%d,%d) - %s\n",bio->num,argi,bio->method->name);
94 break;
95 case BIO_CB_WRITE:
96 if (bio->method->type & BIO_TYPE_DESCRIPTOR)
97 sprintf(p,"write(%d,%d) - %s fd=%d\n",bio->num,argi,bio->method->name,bio->num);
98 else
99 sprintf(p,"write(%d,%d) - %s\n",bio->num,argi,bio->method->name);
100 break;
101 case BIO_CB_PUTS:
102 sprintf(p,"puts() - %s\n",bio->method->name);
103 break;
104 case BIO_CB_GETS:
105 sprintf(p,"gets(%d) - %s\n",argi,bio->method->name);
106 break;
107 case BIO_CB_CTRL:
108 sprintf(p,"ctrl(%d) - %s\n",argi,bio->method->name);
109 break;
110 case BIO_CB_RETURN|BIO_CB_READ:
111 sprintf(p,"read return %ld\n",ret);
112 break;
113 case BIO_CB_RETURN|BIO_CB_WRITE:
114 sprintf(p,"write return %ld\n",ret);
115 break;
116 case BIO_CB_RETURN|BIO_CB_GETS:
117 sprintf(p,"gets return %ld\n",ret);
118 break;
119 case BIO_CB_RETURN|BIO_CB_PUTS:
120 sprintf(p,"puts return %ld\n",ret);
121 break;
122 case BIO_CB_RETURN|BIO_CB_CTRL:
123 sprintf(p,"ctrl return %ld\n",ret);
124 break;
125 default:
126 sprintf(p,"bio callback - unknown type (%d)\n",cmd);
127 break;
128 }
129
130 b=(BIO *)bio->cb_arg;
131 if (b != NULL)
132 BIO_write(b,buf,strlen(buf));
133#if !defined(NO_STDIO) && !defined(WIN16)
134 else
135 fputs(buf,stderr);
136#endif
137 return(r);
138 }
diff --git a/src/lib/libcrypto/bio/bio_err.c b/src/lib/libcrypto/bio/bio_err.c
new file mode 100644
index 0000000000..37e14ca107
--- /dev/null
+++ b/src/lib/libcrypto/bio/bio_err.c
@@ -0,0 +1,130 @@
1/* lib/bio/bio_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "bio.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA BIO_str_functs[]=
65 {
66{ERR_PACK(0,BIO_F_ACPT_STATE,0), "ACPT_STATE"},
67{ERR_PACK(0,BIO_F_BIO_ACCEPT,0), "BIO_accept"},
68{ERR_PACK(0,BIO_F_BIO_CTRL,0), "BIO_ctrl"},
69{ERR_PACK(0,BIO_F_BIO_GETS,0), "BIO_gets"},
70{ERR_PACK(0,BIO_F_BIO_GET_ACCEPT_SOCKET,0), "BIO_get_accept_socket"},
71{ERR_PACK(0,BIO_F_BIO_GET_HOST_IP,0), "BIO_get_host_ip"},
72{ERR_PACK(0,BIO_F_BIO_GET_PORT,0), "BIO_get_port"},
73{ERR_PACK(0,BIO_F_BIO_NEW,0), "BIO_new"},
74{ERR_PACK(0,BIO_F_BIO_NEW_FILE,0), "BIO_new_file"},
75{ERR_PACK(0,BIO_F_BIO_PUTS,0), "BIO_puts"},
76{ERR_PACK(0,BIO_F_BIO_READ,0), "BIO_read"},
77{ERR_PACK(0,BIO_F_BIO_SOCK_INIT,0), "BIO_sock_init"},
78{ERR_PACK(0,BIO_F_BIO_WRITE,0), "BIO_write"},
79{ERR_PACK(0,BIO_F_BUFFER_CTRL,0), "BUFFER_CTRL"},
80{ERR_PACK(0,BIO_F_CONN_STATE,0), "CONN_STATE"},
81{ERR_PACK(0,BIO_F_FILE_CTRL,0), "FILE_CTRL"},
82{ERR_PACK(0,BIO_F_MEM_WRITE,0), "MEM_WRITE"},
83{ERR_PACK(0,BIO_F_SSL_NEW,0), "SSL_NEW"},
84{ERR_PACK(0,BIO_F_WSASTARTUP,0), "WSASTARTUP"},
85{0,NULL},
86 };
87
88static ERR_STRING_DATA BIO_str_reasons[]=
89 {
90{BIO_R_ACCEPT_ERROR ,"accept error"},
91{BIO_R_BAD_FOPEN_MODE ,"bad fopen mode"},
92{BIO_R_BAD_HOSTNAME_LOOKUP ,"bad hostname lookup"},
93{BIO_R_CONNECT_ERROR ,"connect error"},
94{BIO_R_ERROR_SETTING_NBIO ,"error setting nbio"},
95{BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET,"error setting nbio on accepted socket"},
96{BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET,"error setting nbio on accept socket"},
97{BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET ,"gethostbyname addr is not af inet"},
98{BIO_R_INVALID_IP_ADDRESS ,"invalid ip address"},
99{BIO_R_KEEPALIVE ,"keepalive"},
100{BIO_R_NBIO_CONNECT_ERROR ,"nbio connect error"},
101{BIO_R_NO_ACCEPT_PORT_SPECIFIED ,"no accept port specified"},
102{BIO_R_NO_HOSTHNAME_SPECIFIED ,"no hosthname specified"},
103{BIO_R_NO_PORT_DEFINED ,"no port defined"},
104{BIO_R_NO_PORT_SPECIFIED ,"no port specified"},
105{BIO_R_NULL_PARAMETER ,"null parameter"},
106{BIO_R_UNABLE_TO_BIND_SOCKET ,"unable to bind socket"},
107{BIO_R_UNABLE_TO_CREATE_SOCKET ,"unable to create socket"},
108{BIO_R_UNABLE_TO_LISTEN_SOCKET ,"unable to listen socket"},
109{BIO_R_UNINITALISED ,"uninitalised"},
110{BIO_R_UNSUPPORTED_METHOD ,"unsupported method"},
111{BIO_R_WSASTARTUP ,"wsastartup"},
112{0,NULL},
113 };
114
115#endif
116
117void ERR_load_BIO_strings()
118 {
119 static int init=1;
120
121 if (init);
122 {;
123 init=0;
124#ifndef NO_ERR
125 ERR_load_strings(ERR_LIB_BIO,BIO_str_functs);
126 ERR_load_strings(ERR_LIB_BIO,BIO_str_reasons);
127#endif
128
129 }
130 }
diff --git a/src/lib/libcrypto/bio/bio_lib.c b/src/lib/libcrypto/bio/bio_lib.c
new file mode 100644
index 0000000000..7a66b0892e
--- /dev/null
+++ b/src/lib/libcrypto/bio/bio_lib.c
@@ -0,0 +1,519 @@
1/* crypto/bio/bio_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "crypto.h"
62#include "cryptlib.h"
63#include "bio.h"
64#include "stack.h"
65
66static STACK *bio_meth=NULL;
67static int bio_meth_num=0;
68
69BIO *BIO_new(method)
70BIO_METHOD *method;
71 {
72 BIO *ret=NULL;
73
74 ret=(BIO *)Malloc(sizeof(BIO));
75 if (ret == NULL)
76 {
77 BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);
78 return(NULL);
79 }
80 if (!BIO_set(ret,method))
81 {
82 Free(ret);
83 ret=NULL;
84 }
85 return(ret);
86 }
87
88int BIO_set(bio,method)
89BIO *bio;
90BIO_METHOD *method;
91 {
92 bio->method=method;
93 bio->callback=NULL;
94 bio->cb_arg=NULL;
95 bio->init=0;
96 bio->shutdown=1;
97 bio->flags=0;
98 bio->retry_reason=0;
99 bio->num=0;
100 bio->ptr=NULL;
101 bio->prev_bio=NULL;
102 bio->next_bio=NULL;
103 bio->references=1;
104 bio->num_read=0L;
105 bio->num_write=0L;
106 CRYPTO_new_ex_data(bio_meth,(char *)bio,&bio->ex_data);
107 if (method->create != NULL)
108 if (!method->create(bio))
109 return(0);
110 return(1);
111 }
112
113int BIO_free(a)
114BIO *a;
115 {
116 int ret=0,i;
117
118 if (a == NULL) return(0);
119
120 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_BIO);
121#ifdef REF_PRINT
122 REF_PRINT("BIO",a);
123#endif
124 if (i > 0) return(1);
125#ifdef REF_CHECK
126 if (i < 0)
127 {
128 fprintf(stderr,"BIO_free, bad reference count\n");
129 abort();
130 }
131#endif
132 if ((a->callback != NULL) &&
133 ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0))
134 return(i);
135
136 CRYPTO_free_ex_data(bio_meth,(char *)a,&a->ex_data);
137
138 if ((a->method == NULL) || (a->method->destroy == NULL)) return(1);
139 ret=a->method->destroy(a);
140 Free(a);
141 return(1);
142 }
143
144int BIO_read(b,out,outl)
145BIO *b;
146char *out;
147int outl;
148 {
149 int i;
150 long (*cb)();
151
152 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL))
153 {
154 BIOerr(BIO_F_BIO_READ,BIO_R_UNSUPPORTED_METHOD);
155 return(-2);
156 }
157
158 cb=b->callback;
159 if ((cb != NULL) &&
160 ((i=(int)cb(b,BIO_CB_READ,out,outl,0L,1L)) <= 0))
161 return(i);
162
163 if (!b->init)
164 {
165 BIOerr(BIO_F_BIO_READ,BIO_R_UNINITALISED);
166 return(-2);
167 }
168
169 i=b->method->bread(b,out,outl);
170 if (i > 0) b->num_read+=(unsigned long)i;
171
172 if (cb != NULL)
173 i=(int)cb(b,BIO_CB_READ|BIO_CB_RETURN,out,outl,
174 0L,(long)i);
175 return(i);
176 }
177
178int BIO_write(b,in,inl)
179BIO *b;
180char *in;
181int inl;
182 {
183 int i;
184 long (*cb)();
185
186 if (b == NULL)
187 return(0);
188
189 cb=b->callback;
190 if ((b->method == NULL) || (b->method->bwrite == NULL))
191 {
192 BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD);
193 return(-2);
194 }
195
196 if ((cb != NULL) &&
197 ((i=(int)cb(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0))
198 return(i);
199
200 if (!b->init)
201 {
202 BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITALISED);
203 return(-2);
204 }
205
206 i=b->method->bwrite(b,in,inl);
207 if (i > 0) b->num_write+=(unsigned long)i;
208
209 if (cb != NULL)
210 i=(int)cb(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl,
211 0L,(long)i);
212 return(i);
213 }
214
215int BIO_puts(b,in)
216BIO *b;
217char *in;
218 {
219 int i;
220 long (*cb)();
221
222 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL))
223 {
224 BIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD);
225 return(-2);
226 }
227
228 cb=b->callback;
229
230 if ((cb != NULL) &&
231 ((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0))
232 return(i);
233
234 if (!b->init)
235 {
236 BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITALISED);
237 return(-2);
238 }
239
240 i=b->method->bputs(b,in);
241
242 if (cb != NULL)
243 i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0,
244 0L,(long)i);
245 return(i);
246 }
247
248int BIO_gets(b,in,inl)
249BIO *b;
250char *in;
251int inl;
252 {
253 int i;
254 long (*cb)();
255
256 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL))
257 {
258 BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD);
259 return(-2);
260 }
261
262 cb=b->callback;
263
264 if ((cb != NULL) &&
265 ((i=(int)cb(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0))
266 return(i);
267
268 if (!b->init)
269 {
270 BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITALISED);
271 return(-2);
272 }
273
274 i=b->method->bgets(b,in,inl);
275
276 if (cb != NULL)
277 i=(int)cb(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl,
278 0L,(long)i);
279 return(i);
280 }
281
282long BIO_int_ctrl(b,cmd,larg,iarg)
283BIO *b;
284int cmd;
285long larg;
286int iarg;
287 {
288 int i;
289
290 i=iarg;
291 return(BIO_ctrl(b,cmd,larg,(char *)&i));
292 }
293
294char *BIO_ptr_ctrl(b,cmd,larg)
295BIO *b;
296int cmd;
297long larg;
298 {
299 char *p=NULL;
300
301 if (BIO_ctrl(b,cmd,larg,(char *)&p) <= 0)
302 return(NULL);
303 else
304 return(p);
305 }
306
307long BIO_ctrl(b,cmd,larg,parg)
308BIO *b;
309int cmd;
310long larg;
311char *parg;
312 {
313 long ret;
314 long (*cb)();
315
316 if (b == NULL) return(0);
317
318 if ((b->method == NULL) || (b->method->ctrl == NULL))
319 {
320 BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);
321 return(-2);
322 }
323
324 cb=b->callback;
325
326 if ((cb != NULL) &&
327 ((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0))
328 return(ret);
329
330 ret=b->method->ctrl(b,cmd,larg,parg);
331
332 if (cb != NULL)
333 ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd,
334 larg,ret);
335 return(ret);
336 }
337
338/* put the 'bio' on the end of b's list of operators */
339BIO *BIO_push(b,bio)
340BIO *b,*bio;
341 {
342 BIO *lb;
343
344 if (b == NULL) return(bio);
345 lb=b;
346 while (lb->next_bio != NULL)
347 lb=lb->next_bio;
348 lb->next_bio=bio;
349 if (bio != NULL)
350 bio->prev_bio=lb;
351 /* called to do internal processing */
352 BIO_ctrl(b,BIO_CTRL_PUSH,0,NULL);
353 return(b);
354 }
355
356/* Remove the first and return the rest */
357BIO *BIO_pop(b)
358BIO *b;
359 {
360 BIO *ret;
361
362 if (b == NULL) return(NULL);
363 ret=b->next_bio;
364
365 if (b->prev_bio != NULL)
366 b->prev_bio->next_bio=b->next_bio;
367 if (b->next_bio != NULL)
368 b->next_bio->prev_bio=b->prev_bio;
369
370 b->next_bio=NULL;
371 b->prev_bio=NULL;
372 BIO_ctrl(b,BIO_CTRL_POP,0,NULL);
373 return(ret);
374 }
375
376BIO *BIO_get_retry_BIO(bio,reason)
377BIO *bio;
378int *reason;
379 {
380 BIO *b,*last;
381
382 b=last=bio;
383 for (;;)
384 {
385 if (!BIO_should_retry(b)) break;
386 last=b;
387 b=b->next_bio;
388 if (b == NULL) break;
389 }
390 if (reason != NULL) *reason=last->retry_reason;
391 return(last);
392 }
393
394int BIO_get_retry_reason(bio)
395BIO *bio;
396 {
397 return(bio->retry_reason);
398 }
399
400BIO *BIO_find_type(bio,type)
401BIO *bio;
402int type;
403 {
404 int mt,mask;
405
406 mask=type&0xff;
407 do {
408 if (bio->method != NULL)
409 {
410 mt=bio->method->type;
411
412 if (!mask)
413 {
414 if (mt & type) return(bio);
415 }
416 else if (mt == type)
417 return(bio);
418 }
419 bio=bio->next_bio;
420 } while (bio != NULL);
421 return(NULL);
422 }
423
424void BIO_free_all(bio)
425BIO *bio;
426 {
427 BIO *b;
428 int ref;
429
430 while (bio != NULL)
431 {
432 b=bio;
433 ref=b->references;
434 bio=bio->next_bio;
435 BIO_free(b);
436 /* Since ref count > 1, don't free anyone else. */
437 if (ref > 1) break;
438 }
439 }
440
441BIO *BIO_dup_chain(in)
442BIO *in;
443 {
444 BIO *ret=NULL,*eoc=NULL,*bio,*new;
445
446 for (bio=in; bio != NULL; bio=bio->next_bio)
447 {
448 if ((new=BIO_new(bio->method)) == NULL) goto err;
449 new->callback=bio->callback;
450 new->cb_arg=bio->cb_arg;
451 new->init=bio->init;
452 new->shutdown=bio->shutdown;
453 new->flags=bio->flags;
454
455 /* This will let SSL_s_sock() work with stdin/stdout */
456 new->num=bio->num;
457
458 if (!BIO_dup_state(bio,(char *)new))
459 {
460 BIO_free(new);
461 goto err;
462 }
463
464 /* copy app data */
465 if (!CRYPTO_dup_ex_data(bio_meth,&new->ex_data,&bio->ex_data))
466 goto err;
467
468 if (ret == NULL)
469 {
470 eoc=new;
471 ret=eoc;
472 }
473 else
474 {
475 BIO_push(eoc,new);
476 eoc=new;
477 }
478 }
479 return(ret);
480err:
481 if (ret != NULL)
482 BIO_free(ret);
483 return(NULL);
484 }
485
486void BIO_copy_next_retry(b)
487BIO *b;
488 {
489 BIO_set_flags(b,BIO_get_retry_flags(b->next_bio));
490 b->retry_reason=b->next_bio->retry_reason;
491 }
492
493int BIO_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
494long argl;
495char *argp;
496int (*new_func)();
497int (*dup_func)();
498void (*free_func)();
499 {
500 bio_meth_num++;
501 return(CRYPTO_get_ex_new_index(bio_meth_num-1,&bio_meth,
502 argl,argp,new_func,dup_func,free_func));
503 }
504
505int BIO_set_ex_data(bio,idx,data)
506BIO *bio;
507int idx;
508char *data;
509 {
510 return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
511 }
512
513char *BIO_get_ex_data(bio,idx)
514BIO *bio;
515int idx;
516 {
517 return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
518 }
519
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c
new file mode 100644
index 0000000000..e49902fa9f
--- /dev/null
+++ b/src/lib/libcrypto/bio/bss_acpt.c
@@ -0,0 +1,500 @@
1/* crypto/bio/bss_acpt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef NO_SOCK
60
61#include <stdio.h>
62#include <errno.h>
63#define USE_SOCKETS
64#include "cryptlib.h"
65#include "bio.h"
66
67/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */
68
69#ifdef WIN16
70#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
71#else
72#define SOCKET_PROTOCOL IPPROTO_TCP
73#endif
74
75typedef struct bio_accept_st
76 {
77 int state;
78 char *param_addr;
79
80 int accept_sock;
81 int accept_nbio;
82
83 char *addr;
84 int nbio;
85 BIO *bio_chain;
86 } BIO_ACCEPT;
87
88#ifndef NOPROTO
89static int acpt_write(BIO *h,char *buf,int num);
90static int acpt_read(BIO *h,char *buf,int size);
91static int acpt_puts(BIO *h,char *str);
92static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2);
93static int acpt_new(BIO *h);
94static int acpt_free(BIO *data);
95#else
96static int acpt_write();
97static int acpt_read();
98static int acpt_puts();
99static long acpt_ctrl();
100static int acpt_new();
101static int acpt_free();
102#endif
103
104#ifndef NOPROTO
105static int acpt_state(BIO *b, BIO_ACCEPT *c);
106static void acpt_close_socket(BIO *data);
107BIO_ACCEPT *BIO_ACCEPT_new(void );
108void BIO_ACCEPT_free(BIO_ACCEPT *a);
109
110#else
111
112static int acpt_state();
113static void acpt_close_socket();
114BIO_ACCEPT *BIO_ACCEPT_new();
115void BIO_ACCEPT_free();
116#endif
117
118#define ACPT_S_BEFORE 1
119#define ACPT_S_GET_ACCEPT_SOCKET 2
120#define ACPT_S_OK 3
121
122static BIO_METHOD methods_acceptp=
123 {
124 BIO_TYPE_ACCEPT,
125 "socket accept",
126 acpt_write,
127 acpt_read,
128 acpt_puts,
129 NULL, /* connect_gets, */
130 acpt_ctrl,
131 acpt_new,
132 acpt_free,
133 };
134
135BIO_METHOD *BIO_s_accept()
136 {
137 return(&methods_acceptp);
138 }
139
140static int acpt_new(bi)
141BIO *bi;
142 {
143 BIO_ACCEPT *ba;
144
145 bi->init=0;
146 bi->num=INVALID_SOCKET;
147 bi->flags=0;
148 if ((ba=BIO_ACCEPT_new()) == NULL)
149 return(0);
150 bi->ptr=(char *)ba;
151 ba->state=ACPT_S_BEFORE;
152 bi->shutdown=1;
153 return(1);
154 }
155
156BIO_ACCEPT *BIO_ACCEPT_new()
157 {
158 BIO_ACCEPT *ret;
159
160 if ((ret=(BIO_ACCEPT *)Malloc(sizeof(BIO_ACCEPT))) == NULL)
161 return(NULL);
162
163 memset(ret,0,sizeof(BIO_ACCEPT));
164 ret->accept_sock=INVALID_SOCKET;
165 return(ret);
166 }
167
168void BIO_ACCEPT_free(a)
169BIO_ACCEPT *a;
170 {
171 if (a->param_addr != NULL) Free(a->param_addr);
172 if (a->addr != NULL) Free(a->addr);
173 if (a->bio_chain != NULL) BIO_free(a->bio_chain);
174 Free(a);
175 }
176
177static void acpt_close_socket(bio)
178BIO *bio;
179 {
180 BIO_ACCEPT *c;
181
182 c=(BIO_ACCEPT *)bio->ptr;
183 if (c->accept_sock != INVALID_SOCKET)
184 {
185 shutdown(c->accept_sock,2);
186# ifdef WINDOWS
187 closesocket(c->accept_sock);
188# else
189 close(c->accept_sock);
190# endif
191 c->accept_sock=INVALID_SOCKET;
192 bio->num=INVALID_SOCKET;
193 }
194 }
195
196static int acpt_free(a)
197BIO *a;
198 {
199 BIO_ACCEPT *data;
200
201 if (a == NULL) return(0);
202 data=(BIO_ACCEPT *)a->ptr;
203
204 if (a->shutdown)
205 {
206 acpt_close_socket(a);
207 BIO_ACCEPT_free(data);
208 a->ptr=NULL;
209 a->flags=0;
210 a->init=0;
211 }
212 return(1);
213 }
214
215static int acpt_state(b,c)
216BIO *b;
217BIO_ACCEPT *c;
218 {
219 BIO *bio=NULL,*dbio;
220 unsigned long l=1;
221 int s= -1;
222 int i;
223
224again:
225 switch (c->state)
226 {
227 case ACPT_S_BEFORE:
228 if (c->param_addr == NULL)
229 {
230 BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
231 return(-1);
232 }
233 s=BIO_get_accept_socket(c->param_addr);
234 if (s == INVALID_SOCKET)
235 return(-1);
236
237#ifdef FIONBIO
238 if (c->accept_nbio)
239 {
240 i=BIO_socket_ioctl(b->num,FIONBIO,&l);
241 if (i < 0)
242 {
243#ifdef WINDOWS
244 closesocket(s);
245#else
246 close(s);
247# endif
248 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
249 return(-1);
250 }
251 }
252#endif
253 c->accept_sock=s;
254 b->num=s;
255 c->state=ACPT_S_GET_ACCEPT_SOCKET;
256 return(1);
257 break;
258 case ACPT_S_GET_ACCEPT_SOCKET:
259 if (b->next_bio != NULL)
260 {
261 c->state=ACPT_S_OK;
262 goto again;
263 }
264 i=BIO_accept(c->accept_sock,&(c->addr));
265 if (i < 0) return(i);
266 bio=BIO_new_socket(i,BIO_CLOSE);
267 if (bio == NULL) goto err;
268
269 BIO_set_callback(bio,BIO_get_callback(b));
270 BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
271
272#ifdef FIONBIO
273 if (c->nbio)
274 {
275 i=BIO_socket_ioctl(i,FIONBIO,&l);
276 if (i < 0)
277 {
278 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
279 goto err;
280 }
281 }
282#endif
283
284 /* If the accept BIO has an bio_chain, we dup it and
285 * put the new socket at the end. */
286 if (c->bio_chain != NULL)
287 {
288 if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL)
289 goto err;
290 if (!BIO_push(dbio,bio)) goto err;
291 bio=dbio;
292 }
293 if (BIO_push(b,bio) == NULL) goto err;
294
295 c->state=ACPT_S_OK;
296 return(1);
297err:
298 if (bio != NULL)
299 BIO_free(bio);
300 else if (s >= 0)
301 {
302#ifdef WINDOWS
303 closesocket(s);
304#else
305 close(s);
306# endif
307 }
308 return(0);
309 break;
310 case ACPT_S_OK:
311 if (b->next_bio == NULL)
312 {
313 c->state=ACPT_S_GET_ACCEPT_SOCKET;
314 goto again;
315 }
316 return(1);
317 break;
318 default:
319 return(0);
320 break;
321 }
322
323 }
324
325static int acpt_read(b,out,outl)
326BIO *b;
327char *out;
328int outl;
329 {
330 int ret=0;
331 BIO_ACCEPT *data;
332
333 BIO_clear_retry_flags(b);
334 data=(BIO_ACCEPT *)b->ptr;
335
336 while (b->next_bio == NULL)
337 {
338 ret=acpt_state(b,data);
339 if (ret <= 0) return(ret);
340 }
341
342 ret=BIO_read(b->next_bio,out,outl);
343 BIO_copy_next_retry(b);
344 return(ret);
345 }
346
347static int acpt_write(b,in,inl)
348BIO *b;
349char *in;
350int inl;
351 {
352 int ret;
353 BIO_ACCEPT *data;
354
355 BIO_clear_retry_flags(b);
356 data=(BIO_ACCEPT *)b->ptr;
357
358 while (b->next_bio == NULL)
359 {
360 ret=acpt_state(b,data);
361 if (ret <= 0) return(ret);
362 }
363
364 ret=BIO_write(b->next_bio,in,inl);
365 BIO_copy_next_retry(b);
366 return(ret);
367 }
368
369static long acpt_ctrl(b,cmd,num,ptr)
370BIO *b;
371int cmd;
372long num;
373char *ptr;
374 {
375 BIO *dbio;
376 int *ip;
377 long ret=1;
378 BIO_ACCEPT *data;
379 char **pp;
380
381 data=(BIO_ACCEPT *)b->ptr;
382
383 switch (cmd)
384 {
385 case BIO_CTRL_RESET:
386 ret=0;
387 data->state=ACPT_S_BEFORE;
388 acpt_close_socket(b);
389 b->flags=0;
390 break;
391 case BIO_C_DO_STATE_MACHINE:
392 /* use this one to start the connection */
393 ret=(long)acpt_state(b,data);
394 break;
395 case BIO_C_SET_ACCEPT:
396 if (ptr != NULL)
397 {
398 if (num == 0)
399 {
400 b->init=1;
401 if (data->param_addr != NULL)
402 Free(data->param_addr);
403 data->param_addr=BUF_strdup(ptr);
404 }
405 else if (num == 1)
406 {
407 data->accept_nbio=(ptr != NULL);
408 }
409 else if (num == 2)
410 {
411 if (data->bio_chain != NULL)
412 BIO_free(data->bio_chain);
413 data->bio_chain=(BIO *)ptr;
414 }
415 }
416 break;
417 case BIO_C_SET_NBIO:
418 data->nbio=(int)num;
419 break;
420 case BIO_C_GET_FD:
421 if (b->init)
422 {
423 ip=(int *)ptr;
424 if (ip != NULL)
425 *ip=data->accept_sock;
426 ret=b->num;
427 }
428 else
429 ret= -1;
430 break;
431 case BIO_C_GET_ACCEPT:
432 if (b->init)
433 {
434 if (ptr != NULL)
435 {
436 pp=(char **)ptr;
437 *pp=data->param_addr;
438 }
439 else
440 ret= -1;
441 }
442 else
443 ret= -1;
444 break;
445 case BIO_CTRL_GET_CLOSE:
446 ret=b->shutdown;
447 break;
448 case BIO_CTRL_SET_CLOSE:
449 b->shutdown=(int)num;
450 break;
451 case BIO_CTRL_PENDING:
452 case BIO_CTRL_WPENDING:
453 ret=0;
454 break;
455 case BIO_CTRL_FLUSH:
456 break;
457 case BIO_CTRL_DUP:
458 dbio=(BIO *)ptr;
459/* if (data->param_port) EAY EAY
460 BIO_set_port(dbio,data->param_port);
461 if (data->param_hostname)
462 BIO_set_hostname(dbio,data->param_hostname);
463 BIO_set_nbio(dbio,data->nbio); */
464 break;
465
466 default:
467 ret=0;
468 break;
469 }
470 return(ret);
471 }
472
473static int acpt_puts(bp,str)
474BIO *bp;
475char *str;
476 {
477 int n,ret;
478
479 n=strlen(str);
480 ret=acpt_write(bp,str,n);
481 return(ret);
482 }
483
484BIO *BIO_new_accept(str)
485char *str;
486 {
487 BIO *ret;
488
489 ret=BIO_new(BIO_s_accept());
490 if (ret == NULL) return(NULL);
491 if (BIO_set_accept_port(ret,str))
492 return(ret);
493 else
494 {
495 BIO_free(ret);
496 return(NULL);
497 }
498 }
499
500#endif
diff --git a/src/lib/libcrypto/bio/bss_conn.c b/src/lib/libcrypto/bio/bss_conn.c
new file mode 100644
index 0000000000..6e547bf866
--- /dev/null
+++ b/src/lib/libcrypto/bio/bss_conn.c
@@ -0,0 +1,648 @@
1/* crypto/bio/bss_conn.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef NO_SOCK
60
61#include <stdio.h>
62#include <errno.h>
63#define USE_SOCKETS
64#include "cryptlib.h"
65#include "bio.h"
66
67/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */
68
69#ifdef WIN16
70#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
71#else
72#define SOCKET_PROTOCOL IPPROTO_TCP
73#endif
74
75typedef struct bio_connect_st
76 {
77 int state;
78
79 char *param_hostname;
80 char *param_port;
81 int nbio;
82
83 unsigned char ip[4];
84 short port;
85
86 struct sockaddr_in them;
87
88 /* int socket; this will be kept in bio->num so that it is
89 * compatable with the bss_sock bio */
90 int error;
91
92 /* called when the connection is initially made
93 * callback(BIO,state,ret); The callback should return
94 * 'ret'. state is for compatablity with the ssl info_callback */
95 int (*info_callback)();
96 } BIO_CONNECT;
97
98#ifndef NOPROTO
99static int conn_write(BIO *h,char *buf,int num);
100static int conn_read(BIO *h,char *buf,int size);
101static int conn_puts(BIO *h,char *str);
102static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2);
103static int conn_new(BIO *h);
104static int conn_free(BIO *data);
105#else
106static int conn_write();
107static int conn_read();
108static int conn_puts();
109static long conn_ctrl();
110static int conn_new();
111static int conn_free();
112#endif
113
114#ifndef NOPROTO
115
116static int conn_state(BIO *b, BIO_CONNECT *c);
117static void conn_close_socket(BIO *data);
118BIO_CONNECT *BIO_CONNECT_new(void );
119void BIO_CONNECT_free(BIO_CONNECT *a);
120
121#else
122
123static int conn_state();
124static void conn_close_socket();
125BIO_CONNECT *BIO_CONNECT_new();
126void BIO_CONNECT_free();
127
128#endif
129
130static BIO_METHOD methods_connectp=
131 {
132 BIO_TYPE_CONNECT,
133 "socket connect",
134 conn_write,
135 conn_read,
136 conn_puts,
137 NULL, /* connect_gets, */
138 conn_ctrl,
139 conn_new,
140 conn_free,
141 };
142
143static int conn_state(b,c)
144BIO *b;
145BIO_CONNECT *c;
146 {
147 int ret= -1,i;
148 unsigned long l;
149 char *p,*q;
150 int (*cb)()=NULL;
151
152 if (c->info_callback != NULL)
153 cb=c->info_callback;
154
155 for (;;)
156 {
157 switch (c->state)
158 {
159 case BIO_CONN_S_BEFORE:
160 p=c->param_hostname;
161 if (p == NULL)
162 {
163 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTHNAME_SPECIFIED);
164 goto exit_loop;
165 }
166 for ( ; *p != '\0'; p++)
167 {
168 if ((*p == ':') || (*p == '/')) break;
169 }
170
171 i= *p;
172 if ((i == ':') || (i == '/'))
173 {
174
175 *(p++)='\0';
176 if (i == ':')
177 {
178 for (q=p; *q; q++)
179 if (*q == '/')
180 {
181 *q='\0';
182 break;
183 }
184 if (c->param_port != NULL)
185 Free(c->param_port);
186 c->param_port=BUF_strdup(p);
187 }
188 }
189
190 if (p == NULL)
191 {
192 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
193 ERR_add_error_data(2,"host=",c->param_hostname);
194 goto exit_loop;
195 }
196 c->state=BIO_CONN_S_GET_IP;
197 break;
198
199 case BIO_CONN_S_GET_IP:
200 if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
201 goto exit_loop;
202 c->state=BIO_CONN_S_GET_PORT;
203 break;
204
205 case BIO_CONN_S_GET_PORT:
206 if (BIO_get_port(c->param_port,&c->port) <= 0)
207 goto exit_loop;
208 c->state=BIO_CONN_S_CREATE_SOCKET;
209 break;
210
211 case BIO_CONN_S_CREATE_SOCKET:
212 /* now setup address */
213 memset((char *)&c->them,0,sizeof(c->them));
214 c->them.sin_family=AF_INET;
215 c->them.sin_port=htons((unsigned short)c->port);
216 l=(unsigned long)
217 ((unsigned long)c->ip[0]<<24L)|
218 ((unsigned long)c->ip[1]<<16L)|
219 ((unsigned long)c->ip[2]<< 8L)|
220 ((unsigned long)c->ip[3]);
221 c->them.sin_addr.s_addr=htonl(l);
222 c->state=BIO_CONN_S_CREATE_SOCKET;
223
224 ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
225 if (ret == INVALID_SOCKET)
226 {
227 SYSerr(SYS_F_SOCKET,get_last_socket_error());
228 ERR_add_error_data(4,"host=",c->param_hostname,
229 ":",c->param_port);
230 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
231 goto exit_loop;
232 }
233 b->num=ret;
234 c->state=BIO_CONN_S_NBIO;
235 break;
236
237 case BIO_CONN_S_NBIO:
238#ifdef FIONBIO
239 if (c->nbio)
240 {
241 l=1;
242 ret=BIO_socket_ioctl(b->num,FIONBIO,&l);
243 if (ret < 0)
244 {
245 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
246 ERR_add_error_data(4,"host=",
247 c->param_hostname,
248 ":",c->param_port);
249 goto exit_loop;
250 }
251 }
252#endif
253 c->state=BIO_CONN_S_CONNECT;
254
255#ifdef SO_KEEPALIVE
256 i=1;
257 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
258 if (i < 0)
259 {
260 SYSerr(SYS_F_SOCKET,get_last_socket_error());
261 ERR_add_error_data(4,"host=",c->param_hostname,
262 ":",c->param_port);
263 BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
264 goto exit_loop;
265 }
266#endif
267 break;
268
269 case BIO_CONN_S_CONNECT:
270 BIO_clear_retry_flags(b);
271 ret=connect(b->num,
272 (struct sockaddr *)&c->them,
273 sizeof(c->them));
274 b->retry_reason=0;
275 if (ret < 0)
276 {
277 if (BIO_sock_should_retry(ret))
278 {
279 BIO_set_retry_special(b);
280 c->state=BIO_CONN_S_BLOCKED_CONNECT;
281 b->retry_reason=BIO_RR_CONNECT;
282 }
283 else
284 {
285 SYSerr(SYS_F_CONNECT,get_last_socket_error());
286 ERR_add_error_data(4,"host=",
287 c->param_hostname,
288 ":",c->param_port);
289 BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
290 }
291 goto exit_loop;
292 }
293 else
294 c->state=BIO_CONN_S_OK;
295 break;
296
297 case BIO_CONN_S_BLOCKED_CONNECT:
298 i=BIO_sock_error(b->num);
299 if (i)
300 {
301 BIO_clear_retry_flags(b);
302 SYSerr(SYS_F_CONNECT,i);
303 ERR_add_error_data(4,"host=",
304 c->param_hostname,
305 ":",c->param_port);
306 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
307 ret=0;
308 goto exit_loop;
309 }
310 else
311 c->state=BIO_CONN_S_OK;
312 break;
313
314 case BIO_CONN_S_OK:
315 ret=1;
316 goto exit_loop;
317 default:
318 abort();
319 goto exit_loop;
320 }
321
322 if (cb != NULL)
323 {
324 if (!(ret=cb((BIO *)b,c->state,ret)))
325 goto end;
326 }
327 }
328
329 if (1)
330 {
331exit_loop:
332 if (cb != NULL)
333 ret=cb((BIO *)b,c->state,ret);
334 }
335end:
336 return(ret);
337 }
338
339BIO_CONNECT *BIO_CONNECT_new()
340 {
341 BIO_CONNECT *ret;
342
343 if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL)
344 return(NULL);
345 ret->state=BIO_CONN_S_BEFORE;
346 ret->param_hostname=NULL;
347 ret->param_port=NULL;
348 ret->info_callback=NULL;
349 ret->nbio=0;
350 ret->ip[0]=0;
351 ret->ip[1]=0;
352 ret->ip[2]=0;
353 ret->ip[3]=0;
354 ret->port=0;
355 memset((char *)&ret->them,0,sizeof(ret->them));
356 ret->error=0;
357 return(ret);
358 }
359
360void BIO_CONNECT_free(a)
361BIO_CONNECT *a;
362 {
363 if (a->param_hostname != NULL)
364 Free(a->param_hostname);
365 if (a->param_port != NULL)
366 Free(a->param_port);
367 Free(a);
368 }
369
370BIO_METHOD *BIO_s_connect()
371 {
372 return(&methods_connectp);
373 }
374
375static int conn_new(bi)
376BIO *bi;
377 {
378 bi->init=0;
379 bi->num=INVALID_SOCKET;
380 bi->flags=0;
381 if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
382 return(0);
383 else
384 return(1);
385 }
386
387static void conn_close_socket(bio)
388BIO *bio;
389 {
390 BIO_CONNECT *c;
391
392 c=(BIO_CONNECT *)bio->ptr;
393 if (bio->num != INVALID_SOCKET)
394 {
395 /* Only do a shutdown if things were established */
396 if (c->state == BIO_CONN_S_OK)
397 shutdown(bio->num,2);
398# ifdef WINDOWS
399 closesocket(bio->num);
400# else
401 close(bio->num);
402# endif
403 bio->num=INVALID_SOCKET;
404 }
405 }
406
407static int conn_free(a)
408BIO *a;
409 {
410 BIO_CONNECT *data;
411
412 if (a == NULL) return(0);
413 data=(BIO_CONNECT *)a->ptr;
414
415 if (a->shutdown)
416 {
417 conn_close_socket(a);
418 BIO_CONNECT_free(data);
419 a->ptr=NULL;
420 a->flags=0;
421 a->init=0;
422 }
423 return(1);
424 }
425
426static int conn_read(b,out,outl)
427BIO *b;
428char *out;
429int outl;
430 {
431 int ret=0;
432 BIO_CONNECT *data;
433
434 data=(BIO_CONNECT *)b->ptr;
435 if (data->state != BIO_CONN_S_OK)
436 {
437 ret=conn_state(b,data);
438 if (ret <= 0)
439 return(ret);
440 }
441
442 if (out != NULL)
443 {
444 clear_socket_error();
445#if defined(WINDOWS)
446 ret=recv(b->num,out,outl,0);
447#else
448 ret=read(b->num,out,outl);
449#endif
450 BIO_clear_retry_flags(b);
451 if (ret <= 0)
452 {
453 if (BIO_sock_should_retry(ret))
454 BIO_set_retry_read(b);
455 }
456 }
457 return(ret);
458 }
459
460static int conn_write(b,in,inl)
461BIO *b;
462char *in;
463int inl;
464 {
465 int ret;
466 BIO_CONNECT *data;
467
468 data=(BIO_CONNECT *)b->ptr;
469 if (data->state != BIO_CONN_S_OK)
470 {
471 ret=conn_state(b,data);
472 if (ret <= 0) return(ret);
473 }
474
475 clear_socket_error();
476#if defined(WINDOWS)
477 ret=send(b->num,in,inl,0);
478#else
479 ret=write(b->num,in,inl);
480#endif
481 BIO_clear_retry_flags(b);
482 if (ret <= 0)
483 {
484 if (BIO_sock_should_retry(ret))
485 BIO_set_retry_write(b);
486 }
487 return(ret);
488 }
489
490static long conn_ctrl(b,cmd,num,ptr)
491BIO *b;
492int cmd;
493long num;
494char *ptr;
495 {
496 BIO *dbio;
497 int *ip;
498 char **pptr;
499 long ret=1;
500 BIO_CONNECT *data;
501
502 data=(BIO_CONNECT *)b->ptr;
503
504 switch (cmd)
505 {
506 case BIO_CTRL_RESET:
507 ret=0;
508 data->state=BIO_CONN_S_BEFORE;
509 conn_close_socket(b);
510 b->flags=0;
511 break;
512 case BIO_C_DO_STATE_MACHINE:
513 /* use this one to start the connection */
514 if (!data->state != BIO_CONN_S_OK)
515 ret=(long)conn_state(b,data);
516 else
517 ret=1;
518 break;
519 case BIO_C_GET_CONNECT:
520 if (ptr != NULL)
521 {
522 pptr=(char **)ptr;
523 if (num == 0)
524 {
525 *pptr=data->param_hostname;
526
527 }
528 else if (num == 1)
529 {
530 *pptr=data->param_port;
531 }
532 else if (num == 2)
533 {
534 *pptr= (char *)&(data->ip[0]);
535 }
536 else if (num == 3)
537 {
538 *((int *)ptr)=data->port;
539 }
540 if ((!b->init) || (ptr == NULL))
541 *pptr="not initalised";
542 ret=1;
543 }
544 break;
545 case BIO_C_SET_CONNECT:
546 if (ptr != NULL)
547 {
548 b->init=1;
549 if (num == 0)
550 {
551 if (data->param_hostname != NULL)
552 Free(data->param_hostname);
553 data->param_hostname=BUF_strdup(ptr);
554 }
555 else if (num == 1)
556 {
557 if (data->param_port != NULL)
558 Free(data->param_port);
559 data->param_port=BUF_strdup(ptr);
560 }
561 else if (num == 2)
562 memcpy(data->ip,ptr,4);
563 else if (num == 3)
564 data->port= *(int *)ptr;
565 }
566 break;
567 case BIO_C_SET_NBIO:
568 data->nbio=(int)num;
569 break;
570 case BIO_C_GET_FD:
571 if (b->init)
572 {
573 ip=(int *)ptr;
574 if (ip != NULL)
575 *ip=b->num;
576 ret=b->num;
577 }
578 else
579 ret= -1;
580 break;
581 case BIO_CTRL_GET_CLOSE:
582 ret=b->shutdown;
583 break;
584 case BIO_CTRL_SET_CLOSE:
585 b->shutdown=(int)num;
586 break;
587 case BIO_CTRL_PENDING:
588 case BIO_CTRL_WPENDING:
589 ret=0;
590 break;
591 case BIO_CTRL_FLUSH:
592 break;
593 case BIO_CTRL_DUP:
594 dbio=(BIO *)ptr;
595 if (data->param_port)
596 BIO_set_conn_port(dbio,data->param_port);
597 if (data->param_hostname)
598 BIO_set_conn_hostname(dbio,data->param_hostname);
599 BIO_set_nbio(dbio,data->nbio);
600 BIO_set_info_callback(dbio,data->info_callback);
601 break;
602 case BIO_CTRL_SET_CALLBACK:
603 data->info_callback=(int (*)())ptr;
604 break;
605 case BIO_CTRL_GET_CALLBACK:
606 {
607 int (**fptr)();
608
609 fptr=(int (**)())ptr;
610 *fptr=data->info_callback;
611 }
612 break;
613 default:
614 ret=0;
615 break;
616 }
617 return(ret);
618 }
619
620static int conn_puts(bp,str)
621BIO *bp;
622char *str;
623 {
624 int n,ret;
625
626 n=strlen(str);
627 ret=conn_write(bp,str,n);
628 return(ret);
629 }
630
631BIO *BIO_new_connect(str)
632char *str;
633 {
634 BIO *ret;
635
636 ret=BIO_new(BIO_s_connect());
637 if (ret == NULL) return(NULL);
638 if (BIO_set_conn_hostname(ret,str))
639 return(ret);
640 else
641 {
642 BIO_free(ret);
643 return(NULL);
644 }
645 }
646
647#endif
648
diff --git a/src/lib/libcrypto/bio/bss_fd.c b/src/lib/libcrypto/bio/bss_fd.c
new file mode 100644
index 0000000000..686c4909a2
--- /dev/null
+++ b/src/lib/libcrypto/bio/bss_fd.c
@@ -0,0 +1,62 @@
1/* crypto/bio/bss_fd.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#define BIO_FD
60#include "bss_sock.c"
61#undef BIO_FD
62
diff --git a/src/lib/libcrypto/bio/bss_file.c b/src/lib/libcrypto/bio/bss_file.c
new file mode 100644
index 0000000000..1484cf849e
--- /dev/null
+++ b/src/lib/libcrypto/bio/bss_file.c
@@ -0,0 +1,339 @@
1/* crypto/bio/bss_file.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/*
60 * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout
61 * with binary data (e.g. asn1parse -inform DER < xxx) under
62 * Windows
63 */
64
65#ifndef HEADER_BSS_FILE_C
66#define HEADER_BSS_FILE_C
67
68#include <stdio.h>
69#include <errno.h>
70#include "cryptlib.h"
71#include "bio.h"
72#include "err.h"
73
74#if !defined(NO_STDIO)
75
76#ifndef NOPROTO
77static int MS_CALLBACK file_write(BIO *h,char *buf,int num);
78static int MS_CALLBACK file_read(BIO *h,char *buf,int size);
79static int MS_CALLBACK file_puts(BIO *h,char *str);
80static int MS_CALLBACK file_gets(BIO *h,char *str,int size);
81static long MS_CALLBACK file_ctrl(BIO *h,int cmd,long arg1,char *arg2);
82static int MS_CALLBACK file_new(BIO *h);
83static int MS_CALLBACK file_free(BIO *data);
84#else
85static int MS_CALLBACK file_write();
86static int MS_CALLBACK file_read();
87static int MS_CALLBACK file_puts();
88static int MS_CALLBACK file_gets();
89static long MS_CALLBACK file_ctrl();
90static int MS_CALLBACK file_new();
91static int MS_CALLBACK file_free();
92#endif
93
94static BIO_METHOD methods_filep=
95 {
96 BIO_TYPE_FILE,
97 "FILE pointer",
98 file_write,
99 file_read,
100 file_puts,
101 file_gets,
102 file_ctrl,
103 file_new,
104 file_free,
105 };
106
107BIO *BIO_new_file(filename,mode)
108char *filename;
109char *mode;
110 {
111 BIO *ret;
112 FILE *file;
113
114 if ((file=fopen(filename,mode)) == NULL)
115 {
116 SYSerr(SYS_F_FOPEN,get_last_sys_error());
117 ERR_add_error_data(5,"fopen('",filename,"','",mode,"')");
118 BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);
119 return(NULL);
120 }
121 if ((ret=BIO_new(BIO_s_file_internal())) == NULL)
122 return(NULL);
123
124 BIO_set_fp(ret,file,BIO_CLOSE);
125 return(ret);
126 }
127
128BIO *BIO_new_fp(stream,close_flag)
129FILE *stream;
130int close_flag;
131 {
132 BIO *ret;
133
134 if ((ret=BIO_new(BIO_s_file())) == NULL)
135 return(NULL);
136
137 BIO_set_fp(ret,stream,close_flag);
138 return(ret);
139 }
140
141BIO_METHOD *BIO_s_file()
142 {
143 return(&methods_filep);
144 }
145
146static int MS_CALLBACK file_new(bi)
147BIO *bi;
148 {
149 bi->init=0;
150 bi->num=0;
151 bi->ptr=NULL;
152 return(1);
153 }
154
155static int MS_CALLBACK file_free(a)
156BIO *a;
157 {
158 if (a == NULL) return(0);
159 if (a->shutdown)
160 {
161 if ((a->init) && (a->ptr != NULL))
162 {
163 fclose((FILE *)a->ptr);
164 a->ptr=NULL;
165 }
166 a->init=0;
167 }
168 return(1);
169 }
170
171static int MS_CALLBACK file_read(b,out,outl)
172BIO *b;
173char *out;
174int outl;
175 {
176 int ret=0;
177
178 if (b->init && (out != NULL))
179 {
180 ret=fread(out,1,(int)outl,(FILE *)b->ptr);
181 }
182 return(ret);
183 }
184
185static int MS_CALLBACK file_write(b,in,inl)
186BIO *b;
187char *in;
188int inl;
189 {
190 int ret=0;
191
192 if (b->init && (in != NULL))
193 {
194 if (fwrite(in,(int)inl,1,(FILE *)b->ptr))
195 ret=inl;
196 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
197 /* acording to Tim Hudson <tjh@cryptsoft.com>, the commented
198 * out version above can cause 'inl' write calls under
199 * some stupid stdio implementations (VMS) */
200 }
201 return(ret);
202 }
203
204static long MS_CALLBACK file_ctrl(b,cmd,num,ptr)
205BIO *b;
206int cmd;
207long num;
208char *ptr;
209 {
210 long ret=1;
211 FILE *fp=(FILE *)b->ptr;
212 FILE **fpp;
213 char p[4];
214
215 switch (cmd)
216 {
217 case BIO_CTRL_RESET:
218 ret=(long)fseek(fp,num,0);
219 break;
220 case BIO_CTRL_EOF:
221 ret=(long)feof(fp);
222 break;
223 case BIO_CTRL_INFO:
224 ret=ftell(fp);
225 break;
226 case BIO_C_SET_FILE_PTR:
227 file_free(b);
228 b->shutdown=(int)num;
229 b->ptr=(char *)ptr;
230 b->init=1;
231#if defined(MSDOS) || defined(WINDOWS)
232 /* Set correct text/binary mode */
233 if (num & BIO_FP_TEXT)
234 _setmode(fileno((FILE *)ptr),_O_TEXT);
235 else
236 _setmode(fileno((FILE *)ptr),_O_BINARY);
237#endif
238 break;
239 case BIO_C_SET_FILENAME:
240 file_free(b);
241 b->shutdown=(int)num&BIO_CLOSE;
242 if (num & BIO_FP_APPEND)
243 {
244 if (num & BIO_FP_READ)
245 strcpy(p,"a+");
246 else strcpy(p,"a");
247 }
248 else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
249 strcpy(p,"r+");
250 else if (num & BIO_FP_WRITE)
251 strcpy(p,"w");
252 else if (num & BIO_FP_READ)
253 strcpy(p,"r");
254 else
255 {
256 BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE);
257 ret=0;
258 break;
259 }
260#if defined(MSDOS) || defined(WINDOWS)
261 if (!(num & BIO_FP_TEXT))
262 strcat(p,"b");
263 else
264 strcat(p,"t");
265#endif
266 fp=fopen(ptr,p);
267 if (fp == NULL)
268 {
269 SYSerr(SYS_F_FOPEN,get_last_sys_error());
270 ERR_add_error_data(5,"fopen('",ptr,"','",p,"')");
271 BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB);
272 ret=0;
273 break;
274 }
275 b->ptr=(char *)fp;
276 b->init=1;
277 break;
278 case BIO_C_GET_FILE_PTR:
279 /* the ptr parameter is actually a FILE ** in this case. */
280 if (ptr != NULL)
281 {
282 fpp=(FILE **)ptr;
283 *fpp=(FILE *)b->ptr;
284 }
285 break;
286 case BIO_CTRL_GET_CLOSE:
287 ret=(long)b->shutdown;
288 break;
289 case BIO_CTRL_SET_CLOSE:
290 b->shutdown=(int)num;
291 break;
292 case BIO_CTRL_FLUSH:
293 fflush((FILE *)b->ptr);
294 break;
295 case BIO_CTRL_DUP:
296 ret=1;
297 break;
298
299 case BIO_CTRL_WPENDING:
300 case BIO_CTRL_PENDING:
301 case BIO_CTRL_PUSH:
302 case BIO_CTRL_POP:
303 default:
304 ret=0;
305 break;
306 }
307 return(ret);
308 }
309
310static int MS_CALLBACK file_gets(bp,buf,size)
311BIO *bp;
312char *buf;
313int size;
314 {
315 int ret=0;
316
317 buf[0]='\0';
318 fgets(buf,size,(FILE *)bp->ptr);
319 if (buf[0] != '\0')
320 ret=strlen(buf);
321 return(ret);
322 }
323
324static int MS_CALLBACK file_puts(bp,str)
325BIO *bp;
326char *str;
327 {
328 int n,ret;
329
330 n=strlen(str);
331 ret=file_write(bp,str,n);
332 return(ret);
333 }
334
335#endif /* NO_STDIO */
336
337#endif /* HEADER_BSS_FILE_C */
338
339
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c
new file mode 100644
index 0000000000..40c4e39f02
--- /dev/null
+++ b/src/lib/libcrypto/bio/bss_mem.c
@@ -0,0 +1,297 @@
1/* crypto/bio/bss_mem.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include "bio.h"
63
64#ifndef NOPROTO
65static int mem_write(BIO *h,char *buf,int num);
66static int mem_read(BIO *h,char *buf,int size);
67static int mem_puts(BIO *h,char *str);
68static int mem_gets(BIO *h,char *str,int size);
69static long mem_ctrl(BIO *h,int cmd,long arg1,char *arg2);
70static int mem_new(BIO *h);
71static int mem_free(BIO *data);
72#else
73static int mem_write();
74static int mem_read();
75static int mem_puts();
76static int mem_gets();
77static long mem_ctrl();
78static int mem_new();
79static int mem_free();
80#endif
81
82static BIO_METHOD mem_method=
83 {
84 BIO_TYPE_MEM,
85 "memory buffer",
86 mem_write,
87 mem_read,
88 mem_puts,
89 mem_gets,
90 mem_ctrl,
91 mem_new,
92 mem_free,
93 };
94
95BIO_METHOD *BIO_s_mem()
96 {
97 return(&mem_method);
98 }
99
100static int mem_new(bi)
101BIO *bi;
102 {
103 BUF_MEM *b;
104
105 if ((b=BUF_MEM_new()) == NULL)
106 return(0);
107 bi->shutdown=1;
108 bi->init=1;
109 bi->num=0;
110 bi->ptr=(char *)b;
111 return(1);
112 }
113
114static int mem_free(a)
115BIO *a;
116 {
117 if (a == NULL) return(0);
118 if (a->shutdown)
119 {
120 if ((a->init) && (a->ptr != NULL))
121 {
122 BUF_MEM_free((BUF_MEM *)a->ptr);
123 a->ptr=NULL;
124 }
125 }
126 return(1);
127 }
128
129static int mem_read(b,out,outl)
130BIO *b;
131char *out;
132int outl;
133 {
134 int ret= -1;
135 BUF_MEM *bm;
136 int i;
137 char *from,*to;
138
139 bm=(BUF_MEM *)b->ptr;
140 BIO_clear_retry_flags(b);
141 ret=(outl > bm->length)?bm->length:outl;
142 if ((out != NULL) && (ret > 0))
143 {
144 memcpy(out,bm->data,ret);
145 bm->length-=ret;
146 /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */
147 from=(char *)&(bm->data[ret]);
148 to=(char *)&(bm->data[0]);
149 for (i=0; i<bm->length; i++)
150 to[i]=from[i];
151 }
152 else if (bm->length == 0)
153 {
154 BIO_set_retry_read(b);
155 ret= -1;
156 }
157 return(ret);
158 }
159
160static int mem_write(b,in,inl)
161BIO *b;
162char *in;
163int inl;
164 {
165 int ret= -1;
166 int blen;
167 BUF_MEM *bm;
168
169 bm=(BUF_MEM *)b->ptr;
170 if (in == NULL)
171 {
172 BIOerr(BIO_F_MEM_WRITE,BIO_R_NULL_PARAMETER);
173 goto end;
174 }
175
176 BIO_clear_retry_flags(b);
177 blen=bm->length;
178 if (BUF_MEM_grow(bm,blen+inl) != (blen+inl))
179 goto end;
180 memcpy(&(bm->data[blen]),in,inl);
181 ret=inl;
182end:
183 return(ret);
184 }
185
186static long mem_ctrl(b,cmd,num,ptr)
187BIO *b;
188int cmd;
189long num;
190char *ptr;
191 {
192 long ret=1;
193 char **pptr;
194
195 BUF_MEM *bm=(BUF_MEM *)b->ptr;
196
197 switch (cmd)
198 {
199 case BIO_CTRL_RESET:
200 if (bm->data != NULL)
201 memset(bm->data,0,bm->max);
202 bm->length=0;
203 break;
204 case BIO_CTRL_EOF:
205 ret=(long)(bm->length == 0);
206 break;
207 case BIO_CTRL_INFO:
208 ret=(long)bm->length;
209 if (ptr != NULL)
210 {
211 pptr=(char **)ptr;
212 *pptr=(char *)&(bm->data[0]);
213 }
214 break;
215 case BIO_C_SET_BUF_MEM:
216 mem_free(b);
217 b->shutdown=(int)num;
218 b->ptr=ptr;
219 break;
220 case BIO_C_GET_BUF_MEM_PTR:
221 if (ptr != NULL)
222 {
223 pptr=(char **)ptr;
224 *pptr=(char *)bm;
225 }
226 break;
227 case BIO_CTRL_GET_CLOSE:
228 ret=(long)b->shutdown;
229 break;
230 case BIO_CTRL_SET_CLOSE:
231 b->shutdown=(int)num;
232 break;
233
234 case BIO_CTRL_WPENDING:
235 ret=0L;
236 break;
237 case BIO_CTRL_PENDING:
238 ret=(long)bm->length;
239 break;
240 case BIO_CTRL_DUP:
241 case BIO_CTRL_FLUSH:
242 ret=1;
243 break;
244 case BIO_CTRL_PUSH:
245 case BIO_CTRL_POP:
246 default:
247 ret=0;
248 break;
249 }
250 return(ret);
251 }
252
253static int mem_gets(bp,buf,size)
254BIO *bp;
255char *buf;
256int size;
257 {
258 int i,j;
259 int ret= -1;
260 char *p;
261 BUF_MEM *bm=(BUF_MEM *)bp->ptr;
262
263 BIO_clear_retry_flags(bp);
264 j=bm->length;
265 if (j <= 0) return(0);
266 p=bm->data;
267 for (i=0; i<j; i++)
268 {
269 if (p[i] == '\n') break;
270 }
271 if (i == j)
272 {
273 BIO_set_retry_read(bp);
274 /* return(-1); change the semantics 0.6.6a */
275 }
276 else
277 i++;
278 /* i is the max to copy */
279 if ((size-1) < i) i=size-1;
280 i=mem_read(bp,buf,i);
281 if (i > 0) buf[i]='\0';
282 ret=i;
283 return(ret);
284 }
285
286static int mem_puts(bp,str)
287BIO *bp;
288char *str;
289 {
290 int n,ret;
291
292 n=strlen(str);
293 ret=mem_write(bp,str,n);
294 /* memory semantics is that it will always work */
295 return(ret);
296 }
297
diff --git a/src/lib/libcrypto/bio/bss_null.c b/src/lib/libcrypto/bio/bss_null.c
new file mode 100644
index 0000000000..0791a2471a
--- /dev/null
+++ b/src/lib/libcrypto/bio/bss_null.c
@@ -0,0 +1,177 @@
1/* crypto/bio/bss_null.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include "bio.h"
63
64#ifndef NOPROTO
65static int null_write(BIO *h,char *buf,int num);
66static int null_read(BIO *h,char *buf,int size);
67static int null_puts(BIO *h,char *str);
68static int null_gets(BIO *h,char *str,int size);
69static long null_ctrl(BIO *h,int cmd,long arg1,char *arg2);
70static int null_new(BIO *h);
71static int null_free(BIO *data);
72#else
73static int null_write();
74static int null_read();
75static int null_puts();
76static int null_gets();
77static long null_ctrl();
78static int null_new();
79static int null_free();
80#endif
81
82static BIO_METHOD null_method=
83 {
84 BIO_TYPE_NULL,
85 "NULL",
86 null_write,
87 null_read,
88 null_puts,
89 null_gets,
90 null_ctrl,
91 null_new,
92 null_free,
93 };
94
95BIO_METHOD *BIO_s_null()
96 {
97 return(&null_method);
98 }
99
100static int null_new(bi)
101BIO *bi;
102 {
103 bi->init=1;
104 bi->num=0;
105 bi->ptr=(NULL);
106 return(1);
107 }
108
109static int null_free(a)
110BIO *a;
111 {
112 if (a == NULL) return(0);
113 return(1);
114 }
115
116static int null_read(b,out,outl)
117BIO *b;
118char *out;
119int outl;
120 {
121 return(0);
122 }
123
124static int null_write(b,in,inl)
125BIO *b;
126char *in;
127int inl;
128 {
129 return(inl);
130 }
131
132static long null_ctrl(b,cmd,num,ptr)
133BIO *b;
134int cmd;
135long num;
136char *ptr;
137 {
138 long ret=1;
139
140 switch (cmd)
141 {
142 case BIO_CTRL_RESET:
143 case BIO_CTRL_EOF:
144 case BIO_CTRL_SET:
145 case BIO_CTRL_SET_CLOSE:
146 case BIO_CTRL_FLUSH:
147 case BIO_CTRL_DUP:
148 ret=1;
149 break;
150 case BIO_CTRL_GET_CLOSE:
151 case BIO_CTRL_INFO:
152 case BIO_CTRL_GET:
153 case BIO_CTRL_PENDING:
154 case BIO_CTRL_WPENDING:
155 default:
156 ret=0;
157 break;
158 }
159 return(ret);
160 }
161
162static int null_gets(bp,buf,size)
163BIO *bp;
164char *buf;
165int size;
166 {
167 return(0);
168 }
169
170static int null_puts(bp,str)
171BIO *bp;
172char *str;
173 {
174 if (str == NULL) return(0);
175 return(strlen(str));
176 }
177
diff --git a/src/lib/libcrypto/bio/bss_sock.c b/src/lib/libcrypto/bio/bss_sock.c
new file mode 100644
index 0000000000..d907a2867b
--- /dev/null
+++ b/src/lib/libcrypto/bio/bss_sock.c
@@ -0,0 +1,461 @@
1/* crypto/bio/bss_sock.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#if !defined(NO_SOCK) || defined(BIO_FD)
60
61#include <stdio.h>
62#include <errno.h>
63#define USE_SOCKETS
64#include "cryptlib.h"
65#include "bio.h"
66
67#ifndef BIO_FD
68#ifndef NOPROTO
69static int sock_write(BIO *h,char *buf,int num);
70static int sock_read(BIO *h,char *buf,int size);
71static int sock_puts(BIO *h,char *str);
72static long sock_ctrl(BIO *h,int cmd,long arg1,char *arg2);
73static int sock_new(BIO *h);
74static int sock_free(BIO *data);
75int BIO_sock_should_retry(int s);
76#else
77static int sock_write();
78static int sock_read();
79static int sock_puts();
80static long sock_ctrl();
81static int sock_new();
82static int sock_free();
83int BIO_sock_should_retry();
84#endif
85
86#else
87
88#ifndef NOPROTO
89static int fd_write(BIO *h,char *buf,int num);
90static int fd_read(BIO *h,char *buf,int size);
91static int fd_puts(BIO *h,char *str);
92static long fd_ctrl(BIO *h,int cmd,long arg1,char *arg2);
93static int fd_new(BIO *h);
94static int fd_free(BIO *data);
95int BIO_fd_should_retry(int s);
96#else
97static int fd_write();
98static int fd_read();
99static int fd_puts();
100static long fd_ctrl();
101static int fd_new();
102static int fd_free();
103int BIO_fd_should_retry();
104#endif
105#endif
106
107#ifndef BIO_FD
108static BIO_METHOD methods_sockp=
109 {
110 BIO_TYPE_SOCKET,
111 "socket",
112 sock_write,
113 sock_read,
114 sock_puts,
115 NULL, /* sock_gets, */
116 sock_ctrl,
117 sock_new,
118 sock_free,
119 };
120
121BIO_METHOD *BIO_s_socket()
122 {
123 return(&methods_sockp);
124 }
125#else
126static BIO_METHOD methods_fdp=
127 {
128 BIO_TYPE_FD,"file descriptor",
129 fd_write,
130 fd_read,
131 fd_puts,
132 NULL, /* fd_gets, */
133 fd_ctrl,
134 fd_new,
135 fd_free,
136 };
137
138BIO_METHOD *BIO_s_fd()
139 {
140 return(&methods_fdp);
141 }
142#endif
143
144#ifndef BIO_FD
145BIO *BIO_new_socket(fd,close_flag)
146#else
147BIO *BIO_new_fd(fd,close_flag)
148#endif
149int fd;
150int close_flag;
151 {
152 BIO *ret;
153
154#ifndef BIO_FD
155 ret=BIO_new(BIO_s_socket());
156#else
157 ret=BIO_new(BIO_s_fd());
158#endif
159 if (ret == NULL) return(NULL);
160 BIO_set_fd(ret,fd,close_flag);
161 return(ret);
162 }
163
164#ifndef BIO_FD
165static int sock_new(bi)
166#else
167static int fd_new(bi)
168#endif
169BIO *bi;
170 {
171 bi->init=0;
172 bi->num=0;
173 bi->ptr=NULL;
174 bi->flags=0;
175 return(1);
176 }
177
178#ifndef BIO_FD
179static int sock_free(a)
180#else
181static int fd_free(a)
182#endif
183BIO *a;
184 {
185 if (a == NULL) return(0);
186 if (a->shutdown)
187 {
188 if (a->init)
189 {
190#ifndef BIO_FD
191 shutdown(a->num,2);
192# ifdef WINDOWS
193 closesocket(a->num);
194# else
195 close(a->num);
196# endif
197#else /* BIO_FD */
198 close(a->num);
199#endif
200
201 }
202 a->init=0;
203 a->flags=0;
204 }
205 return(1);
206 }
207
208#ifndef BIO_FD
209static int sock_read(b,out,outl)
210#else
211static int fd_read(b,out,outl)
212#endif
213BIO *b;
214char *out;
215int outl;
216 {
217 int ret=0;
218
219 if (out != NULL)
220 {
221#if defined(WINDOWS) && !defined(BIO_FD)
222 clear_socket_error();
223 ret=recv(b->num,out,outl,0);
224#else
225 clear_sys_error();
226 ret=read(b->num,out,outl);
227#endif
228 BIO_clear_retry_flags(b);
229 if (ret <= 0)
230 {
231#ifndef BIO_FD
232 if (BIO_sock_should_retry(ret))
233#else
234 if (BIO_fd_should_retry(ret))
235#endif
236 BIO_set_retry_read(b);
237 }
238 }
239 return(ret);
240 }
241
242#ifndef BIO_FD
243static int sock_write(b,in,inl)
244#else
245static int fd_write(b,in,inl)
246#endif
247BIO *b;
248char *in;
249int inl;
250 {
251 int ret;
252
253#if defined(WINDOWS) && !defined(BIO_FD)
254 clear_socket_error();
255 ret=send(b->num,in,inl,0);
256#else
257 clear_sys_error();
258 ret=write(b->num,in,inl);
259#endif
260 BIO_clear_retry_flags(b);
261 if (ret <= 0)
262 {
263#ifndef BIO_FD
264 if (BIO_sock_should_retry(ret))
265#else
266 if (BIO_fd_should_retry(ret))
267#endif
268 BIO_set_retry_write(b);
269 }
270 return(ret);
271 }
272
273#ifndef BIO_FD
274static long sock_ctrl(b,cmd,num,ptr)
275#else
276static long fd_ctrl(b,cmd,num,ptr)
277#endif
278BIO *b;
279int cmd;
280long num;
281char *ptr;
282 {
283 long ret=1;
284 int *ip;
285
286 switch (cmd)
287 {
288 case BIO_CTRL_RESET:
289#ifdef BIO_FD
290 ret=(long)lseek(b->num,0,0);
291#else
292 ret=0;
293#endif
294 break;
295 case BIO_CTRL_INFO:
296 ret=0;
297 break;
298 case BIO_C_SET_FD:
299#ifndef BIO_FD
300 sock_free(b);
301#else
302 fd_free(b);
303#endif
304 b->num= *((int *)ptr);
305 b->shutdown=(int)num;
306 b->init=1;
307 break;
308 case BIO_C_GET_FD:
309 if (b->init)
310 {
311 ip=(int *)ptr;
312 if (ip != NULL) *ip=b->num;
313 ret=b->num;
314 }
315 else
316 ret= -1;
317 break;
318 case BIO_CTRL_GET_CLOSE:
319 ret=b->shutdown;
320 break;
321 case BIO_CTRL_SET_CLOSE:
322 b->shutdown=(int)num;
323 break;
324 case BIO_CTRL_PENDING:
325 case BIO_CTRL_WPENDING:
326 ret=0;
327 break;
328 case BIO_CTRL_DUP:
329 case BIO_CTRL_FLUSH:
330 ret=1;
331 break;
332 break;
333 default:
334 ret=0;
335 break;
336 }
337 return(ret);
338 }
339
340#ifdef undef
341static int sock_gets(bp,buf,size)
342BIO *bp;
343char *buf;
344int size;
345 {
346 return(-1);
347 }
348#endif
349
350#ifndef BIO_FD
351static int sock_puts(bp,str)
352#else
353static int fd_puts(bp,str)
354#endif
355BIO *bp;
356char *str;
357 {
358 int n,ret;
359
360 n=strlen(str);
361#ifndef BIO_FD
362 ret=sock_write(bp,str,n);
363#else
364 ret=fd_write(bp,str,n);
365#endif
366 return(ret);
367 }
368
369#ifndef BIO_FD
370int BIO_sock_should_retry(i)
371#else
372int BIO_fd_should_retry(i)
373#endif
374int i;
375 {
376 int err;
377
378 if ((i == 0) || (i == -1))
379 {
380#if !defined(BIO_FD) && defined(WINDOWS)
381 err=get_last_socket_error();
382#else
383 err=get_last_sys_error();
384#endif
385
386#if defined(WINDOWS) /* more microsoft stupidity */
387 if ((i == -1) && (err == 0))
388 return(1);
389#endif
390
391#ifndef BIO_FD
392 return(BIO_sock_non_fatal_error(err));
393#else
394 return(BIO_fd_non_fatal_error(err));
395#endif
396 }
397 return(0);
398 }
399
400#ifndef BIO_FD
401int BIO_sock_non_fatal_error(err)
402#else
403int BIO_fd_non_fatal_error(err)
404#endif
405int err;
406 {
407 switch (err)
408 {
409#if !defined(BIO_FD) && defined(WINDOWS)
410# if defined(WSAEWOULDBLOCK)
411 case WSAEWOULDBLOCK:
412# endif
413
414# if defined(WSAENOTCONN)
415 case WSAENOTCONN:
416# endif
417#endif
418
419#ifdef EWOULDBLOCK
420# ifdef WSAEWOULDBLOCK
421# if WSAEWOULDBLOCK != EWOULDBLOCK
422 case EWOULDBLOCK:
423# endif
424# else
425 case EWOULDBLOCK:
426# endif
427#endif
428
429#if defined(ENOTCONN)
430 case ENOTCONN:
431#endif
432
433#ifdef EINTR
434 case EINTR:
435#endif
436
437#ifdef EAGAIN
438#if EWOULDBLOCK != EAGAIN
439 case EAGAIN:
440# endif
441#endif
442
443#ifdef EPROTO
444 case EPROTO:
445#endif
446
447#ifdef EINPROGRESS
448 case EINPROGRESS:
449#endif
450
451#ifdef EALREADY
452 case EALREADY:
453#endif
454 return(1);
455 break;
456 default:
457 break;
458 }
459 return(0);
460 }
461#endif
diff --git a/src/lib/libcrypto/bn/asm/bn-586.pl b/src/lib/libcrypto/bn/asm/bn-586.pl
new file mode 100644
index 0000000000..19d425ee96
--- /dev/null
+++ b/src/lib/libcrypto/bn/asm/bn-586.pl
@@ -0,0 +1,314 @@
1#!/usr/bin/perl
2#
3
4#!/usr/local/bin/perl
5
6push(@INC,"perlasm","../../perlasm");
7require "x86asm.pl";
8
9&asm_init($ARGV[0],"bn-586.pl");
10
11&bn_mul_add_words("bn_mul_add_words");
12&bn_mul_words("bn_mul_words");
13&bn_sqr_words("bn_sqr_words");
14&bn_div64("bn_div64");
15&bn_add_words("bn_add_words");
16
17&asm_finish();
18
19sub bn_mul_add_words
20 {
21 local($name)=@_;
22
23 &function_begin($name,"");
24
25 &comment("");
26 $Low="eax";
27 $High="edx";
28 $a="ebx";
29 $w="ebp";
30 $r="edi";
31 $c="esi";
32
33 &xor($c,$c); # clear carry
34 &mov($r,&wparam(0)); #
35
36 &mov("ecx",&wparam(2)); #
37 &mov($a,&wparam(1)); #
38
39 &and("ecx",0xfffffff8); # num / 8
40 &mov($w,&wparam(3)); #
41
42 &push("ecx"); # Up the stack for a tmp variable
43
44 &jz(&label("maw_finish"));
45
46 &set_label("maw_loop",0);
47
48 &mov(&swtmp(0),"ecx"); #
49
50 for ($i=0; $i<32; $i+=4)
51 {
52 &comment("Round $i");
53
54 &mov("eax",&DWP($i,$a,"",0)); # *a
55 &mul($w); # *a * w
56 &add("eax",$c); # L(t)+= *r
57 &mov($c,&DWP($i,$r,"",0)); # L(t)+= *r
58 &adc("edx",0); # H(t)+=carry
59 &add("eax",$c); # L(t)+=c
60 &adc("edx",0); # H(t)+=carry
61 &mov(&DWP($i,$r,"",0),"eax"); # *r= L(t);
62 &mov($c,"edx"); # c= H(t);
63 }
64
65 &comment("");
66 &mov("ecx",&swtmp(0)); #
67 &add($a,32);
68 &add($r,32);
69 &sub("ecx",8);
70 &jnz(&label("maw_loop"));
71
72 &set_label("maw_finish",0);
73 &mov("ecx",&wparam(2)); # get num
74 &and("ecx",7);
75 &jnz(&label("maw_finish2")); # helps branch prediction
76 &jmp(&label("maw_end"));
77
78 &set_label("maw_finish2",1);
79 for ($i=0; $i<7; $i++)
80 {
81 &comment("Tail Round $i");
82 &mov("eax",&DWP($i*4,$a,"",0));# *a
83 &mul($w); # *a * w
84 &add("eax",$c); # L(t)+=c
85 &mov($c,&DWP($i*4,$r,"",0)); # L(t)+= *r
86 &adc("edx",0); # H(t)+=carry
87 &add("eax",$c);
88 &adc("edx",0); # H(t)+=carry
89 &dec("ecx") if ($i != 7-1);
90 &mov(&DWP($i*4,$r,"",0),"eax"); # *r= L(t);
91 &mov($c,"edx"); # c= H(t);
92 &jz(&label("maw_end")) if ($i != 7-1);
93 }
94 &set_label("maw_end",0);
95 &mov("eax",$c);
96
97 &pop("ecx"); # clear variable from
98
99 &function_end($name);
100 }
101
102sub bn_mul_words
103 {
104 local($name)=@_;
105
106 &function_begin($name,"");
107
108 &comment("");
109 $Low="eax";
110 $High="edx";
111 $a="ebx";
112 $w="ecx";
113 $r="edi";
114 $c="esi";
115 $num="ebp";
116
117 &xor($c,$c); # clear carry
118 &mov($r,&wparam(0)); #
119 &mov($a,&wparam(1)); #
120 &mov($num,&wparam(2)); #
121 &mov($w,&wparam(3)); #
122
123 &and($num,0xfffffff8); # num / 8
124 &jz(&label("mw_finish"));
125
126 &set_label("mw_loop",0);
127 for ($i=0; $i<32; $i+=4)
128 {
129 &comment("Round $i");
130
131 &mov("eax",&DWP($i,$a,"",0)); # *a
132 &mul($w); # *a * w
133 &add("eax",$c); # L(t)+=c
134 # XXX
135
136 &adc("edx",0); # H(t)+=carry
137 &mov(&DWP($i,$r,"",0),"eax"); # *r= L(t);
138
139 &mov($c,"edx"); # c= H(t);
140 }
141
142 &comment("");
143 &add($a,32);
144 &add($r,32);
145 &sub($num,8);
146 &jz(&label("mw_finish"));
147 &jmp(&label("mw_loop"));
148
149 &set_label("mw_finish",0);
150 &mov($num,&wparam(2)); # get num
151 &and($num,7);
152 &jnz(&label("mw_finish2"));
153 &jmp(&label("mw_end"));
154
155 &set_label("mw_finish2",1);
156 for ($i=0; $i<7; $i++)
157 {
158 &comment("Tail Round $i");
159 &mov("eax",&DWP($i*4,$a,"",0));# *a
160 &mul($w); # *a * w
161 &add("eax",$c); # L(t)+=c
162 # XXX
163 &adc("edx",0); # H(t)+=carry
164 &mov(&DWP($i*4,$r,"",0),"eax");# *r= L(t);
165 &mov($c,"edx"); # c= H(t);
166 &dec($num) if ($i != 7-1);
167 &jz(&label("mw_end")) if ($i != 7-1);
168 }
169 &set_label("mw_end",0);
170 &mov("eax",$c);
171
172 &function_end($name);
173 }
174
175sub bn_sqr_words
176 {
177 local($name)=@_;
178
179 &function_begin($name,"");
180
181 &comment("");
182 $r="esi";
183 $a="edi";
184 $num="ebx";
185
186 &mov($r,&wparam(0)); #
187 &mov($a,&wparam(1)); #
188 &mov($num,&wparam(2)); #
189
190 &and($num,0xfffffff8); # num / 8
191 &jz(&label("sw_finish"));
192
193 &set_label("sw_loop",0);
194 for ($i=0; $i<32; $i+=4)
195 {
196 &comment("Round $i");
197 &mov("eax",&DWP($i,$a,"",0)); # *a
198 # XXX
199 &mul("eax"); # *a * *a
200 &mov(&DWP($i*2,$r,"",0),"eax"); #
201 &mov(&DWP($i*2+4,$r,"",0),"edx");#
202 }
203
204 &comment("");
205 &add($a,32);
206 &add($r,64);
207 &sub($num,8);
208 &jnz(&label("sw_loop"));
209
210 &set_label("sw_finish",0);
211 &mov($num,&wparam(2)); # get num
212 &and($num,7);
213 &jz(&label("sw_end"));
214
215 for ($i=0; $i<7; $i++)
216 {
217 &comment("Tail Round $i");
218 &mov("eax",&DWP($i*4,$a,"",0)); # *a
219 # XXX
220 &mul("eax"); # *a * *a
221 &mov(&DWP($i*8,$r,"",0),"eax"); #
222 &dec($num) if ($i != 7-1);
223 &mov(&DWP($i*8+4,$r,"",0),"edx");
224 &jz(&label("sw_end")) if ($i != 7-1);
225 }
226 &set_label("sw_end",0);
227
228 &function_end($name);
229 }
230
231sub bn_div64
232 {
233 local($name)=@_;
234
235 &function_begin($name,"");
236 &mov("edx",&wparam(0)); #
237 &mov("eax",&wparam(1)); #
238 &mov("ebx",&wparam(2)); #
239 &div("ebx");
240 &function_end($name);
241 }
242
243sub bn_add_words
244 {
245 local($name)=@_;
246
247 &function_begin($name,"");
248
249 &comment("");
250 $a="esi";
251 $b="edi";
252 $c="eax";
253 $r="ebx";
254 $tmp1="ecx";
255 $tmp2="edx";
256 $num="ebp";
257
258 &mov($r,&wparam(0)); # get r
259 &mov($a,&wparam(1)); # get a
260 &mov($b,&wparam(2)); # get b
261 &mov($num,&wparam(3)); # get num
262 &xor($c,$c); # clear carry
263 &and($num,0xfffffff8); # num / 8
264
265 &jz(&label("aw_finish"));
266
267 &set_label("aw_loop",0);
268 for ($i=0; $i<8; $i++)
269 {
270 &comment("Round $i");
271
272 &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
273 &mov($tmp2,&DWP($i*4,$b,"",0)); # *b
274 &add($tmp1,$c);
275 &mov($c,0);
276 &adc($c,$c);
277 &add($tmp1,$tmp2);
278 &adc($c,0);
279 &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
280 }
281
282 &comment("");
283 &add($a,32);
284 &add($b,32);
285 &add($r,32);
286 &sub($num,8);
287 &jnz(&label("aw_loop"));
288
289 &set_label("aw_finish",0);
290 &mov($num,&wparam(3)); # get num
291 &and($num,7);
292 &jz(&label("aw_end"));
293
294 for ($i=0; $i<7; $i++)
295 {
296 &comment("Tail Round $i");
297 &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
298 &mov($tmp2,&DWP($i*4,$b,"",0));# *b
299 &add($tmp1,$c);
300 &mov($c,0);
301 &adc($c,$c);
302 &add($tmp1,$tmp2);
303 &adc($c,0);
304 &dec($num) if ($i != 6);
305 &mov(&DWP($i*4,$r,"",0),$tmp1); # *a
306 &jz(&label("aw_end")) if ($i != 6);
307 }
308 &set_label("aw_end",0);
309
310 &mov("eax",$c);
311
312 &function_end($name);
313 }
314
diff --git a/src/lib/libcrypto/bn/asm/pa-risc2.s b/src/lib/libcrypto/bn/asm/pa-risc2.s
new file mode 100644
index 0000000000..c2725996a4
--- /dev/null
+++ b/src/lib/libcrypto/bn/asm/pa-risc2.s
@@ -0,0 +1,416 @@
1 .SPACE $PRIVATE$
2 .SUBSPA $DATA$,QUAD=1,ALIGN=8,ACCESS=31
3 .SUBSPA $BSS$,QUAD=1,ALIGN=8,ACCESS=31,ZERO,SORT=82
4 .SPACE $TEXT$
5 .SUBSPA $LIT$,QUAD=0,ALIGN=8,ACCESS=44
6 .SUBSPA $CODE$,QUAD=0,ALIGN=8,ACCESS=44,CODE_ONLY
7 .IMPORT $global$,DATA
8 .IMPORT $$dyncall,MILLICODE
9; gcc_compiled.:
10 .SPACE $TEXT$
11 .SUBSPA $CODE$
12
13 .align 4
14 .EXPORT bn_mul_add_words,ENTRY,PRIV_LEV=3,ARGW0=GR,ARGW1=GR,ARGW2=GR,ARGW3=GR,RTNVAL=GR
15bn_mul_add_words
16 .PROC
17 .CALLINFO FRAME=64,CALLS,SAVE_RP,ENTRY_GR=4
18 .ENTRY
19 stw %r2,-20(0,%r30)
20 stwm %r4,64(0,%r30)
21 copy %r24,%r31
22 stw %r3,-60(0,%r30)
23 ldi 0,%r20
24 ldo 12(%r26),%r2
25 stw %r23,-16(0,%r30)
26 copy %r25,%r3
27 ldo 12(%r3),%r1
28 fldws -16(0,%r30),%fr8L
29L$0010
30 copy %r20,%r25
31 ldi 0,%r24
32 fldws 0(0,%r3),%fr9L
33 ldw 0(0,%r26),%r19
34 xmpyu %fr8L,%fr9L,%fr9
35 fstds %fr9,-16(0,%r30)
36 copy %r19,%r23
37 ldw -16(0,%r30),%r28
38 ldw -12(0,%r30),%r29
39 ldi 0,%r22
40 add %r23,%r29,%r29
41 addc %r22,%r28,%r28
42 add %r25,%r29,%r29
43 addc %r24,%r28,%r28
44 copy %r28,%r21
45 ldi 0,%r20
46 copy %r21,%r20
47 addib,= -1,%r31,L$0011
48 stw %r29,0(0,%r26)
49 copy %r20,%r25
50 ldi 0,%r24
51 fldws -8(0,%r1),%fr9L
52 ldw -8(0,%r2),%r19
53 xmpyu %fr8L,%fr9L,%fr9
54 fstds %fr9,-16(0,%r30)
55 copy %r19,%r23
56 ldw -16(0,%r30),%r28
57 ldw -12(0,%r30),%r29
58 ldi 0,%r22
59 add %r23,%r29,%r29
60 addc %r22,%r28,%r28
61 add %r25,%r29,%r29
62 addc %r24,%r28,%r28
63 copy %r28,%r21
64 ldi 0,%r20
65 copy %r21,%r20
66 addib,= -1,%r31,L$0011
67 stw %r29,-8(0,%r2)
68 copy %r20,%r25
69 ldi 0,%r24
70 fldws -4(0,%r1),%fr9L
71 ldw -4(0,%r2),%r19
72 xmpyu %fr8L,%fr9L,%fr9
73 fstds %fr9,-16(0,%r30)
74 copy %r19,%r23
75 ldw -16(0,%r30),%r28
76 ldw -12(0,%r30),%r29
77 ldi 0,%r22
78 add %r23,%r29,%r29
79 addc %r22,%r28,%r28
80 add %r25,%r29,%r29
81 addc %r24,%r28,%r28
82 copy %r28,%r21
83 ldi 0,%r20
84 copy %r21,%r20
85 addib,= -1,%r31,L$0011
86 stw %r29,-4(0,%r2)
87 copy %r20,%r25
88 ldi 0,%r24
89 fldws 0(0,%r1),%fr9L
90 ldw 0(0,%r2),%r19
91 xmpyu %fr8L,%fr9L,%fr9
92 fstds %fr9,-16(0,%r30)
93 copy %r19,%r23
94 ldw -16(0,%r30),%r28
95 ldw -12(0,%r30),%r29
96 ldi 0,%r22
97 add %r23,%r29,%r29
98 addc %r22,%r28,%r28
99 add %r25,%r29,%r29
100 addc %r24,%r28,%r28
101 copy %r28,%r21
102 ldi 0,%r20
103 copy %r21,%r20
104 addib,= -1,%r31,L$0011
105 stw %r29,0(0,%r2)
106 ldo 16(%r1),%r1
107 ldo 16(%r3),%r3
108 ldo 16(%r2),%r2
109 bl L$0010,0
110 ldo 16(%r26),%r26
111L$0011
112 copy %r20,%r28
113 ldw -84(0,%r30),%r2
114 ldw -60(0,%r30),%r3
115 bv 0(%r2)
116 ldwm -64(0,%r30),%r4
117 .EXIT
118 .PROCEND
119 .align 4
120 .EXPORT bn_mul_words,ENTRY,PRIV_LEV=3,ARGW0=GR,ARGW1=GR,ARGW2=GR,ARGW3=GR,RTNVAL=GR
121bn_mul_words
122 .PROC
123 .CALLINFO FRAME=64,CALLS,SAVE_RP,ENTRY_GR=3
124 .ENTRY
125 stw %r2,-20(0,%r30)
126 copy %r25,%r2
127 stwm %r4,64(0,%r30)
128 copy %r24,%r19
129 ldi 0,%r28
130 stw %r23,-16(0,%r30)
131 ldo 12(%r26),%r31
132 ldo 12(%r2),%r29
133 fldws -16(0,%r30),%fr8L
134L$0026
135 fldws 0(0,%r2),%fr9L
136 xmpyu %fr8L,%fr9L,%fr9
137 fstds %fr9,-16(0,%r30)
138 copy %r28,%r21
139 ldi 0,%r20
140 ldw -16(0,%r30),%r24
141 ldw -12(0,%r30),%r25
142 add %r21,%r25,%r25
143 addc %r20,%r24,%r24
144 copy %r24,%r23
145 ldi 0,%r22
146 copy %r23,%r28
147 addib,= -1,%r19,L$0027
148 stw %r25,0(0,%r26)
149 fldws -8(0,%r29),%fr9L
150 xmpyu %fr8L,%fr9L,%fr9
151 fstds %fr9,-16(0,%r30)
152 copy %r28,%r21
153 ldi 0,%r20
154 ldw -16(0,%r30),%r24
155 ldw -12(0,%r30),%r25
156 add %r21,%r25,%r25
157 addc %r20,%r24,%r24
158 copy %r24,%r23
159 ldi 0,%r22
160 copy %r23,%r28
161 addib,= -1,%r19,L$0027
162 stw %r25,-8(0,%r31)
163 fldws -4(0,%r29),%fr9L
164 xmpyu %fr8L,%fr9L,%fr9
165 fstds %fr9,-16(0,%r30)
166 copy %r28,%r21
167 ldi 0,%r20
168 ldw -16(0,%r30),%r24
169 ldw -12(0,%r30),%r25
170 add %r21,%r25,%r25
171 addc %r20,%r24,%r24
172 copy %r24,%r23
173 ldi 0,%r22
174 copy %r23,%r28
175 addib,= -1,%r19,L$0027
176 stw %r25,-4(0,%r31)
177 fldws 0(0,%r29),%fr9L
178 xmpyu %fr8L,%fr9L,%fr9
179 fstds %fr9,-16(0,%r30)
180 copy %r28,%r21
181 ldi 0,%r20
182 ldw -16(0,%r30),%r24
183 ldw -12(0,%r30),%r25
184 add %r21,%r25,%r25
185 addc %r20,%r24,%r24
186 copy %r24,%r23
187 ldi 0,%r22
188 copy %r23,%r28
189 addib,= -1,%r19,L$0027
190 stw %r25,0(0,%r31)
191 ldo 16(%r29),%r29
192 ldo 16(%r2),%r2
193 ldo 16(%r31),%r31
194 bl L$0026,0
195 ldo 16(%r26),%r26
196L$0027
197 ldw -84(0,%r30),%r2
198 bv 0(%r2)
199 ldwm -64(0,%r30),%r4
200 .EXIT
201 .PROCEND
202 .align 4
203 .EXPORT bn_sqr_words,ENTRY,PRIV_LEV=3,ARGW0=GR,ARGW1=GR,ARGW2=GR
204bn_sqr_words
205 .PROC
206 .CALLINFO FRAME=0,NO_CALLS
207 .ENTRY
208 ldo 28(%r26),%r19
209 ldo 12(%r25),%r28
210L$0042
211 fldws 0(0,%r25),%fr8L
212 fldws 0(0,%r25),%fr8R
213 xmpyu %fr8L,%fr8R,%fr8
214 fstds %fr8,-16(0,%r30)
215 ldw -16(0,%r30),%r22
216 ldw -12(0,%r30),%r23
217 stw %r23,0(0,%r26)
218 copy %r22,%r21
219 ldi 0,%r20
220 addib,= -1,%r24,L$0049
221 stw %r21,-24(0,%r19)
222 fldws -8(0,%r28),%fr8L
223 fldws -8(0,%r28),%fr8R
224 xmpyu %fr8L,%fr8R,%fr8
225 fstds %fr8,-16(0,%r30)
226 ldw -16(0,%r30),%r22
227 ldw -12(0,%r30),%r23
228 stw %r23,-20(0,%r19)
229 copy %r22,%r21
230 ldi 0,%r20
231 addib,= -1,%r24,L$0049
232 stw %r21,-16(0,%r19)
233 fldws -4(0,%r28),%fr8L
234 fldws -4(0,%r28),%fr8R
235 xmpyu %fr8L,%fr8R,%fr8
236 fstds %fr8,-16(0,%r30)
237 ldw -16(0,%r30),%r22
238 ldw -12(0,%r30),%r23
239 stw %r23,-12(0,%r19)
240 copy %r22,%r21
241 ldi 0,%r20
242 addib,= -1,%r24,L$0049
243 stw %r21,-8(0,%r19)
244 fldws 0(0,%r28),%fr8L
245 fldws 0(0,%r28),%fr8R
246 xmpyu %fr8L,%fr8R,%fr8
247 fstds %fr8,-16(0,%r30)
248 ldw -16(0,%r30),%r22
249 ldw -12(0,%r30),%r23
250 stw %r23,-4(0,%r19)
251 copy %r22,%r21
252 ldi 0,%r20
253 addib,= -1,%r24,L$0049
254 stw %r21,0(0,%r19)
255 ldo 16(%r28),%r28
256 ldo 16(%r25),%r25
257 ldo 32(%r19),%r19
258 bl L$0042,0
259 ldo 32(%r26),%r26
260L$0049
261 bv,n 0(%r2)
262 .EXIT
263 .PROCEND
264 .IMPORT BN_num_bits_word,CODE
265 .IMPORT fprintf,CODE
266 .IMPORT __iob,DATA
267 .SPACE $TEXT$
268 .SUBSPA $LIT$
269
270 .align 4
271L$C0000
272 .STRING "Division would overflow (%d)\x0a\x00"
273 .IMPORT abort,CODE
274 .SPACE $TEXT$
275 .SUBSPA $CODE$
276
277 .align 4
278 .EXPORT bn_div64,ENTRY,PRIV_LEV=3,ARGW0=GR,ARGW1=GR,ARGW2=GR,RTNVAL=GR
279bn_div64
280 .PROC
281 .CALLINFO FRAME=128,CALLS,SAVE_RP,ENTRY_GR=8
282 .ENTRY
283 stw %r2,-20(0,%r30)
284 stwm %r8,128(0,%r30)
285 stw %r7,-124(0,%r30)
286 stw %r4,-112(0,%r30)
287 stw %r3,-108(0,%r30)
288 copy %r26,%r3
289 copy %r25,%r4
290 stw %r6,-120(0,%r30)
291 ldi 0,%r7
292 stw %r5,-116(0,%r30)
293 movb,<> %r24,%r5,L$0051
294 ldi 2,%r6
295 bl L$0068,0
296 ldi -1,%r28
297L$0051
298 .CALL ARGW0=GR
299 bl BN_num_bits_word,%r2
300 copy %r5,%r26
301 copy %r28,%r24
302 ldi 32,%r19
303 comb,= %r19,%r24,L$0052
304 subi 31,%r24,%r19
305 mtsar %r19
306 zvdepi 1,32,%r19
307 comb,>>= %r19,%r3,L$0052
308 addil LR'__iob-$global$+32,%r27
309 ldo RR'__iob-$global$+32(%r1),%r26
310 ldil LR'L$C0000,%r25
311 .CALL ARGW0=GR,ARGW1=GR,ARGW2=GR
312 bl fprintf,%r2
313 ldo RR'L$C0000(%r25),%r25
314 .CALL
315 bl abort,%r2
316 nop
317L$0052
318 comb,>> %r5,%r3,L$0053
319 subi 32,%r24,%r24
320 sub %r3,%r5,%r3
321L$0053
322 comib,= 0,%r24,L$0054
323 subi 31,%r24,%r19
324 mtsar %r19
325 zvdep %r5,32,%r5
326 zvdep %r3,32,%r21
327 subi 32,%r24,%r20
328 mtsar %r20
329 vshd 0,%r4,%r20
330 or %r21,%r20,%r3
331 mtsar %r19
332 zvdep %r4,32,%r4
333L$0054
334 extru %r5,15,16,%r23
335 extru %r5,31,16,%r28
336L$0055
337 extru %r3,15,16,%r19
338 comb,<> %r23,%r19,L$0058
339 copy %r3,%r26
340 bl L$0059,0
341 zdepi -1,31,16,%r29
342L$0058
343 .IMPORT $$divU,MILLICODE
344 bl $$divU,%r31
345 copy %r23,%r25
346L$0059
347 stw %r29,-16(0,%r30)
348 fldws -16(0,%r30),%fr10L
349 stw %r28,-16(0,%r30)
350 fldws -16(0,%r30),%fr10R
351 stw %r23,-16(0,%r30)
352 xmpyu %fr10L,%fr10R,%fr8
353 fldws -16(0,%r30),%fr10R
354 fstws %fr8R,-16(0,%r30)
355 xmpyu %fr10L,%fr10R,%fr9
356 ldw -16(0,%r30),%r8
357 fstws %fr9R,-16(0,%r30)
358 copy %r8,%r22
359 ldw -16(0,%r30),%r8
360 extru %r4,15,16,%r24
361 copy %r8,%r21
362L$0060
363 sub %r3,%r21,%r20
364 copy %r20,%r19
365 depi 0,31,16,%r19
366 comib,<> 0,%r19,L$0061
367 zdep %r20,15,16,%r19
368 addl %r19,%r24,%r19
369 comb,>>= %r19,%r22,L$0061
370 sub %r22,%r28,%r22
371 sub %r21,%r23,%r21
372 bl L$0060,0
373 ldo -1(%r29),%r29
374L$0061
375 stw %r29,-16(0,%r30)
376 fldws -16(0,%r30),%fr10L
377 stw %r28,-16(0,%r30)
378 fldws -16(0,%r30),%fr10R
379 xmpyu %fr10L,%fr10R,%fr8
380 fstws %fr8R,-16(0,%r30)
381 ldw -16(0,%r30),%r8
382 stw %r23,-16(0,%r30)
383 fldws -16(0,%r30),%fr10R
384 copy %r8,%r19
385 xmpyu %fr10L,%fr10R,%fr8
386 fstws %fr8R,-16(0,%r30)
387 extru %r19,15,16,%r20
388 ldw -16(0,%r30),%r8
389 zdep %r19,15,16,%r19
390 addl %r8,%r20,%r20
391 comclr,<<= %r19,%r4,0
392 addi 1,%r20,%r20
393 comb,<<= %r20,%r3,L$0066
394 sub %r4,%r19,%r4
395 addl %r3,%r5,%r3
396 ldo -1(%r29),%r29
397L$0066
398 addib,= -1,%r6,L$0056
399 sub %r3,%r20,%r3
400 zdep %r29,15,16,%r7
401 shd %r3,%r4,16,%r3
402 bl L$0055,0
403 zdep %r4,15,16,%r4
404L$0056
405 or %r7,%r29,%r28
406L$0068
407 ldw -148(0,%r30),%r2
408 ldw -124(0,%r30),%r7
409 ldw -120(0,%r30),%r6
410 ldw -116(0,%r30),%r5
411 ldw -112(0,%r30),%r4
412 ldw -108(0,%r30),%r3
413 bv 0(%r2)
414 ldwm -128(0,%r30),%r8
415 .EXIT
416 .PROCEND
diff --git a/src/lib/libcrypto/bn/bn_add.c b/src/lib/libcrypto/bn/bn_add.c
new file mode 100644
index 0000000000..efb2e312e8
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_add.c
@@ -0,0 +1,167 @@
1/* crypto/bn/bn_add.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63/* r can == a or b */
64int BN_add(r, a, b)
65BIGNUM *r;
66BIGNUM *a;
67BIGNUM *b;
68 {
69 int i;
70 BIGNUM *tmp;
71
72 /* a + b a+b
73 * a + -b a-b
74 * -a + b b-a
75 * -a + -b -(a+b)
76 */
77 if (a->neg ^ b->neg)
78 {
79 /* only one is negative */
80 if (a->neg)
81 { tmp=a; a=b; b=tmp; }
82
83 /* we are now a - b */
84
85 if (BN_ucmp(a,b) < 0)
86 {
87 if (bn_wexpand(r,b->top) == NULL) return(0);
88 bn_qsub(r,b,a);
89 r->neg=1;
90 }
91 else
92 {
93 if (bn_wexpand(r,a->top) == NULL) return(0);
94 bn_qsub(r,a,b);
95 r->neg=0;
96 }
97 return(1);
98 }
99
100 if (a->neg) /* both are neg */
101 r->neg=1;
102 else
103 r->neg=0;
104
105 i=(a->top > b->top);
106
107 if (i)
108 {
109 if (bn_wexpand(r,a->top+1) == NULL) return(0);
110 bn_qadd(r,a,b);
111 }
112 else
113 {
114 if (bn_wexpand(r,b->top+1) == NULL) return(0);
115 bn_qadd(r,b,a);
116 }
117 return(1);
118 }
119
120/* unsigned add of b to a, r must be large enough */
121void bn_qadd(r,a,b)
122BIGNUM *r;
123BIGNUM *a;
124BIGNUM *b;
125 {
126 register int i;
127 int max,min;
128 BN_ULONG *ap,*bp,*rp,carry,t1;
129
130 max=a->top;
131 min=b->top;
132 r->top=max;
133
134 ap=a->d;
135 bp=b->d;
136 rp=r->d;
137 carry=0;
138
139 carry=bn_add_words(rp,ap,bp,min);
140 rp+=min;
141 ap+=min;
142 bp+=min;
143 i=min;
144
145 if (carry)
146 {
147 while (i < max)
148 {
149 i++;
150 t1= *(ap++);
151 if ((*(rp++)=(t1+1)&BN_MASK2) >= t1)
152 {
153 carry=0;
154 break;
155 }
156 }
157 if ((i >= max) && carry)
158 {
159 *(rp++)=1;
160 r->top++;
161 }
162 }
163 for (; i<max; i++)
164 *(rp++)= *(ap++);
165 /* memcpy(rp,ap,sizeof(*ap)*(max-i));*/
166 }
167
diff --git a/src/lib/libcrypto/bn/bn_blind.c b/src/lib/libcrypto/bn/bn_blind.c
new file mode 100644
index 0000000000..a7b34f0bf0
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_blind.c
@@ -0,0 +1,143 @@
1/* crypto/bn/bn_blind.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63BN_BLINDING *BN_BLINDING_new(A,Ai,mod)
64BIGNUM *A;
65BIGNUM *Ai;
66BIGNUM *mod;
67 {
68 BN_BLINDING *ret=NULL;
69
70 if ((ret=(BN_BLINDING *)Malloc(sizeof(BN_BLINDING))) == NULL)
71 BNerr(BN_F_BN_BLINDING_NEW,ERR_R_MALLOC_FAILURE);
72 memset(ret,0,sizeof(BN_BLINDING));
73 if ((ret->A=BN_new()) == NULL) goto err;
74 if ((ret->Ai=BN_new()) == NULL) goto err;
75 if (!BN_copy(ret->A,A)) goto err;
76 if (!BN_copy(ret->Ai,Ai)) goto err;
77 ret->mod=mod;
78 return(ret);
79err:
80 if (ret != NULL) BN_BLINDING_free(ret);
81 return(ret);
82 }
83
84void BN_BLINDING_free(r)
85BN_BLINDING *r;
86 {
87 if (r->A != NULL) BN_free(r->A );
88 if (r->Ai != NULL) BN_free(r->Ai);
89 Free(r);
90 }
91
92int BN_BLINDING_update(b,ctx)
93BN_BLINDING *b;
94BN_CTX *ctx;
95 {
96 int ret=0;
97
98 if ((b->A == NULL) || (b->Ai == NULL))
99 {
100 BNerr(BN_F_BN_BLINDING_UPDATE,BN_R_NOT_INITALISED);
101 goto err;
102 }
103
104 if (!BN_mod_mul(b->A,b->A,b->A,b->mod,ctx)) goto err;
105 if (!BN_mod_mul(b->Ai,b->Ai,b->Ai,b->mod,ctx)) goto err;
106
107 ret=1;
108err:
109 return(ret);
110 }
111
112int BN_BLINDING_convert(n,b,ctx)
113BIGNUM *n;
114BN_BLINDING *b;
115BN_CTX *ctx;
116 {
117 if ((b->A == NULL) || (b->Ai == NULL))
118 {
119 BNerr(BN_F_BN_BLINDING_CONVERT,BN_R_NOT_INITALISED);
120 return(0);
121 }
122 return(BN_mod_mul(n,n,b->A,b->mod,ctx));
123 }
124
125int BN_BLINDING_invert(n,b,ctx)
126BIGNUM *n;
127BN_BLINDING *b;
128BN_CTX *ctx;
129 {
130 int ret;
131 if ((b->A == NULL) || (b->Ai == NULL))
132 {
133 BNerr(BN_F_BN_BLINDING_INVERT,BN_R_NOT_INITALISED);
134 return(0);
135 }
136 if ((ret=BN_mod_mul(n,n,b->Ai,b->mod,ctx)) >= 0)
137 {
138 if (!BN_BLINDING_update(b,ctx))
139 return(0);
140 }
141 return(ret);
142 }
143
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
new file mode 100644
index 0000000000..2263bdc7da
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -0,0 +1,286 @@
1/* crypto/bn/bn_div.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63/* The old slow way */
64#if 0
65int BN_div(dv, rem, m, d,ctx)
66BIGNUM *dv;
67BIGNUM *rem;
68BIGNUM *m;
69BIGNUM *d;
70BN_CTX *ctx;
71 {
72 int i,nm,nd;
73 BIGNUM *D;
74
75 if (BN_is_zero(d))
76 {
77 BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);
78 return(0);
79 }
80
81 if (BN_ucmp(m,d) < 0)
82 {
83 if (rem != NULL)
84 { if (BN_copy(rem,m) == NULL) return(0); }
85 if (dv != NULL) BN_zero(dv);
86 return(1);
87 }
88
89 D=ctx->bn[ctx->tos];
90 if (dv == NULL) dv=ctx->bn[ctx->tos+1];
91 if (rem == NULL) rem=ctx->bn[ctx->tos+2];
92
93 nd=BN_num_bits(d);
94 nm=BN_num_bits(m);
95 if (BN_copy(D,d) == NULL) return(0);
96 if (BN_copy(rem,m) == NULL) return(0);
97
98 /* The next 2 are needed so we can do a dv->d[0]|=1 later
99 * since BN_lshift1 will only work once there is a value :-) */
100 BN_zero(dv);
101 dv->top=1;
102
103 if (!BN_lshift(D,D,nm-nd)) return(0);
104 for (i=nm-nd; i>=0; i--)
105 {
106 if (!BN_lshift1(dv,dv)) return(0);
107 if (BN_ucmp(rem,D) >= 0)
108 {
109 dv->d[0]|=1;
110 bn_qsub(rem,rem,D);
111 }
112/* CAN IMPROVE (and have now :=) */
113 if (!BN_rshift1(D,D)) return(0);
114 }
115 rem->neg=BN_is_zero(rem)?0:m->neg;
116 dv->neg=m->neg^d->neg;
117 return(1);
118 }
119
120#else
121
122int BN_div(dv, rm, num, divisor,ctx)
123BIGNUM *dv;
124BIGNUM *rm;
125BIGNUM *num;
126BIGNUM *divisor;
127BN_CTX *ctx;
128 {
129 int norm_shift,i,j,loop;
130 BIGNUM *tmp,wnum,*snum,*sdiv,*res;
131 BN_ULONG *resp,*wnump;
132 BN_ULONG d0,d1;
133 int num_n,div_n;
134
135 if (BN_is_zero(divisor))
136 {
137 BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);
138 return(0);
139 }
140
141 if (BN_ucmp(num,divisor) < 0)
142 {
143 if (rm != NULL)
144 { if (BN_copy(rm,num) == NULL) return(0); }
145 if (dv != NULL) BN_zero(dv);
146 return(1);
147 }
148
149 tmp=ctx->bn[ctx->tos];
150 tmp->neg=0;
151 snum=ctx->bn[ctx->tos+1];
152 sdiv=ctx->bn[ctx->tos+2];
153 if (dv == NULL)
154 res=ctx->bn[ctx->tos+3];
155 else res=dv;
156
157 /* First we normalise the numbers */
158 norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);
159 BN_lshift(sdiv,divisor,norm_shift);
160 sdiv->neg=0;
161 norm_shift+=BN_BITS2;
162 BN_lshift(snum,num,norm_shift);
163 snum->neg=0;
164 div_n=sdiv->top;
165 num_n=snum->top;
166 loop=num_n-div_n;
167
168 /* Lets setup a 'window' into snum
169 * This is the part that corresponds to the current
170 * 'area' being divided */
171 wnum.d= &(snum->d[loop]);
172 wnum.top= div_n;
173 wnum.max= snum->max; /* a bit of a lie */
174 wnum.neg= 0;
175
176 /* Get the top 2 words of sdiv */
177 /* i=sdiv->top; */
178 d0=sdiv->d[div_n-1];
179 d1=(div_n == 1)?0:sdiv->d[div_n-2];
180
181 /* pointer to the 'top' of snum */
182 wnump= &(snum->d[num_n-1]);
183
184 /* Setup to 'res' */
185 res->neg= (num->neg^divisor->neg);
186 res->top=loop;
187 if (!bn_wexpand(res,(loop+1))) goto err;
188 resp= &(res->d[loop-1]);
189
190 /* space for temp */
191 if (!bn_wexpand(tmp,(div_n+1))) goto err;
192
193 if (BN_ucmp(&wnum,sdiv) >= 0)
194 {
195 bn_qsub(&wnum,&wnum,sdiv);
196 *resp=1;
197 res->d[res->top-1]=1;
198 }
199 else
200 res->top--;
201 resp--;
202
203 for (i=0; i<loop-1; i++)
204 {
205 BN_ULONG q,n0,n1;
206 BN_ULONG l0;
207
208 wnum.d--; wnum.top++;
209 n0=wnump[0];
210 n1=wnump[-1];
211 if (n0 == d0)
212 q=BN_MASK2;
213 else
214 q=bn_div64(n0,n1,d0);
215 {
216#ifdef BN_LLONG
217 BN_ULLONG t1,t2,rem;
218 t1=((BN_ULLONG)n0<<BN_BITS2)|n1;
219 for (;;)
220 {
221 t2=(BN_ULLONG)d1*q;
222 rem=t1-(BN_ULLONG)q*d0;
223 if ((rem>>BN_BITS2) ||
224 (t2 <= ((BN_ULLONG)(rem<<BN_BITS2)+wnump[-2])))
225 break;
226 q--;
227 }
228#else
229 BN_ULONG t1l,t1h,t2l,t2h,t3l,t3h,ql,qh,t3t;
230 t1h=n0;
231 t1l=n1;
232 for (;;)
233 {
234 t2l=LBITS(d1); t2h=HBITS(d1);
235 ql =LBITS(q); qh =HBITS(q);
236 mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */
237
238 t3t=LBITS(d0); t3h=HBITS(d0);
239 mul64(t3t,t3h,ql,qh); /* t3=t1-(BN_ULLONG)q*d0; */
240 t3l=(t1l-t3t)&BN_MASK2;
241 if (t3l > t1l) t3h++;
242 t3h=(t1h-t3h)&BN_MASK2;
243
244 /*if ((t3>>BN_BITS2) ||
245 (t2 <= ((t3<<BN_BITS2)+wnump[-2])))
246 break; */
247 if (t3h) break;
248 if (t2h < t3l) break;
249 if ((t2h == t3l) && (t2l <= wnump[-2])) break;
250
251 q--;
252 }
253#endif
254 }
255 l0=bn_mul_words(tmp->d,sdiv->d,div_n,q);
256 tmp->d[div_n]=l0;
257 for (j=div_n+1; j>0; j--)
258 if (tmp->d[j-1]) break;
259 tmp->top=j;
260
261 j=wnum.top;
262 BN_sub(&wnum,&wnum,tmp);
263
264 snum->top=snum->top+wnum.top-j;
265
266 if (wnum.neg)
267 {
268 q--;
269 j=wnum.top;
270 BN_add(&wnum,&wnum,sdiv);
271 snum->top+=wnum.top-j;
272 }
273 *(resp--)=q;
274 wnump--;
275 }
276 if (rm != NULL)
277 {
278 BN_rshift(rm,snum,norm_shift);
279 rm->neg=num->neg;
280 }
281 return(1);
282err:
283 return(0);
284 }
285
286#endif
diff --git a/src/lib/libcrypto/bn/bn_err.c b/src/lib/libcrypto/bn/bn_err.c
new file mode 100644
index 0000000000..029ae810d5
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_err.c
@@ -0,0 +1,111 @@
1/* lib/bn/bn_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "bn.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA BN_str_functs[]=
65 {
66{ERR_PACK(0,BN_F_BN_BLINDING_CONVERT,0), "BN_BLINDING_convert"},
67{ERR_PACK(0,BN_F_BN_BLINDING_INVERT,0), "BN_BLINDING_invert"},
68{ERR_PACK(0,BN_F_BN_BLINDING_NEW,0), "BN_BLINDING_new"},
69{ERR_PACK(0,BN_F_BN_BLINDING_UPDATE,0), "BN_BLINDING_update"},
70{ERR_PACK(0,BN_F_BN_BN2DEC,0), "BN_bn2dec"},
71{ERR_PACK(0,BN_F_BN_BN2HEX,0), "BN_bn2hex"},
72{ERR_PACK(0,BN_F_BN_CTX_NEW,0), "BN_CTX_new"},
73{ERR_PACK(0,BN_F_BN_DIV,0), "BN_div"},
74{ERR_PACK(0,BN_F_BN_EXPAND2,0), "bn_expand2"},
75{ERR_PACK(0,BN_F_BN_MOD_EXP_MONT,0), "BN_mod_exp_mont"},
76{ERR_PACK(0,BN_F_BN_MOD_INVERSE,0), "BN_mod_inverse"},
77{ERR_PACK(0,BN_F_BN_MOD_MUL_RECIPROCAL,0), "BN_mod_mul_reciprocal"},
78{ERR_PACK(0,BN_F_BN_MPI2BN,0), "BN_mpi2bn"},
79{ERR_PACK(0,BN_F_BN_NEW,0), "BN_new"},
80{ERR_PACK(0,BN_F_BN_RAND,0), "BN_rand"},
81{0,NULL},
82 };
83
84static ERR_STRING_DATA BN_str_reasons[]=
85 {
86{BN_R_BAD_RECIPROCAL ,"bad reciprocal"},
87{BN_R_CALLED_WITH_EVEN_MODULUS ,"called with even modulus"},
88{BN_R_DIV_BY_ZERO ,"div by zero"},
89{BN_R_ENCODING_ERROR ,"encoding error"},
90{BN_R_INVALID_LENGTH ,"invalid length"},
91{BN_R_NOT_INITALISED ,"not initalised"},
92{BN_R_NO_INVERSE ,"no inverse"},
93{0,NULL},
94 };
95
96#endif
97
98void ERR_load_BN_strings()
99 {
100 static int init=1;
101
102 if (init);
103 {;
104 init=0;
105#ifndef NO_ERR
106 ERR_load_strings(ERR_LIB_BN,BN_str_functs);
107 ERR_load_strings(ERR_LIB_BN,BN_str_reasons);
108#endif
109
110 }
111 }
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c
new file mode 100644
index 0000000000..c056a5083f
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_exp.c
@@ -0,0 +1,553 @@
1/* crypto/bn/bn_exp.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63/* slow but works */
64int BN_mod_mul(ret, a, b, m, ctx)
65BIGNUM *ret;
66BIGNUM *a;
67BIGNUM *b;
68BIGNUM *m;
69BN_CTX *ctx;
70 {
71 BIGNUM *t;
72 int r=0;
73
74 t=ctx->bn[ctx->tos++];
75 if (a == b)
76 { if (!BN_sqr(t,a,ctx)) goto err; }
77 else
78 { if (!BN_mul(t,a,b)) goto err; }
79 if (!BN_mod(ret,t,m,ctx)) goto err;
80 r=1;
81err:
82 ctx->tos--;
83 return(r);
84 }
85
86#if 0
87/* this one works - simple but works */
88int BN_mod_exp(r,a,p,m,ctx)
89BIGNUM *r,*a,*p,*m;
90BN_CTX *ctx;
91 {
92 int i,bits,ret=0;
93 BIGNUM *v,*tmp;
94
95 v=ctx->bn[ctx->tos++];
96 tmp=ctx->bn[ctx->tos++];
97
98 if (BN_copy(v,a) == NULL) goto err;
99 bits=BN_num_bits(p);
100
101 if (BN_is_odd(p))
102 { if (BN_copy(r,a) == NULL) goto err; }
103 else { if (BN_one(r)) goto err; }
104
105 for (i=1; i<bits; i++)
106 {
107 if (!BN_sqr(tmp,v,ctx)) goto err;
108 if (!BN_mod(v,tmp,m,ctx)) goto err;
109 if (BN_is_bit_set(p,i))
110 {
111 if (!BN_mul(tmp,r,v)) goto err;
112 if (!BN_mod(r,tmp,m,ctx)) goto err;
113 }
114 }
115 ret=1;
116err:
117 ctx->tos-=2;
118 return(ret);
119 }
120
121#endif
122
123/* this one works - simple but works */
124int BN_exp(r,a,p,ctx)
125BIGNUM *r,*a,*p;
126BN_CTX *ctx;
127 {
128 int i,bits,ret=0;
129 BIGNUM *v,*tmp;
130
131 v=ctx->bn[ctx->tos++];
132 tmp=ctx->bn[ctx->tos++];
133
134 if (BN_copy(v,a) == NULL) goto err;
135 bits=BN_num_bits(p);
136
137 if (BN_is_odd(p))
138 { if (BN_copy(r,a) == NULL) goto err; }
139 else { if (BN_one(r)) goto err; }
140
141 for (i=1; i<bits; i++)
142 {
143 if (!BN_sqr(tmp,v,ctx)) goto err;
144 if (BN_is_bit_set(p,i))
145 {
146 if (!BN_mul(tmp,r,v)) goto err;
147 }
148 }
149 ret=1;
150err:
151 ctx->tos-=2;
152 return(ret);
153 }
154
155int BN_mod_exp(r,a,p,m,ctx)
156BIGNUM *r;
157BIGNUM *a;
158BIGNUM *p;
159BIGNUM *m;
160BN_CTX *ctx;
161 {
162 int ret;
163
164#ifdef MONT_MUL_MOD
165 /* I have finally been able to take out this pre-condition of
166 * the top bit being set. It was caused by an error in BN_div
167 * with negatives. There was also another problem when for a^b%m
168 * a >= m. eay 07-May-97 */
169/* if ((m->d[m->top-1]&BN_TBIT) && BN_is_odd(m)) */
170
171 if (BN_is_odd(m))
172 { ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }
173 else
174#endif
175#ifdef RECP_MUL_MOD
176 { ret=BN_mod_exp_recp(r,a,p,m,ctx); }
177#else
178 { ret=BN_mod_exp_simple(r,a,p,m,ctx); }
179#endif
180
181 return(ret);
182 }
183
184/* #ifdef RECP_MUL_MOD */
185int BN_mod_exp_recp(r,a,p,m,ctx)
186BIGNUM *r;
187BIGNUM *a;
188BIGNUM *p;
189BIGNUM *m;
190BN_CTX *ctx;
191 {
192 int nb,i,j,bits,ret=0,wstart,wend,window,wvalue;
193 int start=1;
194 BIGNUM *d,*aa;
195 BIGNUM *val[16];
196
197 d=ctx->bn[ctx->tos++];
198 aa=ctx->bn[ctx->tos++];
199 bits=BN_num_bits(p);
200
201 if (bits == 0)
202 {
203 BN_one(r);
204 return(1);
205 }
206 nb=BN_reciprocal(d,m,ctx);
207 if (nb == -1) goto err;
208
209 val[0]=BN_new();
210 if (!BN_mod(val[0],a,m,ctx)) goto err; /* 1 */
211 if (!BN_mod_mul_reciprocal(aa,val[0],val[0],m,d,nb,ctx))
212 goto err; /* 2 */
213
214 if (bits <= 17) /* This is probably 3 or 0x10001, so just do singles */
215 window=1;
216 else if (bits >= 256)
217 window=5; /* max size of window */
218 else if (bits >= 128)
219 window=4;
220 else
221 window=3;
222
223 j=1<<(window-1);
224 for (i=1; i<j; i++)
225 {
226 val[i]=BN_new();
227 if (!BN_mod_mul_reciprocal(val[i],val[i-1],aa,m,d,nb,ctx))
228 goto err;
229 }
230 for (; i<16; i++)
231 val[i]=NULL;
232
233 start=1; /* This is used to avoid multiplication etc
234 * when there is only the value '1' in the
235 * buffer. */
236 wvalue=0; /* The 'value' of the window */
237 wstart=bits-1; /* The top bit of the window */
238 wend=0; /* The bottom bit of the window */
239
240 if (!BN_one(r)) goto err;
241
242 for (;;)
243 {
244 if (BN_is_bit_set(p,wstart) == 0)
245 {
246 if (!start)
247 if (!BN_mod_mul_reciprocal(r,r,r,m,d,nb,ctx))
248 goto err;
249 if (wstart == 0) break;
250 wstart--;
251 continue;
252 }
253 /* We now have wstart on a 'set' bit, we now need to work out
254 * how bit a window to do. To do this we need to scan
255 * forward until the last set bit before the end of the
256 * window */
257 j=wstart;
258 wvalue=1;
259 wend=0;
260 for (i=1; i<window; i++)
261 {
262 if (wstart-i < 0) break;
263 if (BN_is_bit_set(p,wstart-i))
264 {
265 wvalue<<=(i-wend);
266 wvalue|=1;
267 wend=i;
268 }
269 }
270
271 /* wend is the size of the current window */
272 j=wend+1;
273 /* add the 'bytes above' */
274 if (!start)
275 for (i=0; i<j; i++)
276 {
277 if (!BN_mod_mul_reciprocal(r,r,r,m,d,nb,ctx))
278 goto err;
279 }
280
281 /* wvalue will be an odd number < 2^window */
282 if (!BN_mod_mul_reciprocal(r,r,val[wvalue>>1],m,d,nb,ctx))
283 goto err;
284
285 /* move the 'window' down further */
286 wstart-=wend+1;
287 wvalue=0;
288 start=0;
289 if (wstart < 0) break;
290 }
291 ret=1;
292err:
293 ctx->tos-=2;
294 for (i=0; i<16; i++)
295 if (val[i] != NULL) BN_clear_free(val[i]);
296 return(ret);
297 }
298/* #endif */
299
300/* #ifdef MONT_MUL_MOD */
301int BN_mod_exp_mont(r,a,p,m,ctx,in_mont)
302BIGNUM *r;
303BIGNUM *a;
304BIGNUM *p;
305BIGNUM *m;
306BN_CTX *ctx;
307BN_MONT_CTX *in_mont;
308 {
309#define TABLE_SIZE 16
310 int i,j,bits,ret=0,wstart,wend,window,wvalue;
311 int start=1;
312 BIGNUM *d,*aa;
313 BIGNUM *val[TABLE_SIZE];
314 BN_MONT_CTX *mont=NULL;
315
316 if (!(m->d[0] & 1))
317 {
318 BNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);
319 return(0);
320 }
321 d=ctx->bn[ctx->tos++];
322 bits=BN_num_bits(p);
323 if (bits == 0)
324 {
325 BN_one(r);
326 return(1);
327 }
328
329 /* If this is not done, things will break in the montgomery
330 * part */
331
332#if 1
333 if (in_mont != NULL)
334 mont=in_mont;
335 else
336#endif
337 {
338 if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
339 if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
340 }
341
342 val[0]=BN_new();
343 if (BN_ucmp(a,m) >= 0)
344 {
345 BN_mod(val[0],a,m,ctx);
346 aa=val[0];
347 }
348 else
349 aa=a;
350 if (!BN_to_montgomery(val[0],aa,mont,ctx)) goto err; /* 1 */
351 if (!BN_mod_mul_montgomery(d,val[0],val[0],mont,ctx)) goto err; /* 2 */
352
353 if (bits <= 20) /* This is probably 3 or 0x10001, so just do singles */
354 window=1;
355 else if (bits > 250)
356 window=5; /* max size of window */
357 else if (bits >= 120)
358 window=4;
359 else
360 window=3;
361
362 j=1<<(window-1);
363 for (i=1; i<j; i++)
364 {
365 val[i]=BN_new();
366 if (!BN_mod_mul_montgomery(val[i],val[i-1],d,mont,ctx))
367 goto err;
368 }
369 for (; i<TABLE_SIZE; i++)
370 val[i]=NULL;
371
372 start=1; /* This is used to avoid multiplication etc
373 * when there is only the value '1' in the
374 * buffer. */
375 wvalue=0; /* The 'value' of the window */
376 wstart=bits-1; /* The top bit of the window */
377 wend=0; /* The bottom bit of the window */
378
379 if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;
380 for (;;)
381 {
382 if (BN_is_bit_set(p,wstart) == 0)
383 {
384 if (!start)
385 {
386 if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
387 goto err;
388 }
389 if (wstart == 0) break;
390 wstart--;
391 continue;
392 }
393 /* We now have wstart on a 'set' bit, we now need to work out
394 * how bit a window to do. To do this we need to scan
395 * forward until the last set bit before the end of the
396 * window */
397 j=wstart;
398 wvalue=1;
399 wend=0;
400 for (i=1; i<window; i++)
401 {
402 if (wstart-i < 0) break;
403 if (BN_is_bit_set(p,wstart-i))
404 {
405 wvalue<<=(i-wend);
406 wvalue|=1;
407 wend=i;
408 }
409 }
410
411 /* wend is the size of the current window */
412 j=wend+1;
413 /* add the 'bytes above' */
414 if (!start)
415 for (i=0; i<j; i++)
416 {
417 if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
418 goto err;
419 }
420
421 /* wvalue will be an odd number < 2^window */
422 if (!BN_mod_mul_montgomery(r,r,val[wvalue>>1],mont,ctx))
423 goto err;
424
425 /* move the 'window' down further */
426 wstart-=wend+1;
427 wvalue=0;
428 start=0;
429 if (wstart < 0) break;
430 }
431 BN_from_montgomery(r,r,mont,ctx);
432 ret=1;
433err:
434 if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
435 ctx->tos--;
436 for (i=0; i<TABLE_SIZE; i++)
437 if (val[i] != NULL) BN_clear_free(val[i]);
438 return(ret);
439 }
440/* #endif */
441
442/* The old fallback, simple version :-) */
443int BN_mod_exp_simple(r,a,p,m,ctx)
444BIGNUM *r;
445BIGNUM *a;
446BIGNUM *p;
447BIGNUM *m;
448BN_CTX *ctx;
449 {
450 int i,j,bits,ret=0,wstart,wend,window,wvalue;
451 int start=1;
452 BIGNUM *d;
453 BIGNUM *val[16];
454
455 d=ctx->bn[ctx->tos++];
456 bits=BN_num_bits(p);
457
458 if (bits == 0)
459 {
460 BN_one(r);
461 return(1);
462 }
463
464 val[0]=BN_new();
465 if (!BN_mod(val[0],a,m,ctx)) goto err; /* 1 */
466 if (!BN_mod_mul(d,val[0],val[0],m,ctx))
467 goto err; /* 2 */
468
469 if (bits <= 17) /* This is probably 3 or 0x10001, so just do singles */
470 window=1;
471 else if (bits >= 256)
472 window=5; /* max size of window */
473 else if (bits >= 128)
474 window=4;
475 else
476 window=3;
477
478 j=1<<(window-1);
479 for (i=1; i<j; i++)
480 {
481 val[i]=BN_new();
482 if (!BN_mod_mul(val[i],val[i-1],d,m,ctx))
483 goto err;
484 }
485 for (; i<16; i++)
486 val[i]=NULL;
487
488 start=1; /* This is used to avoid multiplication etc
489 * when there is only the value '1' in the
490 * buffer. */
491 wvalue=0; /* The 'value' of the window */
492 wstart=bits-1; /* The top bit of the window */
493 wend=0; /* The bottom bit of the window */
494
495 if (!BN_one(r)) goto err;
496
497 for (;;)
498 {
499 if (BN_is_bit_set(p,wstart) == 0)
500 {
501 if (!start)
502 if (!BN_mod_mul(r,r,r,m,ctx))
503 goto err;
504 if (wstart == 0) break;
505 wstart--;
506 continue;
507 }
508 /* We now have wstart on a 'set' bit, we now need to work out
509 * how bit a window to do. To do this we need to scan
510 * forward until the last set bit before the end of the
511 * window */
512 j=wstart;
513 wvalue=1;
514 wend=0;
515 for (i=1; i<window; i++)
516 {
517 if (wstart-i < 0) break;
518 if (BN_is_bit_set(p,wstart-i))
519 {
520 wvalue<<=(i-wend);
521 wvalue|=1;
522 wend=i;
523 }
524 }
525
526 /* wend is the size of the current window */
527 j=wend+1;
528 /* add the 'bytes above' */
529 if (!start)
530 for (i=0; i<j; i++)
531 {
532 if (!BN_mod_mul(r,r,r,m,ctx))
533 goto err;
534 }
535
536 /* wvalue will be an odd number < 2^window */
537 if (!BN_mod_mul(r,r,val[wvalue>>1],m,ctx))
538 goto err;
539
540 /* move the 'window' down further */
541 wstart-=wend+1;
542 wvalue=0;
543 start=0;
544 if (wstart < 0) break;
545 }
546 ret=1;
547err:
548 ctx->tos--;
549 for (i=0; i<16; i++)
550 if (val[i] != NULL) BN_clear_free(val[i]);
551 return(ret);
552 }
553
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
new file mode 100644
index 0000000000..071bba3b4b
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -0,0 +1,203 @@
1/* crypto/bn/bn_gcd.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63#ifndef NOPROTO
64static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
65#else
66static BIGNUM *euclid();
67#endif
68
69int BN_gcd(r,in_a,in_b,ctx)
70BIGNUM *r,*in_a,*in_b;
71BN_CTX *ctx;
72 {
73 BIGNUM *a,*b,*t;
74 int ret=0;
75
76 a=ctx->bn[ctx->tos];
77 b=ctx->bn[ctx->tos+1];
78
79 if (BN_copy(a,in_a) == NULL) goto err;
80 if (BN_copy(b,in_b) == NULL) goto err;
81
82 if (BN_cmp(a,b) < 0) { t=a; a=b; b=t; }
83 t=euclid(a,b);
84 if (t == NULL) goto err;
85
86 if (BN_copy(r,t) == NULL) goto err;
87 ret=1;
88err:
89 return(ret);
90 }
91
92static BIGNUM *euclid(a,b)
93BIGNUM *a,*b;
94 {
95 BIGNUM *t;
96 int shifts=0;
97
98 for (;;)
99 {
100 if (BN_is_zero(b))
101 break;
102
103 if (BN_is_odd(a))
104 {
105 if (BN_is_odd(b))
106 {
107 if (!BN_sub(a,a,b)) goto err;
108 if (!BN_rshift1(a,a)) goto err;
109 if (BN_cmp(a,b) < 0)
110 { t=a; a=b; b=t; }
111 }
112 else /* a odd - b even */
113 {
114 if (!BN_rshift1(b,b)) goto err;
115 if (BN_cmp(a,b) < 0)
116 { t=a; a=b; b=t; }
117 }
118 }
119 else /* a is even */
120 {
121 if (BN_is_odd(b))
122 {
123 if (!BN_rshift1(a,a)) goto err;
124 if (BN_cmp(a,b) < 0)
125 { t=a; a=b; b=t; }
126 }
127 else /* a even - b even */
128 {
129 if (!BN_rshift1(a,a)) goto err;
130 if (!BN_rshift1(b,b)) goto err;
131 shifts++;
132 }
133 }
134 }
135 if (shifts)
136 {
137 if (!BN_lshift(a,a,shifts)) goto err;
138 }
139 return(a);
140err:
141 return(NULL);
142 }
143
144/* solves ax == 1 (mod n) */
145BIGNUM *BN_mod_inverse(a, n, ctx)
146BIGNUM *a;
147BIGNUM *n;
148BN_CTX *ctx;
149 {
150 BIGNUM *A,*B,*X,*Y,*M,*D,*R;
151 BIGNUM *ret=NULL,*T;
152 int sign;
153
154 A=ctx->bn[ctx->tos];
155 B=ctx->bn[ctx->tos+1];
156 X=ctx->bn[ctx->tos+2];
157 D=ctx->bn[ctx->tos+3];
158 M=ctx->bn[ctx->tos+4];
159 Y=ctx->bn[ctx->tos+5];
160 ctx->tos+=6;
161 R=BN_new();
162 if (R == NULL) goto err;
163
164 BN_zero(X);
165 BN_one(Y);
166 if (BN_copy(A,a) == NULL) goto err;
167 if (BN_copy(B,n) == NULL) goto err;
168 sign=1;
169
170 while (!BN_is_zero(B))
171 {
172 if (!BN_div(D,M,A,B,ctx)) goto err;
173 T=A;
174 A=B;
175 B=M;
176 /* T has a struct, M does not */
177
178 if (!BN_mul(T,D,X)) goto err;
179 if (!BN_add(T,T,Y)) goto err;
180 M=Y;
181 Y=X;
182 X=T;
183 sign= -sign;
184 }
185 if (sign < 0)
186 {
187 if (!BN_sub(Y,n,Y)) goto err;
188 }
189
190 if (BN_is_one(A))
191 { if (!BN_mod(R,Y,n,ctx)) goto err; }
192 else
193 {
194 BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);
195 goto err;
196 }
197 ret=R;
198err:
199 if ((ret == NULL) && (R != NULL)) BN_free(R);
200 ctx->tos-=6;
201 return(ret);
202 }
203
diff --git a/src/lib/libcrypto/bn/bn_lcl.h b/src/lib/libcrypto/bn/bn_lcl.h
new file mode 100644
index 0000000000..edfd788338
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_lcl.h
@@ -0,0 +1,199 @@
1/* crypto/bn/bn_lcl.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_BN_LCL_H
60#define HEADER_BN_LCL_H
61
62#include "bn.h"
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68/*************************************************************
69 * Using the long long type
70 */
71#define Lw(t) (((BN_ULONG)(t))&BN_MASK2)
72#define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
73
74#define bn_fix_top(a) \
75 { \
76 BN_ULONG *fix_top_l; \
77 for (fix_top_l= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \
78 if (*(fix_top_l--)) break; \
79 }
80
81/* #define bn_expand(n,b) ((((b)/BN_BITS2) <= (n)->max)?(n):bn_expand2((n),(b))) */
82
83#ifdef BN_LLONG
84#define mul_add(r,a,w,c) { \
85 BN_ULLONG t; \
86 t=(BN_ULLONG)w * (a) + (r) + (c); \
87 (r)= Lw(t); \
88 (c)= Hw(t); \
89 }
90
91#define mul(r,a,w,c) { \
92 BN_ULLONG t; \
93 t=(BN_ULLONG)w * (a) + (c); \
94 (r)= Lw(t); \
95 (c)= Hw(t); \
96 }
97
98#else
99/*************************************************************
100 * No long long type
101 */
102
103#define LBITS(a) ((a)&BN_MASK2l)
104#define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l)
105#define L2HBITS(a) ((BN_ULONG)((a)&BN_MASK2l)<<BN_BITS4)
106
107#define LLBITS(a) ((a)&BN_MASKl)
108#define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl)
109#define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)
110
111#define mul64(l,h,bl,bh) \
112 { \
113 BN_ULONG m,m1,lt,ht; \
114 \
115 lt=l; \
116 ht=h; \
117 m =(bh)*(lt); \
118 lt=(bl)*(lt); \
119 m1=(bl)*(ht); \
120 ht =(bh)*(ht); \
121 m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS(1L); \
122 ht+=HBITS(m); \
123 m1=L2HBITS(m); \
124 lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \
125 (l)=lt; \
126 (h)=ht; \
127 }
128
129#define sqr64(lo,ho,in) \
130 { \
131 BN_ULONG l,h,m; \
132 \
133 h=(in); \
134 l=LBITS(h); \
135 h=HBITS(h); \
136 m =(l)*(h); \
137 l*=l; \
138 h*=h; \
139 h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
140 m =(m&BN_MASK2l)<<(BN_BITS4+1); \
141 l=(l+m)&BN_MASK2; if (l < m) h++; \
142 (lo)=l; \
143 (ho)=h; \
144 }
145
146#define mul_add(r,a,bl,bh,c) { \
147 BN_ULONG l,h; \
148 \
149 h= (a); \
150 l=LBITS(h); \
151 h=HBITS(h); \
152 mul64(l,h,(bl),(bh)); \
153 \
154 /* non-multiply part */ \
155 l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
156 (c)=(r); \
157 l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
158 (c)=h&BN_MASK2; \
159 (r)=l; \
160 }
161
162#define mul(r,a,bl,bh,c) { \
163 BN_ULONG l,h; \
164 \
165 h= (a); \
166 l=LBITS(h); \
167 h=HBITS(h); \
168 mul64(l,h,(bl),(bh)); \
169 \
170 /* non-multiply part */ \
171 l+=(c); if ((l&BN_MASK2) < (c)) h++; \
172 (c)=h&BN_MASK2; \
173 (r)=l&BN_MASK2; \
174 }
175
176#endif
177
178#ifndef NOPROTO
179
180BIGNUM *bn_expand2(BIGNUM *b, int bits);
181
182#ifdef X86_ASM
183void bn_add_words(BN_ULONG *r,BN_ULONG *a,int num);
184#endif
185
186#else
187
188BIGNUM *bn_expand2();
189#ifdef X86_ASM
190BN_ULONG bn_add_words();
191#endif
192
193#endif
194
195#ifdef __cplusplus
196}
197#endif
198
199#endif
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
new file mode 100644
index 0000000000..bfe7628ad4
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -0,0 +1,611 @@
1/* crypto/bn/bn_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63char *BN_version="Big Number part of SSLeay 0.9.0b 29-Jun-1998";
64
65BIGNUM *BN_value_one()
66 {
67 static BN_ULONG data_one=1L;
68 static BIGNUM const_one={&data_one,1,1,0};
69
70 return(&const_one);
71 }
72
73char *BN_options()
74 {
75 static int init=0;
76 static char data[16];
77
78 if (!init)
79 {
80 init++;
81#ifdef BN_LLONG
82 sprintf(data,"bn(%d,%d)",(int)sizeof(BN_ULLONG)*8,
83 (int)sizeof(BN_ULONG)*8);
84#else
85 sprintf(data,"bn(%d,%d)",(int)sizeof(BN_ULONG)*8,
86 (int)sizeof(BN_ULONG)*8);
87#endif
88 }
89 return(data);
90 }
91
92int BN_num_bits_word(l)
93BN_ULONG l;
94 {
95 static char bits[256]={
96 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
97 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
98 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
99 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
100 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
101 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
102 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
103 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
104 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
105 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
106 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
107 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
108 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
109 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
110 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
111 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
112 };
113
114#ifdef SIXTY_FOUR_BIT_LONG
115 if (l & 0xffffffff00000000L)
116 {
117 if (l & 0xffff000000000000L)
118 {
119 if (l & 0xff00000000000000L)
120 {
121 return(bits[l>>56]+56);
122 }
123 else return(bits[l>>48]+48);
124 }
125 else
126 {
127 if (l & 0x0000ff0000000000L)
128 {
129 return(bits[l>>40]+40);
130 }
131 else return(bits[l>>32]+32);
132 }
133 }
134 else
135#else
136#ifdef SIXTY_FOUR_BIT
137 if (l & 0xffffffff00000000LL)
138 {
139 if (l & 0xffff000000000000LL)
140 {
141 if (l & 0xff00000000000000LL)
142 {
143 return(bits[l>>56]+56);
144 }
145 else return(bits[l>>48]+48);
146 }
147 else
148 {
149 if (l & 0x0000ff0000000000LL)
150 {
151 return(bits[l>>40]+40);
152 }
153 else return(bits[l>>32]+32);
154 }
155 }
156 else
157#endif
158#endif
159 {
160#if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
161 if (l & 0xffff0000L)
162 {
163 if (l & 0xff000000L)
164 return(bits[l>>24L]+24);
165 else return(bits[l>>16L]+16);
166 }
167 else
168#endif
169 {
170#if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
171 if (l & 0xff00L)
172 return(bits[l>>8]+8);
173 else
174#endif
175 return(bits[l ] );
176 }
177 }
178 }
179
180int BN_num_bits(a)
181BIGNUM *a;
182 {
183 BN_ULONG l;
184 int i;
185
186 if (a->top == 0) return(0);
187 l=a->d[a->top-1];
188 i=(a->top-1)*BN_BITS2;
189 if (l == 0)
190 {
191#if !defined(NO_STDIO) && !defined(WIN16)
192 fprintf(stderr,"BAD TOP VALUE\n");
193#endif
194 abort();
195 }
196 return(i+BN_num_bits_word(l));
197 }
198
199void BN_clear_free(a)
200BIGNUM *a;
201 {
202 if (a == NULL) return;
203 if (a->d != NULL)
204 {
205 memset(a->d,0,a->max*sizeof(a->d[0]));
206 Free(a->d);
207 }
208 memset(a,0,sizeof(BIGNUM));
209 Free(a);
210 }
211
212void BN_free(a)
213BIGNUM *a;
214 {
215 if (a == NULL) return;
216 if (a->d != NULL) Free(a->d);
217 Free(a);
218 }
219
220BIGNUM *BN_new()
221 {
222 BIGNUM *ret;
223 BN_ULONG *p;
224
225 ret=(BIGNUM *)Malloc(sizeof(BIGNUM));
226 if (ret == NULL) goto err;
227 ret->top=0;
228 ret->neg=0;
229 ret->max=(BN_DEFAULT_BITS/BN_BITS2);
230 p=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(ret->max+1));
231 if (p == NULL) goto err;
232 ret->d=p;
233
234 memset(p,0,(ret->max+1)*sizeof(p[0]));
235 return(ret);
236err:
237 BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);
238 return(NULL);
239 }
240
241BN_CTX *BN_CTX_new()
242 {
243 BN_CTX *ret;
244 BIGNUM *n;
245 int i,j;
246
247 ret=(BN_CTX *)Malloc(sizeof(BN_CTX));
248 if (ret == NULL) goto err2;
249
250 for (i=0; i<BN_CTX_NUM; i++)
251 {
252 n=BN_new();
253 if (n == NULL) goto err;
254 ret->bn[i]=n;
255 }
256
257 /* There is actually an extra one, this is for debugging my
258 * stuff */
259 ret->bn[BN_CTX_NUM]=NULL;
260
261 ret->tos=0;
262 return(ret);
263err:
264 for (j=0; j<i; j++)
265 BN_free(ret->bn[j]);
266 Free(ret);
267err2:
268 BNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);
269 return(NULL);
270 }
271
272void BN_CTX_free(c)
273BN_CTX *c;
274 {
275 int i;
276
277 for (i=0; i<BN_CTX_NUM; i++)
278 BN_clear_free(c->bn[i]);
279 Free(c);
280 }
281
282BIGNUM *bn_expand2(b, words)
283BIGNUM *b;
284int words;
285 {
286 BN_ULONG *p;
287
288 if (words > b->max)
289 {
290 p=(BN_ULONG *)Realloc(b->d,sizeof(BN_ULONG)*(words+1));
291 if (p == NULL)
292 {
293 BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);
294 return(NULL);
295 }
296 b->d=p;
297 memset(&(p[b->max]),0,((words+1)-b->max)*sizeof(BN_ULONG));
298 b->max=words;
299 }
300 return(b);
301 }
302
303BIGNUM *BN_dup(a)
304BIGNUM *a;
305 {
306 BIGNUM *r;
307
308 r=BN_new();
309 if (r == NULL) return(NULL);
310 return((BIGNUM *)BN_copy(r,a));
311 }
312
313BIGNUM *BN_copy(a, b)
314BIGNUM *a;
315BIGNUM *b;
316 {
317 int i;
318 BN_ULONG *A,*B;
319
320 if (a == b) return(a);
321 if (bn_wexpand(a,b->top) == NULL) return(NULL);
322
323#if 1
324 A=a->d;
325 B=b->d;
326 for (i=b->top&(~7); i>0; i-=8)
327 {
328 A[0]=B[0];
329 A[1]=B[1];
330 A[2]=B[2];
331 A[3]=B[3];
332 A[4]=B[4];
333 A[5]=B[5];
334 A[6]=B[6];
335 A[7]=B[7];
336 A+=8;
337 B+=8;
338 }
339 switch (b->top&7)
340 {
341 case 7:
342 A[6]=B[6];
343 case 6:
344 A[5]=B[5];
345 case 5:
346 A[4]=B[4];
347 case 4:
348 A[3]=B[3];
349 case 3:
350 A[2]=B[2];
351 case 2:
352 A[1]=B[1];
353 case 1:
354 A[0]=B[0];
355 }
356#else
357 memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
358#endif
359
360/* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/
361 a->top=b->top;
362 if (a->top == 0)
363 a->d[0]=0;
364 a->neg=b->neg;
365 return(a);
366 }
367
368void BN_clear(a)
369BIGNUM *a;
370 {
371 memset(a->d,0,a->max*sizeof(a->d[0]));
372 a->top=0;
373 a->neg=0;
374 }
375
376unsigned long BN_get_word(a)
377BIGNUM *a;
378 {
379 int i,n;
380 unsigned long ret=0;
381
382 n=BN_num_bytes(a);
383 if (n > sizeof(unsigned long))
384#ifdef SIXTY_FOUR_BIT_LONG
385 return(BN_MASK2);
386#else
387 return(0xFFFFFFFFL);
388#endif
389 for (i=a->top-1; i>=0; i--)
390 {
391#ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
392 ret<<=BN_BITS4; /* stops the compiler complaining */
393 ret<<=BN_BITS4;
394#endif
395 ret|=a->d[i];
396 }
397 return(ret);
398 }
399
400int BN_set_word(a,w)
401BIGNUM *a;
402unsigned long w;
403 {
404 int i,n;
405 if (bn_expand(a,sizeof(unsigned long)*8) == NULL) return(0);
406
407 n=sizeof(unsigned long)/BN_BYTES;
408 a->neg=0;
409 a->top=0;
410 a->d[0]=(BN_ULONG)w&BN_MASK2;
411 if (a->d[0] != 0) a->top=1;
412 for (i=1; i<n; i++)
413 {
414 /* the following is done instead of
415 * w>>=BN_BITS2 so compilers don't complain
416 * on builds where sizeof(long) == BN_TYPES */
417#ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
418 w>>=BN_BITS4;
419 w>>=BN_BITS4;
420#endif
421 a->d[i]=(BN_ULONG)w&BN_MASK2;
422 if (a->d[i] != 0) a->top=i+1;
423 }
424 return(1);
425 }
426
427/* ignore negative */
428BIGNUM *BN_bin2bn(s, len, ret)
429unsigned char *s;
430int len;
431BIGNUM *ret;
432 {
433 unsigned int i,m;
434 unsigned int n;
435 BN_ULONG l;
436
437 if (ret == NULL) ret=BN_new();
438 if (ret == NULL) return(NULL);
439 l=0;
440 n=len;
441 if (n == 0)
442 {
443 ret->top=0;
444 return(ret);
445 }
446 if (bn_expand(ret,(int)(n+2)*8) == NULL)
447 return(NULL);
448 i=((n-1)/BN_BYTES)+1;
449 m=((n-1)%(BN_BYTES));
450 ret->top=i;
451 while (n-- > 0)
452 {
453 l=(l<<8L)| *(s++);
454 if (m-- == 0)
455 {
456 ret->d[--i]=l;
457 l=0;
458 m=BN_BYTES-1;
459 }
460 }
461 /* need to call this due to clear byte at top if avoiding
462 * having the top bit set (-ve number) */
463 bn_fix_top(ret);
464 return(ret);
465 }
466
467/* ignore negative */
468int BN_bn2bin(a, to)
469BIGNUM *a;
470unsigned char *to;
471 {
472 int n,i;
473 BN_ULONG l;
474
475 n=i=BN_num_bytes(a);
476 while (i-- > 0)
477 {
478 l=a->d[i/BN_BYTES];
479 *(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;
480 }
481 return(n);
482 }
483
484int BN_ucmp(a, b)
485BIGNUM *a;
486BIGNUM *b;
487 {
488 int i;
489 BN_ULONG t1,t2,*ap,*bp;
490
491 i=a->top-b->top;
492 if (i != 0) return(i);
493 ap=a->d;
494 bp=b->d;
495 for (i=a->top-1; i>=0; i--)
496 {
497 t1= ap[i];
498 t2= bp[i];
499 if (t1 != t2)
500 return(t1 > t2?1:-1);
501 }
502 return(0);
503 }
504
505int BN_cmp(a, b)
506BIGNUM *a;
507BIGNUM *b;
508 {
509 int i;
510 int gt,lt;
511 BN_ULONG t1,t2;
512
513 if ((a == NULL) || (b == NULL))
514 {
515 if (a != NULL)
516 return(-1);
517 else if (b != NULL)
518 return(1);
519 else
520 return(0);
521 }
522 if (a->neg != b->neg)
523 {
524 if (a->neg)
525 return(-1);
526 else return(1);
527 }
528 if (a->neg == 0)
529 { gt=1; lt= -1; }
530 else { gt= -1; lt=1; }
531
532 if (a->top > b->top) return(gt);
533 if (a->top < b->top) return(lt);
534 for (i=a->top-1; i>=0; i--)
535 {
536 t1=a->d[i];
537 t2=b->d[i];
538 if (t1 > t2) return(gt);
539 if (t1 < t2) return(lt);
540 }
541 return(0);
542 }
543
544int BN_set_bit(a, n)
545BIGNUM *a;
546int n;
547 {
548 int i,j;
549
550 i=n/BN_BITS2;
551 j=n%BN_BITS2;
552 if (a->top <= i)
553 {
554 if (bn_expand(a,n) == NULL) return(0);
555 a->top=i+1;
556 }
557
558 a->d[i]|=(1L<<j);
559 return(1);
560 }
561
562int BN_clear_bit(a, n)
563BIGNUM *a;
564int n;
565 {
566 int i,j;
567
568 i=n/BN_BITS2;
569 j=n%BN_BITS2;
570 if (a->top <= i) return(0);
571
572 a->d[i]&=(~(1L<<j));
573 return(1);
574 }
575
576int BN_is_bit_set(a, n)
577BIGNUM *a;
578int n;
579 {
580 int i,j;
581
582 if (n < 0) return(0);
583 i=n/BN_BITS2;
584 j=n%BN_BITS2;
585 if (a->top <= i) return(0);
586 return((a->d[i]&(((BN_ULONG)1)<<j))?1:0);
587 }
588
589int BN_mask_bits(a,n)
590BIGNUM *a;
591int n;
592 {
593 int b,w;
594
595 w=n/BN_BITS2;
596 b=n%BN_BITS2;
597 if (w >= a->top) return(0);
598 if (b == 0)
599 a->top=w;
600 else
601 {
602 a->top=w+1;
603 a->d[w]&= ~(BN_MASK2<<b);
604 while ((w >= 0) && (a->d[w] == 0))
605 {
606 a->top--;
607 w--;
608 }
609 }
610 return(1);
611 }
diff --git a/src/lib/libcrypto/bn/bn_mod.c b/src/lib/libcrypto/bn/bn_mod.c
new file mode 100644
index 0000000000..c351aac14f
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_mod.c
@@ -0,0 +1,97 @@
1/* crypto/bn/bn_mod.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63/* rem != m */
64int BN_mod(rem, m, d,ctx)
65BIGNUM *rem;
66BIGNUM *m;
67BIGNUM *d;
68BN_CTX *ctx;
69 {
70#if 0 /* The old slow way */
71 int i,nm,nd;
72 BIGNUM *dv;
73
74 if (BN_ucmp(m,d) < 0)
75 return((BN_copy(rem,m) == NULL)?0:1);
76
77 dv=ctx->bn[ctx->tos];
78
79 if (!BN_copy(rem,m)) return(0);
80
81 nm=BN_num_bits(rem);
82 nd=BN_num_bits(d);
83 if (!BN_lshift(dv,d,nm-nd)) return(0);
84 for (i=nm-nd; i>=0; i--)
85 {
86 if (BN_cmp(rem,dv) >= 0)
87 {
88 if (!BN_sub(rem,rem,dv)) return(0);
89 }
90 if (!BN_rshift1(dv,dv)) return(0);
91 }
92 return(1);
93#else
94 return(BN_div(NULL,rem,m,d,ctx));
95#endif
96 }
97
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
new file mode 100644
index 0000000000..e435df61f8
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_mont.c
@@ -0,0 +1,306 @@
1/* crypto/bn/bn_mont.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63int BN_mod_mul_montgomery(r,a,b,mont,ctx)
64BIGNUM *r,*a,*b;
65BN_MONT_CTX *mont;
66BN_CTX *ctx;
67 {
68 BIGNUM *tmp;
69
70 tmp=ctx->bn[ctx->tos++];
71
72 if (a == b)
73 {
74 if (!BN_sqr(tmp,a,ctx)) goto err;
75 }
76 else
77 {
78 if (!BN_mul(tmp,a,b)) goto err;
79 }
80 /* reduce from aRR to aR */
81 if (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;
82 ctx->tos--;
83 return(1);
84err:
85 return(0);
86 }
87
88#define MONT_WORD
89
90#ifdef MONT_WORD
91int BN_from_montgomery(ret,a,mont,ctx)
92BIGNUM *ret;
93BIGNUM *a;
94BN_MONT_CTX *mont;
95BN_CTX *ctx;
96 {
97 BIGNUM *n,*t1,*r;
98 BN_ULONG *ap,*np,*rp,n0,v;
99 int al,nl,max,i,x,ri;
100 int retn=0;
101
102 t1=ctx->bn[ctx->tos];
103 r=ctx->bn[ctx->tos+1];
104
105 if (!BN_copy(r,a)) goto err;
106 n=mont->N;
107
108 ap=a->d;
109 /* mont->ri is the size of mont->N in bits/words */
110 al=ri=mont->ri/BN_BITS2;
111
112 nl=n->top;
113 if ((al == 0) || (nl == 0)) { r->top=0; return(1); }
114
115 max=(nl+al+1); /* allow for overflow (no?) XXX */
116 if (bn_wexpand(r,max) == NULL) goto err;
117 if (bn_wexpand(ret,max) == NULL) goto err;
118
119 r->neg=a->neg^n->neg;
120 np=n->d;
121 rp=r->d;
122
123 /* clear the top words of T */
124#if 1
125 for (i=r->top; i<max; i++) /* memset? XXX */
126 r->d[i]=0;
127#else
128 memset(&(r->d[r->top]),0,(max-r->top)*sizeof(BN_ULONG));
129#endif
130
131 r->top=max;
132 n0=mont->n0;
133
134 for (i=0; i<nl; i++)
135 {
136#if 0
137 int x1,x2;
138
139 if (i+4 > nl)
140 {
141 x2=nl;
142 x1=0;
143 }
144 else
145 {
146 x2=i+4;
147 x1=nl-x2;
148 }
149 v=bn_mul_add_words(&(rp[x1]),&(np[x1]),x2,(rp[x1]*n0)&BN_MASK2);
150#else
151 v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);
152#endif
153
154 if (((rp[nl]+=v)&BN_MASK2) < v)
155 {
156 for (x=(nl+1); (((++rp[x])&BN_MASK2) == 0); x++)
157 ;
158 }
159 rp++;
160 }
161 while (r->d[r->top-1] == 0)
162 r->top--;
163
164 /* mont->ri will be a multiple of the word size */
165#if 0
166 BN_rshift(ret,r,mont->ri);
167#else
168 ap=r->d;
169 rp=ret->d;
170 x=ri;
171 al=r->top-x;
172 for (i=0; i<al; i++)
173 {
174 rp[i]=ap[i+x];
175 }
176 ret->top=al;
177#endif
178
179 if (BN_ucmp(ret,mont->N) >= 0)
180 {
181 bn_qsub(ret,ret,mont->N); /* XXX */
182 }
183 retn=1;
184err:
185 return(retn);
186 }
187#else
188int BN_from_montgomery(r,a,mont,ctx)
189BIGNUM *r;
190BIGNUM *a;
191BN_MONT_CTX *mont;
192BN_CTX *ctx;
193 {
194 BIGNUM *t1,*t2;
195
196 t1=ctx->bn[ctx->tos];
197 t2=ctx->bn[ctx->tos+1];
198
199 if (!BN_copy(t1,a)) goto err;
200 /* can cheat */
201 BN_mask_bits(t1,mont->ri);
202
203 if (!BN_mul(t2,t1,mont->Ni)) goto err;
204 BN_mask_bits(t2,mont->ri);
205
206 if (!BN_mul(t1,t2,mont->N)) goto err;
207 if (!BN_add(t2,a,t1)) goto err;
208 BN_rshift(r,t2,mont->ri);
209
210 if (BN_ucmp(r,mont->N) >= 0)
211 bn_qsub(r,r,mont->N);
212
213 return(1);
214err:
215 return(0);
216 }
217#endif
218
219BN_MONT_CTX *BN_MONT_CTX_new()
220 {
221 BN_MONT_CTX *ret;
222
223 if ((ret=(BN_MONT_CTX *)Malloc(sizeof(BN_MONT_CTX))) == NULL)
224 return(NULL);
225 ret->ri=0;
226 ret->RR=BN_new();
227 ret->N=BN_new();
228 ret->Ni=NULL;
229 if ((ret->RR == NULL) || (ret->N == NULL))
230 {
231 BN_MONT_CTX_free(ret);
232 return(NULL);
233 }
234 return(ret);
235 }
236
237void BN_MONT_CTX_free(mont)
238BN_MONT_CTX *mont;
239 {
240 if (mont->RR != NULL) BN_free(mont->RR);
241 if (mont->N != NULL) BN_free(mont->N);
242 if (mont->Ni != NULL) BN_free(mont->Ni);
243 Free(mont);
244 }
245
246int BN_MONT_CTX_set(mont,mod,ctx)
247BN_MONT_CTX *mont;
248BIGNUM *mod;
249BN_CTX *ctx;
250 {
251 BIGNUM *Ri=NULL,*R=NULL;
252
253 if (mont->RR == NULL) mont->RR=BN_new();
254 if (mont->N == NULL) mont->N=BN_new();
255
256 R=mont->RR; /* grab RR as a temp */
257 BN_copy(mont->N,mod); /* Set N */
258
259#ifdef MONT_WORD
260{
261 BIGNUM tmod;
262 BN_ULONG buf[2];
263 /* int z; */
264
265 mont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;
266 BN_lshift(R,BN_value_one(),BN_BITS2); /* R */
267 /* I was bad, this modification of a passed variable was
268 * breaking the multithreaded stuff :-(
269 * z=mod->top;
270 * mod->top=1; */
271
272 buf[0]=mod->d[0];
273 buf[1]=0;
274 tmod.d=buf;
275 tmod.top=1;
276 tmod.max=mod->max;
277 tmod.neg=mod->neg;
278
279 if ((Ri=BN_mod_inverse(R,&tmod,ctx)) == NULL) goto err; /* Ri */
280 BN_lshift(Ri,Ri,BN_BITS2); /* R*Ri */
281 bn_qsub(Ri,Ri,BN_value_one()); /* R*Ri - 1 */
282 BN_div(Ri,NULL,Ri,&tmod,ctx);
283 mont->n0=Ri->d[0];
284 BN_free(Ri);
285 /* mod->top=z; */
286}
287#else
288 mont->ri=BN_num_bits(mod);
289 BN_lshift(R,BN_value_one(),mont->ri); /* R */
290 if ((Ri=BN_mod_inverse(R,mod,ctx)) == NULL) goto err; /* Ri */
291 BN_lshift(Ri,Ri,mont->ri); /* R*Ri */
292 bn_qsub(Ri,Ri,BN_value_one()); /* R*Ri - 1 */
293 BN_div(Ri,NULL,Ri,mod,ctx);
294 if (mont->Ni != NULL) BN_free(mont->Ni);
295 mont->Ni=Ri; /* Ni=(R*Ri-1)/N */
296#endif
297
298 /* setup RR for conversions */
299 BN_lshift(mont->RR,BN_value_one(),mont->ri*2);
300 BN_mod(mont->RR,mont->RR,mont->N,ctx);
301
302 return(1);
303err:
304 return(0);
305 }
306
diff --git a/src/lib/libcrypto/bn/bn_mpi.c b/src/lib/libcrypto/bn/bn_mpi.c
new file mode 100644
index 0000000000..53945c1057
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_mpi.c
@@ -0,0 +1,134 @@
1/* crypto/bn/bn_mpi.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63int BN_bn2mpi(a,d)
64BIGNUM *a;
65unsigned char *d;
66 {
67 int bits;
68 int num=0;
69 int ext=0;
70 long l;
71
72 bits=BN_num_bits(a);
73 num=(bits+7)/8;
74 if (bits > 0)
75 {
76 ext=((bits & 0x07) == 0);
77 }
78 if (d == NULL)
79 return(num+4+ext);
80
81 l=num+ext;
82 d[0]=(unsigned char)(l>>24)&0xff;
83 d[1]=(unsigned char)(l>>16)&0xff;
84 d[2]=(unsigned char)(l>> 8)&0xff;
85 d[3]=(unsigned char)(l )&0xff;
86 if (ext) d[4]=0;
87 num=BN_bn2bin(a,&(d[4+ext]));
88 if (a->neg)
89 d[4]|=0x80;
90 return(num+4+ext);
91 }
92
93BIGNUM *BN_mpi2bn(d,n,a)
94unsigned char *d;
95int n;
96BIGNUM *a;
97 {
98 long len;
99 int neg=0;
100
101 if (n < 4)
102 {
103 BNerr(BN_F_BN_MPI2BN,BN_R_INVALID_LENGTH);
104 return(NULL);
105 }
106 len=(d[0]<<24)|(d[1]<<16)|(d[2]<<8)|d[3];
107 if ((len+4) != n)
108 {
109 BNerr(BN_F_BN_MPI2BN,BN_R_ENCODING_ERROR);
110 return(NULL);
111 }
112
113 if (a == NULL) a=BN_new();
114 if (a == NULL) return(NULL);
115
116 if (len == 0)
117 {
118 a->neg=0;
119 a->top=0;
120 return(a);
121 }
122 d+=4;
123 if ((*d) & 0x80)
124 neg=1;
125 if (BN_bin2bn(d,(int)len,a) == NULL)
126 return(NULL);
127 a->neg=neg;
128 if (neg)
129 {
130 BN_clear_bit(a,BN_num_bits(a)-1);
131 }
132 return(a);
133 }
134
diff --git a/src/lib/libcrypto/bn/bn_mul.c b/src/lib/libcrypto/bn/bn_mul.c
new file mode 100644
index 0000000000..d0c04e1d4b
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_mul.c
@@ -0,0 +1,209 @@
1/* crypto/bn/bn_mul.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63/* r must be different to a and b */
64/* int BN_mmul(r, a, b) */
65int BN_mul(r, a, b)
66BIGNUM *r;
67BIGNUM *a;
68BIGNUM *b;
69 {
70 int i;
71 int max,al,bl;
72 BN_ULONG *ap,*bp,*rp;
73
74 al=a->top;
75 bl=b->top;
76 if ((al == 0) || (bl == 0))
77 {
78 r->top=0;
79 return(1);
80 }
81
82 max=(al+bl);
83 if (bn_wexpand(r,max) == NULL) return(0);
84 r->top=max;
85 r->neg=a->neg^b->neg;
86 ap=a->d;
87 bp=b->d;
88 rp=r->d;
89
90 rp[al]=bn_mul_words(rp,ap,al,*(bp++));
91 rp++;
92 for (i=1; i<bl; i++)
93 {
94 rp[al]=bn_mul_add_words(rp,ap,al,*(bp++));
95 rp++;
96 }
97 if (r->d[max-1] == 0) r->top--;
98 return(1);
99 }
100
101#if 0
102#include "stack.h"
103
104int limit=16;
105
106typedef struct bn_pool_st
107 {
108 int used;
109 int tos;
110 STACK *sk;
111 } BN_POOL;
112
113BIGNUM *BN_POOL_push(bp)
114BN_POOL *bp;
115 {
116 BIGNUM *ret;
117
118 if (bp->used >= bp->tos)
119 {
120 ret=BN_new();
121 sk_push(bp->sk,(char *)ret);
122 bp->tos++;
123 bp->used++;
124 }
125 else
126 {
127 ret=(BIGNUM *)sk_value(bp->sk,bp->used);
128 bp->used++;
129 }
130 return(ret);
131 }
132
133void BN_POOL_pop(bp,num)
134BN_POOL *bp;
135int num;
136 {
137 bp->used-=num;
138 }
139
140int BN_mul(r,a,b)
141BIGNUM *r,*a,*b;
142 {
143 static BN_POOL bp;
144 static init=1;
145
146 if (init)
147 {
148 bp.used=0;
149 bp.tos=0;
150 bp.sk=sk_new_null();
151 init=0;
152 }
153 return(BN_mm(r,a,b,&bp));
154 }
155
156/* r must be different to a and b */
157int BN_mm(m, A, B, bp)
158BIGNUM *m,*A,*B;
159BN_POOL *bp;
160 {
161 int i,num;
162 int an,bn;
163 BIGNUM *a,*b,*c,*d,*ac,*bd;
164
165 an=A->top;
166 bn=B->top;
167 if ((an <= limit) || (bn <= limit))
168 {
169 return(BN_mmul(m,A,B));
170 }
171
172 a=BN_POOL_push(bp);
173 b=BN_POOL_push(bp);
174 c=BN_POOL_push(bp);
175 d=BN_POOL_push(bp);
176 ac=BN_POOL_push(bp);
177 bd=BN_POOL_push(bp);
178
179 num=(an <= bn)?an:bn;
180 num=1<<(BN_num_bits_word(num-1)-1);
181
182 /* Are going to now chop things into 'num' word chunks. */
183 num*=BN_BITS2;
184
185 BN_copy(a,A);
186 BN_mask_bits(a,num);
187 BN_rshift(b,A,num);
188
189 BN_copy(c,B);
190 BN_mask_bits(c,num);
191 BN_rshift(d,B,num);
192
193 BN_sub(ac ,b,a);
194 BN_sub(bd,c,d);
195 BN_mm(m,ac,bd,bp);
196 BN_mm(ac,a,c,bp);
197 BN_mm(bd,b,d,bp);
198
199 BN_add(m,m,ac);
200 BN_add(m,m,bd);
201 BN_lshift(m,m,num);
202 BN_lshift(bd,bd,num*2);
203
204 BN_add(m,m,ac);
205 BN_add(m,m,bd);
206 BN_POOL_pop(bp,6);
207 return(1);
208 }
209#endif
diff --git a/src/lib/libcrypto/bn/bn_prime.c b/src/lib/libcrypto/bn/bn_prime.c
new file mode 100644
index 0000000000..0c85f70b59
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_prime.c
@@ -0,0 +1,473 @@
1/* crypto/bn/bn_prime.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include "cryptlib.h"
62#include "bn_lcl.h"
63#include "rand.h"
64
65/* The quick seive algorithm approach to weeding out primes is
66 * Philip Zimmermann's, as implemented in PGP. I have had a read of
67 * his comments and implemented my own version.
68 */
69#include "bn_prime.h"
70
71#ifndef NOPROTO
72static int witness(BIGNUM *a, BIGNUM *n, BN_CTX *ctx,BN_CTX *ctx2,
73 BN_MONT_CTX *mont);
74static int probable_prime(BIGNUM *rnd, int bits);
75static int probable_prime_dh(BIGNUM *rnd, int bits,
76 BIGNUM *add, BIGNUM *rem, BN_CTX *ctx);
77static int probable_prime_dh_strong(BIGNUM *rnd, int bits,
78 BIGNUM *add, BIGNUM *rem, BN_CTX *ctx);
79#else
80static int witness();
81static int probable_prime();
82static int probable_prime_dh();
83static int probable_prime_dh_strong();
84#endif
85
86BIGNUM *BN_generate_prime(bits,strong,add,rem,callback,cb_arg)
87int bits;
88int strong;
89BIGNUM *add;
90BIGNUM *rem;
91void (*callback)(P_I_I_P);
92char *cb_arg;
93 {
94 BIGNUM *rnd=NULL;
95 BIGNUM *ret=NULL;
96 BIGNUM *t=NULL;
97 int i,j,c1=0;
98 BN_CTX *ctx;
99
100 ctx=BN_CTX_new();
101 if (ctx == NULL) goto err;
102 if ((rnd=BN_new()) == NULL) goto err;
103 if (strong)
104 if ((t=BN_new()) == NULL) goto err;
105loop:
106 /* make a random number and set the top and bottom bits */
107 if (add == NULL)
108 {
109 if (!probable_prime(rnd,bits)) goto err;
110 }
111 else
112 {
113 if (strong)
114 {
115 if (!probable_prime_dh_strong(rnd,bits,add,rem,ctx))
116 goto err;
117 }
118 else
119 {
120 if (!probable_prime_dh(rnd,bits,add,rem,ctx))
121 goto err;
122 }
123 }
124 /* if (BN_mod_word(rnd,(BN_ULONG)3) == 1) goto loop; */
125 if (callback != NULL) callback(0,c1++,cb_arg);
126
127 if (!strong)
128 {
129 i=BN_is_prime(rnd,BN_prime_checks,callback,ctx,cb_arg);
130 if (i == -1) goto err;
131 if (i == 0) goto loop;
132 }
133 else
134 {
135 /* for a strong prime generation,
136 * check that (p-1)/2 is prime.
137 * Since a prime is odd, We just
138 * need to divide by 2 */
139 if (!BN_rshift1(t,rnd)) goto err;
140
141 for (i=0; i<BN_prime_checks; i++)
142 {
143 j=BN_is_prime(rnd,1,callback,ctx,cb_arg);
144 if (j == -1) goto err;
145 if (j == 0) goto loop;
146
147 j=BN_is_prime(t,1,callback,ctx,cb_arg);
148 if (j == -1) goto err;
149 if (j == 0) goto loop;
150
151 if (callback != NULL) callback(2,c1-1,cb_arg);
152 /* We have a strong prime test pass */
153 }
154 }
155 /* we have a prime :-) */
156 ret=rnd;
157err:
158 if ((ret == NULL) && (rnd != NULL)) BN_free(rnd);
159 if (t != NULL) BN_free(t);
160 if (ctx != NULL) BN_CTX_free(ctx);
161 return(ret);
162 }
163
164int BN_is_prime(a,checks,callback,ctx_passed,cb_arg)
165BIGNUM *a;
166int checks;
167void (*callback)(P_I_I_P);
168BN_CTX *ctx_passed;
169char *cb_arg;
170 {
171 int i,j,c2=0,ret= -1;
172 BIGNUM *check;
173 BN_CTX *ctx=NULL,*ctx2=NULL;
174 BN_MONT_CTX *mont=NULL;
175
176 if (!BN_is_odd(a))
177 return(0);
178 if (ctx_passed != NULL)
179 ctx=ctx_passed;
180 else
181 if ((ctx=BN_CTX_new()) == NULL) goto err;
182
183 if ((ctx2=BN_CTX_new()) == NULL) goto err;
184 if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
185
186 check=ctx->bn[ctx->tos++];
187
188 /* Setup the montgomery structure */
189 if (!BN_MONT_CTX_set(mont,a,ctx2)) goto err;
190
191 for (i=0; i<checks; i++)
192 {
193 if (!BN_rand(check,BN_num_bits(a)-1,0,0)) goto err;
194 j=witness(check,a,ctx,ctx2,mont);
195 if (j == -1) goto err;
196 if (j)
197 {
198 ret=0;
199 goto err;
200 }
201 if (callback != NULL) callback(1,c2++,cb_arg);
202 }
203 ret=1;
204err:
205 ctx->tos--;
206 if ((ctx_passed == NULL) && (ctx != NULL))
207 BN_CTX_free(ctx);
208 if (ctx2 != NULL)
209 BN_CTX_free(ctx2);
210 if (mont != NULL) BN_MONT_CTX_free(mont);
211
212 return(ret);
213 }
214
215#define RECP_MUL_MOD
216
217static int witness(a,n,ctx,ctx2,mont)
218BIGNUM *a;
219BIGNUM *n;
220BN_CTX *ctx,*ctx2;
221BN_MONT_CTX *mont;
222 {
223 int k,i,ret= -1,good;
224 BIGNUM *d,*dd,*tmp,*d1,*d2,*n1;
225 BIGNUM *mont_one,*mont_n1,*mont_a;
226
227 d1=ctx->bn[ctx->tos];
228 d2=ctx->bn[ctx->tos+1];
229 n1=ctx->bn[ctx->tos+2];
230 ctx->tos+=3;
231
232 mont_one=ctx2->bn[ctx2->tos];
233 mont_n1=ctx2->bn[ctx2->tos+1];
234 mont_a=ctx2->bn[ctx2->tos+2];
235 ctx2->tos+=3;
236
237 d=d1;
238 dd=d2;
239 if (!BN_one(d)) goto err;
240 if (!BN_sub(n1,n,d)) goto err; /* n1=n-1; */
241 k=BN_num_bits(n1);
242
243 if (!BN_to_montgomery(mont_one,BN_value_one(),mont,ctx2)) goto err;
244 if (!BN_to_montgomery(mont_n1,n1,mont,ctx2)) goto err;
245 if (!BN_to_montgomery(mont_a,a,mont,ctx2)) goto err;
246
247 BN_copy(d,mont_one);
248 for (i=k-1; i>=0; i--)
249 {
250 if ( (BN_cmp(d,mont_one) != 0) &&
251 (BN_cmp(d,mont_n1) != 0))
252 good=1;
253 else
254 good=0;
255
256 BN_mod_mul_montgomery(dd,d,d,mont,ctx2);
257
258 if (good && (BN_cmp(dd,mont_one) == 0))
259 {
260 ret=1;
261 goto err;
262 }
263 if (BN_is_bit_set(n1,i))
264 {
265 BN_mod_mul_montgomery(d,dd,mont_a,mont,ctx2);
266 }
267 else
268 {
269 tmp=d;
270 d=dd;
271 dd=tmp;
272 }
273 }
274 if (BN_cmp(d,mont_one) == 0)
275 i=0;
276 else i=1;
277 ret=i;
278err:
279 ctx->tos-=3;
280 ctx2->tos-=3;
281 return(ret);
282 }
283
284static int probable_prime(rnd, bits)
285BIGNUM *rnd;
286int bits;
287 {
288 int i;
289 MS_STATIC BN_ULONG mods[NUMPRIMES];
290 BN_ULONG delta;
291
292 if (!BN_rand(rnd,bits,1,1)) return(0);
293 /* we now have a random number 'rand' to test. */
294 for (i=1; i<NUMPRIMES; i++)
295 mods[i]=BN_mod_word(rnd,(BN_ULONG)primes[i]);
296 delta=0;
297 loop: for (i=1; i<NUMPRIMES; i++)
298 {
299 /* check that rnd is not a prime and also
300 * that gcd(rnd-1,primes) == 1 (except for 2) */
301 if (((mods[i]+delta)%primes[i]) <= 1)
302 {
303 delta+=2;
304 /* perhaps need to check for overflow of
305 * delta (but delta can be upto 2^32) */
306 goto loop;
307 }
308 }
309 if (!BN_add_word(rnd,delta)) return(0);
310 return(1);
311 }
312
313static int probable_prime_dh(rnd, bits, add, rem,ctx)
314BIGNUM *rnd;
315int bits;
316BIGNUM *add;
317BIGNUM *rem;
318BN_CTX *ctx;
319 {
320 int i,ret=0;
321 BIGNUM *t1;
322
323 t1=ctx->bn[ctx->tos++];
324
325 if (!BN_rand(rnd,bits,0,1)) goto err;
326
327 /* we need ((rnd-rem) % add) == 0 */
328
329 if (!BN_mod(t1,rnd,add,ctx)) goto err;
330 if (!BN_sub(rnd,rnd,t1)) goto err;
331 if (rem == NULL)
332 { if (!BN_add_word(rnd,1)) goto err; }
333 else
334 { if (!BN_add(rnd,rnd,rem)) goto err; }
335
336 /* we now have a random number 'rand' to test. */
337
338 loop: for (i=1; i<NUMPRIMES; i++)
339 {
340 /* check that rnd is a prime */
341 if (BN_mod_word(rnd,(BN_LONG)primes[i]) <= 1)
342 {
343 if (!BN_add(rnd,rnd,add)) goto err;
344 goto loop;
345 }
346 }
347 ret=1;
348err:
349 ctx->tos--;
350 return(ret);
351 }
352
353static int probable_prime_dh_strong(p, bits, padd, rem,ctx)
354BIGNUM *p;
355int bits;
356BIGNUM *padd;
357BIGNUM *rem;
358BN_CTX *ctx;
359 {
360 int i,ret=0;
361 BIGNUM *t1,*qadd=NULL,*q=NULL;
362
363 bits--;
364 t1=ctx->bn[ctx->tos++];
365 q=ctx->bn[ctx->tos++];
366 qadd=ctx->bn[ctx->tos++];
367
368 if (!BN_rshift1(qadd,padd)) goto err;
369
370 if (!BN_rand(q,bits,0,1)) goto err;
371
372 /* we need ((rnd-rem) % add) == 0 */
373 if (!BN_mod(t1,q,qadd,ctx)) goto err;
374 if (!BN_sub(q,q,t1)) goto err;
375 if (rem == NULL)
376 { if (!BN_add_word(q,1)) goto err; }
377 else
378 {
379 if (!BN_rshift1(t1,rem)) goto err;
380 if (!BN_add(q,q,t1)) goto err;
381 }
382
383 /* we now have a random number 'rand' to test. */
384 if (!BN_lshift1(p,q)) goto err;
385 if (!BN_add_word(p,1)) goto err;
386
387 loop: for (i=1; i<NUMPRIMES; i++)
388 {
389 /* check that p and q are prime */
390 /* check that for p and q
391 * gcd(p-1,primes) == 1 (except for 2) */
392 if ( (BN_mod_word(p,(BN_LONG)primes[i]) == 0) ||
393 (BN_mod_word(q,(BN_LONG)primes[i]) == 0))
394 {
395 if (!BN_add(p,p,padd)) goto err;
396 if (!BN_add(q,q,qadd)) goto err;
397 goto loop;
398 }
399 }
400 ret=1;
401err:
402 ctx->tos-=3;
403 return(ret);
404 }
405
406#if 0
407static int witness(a, n,ctx)
408BIGNUM *a;
409BIGNUM *n;
410BN_CTX *ctx;
411 {
412 int k,i,nb,ret= -1;
413 BIGNUM *d,*dd,*tmp;
414 BIGNUM *d1,*d2,*x,*n1,*inv;
415
416 d1=ctx->bn[ctx->tos];
417 d2=ctx->bn[ctx->tos+1];
418 x=ctx->bn[ctx->tos+2];
419 n1=ctx->bn[ctx->tos+3];
420 inv=ctx->bn[ctx->tos+4];
421 ctx->tos+=5;
422
423 d=d1;
424 dd=d2;
425 if (!BN_one(d)) goto err;
426 if (!BN_sub(n1,n,d)) goto err; /* n1=n-1; */
427 k=BN_num_bits(n1);
428
429 /* i=BN_num_bits(n); */
430#ifdef RECP_MUL_MOD
431 nb=BN_reciprocal(inv,n,ctx); /**/
432 if (nb == -1) goto err;
433#endif
434
435 for (i=k-1; i>=0; i--)
436 {
437 if (BN_copy(x,d) == NULL) goto err;
438#ifndef RECP_MUL_MOD
439 if (!BN_mod_mul(dd,d,d,n,ctx)) goto err;
440#else
441 if (!BN_mod_mul_reciprocal(dd,d,d,n,inv,nb,ctx)) goto err;
442#endif
443 if ( BN_is_one(dd) &&
444 !BN_is_one(x) &&
445 (BN_cmp(x,n1) != 0))
446 {
447 ret=1;
448 goto err;
449 }
450 if (BN_is_bit_set(n1,i))
451 {
452#ifndef RECP_MUL_MOD
453 if (!BN_mod_mul(d,dd,a,n,ctx)) goto err;
454#else
455 if (!BN_mod_mul_reciprocal(d,dd,a,n,inv,nb,ctx)) goto err;
456#endif
457 }
458 else
459 {
460 tmp=d;
461 d=dd;
462 dd=tmp;
463 }
464 }
465 if (BN_is_one(d))
466 i=0;
467 else i=1;
468 ret=i;
469err:
470 ctx->tos-=5;
471 return(ret);
472 }
473#endif
diff --git a/src/lib/libcrypto/bn/bn_prime.h b/src/lib/libcrypto/bn/bn_prime.h
new file mode 100644
index 0000000000..6fce0210cd
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_prime.h
@@ -0,0 +1,325 @@
1/* crypto/bn/bn_prime.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef EIGHT_BIT
60#define NUMPRIMES 2048
61#else
62#define NUMPRIMES 54
63#endif
64static unsigned int primes[NUMPRIMES]=
65 {
66 2, 3, 5, 7, 11, 13, 17, 19,
67 23, 29, 31, 37, 41, 43, 47, 53,
68 59, 61, 67, 71, 73, 79, 83, 89,
69 97, 101, 103, 107, 109, 113, 127, 131,
70 137, 139, 149, 151, 157, 163, 167, 173,
71 179, 181, 191, 193, 197, 199, 211, 223,
72 227, 229, 233, 239, 241, 251,
73#ifndef EIGHT_BIT
74 257, 263,
75 269, 271, 277, 281, 283, 293, 307, 311,
76 313, 317, 331, 337, 347, 349, 353, 359,
77 367, 373, 379, 383, 389, 397, 401, 409,
78 419, 421, 431, 433, 439, 443, 449, 457,
79 461, 463, 467, 479, 487, 491, 499, 503,
80 509, 521, 523, 541, 547, 557, 563, 569,
81 571, 577, 587, 593, 599, 601, 607, 613,
82 617, 619, 631, 641, 643, 647, 653, 659,
83 661, 673, 677, 683, 691, 701, 709, 719,
84 727, 733, 739, 743, 751, 757, 761, 769,
85 773, 787, 797, 809, 811, 821, 823, 827,
86 829, 839, 853, 857, 859, 863, 877, 881,
87 883, 887, 907, 911, 919, 929, 937, 941,
88 947, 953, 967, 971, 977, 983, 991, 997,
89 1009,1013,1019,1021,1031,1033,1039,1049,
90 1051,1061,1063,1069,1087,1091,1093,1097,
91 1103,1109,1117,1123,1129,1151,1153,1163,
92 1171,1181,1187,1193,1201,1213,1217,1223,
93 1229,1231,1237,1249,1259,1277,1279,1283,
94 1289,1291,1297,1301,1303,1307,1319,1321,
95 1327,1361,1367,1373,1381,1399,1409,1423,
96 1427,1429,1433,1439,1447,1451,1453,1459,
97 1471,1481,1483,1487,1489,1493,1499,1511,
98 1523,1531,1543,1549,1553,1559,1567,1571,
99 1579,1583,1597,1601,1607,1609,1613,1619,
100 1621,1627,1637,1657,1663,1667,1669,1693,
101 1697,1699,1709,1721,1723,1733,1741,1747,
102 1753,1759,1777,1783,1787,1789,1801,1811,
103 1823,1831,1847,1861,1867,1871,1873,1877,
104 1879,1889,1901,1907,1913,1931,1933,1949,
105 1951,1973,1979,1987,1993,1997,1999,2003,
106 2011,2017,2027,2029,2039,2053,2063,2069,
107 2081,2083,2087,2089,2099,2111,2113,2129,
108 2131,2137,2141,2143,2153,2161,2179,2203,
109 2207,2213,2221,2237,2239,2243,2251,2267,
110 2269,2273,2281,2287,2293,2297,2309,2311,
111 2333,2339,2341,2347,2351,2357,2371,2377,
112 2381,2383,2389,2393,2399,2411,2417,2423,
113 2437,2441,2447,2459,2467,2473,2477,2503,
114 2521,2531,2539,2543,2549,2551,2557,2579,
115 2591,2593,2609,2617,2621,2633,2647,2657,
116 2659,2663,2671,2677,2683,2687,2689,2693,
117 2699,2707,2711,2713,2719,2729,2731,2741,
118 2749,2753,2767,2777,2789,2791,2797,2801,
119 2803,2819,2833,2837,2843,2851,2857,2861,
120 2879,2887,2897,2903,2909,2917,2927,2939,
121 2953,2957,2963,2969,2971,2999,3001,3011,
122 3019,3023,3037,3041,3049,3061,3067,3079,
123 3083,3089,3109,3119,3121,3137,3163,3167,
124 3169,3181,3187,3191,3203,3209,3217,3221,
125 3229,3251,3253,3257,3259,3271,3299,3301,
126 3307,3313,3319,3323,3329,3331,3343,3347,
127 3359,3361,3371,3373,3389,3391,3407,3413,
128 3433,3449,3457,3461,3463,3467,3469,3491,
129 3499,3511,3517,3527,3529,3533,3539,3541,
130 3547,3557,3559,3571,3581,3583,3593,3607,
131 3613,3617,3623,3631,3637,3643,3659,3671,
132 3673,3677,3691,3697,3701,3709,3719,3727,
133 3733,3739,3761,3767,3769,3779,3793,3797,
134 3803,3821,3823,3833,3847,3851,3853,3863,
135 3877,3881,3889,3907,3911,3917,3919,3923,
136 3929,3931,3943,3947,3967,3989,4001,4003,
137 4007,4013,4019,4021,4027,4049,4051,4057,
138 4073,4079,4091,4093,4099,4111,4127,4129,
139 4133,4139,4153,4157,4159,4177,4201,4211,
140 4217,4219,4229,4231,4241,4243,4253,4259,
141 4261,4271,4273,4283,4289,4297,4327,4337,
142 4339,4349,4357,4363,4373,4391,4397,4409,
143 4421,4423,4441,4447,4451,4457,4463,4481,
144 4483,4493,4507,4513,4517,4519,4523,4547,
145 4549,4561,4567,4583,4591,4597,4603,4621,
146 4637,4639,4643,4649,4651,4657,4663,4673,
147 4679,4691,4703,4721,4723,4729,4733,4751,
148 4759,4783,4787,4789,4793,4799,4801,4813,
149 4817,4831,4861,4871,4877,4889,4903,4909,
150 4919,4931,4933,4937,4943,4951,4957,4967,
151 4969,4973,4987,4993,4999,5003,5009,5011,
152 5021,5023,5039,5051,5059,5077,5081,5087,
153 5099,5101,5107,5113,5119,5147,5153,5167,
154 5171,5179,5189,5197,5209,5227,5231,5233,
155 5237,5261,5273,5279,5281,5297,5303,5309,
156 5323,5333,5347,5351,5381,5387,5393,5399,
157 5407,5413,5417,5419,5431,5437,5441,5443,
158 5449,5471,5477,5479,5483,5501,5503,5507,
159 5519,5521,5527,5531,5557,5563,5569,5573,
160 5581,5591,5623,5639,5641,5647,5651,5653,
161 5657,5659,5669,5683,5689,5693,5701,5711,
162 5717,5737,5741,5743,5749,5779,5783,5791,
163 5801,5807,5813,5821,5827,5839,5843,5849,
164 5851,5857,5861,5867,5869,5879,5881,5897,
165 5903,5923,5927,5939,5953,5981,5987,6007,
166 6011,6029,6037,6043,6047,6053,6067,6073,
167 6079,6089,6091,6101,6113,6121,6131,6133,
168 6143,6151,6163,6173,6197,6199,6203,6211,
169 6217,6221,6229,6247,6257,6263,6269,6271,
170 6277,6287,6299,6301,6311,6317,6323,6329,
171 6337,6343,6353,6359,6361,6367,6373,6379,
172 6389,6397,6421,6427,6449,6451,6469,6473,
173 6481,6491,6521,6529,6547,6551,6553,6563,
174 6569,6571,6577,6581,6599,6607,6619,6637,
175 6653,6659,6661,6673,6679,6689,6691,6701,
176 6703,6709,6719,6733,6737,6761,6763,6779,
177 6781,6791,6793,6803,6823,6827,6829,6833,
178 6841,6857,6863,6869,6871,6883,6899,6907,
179 6911,6917,6947,6949,6959,6961,6967,6971,
180 6977,6983,6991,6997,7001,7013,7019,7027,
181 7039,7043,7057,7069,7079,7103,7109,7121,
182 7127,7129,7151,7159,7177,7187,7193,7207,
183 7211,7213,7219,7229,7237,7243,7247,7253,
184 7283,7297,7307,7309,7321,7331,7333,7349,
185 7351,7369,7393,7411,7417,7433,7451,7457,
186 7459,7477,7481,7487,7489,7499,7507,7517,
187 7523,7529,7537,7541,7547,7549,7559,7561,
188 7573,7577,7583,7589,7591,7603,7607,7621,
189 7639,7643,7649,7669,7673,7681,7687,7691,
190 7699,7703,7717,7723,7727,7741,7753,7757,
191 7759,7789,7793,7817,7823,7829,7841,7853,
192 7867,7873,7877,7879,7883,7901,7907,7919,
193 7927,7933,7937,7949,7951,7963,7993,8009,
194 8011,8017,8039,8053,8059,8069,8081,8087,
195 8089,8093,8101,8111,8117,8123,8147,8161,
196 8167,8171,8179,8191,8209,8219,8221,8231,
197 8233,8237,8243,8263,8269,8273,8287,8291,
198 8293,8297,8311,8317,8329,8353,8363,8369,
199 8377,8387,8389,8419,8423,8429,8431,8443,
200 8447,8461,8467,8501,8513,8521,8527,8537,
201 8539,8543,8563,8573,8581,8597,8599,8609,
202 8623,8627,8629,8641,8647,8663,8669,8677,
203 8681,8689,8693,8699,8707,8713,8719,8731,
204 8737,8741,8747,8753,8761,8779,8783,8803,
205 8807,8819,8821,8831,8837,8839,8849,8861,
206 8863,8867,8887,8893,8923,8929,8933,8941,
207 8951,8963,8969,8971,8999,9001,9007,9011,
208 9013,9029,9041,9043,9049,9059,9067,9091,
209 9103,9109,9127,9133,9137,9151,9157,9161,
210 9173,9181,9187,9199,9203,9209,9221,9227,
211 9239,9241,9257,9277,9281,9283,9293,9311,
212 9319,9323,9337,9341,9343,9349,9371,9377,
213 9391,9397,9403,9413,9419,9421,9431,9433,
214 9437,9439,9461,9463,9467,9473,9479,9491,
215 9497,9511,9521,9533,9539,9547,9551,9587,
216 9601,9613,9619,9623,9629,9631,9643,9649,
217 9661,9677,9679,9689,9697,9719,9721,9733,
218 9739,9743,9749,9767,9769,9781,9787,9791,
219 9803,9811,9817,9829,9833,9839,9851,9857,
220 9859,9871,9883,9887,9901,9907,9923,9929,
221 9931,9941,9949,9967,9973,10007,10009,10037,
222 10039,10061,10067,10069,10079,10091,10093,10099,
223 10103,10111,10133,10139,10141,10151,10159,10163,
224 10169,10177,10181,10193,10211,10223,10243,10247,
225 10253,10259,10267,10271,10273,10289,10301,10303,
226 10313,10321,10331,10333,10337,10343,10357,10369,
227 10391,10399,10427,10429,10433,10453,10457,10459,
228 10463,10477,10487,10499,10501,10513,10529,10531,
229 10559,10567,10589,10597,10601,10607,10613,10627,
230 10631,10639,10651,10657,10663,10667,10687,10691,
231 10709,10711,10723,10729,10733,10739,10753,10771,
232 10781,10789,10799,10831,10837,10847,10853,10859,
233 10861,10867,10883,10889,10891,10903,10909,10937,
234 10939,10949,10957,10973,10979,10987,10993,11003,
235 11027,11047,11057,11059,11069,11071,11083,11087,
236 11093,11113,11117,11119,11131,11149,11159,11161,
237 11171,11173,11177,11197,11213,11239,11243,11251,
238 11257,11261,11273,11279,11287,11299,11311,11317,
239 11321,11329,11351,11353,11369,11383,11393,11399,
240 11411,11423,11437,11443,11447,11467,11471,11483,
241 11489,11491,11497,11503,11519,11527,11549,11551,
242 11579,11587,11593,11597,11617,11621,11633,11657,
243 11677,11681,11689,11699,11701,11717,11719,11731,
244 11743,11777,11779,11783,11789,11801,11807,11813,
245 11821,11827,11831,11833,11839,11863,11867,11887,
246 11897,11903,11909,11923,11927,11933,11939,11941,
247 11953,11959,11969,11971,11981,11987,12007,12011,
248 12037,12041,12043,12049,12071,12073,12097,12101,
249 12107,12109,12113,12119,12143,12149,12157,12161,
250 12163,12197,12203,12211,12227,12239,12241,12251,
251 12253,12263,12269,12277,12281,12289,12301,12323,
252 12329,12343,12347,12373,12377,12379,12391,12401,
253 12409,12413,12421,12433,12437,12451,12457,12473,
254 12479,12487,12491,12497,12503,12511,12517,12527,
255 12539,12541,12547,12553,12569,12577,12583,12589,
256 12601,12611,12613,12619,12637,12641,12647,12653,
257 12659,12671,12689,12697,12703,12713,12721,12739,
258 12743,12757,12763,12781,12791,12799,12809,12821,
259 12823,12829,12841,12853,12889,12893,12899,12907,
260 12911,12917,12919,12923,12941,12953,12959,12967,
261 12973,12979,12983,13001,13003,13007,13009,13033,
262 13037,13043,13049,13063,13093,13099,13103,13109,
263 13121,13127,13147,13151,13159,13163,13171,13177,
264 13183,13187,13217,13219,13229,13241,13249,13259,
265 13267,13291,13297,13309,13313,13327,13331,13337,
266 13339,13367,13381,13397,13399,13411,13417,13421,
267 13441,13451,13457,13463,13469,13477,13487,13499,
268 13513,13523,13537,13553,13567,13577,13591,13597,
269 13613,13619,13627,13633,13649,13669,13679,13681,
270 13687,13691,13693,13697,13709,13711,13721,13723,
271 13729,13751,13757,13759,13763,13781,13789,13799,
272 13807,13829,13831,13841,13859,13873,13877,13879,
273 13883,13901,13903,13907,13913,13921,13931,13933,
274 13963,13967,13997,13999,14009,14011,14029,14033,
275 14051,14057,14071,14081,14083,14087,14107,14143,
276 14149,14153,14159,14173,14177,14197,14207,14221,
277 14243,14249,14251,14281,14293,14303,14321,14323,
278 14327,14341,14347,14369,14387,14389,14401,14407,
279 14411,14419,14423,14431,14437,14447,14449,14461,
280 14479,14489,14503,14519,14533,14537,14543,14549,
281 14551,14557,14561,14563,14591,14593,14621,14627,
282 14629,14633,14639,14653,14657,14669,14683,14699,
283 14713,14717,14723,14731,14737,14741,14747,14753,
284 14759,14767,14771,14779,14783,14797,14813,14821,
285 14827,14831,14843,14851,14867,14869,14879,14887,
286 14891,14897,14923,14929,14939,14947,14951,14957,
287 14969,14983,15013,15017,15031,15053,15061,15073,
288 15077,15083,15091,15101,15107,15121,15131,15137,
289 15139,15149,15161,15173,15187,15193,15199,15217,
290 15227,15233,15241,15259,15263,15269,15271,15277,
291 15287,15289,15299,15307,15313,15319,15329,15331,
292 15349,15359,15361,15373,15377,15383,15391,15401,
293 15413,15427,15439,15443,15451,15461,15467,15473,
294 15493,15497,15511,15527,15541,15551,15559,15569,
295 15581,15583,15601,15607,15619,15629,15641,15643,
296 15647,15649,15661,15667,15671,15679,15683,15727,
297 15731,15733,15737,15739,15749,15761,15767,15773,
298 15787,15791,15797,15803,15809,15817,15823,15859,
299 15877,15881,15887,15889,15901,15907,15913,15919,
300 15923,15937,15959,15971,15973,15991,16001,16007,
301 16033,16057,16061,16063,16067,16069,16073,16087,
302 16091,16097,16103,16111,16127,16139,16141,16183,
303 16187,16189,16193,16217,16223,16229,16231,16249,
304 16253,16267,16273,16301,16319,16333,16339,16349,
305 16361,16363,16369,16381,16411,16417,16421,16427,
306 16433,16447,16451,16453,16477,16481,16487,16493,
307 16519,16529,16547,16553,16561,16567,16573,16603,
308 16607,16619,16631,16633,16649,16651,16657,16661,
309 16673,16691,16693,16699,16703,16729,16741,16747,
310 16759,16763,16787,16811,16823,16829,16831,16843,
311 16871,16879,16883,16889,16901,16903,16921,16927,
312 16931,16937,16943,16963,16979,16981,16987,16993,
313 17011,17021,17027,17029,17033,17041,17047,17053,
314 17077,17093,17099,17107,17117,17123,17137,17159,
315 17167,17183,17189,17191,17203,17207,17209,17231,
316 17239,17257,17291,17293,17299,17317,17321,17327,
317 17333,17341,17351,17359,17377,17383,17387,17389,
318 17393,17401,17417,17419,17431,17443,17449,17467,
319 17471,17477,17483,17489,17491,17497,17509,17519,
320 17539,17551,17569,17573,17579,17581,17597,17599,
321 17609,17623,17627,17657,17659,17669,17681,17683,
322 17707,17713,17729,17737,17747,17749,17761,17783,
323 17789,17791,17807,17827,17837,17839,17851,17863,
324#endif
325 };
diff --git a/src/lib/libcrypto/bn/bn_prime.pl b/src/lib/libcrypto/bn/bn_prime.pl
new file mode 100644
index 0000000000..1b00c21a77
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_prime.pl
@@ -0,0 +1,56 @@
1#!/usr/bin/perl
2# bn_prime.pl
3
4$num=2048;
5$num=$ARGV[0] if ($#ARGV >= 0);
6
7push(@primes,2);
8$p=1;
9loop: while ($#primes < $num-1)
10 {
11 $p+=2;
12 $s=int(sqrt($p));
13
14 for ($i=0; $primes[$i]<=$s; $i++)
15 {
16 next loop if (($p%$primes[$i]) == 0);
17 }
18 push(@primes,$p);
19 }
20
21print <<"EOF";
22/* Auto generated by bn_prime.pl */
23/* Copyright (C) 1995-1997 Eric Young (eay\@mincom.oz.au).
24 * All rights reserved.
25 * Copyright remains Eric Young's, and as such any Copyright notices in
26 * the code are not to be removed.
27 * See the COPYRIGHT file in the SSLeay distribution for more details.
28 */
29
30EOF
31
32for ($i=0; $i <= $#primes; $i++)
33 {
34 if ($primes[$i] > 256)
35 {
36 $eight=$i;
37 last;
38 }
39 }
40
41printf "#ifndef EIGHT_BIT\n";
42printf "#define NUMPRIMES %d\n",$num;
43printf "#else\n";
44printf "#define NUMPRIMES %d\n",$eight;
45printf "#endif\n";
46print "static unsigned int primes[NUMPRIMES]=\n\t{\n\t";
47$init=0;
48for ($i=0; $i <= $#primes; $i++)
49 {
50 printf "\n#ifndef EIGHT_BIT\n\t" if ($primes[$i] > 256) && !($init++);
51 printf("\n\t") if (($i%8) == 0) && ($i != 0);
52 printf("%4d,",$primes[$i]);
53 }
54print "\n#endif\n\t};\n";
55
56
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c
new file mode 100644
index 0000000000..2bcc11c852
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_print.c
@@ -0,0 +1,333 @@
1/* crypto/bn/bn_print.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <ctype.h>
61#include "cryptlib.h"
62#include "buffer.h"
63#include "bn_lcl.h"
64
65static char *Hex="0123456789ABCDEF";
66
67/* Must 'Free' the returned data */
68char *BN_bn2hex(a)
69BIGNUM *a;
70 {
71 int i,j,v,z=0;
72 char *buf;
73 char *p;
74
75 buf=(char *)Malloc(a->top*BN_BYTES*2+2);
76 if (buf == NULL)
77 {
78 BNerr(BN_F_BN_BN2HEX,ERR_R_MALLOC_FAILURE);
79 goto err;
80 }
81 p=buf;
82 if (a->neg) *(p++)='-';
83 if (a->top == 0) *(p++)='0';
84 for (i=a->top-1; i >=0; i--)
85 {
86 for (j=BN_BITS2-8; j >= 0; j-=8)
87 {
88 /* strip leading zeros */
89 v=((int)(a->d[i]>>(long)j))&0xff;
90 if (z || (v != 0))
91 {
92 *(p++)=Hex[v>>4];
93 *(p++)=Hex[v&0x0f];
94 z=1;
95 }
96 }
97 }
98 *p='\0';
99err:
100 return(buf);
101 }
102
103/* Must 'Free' the returned data */
104char *BN_bn2dec(a)
105BIGNUM *a;
106 {
107 int i=0,num;
108 char *buf=NULL;
109 char *p;
110 BIGNUM *t=NULL;
111 BN_ULONG *bn_data=NULL,*lp;
112
113 i=BN_num_bits(a)*3;
114 num=(i/10+i/1000+3)+1;
115 bn_data=(BN_ULONG *)Malloc((num/BN_DEC_NUM+1)*sizeof(BN_ULONG));
116 buf=(char *)Malloc(num+3);
117 if ((buf == NULL) || (bn_data == NULL))
118 {
119 BNerr(BN_F_BN_BN2DEC,ERR_R_MALLOC_FAILURE);
120 goto err;
121 }
122 if ((t=BN_dup(a)) == NULL) goto err;
123
124 p=buf;
125 lp=bn_data;
126 if (t->neg) *(p++)='-';
127 if (t->top == 0)
128 {
129 *(p++)='0';
130 *(p++)='\0';
131 }
132 else
133 {
134 i=0;
135 while (!BN_is_zero(t))
136 {
137 *lp=BN_div_word(t,BN_DEC_CONV);
138 lp++;
139 }
140 lp--;
141 /* We now have a series of blocks, BN_DEC_NUM chars
142 * in length, where the last one needs trucation.
143 * The blocks need to be reversed in order. */
144 sprintf(p,BN_DEC_FMT1,*lp);
145 while (*p) p++;
146 while (lp != bn_data)
147 {
148 lp--;
149 sprintf(p,BN_DEC_FMT2,*lp);
150 while (*p) p++;
151 }
152 }
153err:
154 if (bn_data != NULL) Free(bn_data);
155 if (t != NULL) BN_free(t);
156 return(buf);
157 }
158
159int BN_hex2bn(bn,a)
160BIGNUM **bn;
161char *a;
162 {
163 BIGNUM *ret=NULL;
164 BN_ULONG l=0;
165 int neg=0,h,m,i,j,k,c;
166 int num;
167
168 if ((a == NULL) || (*a == '\0')) return(0);
169
170 if (*a == '-') { neg=1; a++; }
171
172 for (i=0; isxdigit(a[i]); i++)
173 ;
174
175 num=i+neg;
176 if (bn == NULL) return(num);
177
178 /* a is the start of the hex digets, and it is 'i' long */
179 if (*bn == NULL)
180 {
181 if ((ret=BN_new()) == NULL) return(0);
182 }
183 else
184 {
185 ret= *bn;
186 BN_zero(ret);
187 }
188
189 /* i is the number of hex digests; */
190 if (bn_expand(ret,i*4) == NULL) goto err;
191
192 j=i; /* least significate 'hex' */
193 m=0;
194 h=0;
195 while (j > 0)
196 {
197 m=((BN_BYTES*2) <= j)?(BN_BYTES*2):j;
198 l=0;
199 for (;;)
200 {
201 c=a[j-m];
202 if ((c >= '0') && (c <= '9')) k=c-'0';
203 else if ((c >= 'a') && (c <= 'f')) k=c-'a'+10;
204 else if ((c >= 'A') && (c <= 'F')) k=c-'A'+10;
205 else k=0; /* paranoia */
206 l=(l<<4)|k;
207
208 if (--m <= 0)
209 {
210 ret->d[h++]=l;
211 break;
212 }
213 }
214 j-=(BN_BYTES*2);
215 }
216 ret->top=h;
217 bn_fix_top(ret);
218 ret->neg=neg;
219
220 *bn=ret;
221 return(num);
222err:
223 if (*bn == NULL) BN_free(ret);
224 return(0);
225 }
226
227int BN_dec2bn(bn,a)
228BIGNUM **bn;
229char *a;
230 {
231 BIGNUM *ret=NULL;
232 BN_ULONG l=0;
233 int neg=0,i,j;
234 int num;
235
236 if ((a == NULL) || (*a == '\0')) return(0);
237 if (*a == '-') { neg=1; a++; }
238
239 for (i=0; isdigit(a[i]); i++)
240 ;
241
242 num=i+neg;
243 if (bn == NULL) return(num);
244
245 /* a is the start of the digets, and it is 'i' long.
246 * We chop it into BN_DEC_NUM digets at a time */
247 if (*bn == NULL)
248 {
249 if ((ret=BN_new()) == NULL) return(0);
250 }
251 else
252 {
253 ret= *bn;
254 BN_zero(ret);
255 }
256
257 /* i is the number of digests, a bit of an over expand; */
258 if (bn_expand(ret,i*4) == NULL) goto err;
259
260 j=BN_DEC_NUM-(i%BN_DEC_NUM);
261 if (j == BN_DEC_NUM) j=0;
262 l=0;
263 while (*a)
264 {
265 l*=10;
266 l+= *a-'0';
267 a++;
268 if (++j == BN_DEC_NUM)
269 {
270 BN_mul_word(ret,BN_DEC_CONV);
271 BN_add_word(ret,l);
272 l=0;
273 j=0;
274 }
275 }
276 ret->neg=neg;
277
278 bn_fix_top(ret);
279 *bn=ret;
280 return(num);
281err:
282 if (*bn == NULL) BN_free(ret);
283 return(0);
284 }
285
286#ifndef NO_BIO
287
288#ifndef NO_FP_API
289int BN_print_fp(fp, a)
290FILE *fp;
291BIGNUM *a;
292 {
293 BIO *b;
294 int ret;
295
296 if ((b=BIO_new(BIO_s_file())) == NULL)
297 return(0);
298 BIO_set_fp(b,fp,BIO_NOCLOSE);
299 ret=BN_print(b,a);
300 BIO_free(b);
301 return(ret);
302 }
303#endif
304
305int BN_print(bp, a)
306BIO *bp;
307BIGNUM *a;
308 {
309 int i,j,v,z=0;
310 int ret=0;
311
312 if ((a->neg) && (BIO_write(bp,"-",1) != 1)) goto end;
313 if ((a->top == 0) && (BIO_write(bp,"0",1) != 1)) goto end;
314 for (i=a->top-1; i >=0; i--)
315 {
316 for (j=BN_BITS2-4; j >= 0; j-=4)
317 {
318 /* strip leading zeros */
319 v=((int)(a->d[i]>>(long)j))&0x0f;
320 if (z || (v != 0))
321 {
322 if (BIO_write(bp,&(Hex[v]),1) != 1)
323 goto end;
324 z=1;
325 }
326 }
327 }
328 ret=1;
329end:
330 return(ret);
331 }
332
333#endif
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c
new file mode 100644
index 0000000000..75b6b0493b
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_rand.c
@@ -0,0 +1,121 @@
1/* crypto/bn/bn_rand.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include "cryptlib.h"
62#include "bn_lcl.h"
63#include "rand.h"
64
65int BN_rand(rnd, bits, top, bottom)
66BIGNUM *rnd;
67int bits;
68int top;
69int bottom;
70 {
71 unsigned char *buf=NULL;
72 int ret=0,bit,bytes,mask;
73 time_t tim;
74
75 bytes=(bits+7)/8;
76 bit=(bits-1)%8;
77 mask=0xff<<bit;
78
79 buf=(unsigned char *)Malloc(bytes);
80 if (buf == NULL)
81 {
82 BNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);
83 goto err;
84 }
85
86 /* make a random number and set the top and bottom bits */
87 time(&tim);
88 RAND_seed((unsigned char *)&tim,sizeof(tim));
89
90 RAND_bytes(buf,(int)bytes);
91 if (top)
92 {
93 if (bit == 0)
94 {
95 buf[0]=1;
96 buf[1]|=0x80;
97 }
98 else
99 {
100 buf[0]|=(3<<(bit-1));
101 buf[0]&= ~(mask<<1);
102 }
103 }
104 else
105 {
106 buf[0]|=(1<<bit);
107 buf[0]&= ~(mask<<1);
108 }
109 if (bottom) /* set bottom bits to whatever odd is */
110 buf[bytes-1]|=1;
111 if (!BN_bin2bn(buf,bytes,rnd)) goto err;
112 ret=1;
113err:
114 if (buf != NULL)
115 {
116 memset(buf,0,bytes);
117 Free(buf);
118 }
119 return(ret);
120 }
121
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c
new file mode 100644
index 0000000000..72cd69d3fc
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_recp.c
@@ -0,0 +1,125 @@
1/* crypto/bn/bn_recp.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63int BN_mod_mul_reciprocal(r, x, y, m, i, nb, ctx)
64BIGNUM *r;
65BIGNUM *x;
66BIGNUM *y;
67BIGNUM *m;
68BIGNUM *i;
69int nb;
70BN_CTX *ctx;
71 {
72 int ret=0,j;
73 BIGNUM *a,*b,*c,*d;
74
75 a=ctx->bn[ctx->tos++];
76 b=ctx->bn[ctx->tos++];
77 c=ctx->bn[ctx->tos++];
78 d=ctx->bn[ctx->tos++];
79
80 if (x == y)
81 { if (!BN_sqr(a,x,ctx)) goto err; }
82 else
83 { if (!BN_mul(a,x,y)) goto err; }
84 if (!BN_rshift(d,a,nb)) goto err;
85 if (!BN_mul(b,d,i)) goto err;
86 if (!BN_rshift(c,b,nb)) goto err;
87 if (!BN_mul(b,m,c)) goto err;
88 if (!BN_sub(r,a,b)) goto err;
89 j=0;
90 while (BN_cmp(r,m) >= 0)
91 {
92 if (j++ > 2)
93 {
94 BNerr(BN_F_BN_MOD_MUL_RECIPROCAL,BN_R_BAD_RECIPROCAL);
95 goto err;
96 }
97 if (!BN_sub(r,r,m)) goto err;
98 }
99
100 ret=1;
101err:
102 ctx->tos-=4;
103 return(ret);
104 }
105
106int BN_reciprocal(r, m,ctx)
107BIGNUM *r;
108BIGNUM *m;
109BN_CTX *ctx;
110 {
111 int nm,ret= -1;
112 BIGNUM *t;
113
114 t=ctx->bn[ctx->tos++];
115
116 nm=BN_num_bits(m);
117 if (!BN_lshift(t,BN_value_one(),nm*2)) goto err;
118
119 if (!BN_div(r,NULL,t,m,ctx)) goto err;
120 ret=nm;
121err:
122 ctx->tos--;
123 return(ret);
124 }
125
diff --git a/src/lib/libcrypto/bn/bn_shift.c b/src/lib/libcrypto/bn/bn_shift.c
new file mode 100644
index 0000000000..944bf1794b
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_shift.c
@@ -0,0 +1,210 @@
1/* crypto/bn/bn_shift.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63int BN_lshift1(r, a)
64BIGNUM *r;
65BIGNUM *a;
66 {
67 register BN_ULONG *ap,*rp,t,c;
68 int i;
69
70 if (r != a)
71 {
72 r->neg=a->neg;
73 if (bn_wexpand(r,a->top+1) == NULL) return(0);
74 r->top=a->top;
75 }
76 else
77 {
78 if (bn_wexpand(r,a->top+1) == NULL) return(0);
79 }
80 ap=a->d;
81 rp=r->d;
82 c=0;
83 for (i=0; i<a->top; i++)
84 {
85 t= *(ap++);
86 *(rp++)=((t<<1)|c)&BN_MASK2;
87 c=(t & BN_TBIT)?1:0;
88 }
89 if (c)
90 {
91 *rp=1;
92 r->top++;
93 }
94 return(1);
95 }
96
97int BN_rshift1(r, a)
98BIGNUM *r;
99BIGNUM *a;
100 {
101 BN_ULONG *ap,*rp,t,c;
102 int i;
103
104 if (BN_is_zero(a))
105 {
106 BN_zero(r);
107 return(1);
108 }
109 if (a != r)
110 {
111 if (bn_wexpand(r,a->top) == NULL) return(0);
112 r->top=a->top;
113 r->neg=a->neg;
114 }
115 ap=a->d;
116 rp=r->d;
117 c=0;
118 for (i=a->top-1; i>=0; i--)
119 {
120 t=ap[i];
121 rp[i]=((t>>1)&BN_MASK2)|c;
122 c=(t&1)?BN_TBIT:0;
123 }
124 bn_fix_top(r);
125 return(1);
126 }
127
128int BN_lshift(r, a, n)
129BIGNUM *r;
130BIGNUM *a;
131int n;
132 {
133 int i,nw,lb,rb;
134 BN_ULONG *t,*f;
135 BN_ULONG l;
136
137 r->neg=a->neg;
138 if (bn_wexpand(r,a->top+(n/BN_BITS2)+1) == NULL) return(0);
139 nw=n/BN_BITS2;
140 lb=n%BN_BITS2;
141 rb=BN_BITS2-lb;
142 f=a->d;
143 t=r->d;
144 t[a->top+nw]=0;
145 if (lb == 0)
146 for (i=a->top-1; i>=0; i--)
147 t[nw+i]=f[i];
148 else
149 for (i=a->top-1; i>=0; i--)
150 {
151 l=f[i];
152 t[nw+i+1]|=(l>>rb)&BN_MASK2;
153 t[nw+i]=(l<<lb)&BN_MASK2;
154 }
155 memset(t,0,nw*sizeof(t[0]));
156/* for (i=0; i<nw; i++)
157 t[i]=0;*/
158 r->top=a->top+nw+1;
159 bn_fix_top(r);
160 return(1);
161 }
162
163int BN_rshift(r, a, n)
164BIGNUM *r;
165BIGNUM *a;
166int n;
167 {
168 int i,j,nw,lb,rb;
169 BN_ULONG *t,*f;
170 BN_ULONG l,tmp;
171
172 nw=n/BN_BITS2;
173 rb=n%BN_BITS2;
174 lb=BN_BITS2-rb;
175 if (nw > a->top)
176 {
177 BN_zero(r);
178 return(1);
179 }
180 if (r != a)
181 {
182 r->neg=a->neg;
183 if (bn_wexpand(r,a->top-nw+1) == NULL) return(0);
184 }
185
186 f= &(a->d[nw]);
187 t=r->d;
188 j=a->top-nw;
189 r->top=j;
190
191 if (rb == 0)
192 {
193 for (i=j+1; i > 0; i--)
194 *(t++)= *(f++);
195 }
196 else
197 {
198 l= *(f++);
199 for (i=1; i<j; i++)
200 {
201 tmp =(l>>rb)&BN_MASK2;
202 l= *(f++);
203 *(t++) =(tmp|(l<<lb))&BN_MASK2;
204 }
205 *(t++) =(l>>rb)&BN_MASK2;
206 }
207 *t=0;
208 bn_fix_top(r);
209 return(1);
210 }
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c
new file mode 100644
index 0000000000..a8464610e5
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_sqr.c
@@ -0,0 +1,122 @@
1/* crypto/bn/bn_sqr.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63/* r must not be a */
64/* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */
65int BN_sqr(r, a, ctx)
66BIGNUM *r;
67BIGNUM *a;
68BN_CTX *ctx;
69 {
70 int i,j,max,al;
71 BIGNUM *tmp;
72 BN_ULONG *ap,*rp;
73
74 tmp=ctx->bn[ctx->tos];
75
76 al=a->top;
77 if (al == 0)
78 {
79 r->top=0;
80 return(1);
81 }
82
83 max=(al*2);
84 if (bn_wexpand(r,1+max) == NULL) return(0);
85 if (bn_wexpand(tmp,1+max) == NULL) return(0);
86
87 r->neg=0;
88
89 ap=a->d;
90 rp=r->d;
91 rp[0]=rp[max-1]=0;
92 rp++;
93 j=al;
94
95 if (--j > 0)
96 {
97 ap++;
98 rp[j]=bn_mul_words(rp,ap,j,ap[-1]);
99 rp+=2;
100 }
101
102 for (i=2; i<al; i++)
103 {
104 j--;
105 ap++;
106 rp[j]=bn_mul_add_words(rp,ap,j,ap[-1]);
107 rp+=2;
108 }
109
110 bn_add_words(r->d,r->d,r->d,max);
111
112 /* There will not be a carry */
113
114 bn_sqr_words(tmp->d,a->d,al);
115
116 bn_add_words(r->d,r->d,tmp->d,max);
117
118 r->top=max;
119 if (r->d[max-1] == 0) r->top--;
120 return(1);
121 }
122
diff --git a/src/lib/libcrypto/bn/bn_word.c b/src/lib/libcrypto/bn/bn_word.c
new file mode 100644
index 0000000000..4b3d0f011d
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_word.c
@@ -0,0 +1,204 @@
1/* crypto/bn/bn_word.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63BN_ULONG BN_mod_word(a, w)
64BIGNUM *a;
65unsigned long w;
66 {
67#ifndef BN_LLONG
68 BN_ULONG ret=0;
69#else
70 BN_ULLONG ret=0;
71#endif
72 int i;
73
74 w&=BN_MASK2;
75 for (i=a->top-1; i>=0; i--)
76 {
77#ifndef BN_LLONG
78 ret=((ret<<BN_BITS4)|((a->d[i]>>BN_BITS4)&BN_MASK2l))%(unsigned long)w;
79 ret=((ret<<BN_BITS4)|(a->d[i]&BN_MASK2l))%(unsigned long)w;
80#else
81 ret=(BN_ULLONG)(((ret<<(BN_ULLONG)BN_BITS2)|a->d[i])%
82 (BN_ULLONG)w);
83#endif
84 }
85 return((BN_ULONG)ret);
86 }
87
88BN_ULONG BN_div_word(a, w)
89BIGNUM *a;
90unsigned long w;
91 {
92 BN_ULONG ret;
93 int i;
94
95 if (a->top == 0) return(0);
96 ret=0;
97 w&=BN_MASK2;
98 for (i=a->top-1; i>=0; i--)
99 {
100 BN_ULONG l,d;
101
102 l=a->d[i];
103 d=bn_div64(ret,l,w);
104 ret=(l-((d*w)&BN_MASK2))&BN_MASK2;
105 a->d[i]=d;
106 }
107 if (a->d[a->top-1] == 0)
108 a->top--;
109 return(ret);
110 }
111
112int BN_add_word(a, w)
113BIGNUM *a;
114unsigned long w;
115 {
116 BN_ULONG l;
117 int i;
118
119 if (a->neg)
120 {
121 a->neg=0;
122 i=BN_sub_word(a,w);
123 if (!BN_is_zero(a))
124 a->neg=1;
125 return(i);
126 }
127 w&=BN_MASK2;
128 if (bn_wexpand(a,a->top+1) == NULL) return(0);
129 i=0;
130 for (;;)
131 {
132 l=(a->d[i]+(BN_ULONG)w)&BN_MASK2;
133 a->d[i]=l;
134 if (w > l)
135 w=1;
136 else
137 break;
138 i++;
139 }
140 if (i >= a->top)
141 a->top++;
142 return(1);
143 }
144
145int BN_sub_word(a, w)
146BIGNUM *a;
147unsigned long w;
148 {
149 int i;
150
151 if (a->neg)
152 {
153 a->neg=0;
154 i=BN_add_word(a,w);
155 a->neg=1;
156 return(i);
157 }
158
159 w&=BN_MASK2;
160 if ((a->top == 1) && (a->d[0] < w))
161 {
162 a->d[0]=w-a->d[0];
163 a->neg=1;
164 return(1);
165 }
166 i=0;
167 for (;;)
168 {
169 if (a->d[i] >= w)
170 {
171 a->d[i]-=w;
172 break;
173 }
174 else
175 {
176 a->d[i]=(a->d[i]-w)&BN_MASK2;
177 i++;
178 w=1;
179 }
180 }
181 if ((a->d[i] == 0) && (i == (a->top-1)))
182 a->top--;
183 return(1);
184 }
185
186int BN_mul_word(a,w)
187BIGNUM *a;
188unsigned long w;
189 {
190 BN_ULONG ll;
191
192 w&=BN_MASK2;
193 if (a->top)
194 {
195 ll=bn_mul_words(a->d,a->d,a->top,w);
196 if (ll)
197 {
198 if (bn_wexpand(a,a->top+1) == NULL) return(0);
199 a->d[a->top++]=ll;
200 }
201 }
202 return(0);
203 }
204
diff --git a/src/lib/libcrypto/buffer/buf_err.c b/src/lib/libcrypto/buffer/buf_err.c
new file mode 100644
index 0000000000..ff988852cc
--- /dev/null
+++ b/src/lib/libcrypto/buffer/buf_err.c
@@ -0,0 +1,87 @@
1/* lib/buf/buf_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "buffer.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA BUF_str_functs[]=
65 {
66{ERR_PACK(0,BUF_F_BUF_MEM_GROW,0), "BUF_MEM_grow"},
67{ERR_PACK(0,BUF_F_BUF_MEM_NEW,0), "BUF_MEM_new"},
68{ERR_PACK(0,BUF_F_BUF_STRDUP,0), "BUF_strdup"},
69{ERR_PACK(0,BUF_F_PXYCLNT_READ,0), "PXYCLNT_READ"},
70{0,NULL},
71 };
72
73#endif
74
75void ERR_load_BUF_strings()
76 {
77 static int init=1;
78
79 if (init);
80 {;
81 init=0;
82#ifndef NO_ERR
83 ERR_load_strings(ERR_LIB_BUF,BUF_str_functs);
84#endif
85
86 }
87 }
diff --git a/src/lib/libcrypto/buffer/buffer.c b/src/lib/libcrypto/buffer/buffer.c
new file mode 100644
index 0000000000..7e8af9e2fa
--- /dev/null
+++ b/src/lib/libcrypto/buffer/buffer.c
@@ -0,0 +1,145 @@
1/* crypto/buffer/buffer.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62
63BUF_MEM *BUF_MEM_new()
64 {
65 BUF_MEM *ret;
66
67 ret=(BUF_MEM *)Malloc(sizeof(BUF_MEM));
68 if (ret == NULL)
69 {
70 BUFerr(BUF_F_BUF_MEM_NEW,ERR_R_MALLOC_FAILURE);
71 return(NULL);
72 }
73 ret->length=0;
74 ret->max=0;
75 ret->data=NULL;
76 return(ret);
77 }
78
79void BUF_MEM_free(a)
80BUF_MEM *a;
81 {
82 if (a->data != NULL)
83 {
84 memset(a->data,0,(unsigned int)a->max);
85 Free(a->data);
86 }
87 Free(a);
88 }
89
90int BUF_MEM_grow(str, len)
91BUF_MEM *str;
92int len;
93 {
94 char *ret;
95 unsigned int n;
96
97 if (str->length >= len)
98 {
99 str->length=len;
100 return(len);
101 }
102 if (str->max >= len)
103 {
104 memset(&(str->data[str->length]),0,len-str->length);
105 str->length=len;
106 return(len);
107 }
108 n=(len+3)/3*4;
109 if (str->data == NULL)
110 ret=(char *)Malloc(n);
111 else
112 ret=(char *)Realloc(str->data,n);
113 if (ret == NULL)
114 {
115 BUFerr(BUF_F_BUF_MEM_GROW,ERR_R_MALLOC_FAILURE);
116 len=0;
117 }
118 else
119 {
120 str->data=ret;
121 str->length=len;
122 str->max=n;
123 }
124 return(len);
125 }
126
127char *BUF_strdup(str)
128char *str;
129 {
130 char *ret;
131 int n;
132
133 if (str == NULL) return(NULL);
134
135 n=strlen(str);
136 ret=Malloc(n+1);
137 if (ret == NULL)
138 {
139 BUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE);
140 return(NULL);
141 }
142 memcpy(ret,str,n+1);
143 return(ret);
144 }
145
diff --git a/src/lib/libcrypto/buffer/buffer.h b/src/lib/libcrypto/buffer/buffer.h
new file mode 100644
index 0000000000..417548c04a
--- /dev/null
+++ b/src/lib/libcrypto/buffer/buffer.h
@@ -0,0 +1,107 @@
1/* crypto/buffer/buffer.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_BUFFER_H
60#define HEADER_BUFFER_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66typedef struct buf_mem_st
67 {
68 int length; /* current number of bytes */
69 char *data;
70 int max; /* size of buffer */
71 } BUF_MEM;
72
73#ifndef NOPROTO
74BUF_MEM *BUF_MEM_new(void);
75void BUF_MEM_free(BUF_MEM *a);
76int BUF_MEM_grow(BUF_MEM *str, int len);
77char * BUF_strdup(char *str);
78
79void ERR_load_BUF_strings(void );
80
81#else
82
83BUF_MEM *BUF_MEM_new();
84void BUF_MEM_free();
85int BUF_MEM_grow();
86char * BUF_strdup();
87
88void ERR_load_BUF_strings();
89
90#endif
91
92/* BEGIN ERROR CODES */
93/* Error codes for the BUF functions. */
94
95/* Function codes. */
96#define BUF_F_BUF_MEM_GROW 100
97#define BUF_F_BUF_MEM_NEW 101
98#define BUF_F_BUF_STRDUP 102
99#define BUF_F_PXYCLNT_READ 103
100
101/* Reason codes. */
102
103#ifdef __cplusplus
104}
105#endif
106#endif
107
diff --git a/src/lib/libcrypto/cast/asm/cast-586.pl b/src/lib/libcrypto/cast/asm/cast-586.pl
new file mode 100644
index 0000000000..d0be004c99
--- /dev/null
+++ b/src/lib/libcrypto/cast/asm/cast-586.pl
@@ -0,0 +1,167 @@
1#!/usr/bin/perl
2
3# define for pentium pro friendly version
4$ppro=1;
5
6push(@INC,"perlasm","../../perlasm");
7require "x86asm.pl";
8require "cbc.pl";
9
10&asm_init($ARGV[0],"cast-586.pl");
11
12$CAST_ROUNDS=16;
13$L="edi";
14$R="esi";
15$K="ebp";
16$tmp1="ecx";
17$tmp2="ebx";
18$tmp3="eax";
19$tmp4="edx";
20$S1="CAST_S_table0";
21$S2="CAST_S_table1";
22$S3="CAST_S_table2";
23$S4="CAST_S_table3";
24
25@F1=("add","xor","sub");
26@F2=("xor","sub","add");
27@F3=("sub","add","xor");
28
29&CAST_encrypt("CAST_encrypt",1);
30&CAST_encrypt("CAST_decrypt",0);
31&cbc("CAST_cbc_encrypt","CAST_encrypt","CAST_decrypt",1,4,5,3,-1,-1);
32
33&asm_finish();
34
35sub CAST_encrypt
36 {
37 local($name,$enc)=@_;
38
39 local($win_ex)=<<"EOF";
40EXTERN _CAST_S_table0:DWORD
41EXTERN _CAST_S_table1:DWORD
42EXTERN _CAST_S_table2:DWORD
43EXTERN _CAST_S_table3:DWORD
44EOF
45 &main'external_label(
46 "CAST_S_table0",
47 "CAST_S_table1",
48 "CAST_S_table2",
49 "CAST_S_table3",
50 );
51
52 &function_begin_B($name,$win_ex);
53
54 &comment("");
55
56 &push("ebp");
57 &push("ebx");
58 &mov($tmp2,&wparam(0));
59 &mov($K,&wparam(1));
60 &push("esi");
61 &push("edi");
62
63 &comment("Load the 2 words");
64 &mov($L,&DWP(0,$tmp2,"",0));
65 &mov($R,&DWP(4,$tmp2,"",0));
66
67 &xor( $tmp3, $tmp3);
68
69 # encrypting part
70
71 if ($enc)
72 {
73 &E_CAST( 0,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
74 &E_CAST( 1,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
75 &E_CAST( 2,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
76 &E_CAST( 3,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
77 &E_CAST( 4,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
78 &E_CAST( 5,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
79 &E_CAST( 6,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
80 &E_CAST( 7,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
81 &E_CAST( 8,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
82 &E_CAST( 9,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
83 &E_CAST(10,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
84 &E_CAST(11,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
85 &E_CAST(12,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
86 &E_CAST(13,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
87 &E_CAST(14,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
88 &E_CAST(15,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4,1);
89 }
90 else
91 {
92 &E_CAST(15,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
93 &E_CAST(14,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
94 &E_CAST(13,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
95 &E_CAST(12,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
96 &E_CAST(11,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
97 &E_CAST(10,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
98 &E_CAST( 9,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
99 &E_CAST( 8,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
100 &E_CAST( 7,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
101 &E_CAST( 6,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
102 &E_CAST( 5,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
103 &E_CAST( 4,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
104 &E_CAST( 3,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
105 &E_CAST( 2,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
106 &E_CAST( 1,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
107 &E_CAST( 0,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4,1);
108 }
109
110 &nop();
111 &mov(&DWP(4,$tmp3,"",0),$L);
112 &mov(&DWP(0,$tmp3,"",0),$R);
113 &function_end($name);
114 }
115
116sub E_CAST
117 {
118 local($i,$S,$L,$R,$K,$OP1,$OP2,$OP3,$tmp1,$tmp2,$tmp3,$tmp4,$lst)=@_;
119 # Ri needs to have 16 pre added.
120
121 &comment("round $i");
122 &mov( $tmp4, &DWP($i*8,$K,"",1));
123
124 &mov( $tmp1, &DWP($i*8+4,$K,"",1));# must be word
125 &$OP1( $tmp4, $R);
126
127 &rotl( $tmp4, &LB($tmp1));
128
129 if ($ppro)
130 {
131 &mov( $tmp2, $tmp4); # B
132 &xor( $tmp1, $tmp1);
133
134 &movb( &LB($tmp1), &HB($tmp4)); # A
135 &and( $tmp2, 0xff);
136
137 &shr( $tmp4, 16); #
138 &xor( $tmp3, $tmp3);
139 }
140 else
141 {
142 &mov( $tmp2, $tmp4); # B
143 &movb( &LB($tmp1), &HB($tmp4)); # A # BAD BAD BAD
144
145 &shr( $tmp4, 16); #
146 &and( $tmp2, 0xff);
147 }
148
149 &movb( &LB($tmp3), &HB($tmp4)); # C # BAD BAD BAD
150 &and( $tmp4, 0xff); # D
151
152 &mov( $tmp1, &DWP($S1,"",$tmp1,4));
153 &mov( $tmp2, &DWP($S2,"",$tmp2,4));
154
155 &$OP2( $tmp1, $tmp2);
156 &mov( $tmp2, &DWP($S3,"",$tmp3,4));
157
158 &$OP3( $tmp1, $tmp2);
159 &mov( $tmp2, &DWP($S4,"",$tmp4,4));
160
161 &$OP1( $tmp1, $tmp2);
162 &mov($tmp3,&wparam(0)) if $lst;
163 # XXX
164
165 &xor( $L, $tmp1);
166 # XXX
167 }
diff --git a/src/lib/libcrypto/cast/c_cfb64.c b/src/lib/libcrypto/cast/c_cfb64.c
new file mode 100644
index 0000000000..c46c375f75
--- /dev/null
+++ b/src/lib/libcrypto/cast/c_cfb64.c
@@ -0,0 +1,127 @@
1/* crypto/cast/c_cfb64.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "cast.h"
60#include "cast_lcl.h"
61
62/* The input and output encrypted as though 64bit cfb mode is being
63 * used. The extra state information to record how much of the
64 * 64bit block we have used is contained in *num;
65 */
66
67void CAST_cfb64_encrypt(in, out, length, schedule, ivec, num, encrypt)
68unsigned char *in;
69unsigned char *out;
70long length;
71CAST_KEY *schedule;
72unsigned char *ivec;
73int *num;
74int encrypt;
75 {
76 register CAST_LONG v0,v1,t;
77 register int n= *num;
78 register long l=length;
79 CAST_LONG ti[2];
80 unsigned char *iv,c,cc;
81
82 iv=(unsigned char *)ivec;
83 if (encrypt)
84 {
85 while (l--)
86 {
87 if (n == 0)
88 {
89 n2l(iv,v0); ti[0]=v0;
90 n2l(iv,v1); ti[1]=v1;
91 CAST_encrypt((CAST_LONG *)ti,schedule);
92 iv=(unsigned char *)ivec;
93 t=ti[0]; l2n(t,iv);
94 t=ti[1]; l2n(t,iv);
95 iv=(unsigned char *)ivec;
96 }
97 c= *(in++)^iv[n];
98 *(out++)=c;
99 iv[n]=c;
100 n=(n+1)&0x07;
101 }
102 }
103 else
104 {
105 while (l--)
106 {
107 if (n == 0)
108 {
109 n2l(iv,v0); ti[0]=v0;
110 n2l(iv,v1); ti[1]=v1;
111 CAST_encrypt((CAST_LONG *)ti,schedule);
112 iv=(unsigned char *)ivec;
113 t=ti[0]; l2n(t,iv);
114 t=ti[1]; l2n(t,iv);
115 iv=(unsigned char *)ivec;
116 }
117 cc= *(in++);
118 c=iv[n];
119 iv[n]=cc;
120 *(out++)=c^cc;
121 n=(n+1)&0x07;
122 }
123 }
124 v0=v1=ti[0]=ti[1]=t=c=cc=0;
125 *num=n;
126 }
127
diff --git a/src/lib/libcrypto/cast/c_ecb.c b/src/lib/libcrypto/cast/c_ecb.c
new file mode 100644
index 0000000000..f0f2f4df0e
--- /dev/null
+++ b/src/lib/libcrypto/cast/c_ecb.c
@@ -0,0 +1,82 @@
1/* crypto/cast/c_ecb.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "cast.h"
60#include "cast_lcl.h"
61
62char *CAST_version="CAST part of SSLeay 0.9.0b 29-Jun-1998";
63
64void CAST_ecb_encrypt(in, out, ks, encrypt)
65unsigned char *in;
66unsigned char *out;
67CAST_KEY *ks;
68int encrypt;
69 {
70 CAST_LONG l,d[2];
71
72 n2l(in,l); d[0]=l;
73 n2l(in,l); d[1]=l;
74 if (encrypt)
75 CAST_encrypt(d,ks);
76 else
77 CAST_decrypt(d,ks);
78 l=d[0]; l2n(l,out);
79 l=d[1]; l2n(l,out);
80 l=d[0]=d[1]=0;
81 }
82
diff --git a/src/lib/libcrypto/cast/c_enc.c b/src/lib/libcrypto/cast/c_enc.c
new file mode 100644
index 0000000000..d998dd4953
--- /dev/null
+++ b/src/lib/libcrypto/cast/c_enc.c
@@ -0,0 +1,210 @@
1/* crypto/cast/c_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "cast.h"
60#include "cast_lcl.h"
61
62void CAST_encrypt(data,key)
63CAST_LONG *data;
64CAST_KEY *key;
65 {
66 register CAST_LONG l,r,*k,t;
67
68 k= &(key->data[0]);
69 l=data[0];
70 r=data[1];
71
72 E_CAST( 0,k,l,r,+,^,-);
73 E_CAST( 1,k,r,l,^,-,+);
74 E_CAST( 2,k,l,r,-,+,^);
75 E_CAST( 3,k,r,l,+,^,-);
76 E_CAST( 4,k,l,r,^,-,+);
77 E_CAST( 5,k,r,l,-,+,^);
78 E_CAST( 6,k,l,r,+,^,-);
79 E_CAST( 7,k,r,l,^,-,+);
80 E_CAST( 8,k,l,r,-,+,^);
81 E_CAST( 9,k,r,l,+,^,-);
82 E_CAST(10,k,l,r,^,-,+);
83 E_CAST(11,k,r,l,-,+,^);
84 E_CAST(12,k,l,r,+,^,-);
85 E_CAST(13,k,r,l,^,-,+);
86 E_CAST(14,k,l,r,-,+,^);
87 E_CAST(15,k,r,l,+,^,-);
88
89 data[1]=l&0xffffffffL;
90 data[0]=r&0xffffffffL;
91 }
92
93void CAST_decrypt(data,key)
94CAST_LONG *data;
95CAST_KEY *key;
96 {
97 register CAST_LONG l,r,*k,t;
98
99 k= &(key->data[0]);
100 l=data[0];
101 r=data[1];
102
103 E_CAST(15,k,l,r,+,^,-);
104 E_CAST(14,k,r,l,-,+,^);
105 E_CAST(13,k,l,r,^,-,+);
106 E_CAST(12,k,r,l,+,^,-);
107 E_CAST(11,k,l,r,-,+,^);
108 E_CAST(10,k,r,l,^,-,+);
109 E_CAST( 9,k,l,r,+,^,-);
110 E_CAST( 8,k,r,l,-,+,^);
111 E_CAST( 7,k,l,r,^,-,+);
112 E_CAST( 6,k,r,l,+,^,-);
113 E_CAST( 5,k,l,r,-,+,^);
114 E_CAST( 4,k,r,l,^,-,+);
115 E_CAST( 3,k,l,r,+,^,-);
116 E_CAST( 2,k,r,l,-,+,^);
117 E_CAST( 1,k,l,r,^,-,+);
118 E_CAST( 0,k,r,l,+,^,-);
119
120 data[1]=l&0xffffffffL;
121 data[0]=r&0xffffffffL;
122 }
123
124void CAST_cbc_encrypt(in, out, length, ks, iv, encrypt)
125unsigned char *in;
126unsigned char *out;
127long length;
128CAST_KEY *ks;
129unsigned char *iv;
130int encrypt;
131 {
132 register CAST_LONG tin0,tin1;
133 register CAST_LONG tout0,tout1,xor0,xor1;
134 register long l=length;
135 CAST_LONG tin[2];
136
137 if (encrypt)
138 {
139 n2l(iv,tout0);
140 n2l(iv,tout1);
141 iv-=8;
142 for (l-=8; l>=0; l-=8)
143 {
144 n2l(in,tin0);
145 n2l(in,tin1);
146 tin0^=tout0;
147 tin1^=tout1;
148 tin[0]=tin0;
149 tin[1]=tin1;
150 CAST_encrypt(tin,ks);
151 tout0=tin[0];
152 tout1=tin[1];
153 l2n(tout0,out);
154 l2n(tout1,out);
155 }
156 if (l != -8)
157 {
158 n2ln(in,tin0,tin1,l+8);
159 tin0^=tout0;
160 tin1^=tout1;
161 tin[0]=tin0;
162 tin[1]=tin1;
163 CAST_encrypt(tin,ks);
164 tout0=tin[0];
165 tout1=tin[1];
166 l2n(tout0,out);
167 l2n(tout1,out);
168 }
169 l2n(tout0,iv);
170 l2n(tout1,iv);
171 }
172 else
173 {
174 n2l(iv,xor0);
175 n2l(iv,xor1);
176 iv-=8;
177 for (l-=8; l>=0; l-=8)
178 {
179 n2l(in,tin0);
180 n2l(in,tin1);
181 tin[0]=tin0;
182 tin[1]=tin1;
183 CAST_decrypt(tin,ks);
184 tout0=tin[0]^xor0;
185 tout1=tin[1]^xor1;
186 l2n(tout0,out);
187 l2n(tout1,out);
188 xor0=tin0;
189 xor1=tin1;
190 }
191 if (l != -8)
192 {
193 n2l(in,tin0);
194 n2l(in,tin1);
195 tin[0]=tin0;
196 tin[1]=tin1;
197 CAST_decrypt(tin,ks);
198 tout0=tin[0]^xor0;
199 tout1=tin[1]^xor1;
200 l2nn(tout0,tout1,out,l+8);
201 xor0=tin0;
202 xor1=tin1;
203 }
204 l2n(xor0,iv);
205 l2n(xor1,iv);
206 }
207 tin0=tin1=tout0=tout1=xor0=xor1=0;
208 tin[0]=tin[1]=0;
209 }
210
diff --git a/src/lib/libcrypto/cast/c_ofb64.c b/src/lib/libcrypto/cast/c_ofb64.c
new file mode 100644
index 0000000000..2aad2d6d96
--- /dev/null
+++ b/src/lib/libcrypto/cast/c_ofb64.c
@@ -0,0 +1,115 @@
1/* crypto/cast/c_ofb64.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "cast.h"
60#include "cast_lcl.h"
61
62/* The input and output encrypted as though 64bit ofb mode is being
63 * used. The extra state information to record how much of the
64 * 64bit block we have used is contained in *num;
65 */
66void CAST_ofb64_encrypt(in, out, length, schedule, ivec, num)
67unsigned char *in;
68unsigned char *out;
69long length;
70CAST_KEY *schedule;
71unsigned char *ivec;
72int *num;
73 {
74 register CAST_LONG v0,v1,t;
75 register int n= *num;
76 register long l=length;
77 unsigned char d[8];
78 register char *dp;
79 CAST_LONG ti[2];
80 unsigned char *iv;
81 int save=0;
82
83 iv=(unsigned char *)ivec;
84 n2l(iv,v0);
85 n2l(iv,v1);
86 ti[0]=v0;
87 ti[1]=v1;
88 dp=(char *)d;
89 l2n(v0,dp);
90 l2n(v1,dp);
91 while (l--)
92 {
93 if (n == 0)
94 {
95 CAST_encrypt((CAST_LONG *)ti,schedule);
96 dp=(char *)d;
97 t=ti[0]; l2n(t,dp);
98 t=ti[1]; l2n(t,dp);
99 save++;
100 }
101 *(out++)= *(in++)^d[n];
102 n=(n+1)&0x07;
103 }
104 if (save)
105 {
106 v0=ti[0];
107 v1=ti[1];
108 iv=(unsigned char *)ivec;
109 l2n(v0,iv);
110 l2n(v1,iv);
111 }
112 t=v0=v1=ti[0]=ti[1]=0;
113 *num=n;
114 }
115
diff --git a/src/lib/libcrypto/cast/c_skey.c b/src/lib/libcrypto/cast/c_skey.c
new file mode 100644
index 0000000000..2fc3363dcd
--- /dev/null
+++ b/src/lib/libcrypto/cast/c_skey.c
@@ -0,0 +1,165 @@
1/* crypto/cast/c_skey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "cast.h"
60#include "cast_lcl.h"
61#include "cast_s.h"
62
63#define CAST_exp(l,A,a,n) \
64 A[n/4]=l; \
65 a[n+3]=(l )&0xff; \
66 a[n+2]=(l>> 8)&0xff; \
67 a[n+1]=(l>>16)&0xff; \
68 a[n+0]=(l>>24)&0xff;
69
70#define S4 CAST_S_table4
71#define S5 CAST_S_table5
72#define S6 CAST_S_table6
73#define S7 CAST_S_table7
74
75void CAST_set_key(key,len,data)
76CAST_KEY *key;
77int len;
78unsigned char *data;
79 {
80 CAST_LONG x[16];
81 CAST_LONG z[16];
82 CAST_LONG k[32];
83 CAST_LONG X[4],Z[4];
84 CAST_LONG l,*K;
85 int i;
86
87 for (i=0; i<16; i++) x[i]=0;
88 if (len > 16) len=16;
89 for (i=0; i<len; i++)
90 x[i]=data[i];
91
92 K= &k[0];
93 X[0]=((x[ 0]<<24)|(x[ 1]<<16)|(x[ 2]<<8)|x[ 3])&0xffffffffL;
94 X[1]=((x[ 4]<<24)|(x[ 5]<<16)|(x[ 6]<<8)|x[ 7])&0xffffffffL;
95 X[2]=((x[ 8]<<24)|(x[ 9]<<16)|(x[10]<<8)|x[11])&0xffffffffL;
96 X[3]=((x[12]<<24)|(x[13]<<16)|(x[14]<<8)|x[15])&0xffffffffL;
97
98 for (;;)
99 {
100 l=X[0]^S4[x[13]]^S5[x[15]]^S6[x[12]]^S7[x[14]]^S6[x[ 8]];
101 CAST_exp(l,Z,z, 0);
102 l=X[2]^S4[z[ 0]]^S5[z[ 2]]^S6[z[ 1]]^S7[z[ 3]]^S7[x[10]];
103 CAST_exp(l,Z,z, 4);
104 l=X[3]^S4[z[ 7]]^S5[z[ 6]]^S6[z[ 5]]^S7[z[ 4]]^S4[x[ 9]];
105 CAST_exp(l,Z,z, 8);
106 l=X[1]^S4[z[10]]^S5[z[ 9]]^S6[z[11]]^S7[z[ 8]]^S5[x[11]];
107 CAST_exp(l,Z,z,12);
108
109 K[ 0]= S4[z[ 8]]^S5[z[ 9]]^S6[z[ 7]]^S7[z[ 6]]^S4[z[ 2]];
110 K[ 1]= S4[z[10]]^S5[z[11]]^S6[z[ 5]]^S7[z[ 4]]^S5[z[ 6]];
111 K[ 2]= S4[z[12]]^S5[z[13]]^S6[z[ 3]]^S7[z[ 2]]^S6[z[ 9]];
112 K[ 3]= S4[z[14]]^S5[z[15]]^S6[z[ 1]]^S7[z[ 0]]^S7[z[12]];
113
114 l=Z[2]^S4[z[ 5]]^S5[z[ 7]]^S6[z[ 4]]^S7[z[ 6]]^S6[z[ 0]];
115 CAST_exp(l,X,x, 0);
116 l=Z[0]^S4[x[ 0]]^S5[x[ 2]]^S6[x[ 1]]^S7[x[ 3]]^S7[z[ 2]];
117 CAST_exp(l,X,x, 4);
118 l=Z[1]^S4[x[ 7]]^S5[x[ 6]]^S6[x[ 5]]^S7[x[ 4]]^S4[z[ 1]];
119 CAST_exp(l,X,x, 8);
120 l=Z[3]^S4[x[10]]^S5[x[ 9]]^S6[x[11]]^S7[x[ 8]]^S5[z[ 3]];
121 CAST_exp(l,X,x,12);
122
123 K[ 4]= S4[x[ 3]]^S5[x[ 2]]^S6[x[12]]^S7[x[13]]^S4[x[ 8]];
124 K[ 5]= S4[x[ 1]]^S5[x[ 0]]^S6[x[14]]^S7[x[15]]^S5[x[13]];
125 K[ 6]= S4[x[ 7]]^S5[x[ 6]]^S6[x[ 8]]^S7[x[ 9]]^S6[x[ 3]];
126 K[ 7]= S4[x[ 5]]^S5[x[ 4]]^S6[x[10]]^S7[x[11]]^S7[x[ 7]];
127
128 l=X[0]^S4[x[13]]^S5[x[15]]^S6[x[12]]^S7[x[14]]^S6[x[ 8]];
129 CAST_exp(l,Z,z, 0);
130 l=X[2]^S4[z[ 0]]^S5[z[ 2]]^S6[z[ 1]]^S7[z[ 3]]^S7[x[10]];
131 CAST_exp(l,Z,z, 4);
132 l=X[3]^S4[z[ 7]]^S5[z[ 6]]^S6[z[ 5]]^S7[z[ 4]]^S4[x[ 9]];
133 CAST_exp(l,Z,z, 8);
134 l=X[1]^S4[z[10]]^S5[z[ 9]]^S6[z[11]]^S7[z[ 8]]^S5[x[11]];
135 CAST_exp(l,Z,z,12);
136
137 K[ 8]= S4[z[ 3]]^S5[z[ 2]]^S6[z[12]]^S7[z[13]]^S4[z[ 9]];
138 K[ 9]= S4[z[ 1]]^S5[z[ 0]]^S6[z[14]]^S7[z[15]]^S5[z[12]];
139 K[10]= S4[z[ 7]]^S5[z[ 6]]^S6[z[ 8]]^S7[z[ 9]]^S6[z[ 2]];
140 K[11]= S4[z[ 5]]^S5[z[ 4]]^S6[z[10]]^S7[z[11]]^S7[z[ 6]];
141
142 l=Z[2]^S4[z[ 5]]^S5[z[ 7]]^S6[z[ 4]]^S7[z[ 6]]^S6[z[ 0]];
143 CAST_exp(l,X,x, 0);
144 l=Z[0]^S4[x[ 0]]^S5[x[ 2]]^S6[x[ 1]]^S7[x[ 3]]^S7[z[ 2]];
145 CAST_exp(l,X,x, 4);
146 l=Z[1]^S4[x[ 7]]^S5[x[ 6]]^S6[x[ 5]]^S7[x[ 4]]^S4[z[ 1]];
147 CAST_exp(l,X,x, 8);
148 l=Z[3]^S4[x[10]]^S5[x[ 9]]^S6[x[11]]^S7[x[ 8]]^S5[z[ 3]];
149 CAST_exp(l,X,x,12);
150
151 K[12]= S4[x[ 8]]^S5[x[ 9]]^S6[x[ 7]]^S7[x[ 6]]^S4[x[ 3]];
152 K[13]= S4[x[10]]^S5[x[11]]^S6[x[ 5]]^S7[x[ 4]]^S5[x[ 7]];
153 K[14]= S4[x[12]]^S5[x[13]]^S6[x[ 3]]^S7[x[ 2]]^S6[x[ 8]];
154 K[15]= S4[x[14]]^S5[x[15]]^S6[x[ 1]]^S7[x[ 0]]^S7[x[13]];
155 if (K != k) break;
156 K+=16;
157 }
158
159 for (i=0; i<16; i++)
160 {
161 key->data[i*2]=k[i];
162 key->data[i*2+1]=((k[i+16])+16)&0x1f;
163 }
164 }
165
diff --git a/src/lib/libcrypto/cast/cast.h b/src/lib/libcrypto/cast/cast.h
new file mode 100644
index 0000000000..528cb7c824
--- /dev/null
+++ b/src/lib/libcrypto/cast/cast.h
@@ -0,0 +1,109 @@
1/* crypto/cast/cast.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_CAST_H
60#define HEADER_CAST_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#define CAST_ENCRYPT 1
67#define CAST_DECRYPT 0
68
69#define CAST_LONG unsigned long
70
71#define CAST_BLOCK 8
72#define CAST_KEY_LENGTH 16
73
74typedef struct cast_key_st
75 {
76 CAST_LONG data[32];
77 } CAST_KEY;
78
79#ifndef NOPROTO
80
81void CAST_set_key(CAST_KEY *key, int len, unsigned char *data);
82void CAST_ecb_encrypt(unsigned char *in,unsigned char *out,CAST_KEY *key,
83 int enc);
84void CAST_encrypt(CAST_LONG *data,CAST_KEY *key);
85void CAST_decrypt(CAST_LONG *data,CAST_KEY *key);
86void CAST_cbc_encrypt(unsigned char *in, unsigned char *out, long length,
87 CAST_KEY *ks, unsigned char *iv, int enc);
88void CAST_cfb64_encrypt(unsigned char *in, unsigned char *out, long length,
89 CAST_KEY *schedule, unsigned char *ivec, int *num, int enc);
90void CAST_ofb64_encrypt(unsigned char *in, unsigned char *out, long length,
91 CAST_KEY *schedule, unsigned char *ivec, int *num);
92
93#else
94
95void CAST_set_key();
96void CAST_ecb_encrypt();
97void CAST_encrypt();
98void CAST_decrypt();
99void CAST_cbc_encrypt();
100void CAST_cfb64_encrypt();
101void CAST_ofb64_encrypt();
102
103#endif
104
105#ifdef __cplusplus
106}
107#endif
108
109#endif
diff --git a/src/lib/libcrypto/cast/cast_lcl.h b/src/lib/libcrypto/cast/cast_lcl.h
new file mode 100644
index 0000000000..6587952a96
--- /dev/null
+++ b/src/lib/libcrypto/cast/cast_lcl.h
@@ -0,0 +1,224 @@
1/* crypto/cast/cast_lcl.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifdef WIN32
60#include <stdlib.h>
61#endif
62
63#undef c2l
64#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
65 l|=((unsigned long)(*((c)++)))<< 8L, \
66 l|=((unsigned long)(*((c)++)))<<16L, \
67 l|=((unsigned long)(*((c)++)))<<24L)
68
69/* NOTE - c is not incremented as per c2l */
70#undef c2ln
71#define c2ln(c,l1,l2,n) { \
72 c+=n; \
73 l1=l2=0; \
74 switch (n) { \
75 case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
76 case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
77 case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
78 case 5: l2|=((unsigned long)(*(--(c)))); \
79 case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
80 case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
81 case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
82 case 1: l1|=((unsigned long)(*(--(c)))); \
83 } \
84 }
85
86#undef l2c
87#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
88 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
89 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
90 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
91
92/* NOTE - c is not incremented as per l2c */
93#undef l2cn
94#define l2cn(l1,l2,c,n) { \
95 c+=n; \
96 switch (n) { \
97 case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
98 case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
99 case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
100 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
101 case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
102 case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
103 case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
104 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
105 } \
106 }
107
108/* NOTE - c is not incremented as per n2l */
109#define n2ln(c,l1,l2,n) { \
110 c+=n; \
111 l1=l2=0; \
112 switch (n) { \
113 case 8: l2 =((unsigned long)(*(--(c)))) ; \
114 case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
115 case 6: l2|=((unsigned long)(*(--(c))))<<16; \
116 case 5: l2|=((unsigned long)(*(--(c))))<<24; \
117 case 4: l1 =((unsigned long)(*(--(c)))) ; \
118 case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
119 case 2: l1|=((unsigned long)(*(--(c))))<<16; \
120 case 1: l1|=((unsigned long)(*(--(c))))<<24; \
121 } \
122 }
123
124/* NOTE - c is not incremented as per l2n */
125#define l2nn(l1,l2,c,n) { \
126 c+=n; \
127 switch (n) { \
128 case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \
129 case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
130 case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
131 case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
132 case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \
133 case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
134 case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
135 case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
136 } \
137 }
138
139#undef n2l
140#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
141 l|=((unsigned long)(*((c)++)))<<16L, \
142 l|=((unsigned long)(*((c)++)))<< 8L, \
143 l|=((unsigned long)(*((c)++))))
144
145#undef l2n
146#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
147 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
148 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
149 *((c)++)=(unsigned char)(((l) )&0xff))
150
151#if defined(WIN32)
152#define ROTL(a,n) (_lrotl(a,n))
153#else
154#define ROTL(a,n) ((((a)<<(n))&0xffffffffL)|((a)>>(32-(n))))
155#endif
156
157#define C_M 0x3fc
158#define C_0 22L
159#define C_1 14L
160#define C_2 6L
161#define C_3 2L /* left shift */
162
163/* The rotate has an extra 16 added to it to help the x86 asm */
164#if defined(CAST_PTR)
165#define E_CAST(n,key,L,R,OP1,OP2,OP3) \
166 { \
167 int i; \
168 t=(key[n*2] OP1 R)&0xffffffffL; \
169 i=key[n*2+1]; \
170 t=ROTL(t,i); \
171 L^= (((((*(CAST_LONG *)((unsigned char *) \
172 CAST_S_table0+((t>>C_2)&C_M)) OP2 \
173 *(CAST_LONG *)((unsigned char *) \
174 CAST_S_table1+((t<<C_3)&C_M)))&0xffffffffL) OP3 \
175 *(CAST_LONG *)((unsigned char *) \
176 CAST_S_table2+((t>>C_0)&C_M)))&0xffffffffL) OP1 \
177 *(CAST_LONG *)((unsigned char *) \
178 CAST_S_table3+((t>>C_1)&C_M)))&0xffffffffL; \
179 }
180#elif defined(CAST_PTR2)
181#define E_CAST(n,key,L,R,OP1,OP2,OP3) \
182 { \
183 int i; \
184 CAST_LONG u,v,w; \
185 w=(key[n*2] OP1 R)&0xffffffffL; \
186 i=key[n*2+1]; \
187 w=ROTL(w,i); \
188 u=w>>C_2; \
189 v=w<<C_3; \
190 u&=C_M; \
191 v&=C_M; \
192 t= *(CAST_LONG *)((unsigned char *)CAST_S_table0+u); \
193 u=w>>C_0; \
194 t=(t OP2 *(CAST_LONG *)((unsigned char *)CAST_S_table1+v))&0xffffffffL;\
195 v=w>>C_1; \
196 u&=C_M; \
197 v&=C_M; \
198 t=(t OP3 *(CAST_LONG *)((unsigned char *)CAST_S_table2+u)&0xffffffffL);\
199 t=(t OP1 *(CAST_LONG *)((unsigned char *)CAST_S_table3+v)&0xffffffffL);\
200 L^=(t&0xffffffff); \
201 }
202#else
203#define E_CAST(n,key,L,R,OP1,OP2,OP3) \
204 { \
205 CAST_LONG a,b,c,d; \
206 t=(key[n*2] OP1 R)&0xffffffff; \
207 t=ROTL(t,(key[n*2+1])); \
208 a=CAST_S_table0[(t>> 8)&0xff]; \
209 b=CAST_S_table1[(t )&0xff]; \
210 c=CAST_S_table2[(t>>24)&0xff]; \
211 d=CAST_S_table3[(t>>16)&0xff]; \
212 L^=(((((a OP2 b)&0xffffffffL) OP3 c)&0xffffffffL) OP1 d)&0xffffffffL; \
213 }
214#endif
215
216extern CAST_LONG CAST_S_table0[256];
217extern CAST_LONG CAST_S_table1[256];
218extern CAST_LONG CAST_S_table2[256];
219extern CAST_LONG CAST_S_table3[256];
220extern CAST_LONG CAST_S_table4[256];
221extern CAST_LONG CAST_S_table5[256];
222extern CAST_LONG CAST_S_table6[256];
223extern CAST_LONG CAST_S_table7[256];
224
diff --git a/src/lib/libcrypto/cast/cast_s.h b/src/lib/libcrypto/cast/cast_s.h
new file mode 100644
index 0000000000..8fe0152149
--- /dev/null
+++ b/src/lib/libcrypto/cast/cast_s.h
@@ -0,0 +1,585 @@
1/* crypto/cast/cast_s.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58CAST_LONG CAST_S_table0[256]={
59 0x30fb40d4,0x9fa0ff0b,0x6beccd2f,0x3f258c7a,
60 0x1e213f2f,0x9c004dd3,0x6003e540,0xcf9fc949,
61 0xbfd4af27,0x88bbbdb5,0xe2034090,0x98d09675,
62 0x6e63a0e0,0x15c361d2,0xc2e7661d,0x22d4ff8e,
63 0x28683b6f,0xc07fd059,0xff2379c8,0x775f50e2,
64 0x43c340d3,0xdf2f8656,0x887ca41a,0xa2d2bd2d,
65 0xa1c9e0d6,0x346c4819,0x61b76d87,0x22540f2f,
66 0x2abe32e1,0xaa54166b,0x22568e3a,0xa2d341d0,
67 0x66db40c8,0xa784392f,0x004dff2f,0x2db9d2de,
68 0x97943fac,0x4a97c1d8,0x527644b7,0xb5f437a7,
69 0xb82cbaef,0xd751d159,0x6ff7f0ed,0x5a097a1f,
70 0x827b68d0,0x90ecf52e,0x22b0c054,0xbc8e5935,
71 0x4b6d2f7f,0x50bb64a2,0xd2664910,0xbee5812d,
72 0xb7332290,0xe93b159f,0xb48ee411,0x4bff345d,
73 0xfd45c240,0xad31973f,0xc4f6d02e,0x55fc8165,
74 0xd5b1caad,0xa1ac2dae,0xa2d4b76d,0xc19b0c50,
75 0x882240f2,0x0c6e4f38,0xa4e4bfd7,0x4f5ba272,
76 0x564c1d2f,0xc59c5319,0xb949e354,0xb04669fe,
77 0xb1b6ab8a,0xc71358dd,0x6385c545,0x110f935d,
78 0x57538ad5,0x6a390493,0xe63d37e0,0x2a54f6b3,
79 0x3a787d5f,0x6276a0b5,0x19a6fcdf,0x7a42206a,
80 0x29f9d4d5,0xf61b1891,0xbb72275e,0xaa508167,
81 0x38901091,0xc6b505eb,0x84c7cb8c,0x2ad75a0f,
82 0x874a1427,0xa2d1936b,0x2ad286af,0xaa56d291,
83 0xd7894360,0x425c750d,0x93b39e26,0x187184c9,
84 0x6c00b32d,0x73e2bb14,0xa0bebc3c,0x54623779,
85 0x64459eab,0x3f328b82,0x7718cf82,0x59a2cea6,
86 0x04ee002e,0x89fe78e6,0x3fab0950,0x325ff6c2,
87 0x81383f05,0x6963c5c8,0x76cb5ad6,0xd49974c9,
88 0xca180dcf,0x380782d5,0xc7fa5cf6,0x8ac31511,
89 0x35e79e13,0x47da91d0,0xf40f9086,0xa7e2419e,
90 0x31366241,0x051ef495,0xaa573b04,0x4a805d8d,
91 0x548300d0,0x00322a3c,0xbf64cddf,0xba57a68e,
92 0x75c6372b,0x50afd341,0xa7c13275,0x915a0bf5,
93 0x6b54bfab,0x2b0b1426,0xab4cc9d7,0x449ccd82,
94 0xf7fbf265,0xab85c5f3,0x1b55db94,0xaad4e324,
95 0xcfa4bd3f,0x2deaa3e2,0x9e204d02,0xc8bd25ac,
96 0xeadf55b3,0xd5bd9e98,0xe31231b2,0x2ad5ad6c,
97 0x954329de,0xadbe4528,0xd8710f69,0xaa51c90f,
98 0xaa786bf6,0x22513f1e,0xaa51a79b,0x2ad344cc,
99 0x7b5a41f0,0xd37cfbad,0x1b069505,0x41ece491,
100 0xb4c332e6,0x032268d4,0xc9600acc,0xce387e6d,
101 0xbf6bb16c,0x6a70fb78,0x0d03d9c9,0xd4df39de,
102 0xe01063da,0x4736f464,0x5ad328d8,0xb347cc96,
103 0x75bb0fc3,0x98511bfb,0x4ffbcc35,0xb58bcf6a,
104 0xe11f0abc,0xbfc5fe4a,0xa70aec10,0xac39570a,
105 0x3f04442f,0x6188b153,0xe0397a2e,0x5727cb79,
106 0x9ceb418f,0x1cacd68d,0x2ad37c96,0x0175cb9d,
107 0xc69dff09,0xc75b65f0,0xd9db40d8,0xec0e7779,
108 0x4744ead4,0xb11c3274,0xdd24cb9e,0x7e1c54bd,
109 0xf01144f9,0xd2240eb1,0x9675b3fd,0xa3ac3755,
110 0xd47c27af,0x51c85f4d,0x56907596,0xa5bb15e6,
111 0x580304f0,0xca042cf1,0x011a37ea,0x8dbfaadb,
112 0x35ba3e4a,0x3526ffa0,0xc37b4d09,0xbc306ed9,
113 0x98a52666,0x5648f725,0xff5e569d,0x0ced63d0,
114 0x7c63b2cf,0x700b45e1,0xd5ea50f1,0x85a92872,
115 0xaf1fbda7,0xd4234870,0xa7870bf3,0x2d3b4d79,
116 0x42e04198,0x0cd0ede7,0x26470db8,0xf881814c,
117 0x474d6ad7,0x7c0c5e5c,0xd1231959,0x381b7298,
118 0xf5d2f4db,0xab838653,0x6e2f1e23,0x83719c9e,
119 0xbd91e046,0x9a56456e,0xdc39200c,0x20c8c571,
120 0x962bda1c,0xe1e696ff,0xb141ab08,0x7cca89b9,
121 0x1a69e783,0x02cc4843,0xa2f7c579,0x429ef47d,
122 0x427b169c,0x5ac9f049,0xdd8f0f00,0x5c8165bf,
123 };
124CAST_LONG CAST_S_table1[256]={
125 0x1f201094,0xef0ba75b,0x69e3cf7e,0x393f4380,
126 0xfe61cf7a,0xeec5207a,0x55889c94,0x72fc0651,
127 0xada7ef79,0x4e1d7235,0xd55a63ce,0xde0436ba,
128 0x99c430ef,0x5f0c0794,0x18dcdb7d,0xa1d6eff3,
129 0xa0b52f7b,0x59e83605,0xee15b094,0xe9ffd909,
130 0xdc440086,0xef944459,0xba83ccb3,0xe0c3cdfb,
131 0xd1da4181,0x3b092ab1,0xf997f1c1,0xa5e6cf7b,
132 0x01420ddb,0xe4e7ef5b,0x25a1ff41,0xe180f806,
133 0x1fc41080,0x179bee7a,0xd37ac6a9,0xfe5830a4,
134 0x98de8b7f,0x77e83f4e,0x79929269,0x24fa9f7b,
135 0xe113c85b,0xacc40083,0xd7503525,0xf7ea615f,
136 0x62143154,0x0d554b63,0x5d681121,0xc866c359,
137 0x3d63cf73,0xcee234c0,0xd4d87e87,0x5c672b21,
138 0x071f6181,0x39f7627f,0x361e3084,0xe4eb573b,
139 0x602f64a4,0xd63acd9c,0x1bbc4635,0x9e81032d,
140 0x2701f50c,0x99847ab4,0xa0e3df79,0xba6cf38c,
141 0x10843094,0x2537a95e,0xf46f6ffe,0xa1ff3b1f,
142 0x208cfb6a,0x8f458c74,0xd9e0a227,0x4ec73a34,
143 0xfc884f69,0x3e4de8df,0xef0e0088,0x3559648d,
144 0x8a45388c,0x1d804366,0x721d9bfd,0xa58684bb,
145 0xe8256333,0x844e8212,0x128d8098,0xfed33fb4,
146 0xce280ae1,0x27e19ba5,0xd5a6c252,0xe49754bd,
147 0xc5d655dd,0xeb667064,0x77840b4d,0xa1b6a801,
148 0x84db26a9,0xe0b56714,0x21f043b7,0xe5d05860,
149 0x54f03084,0x066ff472,0xa31aa153,0xdadc4755,
150 0xb5625dbf,0x68561be6,0x83ca6b94,0x2d6ed23b,
151 0xeccf01db,0xa6d3d0ba,0xb6803d5c,0xaf77a709,
152 0x33b4a34c,0x397bc8d6,0x5ee22b95,0x5f0e5304,
153 0x81ed6f61,0x20e74364,0xb45e1378,0xde18639b,
154 0x881ca122,0xb96726d1,0x8049a7e8,0x22b7da7b,
155 0x5e552d25,0x5272d237,0x79d2951c,0xc60d894c,
156 0x488cb402,0x1ba4fe5b,0xa4b09f6b,0x1ca815cf,
157 0xa20c3005,0x8871df63,0xb9de2fcb,0x0cc6c9e9,
158 0x0beeff53,0xe3214517,0xb4542835,0x9f63293c,
159 0xee41e729,0x6e1d2d7c,0x50045286,0x1e6685f3,
160 0xf33401c6,0x30a22c95,0x31a70850,0x60930f13,
161 0x73f98417,0xa1269859,0xec645c44,0x52c877a9,
162 0xcdff33a6,0xa02b1741,0x7cbad9a2,0x2180036f,
163 0x50d99c08,0xcb3f4861,0xc26bd765,0x64a3f6ab,
164 0x80342676,0x25a75e7b,0xe4e6d1fc,0x20c710e6,
165 0xcdf0b680,0x17844d3b,0x31eef84d,0x7e0824e4,
166 0x2ccb49eb,0x846a3bae,0x8ff77888,0xee5d60f6,
167 0x7af75673,0x2fdd5cdb,0xa11631c1,0x30f66f43,
168 0xb3faec54,0x157fd7fa,0xef8579cc,0xd152de58,
169 0xdb2ffd5e,0x8f32ce19,0x306af97a,0x02f03ef8,
170 0x99319ad5,0xc242fa0f,0xa7e3ebb0,0xc68e4906,
171 0xb8da230c,0x80823028,0xdcdef3c8,0xd35fb171,
172 0x088a1bc8,0xbec0c560,0x61a3c9e8,0xbca8f54d,
173 0xc72feffa,0x22822e99,0x82c570b4,0xd8d94e89,
174 0x8b1c34bc,0x301e16e6,0x273be979,0xb0ffeaa6,
175 0x61d9b8c6,0x00b24869,0xb7ffce3f,0x08dc283b,
176 0x43daf65a,0xf7e19798,0x7619b72f,0x8f1c9ba4,
177 0xdc8637a0,0x16a7d3b1,0x9fc393b7,0xa7136eeb,
178 0xc6bcc63e,0x1a513742,0xef6828bc,0x520365d6,
179 0x2d6a77ab,0x3527ed4b,0x821fd216,0x095c6e2e,
180 0xdb92f2fb,0x5eea29cb,0x145892f5,0x91584f7f,
181 0x5483697b,0x2667a8cc,0x85196048,0x8c4bacea,
182 0x833860d4,0x0d23e0f9,0x6c387e8a,0x0ae6d249,
183 0xb284600c,0xd835731d,0xdcb1c647,0xac4c56ea,
184 0x3ebd81b3,0x230eabb0,0x6438bc87,0xf0b5b1fa,
185 0x8f5ea2b3,0xfc184642,0x0a036b7a,0x4fb089bd,
186 0x649da589,0xa345415e,0x5c038323,0x3e5d3bb9,
187 0x43d79572,0x7e6dd07c,0x06dfdf1e,0x6c6cc4ef,
188 0x7160a539,0x73bfbe70,0x83877605,0x4523ecf1,
189 };
190CAST_LONG CAST_S_table2[256]={
191 0x8defc240,0x25fa5d9f,0xeb903dbf,0xe810c907,
192 0x47607fff,0x369fe44b,0x8c1fc644,0xaececa90,
193 0xbeb1f9bf,0xeefbcaea,0xe8cf1950,0x51df07ae,
194 0x920e8806,0xf0ad0548,0xe13c8d83,0x927010d5,
195 0x11107d9f,0x07647db9,0xb2e3e4d4,0x3d4f285e,
196 0xb9afa820,0xfade82e0,0xa067268b,0x8272792e,
197 0x553fb2c0,0x489ae22b,0xd4ef9794,0x125e3fbc,
198 0x21fffcee,0x825b1bfd,0x9255c5ed,0x1257a240,
199 0x4e1a8302,0xbae07fff,0x528246e7,0x8e57140e,
200 0x3373f7bf,0x8c9f8188,0xa6fc4ee8,0xc982b5a5,
201 0xa8c01db7,0x579fc264,0x67094f31,0xf2bd3f5f,
202 0x40fff7c1,0x1fb78dfc,0x8e6bd2c1,0x437be59b,
203 0x99b03dbf,0xb5dbc64b,0x638dc0e6,0x55819d99,
204 0xa197c81c,0x4a012d6e,0xc5884a28,0xccc36f71,
205 0xb843c213,0x6c0743f1,0x8309893c,0x0feddd5f,
206 0x2f7fe850,0xd7c07f7e,0x02507fbf,0x5afb9a04,
207 0xa747d2d0,0x1651192e,0xaf70bf3e,0x58c31380,
208 0x5f98302e,0x727cc3c4,0x0a0fb402,0x0f7fef82,
209 0x8c96fdad,0x5d2c2aae,0x8ee99a49,0x50da88b8,
210 0x8427f4a0,0x1eac5790,0x796fb449,0x8252dc15,
211 0xefbd7d9b,0xa672597d,0xada840d8,0x45f54504,
212 0xfa5d7403,0xe83ec305,0x4f91751a,0x925669c2,
213 0x23efe941,0xa903f12e,0x60270df2,0x0276e4b6,
214 0x94fd6574,0x927985b2,0x8276dbcb,0x02778176,
215 0xf8af918d,0x4e48f79e,0x8f616ddf,0xe29d840e,
216 0x842f7d83,0x340ce5c8,0x96bbb682,0x93b4b148,
217 0xef303cab,0x984faf28,0x779faf9b,0x92dc560d,
218 0x224d1e20,0x8437aa88,0x7d29dc96,0x2756d3dc,
219 0x8b907cee,0xb51fd240,0xe7c07ce3,0xe566b4a1,
220 0xc3e9615e,0x3cf8209d,0x6094d1e3,0xcd9ca341,
221 0x5c76460e,0x00ea983b,0xd4d67881,0xfd47572c,
222 0xf76cedd9,0xbda8229c,0x127dadaa,0x438a074e,
223 0x1f97c090,0x081bdb8a,0x93a07ebe,0xb938ca15,
224 0x97b03cff,0x3dc2c0f8,0x8d1ab2ec,0x64380e51,
225 0x68cc7bfb,0xd90f2788,0x12490181,0x5de5ffd4,
226 0xdd7ef86a,0x76a2e214,0xb9a40368,0x925d958f,
227 0x4b39fffa,0xba39aee9,0xa4ffd30b,0xfaf7933b,
228 0x6d498623,0x193cbcfa,0x27627545,0x825cf47a,
229 0x61bd8ba0,0xd11e42d1,0xcead04f4,0x127ea392,
230 0x10428db7,0x8272a972,0x9270c4a8,0x127de50b,
231 0x285ba1c8,0x3c62f44f,0x35c0eaa5,0xe805d231,
232 0x428929fb,0xb4fcdf82,0x4fb66a53,0x0e7dc15b,
233 0x1f081fab,0x108618ae,0xfcfd086d,0xf9ff2889,
234 0x694bcc11,0x236a5cae,0x12deca4d,0x2c3f8cc5,
235 0xd2d02dfe,0xf8ef5896,0xe4cf52da,0x95155b67,
236 0x494a488c,0xb9b6a80c,0x5c8f82bc,0x89d36b45,
237 0x3a609437,0xec00c9a9,0x44715253,0x0a874b49,
238 0xd773bc40,0x7c34671c,0x02717ef6,0x4feb5536,
239 0xa2d02fff,0xd2bf60c4,0xd43f03c0,0x50b4ef6d,
240 0x07478cd1,0x006e1888,0xa2e53f55,0xb9e6d4bc,
241 0xa2048016,0x97573833,0xd7207d67,0xde0f8f3d,
242 0x72f87b33,0xabcc4f33,0x7688c55d,0x7b00a6b0,
243 0x947b0001,0x570075d2,0xf9bb88f8,0x8942019e,
244 0x4264a5ff,0x856302e0,0x72dbd92b,0xee971b69,
245 0x6ea22fde,0x5f08ae2b,0xaf7a616d,0xe5c98767,
246 0xcf1febd2,0x61efc8c2,0xf1ac2571,0xcc8239c2,
247 0x67214cb8,0xb1e583d1,0xb7dc3e62,0x7f10bdce,
248 0xf90a5c38,0x0ff0443d,0x606e6dc6,0x60543a49,
249 0x5727c148,0x2be98a1d,0x8ab41738,0x20e1be24,
250 0xaf96da0f,0x68458425,0x99833be5,0x600d457d,
251 0x282f9350,0x8334b362,0xd91d1120,0x2b6d8da0,
252 0x642b1e31,0x9c305a00,0x52bce688,0x1b03588a,
253 0xf7baefd5,0x4142ed9c,0xa4315c11,0x83323ec5,
254 0xdfef4636,0xa133c501,0xe9d3531c,0xee353783,
255 };
256CAST_LONG CAST_S_table3[256]={
257 0x9db30420,0x1fb6e9de,0xa7be7bef,0xd273a298,
258 0x4a4f7bdb,0x64ad8c57,0x85510443,0xfa020ed1,
259 0x7e287aff,0xe60fb663,0x095f35a1,0x79ebf120,
260 0xfd059d43,0x6497b7b1,0xf3641f63,0x241e4adf,
261 0x28147f5f,0x4fa2b8cd,0xc9430040,0x0cc32220,
262 0xfdd30b30,0xc0a5374f,0x1d2d00d9,0x24147b15,
263 0xee4d111a,0x0fca5167,0x71ff904c,0x2d195ffe,
264 0x1a05645f,0x0c13fefe,0x081b08ca,0x05170121,
265 0x80530100,0xe83e5efe,0xac9af4f8,0x7fe72701,
266 0xd2b8ee5f,0x06df4261,0xbb9e9b8a,0x7293ea25,
267 0xce84ffdf,0xf5718801,0x3dd64b04,0xa26f263b,
268 0x7ed48400,0x547eebe6,0x446d4ca0,0x6cf3d6f5,
269 0x2649abdf,0xaea0c7f5,0x36338cc1,0x503f7e93,
270 0xd3772061,0x11b638e1,0x72500e03,0xf80eb2bb,
271 0xabe0502e,0xec8d77de,0x57971e81,0xe14f6746,
272 0xc9335400,0x6920318f,0x081dbb99,0xffc304a5,
273 0x4d351805,0x7f3d5ce3,0xa6c866c6,0x5d5bcca9,
274 0xdaec6fea,0x9f926f91,0x9f46222f,0x3991467d,
275 0xa5bf6d8e,0x1143c44f,0x43958302,0xd0214eeb,
276 0x022083b8,0x3fb6180c,0x18f8931e,0x281658e6,
277 0x26486e3e,0x8bd78a70,0x7477e4c1,0xb506e07c,
278 0xf32d0a25,0x79098b02,0xe4eabb81,0x28123b23,
279 0x69dead38,0x1574ca16,0xdf871b62,0x211c40b7,
280 0xa51a9ef9,0x0014377b,0x041e8ac8,0x09114003,
281 0xbd59e4d2,0xe3d156d5,0x4fe876d5,0x2f91a340,
282 0x557be8de,0x00eae4a7,0x0ce5c2ec,0x4db4bba6,
283 0xe756bdff,0xdd3369ac,0xec17b035,0x06572327,
284 0x99afc8b0,0x56c8c391,0x6b65811c,0x5e146119,
285 0x6e85cb75,0xbe07c002,0xc2325577,0x893ff4ec,
286 0x5bbfc92d,0xd0ec3b25,0xb7801ab7,0x8d6d3b24,
287 0x20c763ef,0xc366a5fc,0x9c382880,0x0ace3205,
288 0xaac9548a,0xeca1d7c7,0x041afa32,0x1d16625a,
289 0x6701902c,0x9b757a54,0x31d477f7,0x9126b031,
290 0x36cc6fdb,0xc70b8b46,0xd9e66a48,0x56e55a79,
291 0x026a4ceb,0x52437eff,0x2f8f76b4,0x0df980a5,
292 0x8674cde3,0xedda04eb,0x17a9be04,0x2c18f4df,
293 0xb7747f9d,0xab2af7b4,0xefc34d20,0x2e096b7c,
294 0x1741a254,0xe5b6a035,0x213d42f6,0x2c1c7c26,
295 0x61c2f50f,0x6552daf9,0xd2c231f8,0x25130f69,
296 0xd8167fa2,0x0418f2c8,0x001a96a6,0x0d1526ab,
297 0x63315c21,0x5e0a72ec,0x49bafefd,0x187908d9,
298 0x8d0dbd86,0x311170a7,0x3e9b640c,0xcc3e10d7,
299 0xd5cad3b6,0x0caec388,0xf73001e1,0x6c728aff,
300 0x71eae2a1,0x1f9af36e,0xcfcbd12f,0xc1de8417,
301 0xac07be6b,0xcb44a1d8,0x8b9b0f56,0x013988c3,
302 0xb1c52fca,0xb4be31cd,0xd8782806,0x12a3a4e2,
303 0x6f7de532,0x58fd7eb6,0xd01ee900,0x24adffc2,
304 0xf4990fc5,0x9711aac5,0x001d7b95,0x82e5e7d2,
305 0x109873f6,0x00613096,0xc32d9521,0xada121ff,
306 0x29908415,0x7fbb977f,0xaf9eb3db,0x29c9ed2a,
307 0x5ce2a465,0xa730f32c,0xd0aa3fe8,0x8a5cc091,
308 0xd49e2ce7,0x0ce454a9,0xd60acd86,0x015f1919,
309 0x77079103,0xdea03af6,0x78a8565e,0xdee356df,
310 0x21f05cbe,0x8b75e387,0xb3c50651,0xb8a5c3ef,
311 0xd8eeb6d2,0xe523be77,0xc2154529,0x2f69efdf,
312 0xafe67afb,0xf470c4b2,0xf3e0eb5b,0xd6cc9876,
313 0x39e4460c,0x1fda8538,0x1987832f,0xca007367,
314 0xa99144f8,0x296b299e,0x492fc295,0x9266beab,
315 0xb5676e69,0x9bd3ddda,0xdf7e052f,0xdb25701c,
316 0x1b5e51ee,0xf65324e6,0x6afce36c,0x0316cc04,
317 0x8644213e,0xb7dc59d0,0x7965291f,0xccd6fd43,
318 0x41823979,0x932bcdf6,0xb657c34d,0x4edfd282,
319 0x7ae5290c,0x3cb9536b,0x851e20fe,0x9833557e,
320 0x13ecf0b0,0xd3ffb372,0x3f85c5c1,0x0aef7ed2,
321 };
322CAST_LONG CAST_S_table4[256]={
323 0x7ec90c04,0x2c6e74b9,0x9b0e66df,0xa6337911,
324 0xb86a7fff,0x1dd358f5,0x44dd9d44,0x1731167f,
325 0x08fbf1fa,0xe7f511cc,0xd2051b00,0x735aba00,
326 0x2ab722d8,0x386381cb,0xacf6243a,0x69befd7a,
327 0xe6a2e77f,0xf0c720cd,0xc4494816,0xccf5c180,
328 0x38851640,0x15b0a848,0xe68b18cb,0x4caadeff,
329 0x5f480a01,0x0412b2aa,0x259814fc,0x41d0efe2,
330 0x4e40b48d,0x248eb6fb,0x8dba1cfe,0x41a99b02,
331 0x1a550a04,0xba8f65cb,0x7251f4e7,0x95a51725,
332 0xc106ecd7,0x97a5980a,0xc539b9aa,0x4d79fe6a,
333 0xf2f3f763,0x68af8040,0xed0c9e56,0x11b4958b,
334 0xe1eb5a88,0x8709e6b0,0xd7e07156,0x4e29fea7,
335 0x6366e52d,0x02d1c000,0xc4ac8e05,0x9377f571,
336 0x0c05372a,0x578535f2,0x2261be02,0xd642a0c9,
337 0xdf13a280,0x74b55bd2,0x682199c0,0xd421e5ec,
338 0x53fb3ce8,0xc8adedb3,0x28a87fc9,0x3d959981,
339 0x5c1ff900,0xfe38d399,0x0c4eff0b,0x062407ea,
340 0xaa2f4fb1,0x4fb96976,0x90c79505,0xb0a8a774,
341 0xef55a1ff,0xe59ca2c2,0xa6b62d27,0xe66a4263,
342 0xdf65001f,0x0ec50966,0xdfdd55bc,0x29de0655,
343 0x911e739a,0x17af8975,0x32c7911c,0x89f89468,
344 0x0d01e980,0x524755f4,0x03b63cc9,0x0cc844b2,
345 0xbcf3f0aa,0x87ac36e9,0xe53a7426,0x01b3d82b,
346 0x1a9e7449,0x64ee2d7e,0xcddbb1da,0x01c94910,
347 0xb868bf80,0x0d26f3fd,0x9342ede7,0x04a5c284,
348 0x636737b6,0x50f5b616,0xf24766e3,0x8eca36c1,
349 0x136e05db,0xfef18391,0xfb887a37,0xd6e7f7d4,
350 0xc7fb7dc9,0x3063fcdf,0xb6f589de,0xec2941da,
351 0x26e46695,0xb7566419,0xf654efc5,0xd08d58b7,
352 0x48925401,0xc1bacb7f,0xe5ff550f,0xb6083049,
353 0x5bb5d0e8,0x87d72e5a,0xab6a6ee1,0x223a66ce,
354 0xc62bf3cd,0x9e0885f9,0x68cb3e47,0x086c010f,
355 0xa21de820,0xd18b69de,0xf3f65777,0xfa02c3f6,
356 0x407edac3,0xcbb3d550,0x1793084d,0xb0d70eba,
357 0x0ab378d5,0xd951fb0c,0xded7da56,0x4124bbe4,
358 0x94ca0b56,0x0f5755d1,0xe0e1e56e,0x6184b5be,
359 0x580a249f,0x94f74bc0,0xe327888e,0x9f7b5561,
360 0xc3dc0280,0x05687715,0x646c6bd7,0x44904db3,
361 0x66b4f0a3,0xc0f1648a,0x697ed5af,0x49e92ff6,
362 0x309e374f,0x2cb6356a,0x85808573,0x4991f840,
363 0x76f0ae02,0x083be84d,0x28421c9a,0x44489406,
364 0x736e4cb8,0xc1092910,0x8bc95fc6,0x7d869cf4,
365 0x134f616f,0x2e77118d,0xb31b2be1,0xaa90b472,
366 0x3ca5d717,0x7d161bba,0x9cad9010,0xaf462ba2,
367 0x9fe459d2,0x45d34559,0xd9f2da13,0xdbc65487,
368 0xf3e4f94e,0x176d486f,0x097c13ea,0x631da5c7,
369 0x445f7382,0x175683f4,0xcdc66a97,0x70be0288,
370 0xb3cdcf72,0x6e5dd2f3,0x20936079,0x459b80a5,
371 0xbe60e2db,0xa9c23101,0xeba5315c,0x224e42f2,
372 0x1c5c1572,0xf6721b2c,0x1ad2fff3,0x8c25404e,
373 0x324ed72f,0x4067b7fd,0x0523138e,0x5ca3bc78,
374 0xdc0fd66e,0x75922283,0x784d6b17,0x58ebb16e,
375 0x44094f85,0x3f481d87,0xfcfeae7b,0x77b5ff76,
376 0x8c2302bf,0xaaf47556,0x5f46b02a,0x2b092801,
377 0x3d38f5f7,0x0ca81f36,0x52af4a8a,0x66d5e7c0,
378 0xdf3b0874,0x95055110,0x1b5ad7a8,0xf61ed5ad,
379 0x6cf6e479,0x20758184,0xd0cefa65,0x88f7be58,
380 0x4a046826,0x0ff6f8f3,0xa09c7f70,0x5346aba0,
381 0x5ce96c28,0xe176eda3,0x6bac307f,0x376829d2,
382 0x85360fa9,0x17e3fe2a,0x24b79767,0xf5a96b20,
383 0xd6cd2595,0x68ff1ebf,0x7555442c,0xf19f06be,
384 0xf9e0659a,0xeeb9491d,0x34010718,0xbb30cab8,
385 0xe822fe15,0x88570983,0x750e6249,0xda627e55,
386 0x5e76ffa8,0xb1534546,0x6d47de08,0xefe9e7d4,
387 };
388CAST_LONG CAST_S_table5[256]={
389 0xf6fa8f9d,0x2cac6ce1,0x4ca34867,0xe2337f7c,
390 0x95db08e7,0x016843b4,0xeced5cbc,0x325553ac,
391 0xbf9f0960,0xdfa1e2ed,0x83f0579d,0x63ed86b9,
392 0x1ab6a6b8,0xde5ebe39,0xf38ff732,0x8989b138,
393 0x33f14961,0xc01937bd,0xf506c6da,0xe4625e7e,
394 0xa308ea99,0x4e23e33c,0x79cbd7cc,0x48a14367,
395 0xa3149619,0xfec94bd5,0xa114174a,0xeaa01866,
396 0xa084db2d,0x09a8486f,0xa888614a,0x2900af98,
397 0x01665991,0xe1992863,0xc8f30c60,0x2e78ef3c,
398 0xd0d51932,0xcf0fec14,0xf7ca07d2,0xd0a82072,
399 0xfd41197e,0x9305a6b0,0xe86be3da,0x74bed3cd,
400 0x372da53c,0x4c7f4448,0xdab5d440,0x6dba0ec3,
401 0x083919a7,0x9fbaeed9,0x49dbcfb0,0x4e670c53,
402 0x5c3d9c01,0x64bdb941,0x2c0e636a,0xba7dd9cd,
403 0xea6f7388,0xe70bc762,0x35f29adb,0x5c4cdd8d,
404 0xf0d48d8c,0xb88153e2,0x08a19866,0x1ae2eac8,
405 0x284caf89,0xaa928223,0x9334be53,0x3b3a21bf,
406 0x16434be3,0x9aea3906,0xefe8c36e,0xf890cdd9,
407 0x80226dae,0xc340a4a3,0xdf7e9c09,0xa694a807,
408 0x5b7c5ecc,0x221db3a6,0x9a69a02f,0x68818a54,
409 0xceb2296f,0x53c0843a,0xfe893655,0x25bfe68a,
410 0xb4628abc,0xcf222ebf,0x25ac6f48,0xa9a99387,
411 0x53bddb65,0xe76ffbe7,0xe967fd78,0x0ba93563,
412 0x8e342bc1,0xe8a11be9,0x4980740d,0xc8087dfc,
413 0x8de4bf99,0xa11101a0,0x7fd37975,0xda5a26c0,
414 0xe81f994f,0x9528cd89,0xfd339fed,0xb87834bf,
415 0x5f04456d,0x22258698,0xc9c4c83b,0x2dc156be,
416 0x4f628daa,0x57f55ec5,0xe2220abe,0xd2916ebf,
417 0x4ec75b95,0x24f2c3c0,0x42d15d99,0xcd0d7fa0,
418 0x7b6e27ff,0xa8dc8af0,0x7345c106,0xf41e232f,
419 0x35162386,0xe6ea8926,0x3333b094,0x157ec6f2,
420 0x372b74af,0x692573e4,0xe9a9d848,0xf3160289,
421 0x3a62ef1d,0xa787e238,0xf3a5f676,0x74364853,
422 0x20951063,0x4576698d,0xb6fad407,0x592af950,
423 0x36f73523,0x4cfb6e87,0x7da4cec0,0x6c152daa,
424 0xcb0396a8,0xc50dfe5d,0xfcd707ab,0x0921c42f,
425 0x89dff0bb,0x5fe2be78,0x448f4f33,0x754613c9,
426 0x2b05d08d,0x48b9d585,0xdc049441,0xc8098f9b,
427 0x7dede786,0xc39a3373,0x42410005,0x6a091751,
428 0x0ef3c8a6,0x890072d6,0x28207682,0xa9a9f7be,
429 0xbf32679d,0xd45b5b75,0xb353fd00,0xcbb0e358,
430 0x830f220a,0x1f8fb214,0xd372cf08,0xcc3c4a13,
431 0x8cf63166,0x061c87be,0x88c98f88,0x6062e397,
432 0x47cf8e7a,0xb6c85283,0x3cc2acfb,0x3fc06976,
433 0x4e8f0252,0x64d8314d,0xda3870e3,0x1e665459,
434 0xc10908f0,0x513021a5,0x6c5b68b7,0x822f8aa0,
435 0x3007cd3e,0x74719eef,0xdc872681,0x073340d4,
436 0x7e432fd9,0x0c5ec241,0x8809286c,0xf592d891,
437 0x08a930f6,0x957ef305,0xb7fbffbd,0xc266e96f,
438 0x6fe4ac98,0xb173ecc0,0xbc60b42a,0x953498da,
439 0xfba1ae12,0x2d4bd736,0x0f25faab,0xa4f3fceb,
440 0xe2969123,0x257f0c3d,0x9348af49,0x361400bc,
441 0xe8816f4a,0x3814f200,0xa3f94043,0x9c7a54c2,
442 0xbc704f57,0xda41e7f9,0xc25ad33a,0x54f4a084,
443 0xb17f5505,0x59357cbe,0xedbd15c8,0x7f97c5ab,
444 0xba5ac7b5,0xb6f6deaf,0x3a479c3a,0x5302da25,
445 0x653d7e6a,0x54268d49,0x51a477ea,0x5017d55b,
446 0xd7d25d88,0x44136c76,0x0404a8c8,0xb8e5a121,
447 0xb81a928a,0x60ed5869,0x97c55b96,0xeaec991b,
448 0x29935913,0x01fdb7f1,0x088e8dfa,0x9ab6f6f5,
449 0x3b4cbf9f,0x4a5de3ab,0xe6051d35,0xa0e1d855,
450 0xd36b4cf1,0xf544edeb,0xb0e93524,0xbebb8fbd,
451 0xa2d762cf,0x49c92f54,0x38b5f331,0x7128a454,
452 0x48392905,0xa65b1db8,0x851c97bd,0xd675cf2f,
453 };
454CAST_LONG CAST_S_table6[256]={
455 0x85e04019,0x332bf567,0x662dbfff,0xcfc65693,
456 0x2a8d7f6f,0xab9bc912,0xde6008a1,0x2028da1f,
457 0x0227bce7,0x4d642916,0x18fac300,0x50f18b82,
458 0x2cb2cb11,0xb232e75c,0x4b3695f2,0xb28707de,
459 0xa05fbcf6,0xcd4181e9,0xe150210c,0xe24ef1bd,
460 0xb168c381,0xfde4e789,0x5c79b0d8,0x1e8bfd43,
461 0x4d495001,0x38be4341,0x913cee1d,0x92a79c3f,
462 0x089766be,0xbaeeadf4,0x1286becf,0xb6eacb19,
463 0x2660c200,0x7565bde4,0x64241f7a,0x8248dca9,
464 0xc3b3ad66,0x28136086,0x0bd8dfa8,0x356d1cf2,
465 0x107789be,0xb3b2e9ce,0x0502aa8f,0x0bc0351e,
466 0x166bf52a,0xeb12ff82,0xe3486911,0xd34d7516,
467 0x4e7b3aff,0x5f43671b,0x9cf6e037,0x4981ac83,
468 0x334266ce,0x8c9341b7,0xd0d854c0,0xcb3a6c88,
469 0x47bc2829,0x4725ba37,0xa66ad22b,0x7ad61f1e,
470 0x0c5cbafa,0x4437f107,0xb6e79962,0x42d2d816,
471 0x0a961288,0xe1a5c06e,0x13749e67,0x72fc081a,
472 0xb1d139f7,0xf9583745,0xcf19df58,0xbec3f756,
473 0xc06eba30,0x07211b24,0x45c28829,0xc95e317f,
474 0xbc8ec511,0x38bc46e9,0xc6e6fa14,0xbae8584a,
475 0xad4ebc46,0x468f508b,0x7829435f,0xf124183b,
476 0x821dba9f,0xaff60ff4,0xea2c4e6d,0x16e39264,
477 0x92544a8b,0x009b4fc3,0xaba68ced,0x9ac96f78,
478 0x06a5b79a,0xb2856e6e,0x1aec3ca9,0xbe838688,
479 0x0e0804e9,0x55f1be56,0xe7e5363b,0xb3a1f25d,
480 0xf7debb85,0x61fe033c,0x16746233,0x3c034c28,
481 0xda6d0c74,0x79aac56c,0x3ce4e1ad,0x51f0c802,
482 0x98f8f35a,0x1626a49f,0xeed82b29,0x1d382fe3,
483 0x0c4fb99a,0xbb325778,0x3ec6d97b,0x6e77a6a9,
484 0xcb658b5c,0xd45230c7,0x2bd1408b,0x60c03eb7,
485 0xb9068d78,0xa33754f4,0xf430c87d,0xc8a71302,
486 0xb96d8c32,0xebd4e7be,0xbe8b9d2d,0x7979fb06,
487 0xe7225308,0x8b75cf77,0x11ef8da4,0xe083c858,
488 0x8d6b786f,0x5a6317a6,0xfa5cf7a0,0x5dda0033,
489 0xf28ebfb0,0xf5b9c310,0xa0eac280,0x08b9767a,
490 0xa3d9d2b0,0x79d34217,0x021a718d,0x9ac6336a,
491 0x2711fd60,0x438050e3,0x069908a8,0x3d7fedc4,
492 0x826d2bef,0x4eeb8476,0x488dcf25,0x36c9d566,
493 0x28e74e41,0xc2610aca,0x3d49a9cf,0xbae3b9df,
494 0xb65f8de6,0x92aeaf64,0x3ac7d5e6,0x9ea80509,
495 0xf22b017d,0xa4173f70,0xdd1e16c3,0x15e0d7f9,
496 0x50b1b887,0x2b9f4fd5,0x625aba82,0x6a017962,
497 0x2ec01b9c,0x15488aa9,0xd716e740,0x40055a2c,
498 0x93d29a22,0xe32dbf9a,0x058745b9,0x3453dc1e,
499 0xd699296e,0x496cff6f,0x1c9f4986,0xdfe2ed07,
500 0xb87242d1,0x19de7eae,0x053e561a,0x15ad6f8c,
501 0x66626c1c,0x7154c24c,0xea082b2a,0x93eb2939,
502 0x17dcb0f0,0x58d4f2ae,0x9ea294fb,0x52cf564c,
503 0x9883fe66,0x2ec40581,0x763953c3,0x01d6692e,
504 0xd3a0c108,0xa1e7160e,0xe4f2dfa6,0x693ed285,
505 0x74904698,0x4c2b0edd,0x4f757656,0x5d393378,
506 0xa132234f,0x3d321c5d,0xc3f5e194,0x4b269301,
507 0xc79f022f,0x3c997e7e,0x5e4f9504,0x3ffafbbd,
508 0x76f7ad0e,0x296693f4,0x3d1fce6f,0xc61e45be,
509 0xd3b5ab34,0xf72bf9b7,0x1b0434c0,0x4e72b567,
510 0x5592a33d,0xb5229301,0xcfd2a87f,0x60aeb767,
511 0x1814386b,0x30bcc33d,0x38a0c07d,0xfd1606f2,
512 0xc363519b,0x589dd390,0x5479f8e6,0x1cb8d647,
513 0x97fd61a9,0xea7759f4,0x2d57539d,0x569a58cf,
514 0xe84e63ad,0x462e1b78,0x6580f87e,0xf3817914,
515 0x91da55f4,0x40a230f3,0xd1988f35,0xb6e318d2,
516 0x3ffa50bc,0x3d40f021,0xc3c0bdae,0x4958c24c,
517 0x518f36b2,0x84b1d370,0x0fedce83,0x878ddada,
518 0xf2a279c7,0x94e01be8,0x90716f4b,0x954b8aa3,
519 };
520CAST_LONG CAST_S_table7[256]={
521 0xe216300d,0xbbddfffc,0xa7ebdabd,0x35648095,
522 0x7789f8b7,0xe6c1121b,0x0e241600,0x052ce8b5,
523 0x11a9cfb0,0xe5952f11,0xece7990a,0x9386d174,
524 0x2a42931c,0x76e38111,0xb12def3a,0x37ddddfc,
525 0xde9adeb1,0x0a0cc32c,0xbe197029,0x84a00940,
526 0xbb243a0f,0xb4d137cf,0xb44e79f0,0x049eedfd,
527 0x0b15a15d,0x480d3168,0x8bbbde5a,0x669ded42,
528 0xc7ece831,0x3f8f95e7,0x72df191b,0x7580330d,
529 0x94074251,0x5c7dcdfa,0xabbe6d63,0xaa402164,
530 0xb301d40a,0x02e7d1ca,0x53571dae,0x7a3182a2,
531 0x12a8ddec,0xfdaa335d,0x176f43e8,0x71fb46d4,
532 0x38129022,0xce949ad4,0xb84769ad,0x965bd862,
533 0x82f3d055,0x66fb9767,0x15b80b4e,0x1d5b47a0,
534 0x4cfde06f,0xc28ec4b8,0x57e8726e,0x647a78fc,
535 0x99865d44,0x608bd593,0x6c200e03,0x39dc5ff6,
536 0x5d0b00a3,0xae63aff2,0x7e8bd632,0x70108c0c,
537 0xbbd35049,0x2998df04,0x980cf42a,0x9b6df491,
538 0x9e7edd53,0x06918548,0x58cb7e07,0x3b74ef2e,
539 0x522fffb1,0xd24708cc,0x1c7e27cd,0xa4eb215b,
540 0x3cf1d2e2,0x19b47a38,0x424f7618,0x35856039,
541 0x9d17dee7,0x27eb35e6,0xc9aff67b,0x36baf5b8,
542 0x09c467cd,0xc18910b1,0xe11dbf7b,0x06cd1af8,
543 0x7170c608,0x2d5e3354,0xd4de495a,0x64c6d006,
544 0xbcc0c62c,0x3dd00db3,0x708f8f34,0x77d51b42,
545 0x264f620f,0x24b8d2bf,0x15c1b79e,0x46a52564,
546 0xf8d7e54e,0x3e378160,0x7895cda5,0x859c15a5,
547 0xe6459788,0xc37bc75f,0xdb07ba0c,0x0676a3ab,
548 0x7f229b1e,0x31842e7b,0x24259fd7,0xf8bef472,
549 0x835ffcb8,0x6df4c1f2,0x96f5b195,0xfd0af0fc,
550 0xb0fe134c,0xe2506d3d,0x4f9b12ea,0xf215f225,
551 0xa223736f,0x9fb4c428,0x25d04979,0x34c713f8,
552 0xc4618187,0xea7a6e98,0x7cd16efc,0x1436876c,
553 0xf1544107,0xbedeee14,0x56e9af27,0xa04aa441,
554 0x3cf7c899,0x92ecbae6,0xdd67016d,0x151682eb,
555 0xa842eedf,0xfdba60b4,0xf1907b75,0x20e3030f,
556 0x24d8c29e,0xe139673b,0xefa63fb8,0x71873054,
557 0xb6f2cf3b,0x9f326442,0xcb15a4cc,0xb01a4504,
558 0xf1e47d8d,0x844a1be5,0xbae7dfdc,0x42cbda70,
559 0xcd7dae0a,0x57e85b7a,0xd53f5af6,0x20cf4d8c,
560 0xcea4d428,0x79d130a4,0x3486ebfb,0x33d3cddc,
561 0x77853b53,0x37effcb5,0xc5068778,0xe580b3e6,
562 0x4e68b8f4,0xc5c8b37e,0x0d809ea2,0x398feb7c,
563 0x132a4f94,0x43b7950e,0x2fee7d1c,0x223613bd,
564 0xdd06caa2,0x37df932b,0xc4248289,0xacf3ebc3,
565 0x5715f6b7,0xef3478dd,0xf267616f,0xc148cbe4,
566 0x9052815e,0x5e410fab,0xb48a2465,0x2eda7fa4,
567 0xe87b40e4,0xe98ea084,0x5889e9e1,0xefd390fc,
568 0xdd07d35b,0xdb485694,0x38d7e5b2,0x57720101,
569 0x730edebc,0x5b643113,0x94917e4f,0x503c2fba,
570 0x646f1282,0x7523d24a,0xe0779695,0xf9c17a8f,
571 0x7a5b2121,0xd187b896,0x29263a4d,0xba510cdf,
572 0x81f47c9f,0xad1163ed,0xea7b5965,0x1a00726e,
573 0x11403092,0x00da6d77,0x4a0cdd61,0xad1f4603,
574 0x605bdfb0,0x9eedc364,0x22ebe6a8,0xcee7d28a,
575 0xa0e736a0,0x5564a6b9,0x10853209,0xc7eb8f37,
576 0x2de705ca,0x8951570f,0xdf09822b,0xbd691a6c,
577 0xaa12e4f2,0x87451c0f,0xe0f6a27a,0x3ada4819,
578 0x4cf1764f,0x0d771c2b,0x67cdb156,0x350d8384,
579 0x5938fa0f,0x42399ef3,0x36997b07,0x0e84093d,
580 0x4aa93e61,0x8360d87b,0x1fa98b0c,0x1149382c,
581 0xe97625a5,0x0614d1b7,0x0e25244b,0x0c768347,
582 0x589e8d82,0x0d2059d1,0xa466bb1e,0xf8da0a82,
583 0x04f19130,0xba6e4ec0,0x99265164,0x1ee7230d,
584 0x50b2ad80,0xeaee6801,0x8db2a283,0xea8bf59e,
585 };
diff --git a/src/lib/libcrypto/conf/conf.h b/src/lib/libcrypto/conf/conf.h
new file mode 100644
index 0000000000..1446226a16
--- /dev/null
+++ b/src/lib/libcrypto/conf/conf.h
@@ -0,0 +1,114 @@
1/* crypto/conf/conf.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_CONF_H
60#define HEADER_CONF_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "stack.h"
67#include "lhash.h"
68
69typedef struct
70 {
71 char *section;
72 char *name;
73 char *value;
74 } CONF_VALUE;
75
76#ifndef NOPROTO
77
78LHASH *CONF_load(LHASH *conf,char *file,long *eline);
79STACK *CONF_get_section(LHASH *conf,char *section);
80char *CONF_get_string(LHASH *conf,char *group,char *name);
81long CONF_get_number(LHASH *conf,char *group,char *name);
82void CONF_free(LHASH *conf);
83void ERR_load_CONF_strings(void );
84
85#else
86
87LHASH *CONF_load();
88STACK *CONF_get_section();
89char *CONF_get_string();
90long CONF_get_number();
91void CONF_free();
92void ERR_load_CONF_strings();
93
94#endif
95
96/* BEGIN ERROR CODES */
97/* Error codes for the CONF functions. */
98
99/* Function codes. */
100#define CONF_F_CONF_LOAD 100
101#define CONF_F_STR_COPY 101
102
103/* Reason codes. */
104#define CONF_R_MISSING_CLOSE_SQUARE_BRACKET 100
105#define CONF_R_MISSING_EQUAL_SIGN 101
106#define CONF_R_NO_CLOSE_BRACE 102
107#define CONF_R_UNABLE_TO_CREATE_NEW_SECTION 103
108#define CONF_R_VARIABLE_HAS_NO_VALUE 104
109
110#ifdef __cplusplus
111}
112#endif
113#endif
114
diff --git a/src/lib/libcrypto/conf/conf_err.c b/src/lib/libcrypto/conf/conf_err.c
new file mode 100644
index 0000000000..a8db8f266f
--- /dev/null
+++ b/src/lib/libcrypto/conf/conf_err.c
@@ -0,0 +1,96 @@
1/* lib/conf/conf_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "conf.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA CONF_str_functs[]=
65 {
66{ERR_PACK(0,CONF_F_CONF_LOAD,0), "CONF_load"},
67{ERR_PACK(0,CONF_F_STR_COPY,0), "STR_COPY"},
68{0,NULL},
69 };
70
71static ERR_STRING_DATA CONF_str_reasons[]=
72 {
73{CONF_R_MISSING_CLOSE_SQUARE_BRACKET ,"missing close square bracket"},
74{CONF_R_MISSING_EQUAL_SIGN ,"missing equal sign"},
75{CONF_R_NO_CLOSE_BRACE ,"no close brace"},
76{CONF_R_UNABLE_TO_CREATE_NEW_SECTION ,"unable to create new section"},
77{CONF_R_VARIABLE_HAS_NO_VALUE ,"variable has no value"},
78{0,NULL},
79 };
80
81#endif
82
83void ERR_load_CONF_strings()
84 {
85 static int init=1;
86
87 if (init);
88 {;
89 init=0;
90#ifndef NO_ERR
91 ERR_load_strings(ERR_LIB_CONF,CONF_str_functs);
92 ERR_load_strings(ERR_LIB_CONF,CONF_str_reasons);
93#endif
94
95 }
96 }
diff --git a/src/lib/libcrypto/conf/keysets.pl b/src/lib/libcrypto/conf/keysets.pl
new file mode 100644
index 0000000000..e40fed0ca1
--- /dev/null
+++ b/src/lib/libcrypto/conf/keysets.pl
@@ -0,0 +1,61 @@
1#!/usr/bin/perl
2
3$NUMBER=0x01;
4$UPPER=0x02;
5$LOWER=0x04;
6$EOF=0x08;
7$WS=0x10;
8$ESC=0x20;
9$QUOTE=0x40;
10$COMMENT=0x80;
11$UNDER=0x100;
12
13foreach (0 .. 127)
14 {
15 $v=0;
16 $c=sprintf("%c",$_);
17 $v|=$NUMBER if ($c =~ /[0-9]/);
18 $v|=$UPPER if ($c =~ /[A-Z]/);
19 $v|=$LOWER if ($c =~ /[a-z]/);
20 $v|=$UNDER if ($c =~ /_/);
21 $v|=$WS if ($c =~ / \t\r\n/);
22 $v|=$ESC if ($c =~ /\\/);
23 $v|=$QUOTE if ($c =~ /['`"]/);
24 $v|=$COMMENT if ($c =~ /\#/);
25 $v|=$EOF if ($c =~ /\0/);
26
27 push(@V,$v);
28 }
29
30print <<"EOF";
31#define CONF_NUMBER $NUMBER
32#define CONF_UPPER $UPPER
33#define CONF_LOWER $LOWER
34#define CONF_EOF $EOF
35#define CONF_WS $WS
36#define CONF_ESC $ESC
37#define CONF_QUOTE $QUOTE
38#define CONF_COMMENT $COMMENT
39#define CONF_ALPHA (CONF_UPPER|CONF_LOWER)
40#define CONF_ALPHA_NUMERIC (CONF_ALPHA|CONF_NUMBER|CONF_UNDER)
41#define CONF_UNDER $UNDER
42
43#define IS_COMMENT(a) (CONF_COMMENT&(CONF_type[(a)&0x7f]))
44#define IS_EOF(a) ((a) == '\\0')
45#define IS_ESC(a) ((a) == '\\\\')
46#define IS_NUMER(a) (CONF_type[(a)&0x7f]&CONF_NUMBER)
47#define IS_WS(a) (CONF_type[(a)&0x7f]&CONF_WS)
48#define IS_ALPHA_NUMERIC(a) (CONF_type[(a)&0x7f]&CONF_ALPHA_NUMERIC)
49#define IS_QUOTE(a) (CONF_type[(a)&0x7f]&CONF_QUOTE)
50
51EOF
52
53print "static unsigned short CONF_type[128]={";
54
55for ($i=0; $i<128; $i++)
56 {
57 print "\n\t" if ($i % 8) == 0;
58 printf "0x%03X,",$V[$i];
59 }
60
61print "\n\t};\n";
diff --git a/src/lib/libcrypto/conf/ssleay.cnf b/src/lib/libcrypto/conf/ssleay.cnf
new file mode 100644
index 0000000000..ed33af601e
--- /dev/null
+++ b/src/lib/libcrypto/conf/ssleay.cnf
@@ -0,0 +1,78 @@
1#
2# This is a test configuration file for use in SSLeay etc...
3#
4
5init = 5
6in\#it1 =10
7init2='10'
8init3='10\''
9init4="10'"
10init5='='10\'' again'
11
12SSLeay::version = 0.5.0
13
14[genrsa]
15default_bits = 512
16SSLEAY::version = 0.5.0
17
18[gendh]
19default_bits = 512
20def_generator = 2
21
22[s_client]
23cipher1 = DES_CBC_MD5:DES_CBC_SHA:DES_EDE_SHA:RC4_MD5\
24cipher2 = 'DES_CBC_MD5 DES_CBC_SHA DES_EDE_SHA RC4_MD5'
25cipher3 = "DES_CBC_MD5 DES_CBC_SHA DES_EDE_SHA RC4_MD5"
26cipher4 = DES_CBC_MD5 DES_CBC_SHA DES_EDE_SHA RC4_MD5
27
28[ default ]
29cert_dir = $ENV::HOME/.ca_certs
30
31HOME = /tmp/eay
32
33tmp_cert_dir = $HOME/.ca_certs
34tmp2_cert_dir = thisis$(HOME)stuff
35
36LOGNAME = Eric Young (home=$HOME)
37
38[ special ]
39
40H=$HOME
41H=$default::HOME
42H=$ENV::HOME
43#
44# SSLeay example configuration file.
45# This is mostly being used for generation of certificate requests.
46#
47
48RANDFILE = $HOME/.rand
49
50[ req ]
51default_bits = 512
52default_keyfile = privkey.pem
53
54Attribute_type_1 = countryName
55Attribute_text_1 = Country Name (2 letter code)
56Attribute_default_1 = AU
57
58Attribute_type_2 = stateOrProvinceName
59Attribute_text_2 = State or Province Name (full name)
60Attribute_default_2 = Queensland
61
62Attribute_type_3 = localityName
63Attribute_text_3 = Locality Name (eg, city)
64
65Attribute_type_4 = organizationName
66Attribute_text_4 = Organization Name (eg, company)
67Attribute_default_4 = Mincom Pty Ltd
68
69Attribute_type_5 = organizationalUnitName
70Attribute_text_5 = Organizational Unit Name (eg, section)
71Attribute_default_5 = TR
72
73Attribute_type_6 = commonName
74Attribute_text_6 = Common Name (eg, YOUR name)
75
76Attribute_type_7 = emailAddress
77Attribute_text_7 = Email Address
78
diff --git a/src/lib/libcrypto/cpt_err.c b/src/lib/libcrypto/cpt_err.c
new file mode 100644
index 0000000000..ea3c135d39
--- /dev/null
+++ b/src/lib/libcrypto/cpt_err.c
@@ -0,0 +1,86 @@
1/* lib/crypto/crypto_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "crypto.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA CRYPTO_str_functs[]=
65 {
66{ERR_PACK(0,CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,0), "CRYPTO_get_ex_new_index"},
67{ERR_PACK(0,CRYPTO_F_CRYPTO_GET_NEW_LOCKID,0), "CRYPTO_get_new_lockid"},
68{ERR_PACK(0,CRYPTO_F_CRYPTO_SET_EX_DATA,0), "CRYPTO_set_ex_data"},
69{0,NULL},
70 };
71
72#endif
73
74void ERR_load_CRYPTO_strings()
75 {
76 static int init=1;
77
78 if (init);
79 {;
80 init=0;
81#ifndef NO_ERR
82 ERR_load_strings(ERR_LIB_CRYPTO,CRYPTO_str_functs);
83#endif
84
85 }
86 }
diff --git a/src/lib/libcrypto/cryptlib.c b/src/lib/libcrypto/cryptlib.c
new file mode 100644
index 0000000000..9a7e80b7f8
--- /dev/null
+++ b/src/lib/libcrypto/cryptlib.c
@@ -0,0 +1,307 @@
1/* crypto/cryptlib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include "cryptlib.h"
62#include "crypto.h"
63#include "date.h"
64
65#if defined(WIN32) || defined(WIN16)
66static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */
67#endif
68
69/* real #defines in crypto.h, keep these upto date */
70static char* lock_names[CRYPTO_NUM_LOCKS] =
71 {
72 "<<ERROR>>",
73 "err",
74 "err_hash",
75 "x509",
76 "x509_info",
77 "x509_pkey",
78 "x509_crl",
79 "x509_req",
80 "dsa",
81 "rsa",
82 "evp_pkey",
83 "x509_store",
84 "ssl_ctx",
85 "ssl_cert",
86 "ssl_session",
87 "ssl",
88 "rand",
89 "debug_malloc",
90 "BIO",
91 "bio_gethostbyname",
92 "RSA_blinding",
93 };
94
95static STACK *app_locks=NULL;
96
97#ifndef NOPROTO
98static void (MS_FAR *locking_callback)(int mode,int type,
99 char *file,int line)=NULL;
100static int (MS_FAR *add_lock_callback)(int *pointer,int amount,
101 int type,char *file,int line)=NULL;
102static unsigned long (MS_FAR *id_callback)(void)=NULL;
103#else
104static void (MS_FAR *locking_callback)()=NULL;
105static int (MS_FAR *add_lock_callback)()=NULL;
106static unsigned long (MS_FAR *id_callback)()=NULL;
107#endif
108
109int CRYPTO_get_new_lockid(name)
110char *name;
111 {
112 char *str;
113 int i;
114
115 /* A hack to make Visual C++ 5.0 work correctly when linking as
116 * a DLL using /MT. Without this, the application cannot use
117 * and floating point printf's.
118 * It also seems to be needed for Visual C 1.5 (win16) */
119#if defined(WIN32) || defined(WIN16)
120 SSLeay_MSVC5_hack=(double)name[0]*(double)name[1];
121#endif
122
123 if (app_locks == NULL)
124 if ((app_locks=sk_new_null()) == NULL)
125 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
126 return(0);
127 if ((str=BUF_strdup(name)) == NULL)
128 return(0);
129 i=sk_push(app_locks,str);
130 if (!i)
131 Free(str);
132 else
133 i+=CRYPTO_NUM_LOCKS; /* gap of one :-) */
134 return(i);
135 }
136
137void (*CRYPTO_get_locking_callback(P_V))(P_I_I_P_I)
138 {
139 return(locking_callback);
140 }
141
142int (*CRYPTO_get_add_lock_callback(P_V))(P_IP_I_I_P_I)
143 {
144 return(add_lock_callback);
145 }
146
147void CRYPTO_set_locking_callback(func)
148void (*func)(P_I_I_P_I);
149 {
150 locking_callback=func;
151 }
152
153void CRYPTO_set_add_lock_callback(func)
154int (*func)(P_IP_I_I_P_I);
155 {
156 add_lock_callback=func;
157 }
158
159unsigned long (*CRYPTO_get_id_callback(P_V))(P_V)
160 {
161 return(id_callback);
162 }
163
164void CRYPTO_set_id_callback(func)
165unsigned long (*func)(P_V);
166 {
167 id_callback=func;
168 }
169
170unsigned long CRYPTO_thread_id()
171 {
172 unsigned long ret=0;
173
174 if (id_callback == NULL)
175 {
176#ifdef WIN16
177 ret=(unsigned long)GetCurrentTask();
178#elif defined(WIN32)
179 ret=(unsigned long)GetCurrentThreadId();
180#elif defined(MSDOS)
181 ret=1L;
182#else
183 ret=(unsigned long)getpid();
184#endif
185 }
186 else
187 ret=id_callback();
188 return(ret);
189 }
190
191void CRYPTO_lock(mode,type,file,line)
192int mode;
193int type;
194char *file;
195int line;
196 {
197#ifdef LOCK_DEBUG
198 {
199 char *rw_text,*operation_text;
200
201 if (mode & CRYPTO_LOCK)
202 operation_text="lock ";
203 else if (mode & CRYPTO_UNLOCK)
204 operation_text="unlock";
205 else
206 operation_text="ERROR ";
207
208 if (mode & CRYPTO_READ)
209 rw_text="r";
210 else if (mode & CRYPTO_WRITE)
211 rw_text="w";
212 else
213 rw_text="ERROR";
214
215 fprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\n",
216 CRYPTO_thread_id(), rw_text, operation_text,
217 CRYPTO_get_lock_name(type), file, line);
218 }
219#endif
220 if (locking_callback != NULL)
221 locking_callback(mode,type,file,line);
222 }
223
224int CRYPTO_add_lock(pointer,amount,type,file,line)
225int *pointer;
226int amount;
227int type;
228char *file;
229int line;
230 {
231 int ret;
232
233 if (add_lock_callback != NULL)
234 {
235#ifdef LOCK_DEBUG
236 int before= *pointer;
237#endif
238
239 ret=add_lock_callback(pointer,amount,type,file,line);
240#ifdef LOCK_DEBUG
241 fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
242 CRYPTO_thread_id(),
243 before,amount,ret,
244 CRYPTO_get_lock_name(type),
245 file,line);
246#endif
247 *pointer=ret;
248 }
249 else
250 {
251 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);
252
253 ret= *pointer+amount;
254#ifdef LOCK_DEBUG
255 fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
256 CRYPTO_thread_id(),
257 *pointer,amount,ret,
258 CRYPTO_get_lock_name(type),
259 file,line);
260#endif
261 *pointer=ret;
262 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);
263 }
264 return(ret);
265 }
266
267char *CRYPTO_get_lock_name(type)
268int type;
269 {
270 if (type < 0)
271 return("ERROR");
272 else if (type < CRYPTO_NUM_LOCKS)
273 return(lock_names[type]);
274 else if (type-CRYPTO_NUM_LOCKS >= sk_num(app_locks))
275 return("ERROR");
276 else
277 return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS));
278 }
279
280#ifdef _DLL
281#ifdef WIN32
282
283/* All we really need to do is remove the 'error' state when a thread
284 * detaches */
285
286BOOL WINAPI DLLEntryPoint(hinstDLL,fdwReason,lpvReserved)
287HINSTANCE hinstDLL;
288DWORD fdwReason;
289LPVOID lpvReserved;
290 {
291 switch(fdwReason)
292 {
293 case DLL_PROCESS_ATTACH:
294 break;
295 case DLL_THREAD_ATTACH:
296 break;
297 case DLL_THREAD_DETACH:
298 ERR_remove_state(0);
299 break;
300 case DLL_PROCESS_DETACH:
301 break;
302 }
303 return(TRUE);
304 }
305#endif
306
307#endif
diff --git a/src/lib/libcrypto/cryptlib.h b/src/lib/libcrypto/cryptlib.h
new file mode 100644
index 0000000000..32757c9efb
--- /dev/null
+++ b/src/lib/libcrypto/cryptlib.h
@@ -0,0 +1,100 @@
1/* crypto/cryptlib.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_CRYPTLIB_H
60#define HEADER_CRYPTLIB_H
61
62#include <stdlib.h>
63#include <string.h>
64
65#ifdef __cplusplus
66extern "C" {
67#endif
68
69/* #ifdef FLAT_INC */
70
71#include "e_os.h"
72#include "crypto.h"
73#include "buffer.h"
74#include "bio.h"
75#include "err.h"
76
77/*
78#else
79
80#include "../e_os.h"
81#include "crypto.h"
82#include "buffer/buffer.h"
83#include "bio/bio.h"
84#include "err/err.h"
85#endif
86*/
87
88#define X509_CERT_AREA "/etc/ssl"
89#define X509_CERT_DIR "/etc/ssl/certs"
90#define X509_CERT_FILE "/etc/ssl/cert.pem"
91#define X509_PRIVATE_DIR "/etc/ssl/private"
92
93#define X509_CERT_DIR_EVP "SSL_CERT_DIR"
94#define X509_CERT_FILE_EVP "SSL_CERT_FILE"
95
96#ifdef __cplusplus
97}
98#endif
99
100#endif
diff --git a/src/lib/libcrypto/crypto.h b/src/lib/libcrypto/crypto.h
new file mode 100644
index 0000000000..0a38b5b87c
--- /dev/null
+++ b/src/lib/libcrypto/crypto.h
@@ -0,0 +1,319 @@
1/* crypto/crypto.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_CRYPTO_H
60#define HEADER_CRYPTO_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "stack.h"
67
68/* This is more to be used to check the correct DLL is being used
69 * in the MS world. */
70#define SSLEAY_VERSION_NUMBER 0x0902 /* Version 0.5.1c would be 0513 */
71
72#define SSLEAY_VERSION 0
73/* #define SSLEAY_OPTIONS 1 no longer supported */
74#define SSLEAY_CFLAGS 2
75#define SSLEAY_BUILT_ON 3
76
77/* When changing the CRYPTO_LOCK_* list, be sure to maintin the text lock
78 * names in cryptlib.c
79 */
80
81#define CRYPTO_LOCK_ERR 1
82#define CRYPTO_LOCK_ERR_HASH 2
83#define CRYPTO_LOCK_X509 3
84#define CRYPTO_LOCK_X509_INFO 4
85#define CRYPTO_LOCK_X509_PKEY 5
86#define CRYPTO_LOCK_X509_CRL 6
87#define CRYPTO_LOCK_X509_REQ 7
88#define CRYPTO_LOCK_DSA 8
89#define CRYPTO_LOCK_RSA 9
90#define CRYPTO_LOCK_EVP_PKEY 10
91#define CRYPTO_LOCK_X509_STORE 11
92#define CRYPTO_LOCK_SSL_CTX 12
93#define CRYPTO_LOCK_SSL_CERT 13
94#define CRYPTO_LOCK_SSL_SESSION 14
95#define CRYPTO_LOCK_SSL 15
96#define CRYPTO_LOCK_RAND 16
97#define CRYPTO_LOCK_MALLOC 17
98#define CRYPTO_LOCK_BIO 18
99#define CRYPTO_LOCK_BIO_GETHOSTBYNAME 19
100#define CRYPTO_LOCK_RSA_BLINDING 20
101#define CRYPTO_NUM_LOCKS 21
102
103#define CRYPTO_LOCK 1
104#define CRYPTO_UNLOCK 2
105#define CRYPTO_READ 4
106#define CRYPTO_WRITE 8
107
108#ifndef CRYPTO_w_lock
109#define CRYPTO_w_lock(type) \
110 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
111#define CRYPTO_w_unlock(type) \
112 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
113#define CRYPTO_r_lock(type) \
114 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ,type,__FILE__,__LINE__)
115#define CRYPTO_r_unlock(type) \
116 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ,type,__FILE__,__LINE__)
117#define CRYPTO_add(addr,amount,type) \
118 CRYPTO_add_lock(addr,amount,type,__FILE__,__LINE__)
119
120#endif
121
122/* The following can be used to detect memory leaks in the SSLeay library.
123 * It used, it turns on malloc checking */
124
125#define CRYPTO_MEM_CHECK_OFF 0x0
126#define CRYPTO_MEM_CHECK_ON 0x1
127
128/*
129typedef struct crypto_mem_st
130 {
131 char *(*malloc_func)();
132 char *(*realloc_func)();
133 void (*free_func)();
134 } CRYPTO_MEM_FUNC;
135*/
136
137/* predec of the BIO type */
138typedef struct bio_st BIO_dummy;
139
140typedef struct crypto_ex_data_st
141 {
142 STACK *sk;
143 int dummy; /* gcc is screwing up this data structure :-( */
144 } CRYPTO_EX_DATA;
145
146/* This stuff is basically class callback functions
147 * The current classes are SSL_CTX, SSL, SSL_SESION, and a few more */
148typedef struct crypto_ex_data_func_st
149 {
150 long argl; /* Arbitary long */
151 char *argp; /* Arbitary char * */
152 /* Called when a new object is created */
153 int (*new_func)(/*char *obj,
154 char *item,int index,long argl,char *argp*/);
155 /* Called when this object is free()ed */
156 void (*free_func)(/*char *obj,
157 char *item,int index,long argl,char *argp*/);
158
159 /* Called when we need to dup this one */
160 int (*dup_func)(/*char *obj_to,char *obj_from,
161 char **new,int index,long argl,char *argp*/);
162 } CRYPTO_EX_DATA_FUNCS;
163
164/* Per class, we have a STACK of CRYPTO_EX_DATA_FUNCS for each CRYPTO_EX_DATA
165 * entry.
166 */
167
168#define CRYPTO_EX_INDEX_BIO 0
169#define CRYPTO_EX_INDEX_SSL 1
170#define CRYPTO_EX_INDEX_SSL_CTX 2
171#define CRYPTO_EX_INDEX_SSL_SESSION 3
172#define CRYPTO_EX_INDEX_X509_STORE 4
173#define CRYPTO_EX_INDEX_X509_STORE_CTX 5
174
175/* Use this for win32 DLL's */
176#define CRYPTO_malloc_init() CRYPTO_set_mem_functions(\
177 (char *(*)())malloc,\
178 (char *(*)())realloc,\
179 (void (*)())free)
180
181#ifdef CRYPTO_MDEBUG
182#define Malloc(num) CRYPTO_dbg_malloc((int)num,__FILE__,__LINE__)
183#define Realloc(addr,num) \
184 CRYPTO_dbg_realloc((char *)addr,(int)num,__FILE__,__LINE__)
185#define Remalloc(addr,num) \
186 CRYPTO_dbg_remalloc((char **)addr,(int)num,__FILE__,__LINE__)
187#define FreeFunc CRYPTO_dbg_free
188#define Free(addr) CRYPTO_dbg_free((char *)(addr))
189#else
190#define Remalloc CRYPTO_remalloc
191#if defined(WIN32) || defined(MFUNC)
192#define Malloc CRYPTO_malloc
193#define Realloc(a,n) CRYPTO_realloc((char *)(a),(n))
194#define FreeFunc CRYPTO_free
195#define Free(addr) CRYPTO_free((char *)(addr))
196#else
197#define Malloc malloc
198#define Realloc realloc
199#define FreeFunc free
200#define Free(addr) free((char *)(addr))
201#endif /* WIN32 || MFUNC */
202#endif /* MDEBUG */
203
204/* Case insensiteve linking causes problems.... */
205#ifdef WIN16
206#define ERR_load_CRYPTO_strings ERR_load_CRYPTOlib_strings
207#endif
208
209#ifndef NOPROTO
210
211char *SSLeay_version(int type);
212unsigned long SSLeay(void);
213
214int CRYPTO_get_ex_new_index(int idx,STACK **sk,long argl,char *argp,
215 int (*new_func)(),int (*dup_func)(),void (*free_func)());
216int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad,int idx,char *val);
217char *CRYPTO_get_ex_data(CRYPTO_EX_DATA *ad,int idx);
218int CRYPTO_dup_ex_data(STACK *meth,CRYPTO_EX_DATA *from,CRYPTO_EX_DATA *to);
219void CRYPTO_free_ex_data(STACK *meth,char *obj,CRYPTO_EX_DATA *ad);
220void CRYPTO_new_ex_data(STACK *meth, char *obj, CRYPTO_EX_DATA *ad);
221
222int CRYPTO_mem_ctrl(int mode);
223int CRYPTO_get_new_lockid(char *name);
224void CRYPTO_lock(int mode, int type,char *file,int line);
225void CRYPTO_set_locking_callback(void (*func)(int mode,int type,char *file,
226 int line));
227void (*CRYPTO_get_locking_callback(void))(int mode,int type,char *file,
228 int line);
229void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,
230 int type,char *file, int line));
231int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,
232 int type,char *file,int line);
233void CRYPTO_set_id_callback(unsigned long (*func)(void));
234unsigned long (*CRYPTO_get_id_callback(void))(void);
235unsigned long CRYPTO_thread_id(void);
236char *CRYPTO_get_lock_name(int type);
237int CRYPTO_add_lock(int *pointer,int amount,int type, char *file,int line);
238
239void CRYPTO_set_mem_functions(char *(*m)(),char *(*r)(), void (*free_func)());
240void CRYPTO_get_mem_functions(char *(**m)(),char *(**r)(), void (**f)());
241
242char *CRYPTO_malloc(int num);
243char *CRYPTO_realloc(char *addr,int num);
244void CRYPTO_free(char *);
245char *CRYPTO_remalloc(char *addr,int num);
246
247char *CRYPTO_dbg_malloc(int num,char *file,int line);
248char *CRYPTO_dbg_realloc(char *addr,int num,char *file,int line);
249void CRYPTO_dbg_free(char *);
250char *CRYPTO_dbg_remalloc(char *addr,int num,char *file,int line);
251#ifndef NO_FP_API
252void CRYPTO_mem_leaks_fp(FILE *);
253#endif
254void CRYPTO_mem_leaks(struct bio_st *bio);
255/* unsigned long order, char *file, int line, int num_bytes, char *addr */
256void CRYPTO_mem_leaks_cb(void (*cb)());
257
258void ERR_load_CRYPTO_strings(void );
259
260#else
261
262int CRYPTO_get_ex_new_index();
263int CRYPTO_set_ex_data();
264char *CRYPTO_get_ex_data();
265int CRYPTO_dup_ex_data();
266void CRYPTO_free_ex_data();
267void CRYPTO_new_ex_data();
268
269int CRYPTO_mem_ctrl();
270char *SSLeay_version();
271unsigned long SSLeay();
272
273int CRYPTO_get_new_lockid();
274void CRYPTO_lock();
275void CRYPTO_set_locking_callback();
276void (*CRYPTO_get_locking_callback())();
277void CRYPTO_set_add_lock_callback();
278int (*CRYPTO_get_add_lock_callback())();
279void CRYPTO_set_id_callback();
280unsigned long (*CRYPTO_get_id_callback())();
281unsigned long CRYPTO_thread_id();
282char *CRYPTO_get_lock_name();
283int CRYPTO_add_lock();
284
285void CRYPTO_set_mem_functions();
286void CRYPTO_get_mem_functions();
287char *CRYPTO_malloc();
288char *CRYPTO_realloc();
289void CRYPTO_free();
290char *CRYPTO_remalloc();
291char *CRYPTO_dbg_remalloc();
292char *CRYPTO_dbg_malloc();
293char *CRYPTO_dbg_realloc();
294void CRYPTO_dbg_free();
295#ifndef NO_FP_API
296void CRYPTO_mem_leaks_fp();
297#endif
298void CRYPTO_mem_leaks();
299void CRYPTO_mem_leaks_cb();
300
301void ERR_load_CRYPTO_strings();
302
303#endif
304
305/* BEGIN ERROR CODES */
306/* Error codes for the CRYPTO functions. */
307
308/* Function codes. */
309#define CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX 100
310#define CRYPTO_F_CRYPTO_GET_NEW_LOCKID 101
311#define CRYPTO_F_CRYPTO_SET_EX_DATA 102
312
313/* Reason codes. */
314
315#ifdef __cplusplus
316}
317#endif
318#endif
319
diff --git a/src/lib/libcrypto/cversion.c b/src/lib/libcrypto/cversion.c
new file mode 100644
index 0000000000..4e823be52f
--- /dev/null
+++ b/src/lib/libcrypto/cversion.c
@@ -0,0 +1,99 @@
1/* crypto/cversion.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include "cryptlib.h"
62#include "crypto.h"
63#include "date.h"
64
65char *SSLeay_version(t)
66int t;
67 {
68 if (t == SSLEAY_VERSION)
69 return("SSLeay 0.9.0b 29-Jun-1998");
70 if (t == SSLEAY_BUILT_ON)
71 {
72#ifdef DATE
73 static char buf[sizeof(DATE)+10];
74
75 sprintf(buf,"built on %s",DATE);
76 return(buf);
77#else
78 return("build date not available");
79#endif
80 }
81 if (t == SSLEAY_CFLAGS)
82 {
83#ifdef CFLAGS
84 static char buf[sizeof(CFLAGS)+10];
85
86 sprintf(buf,"C flags:%s",CFLAGS);
87 return(buf);
88#else
89 return("C flags not available");
90#endif
91 }
92 return("not available");
93 }
94
95unsigned long SSLeay()
96 {
97 return(SSLEAY_VERSION_NUMBER);
98 }
99
diff --git a/src/lib/libcrypto/des/COPYRIGHT b/src/lib/libcrypto/des/COPYRIGHT
new file mode 100644
index 0000000000..5469e1e469
--- /dev/null
+++ b/src/lib/libcrypto/des/COPYRIGHT
@@ -0,0 +1,50 @@
1Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
2All rights reserved.
3
4This package is an DES implementation written by Eric Young (eay@cryptsoft.com).
5The implementation was written so as to conform with MIT's libdes.
6
7This library is free for commercial and non-commercial use as long as
8the following conditions are aheared to. The following conditions
9apply to all code found in this distribution.
10
11Copyright remains Eric Young's, and as such any Copyright notices in
12the code are not to be removed.
13If this package is used in a product, Eric Young should be given attribution
14as the author of that the SSL library. This can be in the form of a textual
15message at program startup or in documentation (online or textual) provided
16with the package.
17
18Redistribution and use in source and binary forms, with or without
19modification, are permitted provided that the following conditions
20are met:
211. Redistributions of source code must retain the copyright
22 notice, this list of conditions and the following disclaimer.
232. Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
263. All advertising materials mentioning features or use of this software
27 must display the following acknowledgement:
28 This product includes software developed by Eric Young (eay@cryptsoft.com)
29
30THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
31ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40SUCH DAMAGE.
41
42The license and distribution terms for any publically available version or
43derivative of this code cannot be changed. i.e. this code cannot simply be
44copied and put under another distrubution license
45[including the GNU Public License.]
46
47The reason behind this being stated in this direct manner is past
48experience in code simply being copied and the attribution removed
49from it and then being distributed as part of other packages. This
50implementation was a non-trivial and unpaid effort.
diff --git a/src/lib/libcrypto/des/asm/crypt586.pl b/src/lib/libcrypto/des/asm/crypt586.pl
new file mode 100644
index 0000000000..297e38dec8
--- /dev/null
+++ b/src/lib/libcrypto/des/asm/crypt586.pl
@@ -0,0 +1,204 @@
1#!/usr/bin/perl
2#
3# The inner loop instruction sequence and the IP/FP modifications are from
4# Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
5# I've added the stuff needed for crypt() but I've not worried about making
6# things perfect.
7#
8
9push(@INC,"perlasm","../../perlasm");
10require "x86asm.pl";
11
12&asm_init($ARGV[0],"crypt586.pl");
13
14$L="edi";
15$R="esi";
16
17&external_label("des_SPtrans");
18&fcrypt_body("fcrypt_body");
19&asm_finish();
20
21sub fcrypt_body
22 {
23 local($name,$do_ip)=@_;
24
25 &function_begin($name,"EXTRN _des_SPtrans:DWORD");
26
27 &comment("");
28 &comment("Load the 2 words");
29 $ks="ebp";
30
31 &xor( $L, $L);
32 &xor( $R, $R);
33 &mov($ks,&wparam(1));
34
35 &push(25); # add a variable
36
37 &set_label("start");
38 for ($i=0; $i<16; $i+=2)
39 {
40 &comment("");
41 &comment("Round $i");
42 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
43
44 &comment("");
45 &comment("Round ".sprintf("%d",$i+1));
46 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
47 }
48 &mov("ebx", &swtmp(0));
49 &mov("eax", $L);
50 &dec("ebx");
51 &mov($L, $R);
52 &mov($R, "eax");
53 &mov(&swtmp(0), "ebx");
54 &jnz(&label("start"));
55
56 &comment("");
57 &comment("FP");
58 &mov("edx",&wparam(0));
59
60 &FP_new($R,$L,"eax",3);
61 &mov(&DWP(0,"edx","",0),"eax");
62 &mov(&DWP(4,"edx","",0),$L);
63
64 &pop("ecx"); # remove variable
65
66 &function_end($name);
67 }
68
69sub D_ENCRYPT
70 {
71 local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
72
73 &mov( $u, &wparam(2)); # 2
74 &mov( $t, $R);
75 &shr( $t, 16); # 1
76 &mov( $tmp2, &wparam(3)); # 2
77 &xor( $t, $R); # 1
78
79 &and( $u, $t); # 2
80 &and( $t, $tmp2); # 2
81
82 &mov( $tmp1, $u);
83 &shl( $tmp1, 16); # 1
84 &mov( $tmp2, $t);
85 &shl( $tmp2, 16); # 1
86 &xor( $u, $tmp1); # 2
87 &xor( $t, $tmp2); # 2
88 &mov( $tmp1, &DWP(&n2a($S*4),$ks,"",0)); # 2
89 &xor( $u, $tmp1);
90 &mov( $tmp2, &DWP(&n2a(($S+1)*4),$ks,"",0)); # 2
91 &xor( $u, $R);
92 &xor( $t, $R);
93 &xor( $t, $tmp2);
94
95 &and( $u, "0xfcfcfcfc" ); # 2
96 &xor( $tmp1, $tmp1); # 1
97 &and( $t, "0xcfcfcfcf" ); # 2
98 &xor( $tmp2, $tmp2);
99 &movb( &LB($tmp1), &LB($u) );
100 &movb( &LB($tmp2), &HB($u) );
101 &rotr( $t, 4 );
102 &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
103 &movb( &LB($tmp1), &LB($t) );
104 &xor( $L, $ks);
105 &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
106 &xor( $L, $ks);
107 &movb( &LB($tmp2), &HB($t) );
108 &shr( $u, 16);
109 &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
110 &xor( $L, $ks);
111 &movb( &LB($tmp1), &HB($u) );
112 &shr( $t, 16);
113 &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
114 &xor( $L, $ks);
115 &mov( $ks, &wparam(1));
116 &movb( &LB($tmp2), &HB($t) );
117 &and( $u, "0xff" );
118 &and( $t, "0xff" );
119 &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
120 &xor( $L, $tmp1);
121 &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
122 &xor( $L, $tmp1);
123 &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
124 &xor( $L, $tmp1);
125 &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
126 &xor( $L, $tmp1);
127 }
128
129sub n2a
130 {
131 sprintf("%d",$_[0]);
132 }
133
134# now has a side affect of rotating $a by $shift
135sub R_PERM_OP
136 {
137 local($a,$b,$tt,$shift,$mask,$last)=@_;
138
139 &rotl( $a, $shift ) if ($shift != 0);
140 &mov( $tt, $a );
141 &xor( $a, $b );
142 &and( $a, $mask );
143 if ($notlast eq $b)
144 {
145 &xor( $b, $a );
146 &xor( $tt, $a );
147 }
148 else
149 {
150 &xor( $tt, $a );
151 &xor( $b, $a );
152 }
153 &comment("");
154 }
155
156sub IP_new
157 {
158 local($l,$r,$tt,$lr)=@_;
159
160 &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
161 &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
162 &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
163 &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
164 &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
165
166 if ($lr != 3)
167 {
168 if (($lr-3) < 0)
169 { &rotr($tt, 3-$lr); }
170 else { &rotl($tt, $lr-3); }
171 }
172 if ($lr != 2)
173 {
174 if (($lr-2) < 0)
175 { &rotr($r, 2-$lr); }
176 else { &rotl($r, $lr-2); }
177 }
178 }
179
180sub FP_new
181 {
182 local($l,$r,$tt,$lr)=@_;
183
184 if ($lr != 2)
185 {
186 if (($lr-2) < 0)
187 { &rotl($r, 2-$lr); }
188 else { &rotr($r, $lr-2); }
189 }
190 if ($lr != 3)
191 {
192 if (($lr-3) < 0)
193 { &rotl($l, 3-$lr); }
194 else { &rotr($l, $lr-3); }
195 }
196
197 &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
198 &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
199 &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
200 &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
201 &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
202 &rotr($tt , 4);
203 }
204
diff --git a/src/lib/libcrypto/des/asm/des-586.pl b/src/lib/libcrypto/des/asm/des-586.pl
new file mode 100644
index 0000000000..7f2e09fa7a
--- /dev/null
+++ b/src/lib/libcrypto/des/asm/des-586.pl
@@ -0,0 +1,251 @@
1#!/usr/bin/perl
2#
3# The inner loop instruction sequence and the IP/FP modifications are from
4# Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
5#
6
7push(@INC,"perlasm","../../perlasm");
8require "x86asm.pl";
9require "cbc.pl";
10require "desboth.pl";
11
12# base code is in microsft
13# op dest, source
14# format.
15#
16
17&asm_init($ARGV[0],"des-586.pl");
18
19$L="edi";
20$R="esi";
21
22&external_label("des_SPtrans");
23&des_encrypt("des_encrypt",1);
24&des_encrypt("des_encrypt2",0);
25&des_encrypt3("des_encrypt3",1);
26&des_encrypt3("des_decrypt3",0);
27&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
28&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
29
30&asm_finish();
31
32sub des_encrypt
33 {
34 local($name,$do_ip)=@_;
35
36 &function_begin_B($name,"EXTRN _des_SPtrans:DWORD");
37
38 &push("esi");
39 &push("edi");
40
41 &comment("");
42 &comment("Load the 2 words");
43 $ks="ebp";
44
45 if ($do_ip)
46 {
47 &mov($R,&wparam(0));
48 &xor( "ecx", "ecx" );
49
50 &push("ebx");
51 &push("ebp");
52
53 &mov("eax",&DWP(0,$R,"",0));
54 &mov("ebx",&wparam(2)); # get encrypt flag
55 &mov($L,&DWP(4,$R,"",0));
56 &comment("");
57 &comment("IP");
58 &IP_new("eax",$L,$R,3);
59 }
60 else
61 {
62 &mov("eax",&wparam(0));
63 &xor( "ecx", "ecx" );
64
65 &push("ebx");
66 &push("ebp");
67
68 &mov($R,&DWP(0,"eax","",0));
69 &mov("ebx",&wparam(2)); # get encrypt flag
70 &rotl($R,3);
71 &mov($L,&DWP(4,"eax","",0));
72 &rotl($L,3);
73 }
74
75 &mov( $ks, &wparam(1) );
76 &cmp("ebx","0");
77 &je(&label("start_decrypt"));
78
79 for ($i=0; $i<16; $i+=2)
80 {
81 &comment("");
82 &comment("Round $i");
83 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
84
85 &comment("");
86 &comment("Round ".sprintf("%d",$i+1));
87 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
88 }
89 &jmp(&label("end"));
90
91 &set_label("start_decrypt");
92
93 for ($i=15; $i>0; $i-=2)
94 {
95 &comment("");
96 &comment("Round $i");
97 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
98 &comment("");
99 &comment("Round ".sprintf("%d",$i-1));
100 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
101 }
102
103 &set_label("end");
104
105 if ($do_ip)
106 {
107 &comment("");
108 &comment("FP");
109 &mov("edx",&wparam(0));
110 &FP_new($L,$R,"eax",3);
111
112 &mov(&DWP(0,"edx","",0),"eax");
113 &mov(&DWP(4,"edx","",0),$R);
114 }
115 else
116 {
117 &comment("");
118 &comment("Fixup");
119 &rotr($L,3); # r
120 &mov("eax",&wparam(0));
121 &rotr($R,3); # l
122 &mov(&DWP(0,"eax","",0),$L);
123 &mov(&DWP(4,"eax","",0),$R);
124 }
125
126 &pop("ebp");
127 &pop("ebx");
128 &pop("edi");
129 &pop("esi");
130 &ret();
131
132 &function_end_B($name);
133 }
134
135sub D_ENCRYPT
136 {
137 local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
138
139 &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
140 &xor( $tmp1, $tmp1);
141 &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
142 &xor( $u, $R);
143 &xor( $t, $R);
144 &and( $u, "0xfcfcfcfc" );
145 &and( $t, "0xcfcfcfcf" );
146 &movb( &LB($tmp1), &LB($u) );
147 &movb( &LB($tmp2), &HB($u) );
148 &rotr( $t, 4 );
149 &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
150 &movb( &LB($tmp1), &LB($t) );
151 &xor( $L, $ks);
152 &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
153 &xor( $L, $ks); ######
154 &movb( &LB($tmp2), &HB($t) );
155 &shr( $u, 16);
156 &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
157 &xor( $L, $ks); ######
158 &movb( &LB($tmp1), &HB($u) );
159 &shr( $t, 16);
160 &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
161 &xor( $L, $ks);
162 &mov( $ks, &wparam(1) );
163 &movb( &LB($tmp2), &HB($t) );
164 &and( $u, "0xff" );
165 &and( $t, "0xff" );
166 &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
167 &xor( $L, $tmp1);
168 &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
169 &xor( $L, $tmp1);
170 &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
171 &xor( $L, $tmp1);
172 &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
173 &xor( $L, $tmp1);
174 }
175
176sub n2a
177 {
178 sprintf("%d",$_[0]);
179 }
180
181# now has a side affect of rotating $a by $shift
182sub R_PERM_OP
183 {
184 local($a,$b,$tt,$shift,$mask,$last)=@_;
185
186 &rotl( $a, $shift ) if ($shift != 0);
187 &mov( $tt, $a );
188 &xor( $a, $b );
189 &and( $a, $mask );
190 if (!$last eq $b)
191 {
192 &xor( $b, $a );
193 &xor( $tt, $a );
194 }
195 else
196 {
197 &xor( $tt, $a );
198 &xor( $b, $a );
199 }
200 &comment("");
201 }
202
203sub IP_new
204 {
205 local($l,$r,$tt,$lr)=@_;
206
207 &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
208 &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
209 &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
210 &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
211 &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
212
213 if ($lr != 3)
214 {
215 if (($lr-3) < 0)
216 { &rotr($tt, 3-$lr); }
217 else { &rotl($tt, $lr-3); }
218 }
219 if ($lr != 2)
220 {
221 if (($lr-2) < 0)
222 { &rotr($r, 2-$lr); }
223 else { &rotl($r, $lr-2); }
224 }
225 }
226
227sub FP_new
228 {
229 local($l,$r,$tt,$lr)=@_;
230
231 if ($lr != 2)
232 {
233 if (($lr-2) < 0)
234 { &rotl($r, 2-$lr); }
235 else { &rotr($r, $lr-2); }
236 }
237 if ($lr != 3)
238 {
239 if (($lr-3) < 0)
240 { &rotl($l, 3-$lr); }
241 else { &rotr($l, $lr-3); }
242 }
243
244 &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
245 &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
246 &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
247 &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
248 &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
249 &rotr($tt , 4);
250 }
251
diff --git a/src/lib/libcrypto/des/asm/desboth.pl b/src/lib/libcrypto/des/asm/desboth.pl
new file mode 100644
index 0000000000..8f939953a6
--- /dev/null
+++ b/src/lib/libcrypto/des/asm/desboth.pl
@@ -0,0 +1,79 @@
1#!/usr/bin/perl
2
3$L="edi";
4$R="esi";
5
6sub des_encrypt3
7 {
8 local($name,$enc)=@_;
9
10 &function_begin_B($name,"");
11 &push("ebx");
12 &mov("ebx",&wparam(0));
13
14 &push("ebp");
15 &push("esi");
16
17 &push("edi");
18
19 &comment("");
20 &comment("Load the data words");
21 &mov($L,&DWP(0,"ebx","",0));
22 &mov($R,&DWP(4,"ebx","",0));
23 &stack_push(3);
24
25 &comment("");
26 &comment("IP");
27 &IP_new($L,$R,"edx",0);
28
29 # put them back
30
31 if ($enc)
32 {
33 &mov(&DWP(4,"ebx","",0),$R);
34 &mov("eax",&wparam(1));
35 &mov(&DWP(0,"ebx","",0),"edx");
36 &mov("edi",&wparam(2));
37 &mov("esi",&wparam(3));
38 }
39 else
40 {
41 &mov(&DWP(4,"ebx","",0),$R);
42 &mov("esi",&wparam(1));
43 &mov(&DWP(0,"ebx","",0),"edx");
44 &mov("edi",&wparam(2));
45 &mov("eax",&wparam(3));
46 }
47 &mov(&swtmp(2), (($enc)?"1":"0"));
48 &mov(&swtmp(1), "eax");
49 &mov(&swtmp(0), "ebx");
50 &call("des_encrypt2");
51 &mov(&swtmp(2), (($enc)?"0":"1"));
52 &mov(&swtmp(1), "edi");
53 &mov(&swtmp(0), "ebx");
54 &call("des_encrypt2");
55 &mov(&swtmp(2), (($enc)?"1":"0"));
56 &mov(&swtmp(1), "esi");
57 &mov(&swtmp(0), "ebx");
58 &call("des_encrypt2");
59
60 &stack_pop(3);
61 &mov($L,&DWP(0,"ebx","",0));
62 &mov($R,&DWP(4,"ebx","",0));
63
64 &comment("");
65 &comment("FP");
66 &FP_new($L,$R,"eax",0);
67
68 &mov(&DWP(0,"ebx","",0),"eax");
69 &mov(&DWP(4,"ebx","",0),$R);
70
71 &pop("edi");
72 &pop("esi");
73 &pop("ebp");
74 &pop("ebx");
75 &ret();
76 &function_end_B($name);
77 }
78
79
diff --git a/src/lib/libcrypto/des/cbc_cksm.c b/src/lib/libcrypto/des/cbc_cksm.c
new file mode 100644
index 0000000000..edfdec8a0f
--- /dev/null
+++ b/src/lib/libcrypto/des/cbc_cksm.c
@@ -0,0 +1,103 @@
1/* crypto/des/cbc_cksm.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61DES_LONG des_cbc_cksum(input, output, length, schedule, ivec)
62des_cblock (*input);
63des_cblock (*output);
64long length;
65des_key_schedule schedule;
66des_cblock (*ivec);
67 {
68 register DES_LONG tout0,tout1,tin0,tin1;
69 register long l=length;
70 DES_LONG tin[2];
71 unsigned char *in,*out,*iv;
72
73 in=(unsigned char *)input;
74 out=(unsigned char *)output;
75 iv=(unsigned char *)ivec;
76
77 c2l(iv,tout0);
78 c2l(iv,tout1);
79 for (; l>0; l-=8)
80 {
81 if (l >= 8)
82 {
83 c2l(in,tin0);
84 c2l(in,tin1);
85 }
86 else
87 c2ln(in,tin0,tin1,l);
88
89 tin0^=tout0; tin[0]=tin0;
90 tin1^=tout1; tin[1]=tin1;
91 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
92 /* fix 15/10/91 eay - thanks to keithr@sco.COM */
93 tout0=tin[0];
94 tout1=tin[1];
95 }
96 if (out != NULL)
97 {
98 l2c(tout0,out);
99 l2c(tout1,out);
100 }
101 tout0=tin0=tin1=tin[0]=tin[1]=0;
102 return(tout1);
103 }
diff --git a/src/lib/libcrypto/des/cbc_enc.c b/src/lib/libcrypto/des/cbc_enc.c
new file mode 100644
index 0000000000..a84a53633c
--- /dev/null
+++ b/src/lib/libcrypto/des/cbc_enc.c
@@ -0,0 +1,135 @@
1/* crypto/des/cbc_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61void des_cbc_encrypt(input, output, length, schedule, ivec, enc)
62des_cblock (*input);
63des_cblock (*output);
64long length;
65des_key_schedule schedule;
66des_cblock (*ivec);
67int enc;
68 {
69 register DES_LONG tin0,tin1;
70 register DES_LONG tout0,tout1,xor0,xor1;
71 register unsigned char *in,*out;
72 register long l=length;
73 DES_LONG tin[2];
74 unsigned char *iv;
75
76 in=(unsigned char *)input;
77 out=(unsigned char *)output;
78 iv=(unsigned char *)ivec;
79
80 if (enc)
81 {
82 c2l(iv,tout0);
83 c2l(iv,tout1);
84 for (l-=8; l>=0; l-=8)
85 {
86 c2l(in,tin0);
87 c2l(in,tin1);
88 tin0^=tout0; tin[0]=tin0;
89 tin1^=tout1; tin[1]=tin1;
90 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
91 tout0=tin[0]; l2c(tout0,out);
92 tout1=tin[1]; l2c(tout1,out);
93 }
94 if (l != -8)
95 {
96 c2ln(in,tin0,tin1,l+8);
97 tin0^=tout0; tin[0]=tin0;
98 tin1^=tout1; tin[1]=tin1;
99 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
100 tout0=tin[0]; l2c(tout0,out);
101 tout1=tin[1]; l2c(tout1,out);
102 }
103 }
104 else
105 {
106 c2l(iv,xor0);
107 c2l(iv,xor1);
108 for (l-=8; l>=0; l-=8)
109 {
110 c2l(in,tin0); tin[0]=tin0;
111 c2l(in,tin1); tin[1]=tin1;
112 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
113 tout0=tin[0]^xor0;
114 tout1=tin[1]^xor1;
115 l2c(tout0,out);
116 l2c(tout1,out);
117 xor0=tin0;
118 xor1=tin1;
119 }
120 if (l != -8)
121 {
122 c2l(in,tin0); tin[0]=tin0;
123 c2l(in,tin1); tin[1]=tin1;
124 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
125 tout0=tin[0]^xor0;
126 tout1=tin[1]^xor1;
127 l2cn(tout0,tout1,out,l+8);
128 /* xor0=tin0;
129 xor1=tin1; */
130 }
131 }
132 tin0=tin1=tout0=tout1=xor0=xor1=0;
133 tin[0]=tin[1]=0;
134 }
135
diff --git a/src/lib/libcrypto/des/cfb64ede.c b/src/lib/libcrypto/des/cfb64ede.c
new file mode 100644
index 0000000000..80b8a9eaaa
--- /dev/null
+++ b/src/lib/libcrypto/des/cfb64ede.c
@@ -0,0 +1,151 @@
1/* crypto/des/cfb64ede.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61/* The input and output encrypted as though 64bit cfb mode is being
62 * used. The extra state information to record how much of the
63 * 64bit block we have used is contained in *num;
64 */
65
66void des_ede3_cfb64_encrypt(in, out, length, ks1,ks2,ks3, ivec, num, enc)
67unsigned char *in;
68unsigned char *out;
69long length;
70des_key_schedule ks1,ks2,ks3;
71des_cblock (*ivec);
72int *num;
73int enc;
74 {
75 register DES_LONG v0,v1;
76 register long l=length;
77 register int n= *num;
78 DES_LONG ti[2];
79 unsigned char *iv,c,cc;
80
81 iv=(unsigned char *)ivec;
82 if (enc)
83 {
84 while (l--)
85 {
86 if (n == 0)
87 {
88 c2l(iv,v0);
89 c2l(iv,v1);
90
91 ti[0]=v0;
92 ti[1]=v1;
93 des_encrypt3((DES_LONG *)ti,ks1,ks2,ks3);
94 v0=ti[0];
95 v1=ti[1];
96
97 iv=(unsigned char *)ivec;
98 l2c(v0,iv);
99 l2c(v1,iv);
100 iv=(unsigned char *)ivec;
101 }
102 c= *(in++)^iv[n];
103 *(out++)=c;
104 iv[n]=c;
105 n=(n+1)&0x07;
106 }
107 }
108 else
109 {
110 while (l--)
111 {
112 if (n == 0)
113 {
114 c2l(iv,v0);
115 c2l(iv,v1);
116
117 ti[0]=v0;
118 ti[1]=v1;
119 des_encrypt3((DES_LONG *)ti,ks1,ks2,ks3);
120 v0=ti[0];
121 v1=ti[1];
122
123 iv=(unsigned char *)ivec;
124 l2c(v0,iv);
125 l2c(v1,iv);
126 iv=(unsigned char *)ivec;
127 }
128 cc= *(in++);
129 c=iv[n];
130 iv[n]=cc;
131 *(out++)=c^cc;
132 n=(n+1)&0x07;
133 }
134 }
135 v0=v1=ti[0]=ti[1]=c=cc=0;
136 *num=n;
137 }
138
139#ifdef undef /* MACRO */
140void des_ede2_cfb64_encrypt(in, out, length, ks1,ks2, ivec, num, enc)
141unsigned char *in;
142unsigned char *out;
143long length;
144des_key_schedule ks1,ks2;
145des_cblock (*ivec);
146int *num;
147int enc;
148 {
149 des_ede3_cfb64_encrypt(in,out,length,ks1,ks2,ks1,ivec,num,enc);
150 }
151#endif
diff --git a/src/lib/libcrypto/des/cfb64enc.c b/src/lib/libcrypto/des/cfb64enc.c
new file mode 100644
index 0000000000..403da479df
--- /dev/null
+++ b/src/lib/libcrypto/des/cfb64enc.c
@@ -0,0 +1,128 @@
1/* crypto/des/cfb64enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61/* The input and output encrypted as though 64bit cfb mode is being
62 * used. The extra state information to record how much of the
63 * 64bit block we have used is contained in *num;
64 */
65
66void des_cfb64_encrypt(in, out, length, schedule, ivec, num, enc)
67unsigned char *in;
68unsigned char *out;
69long length;
70des_key_schedule schedule;
71des_cblock (*ivec);
72int *num;
73int enc;
74 {
75 register DES_LONG v0,v1;
76 register long l=length;
77 register int n= *num;
78 DES_LONG ti[2];
79 unsigned char *iv,c,cc;
80
81 iv=(unsigned char *)ivec;
82 if (enc)
83 {
84 while (l--)
85 {
86 if (n == 0)
87 {
88 c2l(iv,v0); ti[0]=v0;
89 c2l(iv,v1); ti[1]=v1;
90 des_encrypt((DES_LONG *)ti,
91 schedule,DES_ENCRYPT);
92 iv=(unsigned char *)ivec;
93 v0=ti[0]; l2c(v0,iv);
94 v0=ti[1]; l2c(v0,iv);
95 iv=(unsigned char *)ivec;
96 }
97 c= *(in++)^iv[n];
98 *(out++)=c;
99 iv[n]=c;
100 n=(n+1)&0x07;
101 }
102 }
103 else
104 {
105 while (l--)
106 {
107 if (n == 0)
108 {
109 c2l(iv,v0); ti[0]=v0;
110 c2l(iv,v1); ti[1]=v1;
111 des_encrypt((DES_LONG *)ti,
112 schedule,DES_ENCRYPT);
113 iv=(unsigned char *)ivec;
114 v0=ti[0]; l2c(v0,iv);
115 v0=ti[1]; l2c(v0,iv);
116 iv=(unsigned char *)ivec;
117 }
118 cc= *(in++);
119 c=iv[n];
120 iv[n]=cc;
121 *(out++)=c^cc;
122 n=(n+1)&0x07;
123 }
124 }
125 v0=v1=ti[0]=ti[1]=c=cc=0;
126 *num=n;
127 }
128
diff --git a/src/lib/libcrypto/des/cfb_enc.c b/src/lib/libcrypto/des/cfb_enc.c
new file mode 100644
index 0000000000..342e785691
--- /dev/null
+++ b/src/lib/libcrypto/des/cfb_enc.c
@@ -0,0 +1,171 @@
1/* crypto/des/cfb_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61/* The input and output are loaded in multiples of 8 bits.
62 * What this means is that if you hame numbits=12 and length=2
63 * the first 12 bits will be retrieved from the first byte and half
64 * the second. The second 12 bits will come from the 3rd and half the 4th
65 * byte.
66 */
67void des_cfb_encrypt(in, out, numbits, length, schedule, ivec, enc)
68unsigned char *in;
69unsigned char *out;
70int numbits;
71long length;
72des_key_schedule schedule;
73des_cblock (*ivec);
74int enc;
75 {
76 register DES_LONG d0,d1,v0,v1,n=(numbits+7)/8;
77 register DES_LONG mask0,mask1;
78 register unsigned long l=length;
79 register int num=numbits;
80 DES_LONG ti[2];
81 unsigned char *iv;
82
83 if (num > 64) return;
84 if (num > 32)
85 {
86 mask0=0xffffffffL;
87 if (num == 64)
88 mask1=mask0;
89 else mask1=(1L<<(num-32))-1;
90 }
91 else
92 {
93 if (num == 32)
94 mask0=0xffffffffL;
95 else mask0=(1L<<num)-1;
96 mask1=0x00000000L;
97 }
98
99 iv=(unsigned char *)ivec;
100 c2l(iv,v0);
101 c2l(iv,v1);
102 if (enc)
103 {
104 while (l >= n)
105 {
106 l-=n;
107 ti[0]=v0;
108 ti[1]=v1;
109 des_encrypt((DES_LONG *)ti,schedule,DES_ENCRYPT);
110 c2ln(in,d0,d1,n);
111 in+=n;
112 d0=(d0^ti[0])&mask0;
113 d1=(d1^ti[1])&mask1;
114 l2cn(d0,d1,out,n);
115 out+=n;
116 /* 30-08-94 - eay - changed because l>>32 and
117 * l<<32 are bad under gcc :-( */
118 if (num == 32)
119 { v0=v1; v1=d0; }
120 else if (num == 64)
121 { v0=d0; v1=d1; }
122 else if (num > 32) /* && num != 64 */
123 {
124 v0=((v1>>(num-32))|(d0<<(64-num)))&0xffffffffL;
125 v1=((d0>>(num-32))|(d1<<(64-num)))&0xffffffffL;
126 }
127 else /* num < 32 */
128 {
129 v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL;
130 v1=((v1>>num)|(d0<<(32-num)))&0xffffffffL;
131 }
132 }
133 }
134 else
135 {
136 while (l >= n)
137 {
138 l-=n;
139 ti[0]=v0;
140 ti[1]=v1;
141 des_encrypt((DES_LONG *)ti,schedule,DES_ENCRYPT);
142 c2ln(in,d0,d1,n);
143 in+=n;
144 /* 30-08-94 - eay - changed because l>>32 and
145 * l<<32 are bad under gcc :-( */
146 if (num == 32)
147 { v0=v1; v1=d0; }
148 else if (num == 64)
149 { v0=d0; v1=d1; }
150 else if (num > 32) /* && num != 64 */
151 {
152 v0=((v1>>(num-32))|(d0<<(64-num)))&0xffffffffL;
153 v1=((d0>>(num-32))|(d1<<(64-num)))&0xffffffffL;
154 }
155 else /* num < 32 */
156 {
157 v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL;
158 v1=((v1>>num)|(d0<<(32-num)))&0xffffffffL;
159 }
160 d0=(d0^ti[0])&mask0;
161 d1=(d1^ti[1])&mask1;
162 l2cn(d0,d1,out,n);
163 out+=n;
164 }
165 }
166 iv=(unsigned char *)ivec;
167 l2c(v0,iv);
168 l2c(v1,iv);
169 v0=v1=d0=d1=ti[0]=ti[1]=0;
170 }
171
diff --git a/src/lib/libcrypto/des/des_enc.c b/src/lib/libcrypto/des/des_enc.c
new file mode 100644
index 0000000000..e4db09299e
--- /dev/null
+++ b/src/lib/libcrypto/des/des_enc.c
@@ -0,0 +1,502 @@
1/* crypto/des/des_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61void des_encrypt(data, ks, enc)
62DES_LONG *data;
63des_key_schedule ks;
64int enc;
65 {
66 register DES_LONG l,r,t,u;
67#ifdef DES_PTR
68 register unsigned char *des_SP=(unsigned char *)des_SPtrans;
69#endif
70#ifndef DES_UNROLL
71 register int i;
72#endif
73 register DES_LONG *s;
74
75 r=data[0];
76 l=data[1];
77
78 IP(r,l);
79 /* Things have been modified so that the initial rotate is
80 * done outside the loop. This required the
81 * des_SPtrans values in sp.h to be rotated 1 bit to the right.
82 * One perl script later and things have a 5% speed up on a sparc2.
83 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
84 * for pointing this out. */
85 /* clear the top bits on machines with 8byte longs */
86 /* shift left by 2 */
87 r=ROTATE(r,29)&0xffffffffL;
88 l=ROTATE(l,29)&0xffffffffL;
89
90 s=(DES_LONG *)ks;
91 /* I don't know if it is worth the effort of loop unrolling the
92 * inner loop */
93 if (enc)
94 {
95#ifdef DES_UNROLL
96 D_ENCRYPT(l,r, 0); /* 1 */
97 D_ENCRYPT(r,l, 2); /* 2 */
98 D_ENCRYPT(l,r, 4); /* 3 */
99 D_ENCRYPT(r,l, 6); /* 4 */
100 D_ENCRYPT(l,r, 8); /* 5 */
101 D_ENCRYPT(r,l,10); /* 6 */
102 D_ENCRYPT(l,r,12); /* 7 */
103 D_ENCRYPT(r,l,14); /* 8 */
104 D_ENCRYPT(l,r,16); /* 9 */
105 D_ENCRYPT(r,l,18); /* 10 */
106 D_ENCRYPT(l,r,20); /* 11 */
107 D_ENCRYPT(r,l,22); /* 12 */
108 D_ENCRYPT(l,r,24); /* 13 */
109 D_ENCRYPT(r,l,26); /* 14 */
110 D_ENCRYPT(l,r,28); /* 15 */
111 D_ENCRYPT(r,l,30); /* 16 */
112#else
113 for (i=0; i<32; i+=8)
114 {
115 D_ENCRYPT(l,r,i+0); /* 1 */
116 D_ENCRYPT(r,l,i+2); /* 2 */
117 D_ENCRYPT(l,r,i+4); /* 3 */
118 D_ENCRYPT(r,l,i+6); /* 4 */
119 }
120#endif
121 }
122 else
123 {
124#ifdef DES_UNROLL
125 D_ENCRYPT(l,r,30); /* 16 */
126 D_ENCRYPT(r,l,28); /* 15 */
127 D_ENCRYPT(l,r,26); /* 14 */
128 D_ENCRYPT(r,l,24); /* 13 */
129 D_ENCRYPT(l,r,22); /* 12 */
130 D_ENCRYPT(r,l,20); /* 11 */
131 D_ENCRYPT(l,r,18); /* 10 */
132 D_ENCRYPT(r,l,16); /* 9 */
133 D_ENCRYPT(l,r,14); /* 8 */
134 D_ENCRYPT(r,l,12); /* 7 */
135 D_ENCRYPT(l,r,10); /* 6 */
136 D_ENCRYPT(r,l, 8); /* 5 */
137 D_ENCRYPT(l,r, 6); /* 4 */
138 D_ENCRYPT(r,l, 4); /* 3 */
139 D_ENCRYPT(l,r, 2); /* 2 */
140 D_ENCRYPT(r,l, 0); /* 1 */
141#else
142 for (i=30; i>0; i-=8)
143 {
144 D_ENCRYPT(l,r,i-0); /* 16 */
145 D_ENCRYPT(r,l,i-2); /* 15 */
146 D_ENCRYPT(l,r,i-4); /* 14 */
147 D_ENCRYPT(r,l,i-6); /* 13 */
148 }
149#endif
150 }
151
152 /* rotate and clear the top bits on machines with 8byte longs */
153 l=ROTATE(l,3)&0xffffffffL;
154 r=ROTATE(r,3)&0xffffffffL;
155
156 FP(r,l);
157 data[0]=l;
158 data[1]=r;
159 l=r=t=u=0;
160 }
161
162void des_encrypt2(data, ks, enc)
163DES_LONG *data;
164des_key_schedule ks;
165int enc;
166 {
167 register DES_LONG l,r,t,u;
168#ifdef DES_PTR
169 register unsigned char *des_SP=(unsigned char *)des_SPtrans;
170#endif
171#ifndef DES_UNROLL
172 register int i;
173#endif
174 register DES_LONG *s;
175
176 r=data[0];
177 l=data[1];
178
179 /* Things have been modified so that the initial rotate is
180 * done outside the loop. This required the
181 * des_SPtrans values in sp.h to be rotated 1 bit to the right.
182 * One perl script later and things have a 5% speed up on a sparc2.
183 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
184 * for pointing this out. */
185 /* clear the top bits on machines with 8byte longs */
186 r=ROTATE(r,29)&0xffffffffL;
187 l=ROTATE(l,29)&0xffffffffL;
188
189 s=(DES_LONG *)ks;
190 /* I don't know if it is worth the effort of loop unrolling the
191 * inner loop */
192 if (enc)
193 {
194#ifdef DES_UNROLL
195 D_ENCRYPT(l,r, 0); /* 1 */
196 D_ENCRYPT(r,l, 2); /* 2 */
197 D_ENCRYPT(l,r, 4); /* 3 */
198 D_ENCRYPT(r,l, 6); /* 4 */
199 D_ENCRYPT(l,r, 8); /* 5 */
200 D_ENCRYPT(r,l,10); /* 6 */
201 D_ENCRYPT(l,r,12); /* 7 */
202 D_ENCRYPT(r,l,14); /* 8 */
203 D_ENCRYPT(l,r,16); /* 9 */
204 D_ENCRYPT(r,l,18); /* 10 */
205 D_ENCRYPT(l,r,20); /* 11 */
206 D_ENCRYPT(r,l,22); /* 12 */
207 D_ENCRYPT(l,r,24); /* 13 */
208 D_ENCRYPT(r,l,26); /* 14 */
209 D_ENCRYPT(l,r,28); /* 15 */
210 D_ENCRYPT(r,l,30); /* 16 */
211#else
212 for (i=0; i<32; i+=8)
213 {
214 D_ENCRYPT(l,r,i+0); /* 1 */
215 D_ENCRYPT(r,l,i+2); /* 2 */
216 D_ENCRYPT(l,r,i+4); /* 3 */
217 D_ENCRYPT(r,l,i+6); /* 4 */
218 }
219#endif
220 }
221 else
222 {
223#ifdef DES_UNROLL
224 D_ENCRYPT(l,r,30); /* 16 */
225 D_ENCRYPT(r,l,28); /* 15 */
226 D_ENCRYPT(l,r,26); /* 14 */
227 D_ENCRYPT(r,l,24); /* 13 */
228 D_ENCRYPT(l,r,22); /* 12 */
229 D_ENCRYPT(r,l,20); /* 11 */
230 D_ENCRYPT(l,r,18); /* 10 */
231 D_ENCRYPT(r,l,16); /* 9 */
232 D_ENCRYPT(l,r,14); /* 8 */
233 D_ENCRYPT(r,l,12); /* 7 */
234 D_ENCRYPT(l,r,10); /* 6 */
235 D_ENCRYPT(r,l, 8); /* 5 */
236 D_ENCRYPT(l,r, 6); /* 4 */
237 D_ENCRYPT(r,l, 4); /* 3 */
238 D_ENCRYPT(l,r, 2); /* 2 */
239 D_ENCRYPT(r,l, 0); /* 1 */
240#else
241 for (i=30; i>0; i-=8)
242 {
243 D_ENCRYPT(l,r,i-0); /* 16 */
244 D_ENCRYPT(r,l,i-2); /* 15 */
245 D_ENCRYPT(l,r,i-4); /* 14 */
246 D_ENCRYPT(r,l,i-6); /* 13 */
247 }
248#endif
249 }
250 /* rotate and clear the top bits on machines with 8byte longs */
251 data[0]=ROTATE(l,3)&0xffffffffL;
252 data[1]=ROTATE(r,3)&0xffffffffL;
253 l=r=t=u=0;
254 }
255
256void des_encrypt3(data,ks1,ks2,ks3)
257DES_LONG *data;
258des_key_schedule ks1;
259des_key_schedule ks2;
260des_key_schedule ks3;
261 {
262 register DES_LONG l,r;
263
264 l=data[0];
265 r=data[1];
266 IP(l,r);
267 data[0]=l;
268 data[1]=r;
269 des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
270 des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
271 des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
272 l=data[0];
273 r=data[1];
274 FP(r,l);
275 data[0]=l;
276 data[1]=r;
277 }
278
279void des_decrypt3(data,ks1,ks2,ks3)
280DES_LONG *data;
281des_key_schedule ks1;
282des_key_schedule ks2;
283des_key_schedule ks3;
284 {
285 register DES_LONG l,r;
286
287 l=data[0];
288 r=data[1];
289 IP(l,r);
290 data[0]=l;
291 data[1]=r;
292 des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
293 des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
294 des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
295 l=data[0];
296 r=data[1];
297 FP(r,l);
298 data[0]=l;
299 data[1]=r;
300 }
301
302#ifndef DES_DEFAULT_OPTIONS
303
304void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
305des_cblock (*input);
306des_cblock (*output);
307long length;
308des_key_schedule schedule;
309des_cblock (*ivec);
310int enc;
311 {
312 register DES_LONG tin0,tin1;
313 register DES_LONG tout0,tout1,xor0,xor1;
314 register unsigned char *in,*out;
315 register long l=length;
316 DES_LONG tin[2];
317 unsigned char *iv;
318
319 in=(unsigned char *)input;
320 out=(unsigned char *)output;
321 iv=(unsigned char *)ivec;
322
323 if (enc)
324 {
325 c2l(iv,tout0);
326 c2l(iv,tout1);
327 for (l-=8; l>=0; l-=8)
328 {
329 c2l(in,tin0);
330 c2l(in,tin1);
331 tin0^=tout0; tin[0]=tin0;
332 tin1^=tout1; tin[1]=tin1;
333 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
334 tout0=tin[0]; l2c(tout0,out);
335 tout1=tin[1]; l2c(tout1,out);
336 }
337 if (l != -8)
338 {
339 c2ln(in,tin0,tin1,l+8);
340 tin0^=tout0; tin[0]=tin0;
341 tin1^=tout1; tin[1]=tin1;
342 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
343 tout0=tin[0]; l2c(tout0,out);
344 tout1=tin[1]; l2c(tout1,out);
345 }
346 iv=(unsigned char *)ivec;
347 l2c(tout0,iv);
348 l2c(tout1,iv);
349 }
350 else
351 {
352 c2l(iv,xor0);
353 c2l(iv,xor1);
354 for (l-=8; l>=0; l-=8)
355 {
356 c2l(in,tin0); tin[0]=tin0;
357 c2l(in,tin1); tin[1]=tin1;
358 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
359 tout0=tin[0]^xor0;
360 tout1=tin[1]^xor1;
361 l2c(tout0,out);
362 l2c(tout1,out);
363 xor0=tin0;
364 xor1=tin1;
365 }
366 if (l != -8)
367 {
368 c2l(in,tin0); tin[0]=tin0;
369 c2l(in,tin1); tin[1]=tin1;
370 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
371 tout0=tin[0]^xor0;
372 tout1=tin[1]^xor1;
373 l2cn(tout0,tout1,out,l+8);
374 xor0=tin0;
375 xor1=tin1;
376 }
377
378 iv=(unsigned char *)ivec;
379 l2c(xor0,iv);
380 l2c(xor1,iv);
381 }
382 tin0=tin1=tout0=tout1=xor0=xor1=0;
383 tin[0]=tin[1]=0;
384 }
385
386void des_ede3_cbc_encrypt(input, output, length, ks1, ks2, ks3, ivec, enc)
387des_cblock (*input);
388des_cblock (*output);
389long length;
390des_key_schedule ks1;
391des_key_schedule ks2;
392des_key_schedule ks3;
393des_cblock (*ivec);
394int enc;
395 {
396 register DES_LONG tin0,tin1;
397 register DES_LONG tout0,tout1,xor0,xor1;
398 register unsigned char *in,*out;
399 register long l=length;
400 DES_LONG tin[2];
401 unsigned char *iv;
402
403 in=(unsigned char *)input;
404 out=(unsigned char *)output;
405 iv=(unsigned char *)ivec;
406
407 if (enc)
408 {
409 c2l(iv,tout0);
410 c2l(iv,tout1);
411 for (l-=8; l>=0; l-=8)
412 {
413 c2l(in,tin0);
414 c2l(in,tin1);
415 tin0^=tout0;
416 tin1^=tout1;
417
418 tin[0]=tin0;
419 tin[1]=tin1;
420 des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
421 tout0=tin[0];
422 tout1=tin[1];
423
424 l2c(tout0,out);
425 l2c(tout1,out);
426 }
427 if (l != -8)
428 {
429 c2ln(in,tin0,tin1,l+8);
430 tin0^=tout0;
431 tin1^=tout1;
432
433 tin[0]=tin0;
434 tin[1]=tin1;
435 des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
436 tout0=tin[0];
437 tout1=tin[1];
438
439 l2c(tout0,out);
440 l2c(tout1,out);
441 }
442 iv=(unsigned char *)ivec;
443 l2c(tout0,iv);
444 l2c(tout1,iv);
445 }
446 else
447 {
448 register DES_LONG t0,t1;
449
450 c2l(iv,xor0);
451 c2l(iv,xor1);
452 for (l-=8; l>=0; l-=8)
453 {
454 c2l(in,tin0);
455 c2l(in,tin1);
456
457 t0=tin0;
458 t1=tin1;
459
460 tin[0]=tin0;
461 tin[1]=tin1;
462 des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
463 tout0=tin[0];
464 tout1=tin[1];
465
466 tout0^=xor0;
467 tout1^=xor1;
468 l2c(tout0,out);
469 l2c(tout1,out);
470 xor0=t0;
471 xor1=t1;
472 }
473 if (l != -8)
474 {
475 c2l(in,tin0);
476 c2l(in,tin1);
477
478 t0=tin0;
479 t1=tin1;
480
481 tin[0]=tin0;
482 tin[1]=tin1;
483 des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
484 tout0=tin[0];
485 tout1=tin[1];
486
487 tout0^=xor0;
488 tout1^=xor1;
489 l2cn(tout0,tout1,out,l+8);
490 xor0=t0;
491 xor1=t1;
492 }
493
494 iv=(unsigned char *)ivec;
495 l2c(xor0,iv);
496 l2c(xor1,iv);
497 }
498 tin0=tin1=tout0=tout1=xor0=xor1=0;
499 tin[0]=tin[1]=0;
500 }
501
502#endif /* DES_DEFAULT_OPTIONS */
diff --git a/src/lib/libcrypto/des/ecb3_enc.c b/src/lib/libcrypto/des/ecb3_enc.c
new file mode 100644
index 0000000000..140f6b5285
--- /dev/null
+++ b/src/lib/libcrypto/des/ecb3_enc.c
@@ -0,0 +1,87 @@
1/* crypto/des/ecb3_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61void des_ecb3_encrypt(input, output, ks1, ks2, ks3, enc)
62des_cblock (*input);
63des_cblock (*output);
64des_key_schedule ks1;
65des_key_schedule ks2;
66des_key_schedule ks3;
67int enc;
68 {
69 register DES_LONG l0,l1;
70 register unsigned char *in,*out;
71 DES_LONG ll[2];
72
73 in=(unsigned char *)input;
74 out=(unsigned char *)output;
75 c2l(in,l0);
76 c2l(in,l1);
77 ll[0]=l0;
78 ll[1]=l1;
79 if (enc)
80 des_encrypt3(ll,ks1,ks2,ks3);
81 else
82 des_decrypt3(ll,ks1,ks2,ks3);
83 l0=ll[0];
84 l1=ll[1];
85 l2c(l0,out);
86 l2c(l1,out);
87 }
diff --git a/src/lib/libcrypto/des/ecb_enc.c b/src/lib/libcrypto/des/ecb_enc.c
new file mode 100644
index 0000000000..acf23fdd00
--- /dev/null
+++ b/src/lib/libcrypto/des/ecb_enc.c
@@ -0,0 +1,124 @@
1/* crypto/des/ecb_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60#include "spr.h"
61
62char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay";
63char *DES_version="DES part of SSLeay 0.9.0b 29-Jun-1998";
64
65char *des_options()
66 {
67 static int init=1;
68 static char buf[32];
69
70 if (init)
71 {
72 char *ptr,*unroll,*risc,*size;
73
74 init=0;
75#ifdef DES_PTR
76 ptr="ptr";
77#else
78 ptr="idx";
79#endif
80#if defined(DES_RISC1) || defined(DES_RISC2)
81#ifdef DES_RISC1
82 risc="risc1";
83#endif
84#ifdef DES_RISC2
85 risc="risc2";
86#endif
87#else
88 risc="cisc";
89#endif
90#ifdef DES_UNROLL
91 unroll="16";
92#else
93 unroll="4";
94#endif
95 if (sizeof(DES_LONG) != sizeof(long))
96 size="int";
97 else
98 size="long";
99 sprintf(buf,"des(%s,%s,%s,%s)",ptr,risc,unroll,size);
100 }
101 return(buf);
102 }
103
104
105void des_ecb_encrypt(input, output, ks, enc)
106des_cblock (*input);
107des_cblock (*output);
108des_key_schedule ks;
109int enc;
110 {
111 register DES_LONG l;
112 register unsigned char *in,*out;
113 DES_LONG ll[2];
114
115 in=(unsigned char *)input;
116 out=(unsigned char *)output;
117 c2l(in,l); ll[0]=l;
118 c2l(in,l); ll[1]=l;
119 des_encrypt(ll,ks,enc);
120 l=ll[0]; l2c(l,out);
121 l=ll[1]; l2c(l,out);
122 l=ll[0]=ll[1]=0;
123 }
124
diff --git a/src/lib/libcrypto/des/enc_read.c b/src/lib/libcrypto/des/enc_read.c
new file mode 100644
index 0000000000..e08a904d75
--- /dev/null
+++ b/src/lib/libcrypto/des/enc_read.c
@@ -0,0 +1,218 @@
1/* crypto/des/enc_read.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "des_locl.h"
62
63/* This has some uglies in it but it works - even over sockets. */
64/*extern int errno;*/
65int des_rw_mode=DES_PCBC_MODE;
66
67int des_enc_read(fd, buf, len, sched, iv)
68int fd;
69char *buf;
70int len;
71des_key_schedule sched;
72des_cblock (*iv);
73 {
74 /* data to be unencrypted */
75 int net_num=0;
76 static unsigned char *net=NULL;
77 /* extra unencrypted data
78 * for when a block of 100 comes in but is des_read one byte at
79 * a time. */
80 static char *unnet=NULL;
81 static int unnet_start=0;
82 static int unnet_left=0;
83 static char *tmpbuf=NULL;
84 int i;
85 long num=0,rnum;
86 unsigned char *p;
87
88 if (tmpbuf == NULL)
89 {
90 tmpbuf=(char *)malloc(BSIZE);
91 if (tmpbuf == NULL) return(-1);
92 }
93 if (net == NULL)
94 {
95 net=(unsigned char *)malloc(BSIZE);
96 if (net == NULL) return(-1);
97 }
98 if (unnet == NULL)
99 {
100 unnet=(char *)malloc(BSIZE);
101 if (unnet == NULL) return(-1);
102 }
103 /* left over data from last decrypt */
104 if (unnet_left != 0)
105 {
106 if (unnet_left < len)
107 {
108 /* we still still need more data but will return
109 * with the number of bytes we have - should always
110 * check the return value */
111 memcpy(buf,&(unnet[unnet_start]),
112 (unsigned int)unnet_left);
113 /* eay 26/08/92 I had the next 2 lines
114 * reversed :-( */
115 i=unnet_left;
116 unnet_start=unnet_left=0;
117 }
118 else
119 {
120 memcpy(buf,&(unnet[unnet_start]),(unsigned int)len);
121 unnet_start+=len;
122 unnet_left-=len;
123 i=len;
124 }
125 return(i);
126 }
127
128 /* We need to get more data. */
129 if (len > MAXWRITE) len=MAXWRITE;
130
131 /* first - get the length */
132 while (net_num < HDRSIZE)
133 {
134 i=read(fd,&(net[net_num]),(unsigned int)HDRSIZE-net_num);
135#ifdef EINTR
136 if ((i == -1) && (errno == EINTR)) continue;
137#endif
138 if (i <= 0) return(0);
139 net_num+=i;
140 }
141
142 /* we now have at net_num bytes in net */
143 p=net;
144 /* num=0; */
145 n2l(p,num);
146 /* num should be rounded up to the next group of eight
147 * we make sure that we have read a multiple of 8 bytes from the net.
148 */
149 if ((num > MAXWRITE) || (num < 0)) /* error */
150 return(-1);
151 rnum=(num < 8)?8:((num+7)/8*8);
152
153 net_num=0;
154 while (net_num < rnum)
155 {
156 i=read(fd,&(net[net_num]),(unsigned int)rnum-net_num);
157#ifdef EINTR
158 if ((i == -1) && (errno == EINTR)) continue;
159#endif
160 if (i <= 0) return(0);
161 net_num+=i;
162 }
163
164 /* Check if there will be data left over. */
165 if (len < num)
166 {
167 if (des_rw_mode & DES_PCBC_MODE)
168 des_pcbc_encrypt((des_cblock *)net,(des_cblock *)unnet,
169 num,sched,iv,DES_DECRYPT);
170 else
171 des_cbc_encrypt((des_cblock *)net,(des_cblock *)unnet,
172 num,sched,iv,DES_DECRYPT);
173 memcpy(buf,unnet,(unsigned int)len);
174 unnet_start=len;
175 unnet_left=(int)num-len;
176
177 /* The following line is done because we return num
178 * as the number of bytes read. */
179 num=len;
180 }
181 else
182 {
183 /* >output is a multiple of 8 byes, if len < rnum
184 * >we must be careful. The user must be aware that this
185 * >routine will write more bytes than he asked for.
186 * >The length of the buffer must be correct.
187 * FIXED - Should be ok now 18-9-90 - eay */
188 if (len < rnum)
189 {
190
191 if (des_rw_mode & DES_PCBC_MODE)
192 des_pcbc_encrypt((des_cblock *)net,
193 (des_cblock *)tmpbuf,
194 num,sched,iv,DES_DECRYPT);
195 else
196 des_cbc_encrypt((des_cblock *)net,
197 (des_cblock *)tmpbuf,
198 num,sched,iv,DES_DECRYPT);
199
200 /* eay 26/08/92 fix a bug that returned more
201 * bytes than you asked for (returned len bytes :-( */
202 memcpy(buf,tmpbuf,(unsigned int)num);
203 }
204 else
205 {
206 if (des_rw_mode & DES_PCBC_MODE)
207 des_pcbc_encrypt((des_cblock *)net,
208 (des_cblock *)buf,num,sched,iv,
209 DES_DECRYPT);
210 else
211 des_cbc_encrypt((des_cblock *)net,
212 (des_cblock *)buf,num,sched,iv,
213 DES_DECRYPT);
214 }
215 }
216 return((int)num);
217 }
218
diff --git a/src/lib/libcrypto/des/enc_writ.c b/src/lib/libcrypto/des/enc_writ.c
new file mode 100644
index 0000000000..29a7330fb0
--- /dev/null
+++ b/src/lib/libcrypto/des/enc_writ.c
@@ -0,0 +1,160 @@
1/* crypto/des/enc_writ.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <time.h>
61#include "des_locl.h"
62
63int des_enc_write(fd, buf, len, sched, iv)
64int fd;
65char *buf;
66int len;
67des_key_schedule sched;
68des_cblock (*iv);
69 {
70#ifdef _LIBC
71 extern int srandom();
72 extern unsigned long time();
73 extern int random();
74 extern int write();
75#endif
76
77 long rnum;
78 int i,j,k,outnum;
79 static char *outbuf=NULL;
80 char shortbuf[8];
81 char *p;
82 static int start=1;
83
84 if (outbuf == NULL)
85 {
86 outbuf=(char *)malloc(BSIZE+HDRSIZE);
87 if (outbuf == NULL) return(-1);
88 }
89 /* If we are sending less than 8 bytes, the same char will look
90 * the same if we don't pad it out with random bytes */
91 if (start)
92 {
93 start=0;
94 srandom((unsigned int)time(NULL));
95 }
96
97 /* lets recurse if we want to send the data in small chunks */
98 if (len > MAXWRITE)
99 {
100 j=0;
101 for (i=0; i<len; i+=k)
102 {
103 k=des_enc_write(fd,&(buf[i]),
104 ((len-i) > MAXWRITE)?MAXWRITE:(len-i),sched,iv);
105 if (k < 0)
106 return(k);
107 else
108 j+=k;
109 }
110 return(j);
111 }
112
113 /* write length first */
114 p=outbuf;
115 l2n(len,p);
116
117 /* pad short strings */
118 if (len < 8)
119 {
120 p=shortbuf;
121 memcpy(shortbuf,buf,(unsigned int)len);
122 for (i=len; i<8; i++)
123 shortbuf[i]=random();
124 rnum=8;
125 }
126 else
127 {
128 p=buf;
129 rnum=((len+7)/8*8); /* round up to nearest eight */
130 }
131
132 if (des_rw_mode & DES_PCBC_MODE)
133 des_pcbc_encrypt((des_cblock *)p,
134 (des_cblock *)&(outbuf[HDRSIZE]),
135 (long)((len<8)?8:len),sched,iv,DES_ENCRYPT);
136 else
137 des_cbc_encrypt((des_cblock *)p,
138 (des_cblock *)&(outbuf[HDRSIZE]),
139 (long)((len<8)?8:len),sched,iv,DES_ENCRYPT);
140
141 /* output */
142 outnum=(int)rnum+HDRSIZE;
143
144 for (j=0; j<outnum; j+=i)
145 {
146 /* eay 26/08/92 I was not doing writing from where we
147 * got upto. */
148 i=write(fd,&(outbuf[j]),(unsigned int)(outnum-j));
149 if (i == -1)
150 {
151 if (errno == EINTR)
152 i=0;
153 else /* This is really a bad error - very bad
154 * It will stuff-up both ends. */
155 return(-1);
156 }
157 }
158
159 return(len);
160 }
diff --git a/src/lib/libcrypto/des/fcrypt.c b/src/lib/libcrypto/des/fcrypt.c
new file mode 100644
index 0000000000..129beb27da
--- /dev/null
+++ b/src/lib/libcrypto/des/fcrypt.c
@@ -0,0 +1,153 @@
1/* NOCW */
2#include <stdio.h>
3
4/* This version of crypt has been developed from my MIT compatable
5 * DES library.
6 * The library is available at pub/Crypto/DES at ftp.psy.uq.oz.au
7 * Eric Young (eay@cryptsoft.com)
8 */
9
10/* Modification by Jens Kupferschmidt (Cu)
11 * I have included directive PARA for shared memory computers.
12 * I have included a directive LONGCRYPT to using this routine to cipher
13 * passwords with more then 8 bytes like HP-UX 10.x it used. The MAXPLEN
14 * definition is the maximum of lenght of password and can changed. I have
15 * defined 24.
16 */
17
18#include "des_locl.h"
19
20/* Added more values to handle illegal salt values the way normal
21 * crypt() implementations do. The patch was sent by
22 * Bjorn Gronvall <bg@sics.se>
23 */
24static unsigned const char con_salt[128]={
250xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,
260xDA,0xDB,0xDC,0xDD,0xDE,0xDF,0xE0,0xE1,
270xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,
280xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,
290xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,
300xFA,0xFB,0xFC,0xFD,0xFE,0xFF,0x00,0x01,
310x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
320x0A,0x0B,0x05,0x06,0x07,0x08,0x09,0x0A,
330x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,
340x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,
350x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,
360x23,0x24,0x25,0x20,0x21,0x22,0x23,0x24,
370x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,
380x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,
390x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,
400x3D,0x3E,0x3F,0x40,0x41,0x42,0x43,0x44,
41};
42
43static unsigned const char cov_2char[64]={
440x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
450x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
460x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
470x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
480x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
490x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
500x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
510x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
52};
53
54#ifndef NOPROTO
55void fcrypt_body(DES_LONG *out,des_key_schedule ks,
56 DES_LONG Eswap0, DES_LONG Eswap1);
57
58#if defined(PERL5) || defined(FreeBSD)
59char *des_crypt(const char *buf,const char *salt);
60#else
61char *crypt(const char *buf,const char *salt);
62#endif
63#else
64void fcrypt_body();
65#ifdef PERL5
66char *des_crypt();
67#else
68char *crypt();
69#endif
70#endif
71
72#if defined(PERL5) || defined(FreeBSD)
73char *des_crypt(buf,salt)
74#else
75char *crypt(buf,salt)
76#endif
77const char *buf;
78const char *salt;
79 {
80 static char buff[14];
81
82 return(des_fcrypt(buf,salt,buff));
83 }
84
85
86char *des_fcrypt(buf,salt,ret)
87const char *buf;
88const char *salt;
89char *ret;
90 {
91 unsigned int i,j,x,y;
92 DES_LONG Eswap0,Eswap1;
93 DES_LONG out[2],ll;
94 des_cblock key;
95 des_key_schedule ks;
96 unsigned char bb[9];
97 unsigned char *b=bb;
98 unsigned char c,u;
99
100 /* eay 25/08/92
101 * If you call crypt("pwd","*") as often happens when you
102 * have * as the pwd field in /etc/passwd, the function
103 * returns *\0XXXXXXXXX
104 * The \0 makes the string look like * so the pwd "*" would
105 * crypt to "*". This was found when replacing the crypt in
106 * our shared libraries. People found that the disbled
107 * accounts effectivly had no passwd :-(. */
108 x=ret[0]=((salt[0] == '\0')?'A':salt[0]);
109 Eswap0=con_salt[x]<<2;
110 x=ret[1]=((salt[1] == '\0')?'A':salt[1]);
111 Eswap1=con_salt[x]<<6;
112
113/* EAY
114r=strlen(buf);
115r=(r+7)/8;
116*/
117 for (i=0; i<8; i++)
118 {
119 c= *(buf++);
120 if (!c) break;
121 key[i]=(c<<1);
122 }
123 for (; i<8; i++)
124 key[i]=0;
125
126 des_set_key((des_cblock *)(key),ks);
127 fcrypt_body(&(out[0]),ks,Eswap0,Eswap1);
128
129 ll=out[0]; l2c(ll,b);
130 ll=out[1]; l2c(ll,b);
131 y=0;
132 u=0x80;
133 bb[8]=0;
134 for (i=2; i<13; i++)
135 {
136 c=0;
137 for (j=0; j<6; j++)
138 {
139 c<<=1;
140 if (bb[y] & u) c|=1;
141 u>>=1;
142 if (!u)
143 {
144 y++;
145 u=0x80;
146 }
147 }
148 ret[i]=cov_2char[c];
149 }
150 ret[13]='\0';
151 return(ret);
152 }
153
diff --git a/src/lib/libcrypto/des/fcrypt_b.c b/src/lib/libcrypto/des/fcrypt_b.c
new file mode 100644
index 0000000000..1544634bc1
--- /dev/null
+++ b/src/lib/libcrypto/des/fcrypt_b.c
@@ -0,0 +1,148 @@
1/* crypto/des/fcrypt_b.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60
61/* This version of crypt has been developed from my MIT compatable
62 * DES library.
63 * The library is available at pub/Crypto/DES at ftp.psy.uq.oz.au
64 * Eric Young (eay@cryptsoft.com)
65 */
66
67#define DES_FCRYPT
68#include "des_locl.h"
69#undef DES_FCRYPT
70
71#undef PERM_OP
72#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
73 (b)^=(t),\
74 (a)^=((t)<<(n)))
75
76#undef HPERM_OP
77#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
78 (a)=(a)^(t)^(t>>(16-(n))))\
79
80void fcrypt_body(out, ks, Eswap0, Eswap1)
81DES_LONG *out;
82des_key_schedule ks;
83DES_LONG Eswap0;
84DES_LONG Eswap1;
85 {
86 register DES_LONG l,r,t,u;
87#ifdef DES_PTR
88 register unsigned char *des_SP=(unsigned char *)des_SPtrans;
89#endif
90 register DES_LONG *s;
91 register int j;
92 register DES_LONG E0,E1;
93
94 l=0;
95 r=0;
96
97 s=(DES_LONG *)ks;
98 E0=Eswap0;
99 E1=Eswap1;
100
101 for (j=0; j<25; j++)
102 {
103#ifdef DES_UNROLL
104 register int i;
105
106 for (i=0; i<32; i+=8)
107 {
108 D_ENCRYPT(l,r,i+0); /* 1 */
109 D_ENCRYPT(r,l,i+2); /* 2 */
110 D_ENCRYPT(l,r,i+4); /* 1 */
111 D_ENCRYPT(r,l,i+6); /* 2 */
112 }
113#else
114 D_ENCRYPT(l,r, 0); /* 1 */
115 D_ENCRYPT(r,l, 2); /* 2 */
116 D_ENCRYPT(l,r, 4); /* 3 */
117 D_ENCRYPT(r,l, 6); /* 4 */
118 D_ENCRYPT(l,r, 8); /* 5 */
119 D_ENCRYPT(r,l,10); /* 6 */
120 D_ENCRYPT(l,r,12); /* 7 */
121 D_ENCRYPT(r,l,14); /* 8 */
122 D_ENCRYPT(l,r,16); /* 9 */
123 D_ENCRYPT(r,l,18); /* 10 */
124 D_ENCRYPT(l,r,20); /* 11 */
125 D_ENCRYPT(r,l,22); /* 12 */
126 D_ENCRYPT(l,r,24); /* 13 */
127 D_ENCRYPT(r,l,26); /* 14 */
128 D_ENCRYPT(l,r,28); /* 15 */
129 D_ENCRYPT(r,l,30); /* 16 */
130#endif
131
132 t=l;
133 l=r;
134 r=t;
135 }
136 l=ROTATE(l,3)&0xffffffffL;
137 r=ROTATE(r,3)&0xffffffffL;
138
139 PERM_OP(l,r,t, 1,0x55555555L);
140 PERM_OP(r,l,t, 8,0x00ff00ffL);
141 PERM_OP(l,r,t, 2,0x33333333L);
142 PERM_OP(r,l,t,16,0x0000ffffL);
143 PERM_OP(l,r,t, 4,0x0f0f0f0fL);
144
145 out[0]=r;
146 out[1]=l;
147 }
148
diff --git a/src/lib/libcrypto/des/ncbc_enc.c b/src/lib/libcrypto/des/ncbc_enc.c
new file mode 100644
index 0000000000..1d1a368c22
--- /dev/null
+++ b/src/lib/libcrypto/des/ncbc_enc.c
@@ -0,0 +1,130 @@
1/* crypto/des/ncbc_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
62des_cblock (*input);
63des_cblock (*output);
64long length;
65des_key_schedule schedule;
66des_cblock (*ivec);
67int enc;
68 {
69 register DES_LONG tin0,tin1;
70 register DES_LONG tout0,tout1,xor0,xor1;
71 register unsigned char *in,*out;
72 register long l=length;
73 DES_LONG tin[2];
74 unsigned char *iv;
75
76 in=(unsigned char *)input;
77 out=(unsigned char *)output;
78 iv=(unsigned char *)ivec;
79
80 if (enc)
81 {
82 c2l(iv,tout0);
83 c2l(iv,tout1);
84 for (l-=8; l>=0; l-=8)
85 {
86 c2l(in,tin0);
87 c2l(in,tin1);
88 tin0^=tout0; tin[0]=tin0;
89 tin1^=tout1; tin[1]=tin1;
90 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
91 tout0=tin[0]; l2c(tout0,out);
92 tout1=tin[1]; l2c(tout1,out);
93 }
94 if (l != -8)
95 {
96 c2ln(in,tin0,tin1,l+8);
97 tin0^=tout0; tin[0]=tin0;
98 tin1^=tout1; tin[1]=tin1;
99 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
100 tout0=tin[0]; l2c(tout0,out);
101 tout1=tin[1]; l2c(tout1,out);
102 }
103 iv=(unsigned char *)ivec;
104 l2c(tout0,iv);
105 l2c(tout1,iv);
106 }
107 else
108 {
109 c2l(iv,xor0);
110 c2l(iv,xor1);
111 for (l-=8; l>=0; l-=8)
112 {
113 c2l(in,tin0); tin[0]=tin0;
114 c2l(in,tin1); tin[1]=tin1;
115 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
116 tout0=tin[0]^xor0;
117 tout1=tin[1]^xor1;
118 l2c(tout0,out);
119 l2c(tout1,out);
120 xor0=tin0;
121 xor1=tin1;
122 }
123 iv=(unsigned char *)ivec;
124 l2c(xor0,iv);
125 l2c(xor1,iv);
126 }
127 tin0=tin1=tout0=tout1=xor0=xor1=0;
128 tin[0]=tin[1]=0;
129 }
130
diff --git a/src/lib/libcrypto/des/ofb64ede.c b/src/lib/libcrypto/des/ofb64ede.c
new file mode 100644
index 0000000000..4b1b0199f1
--- /dev/null
+++ b/src/lib/libcrypto/des/ofb64ede.c
@@ -0,0 +1,131 @@
1/* crypto/des/ofb64ede.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61/* The input and output encrypted as though 64bit ofb mode is being
62 * used. The extra state information to record how much of the
63 * 64bit block we have used is contained in *num;
64 */
65void des_ede3_ofb64_encrypt(in, out, length, k1,k2,k3, ivec, num)
66register unsigned char *in;
67register unsigned char *out;
68long length;
69des_key_schedule k1,k2,k3;
70des_cblock (*ivec);
71int *num;
72 {
73 register DES_LONG v0,v1;
74 register int n= *num;
75 register long l=length;
76 des_cblock d;
77 register char *dp;
78 DES_LONG ti[2];
79 unsigned char *iv;
80 int save=0;
81
82 iv=(unsigned char *)ivec;
83 c2l(iv,v0);
84 c2l(iv,v1);
85 ti[0]=v0;
86 ti[1]=v1;
87 dp=(char *)d;
88 l2c(v0,dp);
89 l2c(v1,dp);
90 while (l--)
91 {
92 if (n == 0)
93 {
94 /* ti[0]=v0; */
95 /* ti[1]=v1; */
96 des_encrypt3((DES_LONG *)ti,k1,k2,k3);
97 v0=ti[0];
98 v1=ti[1];
99
100 dp=(char *)d;
101 l2c(v0,dp);
102 l2c(v1,dp);
103 save++;
104 }
105 *(out++)= *(in++)^d[n];
106 n=(n+1)&0x07;
107 }
108 if (save)
109 {
110/* v0=ti[0];
111 v1=ti[1];*/
112 iv=(unsigned char *)ivec;
113 l2c(v0,iv);
114 l2c(v1,iv);
115 }
116 v0=v1=ti[0]=ti[1]=0;
117 *num=n;
118 }
119
120#ifdef undef /* MACRO */
121void des_ede2_ofb64_encrypt(in, out, length, k1,k2, ivec, num)
122register unsigned char *in;
123register unsigned char *out;
124long length;
125des_key_schedule k1,k2;
126des_cblock (*ivec);
127int *num;
128 {
129 des_ede3_ofb64_encrypt(in, out, length, k1,k2,k1, ivec, num);
130 }
131#endif
diff --git a/src/lib/libcrypto/des/ofb64enc.c b/src/lib/libcrypto/des/ofb64enc.c
new file mode 100644
index 0000000000..ea7e612697
--- /dev/null
+++ b/src/lib/libcrypto/des/ofb64enc.c
@@ -0,0 +1,114 @@
1/* crypto/des/ofb64enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61/* The input and output encrypted as though 64bit ofb mode is being
62 * used. The extra state information to record how much of the
63 * 64bit block we have used is contained in *num;
64 */
65void des_ofb64_encrypt(in, out, length, schedule, ivec, num)
66register unsigned char *in;
67register unsigned char *out;
68long length;
69des_key_schedule schedule;
70des_cblock (*ivec);
71int *num;
72 {
73 register DES_LONG v0,v1,t;
74 register int n= *num;
75 register long l=length;
76 des_cblock d;
77 register char *dp;
78 DES_LONG ti[2];
79 unsigned char *iv;
80 int save=0;
81
82 iv=(unsigned char *)ivec;
83 c2l(iv,v0);
84 c2l(iv,v1);
85 ti[0]=v0;
86 ti[1]=v1;
87 dp=(char *)d;
88 l2c(v0,dp);
89 l2c(v1,dp);
90 while (l--)
91 {
92 if (n == 0)
93 {
94 des_encrypt((DES_LONG *)ti,schedule,DES_ENCRYPT);
95 dp=(char *)d;
96 t=ti[0]; l2c(t,dp);
97 t=ti[1]; l2c(t,dp);
98 save++;
99 }
100 *(out++)= *(in++)^d[n];
101 n=(n+1)&0x07;
102 }
103 if (save)
104 {
105 v0=ti[0];
106 v1=ti[1];
107 iv=(unsigned char *)ivec;
108 l2c(v0,iv);
109 l2c(v1,iv);
110 }
111 t=v0=v1=ti[0]=ti[1]=0;
112 *num=n;
113 }
114
diff --git a/src/lib/libcrypto/des/ofb_enc.c b/src/lib/libcrypto/des/ofb_enc.c
new file mode 100644
index 0000000000..4db0cdbd60
--- /dev/null
+++ b/src/lib/libcrypto/des/ofb_enc.c
@@ -0,0 +1,139 @@
1/* crypto/des/ofb_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61/* The input and output are loaded in multiples of 8 bits.
62 * What this means is that if you hame numbits=12 and length=2
63 * the first 12 bits will be retrieved from the first byte and half
64 * the second. The second 12 bits will come from the 3rd and half the 4th
65 * byte.
66 */
67void des_ofb_encrypt(in, out, numbits, length, schedule, ivec)
68unsigned char *in;
69unsigned char *out;
70int numbits;
71long length;
72des_key_schedule schedule;
73des_cblock (*ivec);
74 {
75 register DES_LONG d0,d1,vv0,vv1,v0,v1,n=(numbits+7)/8;
76 register DES_LONG mask0,mask1;
77 register long l=length;
78 register int num=numbits;
79 DES_LONG ti[2];
80 unsigned char *iv;
81
82 if (num > 64) return;
83 if (num > 32)
84 {
85 mask0=0xffffffffL;
86 if (num >= 64)
87 mask1=mask0;
88 else
89 mask1=(1L<<(num-32))-1;
90 }
91 else
92 {
93 if (num == 32)
94 mask0=0xffffffffL;
95 else
96 mask0=(1L<<num)-1;
97 mask1=0x00000000L;
98 }
99
100 iv=(unsigned char *)ivec;
101 c2l(iv,v0);
102 c2l(iv,v1);
103 ti[0]=v0;
104 ti[1]=v1;
105 while (l-- > 0)
106 {
107 ti[0]=v0;
108 ti[1]=v1;
109 des_encrypt((DES_LONG *)ti,schedule,DES_ENCRYPT);
110 vv0=ti[0];
111 vv1=ti[1];
112 c2ln(in,d0,d1,n);
113 in+=n;
114 d0=(d0^vv0)&mask0;
115 d1=(d1^vv1)&mask1;
116 l2cn(d0,d1,out,n);
117 out+=n;
118
119 if (num == 32)
120 { v0=v1; v1=vv0; }
121 else if (num == 64)
122 { v0=vv0; v1=vv1; }
123 else if (num > 32) /* && num != 64 */
124 {
125 v0=((v1>>(num-32))|(vv0<<(64-num)))&0xffffffffL;
126 v1=((vv0>>(num-32))|(vv1<<(64-num)))&0xffffffffL;
127 }
128 else /* num < 32 */
129 {
130 v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL;
131 v1=((v1>>num)|(vv0<<(32-num)))&0xffffffffL;
132 }
133 }
134 iv=(unsigned char *)ivec;
135 l2c(v0,iv);
136 l2c(v1,iv);
137 v0=v1=d0=d1=ti[0]=ti[1]=vv0=vv1=0;
138 }
139
diff --git a/src/lib/libcrypto/des/pcbc_enc.c b/src/lib/libcrypto/des/pcbc_enc.c
new file mode 100644
index 0000000000..4513207d90
--- /dev/null
+++ b/src/lib/libcrypto/des/pcbc_enc.c
@@ -0,0 +1,126 @@
1/* crypto/des/pcbc_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61void des_pcbc_encrypt(input, output, length, schedule, ivec, enc)
62des_cblock (*input);
63des_cblock (*output);
64long length;
65des_key_schedule schedule;
66des_cblock (*ivec);
67int enc;
68 {
69 register DES_LONG sin0,sin1,xor0,xor1,tout0,tout1;
70 DES_LONG tin[2];
71 unsigned char *in,*out,*iv;
72
73 in=(unsigned char *)input;
74 out=(unsigned char *)output;
75 iv=(unsigned char *)ivec;
76
77 if (enc)
78 {
79 c2l(iv,xor0);
80 c2l(iv,xor1);
81 for (; length>0; length-=8)
82 {
83 if (length >= 8)
84 {
85 c2l(in,sin0);
86 c2l(in,sin1);
87 }
88 else
89 c2ln(in,sin0,sin1,length);
90 tin[0]=sin0^xor0;
91 tin[1]=sin1^xor1;
92 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
93 tout0=tin[0];
94 tout1=tin[1];
95 xor0=sin0^tout0;
96 xor1=sin1^tout1;
97 l2c(tout0,out);
98 l2c(tout1,out);
99 }
100 }
101 else
102 {
103 c2l(iv,xor0); c2l(iv,xor1);
104 for (; length>0; length-=8)
105 {
106 c2l(in,sin0);
107 c2l(in,sin1);
108 tin[0]=sin0;
109 tin[1]=sin1;
110 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
111 tout0=tin[0]^xor0;
112 tout1=tin[1]^xor1;
113 if (length >= 8)
114 {
115 l2c(tout0,out);
116 l2c(tout1,out);
117 }
118 else
119 l2cn(tout0,tout1,out,length);
120 xor0=tout0^sin0;
121 xor1=tout1^sin1;
122 }
123 }
124 tin[0]=tin[1]=0;
125 sin0=sin1=xor0=xor1=tout0=tout1=0;
126 }
diff --git a/src/lib/libcrypto/des/qud_cksm.c b/src/lib/libcrypto/des/qud_cksm.c
new file mode 100644
index 0000000000..8526abf334
--- /dev/null
+++ b/src/lib/libcrypto/des/qud_cksm.c
@@ -0,0 +1,144 @@
1/* crypto/des/qud_cksm.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* From "Message Authentication" R.R. Jueneman, S.M. Matyas, C.H. Meyer
60 * IEEE Communications Magazine Sept 1985 Vol. 23 No. 9 p 29-40
61 * This module in only based on the code in this paper and is
62 * almost definitely not the same as the MIT implementation.
63 */
64#include "des_locl.h"
65
66/* bug fix for dos - 7/6/91 - Larry hughes@logos.ucs.indiana.edu */
67#define Q_B0(a) (((DES_LONG)(a)))
68#define Q_B1(a) (((DES_LONG)(a))<<8)
69#define Q_B2(a) (((DES_LONG)(a))<<16)
70#define Q_B3(a) (((DES_LONG)(a))<<24)
71
72/* used to scramble things a bit */
73/* Got the value MIT uses via brute force :-) 2/10/90 eay */
74#define NOISE ((DES_LONG)83653421L)
75
76DES_LONG des_quad_cksum(input, output, length, out_count, seed)
77des_cblock (*input);
78des_cblock (*output);
79long length;
80int out_count;
81des_cblock (*seed);
82 {
83 DES_LONG z0,z1,t0,t1;
84 int i;
85 long l;
86 unsigned char *cp;
87 unsigned char *lp;
88
89 if (out_count < 1) out_count=1;
90 lp=(unsigned char *)output;
91
92 z0=Q_B0((*seed)[0])|Q_B1((*seed)[1])|Q_B2((*seed)[2])|Q_B3((*seed)[3]);
93 z1=Q_B0((*seed)[4])|Q_B1((*seed)[5])|Q_B2((*seed)[6])|Q_B3((*seed)[7]);
94
95 for (i=0; ((i<4)&&(i<out_count)); i++)
96 {
97 cp=(unsigned char *)input;
98 l=length;
99 while (l > 0)
100 {
101 if (l > 1)
102 {
103 t0= (DES_LONG)(*(cp++));
104 t0|=(DES_LONG)Q_B1(*(cp++));
105 l--;
106 }
107 else
108 t0= (DES_LONG)(*(cp++));
109 l--;
110 /* add */
111 t0+=z0;
112 t0&=0xffffffffL;
113 t1=z1;
114 /* square, well sort of square */
115 z0=((((t0*t0)&0xffffffffL)+((t1*t1)&0xffffffffL))
116 &0xffffffffL)%0x7fffffffL;
117 z1=((t0*((t1+NOISE)&0xffffffffL))&0xffffffffL)%0x7fffffffL;
118 }
119 if (lp != NULL)
120 {
121 /* I believe I finally have things worked out.
122 * The MIT library assumes that the checksum
123 * is one huge number and it is returned in a
124 * host dependant byte order.
125 */
126 static DES_LONG ltmp=1;
127 static unsigned char *c=(unsigned char *)&ltmp;
128
129 if (c[0])
130 {
131 l2c(z0,lp);
132 l2c(z1,lp);
133 }
134 else
135 {
136 lp=output[out_count-i-1];
137 l2n(z1,lp);
138 l2n(z0,lp);
139 }
140 }
141 }
142 return(z0);
143 }
144
diff --git a/src/lib/libcrypto/des/rand_key.c b/src/lib/libcrypto/des/rand_key.c
new file mode 100644
index 0000000000..8c30bd029a
--- /dev/null
+++ b/src/lib/libcrypto/des/rand_key.c
@@ -0,0 +1,118 @@
1/* crypto/des/rand_key.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60#include <time.h>
61
62static int seed=0;
63static des_cblock init;
64
65void des_random_seed(key)
66des_cblock key;
67 {
68 memcpy(init,key,sizeof(des_cblock));
69 seed=1;
70 }
71
72void des_random_key(ret)
73unsigned char *ret;
74 {
75 des_key_schedule ks;
76 static DES_LONG c=0;
77 static unsigned short pid=0;
78 static des_cblock data={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
79 des_cblock key;
80 unsigned char *p;
81 DES_LONG t;
82 int i;
83
84#ifdef MSDOS
85 pid=1;
86#else
87 if (!pid) pid=getpid();
88#endif
89 p=key;
90 if (seed)
91 {
92 for (i=0; i<8; i++)
93 {
94 data[i] ^= init[i];
95 init[i]=0;
96 }
97 seed=0;
98 }
99 t=(DES_LONG)time(NULL);
100 l2c(t,p);
101 t=(DES_LONG)((pid)|((c++)<<16));
102 l2c(t,p);
103
104 des_set_odd_parity((des_cblock *)data);
105 des_set_key((des_cblock *)data,ks);
106 des_cbc_cksum((des_cblock *)key,(des_cblock *)key,
107 (long)sizeof(key),ks,(des_cblock *)data);
108
109 des_set_odd_parity((des_cblock *)key);
110 des_set_key((des_cblock *)key,ks);
111 des_cbc_cksum((des_cblock *)key,(des_cblock *)data,
112 (long)sizeof(key),ks,(des_cblock *)key);
113
114 memcpy(ret,data,sizeof(key));
115 memset(key,0,sizeof(key));
116 memset(ks,0,sizeof(ks));
117 t=0;
118 }
diff --git a/src/lib/libcrypto/des/set_key.c b/src/lib/libcrypto/des/set_key.c
new file mode 100644
index 0000000000..c3bcd7ee2b
--- /dev/null
+++ b/src/lib/libcrypto/des/set_key.c
@@ -0,0 +1,246 @@
1/* crypto/des/set_key.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* set_key.c v 1.4 eay 24/9/91
60 * 1.4 Speed up by 400% :-)
61 * 1.3 added register declarations.
62 * 1.2 unrolled make_key_sched a bit more
63 * 1.1 added norm_expand_bits
64 * 1.0 First working version
65 */
66#include "des_locl.h"
67#include "podd.h"
68#include "sk.h"
69
70#ifndef NOPROTO
71static int check_parity(des_cblock (*key));
72#else
73static int check_parity();
74#endif
75
76int des_check_key=0;
77
78void des_set_odd_parity(key)
79des_cblock (*key);
80 {
81 int i;
82
83 for (i=0; i<DES_KEY_SZ; i++)
84 (*key)[i]=odd_parity[(*key)[i]];
85 }
86
87static int check_parity(key)
88des_cblock (*key);
89 {
90 int i;
91
92 for (i=0; i<DES_KEY_SZ; i++)
93 {
94 if ((*key)[i] != odd_parity[(*key)[i]])
95 return(0);
96 }
97 return(1);
98 }
99
100/* Weak and semi week keys as take from
101 * %A D.W. Davies
102 * %A W.L. Price
103 * %T Security for Computer Networks
104 * %I John Wiley & Sons
105 * %D 1984
106 * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
107 * (and actual cblock values).
108 */
109#define NUM_WEAK_KEY 16
110static des_cblock weak_keys[NUM_WEAK_KEY]={
111 /* weak keys */
112 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
113 {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
114 {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
115 {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
116 /* semi-weak keys */
117 {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
118 {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
119 {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
120 {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
121 {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
122 {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
123 {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
124 {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
125 {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
126 {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
127 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
128 {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
129
130int des_is_weak_key(key)
131des_cblock (*key);
132 {
133 int i;
134
135 for (i=0; i<NUM_WEAK_KEY; i++)
136 /* Added == 0 to comparision, I obviously don't run
137 * this section very often :-(, thanks to
138 * engineering@MorningStar.Com for the fix
139 * eay 93/06/29
140 * Another problem, I was comparing only the first 4
141 * bytes, 97/03/18 */
142 if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
143 return(0);
144 }
145
146/* NOW DEFINED IN des_local.h
147 * See ecb_encrypt.c for a pseudo description of these macros.
148 * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
149 * (b)^=(t),\
150 * (a)=((a)^((t)<<(n))))
151 */
152
153#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
154 (a)=(a)^(t)^(t>>(16-(n))))
155
156/* return 0 if key parity is odd (correct),
157 * return -1 if key parity error,
158 * return -2 if illegal weak key.
159 */
160int des_set_key(key, schedule)
161des_cblock (*key);
162des_key_schedule schedule;
163 {
164 static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
165 register DES_LONG c,d,t,s,t2;
166 register unsigned char *in;
167 register DES_LONG *k;
168 register int i;
169
170 if (des_check_key)
171 {
172 if (!check_parity(key))
173 return(-1);
174
175 if (des_is_weak_key(key))
176 return(-2);
177 }
178
179 k=(DES_LONG *)schedule;
180 in=(unsigned char *)key;
181
182 c2l(in,c);
183 c2l(in,d);
184
185 /* do PC1 in 60 simple operations */
186/* PERM_OP(d,c,t,4,0x0f0f0f0fL);
187 HPERM_OP(c,t,-2, 0xcccc0000L);
188 HPERM_OP(c,t,-1, 0xaaaa0000L);
189 HPERM_OP(c,t, 8, 0x00ff0000L);
190 HPERM_OP(c,t,-1, 0xaaaa0000L);
191 HPERM_OP(d,t,-8, 0xff000000L);
192 HPERM_OP(d,t, 8, 0x00ff0000L);
193 HPERM_OP(d,t, 2, 0x33330000L);
194 d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
195 d=(d>>8)|((c&0xf0000000L)>>4);
196 c&=0x0fffffffL; */
197
198 /* I now do it in 47 simple operations :-)
199 * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
200 * for the inspiration. :-) */
201 PERM_OP (d,c,t,4,0x0f0f0f0fL);
202 HPERM_OP(c,t,-2,0xcccc0000L);
203 HPERM_OP(d,t,-2,0xcccc0000L);
204 PERM_OP (d,c,t,1,0x55555555L);
205 PERM_OP (c,d,t,8,0x00ff00ffL);
206 PERM_OP (d,c,t,1,0x55555555L);
207 d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
208 ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
209 c&=0x0fffffffL;
210
211 for (i=0; i<ITERATIONS; i++)
212 {
213 if (shifts2[i])
214 { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
215 else
216 { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
217 c&=0x0fffffffL;
218 d&=0x0fffffffL;
219 /* could be a few less shifts but I am to lazy at this
220 * point in time to investigate */
221 s= des_skb[0][ (c )&0x3f ]|
222 des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
223 des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
224 des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
225 ((c>>22L)&0x38)];
226 t= des_skb[4][ (d )&0x3f ]|
227 des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
228 des_skb[6][ (d>>15L)&0x3f ]|
229 des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
230
231 /* table contained 0213 4657 */
232 t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
233 *(k++)=ROTATE(t2,30)&0xffffffffL;
234
235 t2=((s>>16L)|(t&0xffff0000L));
236 *(k++)=ROTATE(t2,26)&0xffffffffL;
237 }
238 return(0);
239 }
240
241int des_key_sched(key, schedule)
242des_cblock (*key);
243des_key_schedule schedule;
244 {
245 return(des_set_key(key,schedule));
246 }
diff --git a/src/lib/libcrypto/des/spr.h b/src/lib/libcrypto/des/spr.h
new file mode 100644
index 0000000000..81813f9f7a
--- /dev/null
+++ b/src/lib/libcrypto/des/spr.h
@@ -0,0 +1,204 @@
1/* crypto/des/spr.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59const DES_LONG des_SPtrans[8][64]={
60{
61/* nibble 0 */
620x02080800L, 0x00080000L, 0x02000002L, 0x02080802L,
630x02000000L, 0x00080802L, 0x00080002L, 0x02000002L,
640x00080802L, 0x02080800L, 0x02080000L, 0x00000802L,
650x02000802L, 0x02000000L, 0x00000000L, 0x00080002L,
660x00080000L, 0x00000002L, 0x02000800L, 0x00080800L,
670x02080802L, 0x02080000L, 0x00000802L, 0x02000800L,
680x00000002L, 0x00000800L, 0x00080800L, 0x02080002L,
690x00000800L, 0x02000802L, 0x02080002L, 0x00000000L,
700x00000000L, 0x02080802L, 0x02000800L, 0x00080002L,
710x02080800L, 0x00080000L, 0x00000802L, 0x02000800L,
720x02080002L, 0x00000800L, 0x00080800L, 0x02000002L,
730x00080802L, 0x00000002L, 0x02000002L, 0x02080000L,
740x02080802L, 0x00080800L, 0x02080000L, 0x02000802L,
750x02000000L, 0x00000802L, 0x00080002L, 0x00000000L,
760x00080000L, 0x02000000L, 0x02000802L, 0x02080800L,
770x00000002L, 0x02080002L, 0x00000800L, 0x00080802L,
78},{
79/* nibble 1 */
800x40108010L, 0x00000000L, 0x00108000L, 0x40100000L,
810x40000010L, 0x00008010L, 0x40008000L, 0x00108000L,
820x00008000L, 0x40100010L, 0x00000010L, 0x40008000L,
830x00100010L, 0x40108000L, 0x40100000L, 0x00000010L,
840x00100000L, 0x40008010L, 0x40100010L, 0x00008000L,
850x00108010L, 0x40000000L, 0x00000000L, 0x00100010L,
860x40008010L, 0x00108010L, 0x40108000L, 0x40000010L,
870x40000000L, 0x00100000L, 0x00008010L, 0x40108010L,
880x00100010L, 0x40108000L, 0x40008000L, 0x00108010L,
890x40108010L, 0x00100010L, 0x40000010L, 0x00000000L,
900x40000000L, 0x00008010L, 0x00100000L, 0x40100010L,
910x00008000L, 0x40000000L, 0x00108010L, 0x40008010L,
920x40108000L, 0x00008000L, 0x00000000L, 0x40000010L,
930x00000010L, 0x40108010L, 0x00108000L, 0x40100000L,
940x40100010L, 0x00100000L, 0x00008010L, 0x40008000L,
950x40008010L, 0x00000010L, 0x40100000L, 0x00108000L,
96},{
97/* nibble 2 */
980x04000001L, 0x04040100L, 0x00000100L, 0x04000101L,
990x00040001L, 0x04000000L, 0x04000101L, 0x00040100L,
1000x04000100L, 0x00040000L, 0x04040000L, 0x00000001L,
1010x04040101L, 0x00000101L, 0x00000001L, 0x04040001L,
1020x00000000L, 0x00040001L, 0x04040100L, 0x00000100L,
1030x00000101L, 0x04040101L, 0x00040000L, 0x04000001L,
1040x04040001L, 0x04000100L, 0x00040101L, 0x04040000L,
1050x00040100L, 0x00000000L, 0x04000000L, 0x00040101L,
1060x04040100L, 0x00000100L, 0x00000001L, 0x00040000L,
1070x00000101L, 0x00040001L, 0x04040000L, 0x04000101L,
1080x00000000L, 0x04040100L, 0x00040100L, 0x04040001L,
1090x00040001L, 0x04000000L, 0x04040101L, 0x00000001L,
1100x00040101L, 0x04000001L, 0x04000000L, 0x04040101L,
1110x00040000L, 0x04000100L, 0x04000101L, 0x00040100L,
1120x04000100L, 0x00000000L, 0x04040001L, 0x00000101L,
1130x04000001L, 0x00040101L, 0x00000100L, 0x04040000L,
114},{
115/* nibble 3 */
1160x00401008L, 0x10001000L, 0x00000008L, 0x10401008L,
1170x00000000L, 0x10400000L, 0x10001008L, 0x00400008L,
1180x10401000L, 0x10000008L, 0x10000000L, 0x00001008L,
1190x10000008L, 0x00401008L, 0x00400000L, 0x10000000L,
1200x10400008L, 0x00401000L, 0x00001000L, 0x00000008L,
1210x00401000L, 0x10001008L, 0x10400000L, 0x00001000L,
1220x00001008L, 0x00000000L, 0x00400008L, 0x10401000L,
1230x10001000L, 0x10400008L, 0x10401008L, 0x00400000L,
1240x10400008L, 0x00001008L, 0x00400000L, 0x10000008L,
1250x00401000L, 0x10001000L, 0x00000008L, 0x10400000L,
1260x10001008L, 0x00000000L, 0x00001000L, 0x00400008L,
1270x00000000L, 0x10400008L, 0x10401000L, 0x00001000L,
1280x10000000L, 0x10401008L, 0x00401008L, 0x00400000L,
1290x10401008L, 0x00000008L, 0x10001000L, 0x00401008L,
1300x00400008L, 0x00401000L, 0x10400000L, 0x10001008L,
1310x00001008L, 0x10000000L, 0x10000008L, 0x10401000L,
132},{
133/* nibble 4 */
1340x08000000L, 0x00010000L, 0x00000400L, 0x08010420L,
1350x08010020L, 0x08000400L, 0x00010420L, 0x08010000L,
1360x00010000L, 0x00000020L, 0x08000020L, 0x00010400L,
1370x08000420L, 0x08010020L, 0x08010400L, 0x00000000L,
1380x00010400L, 0x08000000L, 0x00010020L, 0x00000420L,
1390x08000400L, 0x00010420L, 0x00000000L, 0x08000020L,
1400x00000020L, 0x08000420L, 0x08010420L, 0x00010020L,
1410x08010000L, 0x00000400L, 0x00000420L, 0x08010400L,
1420x08010400L, 0x08000420L, 0x00010020L, 0x08010000L,
1430x00010000L, 0x00000020L, 0x08000020L, 0x08000400L,
1440x08000000L, 0x00010400L, 0x08010420L, 0x00000000L,
1450x00010420L, 0x08000000L, 0x00000400L, 0x00010020L,
1460x08000420L, 0x00000400L, 0x00000000L, 0x08010420L,
1470x08010020L, 0x08010400L, 0x00000420L, 0x00010000L,
1480x00010400L, 0x08010020L, 0x08000400L, 0x00000420L,
1490x00000020L, 0x00010420L, 0x08010000L, 0x08000020L,
150},{
151/* nibble 5 */
1520x80000040L, 0x00200040L, 0x00000000L, 0x80202000L,
1530x00200040L, 0x00002000L, 0x80002040L, 0x00200000L,
1540x00002040L, 0x80202040L, 0x00202000L, 0x80000000L,
1550x80002000L, 0x80000040L, 0x80200000L, 0x00202040L,
1560x00200000L, 0x80002040L, 0x80200040L, 0x00000000L,
1570x00002000L, 0x00000040L, 0x80202000L, 0x80200040L,
1580x80202040L, 0x80200000L, 0x80000000L, 0x00002040L,
1590x00000040L, 0x00202000L, 0x00202040L, 0x80002000L,
1600x00002040L, 0x80000000L, 0x80002000L, 0x00202040L,
1610x80202000L, 0x00200040L, 0x00000000L, 0x80002000L,
1620x80000000L, 0x00002000L, 0x80200040L, 0x00200000L,
1630x00200040L, 0x80202040L, 0x00202000L, 0x00000040L,
1640x80202040L, 0x00202000L, 0x00200000L, 0x80002040L,
1650x80000040L, 0x80200000L, 0x00202040L, 0x00000000L,
1660x00002000L, 0x80000040L, 0x80002040L, 0x80202000L,
1670x80200000L, 0x00002040L, 0x00000040L, 0x80200040L,
168},{
169/* nibble 6 */
1700x00004000L, 0x00000200L, 0x01000200L, 0x01000004L,
1710x01004204L, 0x00004004L, 0x00004200L, 0x00000000L,
1720x01000000L, 0x01000204L, 0x00000204L, 0x01004000L,
1730x00000004L, 0x01004200L, 0x01004000L, 0x00000204L,
1740x01000204L, 0x00004000L, 0x00004004L, 0x01004204L,
1750x00000000L, 0x01000200L, 0x01000004L, 0x00004200L,
1760x01004004L, 0x00004204L, 0x01004200L, 0x00000004L,
1770x00004204L, 0x01004004L, 0x00000200L, 0x01000000L,
1780x00004204L, 0x01004000L, 0x01004004L, 0x00000204L,
1790x00004000L, 0x00000200L, 0x01000000L, 0x01004004L,
1800x01000204L, 0x00004204L, 0x00004200L, 0x00000000L,
1810x00000200L, 0x01000004L, 0x00000004L, 0x01000200L,
1820x00000000L, 0x01000204L, 0x01000200L, 0x00004200L,
1830x00000204L, 0x00004000L, 0x01004204L, 0x01000000L,
1840x01004200L, 0x00000004L, 0x00004004L, 0x01004204L,
1850x01000004L, 0x01004200L, 0x01004000L, 0x00004004L,
186},{
187/* nibble 7 */
1880x20800080L, 0x20820000L, 0x00020080L, 0x00000000L,
1890x20020000L, 0x00800080L, 0x20800000L, 0x20820080L,
1900x00000080L, 0x20000000L, 0x00820000L, 0x00020080L,
1910x00820080L, 0x20020080L, 0x20000080L, 0x20800000L,
1920x00020000L, 0x00820080L, 0x00800080L, 0x20020000L,
1930x20820080L, 0x20000080L, 0x00000000L, 0x00820000L,
1940x20000000L, 0x00800000L, 0x20020080L, 0x20800080L,
1950x00800000L, 0x00020000L, 0x20820000L, 0x00000080L,
1960x00800000L, 0x00020000L, 0x20000080L, 0x20820080L,
1970x00020080L, 0x20000000L, 0x00000000L, 0x00820000L,
1980x20800080L, 0x20020080L, 0x20020000L, 0x00800080L,
1990x20820000L, 0x00000080L, 0x00800080L, 0x20020000L,
2000x20820080L, 0x00800000L, 0x20800000L, 0x20000080L,
2010x00820000L, 0x00020080L, 0x20020080L, 0x20800000L,
2020x00000080L, 0x20820000L, 0x00820080L, 0x00000000L,
2030x20000000L, 0x20800080L, 0x00020000L, 0x00820080L,
204}};
diff --git a/src/lib/libcrypto/des/str2key.c b/src/lib/libcrypto/des/str2key.c
new file mode 100644
index 0000000000..3365c1bcf3
--- /dev/null
+++ b/src/lib/libcrypto/des/str2key.c
@@ -0,0 +1,171 @@
1/* crypto/des/str2key.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61extern int des_check_key;
62
63void des_string_to_key(str, key)
64char *str;
65des_cblock (*key);
66 {
67 des_key_schedule ks;
68 int i,length;
69 register unsigned char j;
70
71 memset(key,0,8);
72 length=strlen(str);
73#ifdef OLD_STR_TO_KEY
74 for (i=0; i<length; i++)
75 (*key)[i%8]^=(str[i]<<1);
76#else /* MIT COMPATIBLE */
77 for (i=0; i<length; i++)
78 {
79 j=str[i];
80 if ((i%16) < 8)
81 (*key)[i%8]^=(j<<1);
82 else
83 {
84 /* Reverse the bit order 05/05/92 eay */
85 j=((j<<4)&0xf0)|((j>>4)&0x0f);
86 j=((j<<2)&0xcc)|((j>>2)&0x33);
87 j=((j<<1)&0xaa)|((j>>1)&0x55);
88 (*key)[7-(i%8)]^=j;
89 }
90 }
91#endif
92 des_set_odd_parity((des_cblock *)key);
93 i=des_check_key;
94 des_check_key=0;
95 des_set_key((des_cblock *)key,ks);
96 des_check_key=i;
97 des_cbc_cksum((des_cblock *)str,(des_cblock *)key,(long)length,ks,
98 (des_cblock *)key);
99 memset(ks,0,sizeof(ks));
100 des_set_odd_parity((des_cblock *)key);
101 }
102
103void des_string_to_2keys(str, key1, key2)
104char *str;
105des_cblock (*key1);
106des_cblock (*key2);
107 {
108 des_key_schedule ks;
109 int i,length;
110 register unsigned char j;
111
112 memset(key1,0,8);
113 memset(key2,0,8);
114 length=strlen(str);
115#ifdef OLD_STR_TO_KEY
116 if (length <= 8)
117 {
118 for (i=0; i<length; i++)
119 {
120 (*key2)[i]=(*key1)[i]=(str[i]<<1);
121 }
122 }
123 else
124 {
125 for (i=0; i<length; i++)
126 {
127 if ((i/8)&1)
128 (*key2)[i%8]^=(str[i]<<1);
129 else
130 (*key1)[i%8]^=(str[i]<<1);
131 }
132 }
133#else /* MIT COMPATIBLE */
134 for (i=0; i<length; i++)
135 {
136 j=str[i];
137 if ((i%32) < 16)
138 {
139 if ((i%16) < 8)
140 (*key1)[i%8]^=(j<<1);
141 else
142 (*key2)[i%8]^=(j<<1);
143 }
144 else
145 {
146 j=((j<<4)&0xf0)|((j>>4)&0x0f);
147 j=((j<<2)&0xcc)|((j>>2)&0x33);
148 j=((j<<1)&0xaa)|((j>>1)&0x55);
149 if ((i%16) < 8)
150 (*key1)[7-(i%8)]^=j;
151 else
152 (*key2)[7-(i%8)]^=j;
153 }
154 }
155 if (length <= 8) memcpy(key2,key1,8);
156#endif
157 des_set_odd_parity((des_cblock *)key1);
158 des_set_odd_parity((des_cblock *)key2);
159 i=des_check_key;
160 des_check_key=0;
161 des_set_key((des_cblock *)key1,ks);
162 des_cbc_cksum((des_cblock *)str,(des_cblock *)key1,(long)length,ks,
163 (des_cblock *)key1);
164 des_set_key((des_cblock *)key2,ks);
165 des_cbc_cksum((des_cblock *)str,(des_cblock *)key2,(long)length,ks,
166 (des_cblock *)key2);
167 des_check_key=i;
168 memset(ks,0,sizeof(ks));
169 des_set_odd_parity(key1);
170 des_set_odd_parity(key2);
171 }
diff --git a/src/lib/libcrypto/des/xcbc_enc.c b/src/lib/libcrypto/des/xcbc_enc.c
new file mode 100644
index 0000000000..031589bf50
--- /dev/null
+++ b/src/lib/libcrypto/des/xcbc_enc.c
@@ -0,0 +1,206 @@
1/* crypto/des/xcbc_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "des_locl.h"
60
61/* RSA's DESX */
62
63static unsigned char desx_white_in2out[256]={
640xBD,0x56,0xEA,0xF2,0xA2,0xF1,0xAC,0x2A,0xB0,0x93,0xD1,0x9C,0x1B,0x33,0xFD,0xD0,
650x30,0x04,0xB6,0xDC,0x7D,0xDF,0x32,0x4B,0xF7,0xCB,0x45,0x9B,0x31,0xBB,0x21,0x5A,
660x41,0x9F,0xE1,0xD9,0x4A,0x4D,0x9E,0xDA,0xA0,0x68,0x2C,0xC3,0x27,0x5F,0x80,0x36,
670x3E,0xEE,0xFB,0x95,0x1A,0xFE,0xCE,0xA8,0x34,0xA9,0x13,0xF0,0xA6,0x3F,0xD8,0x0C,
680x78,0x24,0xAF,0x23,0x52,0xC1,0x67,0x17,0xF5,0x66,0x90,0xE7,0xE8,0x07,0xB8,0x60,
690x48,0xE6,0x1E,0x53,0xF3,0x92,0xA4,0x72,0x8C,0x08,0x15,0x6E,0x86,0x00,0x84,0xFA,
700xF4,0x7F,0x8A,0x42,0x19,0xF6,0xDB,0xCD,0x14,0x8D,0x50,0x12,0xBA,0x3C,0x06,0x4E,
710xEC,0xB3,0x35,0x11,0xA1,0x88,0x8E,0x2B,0x94,0x99,0xB7,0x71,0x74,0xD3,0xE4,0xBF,
720x3A,0xDE,0x96,0x0E,0xBC,0x0A,0xED,0x77,0xFC,0x37,0x6B,0x03,0x79,0x89,0x62,0xC6,
730xD7,0xC0,0xD2,0x7C,0x6A,0x8B,0x22,0xA3,0x5B,0x05,0x5D,0x02,0x75,0xD5,0x61,0xE3,
740x18,0x8F,0x55,0x51,0xAD,0x1F,0x0B,0x5E,0x85,0xE5,0xC2,0x57,0x63,0xCA,0x3D,0x6C,
750xB4,0xC5,0xCC,0x70,0xB2,0x91,0x59,0x0D,0x47,0x20,0xC8,0x4F,0x58,0xE0,0x01,0xE2,
760x16,0x38,0xC4,0x6F,0x3B,0x0F,0x65,0x46,0xBE,0x7E,0x2D,0x7B,0x82,0xF9,0x40,0xB5,
770x1D,0x73,0xF8,0xEB,0x26,0xC7,0x87,0x97,0x25,0x54,0xB1,0x28,0xAA,0x98,0x9D,0xA5,
780x64,0x6D,0x7A,0xD4,0x10,0x81,0x44,0xEF,0x49,0xD6,0xAE,0x2E,0xDD,0x76,0x5C,0x2F,
790xA7,0x1C,0xC9,0x09,0x69,0x9A,0x83,0xCF,0x29,0x39,0xB9,0xE9,0x4C,0xFF,0x43,0xAB,
80 };
81
82void des_xwhite_in2out(des_key,in_white,out_white)
83des_cblock (*des_key);
84des_cblock (*in_white);
85des_cblock (*out_white);
86 {
87 unsigned char *key,*in,*out;
88 int out0,out1;
89 int i;
90
91 key=(unsigned char *)des_key;
92 in=(unsigned char *)in_white;
93 out=(unsigned char *)out_white;
94
95 out[0]=out[1]=out[2]=out[3]=out[4]=out[5]=out[6]=out[7]=0;
96 out0=out1=0;
97 for (i=0; i<8; i++)
98 {
99 out[i]=key[i]^desx_white_in2out[out0^out1];
100 out0=out1;
101 out1=(int)out[i&0x07];
102 }
103
104 out0=out[0];
105 out1=out[i];
106 for (i=0; i<8; i++)
107 {
108 out[i]=in[i]^desx_white_in2out[out0^out1];
109 out0=out1;
110 out1=(int)out[i&0x07];
111 }
112 }
113
114void des_xcbc_encrypt(input, output, length, schedule, ivec, inw,outw,enc)
115des_cblock (*input);
116des_cblock (*output);
117long length;
118des_key_schedule schedule;
119des_cblock (*ivec);
120des_cblock (*inw);
121des_cblock (*outw);
122int enc;
123 {
124 register DES_LONG tin0,tin1;
125 register DES_LONG tout0,tout1,xor0,xor1;
126 register DES_LONG inW0,inW1,outW0,outW1;
127 register unsigned char *in,*out;
128 register long l=length;
129 DES_LONG tin[2];
130 unsigned char *iv;
131
132 in=(unsigned char *)inw;
133 c2l(in,inW0);
134 c2l(in,inW1);
135 in=(unsigned char *)outw;
136 c2l(in,outW0);
137 c2l(in,outW1);
138
139 in=(unsigned char *)input;
140 out=(unsigned char *)output;
141 iv=(unsigned char *)ivec;
142
143 if (enc)
144 {
145 c2l(iv,tout0);
146 c2l(iv,tout1);
147 for (l-=8; l>=0; l-=8)
148 {
149 c2l(in,tin0);
150 c2l(in,tin1);
151 tin0^=tout0^inW0; tin[0]=tin0;
152 tin1^=tout1^inW1; tin[1]=tin1;
153 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
154 tout0=tin[0]^outW0; l2c(tout0,out);
155 tout1=tin[1]^outW1; l2c(tout1,out);
156 }
157 if (l != -8)
158 {
159 c2ln(in,tin0,tin1,l+8);
160 tin0^=tout0^inW0; tin[0]=tin0;
161 tin1^=tout1^inW1; tin[1]=tin1;
162 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
163 tout0=tin[0]^outW0; l2c(tout0,out);
164 tout1=tin[1]^outW1; l2c(tout1,out);
165 }
166 iv=(unsigned char *)ivec;
167 l2c(tout0,iv);
168 l2c(tout1,iv);
169 }
170 else
171 {
172 c2l(iv,xor0);
173 c2l(iv,xor1);
174 for (l-=8; l>0; l-=8)
175 {
176 c2l(in,tin0); tin[0]=tin0^outW0;
177 c2l(in,tin1); tin[1]=tin1^outW1;
178 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
179 tout0=tin[0]^xor0^inW0;
180 tout1=tin[1]^xor1^inW1;
181 l2c(tout0,out);
182 l2c(tout1,out);
183 xor0=tin0;
184 xor1=tin1;
185 }
186 if (l != -8)
187 {
188 c2l(in,tin0); tin[0]=tin0^outW0;
189 c2l(in,tin1); tin[1]=tin1^outW1;
190 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
191 tout0=tin[0]^xor0^inW0;
192 tout1=tin[1]^xor1^inW1;
193 l2cn(tout0,tout1,out,l+8);
194 xor0=tin0;
195 xor1=tin1;
196 }
197
198 iv=(unsigned char *)ivec;
199 l2c(xor0,iv);
200 l2c(xor1,iv);
201 }
202 tin0=tin1=tout0=tout1=xor0=xor1=0;
203 inW0=inW1=outW0=outW1=0;
204 tin[0]=tin[1]=0;
205 }
206
diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h
new file mode 100644
index 0000000000..4cc1df2650
--- /dev/null
+++ b/src/lib/libcrypto/dh/dh.h
@@ -0,0 +1,162 @@
1/* crypto/dh/dh.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_DH_H
60#define HEADER_DH_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#ifndef HEADER_BN_H
67#define BIGNUM char
68#endif
69
70typedef struct dh_st
71 {
72 /* This first argument is used to pick up errors when
73 * a DH is passed instead of a EVP_PKEY */
74 int pad;
75 int version;
76 BIGNUM *p;
77 BIGNUM *g;
78 int length; /* optional */
79 BIGNUM *pub_key; /* y */
80 BIGNUM *priv_key; /* x */
81 } DH;
82
83#define DH_GENERATOR_2 2
84/* #define DH_GENERATOR_3 3 */
85#define DH_GENERATOR_5 5
86
87/* DH_check error codes */
88#define DH_CHECK_P_NOT_PRIME 0x01
89#define DH_CHECK_P_NOT_STRONG_PRIME 0x02
90#define DH_UNABLE_TO_CHECK_GENERATOR 0x04
91#define DH_NOT_SUITABLE_GENERATOR 0x08
92
93#define DHparams_dup(x) (DH *)ASN1_dup((int (*)())i2d_DHparams, \
94 (char *(*)())d2i_DHparams,(char *)(x))
95#define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
96 (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x))
97#define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \
98 (unsigned char *)(x))
99#define d2i_DHparams_bio(bp,x) (DH *)ASN1_d2i_bio((char *(*)())DH_new, \
100 (char *(*)())d2i_DHparams,(bp),(unsigned char **)(x))
101#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio(i2d_DHparams,(bp), \
102 (unsigned char *)(x))
103
104#ifndef NOPROTO
105DH * DH_new(void);
106void DH_free(DH *dh);
107int DH_size(DH *dh);
108DH * DH_generate_parameters(int prime_len,int generator,
109 void (*callback)(int,int,char *),char *cb_arg);
110int DH_check(DH *dh,int *codes);
111int DH_generate_key(DH *dh);
112int DH_compute_key(unsigned char *key,BIGNUM *pub_key,DH *dh);
113DH * d2i_DHparams(DH **a,unsigned char **pp, long length);
114int i2d_DHparams(DH *a,unsigned char **pp);
115#ifndef NO_FP_API
116int DHparams_print_fp(FILE *fp, DH *x);
117#endif
118#ifdef HEADER_BIO_H
119int DHparams_print(BIO *bp, DH *x);
120#else
121int DHparams_print(char *bp, DH *x);
122#endif
123void ERR_load_DH_strings(void );
124
125#else
126
127DH * DH_new();
128void DH_free();
129int DH_size();
130DH * DH_generate_parameters();
131int DH_check();
132int DH_generate_key();
133int DH_compute_key();
134DH * d2i_DHparams();
135int i2d_DHparams();
136#ifndef NO_FP_API
137int DHparams_print_fp();
138#endif
139int DHparams_print();
140void ERR_load_DH_strings();
141
142#endif
143
144/* BEGIN ERROR CODES */
145/* Error codes for the DH functions. */
146
147/* Function codes. */
148#define DH_F_DHPARAMS_PRINT 100
149#define DH_F_DHPARAMS_PRINT_FP 101
150#define DH_F_DH_COMPUTE_KEY 102
151#define DH_F_DH_GENERATE_KEY 103
152#define DH_F_DH_GENERATE_PARAMETERS 104
153#define DH_F_DH_NEW 105
154
155/* Reason codes. */
156#define DH_R_NO_PRIVATE_VALUE 100
157
158#ifdef __cplusplus
159}
160#endif
161#endif
162
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c
new file mode 100644
index 0000000000..65602e494f
--- /dev/null
+++ b/src/lib/libcrypto/dh/dh_check.c
@@ -0,0 +1,120 @@
1/* crypto/dh/dh_check.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "dh.h"
63
64/* Check that p is a strong prime and
65 * if g is 2, 3 or 5, check that is is a suitable generator
66 * where
67 * for 2, p mod 24 == 11
68 * for 3, p mod 12 == 5
69 * for 5, p mod 10 == 3 or 7
70 * should hold.
71 */
72
73int DH_check(dh,ret)
74DH *dh;
75int *ret;
76 {
77 int ok=0;
78 BN_CTX *ctx=NULL;
79 BN_ULONG l;
80 BIGNUM *q=NULL;
81
82 *ret=0;
83 ctx=BN_CTX_new();
84 if (ctx == NULL) goto err;
85 q=BN_new();
86 if (q == NULL) goto err;
87
88 if (BN_is_word(dh->g,DH_GENERATOR_2))
89 {
90 l=BN_mod_word(dh->p,24);
91 if (l != 11) *ret|=DH_NOT_SUITABLE_GENERATOR;
92 }
93/* else if (BN_is_word(dh->g,DH_GENERATOR_3))
94 {
95 l=BN_mod_word(dh->p,12);
96 if (l != 5) *ret|=DH_NOT_SUITABLE_GENERATOR;
97 }*/
98 else if (BN_is_word(dh->g,DH_GENERATOR_5))
99 {
100 l=BN_mod_word(dh->p,10);
101 if ((l != 3) && (l != 7))
102 *ret|=DH_NOT_SUITABLE_GENERATOR;
103 }
104 else
105 *ret|=DH_UNABLE_TO_CHECK_GENERATOR;
106
107 if (!BN_is_prime(dh->p,BN_prime_checks,NULL,ctx,NULL))
108 *ret|=DH_CHECK_P_NOT_PRIME;
109 else
110 {
111 if (!BN_rshift1(q,dh->p)) goto err;
112 if (!BN_is_prime(q,BN_prime_checks,NULL,ctx,NULL))
113 *ret|=DH_CHECK_P_NOT_STRONG_PRIME;
114 }
115 ok=1;
116err:
117 if (ctx != NULL) BN_CTX_free(ctx);
118 if (q != NULL) BN_free(q);
119 return(ok);
120 }
diff --git a/src/lib/libcrypto/dh/dh_err.c b/src/lib/libcrypto/dh/dh_err.c
new file mode 100644
index 0000000000..9d5c06ac24
--- /dev/null
+++ b/src/lib/libcrypto/dh/dh_err.c
@@ -0,0 +1,96 @@
1/* lib/dh/dh_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "dh.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA DH_str_functs[]=
65 {
66{ERR_PACK(0,DH_F_DHPARAMS_PRINT,0), "DHparams_print"},
67{ERR_PACK(0,DH_F_DHPARAMS_PRINT_FP,0), "DHparams_print_fp"},
68{ERR_PACK(0,DH_F_DH_COMPUTE_KEY,0), "DH_compute_key"},
69{ERR_PACK(0,DH_F_DH_GENERATE_KEY,0), "DH_generate_key"},
70{ERR_PACK(0,DH_F_DH_GENERATE_PARAMETERS,0), "DH_generate_parameters"},
71{ERR_PACK(0,DH_F_DH_NEW,0), "DH_new"},
72{0,NULL},
73 };
74
75static ERR_STRING_DATA DH_str_reasons[]=
76 {
77{DH_R_NO_PRIVATE_VALUE ,"no private value"},
78{0,NULL},
79 };
80
81#endif
82
83void ERR_load_DH_strings()
84 {
85 static int init=1;
86
87 if (init);
88 {;
89 init=0;
90#ifndef NO_ERR
91 ERR_load_strings(ERR_LIB_DH,DH_str_functs);
92 ERR_load_strings(ERR_LIB_DH,DH_str_reasons);
93#endif
94
95 }
96 }
diff --git a/src/lib/libcrypto/dh/dh_gen.c b/src/lib/libcrypto/dh/dh_gen.c
new file mode 100644
index 0000000000..04c7046a7b
--- /dev/null
+++ b/src/lib/libcrypto/dh/dh_gen.c
@@ -0,0 +1,150 @@
1/* crypto/dh/dh_gen.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "dh.h"
63
64/* We generate DH parameters as follows
65 * find a prime q which is prime_len/2 bits long.
66 * p=(2*q)+1 or (p-1)/2 = q
67 * For this case, g is a generator if
68 * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
69 * Since the factors of p-1 are q and 2, we just need to check
70 * g^2 mod p != 1 and g^q mod p != 1.
71 *
72 * Having said all that,
73 * there is another special case method for the generators 2, 3 and 5.
74 * for 2, p mod 24 == 11
75 * for 3, p mod 12 == 5 <<<<< does not work for strong primes.
76 * for 5, p mod 10 == 3 or 7
77 *
78 * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
79 * special generators and for answering some of my questions.
80 *
81 * I've implemented the second simple method :-).
82 * Since DH should be using a strong prime (both p and q are prime),
83 * this generator function can take a very very long time to run.
84 */
85
86DH *DH_generate_parameters(prime_len,generator,callback,cb_arg)
87int prime_len;
88int generator;
89void (*callback)(P_I_I_P);
90char *cb_arg;
91 {
92 BIGNUM *p=NULL,*t1,*t2;
93 DH *ret=NULL;
94 int g,ok= -1;
95 BN_CTX *ctx=NULL;
96
97 ret=DH_new();
98 ctx=BN_CTX_new();
99 if (ctx == NULL) goto err;
100 t1=ctx->bn[0];
101 t2=ctx->bn[1];
102 ctx->tos=2;
103
104 if (generator == DH_GENERATOR_2)
105 {
106 BN_set_word(t1,24);
107 BN_set_word(t2,11);
108 g=2;
109 }
110#ifdef undef /* does not work for strong primes */
111 else if (generator == DH_GENERATOR_3)
112 {
113 BN_set_word(t1,12);
114 BN_set_word(t2,5);
115 g=3;
116 }
117#endif
118 else if (generator == DH_GENERATOR_5)
119 {
120 BN_set_word(t1,10);
121 BN_set_word(t2,3);
122 /* BN_set_word(t3,7); just have to miss
123 * out on these ones :-( */
124 g=5;
125 }
126 else
127 g=generator;
128
129 p=BN_generate_prime(prime_len,1,t1,t2,callback,cb_arg);
130 if (p == NULL) goto err;
131 if (callback != NULL) callback(3,0,cb_arg);
132 ret->p=p;
133 ret->g=BN_new();
134 if (!BN_set_word(ret->g,g)) goto err;
135 ok=1;
136err:
137 if (ok == -1)
138 {
139 DHerr(DH_F_DH_GENERATE_PARAMETERS,ERR_R_BN_LIB);
140 ok=0;
141 }
142
143 if (ctx != NULL) BN_CTX_free(ctx);
144 if (!ok && (ret != NULL))
145 {
146 DH_free(ret);
147 ret=NULL;
148 }
149 return(ret);
150 }
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c
new file mode 100644
index 0000000000..7576772bcd
--- /dev/null
+++ b/src/lib/libcrypto/dh/dh_key.c
@@ -0,0 +1,142 @@
1/* crypto/dh/dh_key.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "rand.h"
63#include "dh.h"
64
65int DH_generate_key(dh)
66DH *dh;
67 {
68 int ok=0;
69 unsigned int i;
70 BN_CTX *ctx=NULL;
71 BIGNUM *pub_key=NULL,*priv_key=NULL;
72
73 ctx=BN_CTX_new();
74 if (ctx == NULL) goto err;
75
76 if (dh->priv_key == NULL)
77 {
78 i=dh->length;
79 if (i == 0)
80 {
81 /* Make the number p-1 bits long */
82 i=BN_num_bits(dh->p)-1;
83 }
84 priv_key=BN_new();
85 if (priv_key == NULL) goto err;
86 if (!BN_rand(priv_key,i,0,0)) goto err;
87 }
88 else
89 priv_key=dh->priv_key;
90
91 if (dh->pub_key == NULL)
92 {
93 pub_key=BN_new();
94 if (pub_key == NULL) goto err;
95 }
96 else
97 pub_key=dh->pub_key;
98
99 if (!BN_mod_exp(pub_key,dh->g,priv_key,dh->p,ctx)) goto err;
100
101 dh->pub_key=pub_key;
102 dh->priv_key=priv_key;
103 ok=1;
104err:
105 if (ok != 1)
106 DHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB);
107
108 if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key);
109 if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);
110 if (ctx != NULL) BN_CTX_free(ctx);
111 return(ok);
112 }
113
114int DH_compute_key(key,pub_key,dh)
115unsigned char *key;
116BIGNUM *pub_key;
117DH *dh;
118 {
119 BN_CTX *ctx;
120 BIGNUM *tmp;
121 int ret= -1;
122
123 ctx=BN_CTX_new();
124 if (ctx == NULL) goto err;
125 tmp=ctx->bn[ctx->tos++];
126
127 if (dh->priv_key == NULL)
128 {
129 DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE);
130 goto err;
131 }
132 if (!BN_mod_exp(tmp,pub_key,dh->priv_key,dh->p,ctx))
133 {
134 DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB);
135 goto err;
136 }
137
138 ret=BN_bn2bin(tmp,key);
139err:
140 if (ctx != NULL) BN_CTX_free(ctx);
141 return(ret);
142 }
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c
new file mode 100644
index 0000000000..a300b38396
--- /dev/null
+++ b/src/lib/libcrypto/dh/dh_lib.c
@@ -0,0 +1,100 @@
1/* crypto/dh/dh_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "dh.h"
63
64char *DH_version="Diffie-Hellman part of SSLeay 0.9.0b 29-Jun-1998";
65
66DH *DH_new()
67 {
68 DH *ret;
69
70 ret=(DH *)Malloc(sizeof(DH));
71 if (ret == NULL)
72 {
73 DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE);
74 return(NULL);
75 }
76 ret->pad=0;
77 ret->version=0;
78 ret->p=NULL;
79 ret->g=NULL;
80 ret->length=0;
81 ret->pub_key=NULL;
82 ret->priv_key=NULL;
83 return(ret);
84 }
85
86void DH_free(r)
87DH *r;
88 {
89 if (r->p != NULL) BN_clear_free(r->p);
90 if (r->g != NULL) BN_clear_free(r->g);
91 if (r->pub_key != NULL) BN_clear_free(r->pub_key);
92 if (r->priv_key != NULL) BN_clear_free(r->priv_key);
93 Free(r);
94 }
95
96int DH_size(dh)
97DH *dh;
98 {
99 return(BN_num_bytes(dh->p));
100 }
diff --git a/src/lib/libcrypto/dsa/dsa.h b/src/lib/libcrypto/dsa/dsa.h
new file mode 100644
index 0000000000..1ca87c1cbe
--- /dev/null
+++ b/src/lib/libcrypto/dsa/dsa.h
@@ -0,0 +1,194 @@
1/* crypto/dsa/dsa.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/*
60 * The DSS routines are based on patches supplied by
61 * Steven Schoch <schoch@sheba.arc.nasa.gov>. He basically did the
62 * work and I have just tweaked them a little to fit into my
63 * stylistic vision for SSLeay :-) */
64
65#ifndef HEADER_DSA_H
66#define HEADER_DSA_H
67
68#ifdef __cplusplus
69extern "C" {
70#endif
71
72#include "bn.h"
73
74typedef struct dsa_st
75 {
76 /* This first variable is used to pick up errors where
77 * a DSA is passed instead of of a EVP_PKEY */
78 int pad;
79 int version;
80 int write_params;
81 BIGNUM *p;
82 BIGNUM *q; /* == 20 */
83 BIGNUM *g;
84
85 BIGNUM *pub_key; /* y public key */
86 BIGNUM *priv_key; /* x private key */
87
88 BIGNUM *kinv; /* Signing pre-calc */
89 BIGNUM *r; /* Signing pre-calc */
90
91 int references;
92 } DSA;
93
94#define DSAparams_dup(x) (DSA *)ASN1_dup((int (*)())i2d_DSAparams, \
95 (char *(*)())d2i_DSAparams,(char *)(x))
96#define d2i_DSAparams_fp(fp,x) (DSA *)ASN1_d2i_fp((char *(*)())DSA_new, \
97 (char *(*)())d2i_DSAparams,(fp),(unsigned char **)(x))
98#define i2d_DSAparams_fp(fp,x) ASN1_i2d_fp(i2d_DSAparams,(fp), \
99 (unsigned char *)(x))
100#define d2i_DSAparams_bio(bp,x) (DSA *)ASN1_d2i_bio((char *(*)())DSA_new, \
101 (char *(*)())d2i_DSAparams,(bp),(unsigned char **)(x))
102#define i2d_DSAparams_bio(bp,x) ASN1_i2d_bio(i2d_DSAparams,(bp), \
103 (unsigned char *)(x))
104
105#ifndef NOPROTO
106
107DSA * DSA_new(void);
108int DSA_size(DSA *);
109 /* next 4 return -1 on error */
110int DSA_sign_setup( DSA *dsa,BN_CTX *ctx_in,BIGNUM **kinvp,BIGNUM **rp);
111int DSA_sign(int type,unsigned char *dgst,int dlen,
112 unsigned char *sig, unsigned int *siglen, DSA *dsa);
113int DSA_verify(int type,unsigned char *dgst,int dgst_len,
114 unsigned char *sigbuf, int siglen, DSA *dsa);
115void DSA_free (DSA *r);
116
117void ERR_load_DSA_strings(void );
118
119DSA * d2i_DSAPublicKey(DSA **a, unsigned char **pp, long length);
120DSA * d2i_DSAPrivateKey(DSA **a, unsigned char **pp, long length);
121DSA * d2i_DSAparams(DSA **a, unsigned char **pp, long length);
122DSA * DSA_generate_parameters(int bits, unsigned char *seed,int seed_len,
123 int *counter_ret, unsigned long *h_ret,void
124 (*callback)(),char *cb_arg);
125int DSA_generate_key(DSA *a);
126int i2d_DSAPublicKey(DSA *a, unsigned char **pp);
127int i2d_DSAPrivateKey(DSA *a, unsigned char **pp);
128int i2d_DSAparams(DSA *a,unsigned char **pp);
129
130#ifdef HEADER_BIO_H
131int DSAparams_print(BIO *bp, DSA *x);
132int DSA_print(BIO *bp, DSA *x, int off);
133#endif
134#ifndef NO_FP_API
135int DSAparams_print_fp(FILE *fp, DSA *x);
136int DSA_print_fp(FILE *bp, DSA *x, int off);
137#endif
138
139int DSA_is_prime(BIGNUM *q,void (*callback)(),char *cb_arg);
140
141#else
142
143DSA * DSA_new();
144int DSA_size();
145int DSA_sign_setup();
146int DSA_sign();
147int DSA_verify();
148void DSA_free ();
149
150void ERR_load_DSA_strings();
151
152DSA * d2i_DSAPublicKey();
153DSA * d2i_DSAPrivateKey();
154DSA * d2i_DSAparams();
155DSA * DSA_generate_parameters();
156int DSA_generate_key();
157int i2d_DSAPublicKey();
158int i2d_DSAPrivateKey();
159int i2d_DSAparams();
160
161int DSA_is_prime();
162
163int DSAparams_print();
164int DSA_print();
165
166#ifndef NO_FP_API
167int DSAparams_print_fp();
168int DSA_print_fp();
169#endif
170
171#endif
172
173/* BEGIN ERROR CODES */
174/* Error codes for the DSA functions. */
175
176/* Function codes. */
177#define DSA_F_DSAPARAMS_PRINT 100
178#define DSA_F_DSAPARAMS_PRINT_FP 101
179#define DSA_F_DSA_IS_PRIME 102
180#define DSA_F_DSA_NEW 103
181#define DSA_F_DSA_PRINT 104
182#define DSA_F_DSA_PRINT_FP 105
183#define DSA_F_DSA_SIGN 106
184#define DSA_F_DSA_SIGN_SETUP 107
185#define DSA_F_DSA_VERIFY 108
186
187/* Reason codes. */
188#define DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 100
189
190#ifdef __cplusplus
191}
192#endif
193#endif
194
diff --git a/src/lib/libcrypto/dsa/dsa_err.c b/src/lib/libcrypto/dsa/dsa_err.c
new file mode 100644
index 0000000000..318e9f31aa
--- /dev/null
+++ b/src/lib/libcrypto/dsa/dsa_err.c
@@ -0,0 +1,99 @@
1/* lib/dsa/dsa_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "dsa.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA DSA_str_functs[]=
65 {
66{ERR_PACK(0,DSA_F_DSAPARAMS_PRINT,0), "DSAparams_print"},
67{ERR_PACK(0,DSA_F_DSAPARAMS_PRINT_FP,0), "DSAparams_print_fp"},
68{ERR_PACK(0,DSA_F_DSA_IS_PRIME,0), "DSA_is_prime"},
69{ERR_PACK(0,DSA_F_DSA_NEW,0), "DSA_new"},
70{ERR_PACK(0,DSA_F_DSA_PRINT,0), "DSA_print"},
71{ERR_PACK(0,DSA_F_DSA_PRINT_FP,0), "DSA_print_fp"},
72{ERR_PACK(0,DSA_F_DSA_SIGN,0), "DSA_sign"},
73{ERR_PACK(0,DSA_F_DSA_SIGN_SETUP,0), "DSA_sign_setup"},
74{ERR_PACK(0,DSA_F_DSA_VERIFY,0), "DSA_verify"},
75{0,NULL},
76 };
77
78static ERR_STRING_DATA DSA_str_reasons[]=
79 {
80{DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE ,"data too large for key size"},
81{0,NULL},
82 };
83
84#endif
85
86void ERR_load_DSA_strings()
87 {
88 static int init=1;
89
90 if (init);
91 {;
92 init=0;
93#ifndef NO_ERR
94 ERR_load_strings(ERR_LIB_DSA,DSA_str_functs);
95 ERR_load_strings(ERR_LIB_DSA,DSA_str_reasons);
96#endif
97
98 }
99 }
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c
new file mode 100644
index 0000000000..d7d30bf90a
--- /dev/null
+++ b/src/lib/libcrypto/dsa/dsa_gen.c
@@ -0,0 +1,328 @@
1/* crypto/dsa/dsa_gen.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#undef GENUINE_DSA
60
61#ifdef GENUINE_DSA
62#define HASH SHA
63#else
64#define HASH SHA1
65#endif
66
67#include <stdio.h>
68#include <time.h>
69#include "cryptlib.h"
70#include "sha.h"
71#include "bn.h"
72#include "dsa.h"
73#include "rand.h"
74
75DSA *DSA_generate_parameters(bits,seed_in,seed_len,counter_ret,h_ret,callback,
76 cb_arg)
77int bits;
78unsigned char *seed_in;
79int seed_len;
80int *counter_ret;
81unsigned long *h_ret;
82void (*callback)();
83char *cb_arg;
84 {
85 int ok=0;
86 unsigned char seed[SHA_DIGEST_LENGTH];
87 unsigned char md[SHA_DIGEST_LENGTH];
88 unsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH];
89 BIGNUM *r0,*W,*X,*c,*test;
90 BIGNUM *g=NULL,*q=NULL,*p=NULL;
91 int k,n=0,i,b,m=0;
92 int counter=0;
93 BN_CTX *ctx=NULL,*ctx2=NULL;
94 unsigned int h=2;
95 DSA *ret=NULL;
96
97 if (bits < 512) bits=512;
98 bits=(bits+63)/64*64;
99
100 if ((seed_in != NULL) && (seed_len == 20))
101 memcpy(seed,seed_in,seed_len);
102
103 ctx=BN_CTX_new();
104 if (ctx == NULL) goto err;
105 ctx2=BN_CTX_new();
106 if (ctx2 == NULL) goto err;
107 ret=DSA_new();
108 if (ret == NULL) goto err;
109 r0=ctx2->bn[0];
110 g=ctx2->bn[1];
111 W=ctx2->bn[2];
112 q=ctx2->bn[3];
113 X=ctx2->bn[4];
114 c=ctx2->bn[5];
115 p=ctx2->bn[6];
116 test=ctx2->bn[7];
117
118 BN_lshift(test,BN_value_one(),bits-1);
119
120 for (;;)
121 {
122 for (;;)
123 {
124 /* step 1 */
125 if (callback != NULL) callback(0,m++,cb_arg);
126
127 if (!seed_len)
128 RAND_bytes(seed,SHA_DIGEST_LENGTH);
129 else
130 seed_len=0;
131
132 memcpy(buf,seed,SHA_DIGEST_LENGTH);
133 memcpy(buf2,seed,SHA_DIGEST_LENGTH);
134 for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)
135 {
136 buf[i]++;
137 if (buf[i] != 0) break;
138 }
139
140 /* step 2 */
141 HASH(seed,SHA_DIGEST_LENGTH,md);
142 HASH(buf,SHA_DIGEST_LENGTH,buf2);
143 for (i=0; i<SHA_DIGEST_LENGTH; i++)
144 md[i]^=buf2[i];
145
146 /* step 3 */
147 md[0]|=0x80;
148 md[SHA_DIGEST_LENGTH-1]|=0x01;
149 if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) abort();
150
151 /* step 4 */
152 if (DSA_is_prime(q,callback,cb_arg) > 0) break;
153 /* do a callback call */
154 /* step 5 */
155 }
156
157 if (callback != NULL) callback(2,0,cb_arg);
158 if (callback != NULL) callback(3,0,cb_arg);
159
160 /* step 6 */
161 counter=0;
162
163 n=(bits-1)/160;
164 b=(bits-1)-n*160;
165
166 for (;;)
167 {
168 /* step 7 */
169 BN_zero(W);
170 for (k=0; k<=n; k++)
171 {
172 for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)
173 {
174 buf[i]++;
175 if (buf[i] != 0) break;
176 }
177
178 HASH(buf,SHA_DIGEST_LENGTH,md);
179
180 /* step 8 */
181 if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0)) abort();
182 BN_lshift(r0,r0,160*k);
183 BN_add(W,W,r0);
184 }
185
186 /* more of step 8 */
187 BN_mask_bits(W,bits-1);
188 BN_copy(X,W); /* this should be ok */
189 BN_add(X,X,test); /* this should be ok */
190
191 /* step 9 */
192 BN_lshift1(r0,q);
193 BN_mod(c,X,r0,ctx);
194 BN_sub(r0,c,BN_value_one());
195 BN_sub(p,X,r0);
196
197 /* step 10 */
198 if (BN_cmp(p,test) >= 0)
199 {
200 /* step 11 */
201 if (DSA_is_prime(p,callback,cb_arg) > 0)
202 goto end;
203 }
204
205 /* step 13 */
206 counter++;
207
208 /* step 14 */
209 if (counter >= 4096) break;
210
211 if (callback != NULL) callback(0,counter,cb_arg);
212 }
213 }
214end:
215 if (callback != NULL) callback(2,1,cb_arg);
216
217 /* We now need to gernerate g */
218 /* Set r0=(p-1)/q */
219 BN_sub(test,p,BN_value_one());
220 BN_div(r0,NULL,test,q,ctx);
221
222 BN_set_word(test,h);
223 for (;;)
224 {
225 /* g=test^r0%p */
226 BN_mod_exp(g,test,r0,p,ctx);
227 if (!BN_is_one(g)) break;
228 BN_add(test,test,BN_value_one());
229 h++;
230 }
231
232 if (callback != NULL) callback(3,1,cb_arg);
233
234 ok=1;
235err:
236 if (!ok)
237 {
238 if (ret != NULL) DSA_free(ret);
239 }
240 else
241 {
242 ret->p=BN_dup(p);
243 ret->q=BN_dup(q);
244 ret->g=BN_dup(g);
245 if ((m > 1) && (seed_in != NULL)) memcpy(seed_in,seed,20);
246 if (counter_ret != NULL) *counter_ret=counter;
247 if (h_ret != NULL) *h_ret=h;
248 }
249 BN_CTX_free(ctx);
250 BN_CTX_free(ctx2);
251 return(ok?ret:NULL);
252 }
253
254int DSA_is_prime(w, callback,cb_arg)
255BIGNUM *w;
256void (*callback)();
257char *cb_arg;
258 {
259 int ok= -1,j,i,n;
260 BN_CTX *ctx=NULL,*ctx2=NULL;
261 BIGNUM *w_1,*b,*m,*z;
262 int a;
263
264 if (!BN_is_bit_set(w,0)) return(0);
265
266 ctx=BN_CTX_new();
267 if (ctx == NULL) goto err;
268 ctx2=BN_CTX_new();
269 if (ctx2 == NULL) goto err;
270
271 m= ctx2->bn[2];
272 b= ctx2->bn[3];
273 z= ctx2->bn[4];
274 w_1=ctx2->bn[5];
275
276 /* step 1 */
277 n=50;
278
279 /* step 2 */
280 if (!BN_sub(w_1,w,BN_value_one())) goto err;
281 for (a=1; !BN_is_bit_set(w_1,a); a++)
282 ;
283 if (!BN_rshift(m,w_1,a)) goto err;
284
285 for (i=1; i < n; i++)
286 {
287 /* step 3 */
288 BN_rand(b,BN_num_bits(w)-2/*-1*/,0,0);
289 BN_set_word(b,0x10001L);
290
291 /* step 4 */
292 j=0;
293 if (!BN_mod_exp(z,b,m,w,ctx)) goto err;
294
295 /* step 5 */
296 for (;;)
297 {
298 if (((j == 0) && BN_is_one(z)) || (BN_cmp(z,w_1) == 0))
299 break;
300
301 /* step 6 */
302 if ((j > 0) && BN_is_one(z))
303 {
304 ok=0;
305 goto err;
306 }
307
308 j++;
309 if (j >= a)
310 {
311 ok=0;
312 goto err;
313 }
314
315 if (!BN_mod_mul(z,z,z,w,ctx)) goto err;
316 if (callback != NULL) callback(1,j,cb_arg);
317 }
318 }
319
320 ok=1;
321err:
322 if (ok == -1) DSAerr(DSA_F_DSA_IS_PRIME,ERR_R_BN_LIB);
323 BN_CTX_free(ctx);
324 BN_CTX_free(ctx2);
325
326 return(ok);
327 }
328
diff --git a/src/lib/libcrypto/dsa/dsa_key.c b/src/lib/libcrypto/dsa/dsa_key.c
new file mode 100644
index 0000000000..d51ed9395f
--- /dev/null
+++ b/src/lib/libcrypto/dsa/dsa_key.c
@@ -0,0 +1,112 @@
1/* crypto/dsa/dsa_key.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include "cryptlib.h"
62#include "sha.h"
63#include "bn.h"
64#include "dsa.h"
65#include "rand.h"
66
67int DSA_generate_key(dsa)
68DSA *dsa;
69 {
70 int ok=0;
71 unsigned int i;
72 BN_CTX *ctx=NULL;
73 BIGNUM *pub_key=NULL,*priv_key=NULL;
74
75 if ((ctx=BN_CTX_new()) == NULL) goto err;
76
77 if (dsa->priv_key == NULL)
78 {
79 if ((priv_key=BN_new()) == NULL) goto err;
80 }
81 else
82 priv_key=dsa->priv_key;
83
84 i=BN_num_bits(dsa->q);
85 for (;;)
86 {
87 BN_rand(priv_key,i,1,0);
88 if (BN_cmp(priv_key,dsa->q) >= 0)
89 BN_sub(priv_key,priv_key,dsa->q);
90 if (!BN_is_zero(priv_key)) break;
91 }
92
93 if (dsa->pub_key == NULL)
94 {
95 if ((pub_key=BN_new()) == NULL) goto err;
96 }
97 else
98 pub_key=dsa->pub_key;
99
100 if (!BN_mod_exp(pub_key,dsa->g,priv_key,dsa->p,ctx)) goto err;
101
102 dsa->priv_key=priv_key;
103 dsa->pub_key=pub_key;
104 ok=1;
105
106err:
107 if ((pub_key != NULL) && (dsa->pub_key == NULL)) BN_free(pub_key);
108 if ((priv_key != NULL) && (dsa->priv_key == NULL)) BN_free(priv_key);
109 if (ctx != NULL) BN_CTX_free(ctx);
110 return(ok);
111 }
112
diff --git a/src/lib/libcrypto/dsa/dsa_lib.c b/src/lib/libcrypto/dsa/dsa_lib.c
new file mode 100644
index 0000000000..b647257f9f
--- /dev/null
+++ b/src/lib/libcrypto/dsa/dsa_lib.c
@@ -0,0 +1,145 @@
1/* crypto/dsa/dsa_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Origional version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
60
61#include <stdio.h>
62#include "cryptlib.h"
63#include "bn.h"
64#include "dsa.h"
65#include "asn1.h"
66
67char *DSA_version="\0DSA part of SSLeay 0.9.0b 29-Jun-1998";
68
69DSA *DSA_new()
70 {
71 DSA *ret;
72
73 ret=(DSA *)Malloc(sizeof(DSA));
74 if (ret == NULL)
75 {
76 DSAerr(DSA_F_DSA_NEW,ERR_R_MALLOC_FAILURE);
77 return(NULL);
78 }
79 ret->pad=0;
80 ret->version=0;
81 ret->write_params=1;
82 ret->p=NULL;
83 ret->q=NULL;
84 ret->g=NULL;
85
86 ret->pub_key=NULL;
87 ret->priv_key=NULL;
88
89 ret->kinv=NULL;
90 ret->r=NULL;
91
92 ret->references=1;
93 return(ret);
94 }
95
96void DSA_free(r)
97DSA *r;
98 {
99 int i;
100
101 if (r == NULL) return;
102
103 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA);
104#ifdef REF_PRINT
105 REF_PRINT("DSA",r);
106#endif
107 if (i > 0) return;
108#ifdef REF_CHECK
109 if (i < 0)
110 {
111 fprintf(stderr,"DSA_free, bad reference count\n");
112 abort();
113 }
114#endif
115
116 if (r->p != NULL) BN_clear_free(r->p);
117 if (r->q != NULL) BN_clear_free(r->q);
118 if (r->g != NULL) BN_clear_free(r->g);
119 if (r->pub_key != NULL) BN_clear_free(r->pub_key);
120 if (r->priv_key != NULL) BN_clear_free(r->priv_key);
121 if (r->kinv != NULL) BN_clear_free(r->kinv);
122 if (r->r != NULL) BN_clear_free(r->r);
123 Free(r);
124 }
125
126int DSA_size(r)
127DSA *r;
128 {
129 int ret,i;
130 ASN1_INTEGER bs;
131 unsigned char buf[4];
132
133 i=BN_num_bits(r->q);
134 bs.length=(i+7)/8;
135 bs.data=buf;
136 bs.type=V_ASN1_INTEGER;
137 /* If the top bit is set the asn1 encoding is 1 larger. */
138 buf[0]=0xff;
139
140 i=i2d_ASN1_INTEGER(&bs,NULL);
141 i+=i; /* r and s */
142 ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
143 return(ret);
144 }
145
diff --git a/src/lib/libcrypto/dsa/dsa_sign.c b/src/lib/libcrypto/dsa/dsa_sign.c
new file mode 100644
index 0000000000..6ca1c318f2
--- /dev/null
+++ b/src/lib/libcrypto/dsa/dsa_sign.c
@@ -0,0 +1,215 @@
1/* crypto/dsa/dsa_sign.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Origional version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
60
61#include <stdio.h>
62#include "cryptlib.h"
63#include "bn.h"
64#include "dsa.h"
65#include "rand.h"
66#include "asn1.h"
67
68/* data has already been hashed (probably with SHA or SHA-1). */
69/* DSAerr(DSA_F_DSA_SIGN,DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); */
70
71int DSA_sign(type,dgst,dlen,sig,siglen,dsa)
72int type;
73unsigned char *dgst;
74int dlen;
75unsigned char *sig; /* out */
76unsigned int *siglen; /* out */
77DSA *dsa;
78 {
79 BIGNUM *kinv=NULL,*r=NULL;
80 BIGNUM *m=NULL;
81 BIGNUM *xr=NULL,*s=NULL;
82 BN_CTX *ctx=NULL;
83 unsigned char *p;
84 int i,len=0,ret=0,reason=ERR_R_BN_LIB;
85 ASN1_INTEGER rbs,sbs;
86 MS_STATIC unsigned char rbuf[50]; /* assuming r is 20 bytes +extra */
87 MS_STATIC unsigned char sbuf[50]; /* assuming s is 20 bytes +extra */
88
89 i=BN_num_bytes(dsa->q); /* should be 20 */
90 if ((dlen > i) || (dlen > 50))
91 {
92 reason=DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE;
93 goto err;
94 }
95
96 ctx=BN_CTX_new();
97 if (ctx == NULL) goto err;
98
99 if ((dsa->kinv == NULL) || (dsa->r == NULL))
100 {
101 if (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err;
102 }
103 else
104 {
105 kinv=dsa->kinv;
106 dsa->kinv=NULL;
107 r=dsa->r;
108 dsa->r=NULL;
109 }
110
111 m=BN_new();
112 xr=BN_new();
113 s=BN_new();
114 if (m == NULL || xr == NULL || s == NULL) goto err;
115
116 if (BN_bin2bn(dgst,dlen,m) == NULL) goto err;
117
118 /* Compute s = inv(k) (m + xr) mod q */
119 if (!BN_mul(xr, dsa->priv_key, r)) goto err; /* s = xr */
120 if (!BN_add(s, xr, m)) goto err; /* s = m + xr */
121 if (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err;
122
123 /*
124 * Now create a ASN.1 sequence of the integers R and S.
125 */
126 rbs.data=rbuf;
127 sbs.data=sbuf;
128 rbs.type = V_ASN1_INTEGER;
129 sbs.type = V_ASN1_INTEGER;
130 rbs.length=BN_bn2bin(r,rbs.data);
131 sbs.length=BN_bn2bin(s,sbs.data);
132
133 len =i2d_ASN1_INTEGER(&rbs,NULL);
134 len+=i2d_ASN1_INTEGER(&sbs,NULL);
135
136 p=sig;
137 ASN1_put_object(&p,1,len,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
138 i2d_ASN1_INTEGER(&rbs,&p);
139 i2d_ASN1_INTEGER(&sbs,&p);
140 *siglen=(p-sig);
141 ret=1;
142err:
143 if (!ret) DSAerr(DSA_F_DSA_SIGN,reason);
144
145#if 1 /* do the right thing :-) */
146 if (kinv != NULL) BN_clear_free(kinv);
147 if (r != NULL) BN_clear_free(r);
148#endif
149 if (ctx != NULL) BN_CTX_free(ctx);
150 if (m != NULL) BN_clear_free(m);
151 if (xr != NULL) BN_clear_free(xr);
152 if (s != NULL) BN_clear_free(s);
153 return(ret);
154 }
155
156int DSA_sign_setup(dsa,ctx_in,kinvp,rp)
157DSA *dsa;
158BN_CTX *ctx_in;
159BIGNUM **kinvp;
160BIGNUM **rp;
161 {
162 BN_CTX *ctx;
163 BIGNUM *k=NULL,*kinv=NULL,*r=NULL;
164 int ret=0;
165
166 if (ctx_in == NULL)
167 {
168 if ((ctx=BN_CTX_new()) == NULL) goto err;
169 }
170 else
171 ctx=ctx_in;
172
173 r=BN_new();
174 k=BN_new();
175 if ((r == NULL) || (k == NULL))
176 goto err;
177 kinv=NULL;
178
179 if (r == NULL) goto err;
180
181 /* Get random k */
182 for (;;)
183 {
184 if (!BN_rand(k, BN_num_bits(dsa->q), 1, 0)) goto err;
185 if (BN_cmp(k,dsa->q) >= 0)
186 BN_sub(k,k,dsa->q);
187 if (!BN_is_zero(k)) break;
188 }
189
190 /* Compute r = (g^k mod p) mod q */
191 if (!BN_mod_exp(r,dsa->g,k,dsa->p,ctx)) goto err;
192 if (!BN_mod(r,r,dsa->q,ctx)) goto err;
193
194 /* Compute part of 's = inv(k) (m + xr) mod q' */
195 if ((kinv=BN_mod_inverse(k,dsa->q,ctx)) == NULL) goto err;
196
197 if (*kinvp != NULL) BN_clear_free(*kinvp);
198 *kinvp=kinv;
199 kinv=NULL;
200 if (*rp != NULL) BN_clear_free(*rp);
201 *rp=r;
202 ret=1;
203err:
204 if (!ret)
205 {
206 DSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB);
207 if (kinv != NULL) BN_clear_free(kinv);
208 if (r != NULL) BN_clear_free(r);
209 }
210 if (ctx_in == NULL) BN_CTX_free(ctx);
211 if (k != NULL) BN_clear_free(k);
212 if (kinv != NULL) BN_clear_free(kinv);
213 return(ret);
214 }
215
diff --git a/src/lib/libcrypto/dsa/dsa_vrf.c b/src/lib/libcrypto/dsa/dsa_vrf.c
new file mode 100644
index 0000000000..0f860984ed
--- /dev/null
+++ b/src/lib/libcrypto/dsa/dsa_vrf.c
@@ -0,0 +1,152 @@
1/* crypto/dsa/dsa_vrf.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Origional version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
60
61#include <stdio.h>
62#include "cryptlib.h"
63#include "bn.h"
64#include "dsa.h"
65#include "rand.h"
66#include "asn1.h"
67#include "asn1_mac.h"
68
69/* data has already been hashed (probably with SHA or SHA-1). */
70/* returns
71 * 1: correct signature
72 * 0: incorrect signature
73 * -1: error
74 */
75int DSA_verify(type,dgst,dgst_len,sigbuf,siglen, dsa)
76int type;
77unsigned char *dgst;
78int dgst_len;
79unsigned char *sigbuf;
80int siglen;
81DSA *dsa;
82 {
83 /* The next 3 are used by the M_ASN1 macros */
84 long length=siglen;
85 ASN1_CTX c;
86 unsigned char **pp= &sigbuf;
87 BN_CTX *ctx;
88 BIGNUM *r=NULL;
89 BIGNUM *t1=NULL,*t2=NULL;
90 BIGNUM *u1=NULL,*u2=NULL;
91 ASN1_INTEGER *bs=NULL;
92 int ret = -1;
93
94 ctx=BN_CTX_new();
95 if (ctx == NULL) goto err;
96
97 t1=BN_new();
98 t2=BN_new();
99 if (t1 == NULL || t2 == NULL) goto err;
100
101 M_ASN1_D2I_Init();
102 M_ASN1_D2I_start_sequence();
103 M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);
104 if ((r=BN_bin2bn(bs->data,bs->length,NULL)) == NULL) goto err_bn;
105 M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);
106 if ((u1=BN_bin2bn(bs->data,bs->length,NULL)) == NULL) goto err_bn;
107 if (!asn1_Finish(&c)) goto err;
108
109 /* Calculate W = inv(S) mod Q
110 * save W in u2 */
111 if ((u2=BN_mod_inverse(u1,dsa->q,ctx)) == NULL) goto err_bn;
112
113 /* save M in u1 */
114 if (BN_bin2bn(dgst,dgst_len,u1) == NULL) goto err_bn;
115
116 /* u1 = M * w mod q */
117 if (!BN_mod_mul(u1,u1,u2,dsa->q,ctx)) goto err_bn;
118
119 /* u2 = r * w mod q */
120 if (!BN_mod_mul(u2,r,u2,dsa->q,ctx)) goto err_bn;
121
122 /* v = ( g^u1 * y^u2 mod p ) mod q */
123 /* let t1 = g ^ u1 mod p */
124 if (!BN_mod_exp(t1,dsa->g,u1,dsa->p,ctx)) goto err_bn;
125 /* let t2 = y ^ u2 mod p */
126 if (!BN_mod_exp(t2,dsa->pub_key,u2,dsa->p,ctx)) goto err_bn;
127 /* let u1 = t1 * t2 mod p */
128 if (!BN_mod_mul(u1,t1,t2,dsa->p,ctx)) goto err_bn;
129 /* let u1 = u1 mod q */
130 if (!BN_mod(u1,u1,dsa->q,ctx)) goto err_bn;
131 /* V is now in u1. If the signature is correct, it will be
132 * equal to R. */
133 ret=(BN_ucmp(u1, r) == 0);
134 if (0)
135 {
136err: /* ASN1 error */
137 DSAerr(DSA_F_DSA_VERIFY,c.error);
138 }
139 if (0)
140 {
141err_bn: /* BN error */
142 DSAerr(DSA_F_DSA_VERIFY,ERR_R_BN_LIB);
143 }
144 if (ctx != NULL) BN_CTX_free(ctx);
145 if (r != NULL) BN_free(r);
146 if (t1 != NULL) BN_free(t1);
147 if (t2 != NULL) BN_free(t2);
148 if (u1 != NULL) BN_free(u1);
149 if (u2 != NULL) BN_free(u2);
150 if (bs != NULL) ASN1_BIT_STRING_free(bs);
151 return(ret);
152 }
diff --git a/src/lib/libcrypto/err/err.c b/src/lib/libcrypto/err/err.c
new file mode 100644
index 0000000000..5aef6a1259
--- /dev/null
+++ b/src/lib/libcrypto/err/err.c
@@ -0,0 +1,642 @@
1/* crypto/err/err.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "lhash.h"
61#include "crypto.h"
62#include "cryptlib.h"
63#include "buffer.h"
64#include "err.h"
65#include "crypto.h"
66
67
68static LHASH *error_hash=NULL;
69static LHASH *thread_hash=NULL;
70
71#ifndef NOPROTO
72static unsigned long err_hash(ERR_STRING_DATA *a);
73static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b);
74static unsigned long pid_hash(ERR_STATE *pid);
75static int pid_cmp(ERR_STATE *a,ERR_STATE *pid);
76static unsigned long get_error_values(int inc,char **file,int *line,
77 char **data,int *flags);
78static void ERR_STATE_free(ERR_STATE *s);
79#else
80static unsigned long err_hash();
81static int err_cmp();
82static unsigned long pid_hash();
83static int pid_cmp();
84static void ERR_STATE_free();
85ERR_STATE *s;
86#endif
87
88#ifndef NO_ERR
89static ERR_STRING_DATA ERR_str_libraries[]=
90 {
91{ERR_PACK(ERR_LIB_NONE,0,0) ,"unknown library"},
92{ERR_PACK(ERR_LIB_SYS,0,0) ,"system library"},
93{ERR_PACK(ERR_LIB_BN,0,0) ,"bignum routines"},
94{ERR_PACK(ERR_LIB_RSA,0,0) ,"rsa routines"},
95{ERR_PACK(ERR_LIB_DH,0,0) ,"Diffie-Hellman routines"},
96{ERR_PACK(ERR_LIB_EVP,0,0) ,"digital envelope routines"},
97{ERR_PACK(ERR_LIB_BUF,0,0) ,"memory buffer routines"},
98{ERR_PACK(ERR_LIB_BIO,0,0) ,"BIO routines"},
99{ERR_PACK(ERR_LIB_OBJ,0,0) ,"object identifier routines"},
100{ERR_PACK(ERR_LIB_PEM,0,0) ,"PEM routines"},
101{ERR_PACK(ERR_LIB_ASN1,0,0) ,"asn1 encoding routines"},
102{ERR_PACK(ERR_LIB_X509,0,0) ,"x509 certificate routines"},
103{ERR_PACK(ERR_LIB_CONF,0,0) ,"configuation file routines"},
104{ERR_PACK(ERR_LIB_METH,0,0) ,"X509 lookup 'method' routines"},
105{ERR_PACK(ERR_LIB_SSL,0,0) ,"SSL routines"},
106{ERR_PACK(ERR_LIB_RSAREF,0,0) ,"RSAref routines"},
107{ERR_PACK(ERR_LIB_PROXY,0,0) ,"Proxy routines"},
108{ERR_PACK(ERR_LIB_BIO,0,0) ,"BIO routines"},
109{ERR_PACK(ERR_LIB_PKCS7,0,0) ,"PKCS7 routines"},
110{0,NULL},
111 };
112
113static ERR_STRING_DATA ERR_str_functs[]=
114 {
115 {ERR_PACK(0,SYS_F_FOPEN,0), "fopen"},
116 {ERR_PACK(0,SYS_F_CONNECT,0), "connect"},
117 {ERR_PACK(0,SYS_F_GETSERVBYNAME,0), "getservbyname"},
118 {ERR_PACK(0,SYS_F_SOCKET,0), "socket"},
119 {ERR_PACK(0,SYS_F_IOCTLSOCKET,0), "ioctlsocket"},
120 {ERR_PACK(0,SYS_F_BIND,0), "bind"},
121 {ERR_PACK(0,SYS_F_LISTEN,0), "listen"},
122 {ERR_PACK(0,SYS_F_ACCEPT,0), "accept"},
123#ifdef WINDOWS
124 {ERR_PACK(0,SYS_F_WSASTARTUP,0), "WSAstartup"},
125#endif
126 {0,NULL},
127 };
128
129static ERR_STRING_DATA ERR_str_reasons[]=
130 {
131{ERR_R_FATAL ,"fatal"},
132{ERR_R_SYS_LIB ,"system lib"},
133{ERR_R_BN_LIB ,"BN lib"},
134{ERR_R_RSA_LIB ,"RSA lib"},
135{ERR_R_DH_LIB ,"DH lib"},
136{ERR_R_EVP_LIB ,"EVP lib"},
137{ERR_R_BUF_LIB ,"BUF lib"},
138{ERR_R_BIO_LIB ,"BIO lib"},
139{ERR_R_OBJ_LIB ,"OBJ lib"},
140{ERR_R_PEM_LIB ,"PEM lib"},
141{ERR_R_X509_LIB ,"X509 lib"},
142{ERR_R_METH_LIB ,"METH lib"},
143{ERR_R_ASN1_LIB ,"ASN1 lib"},
144{ERR_R_CONF_LIB ,"CONF lib"},
145{ERR_R_SSL_LIB ,"SSL lib"},
146{ERR_R_PROXY_LIB ,"PROXY lib"},
147{ERR_R_BIO_LIB ,"BIO lib"},
148{ERR_R_PKCS7_LIB ,"PKCS7 lib"},
149{ERR_R_MALLOC_FAILURE ,"Malloc failure"},
150{ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED ,"called a fuction you should not call"},
151{0,NULL},
152 };
153#endif
154
155#define err_clear_data(p,i) \
156 if (((p)->err_data[i] != NULL) && \
157 (p)->err_data_flags[i] & ERR_TXT_MALLOCED) \
158 { \
159 Free((p)->err_data[i]); \
160 (p)->err_data[i]=NULL; \
161 } \
162 (p)->err_data_flags[i]=0;
163
164static void ERR_STATE_free(s)
165ERR_STATE *s;
166 {
167 int i;
168
169 for (i=0; i<ERR_NUM_ERRORS; i++)
170 {
171 err_clear_data(s,i);
172 }
173 Free(s);
174 }
175
176void ERR_load_ERR_strings()
177 {
178 static int init=1;
179
180 if (init)
181 {
182 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
183 if (init == 0)
184 {
185 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
186 return;
187 }
188 init=0;
189 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
190
191#ifndef NO_ERR
192 ERR_load_strings(0,ERR_str_libraries);
193 ERR_load_strings(0,ERR_str_reasons);
194 ERR_load_strings(ERR_LIB_SYS,ERR_str_functs);
195#endif
196 }
197 }
198
199void ERR_load_strings(lib,str)
200int lib;
201ERR_STRING_DATA *str;
202 {
203 if (error_hash == NULL)
204 {
205 CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH);
206 error_hash=lh_new(err_hash,err_cmp);
207 if (error_hash == NULL)
208 {
209 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
210 return;
211 }
212 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
213
214 ERR_load_ERR_strings();
215 }
216
217 CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH);
218 while (str->error)
219 {
220 str->error|=ERR_PACK(lib,0,0);
221 lh_insert(error_hash,(char *)str);
222 str++;
223 }
224 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
225 }
226
227void ERR_free_strings()
228 {
229 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
230
231 if (error_hash != NULL)
232 {
233 lh_free(error_hash);
234 error_hash=NULL;
235 }
236
237 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
238 }
239
240/********************************************************/
241
242void ERR_put_error(lib,func,reason,file,line)
243int lib,func,reason;
244char *file;
245int line;
246 {
247 ERR_STATE *es;
248
249 es=ERR_get_state();
250
251 es->top=(es->top+1)%ERR_NUM_ERRORS;
252 if (es->top == es->bottom)
253 es->bottom=(es->bottom+1)%ERR_NUM_ERRORS;
254 es->err_buffer[es->top]=ERR_PACK(lib,func,reason);
255 es->err_file[es->top]=file;
256 es->err_line[es->top]=line;
257 err_clear_data(es,es->top);
258 }
259
260void ERR_clear_error()
261 {
262 ERR_STATE *es;
263
264 es=ERR_get_state();
265
266#if 0
267 /* hmm... is this needed */
268 for (i=0; i<ERR_NUM_ERRORS; i++)
269 {
270 es->err_buffer[i]=0;
271 es->err_file[i]=NULL;
272 es->err_line[i]= -1;
273 err_clear_data(es,i);
274 }
275#endif
276 es->top=es->bottom=0;
277 }
278
279
280unsigned long ERR_get_error()
281 { return(get_error_values(1,NULL,NULL,NULL,NULL)); }
282
283unsigned long ERR_get_error_line(file,line)
284char **file;
285int *line;
286 { return(get_error_values(1,file,line,NULL,NULL)); }
287
288unsigned long ERR_get_error_line_data(file,line,data,flags)
289char **file;
290int *line;
291char **data;
292int *flags;
293 { return(get_error_values(1,file,line,data,flags)); }
294
295unsigned long ERR_peek_error()
296 { return(get_error_values(0,NULL,NULL,NULL,NULL)); }
297
298unsigned long ERR_peek_error_line(file,line)
299char **file;
300int *line;
301 { return(get_error_values(0,file,line,NULL,NULL)); }
302
303unsigned long ERR_peek_error_line_data(file,line,data,flags)
304char **file;
305int *line;
306char **data;
307int *flags;
308 { return(get_error_values(0,file,line,data,flags)); }
309
310static unsigned long get_error_values(inc,file,line,data,flags)
311int inc;
312char **file;
313int *line;
314char **data;
315int *flags;
316 {
317 int i=0;
318 ERR_STATE *es;
319 unsigned long ret;
320
321 es=ERR_get_state();
322
323 if (es->bottom == es->top) return(0);
324 i=(es->bottom+1)%ERR_NUM_ERRORS;
325
326 ret=es->err_buffer[i];
327 if (inc)
328 {
329 es->bottom=i;
330 es->err_buffer[i]=0;
331 }
332
333 if ((file != NULL) && (line != NULL))
334 {
335 if (es->err_file[i] == NULL)
336 {
337 *file="NA";
338 if (line != NULL) *line=0;
339 }
340 else
341 {
342 *file=es->err_file[i];
343 if (line != NULL) *line=es->err_line[i];
344 }
345 }
346
347 if (data != NULL)
348 {
349 if (es->err_data[i] == NULL)
350 {
351 *data="";
352 if (flags != NULL) *flags=0;
353 }
354 else
355 {
356 *data=es->err_data[i];
357 if (flags != NULL) *flags=es->err_data_flags[i];
358 }
359 }
360 return(ret);
361 }
362
363/* BAD for multi-threaded, uses a local buffer if ret == NULL */
364char *ERR_error_string(e,ret)
365unsigned long e;
366char *ret;
367 {
368 static char buf[256];
369 char *ls,*fs,*rs;
370 unsigned long l,f,r;
371 int i;
372
373 l=ERR_GET_LIB(e);
374 f=ERR_GET_FUNC(e);
375 r=ERR_GET_REASON(e);
376
377 ls=ERR_lib_error_string(e);
378 fs=ERR_func_error_string(e);
379 rs=ERR_reason_error_string(e);
380
381 if (ret == NULL) ret=buf;
382
383 sprintf(&(ret[0]),"error:%08lX:",e);
384 i=strlen(ret);
385 if (ls == NULL)
386 sprintf(&(ret[i]),":lib(%lu) ",l);
387 else sprintf(&(ret[i]),"%s",ls);
388 i=strlen(ret);
389 if (fs == NULL)
390 sprintf(&(ret[i]),":func(%lu) ",f);
391 else sprintf(&(ret[i]),":%s",fs);
392 i=strlen(ret);
393 if (rs == NULL)
394 sprintf(&(ret[i]),":reason(%lu)",r);
395 else sprintf(&(ret[i]),":%s",rs);
396
397 return(ret);
398 }
399
400LHASH *ERR_get_string_table()
401 {
402 return(error_hash);
403 }
404
405LHASH *ERR_get_err_state_table()
406 {
407 return(thread_hash);
408 }
409
410char *ERR_lib_error_string(e)
411unsigned long e;
412 {
413 ERR_STRING_DATA d,*p=NULL;
414 unsigned long l;
415
416 l=ERR_GET_LIB(e);
417
418 CRYPTO_r_lock(CRYPTO_LOCK_ERR_HASH);
419
420 if (error_hash != NULL)
421 {
422 d.error=ERR_PACK(l,0,0);
423 p=(ERR_STRING_DATA *)lh_retrieve(error_hash,(char *)&d);
424 }
425
426 CRYPTO_r_unlock(CRYPTO_LOCK_ERR_HASH);
427
428 return((p == NULL)?NULL:p->string);
429 }
430
431char *ERR_func_error_string(e)
432unsigned long e;
433 {
434 ERR_STRING_DATA d,*p=NULL;
435 unsigned long l,f;
436
437 l=ERR_GET_LIB(e);
438 f=ERR_GET_FUNC(e);
439
440 CRYPTO_r_lock(CRYPTO_LOCK_ERR_HASH);
441
442 if (error_hash != NULL)
443 {
444 d.error=ERR_PACK(l,f,0);
445 p=(ERR_STRING_DATA *)lh_retrieve(error_hash,(char *)&d);
446 }
447
448 CRYPTO_r_unlock(CRYPTO_LOCK_ERR_HASH);
449
450 return((p == NULL)?NULL:p->string);
451 }
452
453char *ERR_reason_error_string(e)
454unsigned long e;
455 {
456 ERR_STRING_DATA d,*p=NULL;
457 unsigned long l,r;
458
459 l=ERR_GET_LIB(e);
460 r=ERR_GET_REASON(e);
461
462 CRYPTO_r_lock(CRYPTO_LOCK_ERR_HASH);
463
464 if (error_hash != NULL)
465 {
466 d.error=ERR_PACK(l,0,r);
467 p=(ERR_STRING_DATA *)lh_retrieve(error_hash,(char *)&d);
468 if (p == NULL)
469 {
470 d.error=ERR_PACK(0,0,r);
471 p=(ERR_STRING_DATA *)lh_retrieve(error_hash,
472 (char *)&d);
473 }
474 }
475
476 CRYPTO_r_unlock(CRYPTO_LOCK_ERR_HASH);
477
478 return((p == NULL)?NULL:p->string);
479 }
480
481static unsigned long err_hash(a)
482ERR_STRING_DATA *a;
483 {
484 unsigned long ret,l;
485
486 l=a->error;
487 ret=l^ERR_GET_LIB(l)^ERR_GET_FUNC(l);
488 return(ret^ret%19*13);
489 }
490
491static int err_cmp(a,b)
492ERR_STRING_DATA *a,*b;
493 {
494 return((int)(a->error-b->error));
495 }
496
497static unsigned long pid_hash(a)
498ERR_STATE *a;
499 {
500 return(a->pid*13);
501 }
502
503static int pid_cmp(a,b)
504ERR_STATE *a,*b;
505 {
506 return((int)((long)a->pid - (long)b->pid));
507 }
508
509void ERR_remove_state(pid)
510unsigned long pid;
511 {
512 ERR_STATE *p,tmp;
513
514 if (thread_hash == NULL)
515 return;
516 if (pid == 0)
517 pid=(unsigned long)CRYPTO_thread_id();
518 tmp.pid=pid;
519 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
520 p=(ERR_STATE *)lh_delete(thread_hash,(char *)&tmp);
521 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
522
523 if (p != NULL) ERR_STATE_free(p);
524 }
525
526ERR_STATE *ERR_get_state()
527 {
528 static ERR_STATE fallback;
529 ERR_STATE *ret=NULL,tmp,*tmpp;
530 int i;
531 unsigned long pid;
532
533 pid=(unsigned long)CRYPTO_thread_id();
534
535 CRYPTO_r_lock(CRYPTO_LOCK_ERR);
536 if (thread_hash == NULL)
537 {
538 CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
539 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
540 if (thread_hash == NULL)
541 {
542 thread_hash=lh_new(pid_hash,pid_cmp);
543 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
544 if (thread_hash == NULL) return(&fallback);
545 }
546 else
547 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
548 }
549 else
550 {
551 tmp.pid=pid;
552 ret=(ERR_STATE *)lh_retrieve(thread_hash,(char *)&tmp);
553 CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
554 }
555
556 /* ret == the error state, if NULL, make a new one */
557 if (ret == NULL)
558 {
559 ret=(ERR_STATE *)Malloc(sizeof(ERR_STATE));
560 if (ret == NULL) return(&fallback);
561 ret->pid=pid;
562 ret->top=0;
563 ret->bottom=0;
564 for (i=0; i<ERR_NUM_ERRORS; i++)
565 {
566 ret->err_data[i]=NULL;
567 ret->err_data_flags[i]=0;
568 }
569 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
570 tmpp=(ERR_STATE *)lh_insert(thread_hash,(char *)ret);
571 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
572 if (tmpp != NULL) /* old entry - should not happen */
573 {
574 ERR_STATE_free(tmpp);
575 }
576 }
577 return(ret);
578 }
579
580int ERR_get_next_error_library()
581 {
582 static int value=ERR_LIB_USER;
583
584 return(value++);
585 }
586
587void ERR_set_error_data(data,flags)
588char *data;
589int flags;
590 {
591 ERR_STATE *es;
592 int i;
593
594 es=ERR_get_state();
595
596 i=es->top;
597 if (i == 0)
598 i=ERR_NUM_ERRORS-1;
599
600 es->err_data[i]=data;
601 es->err_data_flags[es->top]=flags;
602 }
603
604void ERR_add_error_data( VAR_PLIST(int , num))
605VAR_ALIST
606 {
607 VAR_BDEFN(args, int, num);
608 int i,n,s;
609 char *str,*p,*a;
610
611 s=64;
612 str=Malloc(s+1);
613 if (str == NULL) return;
614 str[0]='\0';
615
616 VAR_INIT(args,int,num);
617 n=0;
618 for (i=0; i<num; i++)
619 {
620 VAR_ARG(args,char *,a);
621 if (a != NULL) {
622 n+=strlen(a);
623 if (n > s)
624 {
625 s=n+20;
626 p=Realloc(str,s+1);
627 if (p == NULL)
628 {
629 Free(str);
630 return;
631 }
632 else
633 str=p;
634 }
635 strcat(str,a);
636 }
637 }
638 ERR_set_error_data(str,ERR_TXT_MALLOCED|ERR_TXT_STRING);
639
640 VAR_END( args );
641 }
642
diff --git a/src/lib/libcrypto/err/err.h b/src/lib/libcrypto/err/err.h
new file mode 100644
index 0000000000..75f931be11
--- /dev/null
+++ b/src/lib/libcrypto/err/err.h
@@ -0,0 +1,287 @@
1/* crypto/err/err.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_ERR_H
60#define HEADER_ERR_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66/* The following is a bit of a trick to help the object files only contain
67 * the 'name of the file' string once. Since 'err.h' is protected by the
68 * HEADER_ERR_H stuff, this should be included only once per file. */
69
70#define ERR_file_name __FILE__
71
72#ifndef NO_ERR
73#define ERR_PUT_error(a,b,c,d,e) ERR_put_error(a,b,c,d,e)
74#else
75#define ERR_PUT_error(a,b,c,d,e) ERR_put_error(a,b,c,NULL,0)
76#endif
77
78#include <errno.h>
79
80#define ERR_TXT_MALLOCED 0x01
81#define ERR_TXT_STRING 0x02
82
83#define ERR_NUM_ERRORS 16
84typedef struct err_state_st
85 {
86 unsigned long pid;
87 unsigned long err_buffer[ERR_NUM_ERRORS];
88 char *err_data[ERR_NUM_ERRORS];
89 int err_data_flags[ERR_NUM_ERRORS];
90 char *err_file[ERR_NUM_ERRORS];
91 int err_line[ERR_NUM_ERRORS];
92 int top,bottom;
93 } ERR_STATE;
94
95/* library */
96#define ERR_LIB_NONE 1
97#define ERR_LIB_SYS 2
98#define ERR_LIB_BN 3
99#define ERR_LIB_RSA 4
100#define ERR_LIB_DH 5
101#define ERR_LIB_EVP 6
102#define ERR_LIB_BUF 7
103#define ERR_LIB_OBJ 8
104#define ERR_LIB_PEM 9
105#define ERR_LIB_DSA 10
106#define ERR_LIB_X509 11
107#define ERR_LIB_METH 12
108#define ERR_LIB_ASN1 13
109#define ERR_LIB_CONF 14
110#define ERR_LIB_CRYPTO 15
111#define ERR_LIB_SSL 20
112#define ERR_LIB_SSL23 21
113#define ERR_LIB_SSL2 22
114#define ERR_LIB_SSL3 23
115#define ERR_LIB_RSAREF 30
116#define ERR_LIB_PROXY 31
117#define ERR_LIB_BIO 32
118#define ERR_LIB_PKCS7 33
119
120#define ERR_LIB_USER 128
121
122#define SYSerr(f,r) ERR_PUT_error(ERR_LIB_SYS,(f),(r),ERR_file_name,__LINE__)
123#define BNerr(f,r) ERR_PUT_error(ERR_LIB_BN,(f),(r),ERR_file_name,__LINE__)
124#define RSAerr(f,r) ERR_PUT_error(ERR_LIB_RSA,(f),(r),ERR_file_name,__LINE__)
125#define DHerr(f,r) ERR_PUT_error(ERR_LIB_DH,(f),(r),ERR_file_name,__LINE__)
126#define EVPerr(f,r) ERR_PUT_error(ERR_LIB_EVP,(f),(r),ERR_file_name,__LINE__)
127#define BUFerr(f,r) ERR_PUT_error(ERR_LIB_BUF,(f),(r),ERR_file_name,__LINE__)
128#define BIOerr(f,r) ERR_PUT_error(ERR_LIB_BIO,(f),(r),ERR_file_name,__LINE__)
129#define OBJerr(f,r) ERR_PUT_error(ERR_LIB_OBJ,(f),(r),ERR_file_name,__LINE__)
130#define PEMerr(f,r) ERR_PUT_error(ERR_LIB_PEM,(f),(r),ERR_file_name,__LINE__)
131#define DSAerr(f,r) ERR_PUT_error(ERR_LIB_DSA,(f),(r),ERR_file_name,__LINE__)
132#define X509err(f,r) ERR_PUT_error(ERR_LIB_X509,(f),(r),ERR_file_name,__LINE__)
133#define METHerr(f,r) ERR_PUT_error(ERR_LIB_METH,(f),(r),ERR_file_name,__LINE__)
134#define ASN1err(f,r) ERR_PUT_error(ERR_LIB_ASN1,(f),(r),ERR_file_name,__LINE__)
135#define CONFerr(f,r) ERR_PUT_error(ERR_LIB_CONF,(f),(r),ERR_file_name,__LINE__)
136#define CRYPTOerr(f,r) ERR_PUT_error(ERR_LIB_CRYPTO,(f),(r),ERR_file_name,__LINE__)
137#define SSLerr(f,r) ERR_PUT_error(ERR_LIB_SSL,(f),(r),ERR_file_name,__LINE__)
138#define SSL23err(f,r) ERR_PUT_error(ERR_LIB_SSL23,(f),(r),ERR_file_name,__LINE__)
139#define SSL2err(f,r) ERR_PUT_error(ERR_LIB_SSL2,(f),(r),ERR_file_name,__LINE__)
140#define SSL3err(f,r) ERR_PUT_error(ERR_LIB_SSL3,(f),(r),ERR_file_name,__LINE__)
141#define RSAREFerr(f,r) ERR_PUT_error(ERR_LIB_RSAREF,(f),(r),ERR_file_name,__LINE__)
142#define PROXYerr(f,r) ERR_PUT_error(ERR_LIB_PROXY,(f),(r),ERR_file_name,__LINE__)
143#define PKCS7err(f,r) ERR_PUT_error(ERR_LIB_PKCS7,(f),(r),ERR_file_name,__LINE__)
144
145/* Borland C seems too stupid to be able to shift and do longs in
146 * the pre-processor :-( */
147#define ERR_PACK(l,f,r) (((((unsigned long)l)&0xffL)*0x1000000)| \
148 ((((unsigned long)f)&0xfffL)*0x1000)| \
149 ((((unsigned long)r)&0xfffL)))
150#define ERR_GET_LIB(l) (int)((((unsigned long)l)>>24L)&0xffL)
151#define ERR_GET_FUNC(l) (int)((((unsigned long)l)>>12L)&0xfffL)
152#define ERR_GET_REASON(l) (int)((l)&0xfffL)
153#define ERR_FATAL_ERROR(l) (int)((l)&ERR_R_FATAL)
154
155/* OS fuctions */
156#define SYS_F_FOPEN 1
157#define SYS_F_CONNECT 2
158#define SYS_F_GETSERVBYNAME 3
159#define SYS_F_SOCKET 4
160#define SYS_F_IOCTLSOCKET 5
161#define SYS_F_BIND 6
162#define SYS_F_LISTEN 7
163#define SYS_F_ACCEPT 8
164#define SYS_F_WSASTARTUP 9 /* Winsock stuff */
165
166#define ERR_R_FATAL 32
167/* reasons */
168#define ERR_R_SYS_LIB ERR_LIB_SYS
169#define ERR_R_BN_LIB ERR_LIB_BN
170#define ERR_R_RSA_LIB ERR_LIB_RSA
171#define ERR_R_DSA_LIB ERR_LIB_DSA
172#define ERR_R_DH_LIB ERR_LIB_DH
173#define ERR_R_EVP_LIB ERR_LIB_EVP
174#define ERR_R_BUF_LIB ERR_LIB_BUF
175#define ERR_R_BIO_LIB ERR_LIB_BIO
176#define ERR_R_OBJ_LIB ERR_LIB_OBJ
177#define ERR_R_PEM_LIB ERR_LIB_PEM
178#define ERR_R_X509_LIB ERR_LIB_X509
179#define ERR_R_METH_LIB ERR_LIB_METH
180#define ERR_R_ASN1_LIB ERR_LIB_ASN1
181#define ERR_R_CONF_LIB ERR_LIB_CONF
182#define ERR_R_CRYPTO_LIB ERR_LIB_CRYPTO
183#define ERR_R_SSL_LIB ERR_LIB_SSL
184#define ERR_R_SSL23_LIB ERR_LIB_SSL23
185#define ERR_R_SSL2_LIB ERR_LIB_SSL2
186#define ERR_R_SSL3_LIB ERR_LIB_SSL3
187#define ERR_R_PROXY_LIB ERR_LIB_PROXY
188#define ERR_R_BIO_LIB ERR_LIB_BIO
189#define ERR_R_PKCS7_LIB ERR_LIB_PKCS7
190
191/* fatal error */
192#define ERR_R_MALLOC_FAILURE (1|ERR_R_FATAL)
193#define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED (2|ERR_R_FATAL)
194#define ERR_R_PASSED_NULL_PARAMETER (3|ERR_R_FATAL)
195
196typedef struct ERR_string_data_st
197 {
198 unsigned long error;
199 char *string;
200 } ERR_STRING_DATA;
201
202#ifndef NOPROTO
203void ERR_put_error(int lib, int func,int reason,char *file,int line);
204void ERR_set_error_data(char *data,int flags);
205
206unsigned long ERR_get_error(void );
207unsigned long ERR_get_error_line(char **file,int *line);
208unsigned long ERR_get_error_line_data(char **file,int *line,
209 char **data, int *flags);
210unsigned long ERR_peek_error(void );
211unsigned long ERR_peek_error_line(char **file,int *line);
212unsigned long ERR_peek_error_line_data(char **file,int *line,
213 char **data,int *flags);
214void ERR_clear_error(void );
215char *ERR_error_string(unsigned long e,char *buf);
216char *ERR_lib_error_string(unsigned long e);
217char *ERR_func_error_string(unsigned long e);
218char *ERR_reason_error_string(unsigned long e);
219#ifndef NO_FP_API
220void ERR_print_errors_fp(FILE *fp);
221#endif
222#ifdef HEADER_BIO_H
223void ERR_print_errors(BIO *bp);
224void ERR_add_error_data( VAR_PLIST( int, num ) );
225#endif
226void ERR_load_strings(int lib,ERR_STRING_DATA str[]);
227void ERR_load_ERR_strings(void );
228void ERR_load_crypto_strings(void );
229void ERR_free_strings(void );
230
231void ERR_remove_state(unsigned long pid); /* if zero we look it up */
232ERR_STATE *ERR_get_state(void);
233
234#ifdef HEADER_LHASH_H
235LHASH *ERR_get_string_table(void );
236LHASH *ERR_get_err_state_table(void );
237#else
238char *ERR_get_string_table(void );
239char *ERR_get_err_state_table(void );
240#endif
241
242int ERR_get_next_error_library(void );
243
244#else
245
246void ERR_put_error();
247void ERR_set_error_data();
248
249unsigned long ERR_get_error();
250unsigned long ERR_get_error_line();
251unsigned long ERR_peek_error();
252unsigned long ERR_peek_error_line();
253void ERR_clear_error();
254char *ERR_error_string();
255char *ERR_lib_error_string();
256char *ERR_func_error_string();
257char *ERR_reason_error_string();
258#ifndef NO_FP_API
259void ERR_print_errors_fp();
260#endif
261void ERR_print_errors();
262void ERR_add_error_data();
263void ERR_load_strings();
264void ERR_load_ERR_strings();
265void ERR_load_crypto_strings();
266void ERR_free_strings();
267
268void ERR_remove_state();
269ERR_STATE *ERR_get_state();
270
271#ifdef HEADER_LHASH_H
272LHASH *ERR_get_string_table();
273LHASH *ERR_get_err_state_table();
274#else
275char *ERR_get_string_table();
276char *ERR_get_err_state_table();
277#endif
278
279int ERR_get_next_error_library();
280
281#endif
282
283#ifdef __cplusplus
284}
285#endif
286
287#endif
diff --git a/src/lib/libcrypto/err/err_all.c b/src/lib/libcrypto/err/err_all.c
new file mode 100644
index 0000000000..f874268e1a
--- /dev/null
+++ b/src/lib/libcrypto/err/err_all.c
@@ -0,0 +1,116 @@
1/* crypto/err/err_all.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "asn1.h"
61#include "bn.h"
62#include "buffer.h"
63#include "bio.h"
64#ifndef NO_RSA
65#include "rsa.h"
66#endif
67#ifdef RSAref
68#include "rsaref.h"
69#endif
70#ifndef NO_DH
71#include "dh.h"
72#endif
73#ifndef NO_DSA
74#include "dsa.h"
75#endif
76#include "evp.h"
77#include "objects.h"
78#include "pem.h"
79#include "x509.h"
80#include "conf.h"
81#include "err.h"
82
83void ERR_load_crypto_strings()
84 {
85 static int done=0;
86
87 if (done) return;
88 done=1;
89#ifndef NO_ERR
90 ERR_load_ASN1_strings();
91 ERR_load_BN_strings();
92 ERR_load_BUF_strings();
93 ERR_load_BIO_strings();
94 ERR_load_CONF_strings();
95#ifndef NO_RSA
96#ifdef RSAref
97 ERR_load_RSAREF_strings();
98#else
99 ERR_load_RSA_strings();
100#endif
101#endif
102#ifndef NO_DH
103 ERR_load_DH_strings();
104#endif
105#ifndef NO_DSA
106 ERR_load_DSA_strings();
107#endif
108 ERR_load_ERR_strings();
109 ERR_load_EVP_strings();
110 ERR_load_OBJ_strings();
111 ERR_load_PEM_strings();
112 ERR_load_X509_strings();
113 ERR_load_CRYPTO_strings();
114 ERR_load_PKCS7_strings();
115#endif
116 }
diff --git a/src/lib/libcrypto/err/err_prn.c b/src/lib/libcrypto/err/err_prn.c
new file mode 100644
index 0000000000..ecd0e7c4fa
--- /dev/null
+++ b/src/lib/libcrypto/err/err_prn.c
@@ -0,0 +1,107 @@
1/* crypto/err/err_prn.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "lhash.h"
61#include "crypto.h"
62#include "cryptlib.h"
63#include "buffer.h"
64#include "err.h"
65#include "crypto.h"
66
67#ifndef NO_FP_API
68void ERR_print_errors_fp(fp)
69FILE *fp;
70 {
71 unsigned long l;
72 char buf[200];
73 char *file,*data;
74 int line,flags;
75 unsigned long es;
76
77 es=CRYPTO_thread_id();
78 while ((l=ERR_get_error_line_data(&file,&line,&data,&flags)) != 0)
79 {
80 fprintf(fp,"%lu:%s:%s:%d:%s\n",es,ERR_error_string(l,buf),
81 file,line,(flags&ERR_TXT_STRING)?data:"");
82 }
83 }
84#endif
85
86void ERR_print_errors(bp)
87BIO *bp;
88 {
89 unsigned long l;
90 char buf[256];
91 char buf2[256];
92 char *file,*data;
93 int line,flags;
94 unsigned long es;
95
96 es=CRYPTO_thread_id();
97 while ((l=ERR_get_error_line_data(&file,&line,&data,&flags)) != 0)
98 {
99 sprintf(buf2,"%lu:%s:%s:%d:",es,ERR_error_string(l,buf),
100 file,line);
101 BIO_write(bp,buf2,strlen(buf2));
102 if (flags & ERR_TXT_STRING)
103 BIO_write(bp,data,strlen(data));
104 BIO_write(bp,"\n",1);
105 }
106 }
107
diff --git a/src/lib/libcrypto/evp/bio_b64.c b/src/lib/libcrypto/evp/bio_b64.c
new file mode 100644
index 0000000000..73172b9a07
--- /dev/null
+++ b/src/lib/libcrypto/evp/bio_b64.c
@@ -0,0 +1,547 @@
1/* crypto/evp/bio_b64.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include "buffer.h"
63#include "evp.h"
64
65#ifndef NOPROTO
66static int b64_write(BIO *h,char *buf,int num);
67static int b64_read(BIO *h,char *buf,int size);
68/*static int b64_puts(BIO *h,char *str); */
69/*static int b64_gets(BIO *h,char *str,int size); */
70static long b64_ctrl(BIO *h,int cmd,long arg1,char *arg2);
71static int b64_new(BIO *h);
72static int b64_free(BIO *data);
73#else
74static int b64_write();
75static int b64_read();
76/*static int b64_puts(); */
77/*static int b64_gets(); */
78static long b64_ctrl();
79static int b64_new();
80static int b64_free();
81#endif
82
83#define B64_BLOCK_SIZE 1024
84#define B64_BLOCK_SIZE2 768
85#define B64_NONE 0
86#define B64_ENCODE 1
87#define B64_DECODE 2
88
89typedef struct b64_struct
90 {
91 /*BIO *bio; moved to the BIO structure */
92 int buf_len;
93 int buf_off;
94 int tmp_len; /* used to find the start when decoding */
95 int tmp_nl; /* If true, scan until '\n' */
96 int encode;
97 int start; /* have we started decoding yet? */
98 int cont; /* <= 0 when finished */
99 EVP_ENCODE_CTX base64;
100 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE)+10];
101 char tmp[B64_BLOCK_SIZE];
102 } BIO_B64_CTX;
103
104static BIO_METHOD methods_b64=
105 {
106 BIO_TYPE_BASE64,"base64 encoding",
107 b64_write,
108 b64_read,
109 NULL, /* b64_puts, */
110 NULL, /* b64_gets, */
111 b64_ctrl,
112 b64_new,
113 b64_free,
114 };
115
116BIO_METHOD *BIO_f_base64()
117 {
118 return(&methods_b64);
119 }
120
121static int b64_new(bi)
122BIO *bi;
123 {
124 BIO_B64_CTX *ctx;
125
126 ctx=(BIO_B64_CTX *)Malloc(sizeof(BIO_B64_CTX));
127 if (ctx == NULL) return(0);
128
129 ctx->buf_len=0;
130 ctx->tmp_len=0;
131 ctx->tmp_nl=0;
132 ctx->buf_off=0;
133 ctx->cont=1;
134 ctx->start=1;
135 ctx->encode=0;
136
137 bi->init=1;
138 bi->ptr=(char *)ctx;
139 bi->flags=0;
140 return(1);
141 }
142
143static int b64_free(a)
144BIO *a;
145 {
146 if (a == NULL) return(0);
147 Free(a->ptr);
148 a->ptr=NULL;
149 a->init=0;
150 a->flags=0;
151 return(1);
152 }
153
154static int b64_read(b,out,outl)
155BIO *b;
156char *out;
157int outl;
158 {
159 int ret=0,i,ii,j,k,x,n,num,ret_code=0;
160 BIO_B64_CTX *ctx;
161 unsigned char *p,*q;
162
163 if (out == NULL) return(0);
164 ctx=(BIO_B64_CTX *)b->ptr;
165
166 if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
167
168 if (ctx->encode != B64_DECODE)
169 {
170 ctx->encode=B64_DECODE;
171 ctx->buf_len=0;
172 ctx->buf_off=0;
173 ctx->tmp_len=0;
174 EVP_DecodeInit(&(ctx->base64));
175 }
176
177 /* First check if there are bytes decoded/encoded */
178 if (ctx->buf_len > 0)
179 {
180 i=ctx->buf_len-ctx->buf_off;
181 if (i > outl) i=outl;
182 memcpy(out,&(ctx->buf[ctx->buf_off]),i);
183 ret=i;
184 out+=i;
185 outl-=i;
186 ctx->buf_off+=i;
187 if (ctx->buf_len == ctx->buf_off)
188 {
189 ctx->buf_len=0;
190 ctx->buf_off=0;
191 }
192 }
193
194 /* At this point, we have room of outl bytes and an empty
195 * buffer, so we should read in some more. */
196
197 ret_code=0;
198 while (outl > 0)
199 {
200 if (ctx->cont <= 0) break;
201
202 i=BIO_read(b->next_bio,&(ctx->tmp[ctx->tmp_len]),
203 B64_BLOCK_SIZE-ctx->tmp_len);
204
205 if (i <= 0)
206 {
207 ret_code=i;
208
209 /* Should be continue next time we are called? */
210 if (!BIO_should_retry(b->next_bio))
211 ctx->cont=i;
212 /* else we should continue when called again */
213 break;
214 }
215 i+=ctx->tmp_len;
216
217 /* We need to scan, a line at a time until we
218 * have a valid line if we are starting. */
219 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL))
220 {
221 /* ctx->start=1; */
222 ctx->tmp_len=0;
223 }
224 else if (ctx->start)
225 {
226 q=p=(unsigned char *)ctx->tmp;
227 for (j=0; j<i; j++)
228 {
229 if (*(q++) != '\n') continue;
230
231 /* due to a previous very long line,
232 * we need to keep on scanning for a '\n'
233 * before we even start looking for
234 * base64 encoded stuff. */
235 if (ctx->tmp_nl)
236 {
237 p=q;
238 ctx->tmp_nl=0;
239 continue;
240 }
241
242 k=EVP_DecodeUpdate(&(ctx->base64),
243 (unsigned char *)ctx->buf,
244 &num,p,q-p);
245 if ((k <= 0) && (num == 0) && (ctx->start))
246 EVP_DecodeInit(&ctx->base64);
247 else
248 {
249 if (p != (unsigned char *)
250 &(ctx->tmp[0]))
251 {
252 i-=(p- (unsigned char *)
253 &(ctx->tmp[0]));
254 for (x=0; x < i; x++)
255 ctx->tmp[x]=p[x];
256 EVP_DecodeInit(&ctx->base64);
257 }
258 ctx->start=0;
259 break;
260 }
261 p=q;
262 }
263
264 /* we fell off the end without starting */
265 if (j == i)
266 {
267 /* Is this is one long chunk?, if so, keep on
268 * reading until a new line. */
269 if (p == (unsigned char *)&(ctx->tmp[0]))
270 {
271 ctx->tmp_nl=1;
272 ctx->tmp_len=0;
273 }
274 else if (p != q) /* finished on a '\n' */
275 {
276 n=q-p;
277 for (ii=0; ii<n; ii++)
278 ctx->tmp[ii]=p[ii];
279 ctx->tmp_len=n;
280 }
281 /* else finished on a '\n' */
282 continue;
283 }
284 else
285 ctx->tmp_len=0;
286 }
287
288 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
289 {
290 int z,jj;
291
292 jj=(i>>2)<<2;
293 z=EVP_DecodeBlock((unsigned char *)ctx->buf,
294 (unsigned char *)ctx->tmp,jj);
295 if (jj > 2)
296 {
297 if (ctx->tmp[jj-1] == '=')
298 {
299 z--;
300 if (ctx->tmp[jj-2] == '=')
301 z--;
302 }
303 }
304 /* z is now number of output bytes and jj is the
305 * number consumed */
306 if (jj != i)
307 {
308 memcpy((unsigned char *)ctx->tmp,
309 (unsigned char *)&(ctx->tmp[jj]),i-jj);
310 ctx->tmp_len=i-jj;
311 }
312 ctx->buf_len=0;
313 if (z > 0)
314 {
315 ctx->buf_len=z;
316 i=1;
317 }
318 else
319 i=z;
320 }
321 else
322 {
323 i=EVP_DecodeUpdate(&(ctx->base64),
324 (unsigned char *)ctx->buf,&ctx->buf_len,
325 (unsigned char *)ctx->tmp,i);
326 }
327 ctx->cont=i;
328 ctx->buf_off=0;
329 if (i < 0)
330 {
331 ret_code=0;
332 ctx->buf_len=0;
333 break;
334 }
335
336 if (ctx->buf_len <= outl)
337 i=ctx->buf_len;
338 else
339 i=outl;
340
341 memcpy(out,ctx->buf,i);
342 ret+=i;
343 ctx->buf_off=i;
344 if (ctx->buf_off == ctx->buf_len)
345 {
346 ctx->buf_len=0;
347 ctx->buf_off=0;
348 }
349 outl-=i;
350 out+=i;
351 }
352 BIO_clear_retry_flags(b);
353 BIO_copy_next_retry(b);
354 return((ret == 0)?ret_code:ret);
355 }
356
357static int b64_write(b,in,inl)
358BIO *b;
359char *in;
360int inl;
361 {
362 int ret=inl,n,i;
363 BIO_B64_CTX *ctx;
364
365 ctx=(BIO_B64_CTX *)b->ptr;
366 BIO_clear_retry_flags(b);
367
368 if (ctx->encode != B64_ENCODE)
369 {
370 ctx->encode=B64_ENCODE;
371 ctx->buf_len=0;
372 ctx->buf_off=0;
373 ctx->tmp_len=0;
374 EVP_EncodeInit(&(ctx->base64));
375 }
376
377 n=ctx->buf_len-ctx->buf_off;
378 while (n > 0)
379 {
380 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
381 if (i <= 0)
382 {
383 BIO_copy_next_retry(b);
384 return(i);
385 }
386 ctx->buf_off+=i;
387 n-=i;
388 }
389 /* at this point all pending data has been written */
390
391 if ((in == NULL) || (inl <= 0)) return(0);
392
393 ctx->buf_off=0;
394 while (inl > 0)
395 {
396 n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
397
398 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
399 {
400 if (ctx->tmp_len > 0)
401 {
402 n=3-ctx->tmp_len;
403 memcpy(&(ctx->tmp[ctx->tmp_len]),in,n);
404 ctx->tmp_len+=n;
405 n=ctx->tmp_len;
406 if (n < 3)
407 break;
408 ctx->buf_len=EVP_EncodeBlock(
409 (unsigned char *)ctx->buf,
410 (unsigned char *)ctx->tmp,n);
411 }
412 else
413 {
414 if (n < 3)
415 {
416 memcpy(&(ctx->tmp[0]),in,n);
417 ctx->tmp_len=n;
418 break;
419 }
420 n-=n%3;
421 ctx->buf_len=EVP_EncodeBlock(
422 (unsigned char *)ctx->buf,
423 (unsigned char *)in,n);
424 }
425 }
426 else
427 {
428 EVP_EncodeUpdate(&(ctx->base64),
429 (unsigned char *)ctx->buf,&ctx->buf_len,
430 (unsigned char *)in,n);
431 }
432 inl-=n;
433 in+=n;
434
435 ctx->buf_off=0;
436 n=ctx->buf_len;
437 while (n > 0)
438 {
439 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
440 if (i <= 0)
441 {
442 BIO_copy_next_retry(b);
443 return((ret == 0)?i:ret);
444 }
445 n-=i;
446 ctx->buf_off+=i;
447 }
448 ctx->buf_len=0;
449 ctx->buf_off=0;
450 }
451 return(ret);
452 }
453
454static long b64_ctrl(b,cmd,num,ptr)
455BIO *b;
456int cmd;
457long num;
458char *ptr;
459 {
460 BIO_B64_CTX *ctx;
461 long ret=1;
462 int i;
463
464 ctx=(BIO_B64_CTX *)b->ptr;
465
466 switch (cmd)
467 {
468 case BIO_CTRL_RESET:
469 ctx->cont=1;
470 ctx->start=1;
471 ctx->encode=B64_NONE;
472 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
473 break;
474 case BIO_CTRL_EOF: /* More to read */
475 if (ctx->cont <= 0)
476 ret=1;
477 else
478 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
479 break;
480 case BIO_CTRL_WPENDING: /* More to write in buffer */
481 ret=ctx->buf_len-ctx->buf_off;
482 if ((ret == 0) && (ctx->base64.num != 0))
483 ret=1;
484 else if (ret <= 0)
485 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
486 break;
487 case BIO_CTRL_PENDING: /* More to read in buffer */
488 ret=ctx->buf_len-ctx->buf_off;
489 if (ret <= 0)
490 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
491 break;
492 case BIO_CTRL_FLUSH:
493 /* do a final write */
494again:
495 while (ctx->buf_len != ctx->buf_off)
496 {
497 i=b64_write(b,NULL,0);
498 if (i < 0)
499 {
500 ret=i;
501 break;
502 }
503 }
504 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
505 {
506 if (ctx->tmp_len != 0)
507 {
508 ctx->buf_len=EVP_EncodeBlock(
509 (unsigned char *)ctx->buf,
510 (unsigned char *)ctx->tmp,
511 ctx->tmp_len);
512 ctx->buf_off=0;
513 ctx->tmp_len=0;
514 goto again;
515 }
516 }
517 else if (ctx->base64.num != 0)
518 {
519 ctx->buf_off=0;
520 EVP_EncodeFinal(&(ctx->base64),
521 (unsigned char *)ctx->buf,
522 &(ctx->buf_len));
523 /* push out the bytes */
524 goto again;
525 }
526 /* Finally flush the underlying BIO */
527 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
528 break;
529
530 case BIO_C_DO_STATE_MACHINE:
531 BIO_clear_retry_flags(b);
532 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
533 BIO_copy_next_retry(b);
534 break;
535
536 case BIO_CTRL_DUP:
537 break;
538 case BIO_CTRL_INFO:
539 case BIO_CTRL_GET:
540 case BIO_CTRL_SET:
541 default:
542 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
543 break;
544 }
545 return(ret);
546 }
547
diff --git a/src/lib/libcrypto/evp/bio_enc.c b/src/lib/libcrypto/evp/bio_enc.c
new file mode 100644
index 0000000000..6c30ddfc54
--- /dev/null
+++ b/src/lib/libcrypto/evp/bio_enc.c
@@ -0,0 +1,423 @@
1/* crypto/evp/bio_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include "buffer.h"
63#include "evp.h"
64
65#ifndef NOPROTO
66static int enc_write(BIO *h,char *buf,int num);
67static int enc_read(BIO *h,char *buf,int size);
68/*static int enc_puts(BIO *h,char *str); */
69/*static int enc_gets(BIO *h,char *str,int size); */
70static long enc_ctrl(BIO *h,int cmd,long arg1,char *arg2);
71static int enc_new(BIO *h);
72static int enc_free(BIO *data);
73#else
74static int enc_write();
75static int enc_read();
76/*static int enc_puts(); */
77/*static int enc_gets(); */
78static long enc_ctrl();
79static int enc_new();
80static int enc_free();
81#endif
82
83#define ENC_BLOCK_SIZE (1024*4)
84
85typedef struct enc_struct
86 {
87 int buf_len;
88 int buf_off;
89 int cont; /* <= 0 when finished */
90 int finished;
91 int ok; /* bad decrypt */
92 EVP_CIPHER_CTX cipher;
93 char buf[ENC_BLOCK_SIZE+10];
94 } BIO_ENC_CTX;
95
96static BIO_METHOD methods_enc=
97 {
98 BIO_TYPE_CIPHER,"cipher",
99 enc_write,
100 enc_read,
101 NULL, /* enc_puts, */
102 NULL, /* enc_gets, */
103 enc_ctrl,
104 enc_new,
105 enc_free,
106 };
107
108BIO_METHOD *BIO_f_cipher()
109 {
110 return(&methods_enc);
111 }
112
113static int enc_new(bi)
114BIO *bi;
115 {
116 BIO_ENC_CTX *ctx;
117
118 ctx=(BIO_ENC_CTX *)Malloc(sizeof(BIO_ENC_CTX));
119 EVP_CIPHER_CTX_init(&ctx->cipher);
120 if (ctx == NULL) return(0);
121
122 ctx->buf_len=0;
123 ctx->buf_off=0;
124 ctx->cont=1;
125 ctx->finished=0;
126 ctx->ok=1;
127
128 bi->init=0;
129 bi->ptr=(char *)ctx;
130 bi->flags=0;
131 return(1);
132 }
133
134static int enc_free(a)
135BIO *a;
136 {
137 BIO_ENC_CTX *b;
138
139 if (a == NULL) return(0);
140 b=(BIO_ENC_CTX *)a->ptr;
141 EVP_CIPHER_CTX_cleanup(&(b->cipher));
142 memset(a->ptr,0,sizeof(BIO_ENC_CTX));
143 Free(a->ptr);
144 a->ptr=NULL;
145 a->init=0;
146 a->flags=0;
147 return(1);
148 }
149
150static int enc_read(b,out,outl)
151BIO *b;
152char *out;
153int outl;
154 {
155 int ret=0,i;
156 BIO_ENC_CTX *ctx;
157
158 if (out == NULL) return(0);
159 ctx=(BIO_ENC_CTX *)b->ptr;
160
161 if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
162
163 /* First check if there are bytes decoded/encoded */
164 if (ctx->buf_len > 0)
165 {
166 i=ctx->buf_len-ctx->buf_off;
167 if (i > outl) i=outl;
168 memcpy(out,&(ctx->buf[ctx->buf_off]),i);
169 ret=i;
170 out+=i;
171 outl-=i;
172 ctx->buf_off+=i;
173 if (ctx->buf_len == ctx->buf_off)
174 {
175 ctx->buf_len=0;
176 ctx->buf_off=0;
177 }
178 }
179
180 /* At this point, we have room of outl bytes and an empty
181 * buffer, so we should read in some more. */
182
183 while (outl > 0)
184 {
185 if (ctx->cont <= 0) break;
186
187 /* read in at offset 8, read the EVP_Cipher
188 * documentation about why */
189 i=BIO_read(b->next_bio,&(ctx->buf[8]),ENC_BLOCK_SIZE);
190
191 if (i <= 0)
192 {
193 /* Should be continue next time we are called? */
194 if (!BIO_should_retry(b->next_bio))
195 {
196 ctx->cont=i;
197 i=EVP_CipherFinal(&(ctx->cipher),
198 (unsigned char *)ctx->buf,
199 &(ctx->buf_len));
200 ctx->ok=i;
201 ctx->buf_off=0;
202 }
203 else
204 ret=(ret == 0)?i:ret;
205 break;
206 }
207 else
208 {
209 EVP_CipherUpdate(&(ctx->cipher),
210 (unsigned char *)ctx->buf,&ctx->buf_len,
211 (unsigned char *)&(ctx->buf[8]),i);
212 ctx->cont=1;
213 }
214
215 if (ctx->buf_len <= outl)
216 i=ctx->buf_len;
217 else
218 i=outl;
219
220 if (i <= 0) break;
221 memcpy(out,ctx->buf,i);
222 ret+=i;
223 ctx->buf_off=i;
224 outl-=i;
225 out+=i;
226 }
227
228 BIO_clear_retry_flags(b);
229 BIO_copy_next_retry(b);
230 return((ret == 0)?ctx->cont:ret);
231 }
232
233static int enc_write(b,in,inl)
234BIO *b;
235char *in;
236int inl;
237 {
238 int ret=0,n,i;
239 BIO_ENC_CTX *ctx;
240
241 ctx=(BIO_ENC_CTX *)b->ptr;
242 ret=inl;
243
244 BIO_clear_retry_flags(b);
245 n=ctx->buf_len-ctx->buf_off;
246 while (n > 0)
247 {
248 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
249 if (i <= 0)
250 {
251 BIO_copy_next_retry(b);
252 return(i);
253 }
254 ctx->buf_off+=i;
255 n-=i;
256 }
257 /* at this point all pending data has been written */
258
259 if ((in == NULL) || (inl <= 0)) return(0);
260
261 ctx->buf_off=0;
262 while (inl > 0)
263 {
264 n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
265 EVP_CipherUpdate(&(ctx->cipher),
266 (unsigned char *)ctx->buf,&ctx->buf_len,
267 (unsigned char *)in,n);
268 inl-=n;
269 in+=n;
270
271 ctx->buf_off=0;
272 n=ctx->buf_len;
273 while (n > 0)
274 {
275 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
276 if (i <= 0)
277 {
278 BIO_copy_next_retry(b);
279 return(i);
280 }
281 n-=i;
282 ctx->buf_off+=i;
283 }
284 ctx->buf_len=0;
285 ctx->buf_off=0;
286 }
287 BIO_copy_next_retry(b);
288 return(ret);
289 }
290
291static long enc_ctrl(b,cmd,num,ptr)
292BIO *b;
293int cmd;
294long num;
295char *ptr;
296 {
297 BIO *dbio;
298 BIO_ENC_CTX *ctx,*dctx;
299 long ret=1;
300 int i;
301
302 ctx=(BIO_ENC_CTX *)b->ptr;
303
304 switch (cmd)
305 {
306 case BIO_CTRL_RESET:
307 ctx->ok=1;
308 ctx->finished=0;
309 EVP_CipherInit(&(ctx->cipher),NULL,NULL,NULL,
310 ctx->cipher.encrypt);
311 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
312 break;
313 case BIO_CTRL_EOF: /* More to read */
314 if (ctx->cont <= 0)
315 ret=1;
316 else
317 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
318 break;
319 case BIO_CTRL_WPENDING:
320 ret=ctx->buf_len-ctx->buf_off;
321 if (ret <= 0)
322 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
323 break;
324 case BIO_CTRL_PENDING: /* More to read in buffer */
325 ret=ctx->buf_len-ctx->buf_off;
326 if (ret <= 0)
327 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
328 break;
329 case BIO_CTRL_FLUSH:
330 /* do a final write */
331again:
332 while (ctx->buf_len != ctx->buf_off)
333 {
334 i=enc_write(b,NULL,0);
335 if (i < 0)
336 {
337 ret=i;
338 break;
339 }
340 }
341
342 if (!ctx->finished)
343 {
344 ctx->finished=1;
345 ctx->buf_off=0;
346 ret=EVP_CipherFinal(&(ctx->cipher),
347 (unsigned char *)ctx->buf,
348 &(ctx->buf_len));
349 ctx->ok=(int)ret;
350 if (ret <= 0) break;
351
352 /* push out the bytes */
353 goto again;
354 }
355
356 /* Finally flush the underlying BIO */
357 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
358 break;
359 case BIO_C_GET_CIPHER_STATUS:
360 ret=(long)ctx->ok;
361 break;
362 case BIO_C_DO_STATE_MACHINE:
363 BIO_clear_retry_flags(b);
364 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
365 BIO_copy_next_retry(b);
366 break;
367
368 case BIO_CTRL_DUP:
369 dbio=(BIO *)ptr;
370 dctx=(BIO_ENC_CTX *)dbio->ptr;
371 memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
372 dbio->init=1;
373 break;
374 default:
375 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
376 break;
377 }
378 return(ret);
379 }
380
381/*
382void BIO_set_cipher_ctx(b,c)
383BIO *b;
384EVP_CIPHER_ctx *c;
385 {
386 if (b == NULL) return;
387
388 if ((b->callback != NULL) &&
389 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
390 return;
391
392 b->init=1;
393 ctx=(BIO_ENC_CTX *)b->ptr;
394 memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
395
396 if (b->callback != NULL)
397 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
398 }
399*/
400
401void BIO_set_cipher(b,c,k,i,e)
402BIO *b;
403EVP_CIPHER *c;
404unsigned char *k;
405unsigned char *i;
406int e;
407 {
408 BIO_ENC_CTX *ctx;
409
410 if (b == NULL) return;
411
412 if ((b->callback != NULL) &&
413 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
414 return;
415
416 b->init=1;
417 ctx=(BIO_ENC_CTX *)b->ptr;
418 EVP_CipherInit(&(ctx->cipher),c,k,i,e);
419
420 if (b->callback != NULL)
421 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
422 }
423
diff --git a/src/lib/libcrypto/evp/bio_md.c b/src/lib/libcrypto/evp/bio_md.c
new file mode 100644
index 0000000000..fa5fdc055b
--- /dev/null
+++ b/src/lib/libcrypto/evp/bio_md.c
@@ -0,0 +1,270 @@
1/* crypto/evp/bio_md.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include "buffer.h"
63#include "evp.h"
64
65/* BIO_put and BIO_get both add to the digest,
66 * BIO_gets returns the digest */
67
68#ifndef NOPROTO
69static int md_write(BIO *h,char *buf,int num);
70static int md_read(BIO *h,char *buf,int size);
71/*static int md_puts(BIO *h,char *str); */
72static int md_gets(BIO *h,char *str,int size);
73static long md_ctrl(BIO *h,int cmd,long arg1,char *arg2);
74static int md_new(BIO *h);
75static int md_free(BIO *data);
76#else
77static int md_write();
78static int md_read();
79/*static int md_puts(); */
80static int md_gets();
81static long md_ctrl();
82static int md_new();
83static int md_free();
84#endif
85
86static BIO_METHOD methods_md=
87 {
88 BIO_TYPE_MD,"message digest",
89 md_write,
90 md_read,
91 NULL, /* md_puts, */
92 md_gets,
93 md_ctrl,
94 md_new,
95 md_free,
96 };
97
98BIO_METHOD *BIO_f_md()
99 {
100 return(&methods_md);
101 }
102
103static int md_new(bi)
104BIO *bi;
105 {
106 EVP_MD_CTX *ctx;
107
108 ctx=(EVP_MD_CTX *)Malloc(sizeof(EVP_MD_CTX));
109 if (ctx == NULL) return(0);
110
111 bi->init=0;
112 bi->ptr=(char *)ctx;
113 bi->flags=0;
114 return(1);
115 }
116
117static int md_free(a)
118BIO *a;
119 {
120 if (a == NULL) return(0);
121 Free(a->ptr);
122 a->ptr=NULL;
123 a->init=0;
124 a->flags=0;
125 return(1);
126 }
127
128static int md_read(b,out,outl)
129BIO *b;
130char *out;
131int outl;
132 {
133 int ret=0;
134 EVP_MD_CTX *ctx;
135
136 if (out == NULL) return(0);
137 ctx=(EVP_MD_CTX *)b->ptr;
138
139 if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
140
141 ret=BIO_read(b->next_bio,out,outl);
142 if (b->init)
143 {
144 if (ret > 0)
145 {
146 EVP_DigestUpdate(ctx,(unsigned char *)out,
147 (unsigned int)ret);
148 }
149 }
150 BIO_clear_retry_flags(b);
151 BIO_copy_next_retry(b);
152 return(ret);
153 }
154
155static int md_write(b,in,inl)
156BIO *b;
157char *in;
158int inl;
159 {
160 int ret=0;
161 EVP_MD_CTX *ctx;
162
163 if ((in == NULL) || (inl <= 0)) return(0);
164 ctx=(EVP_MD_CTX *)b->ptr;
165
166 if ((ctx != NULL) && (b->next_bio != NULL))
167 ret=BIO_write(b->next_bio,in,inl);
168 if (b->init)
169 {
170 if (ret > 0)
171 {
172 EVP_DigestUpdate(ctx,(unsigned char *)in,
173 (unsigned int)ret);
174 }
175 }
176 BIO_clear_retry_flags(b);
177 BIO_copy_next_retry(b);
178 return(ret);
179 }
180
181static long md_ctrl(b,cmd,num,ptr)
182BIO *b;
183int cmd;
184long num;
185char *ptr;
186 {
187 EVP_MD_CTX *ctx,*dctx,**pctx;
188 EVP_MD **ppmd;
189 EVP_MD *md;
190 long ret=1;
191 BIO *dbio;
192
193 ctx=(EVP_MD_CTX *)b->ptr;
194
195 switch (cmd)
196 {
197 case BIO_CTRL_RESET:
198 if (b->init)
199 EVP_DigestInit(ctx,ctx->digest);
200 else
201 ret=0;
202 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
203 break;
204 case BIO_C_GET_MD:
205 if (b->init)
206 {
207 ppmd=(EVP_MD **)ptr;
208 *ppmd=ctx->digest;
209 }
210 else
211 ret=0;
212 break;
213 case BIO_C_GET_MD_CTX:
214 if (b->init)
215 {
216 pctx=(EVP_MD_CTX **)ptr;
217 *pctx=ctx;
218 }
219 else
220 ret=0;
221 break;
222 case BIO_C_DO_STATE_MACHINE:
223 BIO_clear_retry_flags(b);
224 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
225 BIO_copy_next_retry(b);
226 break;
227
228 case BIO_C_SET_MD:
229 md=(EVP_MD *)ptr;
230 EVP_DigestInit(ctx,md);
231 b->init=1;
232 break;
233 case BIO_CTRL_DUP:
234 dbio=(BIO *)ptr;
235 dctx=(EVP_MD_CTX *)dbio->ptr;
236 memcpy(dctx,ctx,sizeof(ctx));
237 b->init=1;
238 break;
239 default:
240 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
241 break;
242 }
243 return(ret);
244 }
245
246static int md_gets(bp,buf,size)
247BIO *bp;
248char *buf;
249int size;
250 {
251 EVP_MD_CTX *ctx;
252 unsigned int ret;
253
254
255 ctx=(EVP_MD_CTX *)bp->ptr;
256 if (size < ctx->digest->md_size)
257 return(0);
258 EVP_DigestFinal(ctx,(unsigned char *)buf,&ret);
259 return((int)ret);
260 }
261
262/*
263static int md_puts(bp,str)
264BIO *bp;
265char *str;
266 {
267 return(-1);
268 }
269*/
270
diff --git a/src/lib/libcrypto/evp/c_all.c b/src/lib/libcrypto/evp/c_all.c
new file mode 100644
index 0000000000..e77d1c896b
--- /dev/null
+++ b/src/lib/libcrypto/evp/c_all.c
@@ -0,0 +1,190 @@
1/* crypto/evp/c_all.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63
64void SSLeay_add_all_algorithms()
65 {
66 SSLeay_add_all_ciphers();
67 SSLeay_add_all_digests();
68 }
69
70void SSLeay_add_all_ciphers()
71 {
72#ifndef NO_DES
73 EVP_add_cipher(EVP_des_cfb());
74 EVP_add_cipher(EVP_des_ede_cfb());
75 EVP_add_cipher(EVP_des_ede3_cfb());
76
77 EVP_add_cipher(EVP_des_ofb());
78 EVP_add_cipher(EVP_des_ede_ofb());
79 EVP_add_cipher(EVP_des_ede3_ofb());
80
81 EVP_add_cipher(EVP_desx_cbc());
82 EVP_add_alias(SN_desx_cbc,"DESX");
83 EVP_add_alias(SN_desx_cbc,"desx");
84
85 EVP_add_cipher(EVP_des_cbc());
86 EVP_add_alias(SN_des_cbc,"DES");
87 EVP_add_alias(SN_des_cbc,"des");
88 EVP_add_cipher(EVP_des_ede_cbc());
89 EVP_add_cipher(EVP_des_ede3_cbc());
90 EVP_add_alias(SN_des_ede3_cbc,"DES3");
91 EVP_add_alias(SN_des_ede3_cbc,"des3");
92
93 EVP_add_cipher(EVP_des_ecb());
94 EVP_add_cipher(EVP_des_ede());
95 EVP_add_cipher(EVP_des_ede3());
96#endif
97
98#ifndef NO_RC4
99 EVP_add_cipher(EVP_rc4());
100 EVP_add_cipher(EVP_rc4_40());
101#endif
102
103#ifndef NO_IDEA
104 EVP_add_cipher(EVP_idea_ecb());
105 EVP_add_cipher(EVP_idea_cfb());
106 EVP_add_cipher(EVP_idea_ofb());
107 EVP_add_cipher(EVP_idea_cbc());
108 EVP_add_alias(SN_idea_cbc,"IDEA");
109 EVP_add_alias(SN_idea_cbc,"idea");
110#endif
111
112#ifndef NO_RC2
113 EVP_add_cipher(EVP_rc2_ecb());
114 EVP_add_cipher(EVP_rc2_cfb());
115 EVP_add_cipher(EVP_rc2_ofb());
116 EVP_add_cipher(EVP_rc2_cbc());
117 EVP_add_cipher(EVP_rc2_40_cbc());
118 EVP_add_alias(SN_rc2_cbc,"RC2");
119 EVP_add_alias(SN_rc2_cbc,"rc2");
120#endif
121
122#ifndef NO_BLOWFISH
123 EVP_add_cipher(EVP_bf_ecb());
124 EVP_add_cipher(EVP_bf_cfb());
125 EVP_add_cipher(EVP_bf_ofb());
126 EVP_add_cipher(EVP_bf_cbc());
127 EVP_add_alias(SN_bf_cbc,"BF");
128 EVP_add_alias(SN_bf_cbc,"bf");
129 EVP_add_alias(SN_bf_cbc,"blowfish");
130#endif
131
132#ifndef NO_CAST
133 EVP_add_cipher(EVP_cast5_ecb());
134 EVP_add_cipher(EVP_cast5_cfb());
135 EVP_add_cipher(EVP_cast5_ofb());
136 EVP_add_cipher(EVP_cast5_cbc());
137 EVP_add_alias(SN_cast5_cbc,"CAST");
138 EVP_add_alias(SN_cast5_cbc,"cast");
139 EVP_add_alias(SN_cast5_cbc,"CAST-cbc");
140 EVP_add_alias(SN_cast5_cbc,"cast-cbc");
141#endif
142
143#ifndef NO_RC5
144 EVP_add_cipher(EVP_rc5_32_12_16_ecb());
145 EVP_add_cipher(EVP_rc5_32_12_16_cfb());
146 EVP_add_cipher(EVP_rc5_32_12_16_ofb());
147 EVP_add_cipher(EVP_rc5_32_12_16_cbc());
148 EVP_add_alias(SN_rc5_cbc,"rc5");
149 EVP_add_alias(SN_rc5_cbc,"RC5");
150 EVP_add_alias(SN_rc5_cbc,"rc5-cbc");
151 EVP_add_alias(SN_rc5_cbc,"RC5-cbc");
152#endif
153 }
154
155
156void SSLeay_add_all_digests()
157 {
158#ifndef NO_MD2
159 EVP_add_digest(EVP_md2());
160#endif
161#ifndef NO_MD5
162 EVP_add_digest(EVP_md5());
163 EVP_add_alias(SN_md5,"ssl2-md5");
164 EVP_add_alias(SN_md5,"ssl3-md5");
165#endif
166#ifndef NO_SHA
167 EVP_add_digest(EVP_sha());
168#ifndef NO_DSA
169 EVP_add_digest(EVP_dss());
170#endif
171#endif
172#ifndef NO_SHA1
173 EVP_add_digest(EVP_sha1());
174 EVP_add_alias(SN_sha1,"ssl3-sha1");
175#ifndef NO_DSA
176 EVP_add_digest(EVP_dss1());
177 EVP_add_alias(SN_dsaWithSHA1,SN_dsaWithSHA1_2);
178 EVP_add_alias(SN_dsaWithSHA1,"DSS1");
179 EVP_add_alias(SN_dsaWithSHA1,"dss1");
180#endif
181#endif
182#if !defined(NO_MDC2) && !defined(NO_DES)
183 EVP_add_digest(EVP_mdc2());
184#endif
185#ifndef NO_RIPEMD160
186 EVP_add_digest(EVP_ripemd160());
187 EVP_add_alias(SN_ripemd160,"ripemd");
188 EVP_add_alias(SN_ripemd160,"rmd160");
189#endif
190 }
diff --git a/src/lib/libcrypto/evp/digest.c b/src/lib/libcrypto/evp/digest.c
new file mode 100644
index 0000000000..d65f0036f7
--- /dev/null
+++ b/src/lib/libcrypto/evp/digest.c
@@ -0,0 +1,89 @@
1/* crypto/evp/digest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "objects.h"
62#include "evp.h"
63
64void EVP_DigestInit(ctx,type)
65EVP_MD_CTX *ctx;
66EVP_MD *type;
67 {
68 ctx->digest=type;
69 type->init(&(ctx->md));
70 }
71
72void EVP_DigestUpdate(ctx,data,count)
73EVP_MD_CTX *ctx;
74unsigned char *data;
75unsigned int count;
76 {
77 ctx->digest->update(&(ctx->md.base[0]),data,(unsigned long)count);
78 }
79
80void EVP_DigestFinal(ctx,md,size)
81EVP_MD_CTX *ctx;
82unsigned char *md;
83unsigned int *size;
84 {
85 ctx->digest->final(md,&(ctx->md.base[0]));
86 if (size != NULL)
87 *size=ctx->digest->md_size;
88 memset(&(ctx->md),0,sizeof(ctx->md));
89 }
diff --git a/src/lib/libcrypto/evp/e_null.c b/src/lib/libcrypto/evp/e_null.c
new file mode 100644
index 0000000000..e4e7ca7606
--- /dev/null
+++ b/src/lib/libcrypto/evp/e_null.c
@@ -0,0 +1,109 @@
1/* crypto/evp/e_null.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63
64#ifndef NOPROTO
65static void null_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key,
66 unsigned char *iv,int enc);
67static void null_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
68 unsigned char *in, unsigned int inl);
69#else
70static void null_init_key();
71static void null_cipher();
72#endif
73
74static EVP_CIPHER n_cipher=
75 {
76 NID_undef,
77 1,0,0,
78 null_init_key,
79 null_cipher,
80 NULL,
81 0,
82 NULL,
83 NULL,
84 };
85
86EVP_CIPHER *EVP_enc_null()
87 {
88 return(&n_cipher);
89 }
90
91static void null_init_key(ctx,key,iv,enc)
92EVP_CIPHER_CTX *ctx;
93unsigned char *key;
94unsigned char *iv;
95int enc;
96 {
97 memset(&(ctx->c),0,sizeof(ctx->c));
98 }
99
100static void null_cipher(ctx,out,in,inl)
101EVP_CIPHER_CTX *ctx;
102unsigned char *out;
103unsigned char *in;
104unsigned int inl;
105 {
106 if (in != out)
107 memcpy((char *)out,(char *)in,(int)inl);
108 }
109
diff --git a/src/lib/libcrypto/evp/e_rc4.c b/src/lib/libcrypto/evp/e_rc4.c
new file mode 100644
index 0000000000..7e9790a94c
--- /dev/null
+++ b/src/lib/libcrypto/evp/e_rc4.c
@@ -0,0 +1,127 @@
1/* crypto/evp/e_rc4.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef NO_RC4
60
61#include <stdio.h>
62#include "cryptlib.h"
63#include "evp.h"
64#include "objects.h"
65
66#ifndef NOPROTO
67static void rc4_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key,
68 unsigned char *iv,int enc);
69static void rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
70 unsigned char *in, unsigned int inl);
71#else
72static void rc4_init_key();
73static void rc4_cipher();
74#endif
75
76static EVP_CIPHER r4_cipher=
77 {
78 NID_rc4,
79 1,EVP_RC4_KEY_SIZE,0,
80 rc4_init_key,
81 rc4_cipher,
82 NULL,
83 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+
84 sizeof((((EVP_CIPHER_CTX *)NULL)->c.rc4)),
85 NULL,
86 NULL,
87 };
88
89static EVP_CIPHER r4_40_cipher=
90 {
91 NID_rc4_40,
92 1,5 /* 40 bit */,0,
93 rc4_init_key,
94 rc4_cipher,
95 };
96
97EVP_CIPHER *EVP_rc4()
98 {
99 return(&r4_cipher);
100 }
101
102EVP_CIPHER *EVP_rc4_40()
103 {
104 return(&r4_40_cipher);
105 }
106
107static void rc4_init_key(ctx,key,iv,enc)
108EVP_CIPHER_CTX *ctx;
109unsigned char *key;
110unsigned char *iv;
111int enc;
112 {
113 if (key != NULL)
114 memcpy(&(ctx->c.rc4.key[0]),key,EVP_CIPHER_CTX_key_length(ctx));
115 RC4_set_key(&(ctx->c.rc4.ks),EVP_CIPHER_CTX_key_length(ctx),
116 ctx->c.rc4.key);
117 }
118
119static void rc4_cipher(ctx,out,in,inl)
120EVP_CIPHER_CTX *ctx;
121unsigned char *out;
122unsigned char *in;
123unsigned int inl;
124 {
125 RC4(&(ctx->c.rc4.ks),inl,in,out);
126 }
127#endif
diff --git a/src/lib/libcrypto/evp/e_xcbc_d.c b/src/lib/libcrypto/evp/e_xcbc_d.c
new file mode 100644
index 0000000000..0d7fda0c47
--- /dev/null
+++ b/src/lib/libcrypto/evp/e_xcbc_d.c
@@ -0,0 +1,122 @@
1/* crypto/evp/e_xcbc_d.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63
64#ifndef NOPROTO
65static void desx_cbc_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key,
66 unsigned char *iv,int enc);
67static void desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
68 unsigned char *in, unsigned int inl);
69#else
70static void desx_cbc_init_key();
71static void desx_cbc_cipher();
72#endif
73
74static EVP_CIPHER d_xcbc_cipher=
75 {
76 NID_desx_cbc,
77 8,24,8,
78 desx_cbc_init_key,
79 desx_cbc_cipher,
80 NULL,
81 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+
82 sizeof((((EVP_CIPHER_CTX *)NULL)->c.desx_cbc)),
83 EVP_CIPHER_set_asn1_iv,
84 EVP_CIPHER_get_asn1_iv,
85 };
86
87EVP_CIPHER *EVP_desx_cbc()
88 {
89 return(&d_xcbc_cipher);
90 }
91
92static void desx_cbc_init_key(ctx,key,iv,enc)
93EVP_CIPHER_CTX *ctx;
94unsigned char *key;
95unsigned char *iv;
96int enc;
97 {
98 if (iv != NULL)
99 memcpy(&(ctx->oiv[0]),iv,8);
100 memcpy(&(ctx->iv[0]),&(ctx->oiv[0]),8);
101 if (key != NULL)
102 {
103 des_set_key((des_cblock *)key,ctx->c.desx_cbc.ks);
104 memcpy(&(ctx->c.desx_cbc.inw[0]),&(key[8]),8);
105 memcpy(&(ctx->c.desx_cbc.outw[0]),&(key[16]),8);
106 }
107 }
108
109static void desx_cbc_cipher(ctx,out,in,inl)
110EVP_CIPHER_CTX *ctx;
111unsigned char *out;
112unsigned char *in;
113unsigned int inl;
114 {
115 des_xcbc_encrypt(
116 (des_cblock *)in,(des_cblock *)out,
117 (long)inl, ctx->c.desx_cbc.ks,
118 (des_cblock *)&(ctx->iv[0]),
119 (des_cblock *)&(ctx->c.desx_cbc.inw[0]),
120 (des_cblock *)&(ctx->c.desx_cbc.outw[0]),
121 ctx->encrypt);
122 }
diff --git a/src/lib/libcrypto/evp/encode.c b/src/lib/libcrypto/evp/encode.c
new file mode 100644
index 0000000000..14d47c1eed
--- /dev/null
+++ b/src/lib/libcrypto/evp/encode.c
@@ -0,0 +1,438 @@
1/* crypto/evp/encode.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62
63#define conv_bin2ascii(a) (data_bin2ascii[(a)&0x3f])
64#define conv_ascii2bin(a) (data_ascii2bin[(a)&0x7f])
65
66/* 64 char lines
67 * pad input with 0
68 * left over chars are set to =
69 * 1 byte => xx==
70 * 2 bytes => xxx=
71 * 3 bytes => xxxx
72 */
73#define BIN_PER_LINE (64/4*3)
74#define CHUNKS_PER_LINE (64/4)
75#define CHAR_PER_LINE (64+1)
76
77static unsigned char data_bin2ascii[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZ\
78abcdefghijklmnopqrstuvwxyz0123456789+/";
79
80/* 0xF0 is a EOLN
81 * 0xF1 is ignore but next needs to be 0xF0 (for \r\n processing).
82 * 0xF2 is EOF
83 * 0xE0 is ignore at start of line.
84 * 0xFF is error
85 */
86
87#define B64_EOLN 0xF0
88#define B64_CR 0xF1
89#define B64_EOF 0xF2
90#define B64_WS 0xE0
91#define B64_ERROR 0xFF
92#define B64_NOT_BASE64(a) (((a)|0x13) == 0xF3)
93
94static unsigned char data_ascii2bin[128]={
95 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
96 0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF,
97 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
98 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
99 0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
100 0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F,
101 0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,
102 0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,
103 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
104 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
105 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
106 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
107 0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,
108 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
109 0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,
110 0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF,
111 };
112
113void EVP_EncodeInit(ctx)
114EVP_ENCODE_CTX *ctx;
115 {
116 ctx->length=48;
117 ctx->num=0;
118 ctx->line_num=0;
119 }
120
121void EVP_EncodeUpdate(ctx,out,outl,in,inl)
122EVP_ENCODE_CTX *ctx;
123unsigned char *out;
124int *outl;
125unsigned char *in;
126int inl;
127 {
128 int i,j;
129 unsigned int total=0;
130
131 *outl=0;
132 if (inl == 0) return;
133 if ((ctx->num+inl) < ctx->length)
134 {
135 memcpy(&(ctx->enc_data[ctx->num]),in,inl);
136 ctx->num+=inl;
137 return;
138 }
139 if (ctx->num != 0)
140 {
141 i=ctx->length-ctx->num;
142 memcpy(&(ctx->enc_data[ctx->num]),in,i);
143 in+=i;
144 inl-=i;
145 j=EVP_EncodeBlock(out,ctx->enc_data,ctx->length);
146 ctx->num=0;
147 out+=j;
148 *(out++)='\n';
149 *out='\0';
150 total=j+1;
151 }
152 while (inl >= ctx->length)
153 {
154 j=EVP_EncodeBlock(out,in,ctx->length);
155 in+=ctx->length;
156 inl-=ctx->length;
157 out+=j;
158 *(out++)='\n';
159 *out='\0';
160 total+=j+1;
161 }
162 if (inl != 0)
163 memcpy(&(ctx->enc_data[0]),in,inl);
164 ctx->num=inl;
165 *outl=total;
166 }
167
168void EVP_EncodeFinal(ctx,out,outl)
169EVP_ENCODE_CTX *ctx;
170unsigned char *out;
171int *outl;
172 {
173 unsigned int ret=0;
174
175 if (ctx->num != 0)
176 {
177 ret=EVP_EncodeBlock(out,ctx->enc_data,ctx->num);
178 out[ret++]='\n';
179 out[ret]='\0';
180 ctx->num=0;
181 }
182 *outl=ret;
183 }
184
185int EVP_EncodeBlock(t,f,dlen)
186unsigned char *t,*f;
187int dlen;
188 {
189 int i,ret=0;
190 unsigned long l;
191
192 for (i=dlen; i > 0; i-=3)
193 {
194 if (i >= 3)
195 {
196 l= (((unsigned long)f[0])<<16L)|
197 (((unsigned long)f[1])<< 8L)|f[2];
198 *(t++)=conv_bin2ascii(l>>18L);
199 *(t++)=conv_bin2ascii(l>>12L);
200 *(t++)=conv_bin2ascii(l>> 6L);
201 *(t++)=conv_bin2ascii(l );
202 }
203 else
204 {
205 l=((unsigned long)f[0])<<16L;
206 if (i == 2) l|=((unsigned long)f[1]<<8L);
207
208 *(t++)=conv_bin2ascii(l>>18L);
209 *(t++)=conv_bin2ascii(l>>12L);
210 *(t++)=(i == 1)?'=':conv_bin2ascii(l>> 6L);
211 *(t++)='=';
212 }
213 ret+=4;
214 f+=3;
215 }
216
217 *t='\0';
218 return(ret);
219 }
220
221void EVP_DecodeInit(ctx)
222EVP_ENCODE_CTX *ctx;
223 {
224 ctx->length=30;
225 ctx->num=0;
226 ctx->line_num=0;
227 ctx->expect_nl=0;
228 }
229
230/* -1 for error
231 * 0 for last line
232 * 1 for full line
233 */
234int EVP_DecodeUpdate(ctx,out,outl,in,inl)
235EVP_ENCODE_CTX *ctx;
236unsigned char *out;
237int *outl;
238unsigned char *in;
239int inl;
240 {
241 int seof= -1,eof=0,rv= -1,ret=0,i,v,tmp,n,ln,tmp2,exp_nl;
242 unsigned char *d;
243
244 n=ctx->num;
245 d=ctx->enc_data;
246 ln=ctx->line_num;
247 exp_nl=ctx->expect_nl;
248
249 /* last line of input. */
250 if ((inl == 0) || ((n == 0) && (conv_ascii2bin(in[0]) == B64_EOF)))
251 { rv=0; goto end; }
252
253 /* We parse the input data */
254 for (i=0; i<inl; i++)
255 {
256 /* If the current line is > 80 characters, scream alot */
257 if (ln >= 80) { rv= -1; goto end; }
258
259 /* Get char and put it into the buffer */
260 tmp= *(in++);
261 v=conv_ascii2bin(tmp);
262 /* only save the good data :-) */
263 if (!B64_NOT_BASE64(v))
264 {
265 d[n++]=tmp;
266 ln++;
267 }
268 else if (v == B64_ERROR)
269 {
270 rv= -1;
271 goto end;
272 }
273
274 /* have we seen a '=' which is 'definitly' the last
275 * input line. seof will point to the character that
276 * holds it. and eof will hold how many characters to
277 * chop off. */
278 if (tmp == '=')
279 {
280 if (seof == -1) seof=n;
281 eof++;
282 }
283
284 /* eoln */
285 if (v == B64_EOLN)
286 {
287 ln=0;
288 if (exp_nl)
289 {
290 exp_nl=0;
291 continue;
292 }
293 }
294 exp_nl=0;
295
296 /* If we are at the end of input and it looks like a
297 * line, process it. */
298 if (((i+1) == inl) && (((n&3) == 0) || eof))
299 v=B64_EOF;
300
301 if ((v == B64_EOF) || (n >= 64))
302 {
303 /* This is needed to work correctly on 64 byte input
304 * lines. We process the line and then need to
305 * accept the '\n' */
306 if ((v != B64_EOF) && (n >= 64)) exp_nl=1;
307 tmp2=v;
308 if (n > 0)
309 {
310 v=EVP_DecodeBlock(out,d,n);
311 if (v < 0) { rv=0; goto end; }
312 n=0;
313 ret+=(v-eof);
314 }
315 else
316 {
317 eof=1;
318 v=0;
319 }
320
321 /* This is the case where we have had a short
322 * but valid input line */
323 if ((v < ctx->length) && eof)
324 {
325 rv=0;
326 goto end;
327 }
328 else
329 ctx->length=v;
330
331 if (seof >= 0) { rv=0; goto end; }
332 out+=v;
333 }
334 }
335 rv=1;
336end:
337 *outl=ret;
338 ctx->num=n;
339 ctx->line_num=ln;
340 ctx->expect_nl=exp_nl;
341 return(rv);
342 }
343
344int EVP_DecodeBlock(t,f,n)
345unsigned char *t,*f;
346int n;
347 {
348 int i,ret=0,a,b,c,d;
349 unsigned long l;
350
351 /* trim white space from the start of the line. */
352 while ((conv_ascii2bin(*f) == B64_WS) && (n > 0))
353 {
354 f++;
355 n--;
356 }
357
358 /* strip off stuff at the end of the line
359 * ascii2bin values B64_WS, B64_EOLN, B64_EOLN and B64_EOF */
360 while ((n > 3) && (B64_NOT_BASE64(conv_ascii2bin(f[n-1]))))
361 n--;
362
363 if (n%4 != 0) return(-1);
364
365 for (i=0; i<n; i+=4)
366 {
367 a=conv_ascii2bin(*(f++));
368 b=conv_ascii2bin(*(f++));
369 c=conv_ascii2bin(*(f++));
370 d=conv_ascii2bin(*(f++));
371 if ( (a & 0x80) || (b & 0x80) ||
372 (c & 0x80) || (d & 0x80))
373 return(-1);
374 l=( (((unsigned long)a)<<18L)|
375 (((unsigned long)b)<<12L)|
376 (((unsigned long)c)<< 6L)|
377 (((unsigned long)d) ));
378 *(t++)=(unsigned char)(l>>16L)&0xff;
379 *(t++)=(unsigned char)(l>> 8L)&0xff;
380 *(t++)=(unsigned char)(l )&0xff;
381 ret+=3;
382 }
383 return(ret);
384 }
385
386int EVP_DecodeFinal(ctx,out,outl)
387EVP_ENCODE_CTX *ctx;
388unsigned char *out;
389int *outl;
390 {
391 int i;
392
393 *outl=0;
394 if (ctx->num != 0)
395 {
396 i=EVP_DecodeBlock(out,ctx->enc_data,ctx->num);
397 if (i < 0) return(-1);
398 ctx->num=0;
399 *outl=i;
400 return(1);
401 }
402 else
403 return(1);
404 }
405
406#ifdef undef
407int EVP_DecodeValid(buf,len)
408unsigned char *buf;
409int len;
410 {
411 int i,num=0,bad=0;
412
413 if (len == 0) return(-1);
414 while (conv_ascii2bin(*buf) == B64_WS)
415 {
416 buf++;
417 len--;
418 if (len == 0) return(-1);
419 }
420
421 for (i=len; i >= 4; i-=4)
422 {
423 if ( (conv_ascii2bin(buf[0]) >= 0x40) ||
424 (conv_ascii2bin(buf[1]) >= 0x40) ||
425 (conv_ascii2bin(buf[2]) >= 0x40) ||
426 (conv_ascii2bin(buf[3]) >= 0x40))
427 return(-1);
428 buf+=4;
429 num+=1+(buf[2] != '=')+(buf[3] != '=');
430 }
431 if ((i == 1) && (conv_ascii2bin(buf[0]) == B64_EOLN))
432 return(num);
433 if ((i == 2) && (conv_ascii2bin(buf[0]) == B64_EOLN) &&
434 (conv_ascii2bin(buf[0]) == B64_EOLN))
435 return(num);
436 return(1);
437 }
438#endif
diff --git a/src/lib/libcrypto/evp/evp.h b/src/lib/libcrypto/evp/evp.h
new file mode 100644
index 0000000000..b39fad93a4
--- /dev/null
+++ b/src/lib/libcrypto/evp/evp.h
@@ -0,0 +1,793 @@
1/* crypto/evp/evp.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_ENVELOPE_H
60#define HEADER_ENVELOPE_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#ifndef NO_MD2
67#include "md2.h"
68#endif
69#ifndef NO_MD5
70#include "md5.h"
71#endif
72#if !defined(NO_SHA) || !defined(NO_SHA1)
73#include "sha.h"
74#endif
75#ifndef NO_RIPEMD
76#include "ripemd.h"
77#endif
78#ifndef NO_DES
79#include "des.h"
80#endif
81#ifndef NO_RC4
82#include "rc4.h"
83#endif
84#ifndef NO_RC2
85#include "rc2.h"
86#endif
87#ifndef NO_RC5
88#include "rc5.h"
89#endif
90#ifndef NO_BLOWFISH
91#include "blowfish.h"
92#endif
93#ifndef NO_CAST
94#include "cast.h"
95#endif
96#ifndef NO_IDEA
97#include "idea.h"
98#endif
99#ifndef NO_MDC2
100#include "mdc2.h"
101#endif
102
103#define EVP_RC2_KEY_SIZE 16
104#define EVP_RC4_KEY_SIZE 16
105#define EVP_BLOWFISH_KEY_SIZE 16
106#define EVP_CAST5_KEY_SIZE 16
107#define EVP_RC5_32_12_16_KEY_SIZE 16
108#define EVP_MAX_MD_SIZE (16+20) /* The SSLv3 md5+sha1 type */
109#define EVP_MAX_KEY_LENGTH 24
110#define EVP_MAX_IV_LENGTH 8
111
112#ifndef NO_RSA
113#include "rsa.h"
114#else
115#define RSA long
116#endif
117
118#ifndef NO_DSA
119#include "dsa.h"
120#else
121#define DSA long
122#endif
123
124#ifndef NO_DH
125#include "dh.h"
126#else
127#define DH long
128#endif
129
130#include "objects.h"
131
132#define EVP_PK_RSA 0x0001
133#define EVP_PK_DSA 0x0002
134#define EVP_PK_DH 0x0004
135#define EVP_PKT_SIGN 0x0010
136#define EVP_PKT_ENC 0x0020
137#define EVP_PKT_EXCH 0x0040
138#define EVP_PKS_RSA 0x0100
139#define EVP_PKS_DSA 0x0200
140#define EVP_PKT_EXP 0x1000 /* <= 512 bit key */
141
142#define EVP_PKEY_NONE NID_undef
143#define EVP_PKEY_RSA NID_rsaEncryption
144#define EVP_PKEY_RSA2 NID_rsa
145#define EVP_PKEY_DSA NID_dsa
146#define EVP_PKEY_DSA1 NID_dsa_2
147#define EVP_PKEY_DSA2 NID_dsaWithSHA
148#define EVP_PKEY_DSA3 NID_dsaWithSHA1
149#define EVP_PKEY_DSA4 NID_dsaWithSHA1_2
150#define EVP_PKEY_DH NID_dhKeyAgreement
151
152/* Type needs to be a bit field
153 * Sub-type needs to be for variations on the method, as in, can it do
154 * arbitary encryption.... */
155typedef struct evp_pkey_st
156 {
157 int type;
158 int save_type;
159 int references;
160 union {
161 char *ptr;
162 struct rsa_st *rsa; /* RSA */
163 struct dsa_st *dsa; /* DSA */
164 struct dh_st *dh; /* DH */
165 } pkey;
166 int save_parameters;
167#ifdef HEADER_STACK_H
168 STACK /* X509_ATTRIBUTE */ *attributes; /* [ 0 ] */
169#else
170 char /* X509_ATTRIBUTE */ *attributes; /* [ 0 ] */
171#endif
172 } EVP_PKEY;
173
174#define EVP_PKEY_MO_SIGN 0x0001
175#define EVP_PKEY_MO_VERIFY 0x0002
176#define EVP_PKEY_MO_ENCRYPT 0x0004
177#define EVP_PKEY_MO_DECRYPT 0x0008
178
179#if 0
180/* This structure is required to tie the message digest and signing together.
181 * The lookup can be done by md/pkey_method, oid, oid/pkey_method, or
182 * oid, md and pkey.
183 * This is required because for various smart-card perform the digest and
184 * signing/verification on-board. To handle this case, the specific
185 * EVP_MD and EVP_PKEY_METHODs need to be closely associated.
186 * When a PKEY is created, it will have a EVP_PKEY_METHOD ossociated with it.
187 * This can either be software or a token to provide the required low level
188 * routines.
189 */
190typedef struct evp_pkey_md_st
191 {
192 int oid;
193 EVP_MD *md;
194 EVP_PKEY_METHOD *pkey;
195 } EVP_PKEY_MD;
196
197#define EVP_rsa_md2()
198 EVP_PKEY_MD_add(NID_md2WithRSAEncryption,\
199 EVP_rsa_pkcs1(),EVP_md2())
200#define EVP_rsa_md5()
201 EVP_PKEY_MD_add(NID_md5WithRSAEncryption,\
202 EVP_rsa_pkcs1(),EVP_md5())
203#define EVP_rsa_sha0()
204 EVP_PKEY_MD_add(NID_shaWithRSAEncryption,\
205 EVP_rsa_pkcs1(),EVP_sha())
206#define EVP_rsa_sha1()
207 EVP_PKEY_MD_add(NID_sha1WithRSAEncryption,\
208 EVP_rsa_pkcs1(),EVP_sha1())
209#define EVP_rsa_ripemd160()
210 EVP_PKEY_MD_add(NID_ripemd160WithRSA,\
211 EVP_rsa_pkcs1(),EVP_ripemd160())
212#define EVP_rsa_mdc2()
213 EVP_PKEY_MD_add(NID_mdc2WithRSA,\
214 EVP_rsa_octet_string(),EVP_mdc2())
215#define EVP_dsa_sha()
216 EVP_PKEY_MD_add(NID_dsaWithSHA,\
217 EVP_dsa(),EVP_mdc2())
218#define EVP_dsa_sha1()
219 EVP_PKEY_MD_add(NID_dsaWithSHA1,\
220 EVP_dsa(),EVP_sha1())
221
222typedef struct evp_pkey_method_st
223 {
224 char *name;
225 int flags;
226 int type; /* RSA, DSA, an SSLeay specific constant */
227 int oid; /* For the pub-key type */
228 int encrypt_oid; /* pub/priv key encryption */
229
230 int (*sign)();
231 int (*verify)();
232 struct {
233 int
234 int (*set)(); /* get and/or set the underlying type */
235 int (*get)();
236 int (*encrypt)();
237 int (*decrypt)();
238 int (*i2d)();
239 int (*d2i)();
240 int (*dup)();
241 } pub,priv;
242 int (*set_asn1_parameters)();
243 int (*get_asn1_parameters)();
244 } EVP_PKEY_METHOD;
245#endif
246
247#ifndef EVP_MD
248typedef struct env_md_st
249 {
250 int type;
251 int pkey_type;
252 int md_size;
253 void (*init)();
254 void (*update)();
255 void (*final)();
256
257 int (*sign)();
258 int (*verify)();
259 int required_pkey_type[5]; /*EVP_PKEY_xxx */
260 int block_size;
261 int ctx_size; /* how big does the ctx need to be */
262 } EVP_MD;
263
264#define EVP_PKEY_NULL_method NULL,NULL,{0,0,0,0}
265
266#ifndef NO_DSA
267#define EVP_PKEY_DSA_method DSA_sign,DSA_verify, \
268 {EVP_PKEY_DSA,EVP_PKEY_DSA2,EVP_PKEY_DSA3, \
269 EVP_PKEY_DSA4,0}
270#else
271#define EVP_PKEY_DSA_method EVP_PKEY_NULL_method
272#endif
273
274#ifndef NO_RSA
275#define EVP_PKEY_RSA_method RSA_sign,RSA_verify, \
276 {EVP_PKEY_RSA,EVP_PKEY_RSA2,0,0}
277#define EVP_PKEY_RSA_ASN1_OCTET_STRING_method \
278 RSA_sign_ASN1_OCTET_STRING, \
279 RSA_verify_ASN1_OCTET_STRING, \
280 {EVP_PKEY_RSA,EVP_PKEY_RSA2,0,0}
281#else
282#define EVP_PKEY_RSA_method EVP_PKEY_NULL_method
283#define EVP_PKEY_RSA_ASN1_OCTET_STRING_method EVP_PKEY_NULL_method
284#endif
285
286#endif /* !EVP_MD */
287
288typedef struct env_md_ctx_st
289 {
290 EVP_MD *digest;
291 union {
292 unsigned char base[4];
293#ifndef NO_MD2
294 MD2_CTX md2;
295#endif
296#ifndef NO_MD5
297 MD5_CTX md5;
298#endif
299#ifndef NO_MD5
300 RIPEMD160_CTX ripemd160;
301#endif
302#if !defined(NO_SHA) || !defined(NO_SHA1)
303 SHA_CTX sha;
304#endif
305#ifndef NO_MDC2
306 MDC2_CTX mdc2;
307#endif
308 } md;
309 } EVP_MD_CTX;
310
311typedef struct evp_cipher_st
312 {
313 int nid;
314 int block_size;
315 int key_len;
316 int iv_len;
317 void (*init)(); /* init for encryption */
318 void (*do_cipher)(); /* encrypt data */
319 void (*cleanup)(); /* used by cipher method */
320 int ctx_size; /* how big the ctx needs to be */
321 /* int set_asn1_parameters(EVP_CIPHER_CTX,ASN1_TYPE *); */
322 int (*set_asn1_parameters)(); /* Populate a ASN1_TYPE with parameters */
323 /* int get_asn1_parameters(EVP_CIPHER_CTX,ASN1_TYPE *); */
324 int (*get_asn1_parameters)(); /* Get parameters from a ASN1_TYPE */
325 } EVP_CIPHER;
326
327typedef struct evp_cipher_info_st
328 {
329 EVP_CIPHER *cipher;
330 unsigned char iv[EVP_MAX_IV_LENGTH];
331 } EVP_CIPHER_INFO;
332
333typedef struct evp_cipher_ctx_st
334 {
335 EVP_CIPHER *cipher;
336 int encrypt; /* encrypt or decrypt */
337 int buf_len; /* number we have left */
338
339 unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
340 unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
341 unsigned char buf[EVP_MAX_IV_LENGTH]; /* saved partial block */
342 int num; /* used by cfb/ofb mode */
343
344 char *app_data; /* aplication stuff */
345 union {
346#ifndef NO_RC4
347 struct
348 {
349 unsigned char key[EVP_RC4_KEY_SIZE];
350 RC4_KEY ks; /* working key */
351 } rc4;
352#endif
353#ifndef NO_DES
354 des_key_schedule des_ks;/* key schedule */
355 struct
356 {
357 des_key_schedule ks;/* key schedule */
358 C_Block inw;
359 C_Block outw;
360 } desx_cbc;
361 struct
362 {
363 des_key_schedule ks1;/* key schedule */
364 des_key_schedule ks2;/* key schedule (for ede) */
365 des_key_schedule ks3;/* key schedule (for ede3) */
366 } des_ede;
367#endif
368#ifndef NO_IDEA
369 IDEA_KEY_SCHEDULE idea_ks;/* key schedule */
370#endif
371#ifndef NO_RC2
372 RC2_KEY rc2_ks;/* key schedule */
373#endif
374#ifndef NO_RC5
375 RC5_32_KEY rc5_ks;/* key schedule */
376#endif
377#ifndef NO_BLOWFISH
378 BF_KEY bf_ks;/* key schedule */
379#endif
380#ifndef NO_CAST
381 CAST_KEY cast_ks;/* key schedule */
382#endif
383 } c;
384 } EVP_CIPHER_CTX;
385
386typedef struct evp_Encode_Ctx_st
387 {
388 int num; /* number saved in a partial encode/decode */
389 int length; /* The length is either the output line length
390 * (in input bytes) or the shortest input line
391 * length that is ok. Once decoding begins,
392 * the length is adjusted up each time a longer
393 * line is decoded */
394 unsigned char enc_data[80]; /* data to encode */
395 int line_num; /* number read on current line */
396 int expect_nl;
397 } EVP_ENCODE_CTX;
398
399#define EVP_PKEY_assign_RSA(pkey,rsa) EVP_PKEY_assign((pkey),EVP_PKEY_RSA,\
400 (char *)(rsa))
401#define EVP_PKEY_assign_DSA(pkey,dsa) EVP_PKEY_assign((pkey),EVP_PKEY_DSA,\
402 (char *)(dsa))
403#define EVP_PKEY_assign_DH(pkey,dh) EVP_PKEY_assign((pkey),EVP_PKEY_DH,\
404 (char *)(dh))
405
406/* Add some extra combinations */
407#define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a))
408#define EVP_get_digestbyobj(a) EVP_get_digestbynid(OBJ_obj2nid(a))
409#define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a))
410#define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a))
411
412#define EVP_MD_type(e) ((e)->type)
413#define EVP_MD_pkey_type(e) ((e)->pkey_type)
414#define EVP_MD_size(e) ((e)->md_size)
415#define EVP_MD_block_size(e) ((e)->block_size)
416
417#define EVP_MD_CTX_size(e) EVP_MD_size((e)->digest)
418#define EVP_MD_CTX_block_size(e) EVP_MD_block_size((e)->digest)
419#define EVP_MD_CTX_type(e) ((e)->digest)
420
421#define EVP_CIPHER_nid(e) ((e)->nid)
422#define EVP_CIPHER_block_size(e) ((e)->block_size)
423#define EVP_CIPHER_key_length(e) ((e)->key_len)
424#define EVP_CIPHER_iv_length(e) ((e)->iv_len)
425
426#define EVP_CIPHER_CTX_cipher(e) ((e)->cipher)
427#define EVP_CIPHER_CTX_nid(e) ((e)->cipher->nid)
428#define EVP_CIPHER_CTX_block_size(e) ((e)->cipher->block_size)
429#define EVP_CIPHER_CTX_key_length(e) ((e)->cipher->key_len)
430#define EVP_CIPHER_CTX_iv_length(e) ((e)->cipher->iv_len)
431#define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data)
432#define EVP_CIPHER_CTX_set_app_data(e,d) ((e)->app_data=(char *)(d))
433
434#define EVP_ENCODE_LENGTH(l) (((l+2)/3*4)+(l/48+1)*2+80)
435#define EVP_DECODE_LENGTH(l) ((l+3)/4*3+80)
436
437#define EVP_SignInit(a,b) EVP_DigestInit(a,b)
438#define EVP_SignUpdate(a,b,c) EVP_DigestUpdate(a,b,c)
439#define EVP_VerifyInit(a,b) EVP_DigestInit(a,b)
440#define EVP_VerifyUpdate(a,b,c) EVP_DigestUpdate(a,b,c)
441#define EVP_OpenUpdate(a,b,c,d,e) EVP_DecryptUpdate(a,b,c,d,e)
442#define EVP_SealUpdate(a,b,c,d,e) EVP_EncryptUpdate(a,b,c,d,e)
443
444#define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,0,(char *)md)
445#define BIO_get_md(b,mdp) BIO_ctrl(b,BIO_C_GET_MD,0,(char *)mdp)
446#define BIO_get_md_ctx(b,mdcp) BIO_ctrl(b,BIO_C_GET_MD_CTX,0,(char *)mdcp)
447#define BIO_get_cipher_status(b) BIO_ctrl(b,BIO_C_GET_CIPHER_STATUS,0,NULL)
448
449#define EVP_Cipher(c,o,i,l) (c)->cipher->do_cipher((c),(o),(i),(l))
450
451#ifndef NOPROTO
452
453void EVP_DigestInit(EVP_MD_CTX *ctx, EVP_MD *type);
454void EVP_DigestUpdate(EVP_MD_CTX *ctx,unsigned char *d,unsigned int cnt);
455void EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
456
457int EVP_read_pw_string(char *buf,int length,char *prompt,int verify);
458void EVP_set_pw_prompt(char *prompt);
459char * EVP_get_pw_prompt(void);
460
461int EVP_BytesToKey(EVP_CIPHER *type,EVP_MD *md,unsigned char *salt,
462 unsigned char *data, int datal, int count,
463 unsigned char *key,unsigned char *iv);
464
465EVP_CIPHER *EVP_get_cipherbyname(char *name);
466
467void EVP_EncryptInit(EVP_CIPHER_CTX *ctx,EVP_CIPHER *type,
468 unsigned char *key, unsigned char *iv);
469void EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
470 int *outl, unsigned char *in, int inl);
471void EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
472
473void EVP_DecryptInit(EVP_CIPHER_CTX *ctx,EVP_CIPHER *type,
474 unsigned char *key, unsigned char *iv);
475void EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
476 int *outl, unsigned char *in, int inl);
477int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
478
479void EVP_CipherInit(EVP_CIPHER_CTX *ctx,EVP_CIPHER *type, unsigned char *key,
480 unsigned char *iv,int enc);
481void EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
482 int *outl, unsigned char *in, int inl);
483int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
484
485int EVP_SignFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s,
486 EVP_PKEY *pkey);
487
488int EVP_VerifyFinal(EVP_MD_CTX *ctx,unsigned char *sigbuf,
489 unsigned int siglen,EVP_PKEY *pkey);
490
491int EVP_OpenInit(EVP_CIPHER_CTX *ctx,EVP_CIPHER *type,unsigned char *ek,
492 int ekl,unsigned char *iv,EVP_PKEY *priv);
493int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
494
495int EVP_SealInit(EVP_CIPHER_CTX *ctx, EVP_CIPHER *type, unsigned char **ek,
496 int *ekl, unsigned char *iv,EVP_PKEY **pubk, int npubk);
497void EVP_SealFinal(EVP_CIPHER_CTX *ctx,unsigned char *out,int *outl);
498
499void EVP_EncodeInit(EVP_ENCODE_CTX *ctx);
500void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx,unsigned char *out,
501 int *outl,unsigned char *in,int inl);
502void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx,unsigned char *out,int *outl);
503int EVP_EncodeBlock(unsigned char *t, unsigned char *f, int n);
504
505void EVP_DecodeInit(EVP_ENCODE_CTX *ctx);
506int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx,unsigned char *out,int *outl,
507 unsigned char *in, int inl);
508int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned
509 char *out, int *outl);
510int EVP_DecodeBlock(unsigned char *t, unsigned
511 char *f, int n);
512
513void ERR_load_EVP_strings(void );
514
515void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a);
516void EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a);
517
518#ifdef HEADER_BIO_H
519BIO_METHOD *BIO_f_md(void);
520BIO_METHOD *BIO_f_base64(void);
521BIO_METHOD *BIO_f_cipher(void);
522void BIO_set_cipher(BIO *b,EVP_CIPHER *c,unsigned char *k,
523 unsigned char *i, int enc);
524#endif
525
526EVP_MD *EVP_md_null(void);
527EVP_MD *EVP_md2(void);
528EVP_MD *EVP_md5(void);
529EVP_MD *EVP_sha(void);
530EVP_MD *EVP_sha1(void);
531EVP_MD *EVP_dss(void);
532EVP_MD *EVP_dss1(void);
533EVP_MD *EVP_mdc2(void);
534EVP_MD *EVP_ripemd160(void);
535
536EVP_CIPHER *EVP_enc_null(void); /* does nothing :-) */
537EVP_CIPHER *EVP_des_ecb(void);
538EVP_CIPHER *EVP_des_ede(void);
539EVP_CIPHER *EVP_des_ede3(void);
540EVP_CIPHER *EVP_des_cfb(void);
541EVP_CIPHER *EVP_des_ede_cfb(void);
542EVP_CIPHER *EVP_des_ede3_cfb(void);
543EVP_CIPHER *EVP_des_ofb(void);
544EVP_CIPHER *EVP_des_ede_ofb(void);
545EVP_CIPHER *EVP_des_ede3_ofb(void);
546EVP_CIPHER *EVP_des_cbc(void);
547EVP_CIPHER *EVP_des_ede_cbc(void);
548EVP_CIPHER *EVP_des_ede3_cbc(void);
549EVP_CIPHER *EVP_desx_cbc(void);
550EVP_CIPHER *EVP_rc4(void);
551EVP_CIPHER *EVP_rc4_40(void);
552EVP_CIPHER *EVP_idea_ecb(void);
553EVP_CIPHER *EVP_idea_cfb(void);
554EVP_CIPHER *EVP_idea_ofb(void);
555EVP_CIPHER *EVP_idea_cbc(void);
556EVP_CIPHER *EVP_rc2_ecb(void);
557EVP_CIPHER *EVP_rc2_cbc(void);
558EVP_CIPHER *EVP_rc2_40_cbc(void);
559EVP_CIPHER *EVP_rc2_cfb(void);
560EVP_CIPHER *EVP_rc2_ofb(void);
561EVP_CIPHER *EVP_bf_ecb(void);
562EVP_CIPHER *EVP_bf_cbc(void);
563EVP_CIPHER *EVP_bf_cfb(void);
564EVP_CIPHER *EVP_bf_ofb(void);
565EVP_CIPHER *EVP_cast5_ecb(void);
566EVP_CIPHER *EVP_cast5_cbc(void);
567EVP_CIPHER *EVP_cast5_cfb(void);
568EVP_CIPHER *EVP_cast5_ofb(void);
569EVP_CIPHER *EVP_rc5_32_12_16_cbc(void);
570EVP_CIPHER *EVP_rc5_32_12_16_ecb(void);
571EVP_CIPHER *EVP_rc5_32_12_16_cfb(void);
572EVP_CIPHER *EVP_rc5_32_12_16_ofb(void);
573
574void SSLeay_add_all_algorithms(void);
575void SSLeay_add_all_ciphers(void);
576void SSLeay_add_all_digests(void);
577
578int EVP_add_cipher(EVP_CIPHER *cipher);
579int EVP_add_digest(EVP_MD *digest);
580int EVP_add_alias(char *name,char *alias);
581int EVP_delete_alias(char *name);
582
583EVP_CIPHER *EVP_get_cipherbyname(char *name);
584EVP_MD *EVP_get_digestbyname(char *name);
585void EVP_cleanup(void);
586
587int EVP_PKEY_decrypt(unsigned char *dec_key,unsigned char *enc_key,
588 int enc_key_len,EVP_PKEY *private_key);
589int EVP_PKEY_encrypt(unsigned char *enc_key,
590 unsigned char *key,int key_len,EVP_PKEY *pub_key);
591int EVP_PKEY_type(int type);
592int EVP_PKEY_bits(EVP_PKEY *pkey);
593int EVP_PKEY_size(EVP_PKEY *pkey);
594int EVP_PKEY_assign(EVP_PKEY *pkey,int type,char *key);
595EVP_PKEY * EVP_PKEY_new(void);
596void EVP_PKEY_free(EVP_PKEY *pkey);
597EVP_PKEY * d2i_PublicKey(int type,EVP_PKEY **a, unsigned char **pp,
598 long length);
599int i2d_PublicKey(EVP_PKEY *a, unsigned char **pp);
600
601EVP_PKEY * d2i_PrivateKey(int type,EVP_PKEY **a, unsigned char **pp,
602 long length);
603int i2d_PrivateKey(EVP_PKEY *a, unsigned char **pp);
604
605int EVP_PKEY_copy_parameters(EVP_PKEY *to,EVP_PKEY *from);
606int EVP_PKEY_missing_parameters(EVP_PKEY *pkey);
607int EVP_PKEY_save_parameters(EVP_PKEY *pkey,int mode);
608int EVP_PKEY_cmp_parameters(EVP_PKEY *a,EVP_PKEY *b);
609
610/* calls methods */
611int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
612int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
613
614/* These are used by EVP_CIPHER methods */
615int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c,ASN1_TYPE *type);
616int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c,ASN1_TYPE *type);
617
618#else
619
620void EVP_DigestInit();
621void EVP_DigestUpdate();
622void EVP_DigestFinal();
623
624int EVP_read_pw_string();
625void EVP_set_pw_prompt();
626char * EVP_get_pw_prompt();
627
628int EVP_BytesToKey();
629
630EVP_CIPHER *EVP_get_cipherbyname();
631
632void EVP_EncryptInit();
633void EVP_EncryptUpdate();
634void EVP_EncryptFinal();
635
636void EVP_DecryptInit();
637void EVP_DecryptUpdate();
638int EVP_DecryptFinal();
639
640void EVP_CipherInit();
641void EVP_CipherUpdate();
642int EVP_CipherFinal();
643
644int EVP_SignFinal();
645
646int EVP_VerifyFinal();
647
648int EVP_OpenInit();
649int EVP_OpenFinal();
650
651int EVP_SealInit();
652void EVP_SealFinal();
653
654void EVP_EncodeInit();
655void EVP_EncodeUpdate();
656void EVP_EncodeFinal();
657int EVP_EncodeBlock();
658
659void EVP_DecodeInit();
660int EVP_DecodeUpdate();
661int EVP_DecodeFinal();
662int EVP_DecodeBlock();
663
664void ERR_load_EVP_strings();
665
666void EVP_CIPHER_CTX_init();
667void EVP_CIPHER_CTX_cleanup();
668
669#ifdef HEADER_BIO_H
670BIO_METHOD *BIO_f_md();
671BIO_METHOD *BIO_f_base64();
672BIO_METHOD *BIO_f_cipher();
673void BIO_set_cipher();
674#endif
675
676EVP_MD *EVP_md_null();
677EVP_MD *EVP_md2();
678EVP_MD *EVP_md5();
679EVP_MD *EVP_sha();
680EVP_MD *EVP_sha1();
681EVP_MD *EVP_dss();
682EVP_MD *EVP_dss1();
683EVP_MD *EVP_mdc2();
684
685EVP_CIPHER *EVP_enc_null();
686EVP_CIPHER *EVP_des_ecb();
687EVP_CIPHER *EVP_des_ede();
688EVP_CIPHER *EVP_des_ede3();
689EVP_CIPHER *EVP_des_cfb();
690EVP_CIPHER *EVP_des_ede_cfb();
691EVP_CIPHER *EVP_des_ede3_cfb();
692EVP_CIPHER *EVP_des_ofb();
693EVP_CIPHER *EVP_des_ede_ofb();
694EVP_CIPHER *EVP_des_ede3_ofb();
695EVP_CIPHER *EVP_des_cbc();
696EVP_CIPHER *EVP_des_ede_cbc();
697EVP_CIPHER *EVP_des_ede3_cbc();
698EVP_CIPHER *EVP_desx_cbc();
699EVP_CIPHER *EVP_rc4();
700EVP_CIPHER *EVP_rc4_40();
701EVP_CIPHER *EVP_idea_ecb();
702EVP_CIPHER *EVP_idea_cfb();
703EVP_CIPHER *EVP_idea_ofb();
704EVP_CIPHER *EVP_idea_cbc();
705EVP_CIPHER *EVP_rc2_ecb();
706EVP_CIPHER *EVP_rc2_cbc();
707EVP_CIPHER *EVP_rc2_40_cbc();
708EVP_CIPHER *EVP_rc2_cfb();
709EVP_CIPHER *EVP_rc2_ofb();
710EVP_CIPHER *EVP_bf_ecb();
711EVP_CIPHER *EVP_bf_cbc();
712EVP_CIPHER *EVP_bf_cfb();
713EVP_CIPHER *EVP_bf_ofb();
714EVP_CIPHER *EVP_cast5_ecb();
715EVP_CIPHER *EVP_cast5_cbc();
716EVP_CIPHER *EVP_cast5_cfb();
717EVP_CIPHER *EVP_cast5_ofb();
718EVP_CIPHER *EVP_rc5_32_12_16_cbc();
719EVP_CIPHER *EVP_rc5_32_12_16_ecb();
720EVP_CIPHER *EVP_rc5_32_12_16_cfb();
721EVP_CIPHER *EVP_rc5_32_12_16_ofb();
722
723void SSLeay_add_all_algorithms();
724void SSLeay_add_all_ciphers();
725void SSLeay_add_all_digests();
726
727int EVP_add_cipher();
728int EVP_add_digest();
729int EVP_add_alias();
730int EVP_delete_alias();
731
732EVP_CIPHER *EVP_get_cipherbyname();
733EVP_MD *EVP_get_digestbyname();
734void EVP_cleanup();
735
736int EVP_PKEY_decrypt();
737int EVP_PKEY_encrypt();
738int EVP_PKEY_type();
739int EVP_PKEY_bits();
740int EVP_PKEY_size();
741int EVP_PKEY_assign();
742EVP_PKEY * EVP_PKEY_new();
743void EVP_PKEY_free();
744EVP_PKEY * d2i_PublicKey();
745int i2d_PublicKey();
746
747EVP_PKEY * d2i_PrivateKey();
748int i2d_PrivateKey();
749
750int EVP_PKEY_copy_parameters();
751int EVP_PKEY_missing_parameters();
752int EVP_PKEY_save_parameters();
753int EVP_PKEY_cmp_parameters();
754
755int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
756int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
757
758int EVP_CIPHER_set_asn1_iv();
759int EVP_CIPHER_get_asn1_iv();
760
761#endif
762
763/* BEGIN ERROR CODES */
764/* Error codes for the EVP functions. */
765
766/* Function codes. */
767#define EVP_F_D2I_PKEY 100
768#define EVP_F_EVP_DECRYPTFINAL 101
769#define EVP_F_EVP_OPENINIT 102
770#define EVP_F_EVP_PKEY_COPY_PARAMETERS 103
771#define EVP_F_EVP_PKEY_DECRYPT 104
772#define EVP_F_EVP_PKEY_ENCRYPT 105
773#define EVP_F_EVP_PKEY_NEW 106
774#define EVP_F_EVP_SIGNFINAL 107
775#define EVP_F_EVP_VERIFYFINAL 108
776
777/* Reason codes. */
778#define EVP_R_BAD_DECRYPT 100
779#define EVP_R_DIFFERENT_KEY_TYPES 101
780#define EVP_R_IV_TOO_LARGE 102
781#define EVP_R_MISSING_PARMATERS 103
782#define EVP_R_NO_SIGN_FUNCTION_CONFIGURED 104
783#define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105
784#define EVP_R_PUBLIC_KEY_NOT_RSA 106
785#define EVP_R_UNSUPPORTED_CIPHER 107
786#define EVP_R_WRONG_FINAL_BLOCK_LENGTH 108
787#define EVP_R_WRONG_PUBLIC_KEY_TYPE 109
788
789#ifdef __cplusplus
790}
791#endif
792#endif
793
diff --git a/src/lib/libcrypto/evp/evp_enc.c b/src/lib/libcrypto/evp/evp_enc.c
new file mode 100644
index 0000000000..93cc3a9464
--- /dev/null
+++ b/src/lib/libcrypto/evp/evp_enc.c
@@ -0,0 +1,303 @@
1/* crypto/evp/evp_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62
63char *EVP_version="EVP part of SSLeay 0.9.0b 29-Jun-1998";
64
65void EVP_CIPHER_CTX_init(ctx)
66EVP_CIPHER_CTX *ctx;
67 {
68 memset(ctx,0,sizeof(EVP_CIPHER_CTX));
69 /* ctx->cipher=NULL; */
70 }
71
72void EVP_CipherInit(ctx,data,key,iv,enc)
73EVP_CIPHER_CTX *ctx;
74EVP_CIPHER *data;
75unsigned char *key;
76unsigned char *iv;
77int enc;
78 {
79 if (enc)
80 EVP_EncryptInit(ctx,data,key,iv);
81 else
82 EVP_DecryptInit(ctx,data,key,iv);
83 }
84
85void EVP_CipherUpdate(ctx,out,outl,in,inl)
86EVP_CIPHER_CTX *ctx;
87unsigned char *out;
88int *outl;
89unsigned char *in;
90int inl;
91 {
92 if (ctx->encrypt)
93 EVP_EncryptUpdate(ctx,out,outl,in,inl);
94 else EVP_DecryptUpdate(ctx,out,outl,in,inl);
95 }
96
97int EVP_CipherFinal(ctx,out,outl)
98EVP_CIPHER_CTX *ctx;
99unsigned char *out;
100int *outl;
101 {
102 if (ctx->encrypt)
103 {
104 EVP_EncryptFinal(ctx,out,outl);
105 return(1);
106 }
107 else return(EVP_DecryptFinal(ctx,out,outl));
108 }
109
110void EVP_EncryptInit(ctx,cipher,key,iv)
111EVP_CIPHER_CTX *ctx;
112EVP_CIPHER *cipher;
113unsigned char *key;
114unsigned char *iv;
115 {
116 if (cipher != NULL)
117 ctx->cipher=cipher;
118 ctx->cipher->init(ctx,key,iv,1);
119 ctx->encrypt=1;
120 ctx->buf_len=0;
121 }
122
123void EVP_DecryptInit(ctx,cipher,key,iv)
124EVP_CIPHER_CTX *ctx;
125EVP_CIPHER *cipher;
126unsigned char *key;
127unsigned char *iv;
128 {
129 if (cipher != NULL)
130 ctx->cipher=cipher;
131 ctx->cipher->init(ctx,key,iv,0);
132 ctx->encrypt=0;
133 ctx->buf_len=0;
134 }
135
136
137void EVP_EncryptUpdate(ctx,out,outl,in,inl)
138EVP_CIPHER_CTX *ctx;
139unsigned char *out;
140int *outl;
141unsigned char *in;
142int inl;
143 {
144 int i,j,bl;
145
146 i=ctx->buf_len;
147 bl=ctx->cipher->block_size;
148 *outl=0;
149 if ((inl == 0) && (i != bl)) return;
150 if (i != 0)
151 {
152 if (i+inl < bl)
153 {
154 memcpy(&(ctx->buf[i]),in,inl);
155 ctx->buf_len+=inl;
156 return;
157 }
158 else
159 {
160 j=bl-i;
161 if (j != 0) memcpy(&(ctx->buf[i]),in,j);
162 ctx->cipher->do_cipher(ctx,out,ctx->buf,bl);
163 inl-=j;
164 in+=j;
165 out+=bl;
166 *outl+=bl;
167 }
168 }
169 i=inl%bl; /* how much is left */
170 inl-=i;
171 if (inl > 0)
172 {
173 ctx->cipher->do_cipher(ctx,out,in,inl);
174 *outl+=inl;
175 }
176
177 if (i != 0)
178 memcpy(ctx->buf,&(in[inl]),i);
179 ctx->buf_len=i;
180 }
181
182void EVP_EncryptFinal(ctx,out,outl)
183EVP_CIPHER_CTX *ctx;
184unsigned char *out;
185int *outl;
186 {
187 int i,n,b,bl;
188
189 b=ctx->cipher->block_size;
190 if (b == 1)
191 {
192 *outl=0;
193 return;
194 }
195 bl=ctx->buf_len;
196 n=b-bl;
197 for (i=bl; i<b; i++)
198 ctx->buf[i]=n;
199 ctx->cipher->do_cipher(ctx,out,ctx->buf,b);
200 *outl=b;
201 }
202
203void EVP_DecryptUpdate(ctx,out,outl,in,inl)
204EVP_CIPHER_CTX *ctx;
205unsigned char *out;
206int *outl;
207unsigned char *in;
208int inl;
209 {
210 int b,bl,n;
211 int keep_last=0;
212
213 *outl=0;
214 if (inl == 0) return;
215
216 b=ctx->cipher->block_size;
217 if (b > 1)
218 {
219 /* Is the input a multiple of the block size? */
220 bl=ctx->buf_len;
221 n=inl+bl;
222 if (n%b == 0)
223 {
224 if (inl < b) /* must be 'just one' buff */
225 {
226 memcpy(&(ctx->buf[bl]),in,inl);
227 ctx->buf_len=b;
228 *outl=0;
229 return;
230 }
231 keep_last=1;
232 inl-=b; /* don't do the last block */
233 }
234 }
235 EVP_EncryptUpdate(ctx,out,outl,in,inl);
236
237 /* if we have 'decrypted' a multiple of block size, make sure
238 * we have a copy of this last block */
239 if (keep_last)
240 {
241 memcpy(&(ctx->buf[0]),&(in[inl]),b);
242#ifdef DEBUG
243 if (ctx->buf_len != 0)
244 {
245 abort();
246 }
247#endif
248 ctx->buf_len=b;
249 }
250 }
251
252int EVP_DecryptFinal(ctx,out,outl)
253EVP_CIPHER_CTX *ctx;
254unsigned char *out;
255int *outl;
256 {
257 int i,b;
258 int n;
259
260 *outl=0;
261 b=ctx->cipher->block_size;
262 if (b > 1)
263 {
264 if (ctx->buf_len != b)
265 {
266 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_WRONG_FINAL_BLOCK_LENGTH);
267 return(0);
268 }
269 EVP_EncryptUpdate(ctx,ctx->buf,&n,ctx->buf,0);
270 if (n != b)
271 return(0);
272 n=ctx->buf[b-1];
273 if (n > b)
274 {
275 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
276 return(0);
277 }
278 for (i=0; i<n; i++)
279 {
280 if (ctx->buf[--b] != n)
281 {
282 EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
283 return(0);
284 }
285 }
286 n=ctx->cipher->block_size-n;
287 for (i=0; i<n; i++)
288 out[i]=ctx->buf[i];
289 *outl=n;
290 }
291 else
292 *outl=0;
293 return(1);
294 }
295
296void EVP_CIPHER_CTX_cleanup(c)
297EVP_CIPHER_CTX *c;
298 {
299 if ((c->cipher != NULL) && (c->cipher->cleanup != NULL))
300 c->cipher->cleanup(c);
301 memset(c,0,sizeof(EVP_CIPHER_CTX));
302 }
303
diff --git a/src/lib/libcrypto/evp/evp_err.c b/src/lib/libcrypto/evp/evp_err.c
new file mode 100644
index 0000000000..2b0a0ab93f
--- /dev/null
+++ b/src/lib/libcrypto/evp/evp_err.c
@@ -0,0 +1,108 @@
1/* lib/evp/evp_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "evp.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA EVP_str_functs[]=
65 {
66{ERR_PACK(0,EVP_F_D2I_PKEY,0), "D2I_PKEY"},
67{ERR_PACK(0,EVP_F_EVP_DECRYPTFINAL,0), "EVP_DecryptFinal"},
68{ERR_PACK(0,EVP_F_EVP_OPENINIT,0), "EVP_OpenInit"},
69{ERR_PACK(0,EVP_F_EVP_PKEY_COPY_PARAMETERS,0), "EVP_PKEY_copy_parameters"},
70{ERR_PACK(0,EVP_F_EVP_PKEY_DECRYPT,0), "EVP_PKEY_decrypt"},
71{ERR_PACK(0,EVP_F_EVP_PKEY_ENCRYPT,0), "EVP_PKEY_encrypt"},
72{ERR_PACK(0,EVP_F_EVP_PKEY_NEW,0), "EVP_PKEY_new"},
73{ERR_PACK(0,EVP_F_EVP_SIGNFINAL,0), "EVP_SignFinal"},
74{ERR_PACK(0,EVP_F_EVP_VERIFYFINAL,0), "EVP_VerifyFinal"},
75{0,NULL},
76 };
77
78static ERR_STRING_DATA EVP_str_reasons[]=
79 {
80{EVP_R_BAD_DECRYPT ,"bad decrypt"},
81{EVP_R_DIFFERENT_KEY_TYPES ,"different key types"},
82{EVP_R_IV_TOO_LARGE ,"iv too large"},
83{EVP_R_MISSING_PARMATERS ,"missing parmaters"},
84{EVP_R_NO_SIGN_FUNCTION_CONFIGURED ,"no sign function configured"},
85{EVP_R_NO_VERIFY_FUNCTION_CONFIGURED ,"no verify function configured"},
86{EVP_R_PUBLIC_KEY_NOT_RSA ,"public key not rsa"},
87{EVP_R_UNSUPPORTED_CIPHER ,"unsupported cipher"},
88{EVP_R_WRONG_FINAL_BLOCK_LENGTH ,"wrong final block length"},
89{EVP_R_WRONG_PUBLIC_KEY_TYPE ,"wrong public key type"},
90{0,NULL},
91 };
92
93#endif
94
95void ERR_load_EVP_strings()
96 {
97 static int init=1;
98
99 if (init);
100 {;
101 init=0;
102#ifndef NO_ERR
103 ERR_load_strings(ERR_LIB_EVP,EVP_str_functs);
104 ERR_load_strings(ERR_LIB_EVP,EVP_str_reasons);
105#endif
106
107 }
108 }
diff --git a/src/lib/libcrypto/evp/evp_key.c b/src/lib/libcrypto/evp/evp_key.c
new file mode 100644
index 0000000000..dafa686f64
--- /dev/null
+++ b/src/lib/libcrypto/evp/evp_key.c
@@ -0,0 +1,167 @@
1/* crypto/evp/evp_key.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "x509.h"
62#include "objects.h"
63#include "evp.h"
64
65/* should be init to zeros. */
66static char prompt_string[80];
67
68void EVP_set_pw_prompt(prompt)
69char *prompt;
70 {
71 if (prompt == NULL)
72 prompt_string[0]='\0';
73 else
74 strncpy(prompt_string,prompt,79);
75 }
76
77char *EVP_get_pw_prompt()
78 {
79 if (prompt_string[0] == '\0')
80 return(NULL);
81 else
82 return(prompt_string);
83 }
84
85#ifdef NO_DES
86int des_read_pw_string(char *buf,int len,char *prompt,int verify);
87#endif
88
89int EVP_read_pw_string(buf,len,prompt,verify)
90char *buf;
91int len;
92char *prompt;
93int verify;
94 {
95 if ((prompt == NULL) && (prompt_string[0] != '\0'))
96 prompt=prompt_string;
97 return(des_read_pw_string(buf,len,prompt,verify));
98 }
99
100int EVP_BytesToKey(type,md,salt,data,datal,count,key,iv)
101EVP_CIPHER *type;
102EVP_MD *md;
103unsigned char *salt;
104unsigned char *data;
105int datal;
106int count;
107unsigned char *key;
108unsigned char *iv;
109 {
110 EVP_MD_CTX c;
111 unsigned char md_buf[EVP_MAX_MD_SIZE];
112 int niv,nkey,addmd=0;
113 unsigned int mds=0,i;
114
115 nkey=type->key_len;
116 niv=type->iv_len;
117
118 if (data == NULL) return(nkey);
119
120 for (;;)
121 {
122 EVP_DigestInit(&c,md);
123 if (addmd++)
124 EVP_DigestUpdate(&c,&(md_buf[0]),mds);
125 EVP_DigestUpdate(&c,data,datal);
126 if (salt != NULL)
127 EVP_DigestUpdate(&c,salt,8);
128 EVP_DigestFinal(&c,&(md_buf[0]),&mds);
129
130 for (i=1; i<(unsigned int)count; i++)
131 {
132 EVP_DigestInit(&c,md);
133 EVP_DigestUpdate(&c,&(md_buf[0]),mds);
134 EVP_DigestFinal(&c,&(md_buf[0]),&mds);
135 }
136 i=0;
137 if (nkey)
138 {
139 for (;;)
140 {
141 if (nkey == 0) break;
142 if (i == mds) break;
143 if (key != NULL)
144 *(key++)=md_buf[i];
145 nkey--;
146 i++;
147 }
148 }
149 if (niv && (i != mds))
150 {
151 for (;;)
152 {
153 if (niv == 0) break;
154 if (i == mds) break;
155 if (iv != NULL)
156 *(iv++)=md_buf[i];
157 niv--;
158 i++;
159 }
160 }
161 if ((nkey == 0) && (niv == 0)) break;
162 }
163 memset(&c,0,sizeof(c));
164 memset(&(md_buf[0]),0,EVP_MAX_MD_SIZE);
165 return(type->key_len);
166 }
167
diff --git a/src/lib/libcrypto/evp/evp_lib.c b/src/lib/libcrypto/evp/evp_lib.c
new file mode 100644
index 0000000000..69784eb555
--- /dev/null
+++ b/src/lib/libcrypto/evp/evp_lib.c
@@ -0,0 +1,117 @@
1/* crypto/evp/evp_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63
64int EVP_CIPHER_param_to_asn1(c,type)
65EVP_CIPHER_CTX *c;
66ASN1_TYPE *type;
67 {
68 int ret;
69
70 if (c->cipher->set_asn1_parameters != NULL)
71 ret=c->cipher->set_asn1_parameters(c,type);
72 else
73 ret=1;
74 return(ret);
75 }
76
77int EVP_CIPHER_asn1_to_param(c,type)
78EVP_CIPHER_CTX *c;
79ASN1_TYPE *type;
80 {
81 int ret;
82
83 if (c->cipher->get_asn1_parameters != NULL)
84 ret=c->cipher->get_asn1_parameters(c,type);
85 else
86 ret=1;
87 return(ret);
88 }
89
90int EVP_CIPHER_get_asn1_iv(c,type)
91EVP_CIPHER_CTX *c;
92ASN1_TYPE *type;
93 {
94 int i=0,l;
95
96 if (type != NULL)
97 {
98 l=EVP_CIPHER_CTX_iv_length(c);
99 i=ASN1_TYPE_get_octetstring(type,c->oiv,l);
100 memcpy(c->iv,c->oiv,l);
101 }
102 return(i);
103 }
104
105int EVP_CIPHER_set_asn1_iv(c,type)
106EVP_CIPHER_CTX *c;
107ASN1_TYPE *type;
108 {
109 int i=0,j;
110
111 if (type != NULL)
112 {
113 j=EVP_CIPHER_CTX_iv_length(c);
114 i=ASN1_TYPE_set_octetstring(type,c->oiv,j);
115 }
116 return(i);
117 }
diff --git a/src/lib/libcrypto/evp/m_dss.c b/src/lib/libcrypto/evp/m_dss.c
new file mode 100644
index 0000000000..3549b1699c
--- /dev/null
+++ b/src/lib/libcrypto/evp/m_dss.c
@@ -0,0 +1,82 @@
1/* crypto/evp/m_dss.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65static EVP_MD dsa_md=
66 {
67 NID_dsaWithSHA,
68 NID_dsaWithSHA,
69 SHA_DIGEST_LENGTH,
70 SHA1_Init,
71 SHA1_Update,
72 SHA1_Final,
73 EVP_PKEY_DSA_method,
74 SHA_CBLOCK,
75 sizeof(EVP_MD *)+sizeof(SHA_CTX),
76 };
77
78EVP_MD *EVP_dss()
79 {
80 return(&dsa_md);
81 }
82
diff --git a/src/lib/libcrypto/evp/m_dss1.c b/src/lib/libcrypto/evp/m_dss1.c
new file mode 100644
index 0000000000..ff256b7b20
--- /dev/null
+++ b/src/lib/libcrypto/evp/m_dss1.c
@@ -0,0 +1,81 @@
1/* crypto/evp/m_dss1.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65static EVP_MD dss1_md=
66 {
67 NID_dsa,
68 NID_dsaWithSHA1,
69 SHA_DIGEST_LENGTH,
70 SHA1_Init,
71 SHA1_Update,
72 SHA1_Final,
73 EVP_PKEY_DSA_method,
74 SHA_CBLOCK,
75 sizeof(EVP_MD *)+sizeof(SHA_CTX),
76 };
77
78EVP_MD *EVP_dss1()
79 {
80 return(&dss1_md);
81 }
diff --git a/src/lib/libcrypto/evp/m_md5.c b/src/lib/libcrypto/evp/m_md5.c
new file mode 100644
index 0000000000..d65db9aa1d
--- /dev/null
+++ b/src/lib/libcrypto/evp/m_md5.c
@@ -0,0 +1,81 @@
1/* crypto/evp/m_md5.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65static EVP_MD md5_md=
66 {
67 NID_md5,
68 NID_md5WithRSAEncryption,
69 MD5_DIGEST_LENGTH,
70 MD5_Init,
71 MD5_Update,
72 MD5_Final,
73 EVP_PKEY_RSA_method,
74 MD5_CBLOCK,
75 sizeof(EVP_MD *)+sizeof(MD5_CTX),
76 };
77
78EVP_MD *EVP_md5()
79 {
80 return(&md5_md);
81 }
diff --git a/src/lib/libcrypto/evp/m_null.c b/src/lib/libcrypto/evp/m_null.c
new file mode 100644
index 0000000000..6d80560df2
--- /dev/null
+++ b/src/lib/libcrypto/evp/m_null.c
@@ -0,0 +1,88 @@
1/* crypto/evp/m_null.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65static void function()
66 {
67 }
68
69static EVP_MD null_md=
70 {
71 NID_undef,
72 NID_undef,
73 0,
74 function,
75 function,
76 function,
77
78 EVP_PKEY_NULL_method,
79 0,
80 sizeof(EVP_MD *),
81 };
82
83EVP_MD *EVP_md_null()
84 {
85 return(&null_md);
86 }
87
88
diff --git a/src/lib/libcrypto/evp/m_ripemd.c b/src/lib/libcrypto/evp/m_ripemd.c
new file mode 100644
index 0000000000..04c5d8897b
--- /dev/null
+++ b/src/lib/libcrypto/evp/m_ripemd.c
@@ -0,0 +1,81 @@
1/* crypto/evp/m_ripemd.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65static EVP_MD ripemd160_md=
66 {
67 NID_ripemd160,
68 NID_ripemd160WithRSA,
69 RIPEMD160_DIGEST_LENGTH,
70 RIPEMD160_Init,
71 RIPEMD160_Update,
72 RIPEMD160_Final,
73 EVP_PKEY_RSA_method,
74 RIPEMD160_CBLOCK,
75 sizeof(EVP_MD *)+sizeof(RIPEMD160_CTX),
76 };
77
78EVP_MD *EVP_ripemd160()
79 {
80 return(&ripemd160_md);
81 }
diff --git a/src/lib/libcrypto/evp/m_sha1.c b/src/lib/libcrypto/evp/m_sha1.c
new file mode 100644
index 0000000000..87135a9cf2
--- /dev/null
+++ b/src/lib/libcrypto/evp/m_sha1.c
@@ -0,0 +1,81 @@
1/* crypto/evp/m_sha1.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65static EVP_MD sha1_md=
66 {
67 NID_sha1,
68 NID_sha1WithRSAEncryption,
69 SHA_DIGEST_LENGTH,
70 SHA1_Init,
71 SHA1_Update,
72 SHA1_Final,
73 EVP_PKEY_RSA_method,
74 SHA_CBLOCK,
75 sizeof(EVP_MD *)+sizeof(SHA_CTX),
76 };
77
78EVP_MD *EVP_sha1()
79 {
80 return(&sha1_md);
81 }
diff --git a/src/lib/libcrypto/evp/names.c b/src/lib/libcrypto/evp/names.c
new file mode 100644
index 0000000000..e0774da20d
--- /dev/null
+++ b/src/lib/libcrypto/evp/names.c
@@ -0,0 +1,285 @@
1/* crypto/evp/names.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63
64typedef struct aliases_st {
65 char *alias;
66 /* This must be the last field becaue I will allocate things
67 * so they go off the end of it */
68 char name[4];
69 } ALIASES;
70
71static STACK /* ALIASES */ *aliases=NULL;
72static STACK /* EVP_CIPHERS */ *ciphers=NULL;
73static STACK /* EVP_MD */ *digests=NULL;
74
75static int cipher_nid_cmp(a,b)
76EVP_CIPHER **a,**b;
77 { return((*a)->nid - (*b)->nid); }
78
79static int digest_type_cmp(a,b)
80EVP_MD **a,**b;
81 { return((*a)->pkey_type - (*b)->pkey_type); }
82
83int EVP_add_cipher(c)
84EVP_CIPHER *c;
85 {
86 int i;
87
88 if (ciphers == NULL)
89 {
90 ciphers=sk_new(cipher_nid_cmp);
91 if (ciphers == NULL) return(0);
92 }
93 if ((i=sk_find(ciphers,(char *)c)) >= 0)
94 {
95 if (sk_value(ciphers,i) == (char *)c)
96 return(1);
97 sk_delete(ciphers,i);
98 }
99 return(sk_push(ciphers,(char *)c));
100 }
101
102int EVP_add_digest(md)
103EVP_MD *md;
104 {
105 int i;
106 char *n;
107
108 if (digests == NULL)
109 {
110 digests=sk_new(digest_type_cmp);
111 if (digests == NULL) return(0);
112 }
113 if ((i=sk_find(digests,(char *)md)) >= 0)
114 {
115 if (sk_value(digests,i) == (char *)md)
116 return(1);
117 sk_delete(digests,i);
118 }
119 if (md->type != md->pkey_type)
120 {
121 n=OBJ_nid2sn(md->pkey_type);
122 EVP_add_alias(n,OBJ_nid2sn(md->type));
123 EVP_add_alias(n,OBJ_nid2ln(md->type));
124 }
125 sk_push(digests,(char *)md);
126 return(1);
127 }
128
129static int alias_cmp(a,b)
130ALIASES **a,**b;
131 {
132 return(strcmp((*a)->alias,(*b)->alias));
133 }
134
135int EVP_add_alias(name,aname)
136char *name;
137char *aname;
138 {
139 int l1,l2,i;
140 ALIASES *a;
141 char *p;
142
143 if ((name == NULL) || (aname == NULL)) return(0);
144 l1=strlen(name)+1;
145 l2=strlen(aname)+1;
146 i=sizeof(ALIASES)+l1+l2;
147 if ((a=(ALIASES *)Malloc(i)) == NULL)
148 return(0);
149 strcpy(a->name,name);
150 p= &(a->name[l1]);
151 strcpy(p,aname);
152 a->alias=p;
153
154 if (aliases == NULL)
155 {
156 aliases=sk_new(alias_cmp);
157 if (aliases == NULL) goto err;
158 }
159
160 if ((i=sk_find(aliases,(char *)a)) >= 0)
161 {
162 Free(sk_delete(aliases,i));
163 }
164 if (!sk_push(aliases,(char *)a)) goto err;
165 return(1);
166err:
167 return(0);
168 }
169
170int EVP_delete_alias(name)
171char *name;
172 {
173 ALIASES a;
174 int i;
175
176 if (aliases != NULL)
177 {
178 a.alias=name;
179 if ((i=sk_find(aliases,(char *)&a)) >= 0)
180 {
181 Free(sk_delete(aliases,i));
182 return(1);
183 }
184 }
185 return(0);
186 }
187
188EVP_CIPHER *EVP_get_cipherbyname(name)
189char *name;
190 {
191 int nid,num=6,i;
192 EVP_CIPHER c,*cp;
193 ALIASES a,*ap;
194
195 if (ciphers == NULL) return(NULL);
196 for (;;)
197 {
198 if (num-- <= 0) return(NULL);
199 if (aliases != NULL)
200 {
201 a.alias=name;
202 i=sk_find(aliases,(char *)&a);
203 if (i >= 0)
204 {
205 ap=(ALIASES *)sk_value(aliases,i);
206 name=ap->name;
207 continue;
208 }
209 }
210
211 nid=OBJ_txt2nid(name);
212 if (nid == NID_undef) return(NULL);
213 c.nid=nid;
214 i=sk_find(ciphers,(char *)&c);
215 if (i >= 0)
216 {
217 cp=(EVP_CIPHER *)sk_value(ciphers,i);
218 return(cp);
219 }
220 else
221 return(NULL);
222 }
223 }
224
225EVP_MD *EVP_get_digestbyname(name)
226char *name;
227 {
228 int nid,num=6,i;
229 EVP_MD c,*cp;
230 ALIASES a,*ap;
231
232 if (digests == NULL) return(NULL);
233
234 for (;;)
235 {
236 if (num-- <= 0) return(NULL);
237
238 if (aliases != NULL)
239 {
240 a.alias=name;
241 i=sk_find(aliases,(char *)&a);
242 if (i >= 0)
243 {
244 ap=(ALIASES *)sk_value(aliases,i);
245 name=ap->name;
246 continue;
247 }
248 }
249
250 nid=OBJ_txt2nid(name);
251 if (nid == NID_undef) return(NULL);
252 c.pkey_type=nid;
253 i=sk_find(digests,(char *)&c);
254 if (i >= 0)
255 {
256 cp=(EVP_MD *)sk_value(digests,i);
257 return(cp);
258 }
259 else
260 return(NULL);
261 }
262 }
263
264void EVP_cleanup()
265 {
266 int i;
267
268 if (aliases != NULL)
269 {
270 for (i=0; i<sk_num(aliases); i++)
271 Free(sk_value(aliases,i));
272 sk_free(aliases);
273 aliases=NULL;
274 }
275 if (ciphers != NULL)
276 {
277 sk_free(ciphers);
278 ciphers=NULL;
279 }
280 if (digests != NULL)
281 {
282 sk_free(digests);
283 digests=NULL;
284 }
285 }
diff --git a/src/lib/libcrypto/evp/p_dec.c b/src/lib/libcrypto/evp/p_dec.c
new file mode 100644
index 0000000000..e845ce70c7
--- /dev/null
+++ b/src/lib/libcrypto/evp/p_dec.c
@@ -0,0 +1,84 @@
1/* crypto/evp/p_dec.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "rand.h"
62#include "rsa.h"
63#include "evp.h"
64#include "objects.h"
65#include "x509.h"
66
67int EVP_PKEY_decrypt(key,ek,ekl,priv)
68unsigned char *key;
69unsigned char *ek;
70int ekl;
71EVP_PKEY *priv;
72 {
73 int ret= -1;
74
75 if (priv->type != EVP_PKEY_RSA)
76 {
77 EVPerr(EVP_F_EVP_PKEY_DECRYPT,EVP_R_PUBLIC_KEY_NOT_RSA);
78 goto err;
79 }
80
81 ret=RSA_private_decrypt(ekl,ek,key,priv->pkey.rsa,RSA_PKCS1_PADDING);
82err:
83 return(ret);
84 }
diff --git a/src/lib/libcrypto/evp/p_enc.c b/src/lib/libcrypto/evp/p_enc.c
new file mode 100644
index 0000000000..a26bfad02a
--- /dev/null
+++ b/src/lib/libcrypto/evp/p_enc.c
@@ -0,0 +1,83 @@
1/* crypto/evp/p_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "rand.h"
62#include "rsa.h"
63#include "evp.h"
64#include "objects.h"
65#include "x509.h"
66
67int EVP_PKEY_encrypt(ek,key,key_len,pubk)
68unsigned char *ek;
69unsigned char *key;
70int key_len;
71EVP_PKEY *pubk;
72 {
73 int ret=0;
74
75 if (pubk->type != EVP_PKEY_RSA)
76 {
77 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,EVP_R_PUBLIC_KEY_NOT_RSA);
78 goto err;
79 }
80 ret=RSA_public_encrypt(key_len,key,ek,pubk->pkey.rsa,RSA_PKCS1_PADDING);
81err:
82 return(ret);
83 }
diff --git a/src/lib/libcrypto/evp/p_lib.c b/src/lib/libcrypto/evp/p_lib.c
new file mode 100644
index 0000000000..395351b373
--- /dev/null
+++ b/src/lib/libcrypto/evp/p_lib.c
@@ -0,0 +1,294 @@
1/* crypto/evp/p_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "objects.h"
62#include "evp.h"
63#include "asn1_mac.h"
64#include "x509.h"
65
66/* EVPerr(EVP_F_D2I_PKEY,EVP_R_UNSUPPORTED_CIPHER); */
67/* EVPerr(EVP_F_D2I_PKEY,EVP_R_IV_TOO_LARGE); */
68
69#ifndef NOPROTO
70static void EVP_PKEY_free_it(EVP_PKEY *x);
71#else
72static void EVP_PKEY_free_it();
73#endif
74
75int EVP_PKEY_bits(pkey)
76EVP_PKEY *pkey;
77 {
78#ifndef NO_RSA
79 if (pkey->type == EVP_PKEY_RSA)
80 return(BN_num_bits(pkey->pkey.rsa->n));
81 else
82#endif
83#ifndef NO_DSA
84 if (pkey->type == EVP_PKEY_DSA)
85 return(BN_num_bits(pkey->pkey.dsa->p));
86#endif
87 return(0);
88 }
89
90int EVP_PKEY_size(pkey)
91EVP_PKEY *pkey;
92 {
93#ifndef NO_RSA
94 if (pkey->type == EVP_PKEY_RSA)
95 return(RSA_size(pkey->pkey.rsa));
96 else
97#endif
98#ifndef NO_DSA
99 if (pkey->type == EVP_PKEY_DSA)
100 return(DSA_size(pkey->pkey.dsa));
101#endif
102 return(0);
103 }
104
105int EVP_PKEY_save_parameters(pkey,mode)
106EVP_PKEY *pkey;
107int mode;
108 {
109#ifndef NO_DSA
110 if (pkey->type == EVP_PKEY_DSA)
111 {
112 int ret=pkey->save_parameters=mode;
113
114 if (mode >= 0)
115 pkey->save_parameters=mode;
116 return(ret);
117 }
118#endif
119 return(0);
120 }
121
122int EVP_PKEY_copy_parameters(to,from)
123EVP_PKEY *to,*from;
124 {
125 if (to->type != from->type)
126 {
127 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES);
128 goto err;
129 }
130
131 if (EVP_PKEY_missing_parameters(from))
132 {
133 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARMATERS);
134 goto err;
135 }
136#ifndef NO_DSA
137 if (to->type == EVP_PKEY_DSA)
138 {
139 BIGNUM *a;
140
141 if ((a=BN_dup(from->pkey.dsa->p)) == NULL) goto err;
142 if (to->pkey.dsa->p != NULL) BN_free(to->pkey.dsa->p);
143 to->pkey.dsa->p=a;
144
145 if ((a=BN_dup(from->pkey.dsa->q)) == NULL) goto err;
146 if (to->pkey.dsa->q != NULL) BN_free(to->pkey.dsa->q);
147 to->pkey.dsa->q=a;
148
149 if ((a=BN_dup(from->pkey.dsa->g)) == NULL) goto err;
150 if (to->pkey.dsa->g != NULL) BN_free(to->pkey.dsa->g);
151 to->pkey.dsa->g=a;
152 }
153#endif
154 return(1);
155err:
156 return(0);
157 }
158
159int EVP_PKEY_missing_parameters(pkey)
160EVP_PKEY *pkey;
161 {
162#ifndef NO_DSA
163 if (pkey->type == EVP_PKEY_DSA)
164 {
165 DSA *dsa;
166
167 dsa=pkey->pkey.dsa;
168 if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
169 return(1);
170 }
171#endif
172 return(0);
173 }
174
175int EVP_PKEY_cmp_parameters(a,b)
176EVP_PKEY *a,*b;
177 {
178#ifndef NO_DSA
179 if ((a->type == EVP_PKEY_DSA) && (b->type == EVP_PKEY_DSA))
180 {
181 if ( BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) ||
182 BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) ||
183 BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g))
184 return(0);
185 else
186 return(1);
187 }
188#endif
189 return(-1);
190 }
191
192EVP_PKEY *EVP_PKEY_new()
193 {
194 EVP_PKEY *ret;
195
196 ret=(EVP_PKEY *)Malloc(sizeof(EVP_PKEY));
197 if (ret == NULL)
198 {
199 EVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);
200 return(NULL);
201 }
202 ret->type=EVP_PKEY_NONE;
203 ret->references=1;
204 ret->pkey.ptr=NULL;
205 ret->attributes=NULL;
206 ret->save_parameters=1;
207 return(ret);
208 }
209
210int EVP_PKEY_assign(pkey,type,key)
211EVP_PKEY *pkey;
212int type;
213char *key;
214 {
215 if (pkey == NULL) return(0);
216 if (pkey->pkey.ptr != NULL)
217 EVP_PKEY_free_it(pkey);
218 pkey->type=EVP_PKEY_type(type);
219 pkey->save_type=type;
220 pkey->pkey.ptr=key;
221 return(1);
222 }
223
224int EVP_PKEY_type(type)
225int type;
226 {
227 switch (type)
228 {
229 case EVP_PKEY_RSA:
230 case EVP_PKEY_RSA2:
231 return(EVP_PKEY_RSA);
232 case EVP_PKEY_DSA:
233 case EVP_PKEY_DSA1:
234 case EVP_PKEY_DSA2:
235 case EVP_PKEY_DSA3:
236 case EVP_PKEY_DSA4:
237 return(EVP_PKEY_DSA);
238 case EVP_PKEY_DH:
239 return(EVP_PKEY_DH);
240 default:
241 return(NID_undef);
242 }
243 }
244
245void EVP_PKEY_free(x)
246EVP_PKEY *x;
247 {
248 int i;
249
250 if (x == NULL) return;
251
252 i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY);
253#ifdef REF_PRINT
254 REF_PRINT("EVP_PKEY",x);
255#endif
256 if (i > 0) return;
257#ifdef REF_CHECK
258 if (i < 0)
259 {
260 fprintf(stderr,"EVP_PKEY_free, bad reference count\n");
261 abort();
262 }
263#endif
264 EVP_PKEY_free_it(x);
265 Free((char *)x);
266 }
267
268static void EVP_PKEY_free_it(x)
269EVP_PKEY *x;
270 {
271 switch (x->type)
272 {
273#ifndef NO_RSA
274 case EVP_PKEY_RSA:
275 case EVP_PKEY_RSA2:
276 RSA_free(x->pkey.rsa);
277 break;
278#endif
279#ifndef NO_DSA
280 case EVP_PKEY_DSA:
281 case EVP_PKEY_DSA2:
282 case EVP_PKEY_DSA3:
283 case EVP_PKEY_DSA4:
284 DSA_free(x->pkey.dsa);
285 break;
286#endif
287#ifndef NO_DH
288 case EVP_PKEY_DH:
289 DH_free(x->pkey.dh);
290 break;
291#endif
292 }
293 }
294
diff --git a/src/lib/libcrypto/evp/p_open.c b/src/lib/libcrypto/evp/p_open.c
new file mode 100644
index 0000000000..28a8e02252
--- /dev/null
+++ b/src/lib/libcrypto/evp/p_open.c
@@ -0,0 +1,119 @@
1/* crypto/evp/p_open.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65int EVP_OpenInit(ctx,type,ek,ekl,iv,priv)
66EVP_CIPHER_CTX *ctx;
67EVP_CIPHER *type;
68unsigned char *ek;
69int ekl;
70unsigned char *iv;
71EVP_PKEY *priv;
72 {
73 unsigned char *key=NULL;
74 int i,size=0,ret=0;
75
76 if (priv->type != EVP_PKEY_RSA)
77 {
78 EVPerr(EVP_F_EVP_OPENINIT,EVP_R_PUBLIC_KEY_NOT_RSA);
79 ret= -1;
80 goto err;
81 }
82
83 size=RSA_size(priv->pkey.rsa);
84 key=(unsigned char *)Malloc(size+2);
85 if (key == NULL)
86 {
87 /* ERROR */
88 EVPerr(EVP_F_EVP_OPENINIT,ERR_R_MALLOC_FAILURE);
89 ret= -1;
90 goto err;
91 }
92
93 i=EVP_PKEY_decrypt(key,ek,ekl,priv);
94 if (i != type->key_len)
95 {
96 /* ERROR */
97 goto err;
98 }
99
100 EVP_CIPHER_CTX_init(ctx);
101 EVP_DecryptInit(ctx,type,key,iv);
102 ret=1;
103err:
104 if (key != NULL) memset(key,0,size);
105 Free(key);
106 return(ret);
107 }
108
109int EVP_OpenFinal(ctx,out,outl)
110EVP_CIPHER_CTX *ctx;
111unsigned char *out;
112int *outl;
113 {
114 int i;
115
116 i=EVP_DecryptFinal(ctx,out,outl);
117 EVP_DecryptInit(ctx,NULL,NULL,NULL);
118 return(i);
119 }
diff --git a/src/lib/libcrypto/evp/p_seal.c b/src/lib/libcrypto/evp/p_seal.c
new file mode 100644
index 0000000000..09a408de35
--- /dev/null
+++ b/src/lib/libcrypto/evp/p_seal.c
@@ -0,0 +1,115 @@
1/* crypto/evp/p_seal.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "rand.h"
62#include "rsa.h"
63#include "evp.h"
64#include "objects.h"
65#include "x509.h"
66
67int EVP_SealInit(ctx,type,ek,ekl,iv,pubk,npubk)
68EVP_CIPHER_CTX *ctx;
69EVP_CIPHER *type;
70unsigned char **ek;
71int *ekl;
72unsigned char *iv;
73EVP_PKEY **pubk;
74int npubk;
75 {
76 unsigned char key[EVP_MAX_KEY_LENGTH];
77 int i;
78
79 if (npubk <= 0) return(0);
80 RAND_bytes(key,EVP_MAX_KEY_LENGTH);
81 if (type->iv_len > 0)
82 RAND_bytes(iv,type->iv_len);
83
84 EVP_CIPHER_CTX_init(ctx);
85 EVP_EncryptInit(ctx,type,key,iv);
86
87 for (i=0; i<npubk; i++)
88 {
89 ekl[i]=EVP_PKEY_encrypt(ek[i],key,EVP_CIPHER_key_length(type),
90 pubk[i]);
91 if (ekl[i] <= 0) return(-1);
92 }
93 return(npubk);
94 }
95
96/* MACRO
97void EVP_SealUpdate(ctx,out,outl,in,inl)
98EVP_CIPHER_CTX *ctx;
99unsigned char *out;
100int *outl;
101unsigned char *in;
102int inl;
103 {
104 EVP_EncryptUpdate(ctx,out,outl,in,inl);
105 }
106*/
107
108void EVP_SealFinal(ctx,out,outl)
109EVP_CIPHER_CTX *ctx;
110unsigned char *out;
111int *outl;
112 {
113 EVP_EncryptFinal(ctx,out,outl);
114 EVP_EncryptInit(ctx,NULL,NULL,NULL);
115 }
diff --git a/src/lib/libcrypto/evp/p_sign.c b/src/lib/libcrypto/evp/p_sign.c
new file mode 100644
index 0000000000..073270ce31
--- /dev/null
+++ b/src/lib/libcrypto/evp/p_sign.c
@@ -0,0 +1,119 @@
1/* crypto/evp/p_sign.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65#ifdef undef
66void EVP_SignInit(ctx,type)
67EVP_MD_CTX *ctx;
68EVP_MD *type;
69 {
70 EVP_DigestInit(ctx,type);
71 }
72
73void EVP_SignUpdate(ctx,data,count)
74EVP_MD_CTX *ctx;
75unsigned char *data;
76unsigned int count;
77 {
78 EVP_DigestUpdate(ctx,data,count);
79 }
80#endif
81
82int EVP_SignFinal(ctx,sigret,siglen,pkey)
83EVP_MD_CTX *ctx;
84unsigned char *sigret;
85unsigned int *siglen;
86EVP_PKEY *pkey;
87 {
88 unsigned char m[EVP_MAX_MD_SIZE];
89 unsigned int m_len;
90 int i,ok=0,v;
91 MS_STATIC EVP_MD_CTX tmp_ctx;
92
93 *siglen=0;
94 memcpy(&tmp_ctx,ctx,sizeof(EVP_MD_CTX));
95 EVP_DigestFinal(&tmp_ctx,&(m[0]),&m_len);
96 for (i=0; i<4; i++)
97 {
98 v=ctx->digest->required_pkey_type[i];
99 if (v == 0) break;
100 if (pkey->type == v)
101 {
102 ok=1;
103 break;
104 }
105 }
106 if (!ok)
107 {
108 EVPerr(EVP_F_EVP_SIGNFINAL,EVP_R_WRONG_PUBLIC_KEY_TYPE);
109 return(0);
110 }
111 if (ctx->digest->sign == NULL)
112 {
113 EVPerr(EVP_F_EVP_SIGNFINAL,EVP_R_NO_SIGN_FUNCTION_CONFIGURED);
114 return(0);
115 }
116 return(ctx->digest->sign(ctx->digest->type,m,m_len,sigret,siglen,
117 pkey->pkey.ptr));
118 }
119
diff --git a/src/lib/libcrypto/evp/p_verify.c b/src/lib/libcrypto/evp/p_verify.c
new file mode 100644
index 0000000000..8d727d8f02
--- /dev/null
+++ b/src/lib/libcrypto/evp/p_verify.c
@@ -0,0 +1,102 @@
1/* crypto/evp/p_verify.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65int EVP_VerifyFinal(ctx,sigbuf,siglen,pkey)
66EVP_MD_CTX *ctx;
67unsigned char *sigbuf;
68unsigned int siglen;
69EVP_PKEY *pkey;
70 {
71 unsigned char m[EVP_MAX_MD_SIZE];
72 unsigned int m_len;
73 int i,ok=0,v;
74 MS_STATIC EVP_MD_CTX tmp_ctx;
75
76 for (i=0; i<4; i++)
77 {
78 v=ctx->digest->required_pkey_type[i];
79 if (v == 0) break;
80 if (pkey->type == v)
81 {
82 ok=1;
83 break;
84 }
85 }
86 if (!ok)
87 {
88 EVPerr(EVP_F_EVP_VERIFYFINAL,EVP_R_WRONG_PUBLIC_KEY_TYPE);
89 return(-1);
90 }
91 memcpy(&tmp_ctx,ctx,sizeof(EVP_MD_CTX));
92 EVP_DigestFinal(&tmp_ctx,&(m[0]),&m_len);
93 if (ctx->digest->verify == NULL)
94 {
95 EVPerr(EVP_F_EVP_VERIFYFINAL,EVP_R_NO_VERIFY_FUNCTION_CONFIGURED);
96 return(0);
97 }
98
99 return(ctx->digest->verify(ctx->digest->type,m,m_len,
100 sigbuf,siglen,pkey->pkey.ptr));
101 }
102
diff --git a/src/lib/libcrypto/ex_data.c b/src/lib/libcrypto/ex_data.c
new file mode 100644
index 0000000000..c858b518ff
--- /dev/null
+++ b/src/lib/libcrypto/ex_data.c
@@ -0,0 +1,236 @@
1/* crypto/ex_data.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include "buffer.h"
62#include "bio.h"
63#include "lhash.h"
64#include "cryptlib.h"
65
66int CRYPTO_get_ex_new_index(idx,skp,argl,argp,new_func,dup_func,free_func)
67int idx;
68STACK **skp;
69long argl;
70char *argp;
71int (*new_func)();
72int (*dup_func)();
73void (*free_func)();
74 {
75 CRYPTO_EX_DATA_FUNCS *a;
76
77 if (*skp == NULL)
78 *skp=sk_new_null();
79 if (*skp == NULL)
80 {
81 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
82 return(-1);
83 }
84 a=(CRYPTO_EX_DATA_FUNCS *)Malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
85 if (a == NULL)
86 {
87 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
88 return(-1);
89 }
90 a->argl=argl;
91 a->argp=argp;
92 a->new_func=new_func;
93 a->dup_func=dup_func;
94 a->free_func=free_func;
95 while (sk_num(*skp) <= idx)
96 {
97 if (!sk_push(*skp,NULL))
98 {
99 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
100 Free(a);
101 return(-1);
102 }
103 }
104 sk_value(*skp,idx)=(char *)a;
105 return(idx);
106 }
107
108int CRYPTO_set_ex_data(ad,idx,val)
109CRYPTO_EX_DATA *ad;
110int idx;
111char *val;
112 {
113 int i;
114
115 if (ad->sk == NULL)
116 {
117 if ((ad->sk=sk_new_null()) == NULL)
118 {
119 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
120 return(0);
121 }
122 }
123 i=sk_num(ad->sk);
124
125 while (i <= idx)
126 {
127 if (!sk_push(ad->sk,NULL))
128 {
129 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
130 return(0);
131 }
132 i++;
133 }
134 sk_value(ad->sk,idx)=val;
135 return(1);
136 }
137
138char *CRYPTO_get_ex_data(ad,idx)
139CRYPTO_EX_DATA *ad;
140int idx;
141 {
142 if (ad->sk == NULL)
143 return(0);
144 else if (idx >= sk_num(ad->sk))
145 return(0);
146 else
147 return(sk_value(ad->sk,idx));
148 }
149
150/* The callback is called with the 'object', which is the origional data object
151 * being duplicated, a pointer to the
152 * 'new' object to be inserted, the index, and the argi/argp
153 */
154int CRYPTO_dup_ex_data(meth,to,from)
155STACK *meth;
156CRYPTO_EX_DATA *to,*from;
157 {
158 int i,j,m,r;
159 CRYPTO_EX_DATA_FUNCS *mm;
160 char *from_d;
161
162 if (meth == NULL) return(1);
163 if (from->sk == NULL) return(1);
164 m=sk_num(meth);
165 j=sk_num(from->sk);
166 for (i=0; i<j; i++)
167 {
168 from_d=CRYPTO_get_ex_data(from,i);
169 if (i < m)
170 {
171 mm=(CRYPTO_EX_DATA_FUNCS *)sk_value(meth,i);
172 if (mm->dup_func != NULL)
173 r=mm->dup_func(to,from,(char **)&from_d,i,
174 mm->argl,mm->argp);
175 }
176 CRYPTO_set_ex_data(to,i,from_d);
177 }
178 return(1);
179 }
180
181/* Call each free callback */
182void CRYPTO_free_ex_data(meth,obj,ad)
183STACK *meth;
184char *obj;
185CRYPTO_EX_DATA *ad;
186 {
187 CRYPTO_EX_DATA_FUNCS *m;
188 char *ptr;
189 int i,max;
190
191 if (meth != NULL)
192 {
193 max=sk_num(meth);
194 for (i=0; i<max; i++)
195 {
196 m=(CRYPTO_EX_DATA_FUNCS *)sk_value(meth,i);
197 if ((m != NULL) && (m->free_func != NULL))
198 {
199 ptr=CRYPTO_get_ex_data(ad,i);
200 m->free_func(obj,ptr,ad,i,m->argl,m->argp);
201 }
202 }
203 }
204 if (ad->sk != NULL)
205 {
206 sk_free(ad->sk);
207 ad->sk=NULL;
208 }
209 }
210
211void CRYPTO_new_ex_data(meth,obj,ad)
212STACK *meth;
213char *obj;
214CRYPTO_EX_DATA *ad;
215 {
216 CRYPTO_EX_DATA_FUNCS *m;
217 char *ptr;
218 int i,max;
219
220 ad->sk=NULL;
221 if (meth != NULL)
222 {
223 max=sk_num(meth);
224 for (i=0; i<max; i++)
225 {
226 m=(CRYPTO_EX_DATA_FUNCS *)sk_value(meth,i);
227 if ((m != NULL) && (m->new_func != NULL))
228 {
229 ptr=CRYPTO_get_ex_data(ad,i);
230 m->new_func(obj,ptr,ad,i,m->argl,m->argp);
231 }
232 }
233 }
234 }
235
236
diff --git a/src/lib/libcrypto/hmac/hmac.c b/src/lib/libcrypto/hmac/hmac.c
new file mode 100644
index 0000000000..fb09129963
--- /dev/null
+++ b/src/lib/libcrypto/hmac/hmac.c
@@ -0,0 +1,165 @@
1/* crypto/hmac/hmac.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include "hmac.h"
62
63void HMAC_Init(ctx,key,len,md)
64HMAC_CTX *ctx;
65unsigned char *key;
66int len;
67EVP_MD *md;
68 {
69 int i,j,reset=0;
70 unsigned char pad[HMAC_MAX_MD_CBLOCK];
71
72 if (md != NULL)
73 {
74 reset=1;
75 ctx->md=md;
76 }
77 else
78 md=ctx->md;
79
80 if (key != NULL)
81 {
82 reset=1;
83 j=EVP_MD_block_size(md);
84 if (j < len)
85 {
86 EVP_DigestInit(&ctx->md_ctx,md);
87 EVP_DigestUpdate(&ctx->md_ctx,key,len);
88 EVP_DigestFinal(&(ctx->md_ctx),ctx->key,
89 &ctx->key_length);
90 }
91 else
92 {
93 memcpy(ctx->key,key,len);
94 memset(&(ctx->key[len]),0,sizeof(ctx->key)-len);
95 ctx->key_length=len;
96 }
97 }
98
99 if (reset)
100 {
101 for (i=0; i<HMAC_MAX_MD_CBLOCK; i++)
102 pad[i]=0x36^ctx->key[i];
103 EVP_DigestInit(&ctx->i_ctx,md);
104 EVP_DigestUpdate(&ctx->i_ctx,pad,EVP_MD_block_size(md));
105
106 for (i=0; i<HMAC_MAX_MD_CBLOCK; i++)
107 pad[i]=0x5c^ctx->key[i];
108 EVP_DigestInit(&ctx->o_ctx,md);
109 EVP_DigestUpdate(&ctx->o_ctx,pad,EVP_MD_block_size(md));
110 }
111
112 memcpy(&ctx->md_ctx,&ctx->i_ctx,sizeof(ctx->i_ctx));
113 }
114
115void HMAC_Update(ctx,data,len)
116HMAC_CTX *ctx;
117unsigned char *data;
118int len;
119 {
120 EVP_DigestUpdate(&(ctx->md_ctx),data,len);
121 }
122
123void HMAC_Final(ctx,md,len)
124HMAC_CTX *ctx;
125unsigned char *md;
126unsigned int *len;
127 {
128 int j;
129 unsigned int i;
130 unsigned char buf[EVP_MAX_MD_SIZE];
131
132 j=EVP_MD_block_size(ctx->md);
133
134 EVP_DigestFinal(&(ctx->md_ctx),buf,&i);
135 memcpy(&(ctx->md_ctx),&(ctx->o_ctx),sizeof(ctx->o_ctx));
136 EVP_DigestUpdate(&(ctx->md_ctx),buf,i);
137 EVP_DigestFinal(&(ctx->md_ctx),md,len);
138 }
139
140void HMAC_cleanup(ctx)
141HMAC_CTX *ctx;
142 {
143 memset(ctx,0,sizeof(HMAC_CTX));
144 }
145
146unsigned char *HMAC(evp_md,key,key_len,d,n,md,md_len)
147EVP_MD *evp_md;
148unsigned char *key;
149int key_len;
150unsigned char *d;
151int n;
152unsigned char *md;
153unsigned int *md_len;
154 {
155 HMAC_CTX c;
156 static unsigned char m[EVP_MAX_MD_SIZE];
157
158 if (md == NULL) md=m;
159 HMAC_Init(&c,key,key_len,evp_md);
160 HMAC_Update(&c,d,n);
161 HMAC_Final(&c,md,md_len);
162 HMAC_cleanup(&c);
163 return(md);
164 }
165
diff --git a/src/lib/libcrypto/hmac/hmac.h b/src/lib/libcrypto/hmac/hmac.h
new file mode 100644
index 0000000000..e6b43f52c4
--- /dev/null
+++ b/src/lib/libcrypto/hmac/hmac.h
@@ -0,0 +1,106 @@
1/* crypto/hmac/hmac.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#ifndef HEADER_HMAC_H
59#define HEADER_HMAC_H
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
65#include "evp.h"
66
67#define HMAC_MAX_MD_CBLOCK 64
68
69typedef struct hmac_ctx_st
70 {
71 EVP_MD *md;
72 EVP_MD_CTX md_ctx;
73 EVP_MD_CTX i_ctx;
74 EVP_MD_CTX o_ctx;
75 unsigned int key_length;
76 unsigned char key[HMAC_MAX_MD_CBLOCK];
77 } HMAC_CTX;
78
79#define HMAC_size(e) (EVP_MD_size((e)->md))
80
81#ifndef NOPROTO
82
83void HMAC_Init(HMAC_CTX *ctx, unsigned char *key, int len,
84 EVP_MD *md);
85void HMAC_Update(HMAC_CTX *ctx,unsigned char *key, int len);
86void HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
87void HMAC_cleanup(HMAC_CTX *ctx);
88unsigned char *HMAC(EVP_MD *evp_md, unsigned char *key, int key_len,
89 unsigned char *d, int n, unsigned char *md, unsigned int *md_len);
90
91
92#else
93
94void HMAC_Init();
95void HMAC_Update();
96void HMAC_Final();
97void HMAC_cleanup();
98unsigned char *HMAC();
99
100#endif
101
102#ifdef __cplusplus
103}
104#endif
105
106#endif
diff --git a/src/lib/libcrypto/lhash/lh_stats.c b/src/lib/libcrypto/lhash/lh_stats.c
new file mode 100644
index 0000000000..23fe82f777
--- /dev/null
+++ b/src/lib/libcrypto/lhash/lh_stats.c
@@ -0,0 +1,289 @@
1/* crypto/lhash/lh_stats.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62/* If you wish to build this outside of SSLeay, remove the following lines
63 * and things should work as expected */
64#include "cryptlib.h"
65
66#include "lhash.h"
67
68#ifndef HEADER_BIO_H
69
70void lh_stats(lh, out)
71LHASH *lh;
72FILE *out;
73 {
74 fprintf(out,"num_items = %lu\n",lh->num_items);
75 fprintf(out,"num_nodes = %u\n",lh->num_nodes);
76 fprintf(out,"num_alloc_nodes = %u\n",lh->num_alloc_nodes);
77 fprintf(out,"num_expands = %lu\n",lh->num_expands);
78 fprintf(out,"num_expand_reallocs = %lu\n",lh->num_expand_reallocs);
79 fprintf(out,"num_contracts = %lu\n",lh->num_contracts);
80 fprintf(out,"num_contract_reallocs = %lu\n",lh->num_contract_reallocs);
81 fprintf(out,"num_hash_calls = %lu\n",lh->num_hash_calls);
82 fprintf(out,"num_comp_calls = %lu\n",lh->num_comp_calls);
83 fprintf(out,"num_insert = %lu\n",lh->num_insert);
84 fprintf(out,"num_replace = %lu\n",lh->num_replace);
85 fprintf(out,"num_delete = %lu\n",lh->num_delete);
86 fprintf(out,"num_no_delete = %lu\n",lh->num_no_delete);
87 fprintf(out,"num_retrieve = %lu\n",lh->num_retrieve);
88 fprintf(out,"num_retrieve_miss = %lu\n",lh->num_retrieve_miss);
89 fprintf(out,"num_hash_comps = %lu\n",lh->num_hash_comps);
90#ifdef DEBUG
91 fprintf(out,"p = %u\n",lh->p);
92 fprintf(out,"pmax = %u\n",lh->pmax);
93 fprintf(out,"up_load = %lu\n",lh->up_load);
94 fprintf(out,"down_load = %lu\n",lh->down_load);
95#endif
96 }
97
98void lh_node_stats(lh, out)
99LHASH *lh;
100FILE *out;
101 {
102 LHASH_NODE *n;
103 unsigned int i,num;
104
105 for (i=0; i<lh->num_nodes; i++)
106 {
107 for (n=lh->b[i],num=0; n != NULL; n=n->next)
108 num++;
109 fprintf(out,"node %6u -> %3u\n",i,num);
110 }
111 }
112
113void lh_node_usage_stats(lh, out)
114LHASH *lh;
115FILE *out;
116 {
117 LHASH_NODE *n;
118 unsigned long num;
119 unsigned int i;
120 unsigned long total=0,n_used=0;
121
122 for (i=0; i<lh->num_nodes; i++)
123 {
124 for (n=lh->b[i],num=0; n != NULL; n=n->next)
125 num++;
126 if (num != 0)
127 {
128 n_used++;
129 total+=num;
130 }
131 }
132 fprintf(out,"%lu nodes used out of %u\n",n_used,lh->num_nodes);
133 fprintf(out,"%lu items\n",total);
134 if (n_used == 0) return;
135 fprintf(out,"load %d.%02d actual load %d.%02d\n",
136 (int)(total/lh->num_nodes),
137 (int)((total%lh->num_nodes)*100/lh->num_nodes),
138 (int)(total/n_used),
139 (int)((total%n_used)*100/n_used));
140 }
141
142#else
143
144#ifndef NO_FP_API
145void lh_stats(lh,fp)
146LHASH *lh;
147FILE *fp;
148 {
149 BIO *bp;
150
151 bp=BIO_new(BIO_s_file());
152 if (bp == NULL) goto end;
153 BIO_set_fp(bp,fp,BIO_NOCLOSE);
154 lh_stats_bio(lh,bp);
155 BIO_free(bp);
156end:;
157 }
158
159void lh_node_stats(lh,fp)
160LHASH *lh;
161FILE *fp;
162 {
163 BIO *bp;
164
165 bp=BIO_new(BIO_s_file());
166 if (bp == NULL) goto end;
167 BIO_set_fp(bp,fp,BIO_NOCLOSE);
168 lh_node_stats_bio(lh,bp);
169 BIO_free(bp);
170end:;
171 }
172
173void lh_node_usage_stats(lh,fp)
174LHASH *lh;
175FILE *fp;
176 {
177 BIO *bp;
178
179 bp=BIO_new(BIO_s_file());
180 if (bp == NULL) goto end;
181 BIO_set_fp(bp,fp,BIO_NOCLOSE);
182 lh_node_usage_stats_bio(lh,bp);
183 BIO_free(bp);
184end:;
185 }
186
187#endif
188
189void lh_stats_bio(lh, out)
190LHASH *lh;
191BIO *out;
192 {
193 char buf[128];
194
195 sprintf(buf,"num_items = %lu\n",lh->num_items);
196 BIO_puts(out,buf);
197 sprintf(buf,"num_nodes = %u\n",lh->num_nodes);
198 BIO_puts(out,buf);
199 sprintf(buf,"num_alloc_nodes = %u\n",lh->num_alloc_nodes);
200 BIO_puts(out,buf);
201 sprintf(buf,"num_expands = %lu\n",lh->num_expands);
202 BIO_puts(out,buf);
203 sprintf(buf,"num_expand_reallocs = %lu\n",lh->num_expand_reallocs);
204 BIO_puts(out,buf);
205 sprintf(buf,"num_contracts = %lu\n",lh->num_contracts);
206 BIO_puts(out,buf);
207 sprintf(buf,"num_contract_reallocs = %lu\n",lh->num_contract_reallocs);
208 BIO_puts(out,buf);
209 sprintf(buf,"num_hash_calls = %lu\n",lh->num_hash_calls);
210 BIO_puts(out,buf);
211 sprintf(buf,"num_comp_calls = %lu\n",lh->num_comp_calls);
212 BIO_puts(out,buf);
213 sprintf(buf,"num_insert = %lu\n",lh->num_insert);
214 BIO_puts(out,buf);
215 sprintf(buf,"num_replace = %lu\n",lh->num_replace);
216 BIO_puts(out,buf);
217 sprintf(buf,"num_delete = %lu\n",lh->num_delete);
218 BIO_puts(out,buf);
219 sprintf(buf,"num_no_delete = %lu\n",lh->num_no_delete);
220 BIO_puts(out,buf);
221 sprintf(buf,"num_retrieve = %lu\n",lh->num_retrieve);
222 BIO_puts(out,buf);
223 sprintf(buf,"num_retrieve_miss = %lu\n",lh->num_retrieve_miss);
224 BIO_puts(out,buf);
225 sprintf(buf,"num_hash_comps = %lu\n",lh->num_hash_comps);
226 BIO_puts(out,buf);
227#ifdef DEBUG
228 sprintf(buf,"p = %u\n",lh->p);
229 BIO_puts(out,buf);
230 sprintf(buf,"pmax = %u\n",lh->pmax);
231 BIO_puts(out,buf);
232 sprintf(buf,"up_load = %lu\n",lh->up_load);
233 BIO_puts(out,buf);
234 sprintf(buf,"down_load = %lu\n",lh->down_load);
235 BIO_puts(out,buf);
236#endif
237 }
238
239void lh_node_stats_bio(lh, out)
240LHASH *lh;
241BIO *out;
242 {
243 LHASH_NODE *n;
244 unsigned int i,num;
245 char buf[128];
246
247 for (i=0; i<lh->num_nodes; i++)
248 {
249 for (n=lh->b[i],num=0; n != NULL; n=n->next)
250 num++;
251 sprintf(buf,"node %6u -> %3u\n",i,num);
252 BIO_puts(out,buf);
253 }
254 }
255
256void lh_node_usage_stats_bio(lh, out)
257LHASH *lh;
258BIO *out;
259 {
260 LHASH_NODE *n;
261 unsigned long num;
262 unsigned int i;
263 unsigned long total=0,n_used=0;
264 char buf[128];
265
266 for (i=0; i<lh->num_nodes; i++)
267 {
268 for (n=lh->b[i],num=0; n != NULL; n=n->next)
269 num++;
270 if (num != 0)
271 {
272 n_used++;
273 total+=num;
274 }
275 }
276 sprintf(buf,"%lu nodes used out of %u\n",n_used,lh->num_nodes);
277 BIO_puts(out,buf);
278 sprintf(buf,"%lu items\n",total);
279 BIO_puts(out,buf);
280 if (n_used == 0) return;
281 sprintf(buf,"load %d.%02d actual load %d.%02d\n",
282 (int)(total/lh->num_nodes),
283 (int)((total%lh->num_nodes)*100/lh->num_nodes),
284 (int)(total/n_used),
285 (int)((total%n_used)*100/n_used));
286 BIO_puts(out,buf);
287 }
288
289#endif
diff --git a/src/lib/libcrypto/lhash/lhash.c b/src/lib/libcrypto/lhash/lhash.c
new file mode 100644
index 0000000000..6dfb5c9ccc
--- /dev/null
+++ b/src/lib/libcrypto/lhash/lhash.c
@@ -0,0 +1,489 @@
1/* crypto/lhash/lhash.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59char *lh_version="lhash part of SSLeay 0.9.0b 29-Jun-1998";
60
61/* Code for dynamic hash table routines
62 * Author - Eric Young v 2.0
63 *
64 * 2.0 eay - Fixed a bug that occured when using lh_delete
65 * from inside lh_doall(). As entries were deleted,
66 * the 'table' was 'contract()ed', making some entries
67 * jump from the end of the table to the start, there by
68 * skiping the lh_doall() processing. eay - 4/12/95
69 *
70 * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
71 * were not being free()ed. 21/11/95
72 *
73 * 1.8 eay - Put the stats routines into a separate file, lh_stats.c
74 * 19/09/95
75 *
76 * 1.7 eay - Removed the fputs() for realloc failures - the code
77 * should silently tolerate them. I have also fixed things
78 * lint complained about 04/05/95
79 *
80 * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92
81 *
82 * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
83 *
84 * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
85 *
86 * 1.3 eay - Fixed a few lint problems 19/3/1991
87 *
88 * 1.2 eay - Fixed lh_doall problem 13/3/1991
89 *
90 * 1.1 eay - Added lh_doall
91 *
92 * 1.0 eay - First version
93 */
94#include <stdio.h>
95#include <string.h>
96#include <stdlib.h>
97#include "lhash.h"
98
99#undef MIN_NODES
100#define MIN_NODES 16
101#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
102#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
103
104#ifndef NOPROTO
105
106#define P_CP char *
107#define P_CPP char *,char *
108static void expand(LHASH *lh);
109static void contract(LHASH *lh);
110static LHASH_NODE **getrn(LHASH *lh, char *data, unsigned long *rhash);
111
112#else
113
114#define P_CP
115#define P_CPP
116static void expand();
117static void contract();
118static LHASH_NODE **getrn();
119
120#endif
121
122LHASH *lh_new(h, c)
123unsigned long (*h)();
124int (*c)();
125 {
126 LHASH *ret;
127 int i;
128
129 if ((ret=(LHASH *)malloc(sizeof(LHASH))) == NULL)
130 goto err0;
131 if ((ret->b=(LHASH_NODE **)malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
132 goto err1;
133 for (i=0; i<MIN_NODES; i++)
134 ret->b[i]=NULL;
135 ret->comp=((c == NULL)?(int (*)())strcmp:c);
136 ret->hash=((h == NULL)?(unsigned long (*)())lh_strhash:h);
137 ret->num_nodes=MIN_NODES/2;
138 ret->num_alloc_nodes=MIN_NODES;
139 ret->p=0;
140 ret->pmax=MIN_NODES/2;
141 ret->up_load=UP_LOAD;
142 ret->down_load=DOWN_LOAD;
143 ret->num_items=0;
144
145 ret->num_expands=0;
146 ret->num_expand_reallocs=0;
147 ret->num_contracts=0;
148 ret->num_contract_reallocs=0;
149 ret->num_hash_calls=0;
150 ret->num_comp_calls=0;
151 ret->num_insert=0;
152 ret->num_replace=0;
153 ret->num_delete=0;
154 ret->num_no_delete=0;
155 ret->num_retrieve=0;
156 ret->num_retrieve_miss=0;
157 ret->num_hash_comps=0;
158
159 return(ret);
160err1:
161 free((char *)ret);
162err0:
163 return(NULL);
164 }
165
166void lh_free(lh)
167LHASH *lh;
168 {
169 unsigned int i;
170 LHASH_NODE *n,*nn;
171
172 for (i=0; i<lh->num_nodes; i++)
173 {
174 n=lh->b[i];
175 while (n != NULL)
176 {
177 nn=n->next;
178 free(n);
179 n=nn;
180 }
181 }
182 free((char *)lh->b);
183 free((char *)lh);
184 }
185
186char *lh_insert(lh, data)
187LHASH *lh;
188char *data;
189 {
190 unsigned long hash;
191 LHASH_NODE *nn,**rn;
192 char *ret;
193
194 if (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))
195 expand(lh);
196
197 rn=getrn(lh,data,&hash);
198
199 if (*rn == NULL)
200 {
201 if ((nn=(LHASH_NODE *)malloc(sizeof(LHASH_NODE))) == NULL)
202 return(NULL);
203 nn->data=data;
204 nn->next=NULL;
205#ifndef NO_HASH_COMP
206 nn->hash=hash;
207#endif
208 *rn=nn;
209 ret=NULL;
210 lh->num_insert++;
211 lh->num_items++;
212 }
213 else /* replace same key */
214 {
215 ret= (*rn)->data;
216 (*rn)->data=data;
217 lh->num_replace++;
218 }
219 return(ret);
220 }
221
222char *lh_delete(lh, data)
223LHASH *lh;
224char *data;
225 {
226 unsigned long hash;
227 LHASH_NODE *nn,**rn;
228 char *ret;
229
230 rn=getrn(lh,data,&hash);
231
232 if (*rn == NULL)
233 {
234 lh->num_no_delete++;
235 return(NULL);
236 }
237 else
238 {
239 nn= *rn;
240 *rn=nn->next;
241 ret=nn->data;
242 free((char *)nn);
243 lh->num_delete++;
244 }
245
246 lh->num_items--;
247 if ((lh->num_nodes > MIN_NODES) &&
248 (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))
249 contract(lh);
250
251 return(ret);
252 }
253
254char *lh_retrieve(lh, data)
255LHASH *lh;
256char *data;
257 {
258 unsigned long hash;
259 LHASH_NODE **rn;
260 char *ret;
261
262 rn=getrn(lh,data,&hash);
263
264 if (*rn == NULL)
265 {
266 lh->num_retrieve_miss++;
267 return(NULL);
268 }
269 else
270 {
271 ret= (*rn)->data;
272 lh->num_retrieve++;
273 }
274 return(ret);
275 }
276
277void lh_doall(lh, func)
278LHASH *lh;
279void (*func)();
280 {
281 lh_doall_arg(lh,func,NULL);
282 }
283
284void lh_doall_arg(lh, func, arg)
285LHASH *lh;
286void (*func)();
287char *arg;
288 {
289 int i;
290 LHASH_NODE *a,*n;
291
292 /* reverse the order so we search from 'top to bottom'
293 * We were having memory leaks otherwise */
294 for (i=lh->num_nodes-1; i>=0; i--)
295 {
296 a=lh->b[i];
297 while (a != NULL)
298 {
299 /* 28/05/91 - eay - n added so items can be deleted
300 * via lh_doall */
301 n=a->next;
302 func(a->data,arg);
303 a=n;
304 }
305 }
306 }
307
308static void expand(lh)
309LHASH *lh;
310 {
311 LHASH_NODE **n,**n1,**n2,*np;
312 unsigned int p,i,j;
313 unsigned long hash,nni;
314
315 lh->num_nodes++;
316 lh->num_expands++;
317 p=(int)lh->p++;
318 n1= &(lh->b[p]);
319 n2= &(lh->b[p+(int)lh->pmax]);
320 *n2=NULL; /* 27/07/92 - eay - undefined pointer bug */
321 nni=lh->num_alloc_nodes;
322
323 for (np= *n1; np != NULL; )
324 {
325#ifndef NO_HASH_COMP
326 hash=np->hash;
327#else
328 hash=(*(lh->hash))(np->data);
329 lh->num_hash_calls++;
330#endif
331 if ((hash%nni) != p)
332 { /* move it */
333 *n1= (*n1)->next;
334 np->next= *n2;
335 *n2=np;
336 }
337 else
338 n1= &((*n1)->next);
339 np= *n1;
340 }
341
342 if ((lh->p) >= lh->pmax)
343 {
344 j=(int)lh->num_alloc_nodes*2;
345 n=(LHASH_NODE **)realloc((char *)lh->b,
346 (unsigned int)sizeof(LHASH_NODE *)*j);
347 if (n == NULL)
348 {
349/* fputs("realloc error in lhash",stderr); */
350 lh->p=0;
351 return;
352 }
353 /* else */
354 for (i=(int)lh->num_alloc_nodes; i<j; i++)/* 26/02/92 eay */
355 n[i]=NULL; /* 02/03/92 eay */
356 lh->pmax=lh->num_alloc_nodes;
357 lh->num_alloc_nodes=j;
358 lh->num_expand_reallocs++;
359 lh->p=0;
360 lh->b=n;
361 }
362 }
363
364static void contract(lh)
365LHASH *lh;
366 {
367 LHASH_NODE **n,*n1,*np;
368
369 np=lh->b[lh->p+lh->pmax-1];
370 lh->b[lh->p+lh->pmax-1]=NULL; /* 24/07-92 - eay - weird but :-( */
371 if (lh->p == 0)
372 {
373 n=(LHASH_NODE **)realloc((char *)lh->b,
374 (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
375 if (n == NULL)
376 {
377/* fputs("realloc error in lhash",stderr); */
378 return;
379 }
380 lh->num_contract_reallocs++;
381 lh->num_alloc_nodes/=2;
382 lh->pmax/=2;
383 lh->p=lh->pmax-1;
384 lh->b=n;
385 }
386 else
387 lh->p--;
388
389 lh->num_nodes--;
390 lh->num_contracts++;
391
392 n1=lh->b[(int)lh->p];
393 if (n1 == NULL)
394 lh->b[(int)lh->p]=np;
395 else
396 {
397 while (n1->next != NULL)
398 n1=n1->next;
399 n1->next=np;
400 }
401 }
402
403static LHASH_NODE **getrn(lh, data, rhash)
404LHASH *lh;
405char *data;
406unsigned long *rhash;
407 {
408 LHASH_NODE **ret,*n1;
409 unsigned long hash,nn;
410 int (*cf)();
411
412 hash=(*(lh->hash))(data);
413 lh->num_hash_calls++;
414 *rhash=hash;
415
416 nn=hash%lh->pmax;
417 if (nn < lh->p)
418 nn=hash%lh->num_alloc_nodes;
419
420 cf=lh->comp;
421 ret= &(lh->b[(int)nn]);
422 for (n1= *ret; n1 != NULL; n1=n1->next)
423 {
424#ifndef NO_HASH_COMP
425 lh->num_hash_comps++;
426 if (n1->hash != hash)
427 {
428 ret= &(n1->next);
429 continue;
430 }
431#endif
432 lh->num_comp_calls++;
433 if ((*cf)(n1->data,data) == 0)
434 break;
435 ret= &(n1->next);
436 }
437 return(ret);
438 }
439
440/*
441static unsigned long lh_strhash(str)
442char *str;
443 {
444 int i,l;
445 unsigned long ret=0;
446 unsigned short *s;
447
448 if (str == NULL) return(0);
449 l=(strlen(str)+1)/2;
450 s=(unsigned short *)str;
451 for (i=0; i<l; i++)
452 ret^=(s[i]<<(i&0x0f));
453 return(ret);
454 } */
455
456/* The following hash seems to work very well on normal text strings
457 * no collisions on /usr/dict/words and it distributes on %2^n quite
458 * well, not as good as MD5, but still good.
459 */
460unsigned long lh_strhash(c)
461char *c;
462 {
463 unsigned long ret=0;
464 long n;
465 unsigned long v;
466 int r;
467
468 if ((c == NULL) || (*c == '\0'))
469 return(ret);
470/*
471 unsigned char b[16];
472 MD5(c,strlen(c),b);
473 return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
474*/
475
476 n=0x100;
477 while (*c)
478 {
479 v=n|(*c);
480 n+=0x100;
481 r= (int)((v>>2)^v)&0x0f;
482 ret=(ret<<r)|(ret>>(32-r));
483 ret&=0xFFFFFFFFL;
484 ret^=v*v;
485 c++;
486 }
487 return((ret>>16)^ret);
488 }
489
diff --git a/src/lib/libcrypto/lhash/lhash.h b/src/lib/libcrypto/lhash/lhash.h
new file mode 100644
index 0000000000..70cbc6dfe7
--- /dev/null
+++ b/src/lib/libcrypto/lhash/lhash.h
@@ -0,0 +1,155 @@
1/* crypto/lhash/lhash.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Header for dynamic hash table routines
60 * Author - Eric Young
61 */
62
63#ifndef HEADER_LHASH_H
64#define HEADER_LHASH_H
65
66#ifdef __cplusplus
67extern "C" {
68#endif
69
70typedef struct lhash_node_st
71 {
72 char *data;
73 struct lhash_node_st *next;
74#ifndef NO_HASH_COMP
75 unsigned long hash;
76#endif
77 } LHASH_NODE;
78
79typedef struct lhash_st
80 {
81 LHASH_NODE **b;
82 int (*comp)();
83 unsigned long (*hash)();
84 unsigned int num_nodes;
85 unsigned int num_alloc_nodes;
86 unsigned int p;
87 unsigned int pmax;
88 unsigned long up_load; /* load times 256 */
89 unsigned long down_load; /* load times 256 */
90 unsigned long num_items;
91
92 unsigned long num_expands;
93 unsigned long num_expand_reallocs;
94 unsigned long num_contracts;
95 unsigned long num_contract_reallocs;
96 unsigned long num_hash_calls;
97 unsigned long num_comp_calls;
98 unsigned long num_insert;
99 unsigned long num_replace;
100 unsigned long num_delete;
101 unsigned long num_no_delete;
102 unsigned long num_retrieve;
103 unsigned long num_retrieve_miss;
104 unsigned long num_hash_comps;
105 } LHASH;
106
107#define LH_LOAD_MULT 256
108
109#ifndef NOPROTO
110LHASH *lh_new(unsigned long (*h)(), int (*c)());
111void lh_free(LHASH *lh);
112char *lh_insert(LHASH *lh, char *data);
113char *lh_delete(LHASH *lh, char *data);
114char *lh_retrieve(LHASH *lh, char *data);
115void lh_doall(LHASH *lh, void (*func)(/* char *b */));
116void lh_doall_arg(LHASH *lh, void (*func)(/*char *a,char *b*/),char *arg);
117unsigned long lh_strhash(char *c);
118
119#ifndef NO_FP_API
120void lh_stats(LHASH *lh, FILE *out);
121void lh_node_stats(LHASH *lh, FILE *out);
122void lh_node_usage_stats(LHASH *lh, FILE *out);
123#endif
124
125#ifdef HEADER_BIO_H
126void lh_stats_bio(LHASH *lh, BIO *out);
127void lh_node_stats_bio(LHASH *lh, BIO *out);
128void lh_node_usage_stats_bio(LHASH *lh, BIO *out);
129#endif
130#else
131LHASH *lh_new();
132void lh_free();
133char *lh_insert();
134char *lh_delete();
135char *lh_retrieve();
136void lh_doall();
137void lh_doall_arg();
138unsigned long lh_strhash();
139
140#ifndef NO_FP_API
141void lh_stats();
142void lh_node_stats();
143void lh_node_usage_stats();
144#endif
145void lh_stats_bio();
146void lh_node_stats_bio();
147void lh_node_usage_stats_bio();
148#endif
149
150#ifdef __cplusplus
151}
152#endif
153
154#endif
155
diff --git a/src/lib/libcrypto/md5/asm/md5-586.pl b/src/lib/libcrypto/md5/asm/md5-586.pl
new file mode 100644
index 0000000000..2c7fb7dd98
--- /dev/null
+++ b/src/lib/libcrypto/md5/asm/md5-586.pl
@@ -0,0 +1,304 @@
1#!/usr/bin/perl
2
3# Normal is the
4# md5_block_x86(MD5_CTX *c, ULONG *X);
5# version, non-normal is the
6# md5_block_x86(MD5_CTX *c, ULONG *X,int blocks);
7
8$normal=0;
9
10push(@INC,"perlasm","../../perlasm");
11require "x86asm.pl";
12
13&asm_init($ARGV[0],$0);
14
15$A="eax";
16$B="ebx";
17$C="ecx";
18$D="edx";
19$tmp1="edi";
20$tmp2="ebp";
21$X="esi";
22
23# What we need to load into $tmp for the next round
24%Ltmp1=("R0",&Np($C), "R1",&Np($C), "R2",&Np($C), "R3",&Np($D));
25@xo=(
26 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, # R0
27 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, # R1
28 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, # R2
29 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9, # R3
30 );
31
32&md5_block("md5_block_x86");
33&asm_finish();
34
35sub Np
36 {
37 local($p)=@_;
38 local(%n)=($A,$D,$B,$A,$C,$B,$D,$C);
39 return($n{$p});
40 }
41
42sub R0
43 {
44 local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
45
46 &mov($tmp1,$C) if $pos < 0;
47 &mov($tmp2,&DWP($xo[$ki]*4,$K,"",0)) if $pos < 0; # very first one
48
49 # body proper
50
51 &comment("R0 $ki");
52 &xor($tmp1,$d); # F function - part 2
53
54 &and($tmp1,$b); # F function - part 3
55 &lea($a,&DWP($t,$a,$tmp2,1));
56
57 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
58 &xor($tmp1,$d); # F function - part 4
59
60 &add($a,$tmp1);
61 &mov($tmp1,&Np($c)) if $pos < 1; # next tmp1 for R0
62 &mov($tmp1,&Np($c)) if $pos == 1; # next tmp1 for R1
63
64 &rotl($a,$s);
65 &add($a,$b);
66
67 }
68
69sub R1
70 {
71 local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
72
73 &comment("R1 $ki");
74
75 &lea($a,&DWP($t,$a,$tmp2,1));
76
77 &xor($tmp1,$b); # G function - part 2
78 &and($tmp1,$d); # G function - part 3
79
80 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
81 &xor($tmp1,$c); # G function - part 4
82
83 &add($a,$tmp1);
84 &mov($tmp1,&Np($c)) if $pos < 1; # G function - part 1
85 &mov($tmp1,&Np($c)) if $pos == 1; # G function - part 1
86
87 &rotl($a,$s);
88
89 &add($a,$b);
90 }
91
92sub R2
93 {
94 local($n,$pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
95 # This one is different, only 3 logical operations
96
97if (($n & 1) == 0)
98 {
99 &comment("R2 $ki");
100 # make sure to do 'D' first, not 'B', else we clash with
101 # the last add from the previous round.
102
103 &xor($tmp1,$d); # H function - part 2
104
105 &xor($tmp1,$b); # H function - part 3
106 &lea($a,&DWP($t,$a,$tmp2,1));
107
108 &add($a,$tmp1);
109 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0));
110
111 &rotl($a,$s);
112
113 &mov($tmp1,&Np($c));
114 }
115else
116 {
117 &comment("R2 $ki");
118 # make sure to do 'D' first, not 'B', else we clash with
119 # the last add from the previous round.
120
121 &lea($a,&DWP($t,$a,$tmp2,1));
122
123 &add($b,$c); # MOVED FORWARD
124 &xor($tmp1,$d); # H function - part 2
125
126 &xor($tmp1,$b); # H function - part 3
127 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
128
129 &add($a,$tmp1);
130 &mov($tmp1,&Np($c)) if $pos < 1; # H function - part 1
131 &mov($tmp1,-1) if $pos == 1; # I function - part 1
132
133 &rotl($a,$s);
134
135 &add($a,$b);
136 }
137 }
138
139sub R3
140 {
141 local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
142
143 &comment("R3 $ki");
144
145 # &not($tmp1)
146 &xor($tmp1,$d) if $pos < 0; # I function - part 2
147
148 &or($tmp1,$b); # I function - part 3
149 &lea($a,&DWP($t,$a,$tmp2,1));
150
151 &xor($tmp1,$c); # I function - part 4
152 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if $pos != 2; # load X/k value
153 &mov($tmp2,&wparam(0)) if $pos == 2;
154
155 &add($a,$tmp1);
156 &mov($tmp1,-1) if $pos < 1; # H function - part 1
157 &add($K,64) if $pos >=1 && !$normal;
158
159 &rotl($a,$s);
160
161 &xor($tmp1,&Np($d)) if $pos <= 0; # I function - part = first time
162 &mov($tmp1,&DWP( 0,$tmp2,"",0)) if $pos > 0;
163 &add($a,$b);
164 }
165
166
167sub md5_block
168 {
169 local($name)=@_;
170
171 &function_begin_B($name,"",3);
172
173 # parameter 1 is the MD5_CTX structure.
174 # A 0
175 # B 4
176 # C 8
177 # D 12
178
179 &push("esi");
180 &push("edi");
181 &mov($tmp1, &wparam(0)); # edi
182 &mov($X, &wparam(1)); # esi
183 &mov($C, &wparam(2));
184 &push("ebp");
185 &push("ebx");
186 &add($C, $X); # offset we end at
187 &sub($C, 64);
188 &mov($A, &DWP( 0,$tmp1,"",0));
189 &push($C); # Put on the TOS
190 &mov($B, &DWP( 4,$tmp1,"",0));
191 &mov($C, &DWP( 8,$tmp1,"",0));
192 &mov($D, &DWP(12,$tmp1,"",0));
193
194 &set_label("start") unless $normal;
195 &comment("");
196 &comment("R0 section");
197
198 &R0(-2,$A,$B,$C,$D,$X, 0, 7,0xd76aa478);
199 &R0( 0,$D,$A,$B,$C,$X, 1,12,0xe8c7b756);
200 &R0( 0,$C,$D,$A,$B,$X, 2,17,0x242070db);
201 &R0( 0,$B,$C,$D,$A,$X, 3,22,0xc1bdceee);
202 &R0( 0,$A,$B,$C,$D,$X, 4, 7,0xf57c0faf);
203 &R0( 0,$D,$A,$B,$C,$X, 5,12,0x4787c62a);
204 &R0( 0,$C,$D,$A,$B,$X, 6,17,0xa8304613);
205 &R0( 0,$B,$C,$D,$A,$X, 7,22,0xfd469501);
206 &R0( 0,$A,$B,$C,$D,$X, 8, 7,0x698098d8);
207 &R0( 0,$D,$A,$B,$C,$X, 9,12,0x8b44f7af);
208 &R0( 0,$C,$D,$A,$B,$X,10,17,0xffff5bb1);
209 &R0( 0,$B,$C,$D,$A,$X,11,22,0x895cd7be);
210 &R0( 0,$A,$B,$C,$D,$X,12, 7,0x6b901122);
211 &R0( 0,$D,$A,$B,$C,$X,13,12,0xfd987193);
212 &R0( 0,$C,$D,$A,$B,$X,14,17,0xa679438e);
213 &R0( 1,$B,$C,$D,$A,$X,15,22,0x49b40821);
214
215 &comment("");
216 &comment("R1 section");
217 &R1(-1,$A,$B,$C,$D,$X,16, 5,0xf61e2562);
218 &R1( 0,$D,$A,$B,$C,$X,17, 9,0xc040b340);
219 &R1( 0,$C,$D,$A,$B,$X,18,14,0x265e5a51);
220 &R1( 0,$B,$C,$D,$A,$X,19,20,0xe9b6c7aa);
221 &R1( 0,$A,$B,$C,$D,$X,20, 5,0xd62f105d);
222 &R1( 0,$D,$A,$B,$C,$X,21, 9,0x02441453);
223 &R1( 0,$C,$D,$A,$B,$X,22,14,0xd8a1e681);
224 &R1( 0,$B,$C,$D,$A,$X,23,20,0xe7d3fbc8);
225 &R1( 0,$A,$B,$C,$D,$X,24, 5,0x21e1cde6);
226 &R1( 0,$D,$A,$B,$C,$X,25, 9,0xc33707d6);
227 &R1( 0,$C,$D,$A,$B,$X,26,14,0xf4d50d87);
228 &R1( 0,$B,$C,$D,$A,$X,27,20,0x455a14ed);
229 &R1( 0,$A,$B,$C,$D,$X,28, 5,0xa9e3e905);
230 &R1( 0,$D,$A,$B,$C,$X,29, 9,0xfcefa3f8);
231 &R1( 0,$C,$D,$A,$B,$X,30,14,0x676f02d9);
232 &R1( 1,$B,$C,$D,$A,$X,31,20,0x8d2a4c8a);
233
234 &comment("");
235 &comment("R2 section");
236 &R2( 0,-1,$A,$B,$C,$D,$X,32, 4,0xfffa3942);
237 &R2( 1, 0,$D,$A,$B,$C,$X,33,11,0x8771f681);
238 &R2( 2, 0,$C,$D,$A,$B,$X,34,16,0x6d9d6122);
239 &R2( 3, 0,$B,$C,$D,$A,$X,35,23,0xfde5380c);
240 &R2( 4, 0,$A,$B,$C,$D,$X,36, 4,0xa4beea44);
241 &R2( 5, 0,$D,$A,$B,$C,$X,37,11,0x4bdecfa9);
242 &R2( 6, 0,$C,$D,$A,$B,$X,38,16,0xf6bb4b60);
243 &R2( 7, 0,$B,$C,$D,$A,$X,39,23,0xbebfbc70);
244 &R2( 8, 0,$A,$B,$C,$D,$X,40, 4,0x289b7ec6);
245 &R2( 9, 0,$D,$A,$B,$C,$X,41,11,0xeaa127fa);
246 &R2(10, 0,$C,$D,$A,$B,$X,42,16,0xd4ef3085);
247 &R2(11, 0,$B,$C,$D,$A,$X,43,23,0x04881d05);
248 &R2(12, 0,$A,$B,$C,$D,$X,44, 4,0xd9d4d039);
249 &R2(13, 0,$D,$A,$B,$C,$X,45,11,0xe6db99e5);
250 &R2(14, 0,$C,$D,$A,$B,$X,46,16,0x1fa27cf8);
251 &R2(15, 1,$B,$C,$D,$A,$X,47,23,0xc4ac5665);
252
253 &comment("");
254 &comment("R3 section");
255 &R3(-1,$A,$B,$C,$D,$X,48, 6,0xf4292244);
256 &R3( 0,$D,$A,$B,$C,$X,49,10,0x432aff97);
257 &R3( 0,$C,$D,$A,$B,$X,50,15,0xab9423a7);
258 &R3( 0,$B,$C,$D,$A,$X,51,21,0xfc93a039);
259 &R3( 0,$A,$B,$C,$D,$X,52, 6,0x655b59c3);
260 &R3( 0,$D,$A,$B,$C,$X,53,10,0x8f0ccc92);
261 &R3( 0,$C,$D,$A,$B,$X,54,15,0xffeff47d);
262 &R3( 0,$B,$C,$D,$A,$X,55,21,0x85845dd1);
263 &R3( 0,$A,$B,$C,$D,$X,56, 6,0x6fa87e4f);
264 &R3( 0,$D,$A,$B,$C,$X,57,10,0xfe2ce6e0);
265 &R3( 0,$C,$D,$A,$B,$X,58,15,0xa3014314);
266 &R3( 0,$B,$C,$D,$A,$X,59,21,0x4e0811a1);
267 &R3( 0,$A,$B,$C,$D,$X,60, 6,0xf7537e82);
268 &R3( 0,$D,$A,$B,$C,$X,61,10,0xbd3af235);
269 &R3( 0,$C,$D,$A,$B,$X,62,15,0x2ad7d2bb);
270 &R3( 2,$B,$C,$D,$A,$X,63,21,0xeb86d391);
271
272 # &mov($tmp2,&wparam(0)); # done in the last R3
273 # &mov($tmp1, &DWP( 0,$tmp2,"",0)); # done is the last R3
274
275 &add($A,$tmp1);
276 &mov($tmp1, &DWP( 4,$tmp2,"",0));
277
278 &add($B,$tmp1);
279 &mov($tmp1, &DWP( 8,$tmp2,"",0));
280
281 &add($C,$tmp1);
282 &mov($tmp1, &DWP(12,$tmp2,"",0));
283
284 &add($D,$tmp1);
285 &mov(&DWP( 0,$tmp2,"",0),$A);
286
287 &mov(&DWP( 4,$tmp2,"",0),$B);
288 &mov($tmp1,&swtmp(0)) unless $normal;
289
290 &mov(&DWP( 8,$tmp2,"",0),$C);
291 &mov(&DWP(12,$tmp2,"",0),$D);
292
293 &cmp($tmp1,$X) unless $normal; # check count
294 &jge(&label("start")) unless $normal;
295
296 &pop("eax"); # pop the temp variable off the stack
297 &pop("ebx");
298 &pop("ebp");
299 &pop("edi");
300 &pop("esi");
301 &ret();
302 &function_end_B($name);
303 }
304
diff --git a/src/lib/libcrypto/md5/md5.h b/src/lib/libcrypto/md5/md5.h
new file mode 100644
index 0000000000..357c6c625d
--- /dev/null
+++ b/src/lib/libcrypto/md5/md5.h
@@ -0,0 +1,99 @@
1/* crypto/md5/md5.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_MD5_H
60#define HEADER_MD5_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#define MD5_CBLOCK 64
67#define MD5_LBLOCK 16
68#define MD5_BLOCK 16
69#define MD5_LAST_BLOCK 56
70#define MD5_LENGTH_BLOCK 8
71#define MD5_DIGEST_LENGTH 16
72
73typedef struct MD5state_st
74 {
75 unsigned long A,B,C,D;
76 unsigned long Nl,Nh;
77 unsigned long data[MD5_LBLOCK];
78 int num;
79 } MD5_CTX;
80
81#ifndef NOPROTO
82void MD5_Init(MD5_CTX *c);
83void MD5_Update(MD5_CTX *c, unsigned char *data, unsigned long len);
84void MD5_Final(unsigned char *md, MD5_CTX *c);
85unsigned char *MD5(unsigned char *d, unsigned long n, unsigned char *md);
86void MD5_Transform(MD5_CTX *c, unsigned char *b);
87#else
88void MD5_Init();
89void MD5_Update();
90void MD5_Final();
91unsigned char *MD5();
92void MD5_Transform();
93#endif
94
95#ifdef __cplusplus
96}
97#endif
98
99#endif
diff --git a/src/lib/libcrypto/md5/md5_dgst.c b/src/lib/libcrypto/md5/md5_dgst.c
new file mode 100644
index 0000000000..43b3498d92
--- /dev/null
+++ b/src/lib/libcrypto/md5/md5_dgst.c
@@ -0,0 +1,440 @@
1/* crypto/md5/md5_dgst.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "md5_locl.h"
61
62char *MD5_version="MD5 part of SSLeay 0.9.0b 29-Jun-1998";
63
64/* Implemented from RFC1321 The MD5 Message-Digest Algorithm
65 */
66
67#define INIT_DATA_A (unsigned long)0x67452301L
68#define INIT_DATA_B (unsigned long)0xefcdab89L
69#define INIT_DATA_C (unsigned long)0x98badcfeL
70#define INIT_DATA_D (unsigned long)0x10325476L
71
72#ifndef NOPROTO
73# ifdef MD5_ASM
74 void md5_block_x86(MD5_CTX *c, unsigned long *p,int num);
75# define md5_block md5_block_x86
76# else
77 static void md5_block(MD5_CTX *c, unsigned long *p,int num);
78# endif
79#else
80# ifdef MD5_ASM
81 void md5_block_x86();
82# define md5_block md5_block_x86
83# else
84 static void md5_block();
85# endif
86#endif
87
88void MD5_Init(c)
89MD5_CTX *c;
90 {
91 c->A=INIT_DATA_A;
92 c->B=INIT_DATA_B;
93 c->C=INIT_DATA_C;
94 c->D=INIT_DATA_D;
95 c->Nl=0;
96 c->Nh=0;
97 c->num=0;
98 }
99
100void MD5_Update(c, data, len)
101MD5_CTX *c;
102register unsigned char *data;
103unsigned long len;
104 {
105 register ULONG *p;
106 int sw,sc;
107 ULONG l;
108
109 if (len == 0) return;
110
111 l=(c->Nl+(len<<3))&0xffffffffL;
112 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
113 * Wei Dai <weidai@eskimo.com> for pointing it out. */
114 if (l < c->Nl) /* overflow */
115 c->Nh++;
116 c->Nh+=(len>>29);
117 c->Nl=l;
118
119 if (c->num != 0)
120 {
121 p=c->data;
122 sw=c->num>>2;
123 sc=c->num&0x03;
124
125 if ((c->num+len) >= MD5_CBLOCK)
126 {
127 l= p[sw];
128 p_c2l(data,l,sc);
129 p[sw++]=l;
130 for (; sw<MD5_LBLOCK; sw++)
131 {
132 c2l(data,l);
133 p[sw]=l;
134 }
135 len-=(MD5_CBLOCK-c->num);
136
137 md5_block(c,p,64);
138 c->num=0;
139 /* drop through and do the rest */
140 }
141 else
142 {
143 int ew,ec;
144
145 c->num+=(int)len;
146 if ((sc+len) < 4) /* ugly, add char's to a word */
147 {
148 l= p[sw];
149 p_c2l_p(data,l,sc,len);
150 p[sw]=l;
151 }
152 else
153 {
154 ew=(c->num>>2);
155 ec=(c->num&0x03);
156 l= p[sw];
157 p_c2l(data,l,sc);
158 p[sw++]=l;
159 for (; sw < ew; sw++)
160 { c2l(data,l); p[sw]=l; }
161 if (ec)
162 {
163 c2l_p(data,l,ec);
164 p[sw]=l;
165 }
166 }
167 return;
168 }
169 }
170 /* we now can process the input data in blocks of MD5_CBLOCK
171 * chars and save the leftovers to c->data. */
172#ifdef L_ENDIAN
173 if ((((unsigned long)data)%sizeof(ULONG)) == 0)
174 {
175 sw=(int)len/MD5_CBLOCK;
176 if (sw > 0)
177 {
178 sw*=MD5_CBLOCK;
179 md5_block(c,(ULONG *)data,sw);
180 data+=sw;
181 len-=sw;
182 }
183 }
184#endif
185 p=c->data;
186 while (len >= MD5_CBLOCK)
187 {
188#if defined(L_ENDIAN) || defined(B_ENDIAN)
189 if (p != (unsigned long *)data)
190 memcpy(p,data,MD5_CBLOCK);
191 data+=MD5_CBLOCK;
192#ifdef B_ENDIAN
193 for (sw=(MD5_LBLOCK/4); sw; sw--)
194 {
195 Endian_Reverse32(p[0]);
196 Endian_Reverse32(p[1]);
197 Endian_Reverse32(p[2]);
198 Endian_Reverse32(p[3]);
199 p+=4;
200 }
201#endif
202#else
203 for (sw=(MD5_LBLOCK/4); sw; sw--)
204 {
205 c2l(data,l); *(p++)=l;
206 c2l(data,l); *(p++)=l;
207 c2l(data,l); *(p++)=l;
208 c2l(data,l); *(p++)=l;
209 }
210#endif
211 p=c->data;
212 md5_block(c,p,64);
213 len-=MD5_CBLOCK;
214 }
215 sc=(int)len;
216 c->num=sc;
217 if (sc)
218 {
219 sw=sc>>2; /* words to copy */
220#ifdef L_ENDIAN
221 p[sw]=0;
222 memcpy(p,data,sc);
223#else
224 sc&=0x03;
225 for ( ; sw; sw--)
226 { c2l(data,l); *(p++)=l; }
227 c2l_p(data,l,sc);
228 *p=l;
229#endif
230 }
231 }
232
233void MD5_Transform(c,b)
234MD5_CTX *c;
235unsigned char *b;
236 {
237 ULONG p[16];
238#if !defined(L_ENDIAN)
239 ULONG *q;
240 int i;
241#endif
242
243#if defined(B_ENDIAN) || defined(L_ENDIAN)
244 memcpy(p,b,64);
245#ifdef B_ENDIAN
246 q=p;
247 for (i=(MD5_LBLOCK/4); i; i--)
248 {
249 Endian_Reverse32(q[0]);
250 Endian_Reverse32(q[1]);
251 Endian_Reverse32(q[2]);
252 Endian_Reverse32(q[3]);
253 q+=4;
254 }
255#endif
256#else
257 q=p;
258 for (i=(MD5_LBLOCK/4); i; i--)
259 {
260 ULONG l;
261 c2l(b,l); *(q++)=l;
262 c2l(b,l); *(q++)=l;
263 c2l(b,l); *(q++)=l;
264 c2l(b,l); *(q++)=l;
265 }
266#endif
267 md5_block(c,p,64);
268 }
269
270#ifndef MD5_ASM
271
272static void md5_block(c, X, num)
273MD5_CTX *c;
274register ULONG *X;
275int num;
276 {
277 register ULONG A,B,C,D;
278
279 A=c->A;
280 B=c->B;
281 C=c->C;
282 D=c->D;
283 for (;;)
284 {
285 /* Round 0 */
286 R0(A,B,C,D,X[ 0], 7,0xd76aa478L);
287 R0(D,A,B,C,X[ 1],12,0xe8c7b756L);
288 R0(C,D,A,B,X[ 2],17,0x242070dbL);
289 R0(B,C,D,A,X[ 3],22,0xc1bdceeeL);
290 R0(A,B,C,D,X[ 4], 7,0xf57c0fafL);
291 R0(D,A,B,C,X[ 5],12,0x4787c62aL);
292 R0(C,D,A,B,X[ 6],17,0xa8304613L);
293 R0(B,C,D,A,X[ 7],22,0xfd469501L);
294 R0(A,B,C,D,X[ 8], 7,0x698098d8L);
295 R0(D,A,B,C,X[ 9],12,0x8b44f7afL);
296 R0(C,D,A,B,X[10],17,0xffff5bb1L);
297 R0(B,C,D,A,X[11],22,0x895cd7beL);
298 R0(A,B,C,D,X[12], 7,0x6b901122L);
299 R0(D,A,B,C,X[13],12,0xfd987193L);
300 R0(C,D,A,B,X[14],17,0xa679438eL);
301 R0(B,C,D,A,X[15],22,0x49b40821L);
302 /* Round 1 */
303 R1(A,B,C,D,X[ 1], 5,0xf61e2562L);
304 R1(D,A,B,C,X[ 6], 9,0xc040b340L);
305 R1(C,D,A,B,X[11],14,0x265e5a51L);
306 R1(B,C,D,A,X[ 0],20,0xe9b6c7aaL);
307 R1(A,B,C,D,X[ 5], 5,0xd62f105dL);
308 R1(D,A,B,C,X[10], 9,0x02441453L);
309 R1(C,D,A,B,X[15],14,0xd8a1e681L);
310 R1(B,C,D,A,X[ 4],20,0xe7d3fbc8L);
311 R1(A,B,C,D,X[ 9], 5,0x21e1cde6L);
312 R1(D,A,B,C,X[14], 9,0xc33707d6L);
313 R1(C,D,A,B,X[ 3],14,0xf4d50d87L);
314 R1(B,C,D,A,X[ 8],20,0x455a14edL);
315 R1(A,B,C,D,X[13], 5,0xa9e3e905L);
316 R1(D,A,B,C,X[ 2], 9,0xfcefa3f8L);
317 R1(C,D,A,B,X[ 7],14,0x676f02d9L);
318 R1(B,C,D,A,X[12],20,0x8d2a4c8aL);
319 /* Round 2 */
320 R2(A,B,C,D,X[ 5], 4,0xfffa3942L);
321 R2(D,A,B,C,X[ 8],11,0x8771f681L);
322 R2(C,D,A,B,X[11],16,0x6d9d6122L);
323 R2(B,C,D,A,X[14],23,0xfde5380cL);
324 R2(A,B,C,D,X[ 1], 4,0xa4beea44L);
325 R2(D,A,B,C,X[ 4],11,0x4bdecfa9L);
326 R2(C,D,A,B,X[ 7],16,0xf6bb4b60L);
327 R2(B,C,D,A,X[10],23,0xbebfbc70L);
328 R2(A,B,C,D,X[13], 4,0x289b7ec6L);
329 R2(D,A,B,C,X[ 0],11,0xeaa127faL);
330 R2(C,D,A,B,X[ 3],16,0xd4ef3085L);
331 R2(B,C,D,A,X[ 6],23,0x04881d05L);
332 R2(A,B,C,D,X[ 9], 4,0xd9d4d039L);
333 R2(D,A,B,C,X[12],11,0xe6db99e5L);
334 R2(C,D,A,B,X[15],16,0x1fa27cf8L);
335 R2(B,C,D,A,X[ 2],23,0xc4ac5665L);
336 /* Round 3 */
337 R3(A,B,C,D,X[ 0], 6,0xf4292244L);
338 R3(D,A,B,C,X[ 7],10,0x432aff97L);
339 R3(C,D,A,B,X[14],15,0xab9423a7L);
340 R3(B,C,D,A,X[ 5],21,0xfc93a039L);
341 R3(A,B,C,D,X[12], 6,0x655b59c3L);
342 R3(D,A,B,C,X[ 3],10,0x8f0ccc92L);
343 R3(C,D,A,B,X[10],15,0xffeff47dL);
344 R3(B,C,D,A,X[ 1],21,0x85845dd1L);
345 R3(A,B,C,D,X[ 8], 6,0x6fa87e4fL);
346 R3(D,A,B,C,X[15],10,0xfe2ce6e0L);
347 R3(C,D,A,B,X[ 6],15,0xa3014314L);
348 R3(B,C,D,A,X[13],21,0x4e0811a1L);
349 R3(A,B,C,D,X[ 4], 6,0xf7537e82L);
350 R3(D,A,B,C,X[11],10,0xbd3af235L);
351 R3(C,D,A,B,X[ 2],15,0x2ad7d2bbL);
352 R3(B,C,D,A,X[ 9],21,0xeb86d391L);
353
354 A+=c->A&0xffffffffL;
355 B+=c->B&0xffffffffL;
356 c->A=A;
357 c->B=B;
358 C+=c->C&0xffffffffL;
359 D+=c->D&0xffffffffL;
360 c->C=C;
361 c->D=D;
362 X+=16;
363 num-=64;
364 if (num <= 0) break;
365 }
366 }
367#endif
368
369void MD5_Final(md, c)
370unsigned char *md;
371MD5_CTX *c;
372 {
373 register int i,j;
374 register ULONG l;
375 register ULONG *p;
376 static unsigned char end[4]={0x80,0x00,0x00,0x00};
377 unsigned char *cp=end;
378
379 /* c->num should definitly have room for at least one more byte. */
380 p=c->data;
381 j=c->num;
382 i=j>>2;
383
384 /* purify often complains about the following line as an
385 * Uninitialized Memory Read. While this can be true, the
386 * following p_c2l macro will reset l when that case is true.
387 * This is because j&0x03 contains the number of 'valid' bytes
388 * already in p[i]. If and only if j&0x03 == 0, the UMR will
389 * occur but this is also the only time p_c2l will do
390 * l= *(cp++) instead of l|= *(cp++)
391 * Many thanks to Alex Tang <altitude@cic.net> for pickup this
392 * 'potential bug' */
393#ifdef PURIFY
394 if ((j&0x03) == 0) p[i]=0;
395#endif
396 l=p[i];
397 p_c2l(cp,l,j&0x03);
398 p[i]=l;
399 i++;
400 /* i is the next 'undefined word' */
401 if (c->num >= MD5_LAST_BLOCK)
402 {
403 for (; i<MD5_LBLOCK; i++)
404 p[i]=0;
405 md5_block(c,p,64);
406 i=0;
407 }
408 for (; i<(MD5_LBLOCK-2); i++)
409 p[i]=0;
410 p[MD5_LBLOCK-2]=c->Nl;
411 p[MD5_LBLOCK-1]=c->Nh;
412 md5_block(c,p,64);
413 cp=md;
414 l=c->A; l2c(l,cp);
415 l=c->B; l2c(l,cp);
416 l=c->C; l2c(l,cp);
417 l=c->D; l2c(l,cp);
418
419 /* clear stuff, md5_block may be leaving some stuff on the stack
420 * but I'm not worried :-) */
421 c->num=0;
422/* memset((char *)&c,0,sizeof(c));*/
423 }
424
425#ifdef undef
426int printit(l)
427unsigned long *l;
428 {
429 int i,ii;
430
431 for (i=0; i<2; i++)
432 {
433 for (ii=0; ii<8; ii++)
434 {
435 fprintf(stderr,"%08lx ",l[i*8+ii]);
436 }
437 fprintf(stderr,"\n");
438 }
439 }
440#endif
diff --git a/src/lib/libcrypto/md5/md5_locl.h b/src/lib/libcrypto/md5/md5_locl.h
new file mode 100644
index 0000000000..dbbe1b71ca
--- /dev/null
+++ b/src/lib/libcrypto/md5/md5_locl.h
@@ -0,0 +1,195 @@
1/* crypto/md5/md5_locl.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* On sparc, this actually slows things down :-( */
60#if defined(sun)
61#undef B_ENDIAN
62#endif
63
64#include <stdlib.h>
65#include <string.h>
66#include "md5.h"
67
68#define ULONG unsigned long
69#define UCHAR unsigned char
70#define UINT unsigned int
71
72#if defined(NOCONST)
73#define const
74#endif
75
76#undef c2l
77#define c2l(c,l) (l = ((unsigned long)(*((c)++))) , \
78 l|=(((unsigned long)(*((c)++)))<< 8), \
79 l|=(((unsigned long)(*((c)++)))<<16), \
80 l|=(((unsigned long)(*((c)++)))<<24))
81
82#undef p_c2l
83#define p_c2l(c,l,n) { \
84 switch (n) { \
85 case 0: l =((unsigned long)(*((c)++))); \
86 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
87 case 2: l|=((unsigned long)(*((c)++)))<<16; \
88 case 3: l|=((unsigned long)(*((c)++)))<<24; \
89 } \
90 }
91
92/* NOTE the pointer is not incremented at the end of this */
93#undef c2l_p
94#define c2l_p(c,l,n) { \
95 l=0; \
96 (c)+=n; \
97 switch (n) { \
98 case 3: l =((unsigned long)(*(--(c))))<<16; \
99 case 2: l|=((unsigned long)(*(--(c))))<< 8; \
100 case 1: l|=((unsigned long)(*(--(c)))) ; \
101 } \
102 }
103
104#undef p_c2l_p
105#define p_c2l_p(c,l,sc,len) { \
106 switch (sc) \
107 { \
108 case 0: l =((unsigned long)(*((c)++))); \
109 if (--len == 0) break; \
110 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
111 if (--len == 0) break; \
112 case 2: l|=((unsigned long)(*((c)++)))<<16; \
113 } \
114 }
115
116#undef l2c
117#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
118 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
119 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
120 *((c)++)=(unsigned char)(((l)>>24)&0xff))
121
122/* NOTE - c is not incremented as per l2c */
123#undef l2cn
124#define l2cn(l1,l2,c,n) { \
125 c+=n; \
126 switch (n) { \
127 case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
128 case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
129 case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
130 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
131 case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
132 case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
133 case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
134 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
135 } \
136 }
137
138/* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
139#if defined(WIN32)
140/* 5 instructions with rotate instruction, else 9 */
141#define Endian_Reverse32(a) \
142 { \
143 unsigned long l=(a); \
144 (a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \
145 }
146#else
147/* 6 instructions with rotate instruction, else 8 */
148#define Endian_Reverse32(a) \
149 { \
150 unsigned long l=(a); \
151 l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
152 (a)=ROTATE(l,16L); \
153 }
154#endif
155/*
156#define F(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
157#define G(x,y,z) (((x) & (z)) | ((y) & (~(z))))
158*/
159
160/* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be
161 * simplified to the code below. Wei attributes these optimisations
162 * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
163 */
164#define F(b,c,d) ((((c) ^ (d)) & (b)) ^ (d))
165#define G(b,c,d) ((((b) ^ (c)) & (d)) ^ (c))
166#define H(b,c,d) ((b) ^ (c) ^ (d))
167#define I(b,c,d) (((~(d)) | (b)) ^ (c))
168
169#undef ROTATE
170#if defined(WIN32)
171#define ROTATE(a,n) _lrotl(a,n)
172#else
173#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
174#endif
175
176
177#define R0(a,b,c,d,k,s,t) { \
178 a+=((k)+(t)+F((b),(c),(d))); \
179 a=ROTATE(a,s); \
180 a+=b; };\
181
182#define R1(a,b,c,d,k,s,t) { \
183 a+=((k)+(t)+G((b),(c),(d))); \
184 a=ROTATE(a,s); \
185 a+=b; };
186
187#define R2(a,b,c,d,k,s,t) { \
188 a+=((k)+(t)+H((b),(c),(d))); \
189 a=ROTATE(a,s); \
190 a+=b; };
191
192#define R3(a,b,c,d,k,s,t) { \
193 a+=((k)+(t)+I((b),(c),(d))); \
194 a=ROTATE(a,s); \
195 a+=b; };
diff --git a/src/lib/libcrypto/md5/md5_one.c b/src/lib/libcrypto/md5/md5_one.c
new file mode 100644
index 0000000000..ab6bb435f9
--- /dev/null
+++ b/src/lib/libcrypto/md5/md5_one.c
@@ -0,0 +1,77 @@
1/* crypto/md5/md5_one.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "md5_locl.h"
61
62unsigned char *MD5(d, n, md)
63unsigned char *d;
64unsigned long n;
65unsigned char *md;
66 {
67 MD5_CTX c;
68 static unsigned char m[MD5_DIGEST_LENGTH];
69
70 if (md == NULL) md=m;
71 MD5_Init(&c);
72 MD5_Update(&c,d,n);
73 MD5_Final(md,&c);
74 memset(&c,0,sizeof(c)); /* security consideration */
75 return(md);
76 }
77
diff --git a/src/lib/libcrypto/objects/obj_dat.c b/src/lib/libcrypto/objects/obj_dat.c
new file mode 100644
index 0000000000..34866ebbd2
--- /dev/null
+++ b/src/lib/libcrypto/objects/obj_dat.c
@@ -0,0 +1,578 @@
1/* crypto/objects/obj_dat.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <ctype.h>
61#include "cryptlib.h"
62#include "lhash.h"
63#include "asn1.h"
64#include "objects.h"
65
66/* obj_dat.h is generated from objects.h by obj_dat.pl */
67#include "obj_dat.h"
68
69#ifndef NOPROTO
70static int sn_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
71static int ln_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
72static int obj_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
73#else
74static int sn_cmp();
75static int ln_cmp();
76static int obj_cmp();
77#endif
78
79#define ADDED_DATA 0
80#define ADDED_SNAME 1
81#define ADDED_LNAME 2
82#define ADDED_NID 3
83
84typedef struct added_obj_st
85 {
86 int type;
87 ASN1_OBJECT *obj;
88 } ADDED_OBJ;
89
90static int new_nid=NUM_NID;
91static LHASH *added=NULL;
92
93static int sn_cmp(ap,bp)
94ASN1_OBJECT **ap;
95ASN1_OBJECT **bp;
96 { return(strcmp((*ap)->sn,(*bp)->sn)); }
97
98static int ln_cmp(ap,bp)
99ASN1_OBJECT **ap;
100ASN1_OBJECT **bp;
101 { return(strcmp((*ap)->ln,(*bp)->ln)); }
102
103static unsigned long add_hash(ca)
104ADDED_OBJ *ca;
105 {
106 ASN1_OBJECT *a;
107 int i;
108 unsigned long ret=0;
109 unsigned char *p;
110
111 a=ca->obj;
112 switch (ca->type)
113 {
114 case ADDED_DATA:
115 ret=a->length<<20L;
116 p=(unsigned char *)a->data;
117 for (i=0; i<a->length; i++)
118 ret^=p[i]<<((i*3)%24);
119 break;
120 case ADDED_SNAME:
121 ret=lh_strhash(a->sn);
122 break;
123 case ADDED_LNAME:
124 ret=lh_strhash(a->ln);
125 break;
126 case ADDED_NID:
127 ret=a->nid;
128 break;
129 default:
130 abort();
131 }
132 ret&=0x3fffffffL;
133 ret|=ca->type<<30L;
134 return(ret);
135 }
136
137static int add_cmp(ca,cb)
138ADDED_OBJ *ca,*cb;
139 {
140 ASN1_OBJECT *a,*b;
141 int i;
142
143 i=ca->type-cb->type;
144 if (i) return(i);
145 a=ca->obj;
146 b=cb->obj;
147 switch (ca->type)
148 {
149 case ADDED_DATA:
150 i=(a->length - b->length);
151 if (i) return(i);
152 return(memcmp(a->data,b->data,a->length));
153 case ADDED_SNAME:
154 if (a->sn == NULL) return(-1);
155 else if (b->sn == NULL) return(1);
156 else return(strcmp(a->sn,b->sn));
157 case ADDED_LNAME:
158 if (a->ln == NULL) return(-1);
159 else if (b->ln == NULL) return(1);
160 else return(strcmp(a->ln,b->ln));
161 case ADDED_NID:
162 return(a->nid-b->nid);
163 default:
164 abort();
165 }
166 }
167
168static int init_added()
169 {
170 if (added != NULL) return(1);
171 added=lh_new(add_hash,add_cmp);
172 return(added != NULL);
173 }
174
175static void cleanup1(a)
176ADDED_OBJ *a;
177 {
178 a->obj->nid=0;
179 a->obj->flags|=ASN1_OBJECT_FLAG_DYNAMIC|
180 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
181 }
182
183static void cleanup2(a)
184ADDED_OBJ *a;
185 { a->obj->nid++; }
186
187static void cleanup3(a)
188ADDED_OBJ *a;
189 {
190 if (--a->obj->nid == 0)
191 ASN1_OBJECT_free(a->obj);
192 Free(a);
193 }
194
195void OBJ_cleanup()
196 {
197 if (added == NULL) return;
198 added->down_load=0;
199 lh_doall(added,cleanup1); /* zero counters */
200 lh_doall(added,cleanup2); /* set counters */
201 lh_doall(added,cleanup3); /* free objects */
202 lh_free(added);
203 added=NULL;
204 }
205
206int OBJ_new_nid(num)
207int num;
208 {
209 int i;
210
211 i=new_nid;
212 new_nid+=num;
213 return(i);
214 }
215
216int OBJ_add_object(obj)
217ASN1_OBJECT *obj;
218 {
219 ASN1_OBJECT *o;
220 ADDED_OBJ *ao[4],*aop;
221 int i;
222
223 if (added == NULL)
224 if (!init_added()) return(0);
225 if ((o=OBJ_dup(obj)) == NULL) goto err;
226 ao[ADDED_DATA]=NULL;
227 ao[ADDED_SNAME]=NULL;
228 ao[ADDED_LNAME]=NULL;
229 ao[ADDED_NID]=NULL;
230 ao[ADDED_NID]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
231 if ((o->length != 0) && (obj->data != NULL))
232 ao[ADDED_DATA]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
233 if (o->sn != NULL)
234 ao[ADDED_SNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
235 if (o->ln != NULL)
236 ao[ADDED_LNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
237
238 for (i=ADDED_DATA; i<=ADDED_NID; i++)
239 {
240 if (ao[i] != NULL)
241 {
242 ao[i]->type=i;
243 ao[i]->obj=o;
244 aop=(ADDED_OBJ *)lh_insert(added,(char *)ao[i]);
245 /* memory leak, buit should not normally matter */
246 if (aop != NULL)
247 Free(aop);
248 }
249 }
250 o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS);
251 return(o->nid);
252err:
253 for (i=ADDED_DATA; i<=ADDED_NID; i++)
254 if (ao[i] != NULL) Free(ao[i]);
255 if (o != NULL) Free(o);
256 return(NID_undef);
257 }
258
259ASN1_OBJECT *OBJ_nid2obj(n)
260int n;
261 {
262 ADDED_OBJ ad,*adp;
263 ASN1_OBJECT ob;
264
265 if ((n >= 0) && (n < NUM_NID))
266 {
267 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
268 {
269 OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
270 return(NULL);
271 }
272 return((ASN1_OBJECT *)&(nid_objs[n]));
273 }
274 else if (added == NULL)
275 return(NULL);
276 else
277 {
278 ad.type=ADDED_NID;
279 ad.obj= &ob;
280 ob.nid=n;
281 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
282 if (adp != NULL)
283 return(adp->obj);
284 else
285 {
286 OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
287 return(NULL);
288 }
289 }
290 }
291
292char *OBJ_nid2sn(n)
293int n;
294 {
295 ADDED_OBJ ad,*adp;
296 ASN1_OBJECT ob;
297
298 if ((n >= 0) && (n < NUM_NID))
299 {
300 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
301 {
302 OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
303 return(NULL);
304 }
305 return(nid_objs[n].sn);
306 }
307 else if (added == NULL)
308 return(NULL);
309 else
310 {
311 ad.type=ADDED_NID;
312 ad.obj= &ob;
313 ob.nid=n;
314 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
315 if (adp != NULL)
316 return(adp->obj->sn);
317 else
318 {
319 OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
320 return(NULL);
321 }
322 }
323 }
324
325char *OBJ_nid2ln(n)
326int n;
327 {
328 ADDED_OBJ ad,*adp;
329 ASN1_OBJECT ob;
330
331 if ((n >= 0) && (n < NUM_NID))
332 {
333 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
334 {
335 OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
336 return(NULL);
337 }
338 return(nid_objs[n].ln);
339 }
340 else if (added == NULL)
341 return(NULL);
342 else
343 {
344 ad.type=ADDED_NID;
345 ad.obj= &ob;
346 ob.nid=n;
347 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
348 if (adp != NULL)
349 return(adp->obj->ln);
350 else
351 {
352 OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
353 return(NULL);
354 }
355 }
356 }
357
358int OBJ_obj2nid(a)
359ASN1_OBJECT *a;
360 {
361 ASN1_OBJECT **op;
362 ADDED_OBJ ad,*adp;
363
364 if (a == NULL)
365 return(NID_undef);
366 if (a->nid != 0)
367 return(a->nid);
368
369 if (added != NULL)
370 {
371 ad.type=ADDED_DATA;
372 ad.obj=a;
373 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
374 if (adp != NULL) return (adp->obj->nid);
375 }
376 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,
377 sizeof(ASN1_OBJECT *),(int (*)())obj_cmp);
378 if (op == NULL)
379 return(NID_undef);
380 return((*op)->nid);
381 }
382
383int OBJ_txt2nid(s)
384char *s;
385 {
386 int ret;
387
388 ret=OBJ_sn2nid(s);
389 if (ret == NID_undef)
390 {
391 ret=OBJ_ln2nid(s);
392 if (ret == NID_undef)
393 {
394 ASN1_OBJECT *op=NULL;
395 unsigned char *buf,*p;
396 int i;
397
398 i=a2d_ASN1_OBJECT(NULL,0,s,-1);
399 if (i <= 0)
400 {
401 /* clear the error */
402 ERR_get_error();
403 return(0);
404 }
405
406 if ((buf=(unsigned char *)Malloc(i)) == NULL)
407 return(NID_undef);
408 a2d_ASN1_OBJECT(buf,i,s,-1);
409 p=buf;
410 op=d2i_ASN1_OBJECT(NULL,&p,i);
411 if (op == NULL) return(NID_undef);
412 ret=OBJ_obj2nid(op);
413 ASN1_OBJECT_free(op);
414 Free(buf);
415 }
416 }
417 return(ret);
418 }
419
420int OBJ_ln2nid(s)
421char *s;
422 {
423 ASN1_OBJECT o,*oo= &o,**op;
424 ADDED_OBJ ad,*adp;
425
426 o.ln=s;
427 if (added != NULL)
428 {
429 ad.type=ADDED_LNAME;
430 ad.obj= &o;
431 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
432 if (adp != NULL) return (adp->obj->nid);
433 }
434 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs,NUM_LN,
435 sizeof(ASN1_OBJECT *),(int (*)())ln_cmp);
436 if (op == NULL) return(NID_undef);
437 return((*op)->nid);
438 }
439
440int OBJ_sn2nid(s)
441char *s;
442 {
443 ASN1_OBJECT o,*oo= &o,**op;
444 ADDED_OBJ ad,*adp;
445
446 o.sn=s;
447 if (added != NULL)
448 {
449 ad.type=ADDED_SNAME;
450 ad.obj= &o;
451 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
452 if (adp != NULL) return (adp->obj->nid);
453 }
454 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)sn_objs,NUM_SN,
455 sizeof(ASN1_OBJECT *),(int (*)())sn_cmp);
456 if (op == NULL) return(NID_undef);
457 return((*op)->nid);
458 }
459
460static int obj_cmp(ap, bp)
461ASN1_OBJECT **ap;
462ASN1_OBJECT **bp;
463 {
464 int j;
465 ASN1_OBJECT *a= *ap;
466 ASN1_OBJECT *b= *bp;
467
468 j=(a->length - b->length);
469 if (j) return(j);
470 return(memcmp(a->data,b->data,a->length));
471 }
472
473char *OBJ_bsearch(key,base,num,size,cmp)
474char *key;
475char *base;
476int num;
477int size;
478int (*cmp)();
479 {
480 int l,h,i,c;
481 char *p;
482
483 if (num == 0) return(NULL);
484 l=0;
485 h=num;
486 while (l < h)
487 {
488 i=(l+h)/2;
489 p= &(base[i*size]);
490 c=(*cmp)(key,p);
491 if (c < 0)
492 h=i;
493 else if (c > 0)
494 l=i+1;
495 else
496 return(p);
497 }
498 return(NULL);
499 }
500
501int OBJ_create_objects(in)
502BIO *in;
503 {
504 MS_STATIC char buf[512];
505 int i,num= -1;
506 char *o,*s,*l=NULL;
507
508 for (;;)
509 {
510 s=o=NULL;
511 i=BIO_gets(in,buf,512);
512 if (i <= 0) return(num);
513 buf[i-1]='\0';
514 if (!isalnum(buf[0])) return(num);
515 o=s=buf;
516 while (isdigit(*s) || (*s == '.'))
517 s++;
518 if (*s != '\0')
519 {
520 *(s++)='\0';
521 while (isspace(*s))
522 s++;
523 if (*s == '\0')
524 s=NULL;
525 else
526 {
527 l=s;
528 while ((*l != '\0') && !isspace(*l))
529 l++;
530 if (*l != '\0')
531 {
532 *(l++)='\0';
533 while (isspace(*l))
534 l++;
535 if (*l == '\0') l=NULL;
536 }
537 else
538 l=NULL;
539 }
540 }
541 else
542 s=NULL;
543 if ((o == NULL) || (*o == '\0')) return(num);
544 if (!OBJ_create(o,s,l)) return(num);
545 num++;
546 }
547 return(num);
548 }
549
550int OBJ_create(oid,sn,ln)
551char *oid;
552char *sn;
553char *ln;
554 {
555 int ok=0;
556 ASN1_OBJECT *op=NULL;
557 unsigned char *buf;
558 int i;
559
560 i=a2d_ASN1_OBJECT(NULL,0,oid,-1);
561 if (i <= 0) return(0);
562
563 if ((buf=(unsigned char *)Malloc(i)) == NULL)
564 {
565 OBJerr(OBJ_F_OBJ_CREATE,OBJ_R_MALLOC_FAILURE);
566 return(0);
567 }
568 i=a2d_ASN1_OBJECT(buf,i,oid,-1);
569 op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);
570 if (op == NULL)
571 goto err;
572 ok=OBJ_add_object(op);
573err:
574 ASN1_OBJECT_free(op);
575 Free((char *)buf);
576 return(ok);
577 }
578
diff --git a/src/lib/libcrypto/objects/obj_dat.pl b/src/lib/libcrypto/objects/obj_dat.pl
new file mode 100644
index 0000000000..4e7879d3f3
--- /dev/null
+++ b/src/lib/libcrypto/objects/obj_dat.pl
@@ -0,0 +1,269 @@
1#!/usr/bin/perl
2
3sub obj_cmp
4 {
5 local(@a,@b,$_,$r);
6
7 $A=$obj_len{$obj{$nid{$a}}};
8 $B=$obj_len{$obj{$nid{$b}}};
9
10 $r=($A-$B);
11 return($r) if $r != 0;
12
13 $A=$obj_der{$obj{$nid{$a}}};
14 $B=$obj_der{$obj{$nid{$b}}};
15
16 return($A cmp $B);
17 }
18
19sub expand_obj
20 {
21 local(*v)=@_;
22 local($k,$d);
23 local($i);
24
25 do {
26 $i=0;
27 foreach $k (keys %v)
28 {
29 if (($v{$k} =~ s/(OBJ_[^,]+),/$v{$1},/))
30 { $i++; }
31 }
32 } while($i);
33 foreach $k (keys %v)
34 {
35 @a=split(/,/,$v{$k});
36 $objn{$k}=$#a+1;
37 }
38 return(%objn);
39 }
40
41while (<>)
42 {
43 next unless /^\#define\s+(\S+)\s+(.*)$/;
44 $v=$1;
45 $d=$2;
46 if ($v =~ /^SN_(.*)$/)
47 { $sn{$1}=$d; }
48 elsif ($v =~ /^LN_(.*)$/)
49 { $ln{$1}=$d; }
50 elsif ($v =~ /^NID_(.*)$/)
51 { $nid{$d}=$1; }
52 elsif ($v =~ /^OBJ_(.*)$/)
53 {
54 $obj{$1}=$v;
55 $objd{$v}=$d;
56 }
57 }
58
59%ob=&expand_obj(*objd);
60
61@a=sort { $a <=> $b } keys %nid;
62$n=$a[$#a]+1;
63
64@lvalues=();
65$lvalues=0;
66
67for ($i=0; $i<$n; $i++)
68 {
69 if (!defined($nid{$i}))
70 {
71 push(@out,"{NULL,NULL,NID_undef,0,NULL},\n");
72 }
73 else
74 {
75 $sn=defined($sn{$nid{$i}})?"$sn{$nid{$i}}":"NULL";
76 $ln=defined($ln{$nid{$i}})?"$ln{$nid{$i}}":"NULL";
77 $sn=$ln if ($sn eq "NULL");
78 $ln=$sn if ($ln eq "NULL");
79 $out ="{";
80 $out.=$sn;
81 $out.=",".$ln;
82 $out.=",NID_$nid{$i},";
83 if (defined($obj{$nid{$i}}))
84 {
85 $v=$objd{$obj{$nid{$i}}};
86 $v =~ s/L//g;
87 $v =~ s/,/ /g;
88 $r=&der_it($v);
89 $z="";
90 $length=0;
91 foreach (unpack("C*",$r))
92 {
93 $z.=sprintf("0x%02X,",$_);
94 $length++;
95 }
96 $obj_der{$obj{$nid{$i}}}=$z;
97 $obj_len{$obj{$nid{$i}}}=$length;
98
99 push(@lvalues,sprintf("%-45s/* [%3d] %s */\n",
100 $z,$lvalues,$obj{$nid{$i}}));
101 $out.="$length,&(lvalues[$lvalues]),0";
102 $lvalues+=$length;
103 }
104 else
105 {
106 $out.="0,NULL";
107 }
108 $out.="},\n";
109 push(@out,$out);
110 }
111 }
112
113@a=grep(defined($sn{$nid{$_}}),0 .. $n);
114foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a)
115 {
116 push(@sn,sprintf("&(nid_objs[%2d]),/* $sn{$nid{$_}} */\n",$_));
117 }
118
119@a=grep(defined($ln{$nid{$_}}),0 .. $n);
120foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a)
121 {
122 push(@ln,sprintf("&(nid_objs[%2d]),/* $ln{$nid{$_}} */\n",$_));
123 }
124
125@a=grep(defined($obj{$nid{$_}}),0 .. $n);
126foreach (sort obj_cmp @a)
127 {
128 $m=$obj{$nid{$_}};
129 $v=$objd{$m};
130 $v =~ s/L//g;
131 $v =~ s/,/ /g;
132 push(@ob,sprintf("&(nid_objs[%2d]),/* %-32s %s */\n",$_,$m,$v));
133 }
134
135print <<'EOF';
136/* lib/obj/obj_dat.h */
137/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
138 * All rights reserved.
139 *
140 * This package is an SSL implementation written
141 * by Eric Young (eay@cryptsoft.com).
142 * The implementation was written so as to conform with Netscapes SSL.
143 *
144 * This library is free for commercial and non-commercial use as long as
145 * the following conditions are aheared to. The following conditions
146 * apply to all code found in this distribution, be it the RC4, RSA,
147 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
148 * included with this distribution is covered by the same copyright terms
149 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150 *
151 * Copyright remains Eric Young's, and as such any Copyright notices in
152 * the code are not to be removed.
153 * If this package is used in a product, Eric Young should be given attribution
154 * as the author of the parts of the library used.
155 * This can be in the form of a textual message at program startup or
156 * in documentation (online or textual) provided with the package.
157 *
158 * Redistribution and use in source and binary forms, with or without
159 * modification, are permitted provided that the following conditions
160 * are met:
161 * 1. Redistributions of source code must retain the copyright
162 * notice, this list of conditions and the following disclaimer.
163 * 2. Redistributions in binary form must reproduce the above copyright
164 * notice, this list of conditions and the following disclaimer in the
165 * documentation and/or other materials provided with the distribution.
166 * 3. All advertising materials mentioning features or use of this software
167 * must display the following acknowledgement:
168 * "This product includes cryptographic software written by
169 * Eric Young (eay@cryptsoft.com)"
170 * The word 'cryptographic' can be left out if the rouines from the library
171 * being used are not cryptographic related :-).
172 * 4. If you include any Windows specific code (or a derivative thereof) from
173 * the apps directory (application code) you must include an acknowledgement:
174 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
175 *
176 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
177 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
178 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
179 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
181 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
182 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
183 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
184 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
185 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
186 * SUCH DAMAGE.
187 *
188 * The licence and distribution terms for any publically available version or
189 * derivative of this code cannot be changed. i.e. this code cannot simply be
190 * copied and put under another distribution licence
191 * [including the GNU Public Licence.]
192 */
193
194/* THIS FILE IS GENERATED FROM Objects.h by obj_dat.pl via the
195 * following command:
196 * perl obj_dat.pl < objects.h > obj_dat.h
197 */
198
199EOF
200
201printf "#define NUM_NID %d\n",$n;
202printf "#define NUM_SN %d\n",$#sn+1;
203printf "#define NUM_LN %d\n",$#ln+1;
204printf "#define NUM_OBJ %d\n\n",$#ob+1;
205
206printf "static unsigned char lvalues[%d]={\n",$lvalues+1;
207print @lvalues;
208print "};\n\n";
209
210printf "static ASN1_OBJECT nid_objs[NUM_NID]={\n";
211foreach (@out)
212 {
213 if (length($_) > 75)
214 {
215 $out="";
216 foreach (split(/,/))
217 {
218 $t=$out.$_.",";
219 if (length($t) > 70)
220 {
221 print "$out\n";
222 $t="\t$_,";
223 }
224 $out=$t;
225 }
226 chop $out;
227 print "$out";
228 }
229 else
230 { print $_; }
231 }
232print "};\n\n";
233
234printf "static ASN1_OBJECT *sn_objs[NUM_SN]={\n";
235print @sn;
236print "};\n\n";
237
238printf "static ASN1_OBJECT *ln_objs[NUM_LN]={\n";
239print @ln;
240print "};\n\n";
241
242printf "static ASN1_OBJECT *obj_objs[NUM_OBJ]={\n";
243print @ob;
244print "};\n\n";
245
246sub der_it
247 {
248 local($v)=@_;
249 local(@a,$i,$ret,@r);
250
251 @a=split(/\s+/,$v);
252 $ret.=pack("C*",$a[0]*40+$a[1]);
253 shift @a;
254 shift @a;
255 while ($_=shift(@a))
256 {
257 @r=();
258 $t=0;
259 while ($_ >= 128)
260 {
261 $x=$_%128;
262 $_/=128;
263 push(@r,((($t++)?0x80:0)|$x));
264 }
265 push(@r,((($t++)?0x80:0)|$_));
266 $ret.=pack("C*",reverse(@r));
267 }
268 return($ret);
269 }
diff --git a/src/lib/libcrypto/objects/obj_err.c b/src/lib/libcrypto/objects/obj_err.c
new file mode 100644
index 0000000000..45206c616c
--- /dev/null
+++ b/src/lib/libcrypto/objects/obj_err.c
@@ -0,0 +1,96 @@
1/* lib/obj/obj_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "objects.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA OBJ_str_functs[]=
65 {
66{ERR_PACK(0,OBJ_F_OBJ_CREATE,0), "OBJ_create"},
67{ERR_PACK(0,OBJ_F_OBJ_DUP,0), "OBJ_dup"},
68{ERR_PACK(0,OBJ_F_OBJ_NID2LN,0), "OBJ_nid2ln"},
69{ERR_PACK(0,OBJ_F_OBJ_NID2OBJ,0), "OBJ_nid2obj"},
70{ERR_PACK(0,OBJ_F_OBJ_NID2SN,0), "OBJ_nid2sn"},
71{0,NULL},
72 };
73
74static ERR_STRING_DATA OBJ_str_reasons[]=
75 {
76{OBJ_R_MALLOC_FAILURE ,"malloc failure"},
77{OBJ_R_UNKNOWN_NID ,"unknown nid"},
78{0,NULL},
79 };
80
81#endif
82
83void ERR_load_OBJ_strings()
84 {
85 static int init=1;
86
87 if (init);
88 {;
89 init=0;
90#ifndef NO_ERR
91 ERR_load_strings(ERR_LIB_OBJ,OBJ_str_functs);
92 ERR_load_strings(ERR_LIB_OBJ,OBJ_str_reasons);
93#endif
94
95 }
96 }
diff --git a/src/lib/libcrypto/objects/obj_lib.c b/src/lib/libcrypto/objects/obj_lib.c
new file mode 100644
index 0000000000..0a9c756197
--- /dev/null
+++ b/src/lib/libcrypto/objects/obj_lib.c
@@ -0,0 +1,126 @@
1/* crypto/objects/obj_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "lhash.h"
62#include "objects.h"
63#include "buffer.h"
64
65ASN1_OBJECT *OBJ_dup(o)
66ASN1_OBJECT *o;
67 {
68 ASN1_OBJECT *r;
69 int i;
70
71 if (o == NULL) return(NULL);
72 if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC))
73 return(o);
74
75 r=(ASN1_OBJECT *)ASN1_OBJECT_new();
76 if (r == NULL)
77 {
78 OBJerr(OBJ_F_OBJ_DUP,ERR_R_ASN1_LIB);
79 return(NULL);
80 }
81 r->data=(unsigned char *)Malloc(o->length);
82 if (r->data == NULL)
83 goto err;
84 memcpy(r->data,o->data,o->length);
85 r->length=o->length;
86 r->nid=o->nid;
87 r->ln=r->sn=NULL;
88 if (o->ln != NULL)
89 {
90 i=strlen(o->ln)+1;
91 r->ln=(char *)Malloc(i);
92 if (r->ln == NULL) goto err;
93 memcpy(r->ln,o->ln,i);
94 }
95
96 if (o->sn != NULL)
97 {
98 i=strlen(o->sn)+1;
99 r->sn=(char *)Malloc(i);
100 if (r->sn == NULL) goto err;
101 memcpy(r->sn,o->sn,i);
102 }
103 r->flags=o->flags|(ASN1_OBJECT_FLAG_DYNAMIC|
104 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS);
105 return(r);
106err:
107 OBJerr(OBJ_F_OBJ_DUP,ERR_R_MALLOC_FAILURE);
108 if (r != NULL)
109 {
110 if (r->ln != NULL) Free(r->ln);
111 if (r->data != NULL) Free(r->data);
112 Free(r);
113 }
114 return(NULL);
115 }
116
117int OBJ_cmp(a,b)
118ASN1_OBJECT *a;
119ASN1_OBJECT *b;
120 {
121 int ret;
122
123 ret=(a->length-b->length);
124 if (ret) return(ret);
125 return(memcmp(a->data,b->data,a->length));
126 }
diff --git a/src/lib/libcrypto/objects/objects.h b/src/lib/libcrypto/objects/objects.h
new file mode 100644
index 0000000000..e1d555b47c
--- /dev/null
+++ b/src/lib/libcrypto/objects/objects.h
@@ -0,0 +1,724 @@
1/* crypto/objects/objects.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_OBJECTS_H
60#define HEADER_OBJECTS_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#define SN_undef "UNDEF"
67#define LN_undef "undefined"
68#define NID_undef 0
69
70#define SN_Algorithm "Algorithm"
71#define LN_algorithm "algorithm"
72#define NID_algorithm 38
73#define OBJ_algorithm 1L,3L,14L,3L,2L
74
75#define LN_rsadsi "rsadsi"
76#define NID_rsadsi 1
77#define OBJ_rsadsi 1L,2L,840L,113549L
78
79#define LN_pkcs "pkcs"
80#define NID_pkcs 2
81#define OBJ_pkcs OBJ_rsadsi,1L
82
83#define SN_md2 "MD2"
84#define LN_md2 "md2"
85#define NID_md2 3
86#define OBJ_md2 OBJ_rsadsi,2L,2L
87
88#define SN_md5 "MD5"
89#define LN_md5 "md5"
90#define NID_md5 4
91#define OBJ_md5 OBJ_rsadsi,2L,5L
92
93#define SN_rc4 "RC4"
94#define LN_rc4 "rc4"
95#define NID_rc4 5
96#define OBJ_rc4 OBJ_rsadsi,3L,4L
97
98#define LN_rsaEncryption "rsaEncryption"
99#define NID_rsaEncryption 6
100#define OBJ_rsaEncryption OBJ_pkcs,1L,1L
101
102#define SN_md2WithRSAEncryption "RSA-MD2"
103#define LN_md2WithRSAEncryption "md2WithRSAEncryption"
104#define NID_md2WithRSAEncryption 7
105#define OBJ_md2WithRSAEncryption OBJ_pkcs,1L,2L
106
107#define SN_md5WithRSAEncryption "RSA-MD5"
108#define LN_md5WithRSAEncryption "md5WithRSAEncryption"
109#define NID_md5WithRSAEncryption 8
110#define OBJ_md5WithRSAEncryption OBJ_pkcs,1L,4L
111
112#define LN_pbeWithMD2AndDES_CBC "pbeWithMD2AndDES-CBC"
113#define NID_pbeWithMD2AndDES_CBC 9
114#define OBJ_pbeWithMD2AndDES_CBC OBJ_pkcs,5L,1L
115
116#define LN_pbeWithMD5AndDES_CBC "pbeWithMD5AndDES-CBC"
117#define NID_pbeWithMD5AndDES_CBC 10
118#define OBJ_pbeWithMD5AndDES_CBC OBJ_pkcs,5L,3L
119
120#define LN_X500 "X500"
121#define NID_X500 11
122#define OBJ_X500 2L,5L
123
124#define LN_X509 "X509"
125#define NID_X509 12
126#define OBJ_X509 OBJ_X500,4L
127
128#define SN_commonName "CN"
129#define LN_commonName "commonName"
130#define NID_commonName 13
131#define OBJ_commonName OBJ_X509,3L
132
133#define SN_countryName "C"
134#define LN_countryName "countryName"
135#define NID_countryName 14
136#define OBJ_countryName OBJ_X509,6L
137
138#define SN_localityName "L"
139#define LN_localityName "localityName"
140#define NID_localityName 15
141#define OBJ_localityName OBJ_X509,7L
142
143/* Postal Address? PA */
144
145/* should be "ST" (rfc1327) but MS uses 'S' */
146#define SN_stateOrProvinceName "ST"
147#define LN_stateOrProvinceName "stateOrProvinceName"
148#define NID_stateOrProvinceName 16
149#define OBJ_stateOrProvinceName OBJ_X509,8L
150
151#define SN_organizationName "O"
152#define LN_organizationName "organizationName"
153#define NID_organizationName 17
154#define OBJ_organizationName OBJ_X509,10L
155
156#define SN_organizationalUnitName "OU"
157#define LN_organizationalUnitName "organizationalUnitName"
158#define NID_organizationalUnitName 18
159#define OBJ_organizationalUnitName OBJ_X509,11L
160
161#define SN_rsa "RSA"
162#define LN_rsa "rsa"
163#define NID_rsa 19
164#define OBJ_rsa OBJ_X500,8L,1L,1L
165
166#define LN_pkcs7 "pkcs7"
167#define NID_pkcs7 20
168#define OBJ_pkcs7 OBJ_pkcs,7L
169
170#define LN_pkcs7_data "pkcs7-data"
171#define NID_pkcs7_data 21
172#define OBJ_pkcs7_data OBJ_pkcs7,1L
173
174#define LN_pkcs7_signed "pkcs7-signedData"
175#define NID_pkcs7_signed 22
176#define OBJ_pkcs7_signed OBJ_pkcs7,2L
177
178#define LN_pkcs7_enveloped "pkcs7-envelopedData"
179#define NID_pkcs7_enveloped 23
180#define OBJ_pkcs7_enveloped OBJ_pkcs7,3L
181
182#define LN_pkcs7_signedAndEnveloped "pkcs7-signedAndEnvelopedData"
183#define NID_pkcs7_signedAndEnveloped 24
184#define OBJ_pkcs7_signedAndEnveloped OBJ_pkcs7,4L
185
186#define LN_pkcs7_digest "pkcs7-digestData"
187#define NID_pkcs7_digest 25
188#define OBJ_pkcs7_digest OBJ_pkcs7,5L
189
190#define LN_pkcs7_encrypted "pkcs7-encryptedData"
191#define NID_pkcs7_encrypted 26
192#define OBJ_pkcs7_encrypted OBJ_pkcs7,6L
193
194#define LN_pkcs3 "pkcs3"
195#define NID_pkcs3 27
196#define OBJ_pkcs3 OBJ_pkcs,3L
197
198#define LN_dhKeyAgreement "dhKeyAgreement"
199#define NID_dhKeyAgreement 28
200#define OBJ_dhKeyAgreement OBJ_pkcs3,1L
201
202#define SN_des_ecb "DES-ECB"
203#define LN_des_ecb "des-ecb"
204#define NID_des_ecb 29
205#define OBJ_des_ecb OBJ_algorithm,6L
206
207#define SN_des_cfb64 "DES-CFB"
208#define LN_des_cfb64 "des-cfb"
209#define NID_des_cfb64 30
210/* IV + num */
211#define OBJ_des_cfb64 OBJ_algorithm,9L
212
213#define SN_des_cbc "DES-CBC"
214#define LN_des_cbc "des-cbc"
215#define NID_des_cbc 31
216/* IV */
217#define OBJ_des_cbc OBJ_algorithm,7L
218
219#define SN_des_ede "DES-EDE"
220#define LN_des_ede "des-ede"
221#define NID_des_ede 32
222/* ?? */
223#define OBJ_des_ede OBJ_algorithm,17L
224
225#define SN_des_ede3 "DES-EDE3"
226#define LN_des_ede3 "des-ede3"
227#define NID_des_ede3 33
228
229#define SN_idea_cbc "IDEA-CBC"
230#define LN_idea_cbc "idea-cbc"
231#define NID_idea_cbc 34
232
233#define SN_idea_cfb64 "IDEA-CFB"
234#define LN_idea_cfb64 "idea-cfb"
235#define NID_idea_cfb64 35
236
237#define SN_idea_ecb "IDEA-ECB"
238#define LN_idea_ecb "idea-ecb"
239#define NID_idea_ecb 36
240
241#define SN_rc2_cbc "RC2-CBC"
242#define LN_rc2_cbc "rc2-cbc"
243#define NID_rc2_cbc 37
244#define OBJ_rc2_cbc OBJ_rsadsi,3L,2L
245
246#define SN_rc2_ecb "RC2-ECB"
247#define LN_rc2_ecb "rc2-ecb"
248#define NID_rc2_ecb 38
249
250#define SN_rc2_cfb64 "RC2-CFB"
251#define LN_rc2_cfb64 "rc2-cfb"
252#define NID_rc2_cfb64 39
253
254#define SN_rc2_ofb64 "RC2-OFB"
255#define LN_rc2_ofb64 "rc2-ofb"
256#define NID_rc2_ofb64 40
257
258#define SN_sha "SHA"
259#define LN_sha "sha"
260#define NID_sha 41
261#define OBJ_sha OBJ_algorithm,18L
262
263#define SN_shaWithRSAEncryption "RSA-SHA"
264#define LN_shaWithRSAEncryption "shaWithRSAEncryption"
265#define NID_shaWithRSAEncryption 42
266#define OBJ_shaWithRSAEncryption OBJ_algorithm,15L
267
268#define SN_des_ede_cbc "DES-EDE-CBC"
269#define LN_des_ede_cbc "des-ede-cbc"
270#define NID_des_ede_cbc 43
271
272#define SN_des_ede3_cbc "DES-EDE3-CBC"
273#define LN_des_ede3_cbc "des-ede3-cbc"
274#define NID_des_ede3_cbc 44
275#define OBJ_des_ede3_cbc OBJ_rsadsi,3L,7L
276
277#define SN_des_ofb64 "DES-OFB"
278#define LN_des_ofb64 "des-ofb"
279#define NID_des_ofb64 45
280#define OBJ_des_ofb64 OBJ_algorithm,8L
281
282#define SN_idea_ofb64 "IDEA-OFB"
283#define LN_idea_ofb64 "idea-ofb"
284#define NID_idea_ofb64 46
285
286#define LN_pkcs9 "pkcs9"
287#define NID_pkcs9 47
288#define OBJ_pkcs9 OBJ_pkcs,9L
289
290#define SN_pkcs9_emailAddress "Email"
291#define LN_pkcs9_emailAddress "emailAddress"
292#define NID_pkcs9_emailAddress 48
293#define OBJ_pkcs9_emailAddress OBJ_pkcs9,1L
294
295#define LN_pkcs9_unstructuredName "unstructuredName"
296#define NID_pkcs9_unstructuredName 49
297#define OBJ_pkcs9_unstructuredName OBJ_pkcs9,2L
298
299#define LN_pkcs9_contentType "contentType"
300#define NID_pkcs9_contentType 50
301#define OBJ_pkcs9_contentType OBJ_pkcs9,3L
302
303#define LN_pkcs9_messageDigest "messageDigest"
304#define NID_pkcs9_messageDigest 51
305#define OBJ_pkcs9_messageDigest OBJ_pkcs9,4L
306
307#define LN_pkcs9_signingTime "signingTime"
308#define NID_pkcs9_signingTime 52
309#define OBJ_pkcs9_signingTime OBJ_pkcs9,5L
310
311#define LN_pkcs9_countersignature "countersignature"
312#define NID_pkcs9_countersignature 53
313#define OBJ_pkcs9_countersignature OBJ_pkcs9,6L
314
315#define LN_pkcs9_challengePassword "challengePassword"
316#define NID_pkcs9_challengePassword 54
317#define OBJ_pkcs9_challengePassword OBJ_pkcs9,7L
318
319#define LN_pkcs9_unstructuredAddress "unstructuredAddress"
320#define NID_pkcs9_unstructuredAddress 55
321#define OBJ_pkcs9_unstructuredAddress OBJ_pkcs9,8L
322
323#define LN_pkcs9_extCertAttributes "extendedCertificateAttributes"
324#define NID_pkcs9_extCertAttributes 56
325#define OBJ_pkcs9_extCertAttributes OBJ_pkcs9,9L
326
327#define SN_netscape "Netscape"
328#define LN_netscape "Netscape Communications Corp."
329#define NID_netscape 57
330#define OBJ_netscape 2L,16L,840L,1L,113730L
331
332#define SN_netscape_cert_extension "nsCertExt"
333#define LN_netscape_cert_extension "Netscape Certificate Extension"
334#define NID_netscape_cert_extension 58
335#define OBJ_netscape_cert_extension OBJ_netscape,1L
336
337#define SN_netscape_data_type "nsDataType"
338#define LN_netscape_data_type "Netscape Data Type"
339#define NID_netscape_data_type 59
340#define OBJ_netscape_data_type OBJ_netscape,2L
341
342#define SN_des_ede_cfb64 "DES-EDE-CFB"
343#define LN_des_ede_cfb64 "des-ede-cfb"
344#define NID_des_ede_cfb64 60
345
346#define SN_des_ede3_cfb64 "DES-EDE3-CFB"
347#define LN_des_ede3_cfb64 "des-ede3-cfb"
348#define NID_des_ede3_cfb64 61
349
350#define SN_des_ede_ofb64 "DES-EDE-OFB"
351#define LN_des_ede_ofb64 "des-ede-ofb"
352#define NID_des_ede_ofb64 62
353
354#define SN_des_ede3_ofb64 "DES-EDE3-OFB"
355#define LN_des_ede3_ofb64 "des-ede3-ofb"
356#define NID_des_ede3_ofb64 63
357
358/* I'm not sure about the object ID */
359#define SN_sha1 "SHA1"
360#define LN_sha1 "sha1"
361#define NID_sha1 64
362#define OBJ_sha1 OBJ_algorithm,26L
363/* 28 Jun 1996 - eay */
364/* #define OBJ_sha1 1L,3L,14L,2L,26L,05L <- wrong */
365
366#define SN_sha1WithRSAEncryption "RSA-SHA1"
367#define LN_sha1WithRSAEncryption "sha1WithRSAEncryption"
368#define NID_sha1WithRSAEncryption 65
369#define OBJ_sha1WithRSAEncryption OBJ_pkcs,1L,5L
370
371#define SN_dsaWithSHA "DSA-SHA"
372#define LN_dsaWithSHA "dsaWithSHA"
373#define NID_dsaWithSHA 66
374#define OBJ_dsaWithSHA OBJ_algorithm,13L
375
376#define SN_dsa_2 "DSA-old"
377#define LN_dsa_2 "dsaEncryption-old"
378#define NID_dsa_2 67
379#define OBJ_dsa_2 OBJ_algorithm,12L
380
381/* proposed by microsoft to RSA */
382#define LN_pbeWithSHA1AndRC2_CBC "pbeWithSHA1AndRC2-CBC"
383#define NID_pbeWithSHA1AndRC2_CBC 68
384#define OBJ_pbeWithSHA1AndRC2_CBC OBJ_pkcs,5L,11L
385
386/* proposed by microsoft to RSA */
387#define LN_pbeWithSHA1AndRC4 "pbeWithSHA1AndRC4"
388#define NID_pbeWithSHA1AndRC4 69
389#define OBJ_pbeWithSHA1AndRC4 OBJ_pkcs,5L,12L
390
391#define SN_dsaWithSHA1_2 "DSA-SHA1-old"
392#define LN_dsaWithSHA1_2 "dsaWithSHA1"
393#define NID_dsaWithSHA1_2 70
394/* Got this one from 'sdn706r20.pdf' which is actually an NSA document :-) */
395#define OBJ_dsaWithSHA1_2 OBJ_algorithm,27L
396
397#define SN_netscape_cert_type "nsCertType"
398#define LN_netscape_cert_type "Netscape Cert Type"
399#define NID_netscape_cert_type 71
400#define OBJ_netscape_cert_type OBJ_netscape_cert_extension,1L
401
402#define SN_netscape_base_url "nsBaseUrl"
403#define LN_netscape_base_url "Netscape Base Url"
404#define NID_netscape_base_url 72
405#define OBJ_netscape_base_url OBJ_netscape_cert_extension,2L
406
407#define SN_netscape_revocation_url "nsRevocationUrl"
408#define LN_netscape_revocation_url "Netscape Revocation Url"
409#define NID_netscape_revocation_url 73
410#define OBJ_netscape_revocation_url OBJ_netscape_cert_extension,3L
411
412#define SN_netscape_ca_revocation_url "nsCaRevocationUrl"
413#define LN_netscape_ca_revocation_url "Netscape CA Revocation Url"
414#define NID_netscape_ca_revocation_url 74
415#define OBJ_netscape_ca_revocation_url OBJ_netscape_cert_extension,4L
416
417#define SN_netscape_renewal_url "nsRenewalUrl"
418#define LN_netscape_renewal_url "Netscape Renewal Url"
419#define NID_netscape_renewal_url 75
420#define OBJ_netscape_renewal_url OBJ_netscape_cert_extension,7L
421
422#define SN_netscape_ca_policy_url "nsCaPolicyUrl"
423#define LN_netscape_ca_policy_url "Netscape CA Policy Url"
424#define NID_netscape_ca_policy_url 76
425#define OBJ_netscape_ca_policy_url OBJ_netscape_cert_extension,8L
426
427#define SN_netscape_ssl_server_name "nsSslServerName"
428#define LN_netscape_ssl_server_name "Netscape SSL Server Name"
429#define NID_netscape_ssl_server_name 77
430#define OBJ_netscape_ssl_server_name OBJ_netscape_cert_extension,12L
431
432#define SN_netscape_comment "nsComment"
433#define LN_netscape_comment "Netscape Comment"
434#define NID_netscape_comment 78
435#define OBJ_netscape_comment OBJ_netscape_cert_extension,13L
436
437#define SN_netscape_cert_sequence "nsCertSequence"
438#define LN_netscape_cert_sequence "Netscape Certificate Sequence"
439#define NID_netscape_cert_sequence 79
440#define OBJ_netscape_cert_sequence OBJ_netscape_data_type,5L
441
442#define SN_desx_cbc "DESX-CBC"
443#define LN_desx_cbc "desx-cbc"
444#define NID_desx_cbc 80
445
446#define SN_ld_ce "ld-ce"
447#define NID_ld_ce 81
448#define OBJ_ld_ce 2L,5L,29L
449
450#define SN_subject_key_identifier "subjectKeyIdentifier"
451#define LN_subject_key_identifier "X509v3 Subject Key Identifier"
452#define NID_subject_key_identifier 82
453#define OBJ_subject_key_identifier OBJ_ld_ce,14L
454
455#define SN_key_usage "keyUsage"
456#define LN_key_usage "X509v3 Key Usage"
457#define NID_key_usage 83
458#define OBJ_key_usage OBJ_ld_ce,15L
459
460#define SN_private_key_usage_period "privateKeyUsagePeriod"
461#define LN_private_key_usage_period "X509v3 Private Key Usage Period"
462#define NID_private_key_usage_period 84
463#define OBJ_private_key_usage_period OBJ_ld_ce,16L
464
465#define SN_subject_alt_name "subjectAltName"
466#define LN_subject_alt_name "X509v3 Subject Alternative Name"
467#define NID_subject_alt_name 85
468#define OBJ_subject_alt_name OBJ_ld_ce,17L
469
470#define SN_issuer_alt_name "issuerAltName"
471#define LN_issuer_alt_name "X509v3 Issuer Alternative Name"
472#define NID_issuer_alt_name 86
473#define OBJ_issuer_alt_name OBJ_ld_ce,18L
474
475#define SN_basic_constraints "basicConstraints"
476#define LN_basic_constraints "X509v3 Basic Constraints"
477#define NID_basic_constraints 87
478#define OBJ_basic_constraints OBJ_ld_ce,19L
479
480#define SN_crl_number "crlNumber"
481#define LN_crl_number "X509v3 CRL Number"
482#define NID_crl_number 88
483#define OBJ_crl_number OBJ_ld_ce,20L
484
485#define SN_certificate_policies "certificatePolicies"
486#define LN_certificate_policies "X509v3 Certificate Policies"
487#define NID_certificate_policies 89
488#define OBJ_certificate_policies OBJ_ld_ce,32L
489
490#define SN_authority_key_identifier "authorityKeyIdentifier"
491#define LN_authority_key_identifier "X509v3 Authority Key Identifier"
492#define NID_authority_key_identifier 90
493#define OBJ_authority_key_identifier OBJ_ld_ce,35L
494
495#define SN_bf_cbc "BF-CBC"
496#define LN_bf_cbc "bf-cbc"
497#define NID_bf_cbc 91
498
499#define SN_bf_ecb "BF-ECB"
500#define LN_bf_ecb "bf-ecb"
501#define NID_bf_ecb 92
502
503#define SN_bf_cfb64 "BF-CFB"
504#define LN_bf_cfb64 "bf-cfb"
505#define NID_bf_cfb64 93
506
507#define SN_bf_ofb64 "BF-OFB"
508#define LN_bf_ofb64 "bf-ofb"
509#define NID_bf_ofb64 94
510
511#define SN_mdc2 "MDC2"
512#define LN_mdc2 "mdc2"
513#define NID_mdc2 95
514#define OBJ_mdc2 2L,5L,8L,3L,101L
515/* An alternative? 1L,3L,14L,3L,2L,19L */
516
517#define SN_mdc2WithRSA "RSA-MDC2"
518#define LN_mdc2WithRSA "mdc2withRSA"
519#define NID_mdc2WithRSA 96
520#define OBJ_mdc2WithRSA 2L,5L,8L,3L,100L
521
522#define SN_rc4_40 "RC4-40"
523#define LN_rc4_40 "rc4-40"
524#define NID_rc4_40 97
525
526#define SN_rc2_40_cbc "RC2-40-CBC"
527#define LN_rc2_40_cbc "rc2-40-cbc"
528#define NID_rc2_40_cbc 98
529
530#define SN_givenName "G"
531#define LN_givenName "givenName"
532#define NID_givenName 99
533#define OBJ_givenName OBJ_X509,42L
534
535#define SN_surname "S"
536#define LN_surname "surname"
537#define NID_surname 100
538#define OBJ_surname OBJ_X509,4L
539
540#define SN_initials "I"
541#define LN_initials "initials"
542#define NID_initials 101
543#define OBJ_initials OBJ_X509,43L
544
545#define SN_uniqueIdentifier "UID"
546#define LN_uniqueIdentifier "uniqueIdentifier"
547#define NID_uniqueIdentifier 102
548#define OBJ_uniqueIdentifier OBJ_X509,45L
549
550#define SN_crl_distribution_points "crlDistributionPoints"
551#define LN_crl_distribution_points "X509v3 CRL Distribution Points"
552#define NID_crl_distribution_points 103
553#define OBJ_crl_distribution_points OBJ_ld_ce,31L
554
555#define SN_md5WithRSA "RSA-NP-MD5"
556#define LN_md5WithRSA "md5WithRSA"
557#define NID_md5WithRSA 104
558#define OBJ_md5WithRSA OBJ_algorithm,3L
559
560#define SN_serialNumber "SN"
561#define LN_serialNumber "serialNumber"
562#define NID_serialNumber 105
563#define OBJ_serialNumber OBJ_X509,5L
564
565#define SN_title "T"
566#define LN_title "title"
567#define NID_title 106
568#define OBJ_title OBJ_X509,12L
569
570#define SN_description "D"
571#define LN_description "description"
572#define NID_description 107
573#define OBJ_description OBJ_X509,13L
574
575/* CAST5 is CAST-128, I'm just sticking with the documentation */
576#define SN_cast5_cbc "CAST5-CBC"
577#define LN_cast5_cbc "cast5-cbc"
578#define NID_cast5_cbc 108
579#define OBJ_cast5_cbc 1L,2L,840L,113533L,7L,66L,10L
580
581#define SN_cast5_ecb "CAST5-ECB"
582#define LN_cast5_ecb "cast5-ecb"
583#define NID_cast5_ecb 109
584
585#define SN_cast5_cfb64 "CAST5-CFB"
586#define LN_cast5_cfb64 "cast5-cfb"
587#define NID_cast5_cfb64 110
588
589#define SN_cast5_ofb64 "CAST5-OFB"
590#define LN_cast5_ofb64 "cast5-ofb"
591#define NID_cast5_ofb64 111
592
593#define LN_pbeWithMD5AndCast5_CBC "pbeWithMD5AndCast5CBC"
594#define NID_pbeWithMD5AndCast5_CBC 112
595#define OBJ_pbeWithMD5AndCast5_CBC 1L,2L,840L,113533L,7L,66L,12L
596
597/* This is one sun will soon be using :-(
598 * id-dsa-with-sha1 ID ::= {
599 * iso(1) member-body(2) us(840) x9-57 (10040) x9cm(4) 3 }
600 */
601#define SN_dsaWithSHA1 "DSA-SHA1"
602#define LN_dsaWithSHA1 "dsaWithSHA1"
603#define NID_dsaWithSHA1 113
604#define OBJ_dsaWithSHA1 1L,2L,840L,10040L,4L,3L
605
606#define NID_md5_sha1 114
607#define SN_md5_sha1 "MD5-SHA1"
608#define LN_md5_sha1 "md5-sha1"
609
610#define SN_sha1WithRSA "RSA-SHA1-2"
611#define LN_sha1WithRSA "sha1WithRSA"
612#define NID_sha1WithRSA 115
613#define OBJ_sha1WithRSA OBJ_algorithm,29L
614
615#define SN_dsa "DSA"
616#define LN_dsa "dsaEncryption"
617#define NID_dsa 116
618#define OBJ_dsa 1L,2L,840L,10040L,4L,1L
619
620#define SN_ripemd160 "RIPEMD160"
621#define LN_ripemd160 "ripemd160"
622#define NID_ripemd160 117
623#define OBJ_ripemd160 1L,3L,36L,3L,2L,1L
624
625/* The name should actually be rsaSignatureWithripemd160, but I'm going
626 * to contiune using the convention I'm using with the other ciphers */
627#define SN_ripemd160WithRSA "RSA-RIPEMD160"
628#define LN_ripemd160WithRSA "ripemd160WithRSA"
629#define NID_ripemd160WithRSA 119
630#define OBJ_ripemd160WithRSA 1L,3L,36L,3L,3L,1L,2L
631
632/* Taken from rfc2040
633 * RC5_CBC_Parameters ::= SEQUENCE {
634 * version INTEGER (v1_0(16)),
635 * rounds INTEGER (8..127),
636 * blockSizeInBits INTEGER (64, 128),
637 * iv OCTET STRING OPTIONAL
638 * }
639 */
640#define SN_rc5_cbc "RC5-CBC"
641#define LN_rc5_cbc "rc5-cbc"
642#define NID_rc5_cbc 120
643#define OBJ_rc5_cbc OBJ_rsadsi,3L,8L
644
645#define SN_rc5_ecb "RC5-ECB"
646#define LN_rc5_ecb "rc5-ecb"
647#define NID_rc5_ecb 121
648
649#define SN_rc5_cfb64 "RC5-CFB"
650#define LN_rc5_cfb64 "rc5-cfb"
651#define NID_rc5_cfb64 122
652
653#define SN_rc5_ofb64 "RC5-OFB"
654#define LN_rc5_ofb64 "rc5-ofb"
655#define NID_rc5_ofb64 123
656
657#include "bio.h"
658#include "asn1.h"
659
660#define OBJ_create_and_add_object(a,b,c) OBJ_create(a,b,c)
661
662#ifndef NOPROTO
663
664ASN1_OBJECT * OBJ_dup(ASN1_OBJECT *o);
665ASN1_OBJECT * OBJ_nid2obj(int n);
666char * OBJ_nid2ln(int n);
667char * OBJ_nid2sn(int n);
668int OBJ_obj2nid(ASN1_OBJECT *o);
669int OBJ_txt2nid(char *s);
670int OBJ_ln2nid(char *s);
671int OBJ_sn2nid(char *s);
672int OBJ_cmp(ASN1_OBJECT *a,ASN1_OBJECT *b);
673char * OBJ_bsearch(char *key,char *base,int num,int size,int (*cmp)());
674
675void ERR_load_OBJ_strings(void );
676
677int OBJ_new_nid(int num);
678int OBJ_add_object(ASN1_OBJECT *obj);
679int OBJ_create(char *oid,char *sn,char *ln);
680void OBJ_cleanup(void );
681int OBJ_create_objects(BIO *in);
682
683#else
684
685ASN1_OBJECT * OBJ_dup();
686ASN1_OBJECT * OBJ_nid2obj();
687char * OBJ_nid2ln();
688char * OBJ_nid2sn();
689int OBJ_obj2nid();
690int OBJ_txt2nid();
691int OBJ_ln2nid();
692int OBJ_sn2nid();
693int OBJ_cmp();
694char * OBJ_bsearch();
695
696void ERR_load_OBJ_strings();
697
698int OBJ_new_nid();
699int OBJ_add_object();
700int OBJ_create();
701void OBJ_cleanup();
702int OBJ_create_objects();
703
704#endif
705
706/* BEGIN ERROR CODES */
707/* Error codes for the OBJ functions. */
708
709/* Function codes. */
710#define OBJ_F_OBJ_CREATE 100
711#define OBJ_F_OBJ_DUP 101
712#define OBJ_F_OBJ_NID2LN 102
713#define OBJ_F_OBJ_NID2OBJ 103
714#define OBJ_F_OBJ_NID2SN 104
715
716/* Reason codes. */
717#define OBJ_R_MALLOC_FAILURE 100
718#define OBJ_R_UNKNOWN_NID 101
719
720#ifdef __cplusplus
721}
722#endif
723#endif
724
diff --git a/src/lib/libcrypto/objects/objects.txt b/src/lib/libcrypto/objects/objects.txt
new file mode 100644
index 0000000000..cb276e90e9
--- /dev/null
+++ b/src/lib/libcrypto/objects/objects.txt
@@ -0,0 +1,40 @@
11 2 : ISO member bodies
21 2 840 : US (ANSI)
31 2 840 113549 : rsadsi : RSA Data Security, Inc.
41 2 840 113549 1 : pkcs : RSA Data Security, Inc. PKCS
51 2 840 113549 1 1 1 : rsaEncryption
61 2 840 113549 1 1 2 : md2withRSAEncryption
71 2 840 113549 1 1 4 : md5withRSAEncryption
81 2 840 113549 1 7 : pkcs-7
91 2 840 113549 1 7 1 : pkcs-7-data
101 2 840 113549 1 7 2 : pkcs-7-signedData
111 2 840 113549 1 7 3 : pkcs-7-envelopedData
121 2 840 113549 1 7 4 : pkcs-7-signedAndEnvelopedData
131 2 840 113549 1 7 5 : pkcs-7-digestData
141 2 840 113549 1 7 6 : pkcs-7-encryptedData
151 2 840 113549 2 2 : md2
161 2 840 113549 2 4 : md4
171 2 840 113549 2 5 : md5
181 2 840 113549 3 4 : rc4
191 2 840 113549 5 1 : pbeWithMD2AndDES_CBC
201 2 840 113549 5 3 : pbeWithMD5AndDES_CBC
212 5 : X500 : directory services (X.500)
222 5 4 : X509
232 5 4 3 : commonName
242 5 4 6 : countryName
252 5 4 7 : localityName
262 5 4 8 : stateOrProvinceName
272 5 4 10 : organizationName
282 5 4 11 : organizationalUnitName
292 5 8 : directory services - algorithms
302 5 8 1 1 : rsa
31
32algorithm 18 : sha
33encryptionAlgorithm 1 : rsa
34algorithm 11 : rsaSignature
35
36algorithm 6 : desECB
37algorithm 7 : desCBC
38algorithm 8 : desOFB
39algorithm 9 : desCFB
40algorithm 17 : desEDE2
diff --git a/src/lib/libcrypto/pem/message b/src/lib/libcrypto/pem/message
new file mode 100644
index 0000000000..e8bf9d7592
--- /dev/null
+++ b/src/lib/libcrypto/pem/message
@@ -0,0 +1,16 @@
1-----BEGIN PRIVACY-ENHANCED MESSAGE-----
2Proc-Type: 4,ENCRYPTED
3Proc-Type: 4,MIC-ONLY
4Proc-Type: 4,MIC-CLEAR
5Content-Domain: RFC822
6DEK-Info: DES-CBC,0123456789abcdef
7Originator-Certificate
8 xxxx
9Issuer-Certificate
10 xxxx
11MIC-Info: RSA-MD5,RSA,
12 xxxx
13
14
15-----END PRIVACY-ENHANCED MESSAGE-----
16
diff --git a/src/lib/libcrypto/pem/pem.h b/src/lib/libcrypto/pem/pem.h
new file mode 100644
index 0000000000..55fbaeffe2
--- /dev/null
+++ b/src/lib/libcrypto/pem/pem.h
@@ -0,0 +1,562 @@
1/* crypto/pem/pem.org */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
60 *
61 * Always modify pem.org since pem.h is automatically generated from
62 * it during SSLeay configuration.
63 *
64 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
65 */
66
67#ifndef HEADER_PEM_H
68#define HEADER_PEM_H
69
70#ifdef __cplusplus
71extern "C" {
72#endif
73
74#include "evp.h"
75#include "x509.h"
76
77#define PEM_OBJ_UNDEF 0
78#define PEM_OBJ_X509 1
79#define PEM_OBJ_X509_REQ 2
80#define PEM_OBJ_CRL 3
81#define PEM_OBJ_SSL_SESSION 4
82#define PEM_OBJ_PRIV_KEY 10
83#define PEM_OBJ_PRIV_RSA 11
84#define PEM_OBJ_PRIV_DSA 12
85#define PEM_OBJ_PRIV_DH 13
86#define PEM_OBJ_PUB_RSA 14
87#define PEM_OBJ_PUB_DSA 15
88#define PEM_OBJ_PUB_DH 16
89#define PEM_OBJ_DHPARAMS 17
90#define PEM_OBJ_DSAPARAMS 18
91#define PEM_OBJ_PRIV_RSA_PUBLIC 19
92
93#define PEM_ERROR 30
94#define PEM_DEK_DES_CBC 40
95#define PEM_DEK_IDEA_CBC 45
96#define PEM_DEK_DES_EDE 50
97#define PEM_DEK_DES_ECB 60
98#define PEM_DEK_RSA 70
99#define PEM_DEK_RSA_MD2 80
100#define PEM_DEK_RSA_MD5 90
101
102#define PEM_MD_MD2 NID_md2
103#define PEM_MD_MD5 NID_md5
104#define PEM_MD_SHA NID_sha
105#define PEM_MD_MD2_RSA NID_md2WithRSAEncryption
106#define PEM_MD_MD5_RSA NID_md5WithRSAEncryption
107#define PEM_MD_SHA_RSA NID_sha1WithRSAEncryption
108
109#define PEM_STRING_X509_OLD "X509 CERTIFICATE"
110#define PEM_STRING_X509 "CERTIFICATE"
111#define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
112#define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
113#define PEM_STRING_X509_CRL "X509 CRL"
114#define PEM_STRING_EVP_PKEY "PRIVATE KEY"
115#define PEM_STRING_RSA "RSA PRIVATE KEY"
116#define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
117#define PEM_STRING_DSA "DSA PRIVATE KEY"
118#define PEM_STRING_PKCS7 "PKCS7"
119#define PEM_STRING_DHPARAMS "DH PARAMETERS"
120#define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
121#define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
122
123#ifndef HEADER_ENVELOPE_H
124
125#define EVP_ENCODE_CTX_SIZE 96
126#define EVP_MD_SIZE 60
127#define EVP_MD_CTX_SIZE 152
128#define EVP_CIPHER_SIZE 40
129#define EVP_CIPHER_CTX_SIZE 4212
130#define EVP_MAX_MD_SIZE 20
131
132typedef struct evp_encode_ctx_st
133 {
134 char data[EVP_ENCODE_CTX_SIZE];
135 } EVP_ENCODE_CTX;
136
137typedef struct env_md_ctx_st
138 {
139 char data[EVP_MD_CTX_SIZE];
140 } EVP_MD_CTX;
141
142typedef struct evp_cipher_st
143 {
144 char data[EVP_CIPHER_SIZE];
145 } EVP_CIPHER;
146
147typedef struct evp_cipher_ctx_st
148 {
149 char data[EVP_CIPHER_CTX_SIZE];
150 } EVP_CIPHER_CTX;
151#endif
152
153
154typedef struct PEM_Encode_Seal_st
155 {
156 EVP_ENCODE_CTX encode;
157 EVP_MD_CTX md;
158 EVP_CIPHER_CTX cipher;
159 } PEM_ENCODE_SEAL_CTX;
160
161/* enc_type is one off */
162#define PEM_TYPE_ENCRYPTED 10
163#define PEM_TYPE_MIC_ONLY 20
164#define PEM_TYPE_MIC_CLEAR 30
165#define PEM_TYPE_CLEAR 40
166
167typedef struct pem_recip_st
168 {
169 char *name;
170 X509_NAME *dn;
171
172 int cipher;
173 int key_enc;
174 char iv[8];
175 } PEM_USER;
176
177typedef struct pem_ctx_st
178 {
179 int type; /* what type of object */
180
181 struct {
182 int version;
183 int mode;
184 } proc_type;
185
186 char *domain;
187
188 struct {
189 int cipher;
190 unsigned char iv[8];
191 } DEK_info;
192
193 PEM_USER *originator;
194
195 int num_recipient;
196 PEM_USER **recipient;
197
198#ifdef HEADER_STACK_H
199 STACK *x509_chain; /* certificate chain */
200#else
201 char *x509_chain; /* certificate chain */
202#endif
203 EVP_MD *md; /* signature type */
204
205 int md_enc; /* is the md encrypted or not? */
206 int md_len; /* length of md_data */
207 char *md_data; /* message digest, could be pkey encrypted */
208
209 EVP_CIPHER *dec; /* date encryption cipher */
210 int key_len; /* key length */
211 unsigned char *key; /* key */
212 unsigned char iv[8]; /* the iv */
213
214
215 int data_enc; /* is the data encrypted */
216 int data_len;
217 unsigned char *data;
218 } PEM_CTX;
219
220#ifdef SSLEAY_MACROS
221
222#define PEM_write_SSL_SESSION(fp,x) \
223 PEM_ASN1_write((int (*)())i2d_SSL_SESSION, \
224 PEM_STRING_SSL_SESSION,fp, (char *)x, NULL,NULL,0,NULL)
225#define PEM_write_X509(fp,x) \
226 PEM_ASN1_write((int (*)())i2d_X509,PEM_STRING_X509,fp, \
227 (char *)x, NULL,NULL,0,NULL)
228#define PEM_write_X509_REQ(fp,x) PEM_ASN1_write( \
229 (int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,fp,(char *)x, \
230 NULL,NULL,0,NULL)
231#define PEM_write_X509_CRL(fp,x) \
232 PEM_ASN1_write((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL, \
233 fp,(char *)x, NULL,NULL,0,NULL)
234#define PEM_write_RSAPrivateKey(fp,x,enc,kstr,klen,cb) \
235 PEM_ASN1_write((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,fp,\
236 (char *)x,enc,kstr,klen,cb)
237#define PEM_write_RSAPublicKey(fp,x) \
238 PEM_ASN1_write((int (*)())i2d_RSAPublicKey,\
239 PEM_STRING_RSA_PUBLIC,fp,(char *)x,NULL,NULL,0,NULL)
240#define PEM_write_DSAPrivateKey(fp,x,enc,kstr,klen,cb) \
241 PEM_ASN1_write((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,fp,\
242 (char *)x,enc,kstr,klen,cb)
243#define PEM_write_PrivateKey(bp,x,enc,kstr,klen,cb) \
244 PEM_ASN1_write((int (*)())i2d_PrivateKey,\
245 (((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),\
246 bp,(char *)x,enc,kstr,klen,cb)
247#define PEM_write_PKCS7(fp,x) \
248 PEM_ASN1_write((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,fp, \
249 (char *)x, NULL,NULL,0,NULL)
250#define PEM_write_DHparams(fp,x) \
251 PEM_ASN1_write((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,fp,\
252 (char *)x,NULL,NULL,0,NULL)
253
254#define PEM_read_SSL_SESSION(fp,x,cb) (SSL_SESSION *)PEM_ASN1_read( \
255 (char *(*)())d2i_SSL_SESSION,PEM_STRING_SSL_SESSION,fp,(char **)x,cb)
256#define PEM_read_X509(fp,x,cb) (X509 *)PEM_ASN1_read( \
257 (char *(*)())d2i_X509,PEM_STRING_X509,fp,(char **)x,cb)
258#define PEM_read_X509_REQ(fp,x,cb) (X509_REQ *)PEM_ASN1_read( \
259 (char *(*)())d2i_X509_REQ,PEM_STRING_X509_REQ,fp,(char **)x,cb)
260#define PEM_read_X509_CRL(fp,x,cb) (X509_CRL *)PEM_ASN1_read( \
261 (char *(*)())d2i_X509_CRL,PEM_STRING_X509_CRL,fp,(char **)x,cb)
262#define PEM_read_RSAPrivateKey(fp,x,cb) (RSA *)PEM_ASN1_read( \
263 (char *(*)())d2i_RSAPrivateKey,PEM_STRING_RSA,fp,(char **)x,cb)
264#define PEM_read_RSAPublicKey(fp,x,cb) (RSA *)PEM_ASN1_read( \
265 (char *(*)())d2i_RSAPublicKey,PEM_STRING_RSA_PUBLIC,fp,(char **)x,cb)
266#define PEM_read_DSAPrivateKey(fp,x,cb) (DSA *)PEM_ASN1_read( \
267 (char *(*)())d2i_DSAPrivateKey,PEM_STRING_DSA,fp,(char **)x,cb)
268#define PEM_read_PrivateKey(fp,x,cb) (EVP_PKEY *)PEM_ASN1_read( \
269 (char *(*)())d2i_PrivateKey,PEM_STRING_EVP_PKEY,fp,(char **)x,cb)
270#define PEM_read_PKCS7(fp,x,cb) (PKCS7 *)PEM_ASN1_read( \
271 (char *(*)())d2i_PKCS7,PEM_STRING_PKCS7,fp,(char **)x,cb)
272#define PEM_read_DHparams(fp,x,cb) (DH *)PEM_ASN1_read( \
273 (char *(*)())d2i_DHparams,PEM_STRING_DHPARAMS,fp,(char **)x,cb)
274
275#define PEM_write_bio_SSL_SESSION(bp,x) \
276 PEM_ASN1_write_bio((int (*)())i2d_SSL_SESSION, \
277 PEM_STRING_SSL_SESSION,bp, (char *)x, NULL,NULL,0,NULL)
278#define PEM_write_bio_X509(bp,x) \
279 PEM_ASN1_write_bio((int (*)())i2d_X509,PEM_STRING_X509,bp, \
280 (char *)x, NULL,NULL,0,NULL)
281#define PEM_write_bio_X509_REQ(bp,x) PEM_ASN1_write_bio( \
282 (int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,bp,(char *)x, \
283 NULL,NULL,0,NULL)
284#define PEM_write_bio_X509_CRL(bp,x) \
285 PEM_ASN1_write_bio((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,\
286 bp,(char *)x, NULL,NULL,0,NULL)
287#define PEM_write_bio_RSAPrivateKey(bp,x,enc,kstr,klen,cb) \
288 PEM_ASN1_write_bio((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,\
289 bp,(char *)x,enc,kstr,klen,cb)
290#define PEM_write_bio_RSAPublicKey(bp,x) \
291 PEM_ASN1_write_bio((int (*)())i2d_RSAPublicKey, \
292 PEM_STRING_RSA_PUBLIC,\
293 bp,(char *)x,NULL,NULL,0,NULL)
294#define PEM_write_bio_DSAPrivateKey(bp,x,enc,kstr,klen,cb) \
295 PEM_ASN1_write_bio((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,\
296 bp,(char *)x,enc,kstr,klen,cb)
297#define PEM_write_bio_PrivateKey(bp,x,enc,kstr,klen,cb) \
298 PEM_ASN1_write_bio((int (*)())i2d_PrivateKey,\
299 (((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),\
300 bp,(char *)x,enc,kstr,klen,cb)
301#define PEM_write_bio_PKCS7(bp,x) \
302 PEM_ASN1_write_bio((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,bp, \
303 (char *)x, NULL,NULL,0,NULL)
304#define PEM_write_bio_DHparams(bp,x) \
305 PEM_ASN1_write_bio((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,\
306 bp,(char *)x,NULL,NULL,0,NULL)
307#define PEM_write_bio_DSAparams(bp,x) \
308 PEM_ASN1_write_bio((int (*)())i2d_DSAparams, \
309 PEM_STRING_DSAPARAMS,bp,(char *)x,NULL,NULL,0,NULL)
310
311#define PEM_read_bio_SSL_SESSION(bp,x,cb) (SSL_SESSION *)PEM_ASN1_read_bio( \
312 (char *(*)())d2i_SSL_SESSION,PEM_STRING_SSL_SESSION,bp,(char **)x,cb)
313#define PEM_read_bio_X509(bp,x,cb) (X509 *)PEM_ASN1_read_bio( \
314 (char *(*)())d2i_X509,PEM_STRING_X509,bp,(char **)x,cb)
315#define PEM_read_bio_X509_REQ(bp,x,cb) (X509_REQ *)PEM_ASN1_read_bio( \
316 (char *(*)())d2i_X509_REQ,PEM_STRING_X509_REQ,bp,(char **)x,cb)
317#define PEM_read_bio_X509_CRL(bp,x,cb) (X509_CRL *)PEM_ASN1_read_bio( \
318 (char *(*)())d2i_X509_CRL,PEM_STRING_X509_CRL,bp,(char **)x,cb)
319#define PEM_read_bio_RSAPrivateKey(bp,x,cb) (RSA *)PEM_ASN1_read_bio( \
320 (char *(*)())d2i_RSAPrivateKey,PEM_STRING_RSA,bp,(char **)x,cb)
321#define PEM_read_bio_RSAPublicKey(bp,x,cb) (RSA *)PEM_ASN1_read_bio( \
322 (char *(*)())d2i_RSAPublicKey,PEM_STRING_RSA_PUBLIC,bp,(char **)x,cb)
323#define PEM_read_bio_DSAPrivateKey(bp,x,cb) (DSA *)PEM_ASN1_read_bio( \
324 (char *(*)())d2i_DSAPrivateKey,PEM_STRING_DSA,bp,(char **)x,cb)
325#define PEM_read_bio_PrivateKey(bp,x,cb) (EVP_PKEY *)PEM_ASN1_read_bio( \
326 (char *(*)())d2i_PrivateKey,PEM_STRING_EVP_PKEY,bp,(char **)x,cb)
327
328#define PEM_read_bio_PKCS7(bp,x,cb) (PKCS7 *)PEM_ASN1_read_bio( \
329 (char *(*)())d2i_PKCS7,PEM_STRING_PKCS7,bp,(char **)x,cb)
330#define PEM_read_bio_DHparams(bp,x,cb) (DH *)PEM_ASN1_read_bio( \
331 (char *(*)())d2i_DHparams,PEM_STRING_DHPARAMS,bp,(char **)x,cb)
332#define PEM_read_bio_DSAparams(bp,x,cb) (DSA *)PEM_ASN1_read_bio( \
333 (char *(*)())d2i_DSAparams,PEM_STRING_DSAPARAMS,bp,(char **)x,cb)
334
335#endif
336
337#ifndef NOPROTO
338int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
339int PEM_do_header (EVP_CIPHER_INFO *cipher, unsigned char *data,long *len,
340 int (*callback)());
341
342#ifdef HEADER_BIO_H
343int PEM_read_bio(BIO *bp, char **name, char **header,
344 unsigned char **data,long *len);
345int PEM_write_bio(BIO *bp,char *name,char *hdr,unsigned char *data,
346 long len);
347char * PEM_ASN1_read_bio(char *(*d2i)(),char *name,BIO *bp,char **x,
348 int (*cb)());
349int PEM_ASN1_write_bio(int (*i2d)(),char *name,BIO *bp,char *x,
350 EVP_CIPHER *enc,unsigned char *kstr,int klen,int (*callback)());
351STACK * PEM_X509_INFO_read_bio(BIO *bp, STACK *sk, int (*cb)());
352int PEM_X509_INFO_write_bio(BIO *bp,X509_INFO *xi, EVP_CIPHER *enc,
353 unsigned char *kstr, int klen, int (*cb)());
354#endif
355
356#ifndef WIN16
357int PEM_read(FILE *fp, char **name, char **header,
358 unsigned char **data,long *len);
359int PEM_write(FILE *fp,char *name,char *hdr,unsigned char *data,long len);
360char * PEM_ASN1_read(char *(*d2i)(),char *name,FILE *fp,char **x,
361 int (*cb)());
362int PEM_ASN1_write(int (*i2d)(),char *name,FILE *fp,char *x,
363 EVP_CIPHER *enc,unsigned char *kstr,int klen,int (*callback)());
364STACK * PEM_X509_INFO_read(FILE *fp, STACK *sk, int (*cb)());
365#endif
366
367int PEM_SealInit(PEM_ENCODE_SEAL_CTX *ctx, EVP_CIPHER *type,
368 EVP_MD *md_type, unsigned char **ek, int *ekl,
369 unsigned char *iv, EVP_PKEY **pubk, int npubk);
370void PEM_SealUpdate(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *out, int *outl,
371 unsigned char *in, int inl);
372int PEM_SealFinal(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *sig,int *sigl,
373 unsigned char *out, int *outl, EVP_PKEY *priv);
374
375void PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
376void PEM_SignUpdate(EVP_MD_CTX *ctx,unsigned char *d,unsigned int cnt);
377int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
378 unsigned int *siglen, EVP_PKEY *pkey);
379
380void ERR_load_PEM_strings(void);
381
382void PEM_proc_type(char *buf, int type);
383void PEM_dek_info(char *buf, char *type, int len, char *str);
384
385#ifndef SSLEAY_MACROS
386
387#ifndef WIN16
388X509 *PEM_read_X509(FILE *fp,X509 **x,int (*cb)());
389X509_REQ *PEM_read_X509_REQ(FILE *fp,X509_REQ **x,int (*cb)());
390X509_CRL *PEM_read_X509_CRL(FILE *fp,X509_CRL **x,int (*cb)());
391RSA *PEM_read_RSAPrivateKey(FILE *fp,RSA **x,int (*cb)());
392RSA *PEM_read_RSAPublicKey(FILE *fp,RSA **x,int (*cb)());
393DSA *PEM_read_DSAPrivateKey(FILE *fp,DSA **x,int (*cb)());
394EVP_PKEY *PEM_read_PrivateKey(FILE *fp,EVP_PKEY **x,int (*cb)());
395PKCS7 *PEM_read_PKCS7(FILE *fp,PKCS7 **x,int (*cb)());
396DH *PEM_read_DHparams(FILE *fp,DH **x,int (*cb)());
397DSA *PEM_read_DSAparams(FILE *fp,DSA **x,int (*cb)());
398int PEM_write_X509(FILE *fp,X509 *x);
399int PEM_write_X509_REQ(FILE *fp,X509_REQ *x);
400int PEM_write_X509_CRL(FILE *fp,X509_CRL *x);
401int PEM_write_RSAPrivateKey(FILE *fp,RSA *x,EVP_CIPHER *enc,unsigned char *kstr,
402 int klen,int (*cb)());
403int PEM_write_RSAPublicKey(FILE *fp,RSA *x);
404int PEM_write_DSAPrivateKey(FILE *fp,DSA *x,EVP_CIPHER *enc,unsigned char *kstr,
405 int klen,int (*cb)());
406int PEM_write_PrivateKey(FILE *fp,EVP_PKEY *x,EVP_CIPHER *enc,
407 unsigned char *kstr,int klen,int (*cb)());
408int PEM_write_PKCS7(FILE *fp,PKCS7 *x);
409int PEM_write_DHparams(FILE *fp,DH *x);
410int PEM_write_DSAparams(FILE *fp,DSA *x);
411#endif
412
413#ifdef HEADER_BIO_H
414X509 *PEM_read_bio_X509(BIO *bp,X509 **x,int (*cb)());
415X509_REQ *PEM_read_bio_X509_REQ(BIO *bp,X509_REQ **x,int (*cb)());
416X509_CRL *PEM_read_bio_X509_CRL(BIO *bp,X509_CRL **x,int (*cb)());
417RSA *PEM_read_bio_RSAPrivateKey(BIO *bp,RSA **x,int (*cb)());
418RSA *PEM_read_bio_RSAPublicKey(BIO *bp,RSA **x,int (*cb)());
419DSA *PEM_read_bio_DSAPrivateKey(BIO *bp,DSA **x,int (*cb)());
420EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp,EVP_PKEY **x,int (*cb)());
421PKCS7 *PEM_read_bio_PKCS7(BIO *bp,PKCS7 **x,int (*cb)());
422DH *PEM_read_bio_DHparams(BIO *bp,DH **x,int (*cb)());
423DSA *PEM_read_bio_DSAparams(BIO *bp,DSA **x,int (*cb)());
424int PEM_write_bio_X509(BIO *bp,X509 *x);
425int PEM_write_bio_X509_REQ(BIO *bp,X509_REQ *x);
426int PEM_write_bio_X509_CRL(BIO *bp,X509_CRL *x);
427int PEM_write_bio_RSAPrivateKey(BIO *fp,RSA *x,EVP_CIPHER *enc,
428 unsigned char *kstr,int klen,int (*cb)());
429int PEM_write_bio_RSAPublicKey(BIO *fp,RSA *x);
430int PEM_write_bio_DSAPrivateKey(BIO *fp,DSA *x,EVP_CIPHER *enc,
431 unsigned char *kstr,int klen,int (*cb)());
432int PEM_write_bio_PrivateKey(BIO *fp,EVP_PKEY *x,EVP_CIPHER *enc,
433 unsigned char *kstr,int klen,int (*cb)());
434int PEM_write_bio_PKCS7(BIO *bp,PKCS7 *x);
435int PEM_write_bio_DHparams(BIO *bp,DH *x);
436int PEM_write_bio_DSAparams(BIO *bp,DSA *x);
437#endif
438
439#endif /* SSLEAY_MACROS */
440
441
442#else
443
444int PEM_get_EVP_CIPHER_INFO();
445int PEM_do_header();
446int PEM_read_bio();
447int PEM_write_bio();
448#ifndef WIN16
449int PEM_read();
450int PEM_write();
451STACK * PEM_X509_INFO_read();
452char * PEM_ASN1_read();
453int PEM_ASN1_write();
454#endif
455STACK * PEM_X509_INFO_read_bio();
456int PEM_X509_INFO_write_bio();
457char * PEM_ASN1_read_bio();
458int PEM_ASN1_write_bio();
459int PEM_SealInit();
460void PEM_SealUpdate();
461int PEM_SealFinal();
462int PEM_SignFinal();
463
464void ERR_load_PEM_strings();
465
466void PEM_proc_type();
467void PEM_dek_info();
468
469#ifndef SSLEAY_MACROS
470#ifndef WIN16
471X509 *PEM_read_X509();
472X509_REQ *PEM_read_X509_REQ();
473X509_CRL *PEM_read_X509_CRL();
474RSA *PEM_read_RSAPrivateKey();
475RSA *PEM_read_RSAPublicKey();
476DSA *PEM_read_DSAPrivateKey();
477EVP_PKEY *PEM_read_PrivateKey();
478PKCS7 *PEM_read_PKCS7();
479DH *PEM_read_DHparams();
480DSA *PEM_read_DSAparams();
481int PEM_write_X509();
482int PEM_write_X509_REQ();
483int PEM_write_X509_CRL();
484int PEM_write_RSAPrivateKey();
485int PEM_write_RSAPublicKey();
486int PEM_write_DSAPrivateKey();
487int PEM_write_PrivateKey();
488int PEM_write_PKCS7();
489int PEM_write_DHparams();
490int PEM_write_DSAparams();
491#endif
492
493X509 *PEM_read_bio_X509();
494X509_REQ *PEM_read_bio_X509_REQ();
495X509_CRL *PEM_read_bio_X509_CRL();
496RSA *PEM_read_bio_RSAPrivateKey();
497RSA *PEM_read_bio_RSAPublicKey();
498DSA *PEM_read_bio_DSAPrivateKey();
499EVP_PKEY *PEM_read_bio_PrivateKey();
500PKCS7 *PEM_read_bio_PKCS7();
501DH *PEM_read_bio_DHparams();
502DSA *PEM_read_bio_DSAparams();
503int PEM_write_bio_X509();
504int PEM_write_bio_X509_REQ();
505int PEM_write_bio_X509_CRL();
506int PEM_write_bio_RSAPrivateKey();
507int PEM_write_bio_RSAPublicKey();
508int PEM_write_bio_DSAPrivateKey();
509int PEM_write_bio_PrivateKey();
510int PEM_write_bio_PKCS7();
511int PEM_write_bio_DHparams();
512int PEM_write_bio_DSAparams();
513
514#endif /* SSLEAY_MACROS */
515
516#endif
517
518/* BEGIN ERROR CODES */
519/* Error codes for the PEM functions. */
520
521/* Function codes. */
522#define PEM_F_DEF_CALLBACK 100
523#define PEM_F_LOAD_IV 101
524#define PEM_F_PEM_ASN1_READ 102
525#define PEM_F_PEM_ASN1_READ_BIO 103
526#define PEM_F_PEM_ASN1_WRITE 104
527#define PEM_F_PEM_ASN1_WRITE_BIO 105
528#define PEM_F_PEM_DO_HEADER 106
529#define PEM_F_PEM_GET_EVP_CIPHER_INFO 107
530#define PEM_F_PEM_READ 108
531#define PEM_F_PEM_READ_BIO 109
532#define PEM_F_PEM_SEALFINAL 110
533#define PEM_F_PEM_SEALINIT 111
534#define PEM_F_PEM_SIGNFINAL 112
535#define PEM_F_PEM_WRITE 113
536#define PEM_F_PEM_WRITE_BIO 114
537#define PEM_F_PEM_X509_INFO_READ 115
538#define PEM_F_PEM_X509_INFO_READ_BIO 116
539#define PEM_F_PEM_X509_INFO_WRITE_BIO 117
540
541/* Reason codes. */
542#define PEM_R_BAD_BASE64_DECODE 100
543#define PEM_R_BAD_DECRYPT 101
544#define PEM_R_BAD_END_LINE 102
545#define PEM_R_BAD_IV_CHARS 103
546#define PEM_R_BAD_PASSWORD_READ 104
547#define PEM_R_NOT_DEK_INFO 105
548#define PEM_R_NOT_ENCRYPTED 106
549#define PEM_R_NOT_PROC_TYPE 107
550#define PEM_R_NO_START_LINE 108
551#define PEM_R_PROBLEMS_GETTING_PASSWORD 109
552#define PEM_R_PUBLIC_KEY_NO_RSA 110
553#define PEM_R_READ_KEY 111
554#define PEM_R_SHORT_HEADER 112
555#define PEM_R_UNSUPPORTED_CIPHER 113
556#define PEM_R_UNSUPPORTED_ENCRYPTION 114
557
558#ifdef __cplusplus
559}
560#endif
561#endif
562
diff --git a/src/lib/libcrypto/pem/pem_all.c b/src/lib/libcrypto/pem/pem_all.c
new file mode 100644
index 0000000000..d1cda7aabe
--- /dev/null
+++ b/src/lib/libcrypto/pem/pem_all.c
@@ -0,0 +1,488 @@
1/* crypto/pem/pem_all.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#undef SSLEAY_MACROS
61#include "cryptlib.h"
62#include "bio.h"
63#include "evp.h"
64#include "x509.h"
65#include "pkcs7.h"
66#include "pem.h"
67
68#ifndef NO_FP_API
69/* The X509 functions */
70X509 *PEM_read_X509(fp,x,cb)
71FILE *fp;
72X509 **x;
73int (*cb)();
74 {
75 return((X509 *)PEM_ASN1_read((char *(*)())d2i_X509,
76 PEM_STRING_X509,fp,(char **)x,cb));
77 }
78#endif
79
80X509 *PEM_read_bio_X509(bp,x,cb)
81BIO *bp;
82X509 **x;
83int (*cb)();
84 {
85 return((X509 *)PEM_ASN1_read_bio((char *(*)())d2i_X509,
86 PEM_STRING_X509,bp,(char **)x,cb));
87 }
88
89#ifndef NO_FP_API
90int PEM_write_X509(fp,x)
91FILE *fp;
92X509 *x;
93 {
94 return(PEM_ASN1_write((int (*)())i2d_X509,PEM_STRING_X509,fp,
95 (char *)x, NULL,NULL,0,NULL));
96 }
97#endif
98
99int PEM_write_bio_X509(bp,x)
100BIO *bp;
101X509 *x;
102 {
103 return(PEM_ASN1_write_bio((int (*)())i2d_X509,PEM_STRING_X509,bp,
104 (char *)x, NULL,NULL,0,NULL));
105 }
106
107#ifndef NO_FP_API
108/* The X509_REQ functions */
109X509_REQ *PEM_read_X509_REQ(fp,x,cb)
110FILE *fp;
111X509_REQ **x;
112int (*cb)();
113 {
114 return((X509_REQ *)PEM_ASN1_read((char *(*)())d2i_X509_REQ,
115 PEM_STRING_X509_REQ,fp,(char **)x,cb));
116 }
117#endif
118
119X509_REQ *PEM_read_bio_X509_REQ(bp,x,cb)
120BIO *bp;
121X509_REQ **x;
122int (*cb)();
123 {
124 return((X509_REQ *)PEM_ASN1_read_bio((char *(*)())d2i_X509_REQ,
125 PEM_STRING_X509_REQ,bp,(char **)x,cb));
126 }
127
128#ifndef NO_FP_API
129int PEM_write_X509_REQ(fp,x)
130FILE *fp;
131X509_REQ *x;
132 {
133 return(PEM_ASN1_write((int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,fp,
134 (char *)x, NULL,NULL,0,NULL));
135 }
136#endif
137
138int PEM_write_bio_X509_REQ(bp,x)
139BIO *bp;
140X509_REQ *x;
141 {
142 return(PEM_ASN1_write_bio((int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,
143 bp,(char *)x, NULL,NULL,0,NULL));
144 }
145
146#ifndef NO_FP_API
147/* The X509_CRL functions */
148X509_CRL *PEM_read_X509_CRL(fp,x,cb)
149FILE *fp;
150X509_CRL **x;
151int (*cb)();
152 {
153 return((X509_CRL *)PEM_ASN1_read((char *(*)())d2i_X509_CRL,
154 PEM_STRING_X509_CRL,fp,(char **)x,cb));
155 }
156#endif
157
158X509_CRL *PEM_read_bio_X509_CRL(bp,x,cb)
159BIO *bp;
160X509_CRL **x;
161int (*cb)();
162 {
163 return((X509_CRL *)PEM_ASN1_read_bio((char *(*)())d2i_X509_CRL,
164 PEM_STRING_X509_CRL,bp,(char **)x,cb));
165 }
166
167#ifndef NO_FP_API
168int PEM_write_X509_CRL(fp,x)
169FILE *fp;
170X509_CRL *x;
171 {
172 return(PEM_ASN1_write((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,fp,
173 (char *)x, NULL,NULL,0,NULL));
174 }
175#endif
176
177int PEM_write_bio_X509_CRL(bp,x)
178BIO *bp;
179X509_CRL *x;
180 {
181 return(PEM_ASN1_write_bio((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,
182 bp,(char *)x, NULL,NULL,0,NULL));
183 }
184
185#ifndef NO_RSA
186#ifndef NO_FP_API
187/* The RSAPrivateKey functions */
188RSA *PEM_read_RSAPrivateKey(fp,x,cb)
189FILE *fp;
190RSA **x;
191int (*cb)();
192 {
193 return((RSA *)PEM_ASN1_read((char *(*)())d2i_RSAPrivateKey,
194 PEM_STRING_RSA,fp,(char **)x,cb));
195 }
196
197RSA *PEM_read_RSAPublicKey(fp,x,cb)
198FILE *fp;
199RSA **x;
200int (*cb)();
201 {
202 return((RSA *)PEM_ASN1_read((char *(*)())d2i_RSAPublicKey,
203 PEM_STRING_RSA_PUBLIC,fp,(char **)x,cb));
204 }
205#endif
206
207RSA *PEM_read_bio_RSAPrivateKey(bp,x,cb)
208BIO *bp;
209RSA **x;
210int (*cb)();
211 {
212 return((RSA *)PEM_ASN1_read_bio((char *(*)())d2i_RSAPrivateKey,
213 PEM_STRING_RSA,bp,(char **)x,cb));
214 }
215
216RSA *PEM_read_bio_RSAPublicKey(bp,x,cb)
217BIO *bp;
218RSA **x;
219int (*cb)();
220 {
221 return((RSA *)PEM_ASN1_read_bio((char *(*)())d2i_RSAPublicKey,
222 PEM_STRING_RSA_PUBLIC,bp,(char **)x,cb));
223 }
224
225#ifndef NO_FP_API
226int PEM_write_RSAPrivateKey(fp,x,enc,kstr,klen,cb)
227FILE *fp;
228RSA *x;
229EVP_CIPHER *enc;
230unsigned char *kstr;
231int klen;
232int (*cb)();
233 {
234 return(PEM_ASN1_write((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,fp,
235 (char *)x,enc,kstr,klen,cb));
236 }
237
238int PEM_write_RSAPublicKey(fp,x)
239FILE *fp;
240RSA *x;
241 {
242 return(PEM_ASN1_write((int (*)())i2d_RSAPublicKey,
243 PEM_STRING_RSA_PUBLIC,fp,
244 (char *)x,NULL,NULL,0,NULL));
245 }
246#endif
247
248int PEM_write_bio_RSAPrivateKey(bp,x,enc,kstr,klen,cb)
249BIO *bp;
250RSA *x;
251EVP_CIPHER *enc;
252unsigned char *kstr;
253int klen;
254int (*cb)();
255 {
256 return(PEM_ASN1_write_bio((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,
257 bp,(char *)x,enc,kstr,klen,cb));
258 }
259
260int PEM_write_bio_RSAPublicKey(bp,x)
261BIO *bp;
262RSA *x;
263 {
264 return(PEM_ASN1_write_bio((int (*)())i2d_RSAPublicKey,
265 PEM_STRING_RSA_PUBLIC,
266 bp,(char *)x,NULL,NULL,0,NULL));
267 }
268#endif /* !NO_RSA */
269
270#ifndef NO_DSA
271#ifndef NO_FP_API
272/* The DSAPrivateKey functions */
273DSA *PEM_read_DSAPrivateKey(fp,x,cb)
274FILE *fp;
275DSA **x;
276int (*cb)();
277 {
278 return((DSA *)PEM_ASN1_read((char *(*)())d2i_DSAPrivateKey,
279 PEM_STRING_DSA,fp,(char **)x,cb));
280 }
281#endif
282
283DSA *PEM_read_bio_DSAPrivateKey(bp,x,cb)
284BIO *bp;
285DSA **x;
286int (*cb)();
287 {
288 return((DSA *)PEM_ASN1_read_bio((char *(*)())d2i_DSAPrivateKey,
289 PEM_STRING_DSA,bp,(char **)x,cb));
290 }
291
292#ifndef NO_FP_API
293int PEM_write_DSAPrivateKey(fp,x,enc,kstr,klen,cb)
294FILE *fp;
295DSA *x;
296EVP_CIPHER *enc;
297unsigned char *kstr;
298int klen;
299int (*cb)();
300 {
301 return(PEM_ASN1_write((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,fp,
302 (char *)x,enc,kstr,klen,cb));
303 }
304#endif
305
306int PEM_write_bio_DSAPrivateKey(bp,x,enc,kstr,klen,cb)
307BIO *bp;
308DSA *x;
309EVP_CIPHER *enc;
310unsigned char *kstr;
311int klen;
312int (*cb)();
313 {
314 return(PEM_ASN1_write_bio((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,
315 bp,(char *)x,enc,kstr,klen,cb));
316 }
317#endif
318
319#ifndef NO_FP_API
320/* The PrivateKey functions */
321EVP_PKEY *PEM_read_PrivateKey(fp,x,cb)
322FILE *fp;
323EVP_PKEY **x;
324int (*cb)();
325 {
326 return((EVP_PKEY *)PEM_ASN1_read((char *(*)())d2i_PrivateKey,
327 PEM_STRING_EVP_PKEY,fp,(char **)x,cb));
328 }
329#endif
330
331EVP_PKEY *PEM_read_bio_PrivateKey(bp,x,cb)
332BIO *bp;
333EVP_PKEY **x;
334int (*cb)();
335 {
336 return((EVP_PKEY *)PEM_ASN1_read_bio((char *(*)())d2i_PrivateKey,
337 PEM_STRING_EVP_PKEY,bp,(char **)x,cb));
338 }
339
340#ifndef NO_FP_API
341int PEM_write_PrivateKey(fp,x,enc,kstr,klen,cb)
342FILE *fp;
343EVP_PKEY *x;
344EVP_CIPHER *enc;
345unsigned char *kstr;
346int klen;
347int (*cb)();
348 {
349 return(PEM_ASN1_write((int (*)())i2d_PrivateKey,
350 ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),
351 fp,(char *)x,enc,kstr,klen,cb));
352 }
353#endif
354
355int PEM_write_bio_PrivateKey(bp,x,enc,kstr,klen,cb)
356BIO *bp;
357EVP_PKEY *x;
358EVP_CIPHER *enc;
359unsigned char *kstr;
360int klen;
361int (*cb)();
362 {
363 return(PEM_ASN1_write_bio((int (*)())i2d_PrivateKey,
364 ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),
365 bp,(char *)x,enc,kstr,klen,cb));
366 }
367
368#ifndef NO_FP_API
369/* The PKCS7 functions */
370PKCS7 *PEM_read_PKCS7(fp,x,cb)
371FILE *fp;
372PKCS7 **x;
373int (*cb)();
374 {
375 return((PKCS7 *)PEM_ASN1_read((char *(*)())d2i_PKCS7,
376 PEM_STRING_PKCS7,fp,(char **)x,cb));
377 }
378#endif
379
380PKCS7 *PEM_read_bio_PKCS7(bp,x,cb)
381BIO *bp;
382PKCS7 **x;
383int (*cb)();
384 {
385 return((PKCS7 *)PEM_ASN1_read_bio((char *(*)())d2i_PKCS7,
386 PEM_STRING_PKCS7,bp,(char **)x,cb));
387 }
388
389#ifndef NO_FP_API
390int PEM_write_PKCS7(fp,x)
391FILE *fp;
392PKCS7 *x;
393 {
394 return(PEM_ASN1_write((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,fp,
395 (char *)x, NULL,NULL,0,NULL));
396 }
397#endif
398
399int PEM_write_bio_PKCS7(bp,x)
400BIO *bp;
401PKCS7 *x;
402 {
403 return(PEM_ASN1_write_bio((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,bp,
404 (char *)x, NULL,NULL,0,NULL));
405 }
406
407#ifndef NO_DH
408#ifndef NO_FP_API
409/* The DHparams functions */
410DH *PEM_read_DHparams(fp,x,cb)
411FILE *fp;
412DH **x;
413int (*cb)();
414 {
415 return((DH *)PEM_ASN1_read((char *(*)())d2i_DHparams,
416 PEM_STRING_DHPARAMS,fp,(char **)x,cb));
417 }
418#endif
419
420DH *PEM_read_bio_DHparams(bp,x,cb)
421BIO *bp;
422DH **x;
423int (*cb)();
424 {
425 return((DH *)PEM_ASN1_read_bio((char *(*)())d2i_DHparams,
426 PEM_STRING_DHPARAMS,bp,(char **)x,cb));
427 }
428
429#ifndef NO_FP_API
430int PEM_write_DHparams(fp,x)
431FILE *fp;
432DH *x;
433 {
434 return(PEM_ASN1_write((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,fp,
435 (char *)x, NULL,NULL,0,NULL));
436 }
437#endif
438
439int PEM_write_bio_DHparams(bp,x)
440BIO *bp;
441DH *x;
442 {
443 return(PEM_ASN1_write_bio((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,
444 bp,(char *)x, NULL,NULL,0,NULL));
445 }
446#endif
447
448#ifndef NO_DSA
449#ifndef NO_FP_API
450/* The DSAparams functions */
451DSA *PEM_read_DSAparams(fp,x,cb)
452FILE *fp;
453DSA **x;
454int (*cb)();
455 {
456 return((DSA *)PEM_ASN1_read((char *(*)())d2i_DSAparams,
457 PEM_STRING_DSAPARAMS,fp,(char **)x,cb));
458 }
459#endif
460
461DSA *PEM_read_bio_DSAparams(bp,x,cb)
462BIO *bp;
463DSA **x;
464int (*cb)();
465 {
466 return((DSA *)PEM_ASN1_read_bio((char *(*)())d2i_DSAparams,
467 PEM_STRING_DSAPARAMS,bp,(char **)x,cb));
468 }
469
470#ifndef NO_FP_API
471int PEM_write_DSAparams(fp,x)
472FILE *fp;
473DSA *x;
474 {
475 return(PEM_ASN1_write((int (*)())i2d_DSAparams,PEM_STRING_DSAPARAMS,fp,
476 (char *)x, NULL,NULL,0,NULL));
477 }
478#endif
479
480int PEM_write_bio_DSAparams(bp,x)
481BIO *bp;
482DSA *x;
483 {
484 return(PEM_ASN1_write_bio((int (*)())i2d_DSAparams,PEM_STRING_DSAPARAMS,
485 bp,(char *)x, NULL,NULL,0,NULL));
486 }
487#endif
488
diff --git a/src/lib/libcrypto/pem/pem_err.c b/src/lib/libcrypto/pem/pem_err.c
new file mode 100644
index 0000000000..e17fcdb540
--- /dev/null
+++ b/src/lib/libcrypto/pem/pem_err.c
@@ -0,0 +1,122 @@
1/* lib/pem/pem_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "pem.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA PEM_str_functs[]=
65 {
66{ERR_PACK(0,PEM_F_DEF_CALLBACK,0), "DEF_CALLBACK"},
67{ERR_PACK(0,PEM_F_LOAD_IV,0), "LOAD_IV"},
68{ERR_PACK(0,PEM_F_PEM_ASN1_READ,0), "PEM_ASN1_read"},
69{ERR_PACK(0,PEM_F_PEM_ASN1_READ_BIO,0), "PEM_ASN1_read_bio"},
70{ERR_PACK(0,PEM_F_PEM_ASN1_WRITE,0), "PEM_ASN1_write"},
71{ERR_PACK(0,PEM_F_PEM_ASN1_WRITE_BIO,0), "PEM_ASN1_write_bio"},
72{ERR_PACK(0,PEM_F_PEM_DO_HEADER,0), "PEM_do_header"},
73{ERR_PACK(0,PEM_F_PEM_GET_EVP_CIPHER_INFO,0), "PEM_get_EVP_CIPHER_INFO"},
74{ERR_PACK(0,PEM_F_PEM_READ,0), "PEM_read"},
75{ERR_PACK(0,PEM_F_PEM_READ_BIO,0), "PEM_read_bio"},
76{ERR_PACK(0,PEM_F_PEM_SEALFINAL,0), "PEM_SealFinal"},
77{ERR_PACK(0,PEM_F_PEM_SEALINIT,0), "PEM_SealInit"},
78{ERR_PACK(0,PEM_F_PEM_SIGNFINAL,0), "PEM_SignFinal"},
79{ERR_PACK(0,PEM_F_PEM_WRITE,0), "PEM_write"},
80{ERR_PACK(0,PEM_F_PEM_WRITE_BIO,0), "PEM_write_bio"},
81{ERR_PACK(0,PEM_F_PEM_X509_INFO_READ,0), "PEM_X509_INFO_read"},
82{ERR_PACK(0,PEM_F_PEM_X509_INFO_READ_BIO,0), "PEM_X509_INFO_read_bio"},
83{ERR_PACK(0,PEM_F_PEM_X509_INFO_WRITE_BIO,0), "PEM_X509_INFO_write_bio"},
84{0,NULL},
85 };
86
87static ERR_STRING_DATA PEM_str_reasons[]=
88 {
89{PEM_R_BAD_BASE64_DECODE ,"bad base64 decode"},
90{PEM_R_BAD_DECRYPT ,"bad decrypt"},
91{PEM_R_BAD_END_LINE ,"bad end line"},
92{PEM_R_BAD_IV_CHARS ,"bad iv chars"},
93{PEM_R_BAD_PASSWORD_READ ,"bad password read"},
94{PEM_R_NOT_DEK_INFO ,"not dek info"},
95{PEM_R_NOT_ENCRYPTED ,"not encrypted"},
96{PEM_R_NOT_PROC_TYPE ,"not proc type"},
97{PEM_R_NO_START_LINE ,"no start line"},
98{PEM_R_PROBLEMS_GETTING_PASSWORD ,"problems getting password"},
99{PEM_R_PUBLIC_KEY_NO_RSA ,"public key no rsa"},
100{PEM_R_READ_KEY ,"read key"},
101{PEM_R_SHORT_HEADER ,"short header"},
102{PEM_R_UNSUPPORTED_CIPHER ,"unsupported cipher"},
103{PEM_R_UNSUPPORTED_ENCRYPTION ,"unsupported encryption"},
104{0,NULL},
105 };
106
107#endif
108
109void ERR_load_PEM_strings()
110 {
111 static int init=1;
112
113 if (init);
114 {;
115 init=0;
116#ifndef NO_ERR
117 ERR_load_strings(ERR_LIB_PEM,PEM_str_functs);
118 ERR_load_strings(ERR_LIB_PEM,PEM_str_reasons);
119#endif
120
121 }
122 }
diff --git a/src/lib/libcrypto/pem/pem_info.c b/src/lib/libcrypto/pem/pem_info.c
new file mode 100644
index 0000000000..4b69833b62
--- /dev/null
+++ b/src/lib/libcrypto/pem/pem_info.c
@@ -0,0 +1,365 @@
1/* crypto/pem/pem_info.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "objects.h"
63#include "evp.h"
64#include "x509.h"
65#include "pem.h"
66
67#ifndef NO_FP_API
68STACK *PEM_X509_INFO_read(fp,sk,cb)
69FILE *fp;
70STACK *sk;
71int (*cb)();
72 {
73 BIO *b;
74 STACK *ret;
75
76 if ((b=BIO_new(BIO_s_file())) == NULL)
77 {
78 PEMerr(PEM_F_PEM_X509_INFO_READ,ERR_R_BUF_LIB);
79 return(0);
80 }
81 BIO_set_fp(b,fp,BIO_NOCLOSE);
82 ret=PEM_X509_INFO_read_bio(b,sk,cb);
83 BIO_free(b);
84 return(ret);
85 }
86#endif
87
88STACK *PEM_X509_INFO_read_bio(bp,sk,cb)
89BIO *bp;
90STACK *sk;
91int (*cb)();
92 {
93 X509_INFO *xi=NULL;
94 char *name=NULL,*header=NULL,**pp;
95 unsigned char *data=NULL,*p;
96 long len,error=0;
97 int ok=0;
98 STACK *ret=NULL;
99 unsigned int i,raw;
100 char *(*d2i)();
101
102 if (sk == NULL)
103 {
104 if ((ret=sk_new_null()) == NULL)
105 {
106 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_MALLOC_FAILURE);
107 goto err;
108 }
109 }
110 else
111 ret=sk;
112
113 if ((xi=X509_INFO_new()) == NULL) goto err;
114 for (;;)
115 {
116 raw=0;
117 i=PEM_read_bio(bp,&name,&header,&data,&len);
118 if (i == 0)
119 {
120 error=ERR_GET_REASON(ERR_peek_error());
121 if (error == PEM_R_NO_START_LINE)
122 {
123 ERR_clear_error();
124 break;
125 }
126 goto err;
127 }
128start:
129 if ( (strcmp(name,PEM_STRING_X509) == 0) ||
130 (strcmp(name,PEM_STRING_X509_OLD) == 0))
131 {
132 d2i=(char *(*)())d2i_X509;
133 if (xi->x509 != NULL)
134 {
135 if (!sk_push(ret,(char *)xi)) goto err;
136 if ((xi=X509_INFO_new()) == NULL) goto err;
137 goto start;
138 }
139 pp=(char **)&(xi->x509);
140 }
141 else if (strcmp(name,PEM_STRING_X509_CRL) == 0)
142 {
143 d2i=(char *(*)())d2i_X509_CRL;
144 if (xi->crl != NULL)
145 {
146 if (!sk_push(ret,(char *)xi)) goto err;
147 if ((xi=X509_INFO_new()) == NULL) goto err;
148 goto start;
149 }
150 pp=(char **)&(xi->crl);
151 }
152 else
153#ifndef NO_RSA
154 if (strcmp(name,PEM_STRING_RSA) == 0)
155 {
156 d2i=(char *(*)())d2i_RSAPrivateKey;
157 if (xi->x_pkey != NULL)
158 {
159 if (!sk_push(ret,(char *)xi)) goto err;
160 if ((xi=X509_INFO_new()) == NULL) goto err;
161 goto start;
162 }
163
164 xi->enc_data=NULL;
165 xi->enc_len=0;
166
167 xi->x_pkey=X509_PKEY_new();
168 if ((xi->x_pkey->dec_pkey=EVP_PKEY_new()) == NULL)
169 goto err;
170 xi->x_pkey->dec_pkey->type=EVP_PKEY_RSA;
171 pp=(char **)&(xi->x_pkey->dec_pkey->pkey.rsa);
172 if ((int)strlen(header) > 10) /* assume encrypted */
173 raw=1;
174 }
175 else
176#endif
177#ifndef NO_DSA
178 if (strcmp(name,PEM_STRING_DSA) == 0)
179 {
180 d2i=(char *(*)())d2i_DSAPrivateKey;
181 if (xi->x_pkey != NULL)
182 {
183 if (!sk_push(ret,(char *)xi)) goto err;
184 if ((xi=X509_INFO_new()) == NULL) goto err;
185 goto start;
186 }
187
188 xi->enc_data=NULL;
189 xi->enc_len=0;
190
191 xi->x_pkey=X509_PKEY_new();
192 if ((xi->x_pkey->dec_pkey=EVP_PKEY_new()) == NULL)
193 goto err;
194 xi->x_pkey->dec_pkey->type=EVP_PKEY_DSA;
195 pp=(char **)&(xi->x_pkey->dec_pkey->pkey.dsa);
196 if ((int)strlen(header) > 10) /* assume encrypted */
197 raw=1;
198 }
199 else
200#endif
201 {
202 d2i=NULL;
203 pp=NULL;
204 }
205
206 if (d2i != NULL)
207 {
208 if (!raw)
209 {
210 EVP_CIPHER_INFO cipher;
211
212 if (!PEM_get_EVP_CIPHER_INFO(header,&cipher))
213 goto err;
214 if (!PEM_do_header(&cipher,data,&len,cb))
215 goto err;
216 p=data;
217 if (d2i(pp,&p,len) == NULL)
218 {
219 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_ASN1_LIB);
220 goto err;
221 }
222 }
223 else
224 { /* encrypted RSA data */
225 if (!PEM_get_EVP_CIPHER_INFO(header,
226 &xi->enc_cipher)) goto err;
227 xi->enc_data=(char *)data;
228 xi->enc_len=(int)len;
229 data=NULL;
230 }
231 }
232 else {
233 /* unknown */
234 }
235 if (name != NULL) Free(name);
236 if (header != NULL) Free(header);
237 if (data != NULL) Free(data);
238 name=NULL;
239 header=NULL;
240 data=NULL;
241 }
242
243 /* if the last one hasn't been pushed yet and there is anything
244 * in it then add it to the stack ...
245 */
246 if ((xi->x509 != NULL) || (xi->crl != NULL) ||
247 (xi->x_pkey != NULL) || (xi->enc_data != NULL))
248 {
249 if (!sk_push(ret,(char *)xi)) goto err;
250 xi=NULL;
251 }
252 ok=1;
253err:
254 if (xi != NULL) X509_INFO_free(xi);
255 if (!ok)
256 {
257 for (i=0; ((int)i)<sk_num(ret); i++)
258 {
259 xi=(X509_INFO *)sk_value(ret,i);
260 X509_INFO_free(xi);
261 }
262 if (ret != sk) sk_free(ret);
263 ret=NULL;
264 }
265
266 if (name != NULL) Free(name);
267 if (header != NULL) Free(header);
268 if (data != NULL) Free(data);
269 return(ret);
270 }
271
272
273/* A TJH addition */
274int PEM_X509_INFO_write_bio(bp,xi,enc,kstr,klen,cb)
275BIO *bp;
276X509_INFO *xi;
277EVP_CIPHER *enc;
278unsigned char *kstr;
279int klen;
280int (*cb)();
281 {
282 EVP_CIPHER_CTX ctx;
283 int i,ret=0;
284 unsigned char *data=NULL;
285 char *objstr=NULL;
286#define PEM_BUFSIZE 1024
287 char buf[PEM_BUFSIZE];
288 unsigned char *iv=NULL;
289
290 if (enc != NULL)
291 {
292 objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc));
293 if (objstr == NULL)
294 {
295 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
296 goto err;
297 }
298 }
299
300 /* now for the fun part ... if we have a private key then
301 * we have to be able to handle a not-yet-decrypted key
302 * being written out correctly ... if it is decrypted or
303 * it is non-encrypted then we use the base code
304 */
305 if (xi->x_pkey!=NULL)
306 {
307 if ( (xi->enc_data!=NULL) && (xi->enc_len>0) )
308 {
309 /* copy from wierdo names into more normal things */
310 iv=xi->enc_cipher.iv;
311 data=(unsigned char *)xi->enc_data;
312 i=xi->enc_len;
313
314 /* we take the encryption data from the
315 * internal stuff rather than what the
316 * user has passed us ... as we have to
317 * match exactly for some strange reason
318 */
319 objstr=OBJ_nid2sn(
320 EVP_CIPHER_nid(xi->enc_cipher.cipher));
321 if (objstr == NULL)
322 {
323 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
324 goto err;
325 }
326
327 /* create the right magic header stuff */
328 buf[0]='\0';
329 PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
330 PEM_dek_info(buf,objstr,8,(char *)iv);
331
332 /* use the normal code to write things out */
333 i=PEM_write_bio(bp,PEM_STRING_RSA,buf,data,i);
334 if (i <= 0) goto err;
335 }
336 else
337 {
338 /* Add DSA/DH */
339#ifndef NO_RSA
340 /* normal optionally encrypted stuff */
341 if (PEM_write_bio_RSAPrivateKey(bp,
342 xi->x_pkey->dec_pkey->pkey.rsa,
343 enc,kstr,klen,cb)<=0)
344 goto err;
345#endif
346 }
347 }
348
349 /* if we have a certificate then write it out now */
350 if ((xi->x509 != NULL) || (PEM_write_bio_X509(bp,xi->x509) <= 0))
351 goto err;
352
353 /* we are ignoring anything else that is loaded into the X509_INFO
354 * structure for the moment ... as I don't need it so I'm not
355 * coding it here and Eric can do it when this makes it into the
356 * base library --tjh
357 */
358
359 ret=1;
360
361err:
362 memset((char *)&ctx,0,sizeof(ctx));
363 memset(buf,0,PEM_BUFSIZE);
364 return(ret);
365 }
diff --git a/src/lib/libcrypto/pem/pem_lib.c b/src/lib/libcrypto/pem/pem_lib.c
new file mode 100644
index 0000000000..7a2c0ad83b
--- /dev/null
+++ b/src/lib/libcrypto/pem/pem_lib.c
@@ -0,0 +1,762 @@
1/* crypto/pem/pem_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "buffer.h"
62#include "objects.h"
63#include "evp.h"
64#include "rand.h"
65#include "x509.h"
66#include "pem.h"
67#ifndef NO_DES
68#include "des.h"
69#endif
70
71char *PEM_version="PEM part of SSLeay 0.9.0b 29-Jun-1998";
72
73#define MIN_LENGTH 4
74
75/* PEMerr(PEM_F_PEM_WRITE_BIO,ERR_R_MALLOC_FAILURE);
76 * PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
77 */
78
79#ifndef NOPROTO
80static int def_callback(char *buf, int num, int w);
81static int load_iv(unsigned char **fromp,unsigned char *to, int num);
82#else
83static int def_callback();
84static int load_iv();
85#endif
86
87static int def_callback(buf, num, w)
88char *buf;
89int num;
90int w;
91 {
92#ifdef NO_FP_API
93 /* We should not ever call the default callback routine from
94 * windows. */
95 PEMerr(PEM_F_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
96 return(-1);
97#else
98 int i,j;
99 char *prompt;
100
101 prompt=EVP_get_pw_prompt();
102 if (prompt == NULL)
103 prompt="Enter PEM pass phrase:";
104
105 for (;;)
106 {
107 i=EVP_read_pw_string(buf,num,prompt,w);
108 if (i != 0)
109 {
110 PEMerr(PEM_F_DEF_CALLBACK,PEM_R_PROBLEMS_GETTING_PASSWORD);
111 memset(buf,0,(unsigned int)num);
112 return(-1);
113 }
114 j=strlen(buf);
115 if (j < MIN_LENGTH)
116 {
117 fprintf(stderr,"phrase is too short, needs to be at least %d chars\n",MIN_LENGTH);
118 }
119 else
120 break;
121 }
122 return(j);
123#endif
124 }
125
126void PEM_proc_type(buf, type)
127char *buf;
128int type;
129 {
130 char *str;
131
132 if (type == PEM_TYPE_ENCRYPTED)
133 str="ENCRYPTED";
134 else if (type == PEM_TYPE_MIC_CLEAR)
135 str="MIC-CLEAR";
136 else if (type == PEM_TYPE_MIC_ONLY)
137 str="MIC-ONLY";
138 else
139 str="BAD-TYPE";
140
141 strcat(buf,"Proc-Type: 4,");
142 strcat(buf,str);
143 strcat(buf,"\n");
144 }
145
146void PEM_dek_info(buf, type, len, str)
147char *buf;
148char *type;
149int len;
150char *str;
151 {
152 static unsigned char map[17]="0123456789ABCDEF";
153 long i;
154 int j;
155
156 strcat(buf,"DEK-Info: ");
157 strcat(buf,type);
158 strcat(buf,",");
159 j=strlen(buf);
160 for (i=0; i<len; i++)
161 {
162 buf[j+i*2] =map[(str[i]>>4)&0x0f];
163 buf[j+i*2+1]=map[(str[i] )&0x0f];
164 }
165 buf[j+i*2]='\n';
166 buf[j+i*2+1]='\0';
167 }
168
169#ifndef NO_FP_API
170char *PEM_ASN1_read(d2i,name,fp, x, cb)
171char *(*d2i)();
172char *name;
173FILE *fp;
174char **x;
175int (*cb)();
176 {
177 BIO *b;
178 char *ret;
179
180 if ((b=BIO_new(BIO_s_file())) == NULL)
181 {
182 PEMerr(PEM_F_PEM_ASN1_READ,ERR_R_BUF_LIB);
183 return(0);
184 }
185 BIO_set_fp(b,fp,BIO_NOCLOSE);
186 ret=PEM_ASN1_read_bio(d2i,name,b,x,cb);
187 BIO_free(b);
188 return(ret);
189 }
190#endif
191
192char *PEM_ASN1_read_bio(d2i,name,bp, x, cb)
193char *(*d2i)();
194char *name;
195BIO *bp;
196char **x;
197int (*cb)();
198 {
199 EVP_CIPHER_INFO cipher;
200 char *nm=NULL,*header=NULL;
201 unsigned char *p=NULL,*data=NULL;
202 long len;
203 char *ret=NULL;
204
205 for (;;)
206 {
207 if (!PEM_read_bio(bp,&nm,&header,&data,&len)) return(NULL);
208 if ( (strcmp(nm,name) == 0) ||
209 ((strcmp(nm,PEM_STRING_RSA) == 0) &&
210 (strcmp(name,PEM_STRING_EVP_PKEY) == 0)) ||
211 ((strcmp(nm,PEM_STRING_DSA) == 0) &&
212 (strcmp(name,PEM_STRING_EVP_PKEY) == 0)) ||
213 ((strcmp(nm,PEM_STRING_X509_OLD) == 0) &&
214 (strcmp(name,PEM_STRING_X509) == 0)) ||
215 ((strcmp(nm,PEM_STRING_X509_REQ_OLD) == 0) &&
216 (strcmp(name,PEM_STRING_X509_REQ) == 0))
217 )
218 break;
219 Free(nm);
220 Free(header);
221 Free(data);
222 }
223 if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err;
224 if (!PEM_do_header(&cipher,data,&len,cb)) goto err;
225 p=data;
226 if (strcmp(name,PEM_STRING_EVP_PKEY) == 0)
227 {
228 if (strcmp(nm,PEM_STRING_RSA) == 0)
229 ret=d2i(EVP_PKEY_RSA,x,&p,len);
230 else if (strcmp(nm,PEM_STRING_DSA) == 0)
231 ret=d2i(EVP_PKEY_DSA,x,&p,len);
232 }
233 else
234 ret=d2i(x,&p,len);
235 if (ret == NULL)
236 PEMerr(PEM_F_PEM_ASN1_READ_BIO,ERR_R_ASN1_LIB);
237err:
238 Free(nm);
239 Free(header);
240 Free(data);
241 return(ret);
242 }
243
244#ifndef NO_FP_API
245int PEM_ASN1_write(i2d,name,fp, x, enc, kstr, klen, callback)
246int (*i2d)();
247char *name;
248FILE *fp;
249char *x;
250EVP_CIPHER *enc;
251unsigned char *kstr;
252int klen;
253int (*callback)();
254 {
255 BIO *b;
256 int ret;
257
258 if ((b=BIO_new(BIO_s_file())) == NULL)
259 {
260 PEMerr(PEM_F_PEM_ASN1_WRITE,ERR_R_BUF_LIB);
261 return(0);
262 }
263 BIO_set_fp(b,fp,BIO_NOCLOSE);
264 ret=PEM_ASN1_write_bio(i2d,name,b,x,enc,kstr,klen,callback);
265 BIO_free(b);
266 return(ret);
267 }
268#endif
269
270int PEM_ASN1_write_bio(i2d,name,bp, x, enc, kstr, klen, callback)
271int (*i2d)();
272char *name;
273BIO *bp;
274char *x;
275EVP_CIPHER *enc;
276unsigned char *kstr;
277int klen;
278int (*callback)();
279 {
280 EVP_CIPHER_CTX ctx;
281 int dsize=0,i,j,ret=0;
282 unsigned char *p,*data=NULL;
283 char *objstr=NULL;
284#define PEM_BUFSIZE 1024
285 char buf[PEM_BUFSIZE];
286 unsigned char key[EVP_MAX_KEY_LENGTH];
287 unsigned char iv[EVP_MAX_IV_LENGTH];
288
289 if (enc != NULL)
290 {
291 objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc));
292 if (objstr == NULL)
293 {
294 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
295 goto err;
296 }
297 }
298
299 if ((dsize=i2d(x,NULL)) < 0)
300 {
301 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE);
302 dsize=0;
303 goto err;
304 }
305 /* dzise + 8 bytes are needed */
306 data=(unsigned char *)Malloc((unsigned int)dsize+20);
307 if (data == NULL)
308 {
309 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE);
310 goto err;
311 }
312 p=data;
313 i=i2d(x,&p);
314
315 if (enc != NULL)
316 {
317 if (kstr == NULL)
318 {
319 if (callback == NULL)
320 klen=def_callback(buf,PEM_BUFSIZE,1);
321 else
322 klen=(*callback)(buf,PEM_BUFSIZE,1);
323 if (klen <= 0)
324 {
325 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_READ_KEY);
326 goto err;
327 }
328 kstr=(unsigned char *)buf;
329 }
330 RAND_seed(data,i);/* put in the RSA key. */
331 RAND_bytes(iv,8); /* Generate a salt */
332 /* The 'iv' is used as the iv and as a salt. It is
333 * NOT taken from the BytesToKey function */
334 EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL);
335
336 if (kstr == (unsigned char *)buf) memset(buf,0,PEM_BUFSIZE);
337
338 buf[0]='\0';
339 PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
340 PEM_dek_info(buf,objstr,8,(char *)iv);
341 /* k=strlen(buf); */
342
343 EVP_EncryptInit(&ctx,enc,key,iv);
344 EVP_EncryptUpdate(&ctx,data,&j,data,i);
345 EVP_EncryptFinal(&ctx,&(data[j]),&i);
346 i+=j;
347 ret=1;
348 }
349 else
350 {
351 ret=1;
352 buf[0]='\0';
353 }
354 i=PEM_write_bio(bp,name,buf,data,i);
355 if (i <= 0) ret=0;
356err:
357 memset(key,0,sizeof(key));
358 memset(iv,0,sizeof(iv));
359 memset((char *)&ctx,0,sizeof(ctx));
360 memset(buf,0,PEM_BUFSIZE);
361 memset(data,0,(unsigned int)dsize);
362 Free(data);
363 return(ret);
364 }
365
366int PEM_do_header(cipher, data, plen, callback)
367EVP_CIPHER_INFO *cipher;
368unsigned char *data;
369long *plen;
370int (*callback)();
371 {
372 int i,j,o,klen;
373 long len;
374 EVP_CIPHER_CTX ctx;
375 unsigned char key[EVP_MAX_KEY_LENGTH];
376 char buf[PEM_BUFSIZE];
377
378 len= *plen;
379
380 if (cipher->cipher == NULL) return(1);
381 if (callback == NULL)
382 klen=def_callback(buf,PEM_BUFSIZE,0);
383 else
384 klen=callback(buf,PEM_BUFSIZE,0);
385 if (klen <= 0)
386 {
387 PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_PASSWORD_READ);
388 return(0);
389 }
390 EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]),
391 (unsigned char *)buf,klen,1,key,NULL);
392
393 j=(int)len;
394 EVP_DecryptInit(&ctx,cipher->cipher,key,&(cipher->iv[0]));
395 EVP_DecryptUpdate(&ctx,data,&i,data,j);
396 o=EVP_DecryptFinal(&ctx,&(data[i]),&j);
397 EVP_CIPHER_CTX_cleanup(&ctx);
398 memset((char *)buf,0,sizeof(buf));
399 memset((char *)key,0,sizeof(key));
400 j+=i;
401 if (!o)
402 {
403 PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_DECRYPT);
404 return(0);
405 }
406 *plen=j;
407 return(1);
408 }
409
410int PEM_get_EVP_CIPHER_INFO(header,cipher)
411char *header;
412EVP_CIPHER_INFO *cipher;
413 {
414 int o;
415 EVP_CIPHER *enc=NULL;
416 char *p,c;
417
418 cipher->cipher=NULL;
419 if ((header == NULL) || (*header == '\0') || (*header == '\n'))
420 return(1);
421 if (strncmp(header,"Proc-Type: ",11) != 0)
422 { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_PROC_TYPE); return(0); }
423 header+=11;
424 if (*header != '4') return(0); header++;
425 if (*header != ',') return(0); header++;
426 if (strncmp(header,"ENCRYPTED",9) != 0)
427 { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_ENCRYPTED); return(0); }
428 for (; (*header != '\n') && (*header != '\0'); header++)
429 ;
430 if (*header == '\0')
431 { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_SHORT_HEADER); return(0); }
432 header++;
433 if (strncmp(header,"DEK-Info: ",10) != 0)
434 { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_DEK_INFO); return(0); }
435 header+=10;
436
437 p=header;
438 for (;;)
439 {
440 c= *header;
441 if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') ||
442 ((c >= '0') && (c <= '9'))))
443 break;
444 header++;
445 }
446 *header='\0';
447 o=OBJ_sn2nid(p);
448 cipher->cipher=enc=EVP_get_cipherbyname(p);
449 *header=c;
450 header++;
451
452 if (enc == NULL)
453 {
454 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_UNSUPPORTED_ENCRYPTION);
455 return(0);
456 }
457 if (!load_iv((unsigned char **)&header,&(cipher->iv[0]),8)) return(0);
458
459 return(1);
460 }
461
462static int load_iv(fromp,to,num)
463unsigned char **fromp,*to;
464int num;
465 {
466 int v,i;
467 unsigned char *from;
468
469 from= *fromp;
470 for (i=0; i<num; i++) to[i]=0;
471 num*=2;
472 for (i=0; i<num; i++)
473 {
474 if ((*from >= '0') && (*from <= '9'))
475 v= *from-'0';
476 else if ((*from >= 'A') && (*from <= 'F'))
477 v= *from-'A'+10;
478 else if ((*from >= 'a') && (*from <= 'f'))
479 v= *from-'a'+10;
480 else
481 {
482 PEMerr(PEM_F_LOAD_IV,PEM_R_BAD_IV_CHARS);
483 return(0);
484 }
485 from++;
486 to[i/2]|=v<<(long)((!(i&1))*4);
487 }
488
489 *fromp=from;
490 return(1);
491 }
492
493#ifndef NO_FP_API
494int PEM_write(fp, name, header, data,len)
495FILE *fp;
496char *name;
497char *header;
498unsigned char *data;
499long len;
500 {
501 BIO *b;
502 int ret;
503
504 if ((b=BIO_new(BIO_s_file())) == NULL)
505 {
506 PEMerr(PEM_F_PEM_WRITE,ERR_R_BUF_LIB);
507 return(0);
508 }
509 BIO_set_fp(b,fp,BIO_NOCLOSE);
510 ret=PEM_write_bio(b, name, header, data,len);
511 BIO_free(b);
512 return(ret);
513 }
514#endif
515
516int PEM_write_bio(bp, name, header, data,len)
517BIO *bp;
518char *name;
519char *header;
520unsigned char *data;
521long len;
522 {
523 int nlen,n,i,j,outl;
524 unsigned char *buf;
525 EVP_ENCODE_CTX ctx;
526 int reason=ERR_R_BUF_LIB;
527
528 EVP_EncodeInit(&ctx);
529 nlen=strlen(name);
530
531 if ( (BIO_write(bp,"-----BEGIN ",11) != 11) ||
532 (BIO_write(bp,name,nlen) != nlen) ||
533 (BIO_write(bp,"-----\n",6) != 6))
534 goto err;
535
536 i=strlen(header);
537 if (i > 0)
538 {
539 if ( (BIO_write(bp,header,i) != i) ||
540 (BIO_write(bp,"\n",1) != 1))
541 goto err;
542 }
543
544 buf=(unsigned char *)Malloc(PEM_BUFSIZE*8);
545 if (buf == NULL)
546 {
547 reason=ERR_R_MALLOC_FAILURE;
548 goto err;
549 }
550
551 i=j=0;
552 while (len > 0)
553 {
554 n=(int)((len>(PEM_BUFSIZE*5))?(PEM_BUFSIZE*5):len);
555 EVP_EncodeUpdate(&ctx,buf,&outl,&(data[j]),n);
556 if ((outl) && (BIO_write(bp,(char *)buf,outl) != outl))
557 goto err;
558 i+=outl;
559 len-=n;
560 j+=n;
561 }
562 EVP_EncodeFinal(&ctx,buf,&outl);
563 if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err;
564 Free(buf);
565 if ( (BIO_write(bp,"-----END ",9) != 9) ||
566 (BIO_write(bp,name,nlen) != nlen) ||
567 (BIO_write(bp,"-----\n",6) != 6))
568 goto err;
569 return(i+outl);
570err:
571 PEMerr(PEM_F_PEM_WRITE_BIO,reason);
572 return(0);
573 }
574
575#ifndef NO_FP_API
576int PEM_read(fp, name, header, data,len)
577FILE *fp;
578char **name;
579char **header;
580unsigned char **data;
581long *len;
582 {
583 BIO *b;
584 int ret;
585
586 if ((b=BIO_new(BIO_s_file())) == NULL)
587 {
588 PEMerr(PEM_F_PEM_READ,ERR_R_BUF_LIB);
589 return(0);
590 }
591 BIO_set_fp(b,fp,BIO_NOCLOSE);
592 ret=PEM_read_bio(b, name, header, data,len);
593 BIO_free(b);
594 return(ret);
595 }
596#endif
597
598int PEM_read_bio(bp, name, header, data, len)
599BIO *bp;
600char **name;
601char **header;
602unsigned char **data;
603long *len;
604 {
605 EVP_ENCODE_CTX ctx;
606 int end=0,i,k,bl=0,hl=0,nohead=0;
607 char buf[256];
608 BUF_MEM *nameB;
609 BUF_MEM *headerB;
610 BUF_MEM *dataB,*tmpB;
611
612 nameB=BUF_MEM_new();
613 headerB=BUF_MEM_new();
614 dataB=BUF_MEM_new();
615 if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL))
616 {
617 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
618 return(0);
619 }
620
621 buf[254]='\0';
622 for (;;)
623 {
624 i=BIO_gets(bp,buf,254);
625
626 if (i <= 0)
627 {
628 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_NO_START_LINE);
629 goto err;
630 }
631
632 while ((i >= 0) && (buf[i] <= ' ')) i--;
633 buf[++i]='\n'; buf[++i]='\0';
634
635 if (strncmp(buf,"-----BEGIN ",11) == 0)
636 {
637 i=strlen(&(buf[11]));
638
639 if (strncmp(&(buf[11+i-6]),"-----\n",6) != 0)
640 continue;
641 if (!BUF_MEM_grow(nameB,i+9))
642 {
643 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
644 goto err;
645 }
646 strncpy(nameB->data,&(buf[11]),(unsigned int)i-6);
647 nameB->data[i-6]='\0';
648 break;
649 }
650 }
651 hl=0;
652 if (!BUF_MEM_grow(headerB,256))
653 { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
654 headerB->data[0]='\0';
655 for (;;)
656 {
657 i=BIO_gets(bp,buf,254);
658 if (i <= 0) break;
659
660 while ((i >= 0) && (buf[i] <= ' ')) i--;
661 buf[++i]='\n'; buf[++i]='\0';
662
663 if (buf[0] == '\n') break;
664 if (!BUF_MEM_grow(headerB,hl+i+9))
665 { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
666 if (strncmp(buf,"-----END ",9) == 0)
667 {
668 nohead=1;
669 break;
670 }
671 strncpy(&(headerB->data[hl]),buf,(unsigned int)i);
672 headerB->data[hl+i]='\0';
673 hl+=i;
674 }
675
676 bl=0;
677 if (!BUF_MEM_grow(dataB,1024))
678 { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
679 dataB->data[0]='\0';
680 if (!nohead)
681 {
682 for (;;)
683 {
684 i=BIO_gets(bp,buf,254);
685 if (i <= 0) break;
686
687 while ((i >= 0) && (buf[i] <= ' ')) i--;
688 buf[++i]='\n'; buf[++i]='\0';
689
690 if (i != 65) end=1;
691 if (strncmp(buf,"-----END ",9) == 0)
692 break;
693 if (i > 65) break;
694 if (!BUF_MEM_grow(dataB,i+bl+9))
695 {
696 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
697 goto err;
698 }
699 strncpy(&(dataB->data[bl]),buf,(unsigned int)i);
700 dataB->data[bl+i]='\0';
701 bl+=i;
702 if (end)
703 {
704 buf[0]='\0';
705 i=BIO_gets(bp,buf,254);
706 if (i <= 0) break;
707
708 while ((i >= 0) && (buf[i] <= ' ')) i--;
709 buf[++i]='\n'; buf[++i]='\0';
710
711 break;
712 }
713 }
714 }
715 else
716 {
717 tmpB=headerB;
718 headerB=dataB;
719 dataB=tmpB;
720 bl=hl;
721 }
722 i=strlen(nameB->data);
723 if ( (strncmp(buf,"-----END ",9) != 0) ||
724 (strncmp(nameB->data,&(buf[9]),(unsigned int)i) != 0) ||
725 (strncmp(&(buf[9+i]),"-----\n",6) != 0))
726 {
727 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE);
728 goto err;
729 }
730
731 EVP_DecodeInit(&ctx);
732 i=EVP_DecodeUpdate(&ctx,
733 (unsigned char *)dataB->data,&bl,
734 (unsigned char *)dataB->data,bl);
735 if (i < 0)
736 {
737 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);
738 goto err;
739 }
740 i=EVP_DecodeFinal(&ctx,(unsigned char *)&(dataB->data[bl]),&k);
741 if (i < 0)
742 {
743 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);
744 goto err;
745 }
746 bl+=k;
747
748 if (bl == 0) goto err;
749 *name=nameB->data;
750 *header=headerB->data;
751 *data=(unsigned char *)dataB->data;
752 *len=bl;
753 Free(nameB);
754 Free(headerB);
755 Free(dataB);
756 return(1);
757err:
758 BUF_MEM_free(nameB);
759 BUF_MEM_free(headerB);
760 BUF_MEM_free(dataB);
761 return(0);
762 }
diff --git a/src/lib/libcrypto/pem/pem_seal.c b/src/lib/libcrypto/pem/pem_seal.c
new file mode 100644
index 0000000000..b4b36df453
--- /dev/null
+++ b/src/lib/libcrypto/pem/pem_seal.c
@@ -0,0 +1,191 @@
1/* crypto/pem/pem_seal.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "rand.h"
63#include "objects.h"
64#include "x509.h"
65#include "pem.h"
66
67int PEM_SealInit(ctx,type,md_type,ek,ekl,iv,pubk,npubk)
68PEM_ENCODE_SEAL_CTX *ctx;
69EVP_CIPHER *type;
70EVP_MD *md_type;
71unsigned char **ek;
72int *ekl;
73unsigned char *iv;
74EVP_PKEY **pubk;
75int npubk;
76 {
77 unsigned char key[EVP_MAX_KEY_LENGTH];
78 int ret= -1;
79 int i,j,max=0;
80 char *s=NULL;
81
82 for (i=0; i<npubk; i++)
83 {
84 if (pubk[i]->type != EVP_PKEY_RSA)
85 {
86 PEMerr(PEM_F_PEM_SEALINIT,PEM_R_PUBLIC_KEY_NO_RSA);
87 goto err;
88 }
89 j=RSA_size(pubk[i]->pkey.rsa);
90 if (j > max) max=j;
91 }
92 s=(char *)Malloc(max*2);
93 if (s == NULL)
94 {
95 PEMerr(PEM_F_PEM_SEALINIT,ERR_R_MALLOC_FAILURE);
96 goto err;
97 }
98
99 EVP_EncodeInit(&(ctx->encode));
100 EVP_SignInit(&(ctx->md),md_type);
101
102 ret=EVP_SealInit(&(ctx->cipher),type,ek,ekl,iv,pubk,npubk);
103 if (!ret) goto err;
104
105 /* base64 encode the keys */
106 for (i=0; i<npubk; i++)
107 {
108 j=EVP_EncodeBlock((unsigned char *)s,ek[i],
109 RSA_size(pubk[i]->pkey.rsa));
110 ekl[i]=j;
111 memcpy(ek[i],s,j+1);
112 }
113
114 ret=npubk;
115err:
116 if (s != NULL) Free(s);
117 memset(key,0,EVP_MAX_KEY_LENGTH);
118 return(ret);
119 }
120
121void PEM_SealUpdate(ctx,out,outl,in,inl)
122PEM_ENCODE_SEAL_CTX *ctx;
123unsigned char *out;
124int *outl;
125unsigned char *in;
126int inl;
127 {
128 unsigned char buffer[1600];
129 int i,j;
130
131 *outl=0;
132 EVP_SignUpdate(&(ctx->md),in,inl);
133 for (;;)
134 {
135 if (inl <= 0) break;
136 if (inl > 1200)
137 i=1200;
138 else
139 i=inl;
140 EVP_EncryptUpdate(&(ctx->cipher),buffer,&j,in,i);
141 EVP_EncodeUpdate(&(ctx->encode),out,&j,buffer,j);
142 *outl+=j;
143 out+=j;
144 in+=i;
145 inl-=i;
146 }
147 }
148
149int PEM_SealFinal(ctx,sig,sigl,out,outl,priv)
150PEM_ENCODE_SEAL_CTX *ctx;
151unsigned char *sig;
152int *sigl;
153unsigned char *out;
154int *outl;
155EVP_PKEY *priv;
156 {
157 unsigned char *s=NULL;
158 int ret=0,j;
159 unsigned int i;
160
161 if (priv->type != EVP_PKEY_RSA)
162 {
163 PEMerr(PEM_F_PEM_SEALFINAL,PEM_R_PUBLIC_KEY_NO_RSA);
164 goto err;
165 }
166 i=RSA_size(priv->pkey.rsa);
167 if (i < 100) i=100;
168 s=(unsigned char *)Malloc(i*2);
169 if (s == NULL)
170 {
171 PEMerr(PEM_F_PEM_SEALFINAL,ERR_R_MALLOC_FAILURE);
172 goto err;
173 }
174
175 EVP_EncryptFinal(&(ctx->cipher),s,(int *)&i);
176 EVP_EncodeUpdate(&(ctx->encode),out,&j,s,i);
177 *outl=j;
178 out+=j;
179 EVP_EncodeFinal(&(ctx->encode),out,&j);
180 *outl+=j;
181
182 if (!EVP_SignFinal(&(ctx->md),s,&i,priv)) goto err;
183 *sigl=EVP_EncodeBlock(sig,s,i);
184
185 ret=1;
186err:
187 memset((char *)&(ctx->md),0,sizeof(ctx->md));
188 memset((char *)&(ctx->cipher),0,sizeof(ctx->cipher));
189 if (s != NULL) Free(s);
190 return(ret);
191 }
diff --git a/src/lib/libcrypto/pem/pem_sign.c b/src/lib/libcrypto/pem/pem_sign.c
new file mode 100644
index 0000000000..d56f9f9e14
--- /dev/null
+++ b/src/lib/libcrypto/pem/pem_sign.c
@@ -0,0 +1,109 @@
1/* crypto/pem/pem_sign.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "rand.h"
62#include "evp.h"
63#include "objects.h"
64#include "x509.h"
65#include "pem.h"
66
67void PEM_SignInit(ctx,type)
68EVP_MD_CTX *ctx;
69EVP_MD *type;
70 {
71 EVP_DigestInit(ctx,type);
72 }
73
74void PEM_SignUpdate(ctx,data,count)
75EVP_MD_CTX *ctx;
76unsigned char *data;
77unsigned int count;
78 {
79 EVP_DigestUpdate(ctx,data,count);
80 }
81
82int PEM_SignFinal(ctx,sigret,siglen,pkey)
83EVP_MD_CTX *ctx;
84unsigned char *sigret;
85unsigned int *siglen;
86EVP_PKEY *pkey;
87 {
88 unsigned char *m;
89 int i,ret=0;
90 unsigned int m_len;
91
92 m=(unsigned char *)Malloc(EVP_PKEY_size(pkey)+2);
93 if (m == NULL)
94 {
95 PEMerr(PEM_F_PEM_SIGNFINAL,ERR_R_MALLOC_FAILURE);
96 goto err;
97 }
98
99 if (EVP_SignFinal(ctx,m,&m_len,pkey) <= 0) goto err;
100
101 i=EVP_EncodeBlock(sigret,m,m_len);
102 *siglen=i;
103 ret=1;
104err:
105 /* ctx has been zeroed by EVP_SignFinal() */
106 if (m != NULL) Free(m);
107 return(ret);
108 }
109
diff --git a/src/lib/libcrypto/pem/pkcs7.lis b/src/lib/libcrypto/pem/pkcs7.lis
new file mode 100644
index 0000000000..be90c5d87f
--- /dev/null
+++ b/src/lib/libcrypto/pem/pkcs7.lis
@@ -0,0 +1,22 @@
121 0:d=0 hl=2 l= 0 cons: univ: SEQUENCE
2 00 2:d=0 hl=2 l= 9 prim: univ: OBJECT_IDENTIFIER :pkcs-7-signedData
3 21 13:d=0 hl=2 l= 0 cons: cont: 00 # explicit tag
4 21 15:d=0 hl=2 l= 0 cons: univ: SEQUENCE
5 00 17:d=0 hl=2 l= 1 prim: univ: INTEGER # version
6 20 20:d=0 hl=2 l= 0 cons: univ: SET
7 21 22:d=0 hl=2 l= 0 cons: univ: SEQUENCE
8 00 24:d=0 hl=2 l= 9 prim: univ: OBJECT_IDENTIFIER :pkcs-7-data
9 00 35:d=0 hl=2 l= 0 prim: univ: EOC
10 21 37:d=0 hl=2 l= 0 cons: cont: 00 # cert tag
11 20 39:d=0 hl=4 l=545 cons: univ: SEQUENCE
12 20 588:d=0 hl=4 l=524 cons: univ: SEQUENCE
13 00 1116:d=0 hl=2 l= 0 prim: univ: EOC
14 21 1118:d=0 hl=2 l= 0 cons: cont: 01 # crl tag
15 20 1120:d=0 hl=4 l=653 cons: univ: SEQUENCE
16 20 1777:d=0 hl=4 l=285 cons: univ: SEQUENCE
17 00 2066:d=0 hl=2 l= 0 prim: univ: EOC
18 21 2068:d=0 hl=2 l= 0 cons: univ: SET # signers
19 00 2070:d=0 hl=2 l= 0 prim: univ: EOC
20 00 2072:d=0 hl=2 l= 0 prim: univ: EOC
21 00 2074:d=0 hl=2 l= 0 prim: univ: EOC
2200 2076:d=0 hl=2 l= 0 prim: univ: EOC
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl
new file mode 100644
index 0000000000..2789305790
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/cbc.pl
@@ -0,0 +1,342 @@
1#!/usr/bin/perl
2
3# void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
4# des_cblock (*input);
5# des_cblock (*output);
6# long length;
7# des_key_schedule schedule;
8# des_cblock (*ivec);
9# int enc;
10#
11# calls
12# des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
13#
14
15#&cbc("des_ncbc_encrypt","des_encrypt",0);
16#&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",
17# 1,4,5,3,5,-1);
18#&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",
19# 0,4,5,3,5,-1);
20#&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",
21# 0,6,7,3,4,5);
22#
23# When doing a cipher that needs bigendian order,
24# for encrypt, the iv is kept in bigendian form,
25# while for decrypt, it is kept in little endian.
26sub cbc
27 {
28 local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_;
29 # name is the function name
30 # enc_func and dec_func and the functions to call for encrypt/decrypt
31 # swap is true if byte order needs to be reversed
32 # iv_off is parameter number for the iv
33 # enc_off is parameter number for the encrypt/decrypt flag
34 # p1,p2,p3 are the offsets for parameters to be passed to the
35 # underlying calls.
36
37 &function_begin_B($name,"");
38 &comment("");
39
40 $in="esi";
41 $out="edi";
42 $count="ebp";
43
44 &push("ebp");
45 &push("ebx");
46 &push("esi");
47 &push("edi");
48
49 $data_off=4;
50 $data_off+=4 if ($p1 > 0);
51 $data_off+=4 if ($p2 > 0);
52 $data_off+=4 if ($p3 > 0);
53
54 &mov($count, &wparam(2)); # length
55
56 &comment("getting iv ptr from parameter $iv_off");
57 &mov("ebx", &wparam($iv_off)); # Get iv ptr
58
59 &mov($in, &DWP(0,"ebx","",0));# iv[0]
60 &mov($out, &DWP(4,"ebx","",0));# iv[1]
61
62 &push($out);
63 &push($in);
64 &push($out); # used in decrypt for iv[1]
65 &push($in); # used in decrypt for iv[0]
66
67 &mov("ebx", "esp"); # This is the address of tin[2]
68
69 &mov($in, &wparam(0)); # in
70 &mov($out, &wparam(1)); # out
71
72 # We have loaded them all, how lets push things
73 &comment("getting encrypt flag from parameter $enc_off");
74 &mov("ecx", &wparam($enc_off)); # Get enc flag
75 if ($p3 > 0)
76 {
77 &comment("get and push parameter $p3");
78 if ($enc_off != $p3)
79 { &mov("eax", &wparam($p3)); &push("eax"); }
80 else { &push("ecx"); }
81 }
82 if ($p2 > 0)
83 {
84 &comment("get and push parameter $p2");
85 if ($enc_off != $p2)
86 { &mov("eax", &wparam($p2)); &push("eax"); }
87 else { &push("ecx"); }
88 }
89 if ($p1 > 0)
90 {
91 &comment("get and push parameter $p1");
92 if ($enc_off != $p1)
93 { &mov("eax", &wparam($p1)); &push("eax"); }
94 else { &push("ecx"); }
95 }
96 &push("ebx"); # push data/iv
97
98 &cmp("ecx",0);
99 &jz(&label("decrypt"));
100
101 &and($count,0xfffffff8);
102 &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0]
103 &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1]
104
105 &jz(&label("encrypt_finish"));
106
107 #############################################################
108
109 &set_label("encrypt_loop");
110 # encrypt start
111 # "eax" and "ebx" hold iv (or the last cipher text)
112
113 &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes
114 &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes
115
116 &xor("eax", "ecx");
117 &xor("ebx", "edx");
118
119 &bswap("eax") if $swap;
120 &bswap("ebx") if $swap;
121
122 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
123 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
124
125 &call($enc_func);
126
127 &mov("eax", &DWP($data_off,"esp","",0));
128 &mov("ebx", &DWP($data_off+4,"esp","",0));
129
130 &bswap("eax") if $swap;
131 &bswap("ebx") if $swap;
132
133 &mov(&DWP(0,$out,"",0),"eax");
134 &mov(&DWP(4,$out,"",0),"ebx");
135
136 # eax and ebx are the next iv.
137
138 &add($in, 8);
139 &add($out, 8);
140
141 &sub($count, 8);
142 &jnz(&label("encrypt_loop"));
143
144###################################################################3
145 &set_label("encrypt_finish");
146 &mov($count, &wparam(2)); # length
147 &and($count, 7);
148 &jz(&label("finish"));
149 &xor("ecx","ecx");
150 &xor("edx","edx");
151 &mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4));
152 &jmp_ptr($count);
153
154&set_label("ej7");
155 &xor("edx", "edx") if $ppro; # ppro friendly
156 &movb(&HB("edx"), &BP(6,$in,"",0));
157 &shl("edx",8);
158&set_label("ej6");
159 &movb(&HB("edx"), &BP(5,$in,"",0));
160&set_label("ej5");
161 &movb(&LB("edx"), &BP(4,$in,"",0));
162&set_label("ej4");
163 &mov("ecx", &DWP(0,$in,"",0));
164 &jmp(&label("ejend"));
165&set_label("ej3");
166 &movb(&HB("ecx"), &BP(2,$in,"",0));
167 &xor("ecx", "ecx") if $ppro; # ppro friendly
168 &shl("ecx",8);
169&set_label("ej2");
170 &movb(&HB("ecx"), &BP(1,$in,"",0));
171&set_label("ej1");
172 &movb(&LB("ecx"), &BP(0,$in,"",0));
173&set_label("ejend");
174
175 &xor("eax", "ecx");
176 &xor("ebx", "edx");
177
178 &bswap("eax") if $swap;
179 &bswap("ebx") if $swap;
180
181 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
182 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
183
184 &call($enc_func);
185
186 &mov("eax", &DWP($data_off,"esp","",0));
187 &mov("ebx", &DWP($data_off+4,"esp","",0));
188
189 &bswap("eax") if $swap;
190 &bswap("ebx") if $swap;
191
192 &mov(&DWP(0,$out,"",0),"eax");
193 &mov(&DWP(4,$out,"",0),"ebx");
194
195 &jmp(&label("finish"));
196
197 #############################################################
198 #############################################################
199 &set_label("decrypt",1);
200 # decrypt start
201 &and($count,0xfffffff8);
202 # The next 2 instructions are only for if the jz is taken
203 &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0]
204 &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1]
205 &jz(&label("decrypt_finish"));
206
207 &set_label("decrypt_loop");
208 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
209 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
210
211 &bswap("eax") if $swap;
212 &bswap("ebx") if $swap;
213
214 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
215 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
216
217 &call($dec_func);
218
219 &mov("eax", &DWP($data_off,"esp","",0)); # get return
220 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
221
222 &bswap("eax") if $swap;
223 &bswap("ebx") if $swap;
224
225 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
226 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
227
228 &xor("ecx", "eax");
229 &xor("edx", "ebx");
230
231 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
232 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
233
234 &mov(&DWP(0,$out,"",0),"ecx");
235 &mov(&DWP(4,$out,"",0),"edx");
236
237 &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv
238 &mov(&DWP($data_off+12,"esp","",0), "ebx"); #
239
240 &add($in, 8);
241 &add($out, 8);
242
243 &sub($count, 8);
244 &jnz(&label("decrypt_loop"));
245############################ ENDIT #######################3
246 &set_label("decrypt_finish");
247 &mov($count, &wparam(2)); # length
248 &and($count, 7);
249 &jz(&label("finish"));
250
251 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
252 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
253
254 &bswap("eax") if $swap;
255 &bswap("ebx") if $swap;
256
257 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
258 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
259
260 &call($dec_func);
261
262 &mov("eax", &DWP($data_off,"esp","",0)); # get return
263 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
264
265 &bswap("eax") if $swap;
266 &bswap("ebx") if $swap;
267
268 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
269 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
270
271 &xor("ecx", "eax");
272 &xor("edx", "ebx");
273
274 # this is for when we exit
275 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
276 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
277
278&set_label("dj7");
279 &rotr("edx", 16);
280 &movb(&BP(6,$out,"",0), &LB("edx"));
281 &shr("edx",16);
282&set_label("dj6");
283 &movb(&BP(5,$out,"",0), &HB("edx"));
284&set_label("dj5");
285 &movb(&BP(4,$out,"",0), &LB("edx"));
286&set_label("dj4");
287 &mov(&DWP(0,$out,"",0), "ecx");
288 &jmp(&label("djend"));
289&set_label("dj3");
290 &rotr("ecx", 16);
291 &movb(&BP(2,$out,"",0), &LB("ecx"));
292 &shl("ecx",16);
293&set_label("dj2");
294 &movb(&BP(1,$in,"",0), &HB("ecx"));
295&set_label("dj1");
296 &movb(&BP(0,$in,"",0), &LB("ecx"));
297&set_label("djend");
298
299 # final iv is still in eax:ebx
300 &jmp(&label("finish"));
301
302
303############################ FINISH #######################3
304 &set_label("finish",1);
305 &mov("ecx", &wparam($iv_off)); # Get iv ptr
306
307 #################################################
308 $total=16+4;
309 $total+=4 if ($p1 > 0);
310 $total+=4 if ($p2 > 0);
311 $total+=4 if ($p3 > 0);
312 &add("esp",$total);
313
314 &mov(&DWP(0,"ecx","",0), "eax"); # save iv
315 &mov(&DWP(4,"ecx","",0), "ebx"); # save iv
316
317 &function_end_A($name);
318
319 &set_label("cbc_enc_jmp_table",1);
320 &data_word("0");
321 &data_word(&label("ej1"));
322 &data_word(&label("ej2"));
323 &data_word(&label("ej3"));
324 &data_word(&label("ej4"));
325 &data_word(&label("ej5"));
326 &data_word(&label("ej6"));
327 &data_word(&label("ej7"));
328 &set_label("cbc_dec_jmp_table",1);
329 &data_word("0");
330 &data_word(&label("dj1"));
331 &data_word(&label("dj2"));
332 &data_word(&label("dj3"));
333 &data_word(&label("dj4"));
334 &data_word(&label("dj5"));
335 &data_word(&label("dj6"));
336 &data_word(&label("dj7"));
337
338 &function_end_B($name);
339
340 }
341
3421;
diff --git a/src/lib/libcrypto/perlasm/readme b/src/lib/libcrypto/perlasm/readme
new file mode 100644
index 0000000000..f02bbee75a
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/readme
@@ -0,0 +1,124 @@
1The perl scripts in this directory are my 'hack' to generate
2multiple different assembler formats via the one origional script.
3
4The way to use this library is to start with adding the path to this directory
5and then include it.
6
7push(@INC,"perlasm","../../perlasm");
8require "x86asm.pl";
9
10The first thing we do is setup the file and type of assember
11
12&asm_init($ARGV[0],$0);
13
14The first argument is the 'type'. Currently
15'cpp', 'sol', 'a.out', 'elf' or 'win32'.
16Argument 2 is the file name.
17
18The reciprocal function is
19&asm_finish() which should be called at the end.
20
21There are 2 main 'packages'. x86ms.pl, which is the microsoft assembler,
22and x86unix.pl which is the unix (gas) version.
23
24Functions of interest are:
25&external_label("des_SPtrans"); declare and external variable
26&LB(reg); Low byte for a register
27&HB(reg); High byte for a register
28&BP(off,base,index,scale) Byte pointer addressing
29&DWP(off,base,index,scale) Word pointer addressing
30&stack_push(num) Basically a 'sub esp, num*4' with extra
31&stack_pop(num) inverse of stack_push
32&function_begin(name,extra) Start a function with pushing of
33 edi, esi, ebx and ebp. extra is extra win32
34 external info that may be required.
35&function_begin_B(name,extra) Same as norma function_begin but no pushing.
36&function_end(name) Call at end of function.
37&function_end_A(name) Standard pop and ret, for use inside functions
38&function_end_B(name) Call at end but with poping or 'ret'.
39&swtmp(num) Address on stack temp word.
40&wparam(num) Parameter number num, that was push
41 in C convention. This all works over pushes
42 and pops.
43&comment("hello there") Put in a comment.
44&label("loop") Refer to a label, normally a jmp target.
45&set_label("loop") Set a label at this point.
46&data_word(word) Put in a word of data.
47
48So how does this all hold together? Given
49
50int calc(int len, int *data)
51 {
52 int i,j=0;
53
54 for (i=0; i<len; i++)
55 {
56 j+=other(data[i]);
57 }
58 }
59
60So a very simple version of this function could be coded as
61
62 push(@INC,"perlasm","../../perlasm");
63 require "x86asm.pl";
64
65 &asm_init($ARGV[0],"cacl.pl");
66
67 &external_label("other");
68
69 $tmp1= "eax";
70 $j= "edi";
71 $data= "esi";
72 $i= "ebp";
73
74 &comment("a simple function");
75 &function_begin("calc");
76 &mov( $data, &wparam(1)); # data
77 &xor( $j, $j);
78 &xor( $i, $i);
79
80 &set_label("loop");
81 &cmp( $i, &wparam(0));
82 &jge( &label("end"));
83
84 &mov( $tmp1, &DWP(0,$data,$i,4));
85 &push( $tmp1);
86 &call( "other");
87 &add( $j, "eax");
88 &pop( $tmp1);
89 &inc( $i);
90 &jmp( &label("loop"));
91
92 &set_label("end");
93 &mov( "eax", $j);
94
95 &function_end("calc");
96
97 &asm_finish();
98
99The above example is very very unoptimised but gives an idea of how
100things work.
101
102There is also a cbc mode function generator in cbc.pl
103
104&cbc( $name,
105 $encrypt_function_name,
106 $decrypt_function_name,
107 $true_if_byte_swap_needed,
108 $parameter_number_for_iv,
109 $parameter_number_for_encrypt_flag,
110 $first_parameter_to_pass,
111 $second_parameter_to_pass,
112 $third_parameter_to_pass);
113
114So for example, given
115void BF_encrypt(BF_LONG *data,BF_KEY *key);
116void BF_decrypt(BF_LONG *data,BF_KEY *key);
117void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length,
118 BF_KEY *ks, unsigned char *iv, int enc);
119
120&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",1,4,5,3,-1,-1);
121
122&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
123&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
124
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl
new file mode 100644
index 0000000000..6a9156ae9a
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86asm.pl
@@ -0,0 +1,113 @@
1#!/usr/bin/perl
2
3# require 'x86asm.pl';
4# &asm_init("cpp","des-586.pl");
5# XXX
6# XXX
7# main'asm_finish
8
9sub main'asm_finish
10 {
11 &file_end();
12 &asm_finish_cpp() if $cpp;
13 print &asm_get_output();
14 }
15
16sub main'asm_init
17 {
18 ($type,$fn)=@_;
19 $filename=$fn;
20
21 $cpp=$sol=$aout=$win32=0;
22 if ( ($type eq "elf"))
23 { require "x86unix.pl"; }
24 elsif ( ($type eq "a.out"))
25 { $aout=1; require "x86unix.pl"; }
26 elsif ( ($type eq "sol"))
27 { $sol=1; require "x86unix.pl"; }
28 elsif ( ($type eq "cpp"))
29 { $cpp=1; require "x86unix.pl"; }
30 elsif ( ($type eq "win32"))
31 { $win32=1; require "x86ms.pl"; }
32 else
33 {
34 print STDERR <<"EOF";
35Pick one target type from
36 elf - linux, FreeBSD etc
37 a.out - old linux
38 sol - x86 solaris
39 cpp - format so x86unix.cpp can be used
40 win32 - Windows 95/Windows NT
41EOF
42 exit(1);
43 }
44
45 &asm_init_output();
46
47&comment("Don't even think of reading this code");
48&comment("It was automatically generated by $filename");
49&comment("Which is a perl program used to generate the x86 assember for");
50&comment("any of elf, a.out, BSDI,Win32, or Solaris");
51&comment("eric <eay\@cryptsoft.com>");
52&comment("");
53
54 $filename =~ s/\.pl$//;
55 &file($filename);
56 }
57
58sub asm_finish_cpp
59 {
60 return unless $cpp;
61
62 local($tmp,$i);
63 foreach $i (&get_labels())
64 {
65 $tmp.="#define $i _$i\n";
66 }
67 print <<"EOF";
68/* Run the C pre-processor over this file with one of the following defined
69 * ELF - elf object files,
70 * OUT - a.out object files,
71 * BSDI - BSDI style a.out object files
72 * SOL - Solaris style elf
73 */
74
75#define TYPE(a,b) .type a,b
76#define SIZE(a,b) .size a,b
77
78#if defined(OUT) || defined(BSDI)
79$tmp
80#endif
81
82#ifdef OUT
83#define OK 1
84#define ALIGN 4
85#endif
86
87#ifdef BSDI
88#define OK 1
89#define ALIGN 4
90#undef SIZE
91#undef TYPE
92#define SIZE(a,b)
93#define TYPE(a,b)
94#endif
95
96#if defined(ELF) || defined(SOL)
97#define OK 1
98#define ALIGN 16
99#endif
100
101#ifndef OK
102You need to define one of
103ELF - elf systems - linux-elf, NetBSD and DG-UX
104OUT - a.out systems - linux-a.out and FreeBSD
105SOL - solaris systems, which are elf with strange comment lines
106BSDI - a.out with a very primative version of as.
107#endif
108
109/* Let the Assembler begin :-) */
110EOF
111 }
112
1131;
diff --git a/src/lib/libcrypto/pkcs7/pk7_doit.c b/src/lib/libcrypto/pkcs7/pk7_doit.c
new file mode 100644
index 0000000000..b5689b3fe4
--- /dev/null
+++ b/src/lib/libcrypto/pkcs7/pk7_doit.c
@@ -0,0 +1,408 @@
1/* crypto/pkcs7/pk7_doit.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "rand.h"
62#include "objects.h"
63#include "x509.h"
64
65BIO *PKCS7_dataInit(p7,bio)
66PKCS7 *p7;
67BIO *bio;
68 {
69 int i,j;
70 BIO *out=NULL,*btmp;
71 X509_ALGOR *xa;
72 EVP_MD *evp_md;
73 EVP_CIPHER *evp_cipher=NULL;
74 STACK *md_sk=NULL,*rsk=NULL;
75 X509_ALGOR *xalg=NULL;
76 PKCS7_RECIP_INFO *ri=NULL;
77 EVP_PKEY *pkey;
78
79 i=OBJ_obj2nid(p7->type);
80 p7->state=PKCS7_S_HEADER;
81
82 switch (i)
83 {
84 case NID_pkcs7_signed:
85 md_sk=p7->d.sign->md_algs;
86 break;
87 case NID_pkcs7_signedAndEnveloped:
88 rsk=p7->d.signed_and_enveloped->recipientinfo;
89 md_sk=p7->d.signed_and_enveloped->md_algs;
90 evp_cipher=EVP_get_cipherbyname(OBJ_nid2sn(OBJ_obj2nid(p7->d.signed_and_enveloped->enc_data->algorithm->algorithm)));
91 if (evp_cipher == NULL)
92 {
93 PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
94 goto err;
95 }
96 xalg=p7->d.signed_and_enveloped->enc_data->algorithm;
97 break;
98 default:
99 PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
100 goto err;
101 }
102
103 if (md_sk != NULL)
104 {
105 for (i=0; i<sk_num(md_sk); i++)
106 {
107 xa=(X509_ALGOR *)sk_value(md_sk,i);
108 if ((btmp=BIO_new(BIO_f_md())) == NULL) goto err;
109
110 j=OBJ_obj2nid(xa->algorithm);
111 evp_md=EVP_get_digestbyname(OBJ_nid2sn(j));
112 if (evp_md == NULL)
113 {
114 PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNKNOWN_DIGEST_TYPE);
115 goto err;
116 }
117
118 BIO_set_md(btmp,evp_md);
119 if (out == NULL)
120 out=btmp;
121 else
122 BIO_push(out,btmp);
123 }
124 }
125
126 if (evp_cipher != NULL)
127 {
128 unsigned char key[EVP_MAX_KEY_LENGTH];
129 unsigned char iv[EVP_MAX_IV_LENGTH];
130 int keylen,ivlen;
131 int jj,max;
132 unsigned char *tmp;
133
134 if ((btmp=BIO_new(BIO_f_cipher())) == NULL) goto err;
135 keylen=EVP_CIPHER_key_length(evp_cipher);
136 ivlen=EVP_CIPHER_iv_length(evp_cipher);
137
138 if (ivlen > 0)
139 {
140 ASN1_OCTET_STRING *os;
141
142 RAND_bytes(iv,ivlen);
143 os=ASN1_OCTET_STRING_new();
144 ASN1_OCTET_STRING_set(os,iv,ivlen);
145 /* ASN1_TYPE_set(xalg->parameter,V_ASN1_OCTET_STRING,
146 (char *)os);
147 */ }
148 RAND_bytes(key,keylen);
149
150 /* Lets do the pub key stuff :-) */
151 max=0;
152 for (i=0; i<sk_num(rsk); i++)
153 {
154 ri=(PKCS7_RECIP_INFO *)sk_value(rsk,i);
155 if (ri->cert == NULL) abort();
156 pkey=X509_get_pubkey(ri->cert);
157 jj=EVP_PKEY_size(pkey);
158 if (max < jj) max=jj;
159 }
160 if ((tmp=(unsigned char *)Malloc(max)) == NULL) abort();
161 for (i=0; i<sk_num(rsk); i++)
162 {
163 ri=(PKCS7_RECIP_INFO *)sk_value(rsk,i);
164 pkey=X509_get_pubkey(ri->cert);
165 jj=EVP_PKEY_encrypt(tmp,key,keylen,pkey);
166 if (jj <= 0) abort();
167 ASN1_OCTET_STRING_set(ri->enc_key,tmp,jj);
168 }
169
170 BIO_set_cipher(btmp,evp_cipher,key,iv,1);
171
172 if (out == NULL)
173 out=btmp;
174 else
175 BIO_push(out,btmp);
176 }
177
178 if (bio == NULL) /* ??????????? */
179 {
180 if (p7->detached)
181 bio=BIO_new(BIO_s_null());
182 else
183 {
184 bio=BIO_new(BIO_s_mem());
185 if (PKCS7_type_is_signed(p7) &&
186 PKCS7_type_is_data(p7->d.sign->contents))
187 {
188 ASN1_OCTET_STRING *os;
189
190 os=p7->d.sign->contents->d.data;
191 if (os->length > 0)
192 BIO_write(bio,(char *)os->data,
193 os->length);
194 }
195 }
196 }
197 BIO_push(out,bio);
198 return(out);
199err:
200 return(NULL);
201 }
202
203int PKCS7_dataSign(p7,bio)
204PKCS7 *p7;
205BIO *bio;
206 {
207 int ret=0;
208 int i,j;
209 BIO *btmp;
210 BUF_MEM *buf_mem=NULL;
211 BUF_MEM *buf=NULL;
212 PKCS7_SIGNER_INFO *si;
213 EVP_MD_CTX *mdc,ctx_tmp;
214 STACK *sk,*si_sk=NULL;
215 unsigned char *p,*pp=NULL;
216 int x;
217 ASN1_OCTET_STRING *os=NULL;
218
219 i=OBJ_obj2nid(p7->type);
220 p7->state=PKCS7_S_HEADER;
221
222 switch (i)
223 {
224 case NID_pkcs7_signedAndEnveloped:
225 /* XXXXXXXXXXXXXXXX */
226 si_sk=p7->d.signed_and_enveloped->signer_info;
227 os=ASN1_OCTET_STRING_new();
228 p7->d.signed_and_enveloped->enc_data->enc_data=os;
229 break;
230 case NID_pkcs7_signed:
231 si_sk=p7->d.sign->signer_info;
232 os=p7->d.sign->contents->d.data;
233 break;
234 }
235
236 if (si_sk != NULL)
237 {
238 if ((buf=BUF_MEM_new()) == NULL) goto err;
239 for (i=0; i<sk_num(si_sk); i++)
240 {
241 si=(PKCS7_SIGNER_INFO *)
242 sk_value(si_sk,i);
243 if (si->pkey == NULL)
244 continue;
245 j=OBJ_obj2nid(si->digest_enc_alg->algorithm);
246
247 btmp=bio;
248 for (;;)
249 {
250 if ((btmp=BIO_find_type(btmp,BIO_TYPE_MD))
251 == NULL)
252 {
253 PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
254 goto err;
255 }
256 BIO_get_md_ctx(btmp,&mdc);
257 if (mdc == NULL)
258 {
259 PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_INTERNAL_ERROR);
260 goto err;
261 }
262 if (EVP_MD_pkey_type(EVP_MD_CTX_type(mdc)) == j)
263 break;
264 else
265 btmp=btmp->next_bio;
266 }
267
268 /* We now have the EVP_MD_CTX, lets do the
269 * signing. */
270 memcpy(&ctx_tmp,mdc,sizeof(ctx_tmp));
271 if (!BUF_MEM_grow(buf,EVP_PKEY_size(si->pkey)))
272 goto err;
273
274 sk=si->auth_attr;
275 if ((sk != NULL) && (sk_num(sk) != 0))
276 {
277 x=i2d_ASN1_SET(sk,NULL,i2d_X509_ATTRIBUTE,
278 V_ASN1_SET,V_ASN1_UNIVERSAL);
279 pp=(unsigned char *)Malloc(i);
280 p=pp;
281 i2d_ASN1_SET(sk,&p,i2d_X509_ATTRIBUTE,
282 V_ASN1_SET,V_ASN1_UNIVERSAL);
283 EVP_SignUpdate(&ctx_tmp,pp,x);
284 Free(pp);
285 }
286
287 if (!EVP_SignFinal(&ctx_tmp,(unsigned char *)buf->data,
288 (unsigned int *)&buf->length,si->pkey))
289 goto err;
290 if (!ASN1_STRING_set(si->enc_digest,
291 (unsigned char *)buf->data,buf->length))
292 goto err;
293 }
294 if (p7->detached)
295 ASN1_OCTET_STRING_set(os,(unsigned char *)"",0);
296 else
297 {
298 btmp=BIO_find_type(bio,BIO_TYPE_MEM);
299 if (btmp == NULL)
300 {
301 PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
302 goto err;
303 }
304 BIO_get_mem_ptr(btmp,&buf_mem);
305 ASN1_OCTET_STRING_set(os,
306 (unsigned char *)buf_mem->data,buf_mem->length);
307 }
308 if (pp != NULL) Free(pp);
309 pp=NULL;
310 }
311
312 ret=1;
313err:
314 if (buf != NULL) BUF_MEM_free(buf);
315 return(ret);
316 }
317
318int PKCS7_dataVerify(cert_store,ctx,bio,p7,si)
319X509_STORE *cert_store;
320X509_STORE_CTX *ctx;
321BIO *bio;
322PKCS7 *p7;
323PKCS7_SIGNER_INFO *si;
324 {
325 PKCS7_SIGNED *s;
326 ASN1_OCTET_STRING *os;
327 EVP_MD_CTX mdc_tmp,*mdc;
328 unsigned char *pp,*p;
329 PKCS7_ISSUER_AND_SERIAL *ias;
330 int ret=0,md_type,i;
331 STACK *sk;
332 BIO *btmp;
333 X509 *x509;
334
335 if (!PKCS7_type_is_signed(p7)) abort();
336 /* XXXXXXXXXXXXXXXXXXXXXXX */
337 ias=si->issuer_and_serial;
338 s=p7->d.sign;
339
340 x509=X509_find_by_issuer_and_serial(s->cert,ias->issuer,ias->serial);
341
342 /* were we able to find the cert in passed to us */
343 if (x509 == NULL)
344 {
345 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
346 goto err;
347 }
348
349 /* Lets verify */
350 X509_STORE_CTX_init(ctx,cert_store,x509,s->cert);
351 i=X509_verify_cert(ctx);
352 if (i <= 0) goto err;
353 X509_STORE_CTX_cleanup(ctx);
354
355 /* So we like 'x509', lets check the signature. */
356 md_type=OBJ_obj2nid(si->digest_alg->algorithm);
357
358 btmp=bio;
359 for (;;)
360 {
361 if ((btmp == NULL) ||
362 ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) == NULL))
363 {
364 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
365 goto err;
366 }
367 BIO_get_md_ctx(btmp,&mdc);
368 if (mdc == NULL)
369 {
370 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_INTERNAL_ERROR);
371 goto err;
372 }
373 if (EVP_MD_type(EVP_MD_CTX_type(mdc)) == md_type)
374 break;
375 btmp=btmp->next_bio;
376 }
377
378 /* mdc is the digest ctx that we want */
379 memcpy(&mdc_tmp,mdc,sizeof(mdc_tmp));
380
381 sk=si->auth_attr;
382 if ((sk != NULL) && (sk_num(sk) != 0))
383 {
384 i=i2d_ASN1_SET(sk,NULL,i2d_X509_ATTRIBUTE,
385 V_ASN1_SET,V_ASN1_UNIVERSAL);
386 pp=(unsigned char *)malloc(i);
387 p=pp;
388 i2d_ASN1_SET(sk,&p,i2d_X509_ATTRIBUTE,
389 V_ASN1_SET,V_ASN1_UNIVERSAL);
390 EVP_VerifyUpdate(&mdc_tmp,pp,i);
391 free(pp);
392 }
393
394 os=si->enc_digest;
395 i=EVP_VerifyFinal(&mdc_tmp,os->data,os->length,
396 X509_get_pubkey(x509));
397 if (i <= 0)
398 {
399 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_SIGNATURE_FAILURE);
400 ret= -1;
401 goto err;
402 }
403 else
404 ret=1;
405err:
406 return(ret);
407 }
408
diff --git a/src/lib/libcrypto/pkcs7/pk7_lib.c b/src/lib/libcrypto/pkcs7/pk7_lib.c
new file mode 100644
index 0000000000..7d14ad1173
--- /dev/null
+++ b/src/lib/libcrypto/pkcs7/pk7_lib.c
@@ -0,0 +1,449 @@
1/* crypto/pkcs7/pk7_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "objects.h"
62#include "x509.h"
63
64long PKCS7_ctrl(p7,cmd,larg,parg)
65PKCS7 *p7;
66int cmd;
67long larg;
68char *parg;
69 {
70 int nid;
71 long ret;
72
73 nid=OBJ_obj2nid(p7->type);
74
75 switch (cmd)
76 {
77 case PKCS7_OP_SET_DETACHED_SIGNATURE:
78 if (nid == NID_pkcs7_signed)
79 {
80 ret=p7->detached=(int)larg;
81 }
82 else
83 {
84 PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
85 ret=0;
86 }
87 break;
88 case PKCS7_OP_GET_DETACHED_SIGNATURE:
89 if (nid == NID_pkcs7_signed)
90 {
91 ret=p7->detached;
92 }
93 else
94 {
95 PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
96 ret=0;
97 }
98
99 break;
100 default:
101 abort();
102 }
103 return(ret);
104 }
105
106int PKCS7_content_new(p7,type)
107PKCS7 *p7;
108int type;
109 {
110 PKCS7 *ret=NULL;
111
112 if ((ret=PKCS7_new()) == NULL) goto err;
113 if (!PKCS7_set_type(ret,type)) goto err;
114 if (!PKCS7_set_content(p7,ret)) goto err;
115
116 return(1);
117err:
118 if (ret != NULL) PKCS7_free(ret);
119 return(0);
120 }
121
122int PKCS7_set_content(p7,p7_data)
123PKCS7 *p7;
124PKCS7 *p7_data;
125 {
126 int i;
127
128 i=OBJ_obj2nid(p7->type);
129 switch (i)
130 {
131 case NID_pkcs7_signed:
132 if (p7->d.sign->contents != NULL)
133 PKCS7_content_free(p7->d.sign->contents);
134 p7->d.sign->contents=p7_data;
135 break;
136 case NID_pkcs7_digest:
137 case NID_pkcs7_data:
138 case NID_pkcs7_enveloped:
139 case NID_pkcs7_signedAndEnveloped:
140 case NID_pkcs7_encrypted:
141 default:
142 PKCS7err(PKCS7_F_PKCS7_SET_CONTENT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
143 goto err;
144 }
145 return(1);
146err:
147 return(0);
148 }
149
150int PKCS7_set_type(p7,type)
151PKCS7 *p7;
152int type;
153 {
154 ASN1_OBJECT *obj;
155
156 PKCS7_content_free(p7);
157 obj=OBJ_nid2obj(type); /* will not fail */
158
159 switch (type)
160 {
161 case NID_pkcs7_signed:
162 p7->type=obj;
163 if ((p7->d.sign=PKCS7_SIGNED_new()) == NULL)
164 goto err;
165 ASN1_INTEGER_set(p7->d.sign->version,1);
166 break;
167 case NID_pkcs7_data:
168 p7->type=obj;
169 if ((p7->d.data=ASN1_OCTET_STRING_new()) == NULL)
170 goto err;
171 break;
172 case NID_pkcs7_signedAndEnveloped:
173 p7->type=obj;
174 if ((p7->d.signed_and_enveloped=PKCS7_SIGN_ENVELOPE_new())
175 == NULL)
176 goto err;
177 ASN1_INTEGER_set(p7->d.sign->version,1);
178 break;
179 case NID_pkcs7_digest:
180 case NID_pkcs7_enveloped:
181 case NID_pkcs7_encrypted:
182 default:
183 PKCS7err(PKCS7_F_PKCS7_SET_TYPE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
184 goto err;
185 }
186 return(1);
187err:
188 return(0);
189 }
190
191int PKCS7_add_signer(p7,psi)
192PKCS7 *p7;
193PKCS7_SIGNER_INFO *psi;
194 {
195 int i,j,nid;
196 X509_ALGOR *alg;
197 STACK *signer_sk;
198 STACK *md_sk;
199
200 i=OBJ_obj2nid(p7->type);
201 switch (i)
202 {
203 case NID_pkcs7_signed:
204 signer_sk= p7->d.sign->signer_info;
205 md_sk= p7->d.sign->md_algs;
206 break;
207 case NID_pkcs7_signedAndEnveloped:
208 signer_sk= p7->d.signed_and_enveloped->signer_info;
209 md_sk= p7->d.signed_and_enveloped->md_algs;
210 break;
211 default:
212 PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,PKCS7_R_WRONG_CONTENT_TYPE);
213 return(0);
214 }
215
216 nid=OBJ_obj2nid(psi->digest_alg->algorithm);
217
218 /* If the digest is not currently listed, add it */
219 j=0;
220 for (i=0; i<sk_num(md_sk); i++)
221 {
222 alg=(X509_ALGOR *)sk_value(md_sk,i);
223 if (OBJ_obj2nid(alg->algorithm) == nid)
224 {
225 j=1;
226 break;
227 }
228 }
229 if (!j) /* we need to add another algorithm */
230 {
231 alg=X509_ALGOR_new();
232 alg->algorithm=OBJ_nid2obj(nid);
233 sk_push(md_sk,(char *)alg);
234 }
235
236 sk_push(signer_sk,(char *)psi);
237 return(1);
238 }
239
240int PKCS7_add_certificate(p7,x509)
241PKCS7 *p7;
242X509 *x509;
243 {
244 int i;
245 STACK **sk;
246
247 i=OBJ_obj2nid(p7->type);
248 switch (i)
249 {
250 case NID_pkcs7_signed:
251 sk= &(p7->d.sign->cert);
252 break;
253 case NID_pkcs7_signedAndEnveloped:
254 sk= &(p7->d.signed_and_enveloped->cert);
255 break;
256 default:
257 PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,PKCS7_R_WRONG_CONTENT_TYPE);
258 return(0);
259 }
260
261 if (*sk == NULL)
262 *sk=sk_new_null();
263 CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
264 sk_push(*sk,(char *)x509);
265 return(1);
266 }
267
268int PKCS7_add_crl(p7,crl)
269PKCS7 *p7;
270X509_CRL *crl;
271 {
272 int i;
273 STACK **sk;
274
275 i=OBJ_obj2nid(p7->type);
276 switch (i)
277 {
278 case NID_pkcs7_signed:
279 sk= &(p7->d.sign->crl);
280 break;
281 case NID_pkcs7_signedAndEnveloped:
282 sk= &(p7->d.signed_and_enveloped->crl);
283 break;
284 default:
285 PKCS7err(PKCS7_F_PKCS7_ADD_CRL,PKCS7_R_WRONG_CONTENT_TYPE);
286 return(0);
287 }
288
289 if (*sk == NULL)
290 *sk=sk_new_null();
291
292 CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL);
293 sk_push(*sk,(char *)crl);
294 return(1);
295 }
296
297int PKCS7_SIGNER_INFO_set(p7i,x509,pkey,dgst)
298PKCS7_SIGNER_INFO *p7i;
299X509 *x509;
300EVP_PKEY *pkey;
301EVP_MD *dgst;
302 {
303 /* We now need to add another PKCS7_SIGNER_INFO entry */
304 ASN1_INTEGER_set(p7i->version,1);
305 X509_NAME_set(&p7i->issuer_and_serial->issuer,
306 X509_get_issuer_name(x509));
307
308 /* because ASN1_INTEGER_set is used to set a 'long' we will do
309 * things the ugly way. */
310 ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
311 p7i->issuer_and_serial->serial=
312 ASN1_INTEGER_dup(X509_get_serialNumber(x509));
313
314 /* lets keep the pkey around for a while */
315 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
316 p7i->pkey=pkey;
317
318 /* Set the algorithms */
319 p7i->digest_alg->algorithm=OBJ_nid2obj(EVP_MD_type(dgst));
320 p7i->digest_enc_alg->algorithm=OBJ_nid2obj(EVP_MD_pkey_type(dgst));
321
322#if 1
323 if (p7i->digest_enc_alg->parameter != NULL)
324 ASN1_TYPE_free(p7i->digest_enc_alg->parameter);
325 if ((p7i->digest_enc_alg->parameter=ASN1_TYPE_new()) == NULL)
326 goto err;
327 p7i->digest_enc_alg->parameter->type=V_ASN1_NULL;
328#endif
329
330 return(1);
331err:
332 return(0);
333 }
334
335PKCS7_SIGNER_INFO *PKCS7_add_signature(p7,x509,pkey,dgst)
336PKCS7 *p7;
337X509 *x509;
338EVP_PKEY *pkey;
339EVP_MD *dgst;
340 {
341 PKCS7_SIGNER_INFO *si;
342
343 if ((si=PKCS7_SIGNER_INFO_new()) == NULL) goto err;
344 if (!PKCS7_SIGNER_INFO_set(si,x509,pkey,dgst)) goto err;
345 if (!PKCS7_add_signer(p7,si)) goto err;
346 return(si);
347err:
348 return(NULL);
349 }
350
351STACK *PKCS7_get_signer_info(p7)
352PKCS7 *p7;
353 {
354 if (PKCS7_type_is_signed(p7))
355 {
356 return(p7->d.sign->signer_info);
357 }
358 else
359 return(NULL);
360 }
361
362PKCS7_RECIP_INFO *PKCS7_add_recipient(p7,x509)
363PKCS7 *p7;
364X509 *x509;
365 {
366 PKCS7_RECIP_INFO *ri;
367
368 if ((ri=PKCS7_RECIP_INFO_new()) == NULL) goto err;
369 if (!PKCS7_RECIP_INFO_set(ri,x509)) goto err;
370 if (!PKCS7_add_recipient_info(p7,ri)) goto err;
371 return(ri);
372err:
373 return(NULL);
374 }
375
376int PKCS7_add_recipient_info(p7,ri)
377PKCS7 *p7;
378PKCS7_RECIP_INFO *ri;
379 {
380 int i;
381 STACK *sk;
382
383 i=OBJ_obj2nid(p7->type);
384 switch (i)
385 {
386 case NID_pkcs7_signedAndEnveloped:
387 sk= p7->d.signed_and_enveloped->recipientinfo;
388 break;
389 default:
390 PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,PKCS7_R_WRONG_CONTENT_TYPE);
391 return(0);
392 }
393
394 sk_push(sk,(char *)ri);
395 return(1);
396 }
397
398int PKCS7_RECIP_INFO_set(p7i,x509)
399PKCS7_RECIP_INFO *p7i;
400X509 *x509;
401 {
402 ASN1_INTEGER_set(p7i->version,0);
403 X509_NAME_set(&p7i->issuer_and_serial->issuer,
404 X509_get_issuer_name(x509));
405
406 ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
407 p7i->issuer_and_serial->serial=
408 ASN1_INTEGER_dup(X509_get_serialNumber(x509));
409
410 CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
411 p7i->cert=x509;
412
413 return(1);
414 }
415
416X509 *PKCS7_cert_from_signer_info(p7,si)
417PKCS7 *p7;
418PKCS7_SIGNER_INFO *si;
419 {
420 if (PKCS7_type_is_signed(p7))
421 return(X509_find_by_issuer_and_serial(p7->d.sign->cert,
422 si->issuer_and_serial->issuer,
423 si->issuer_and_serial->serial));
424 else
425 return(NULL);
426 }
427
428int PKCS7_set_cipher(p7,cipher)
429PKCS7 *p7;
430EVP_CIPHER *cipher;
431 {
432 int i;
433 PKCS7_ENC_CONTENT *ec;
434
435 i=OBJ_obj2nid(p7->type);
436 switch (i)
437 {
438 case NID_pkcs7_signedAndEnveloped:
439 ec=p7->d.signed_and_enveloped->enc_data;
440 break;
441 default:
442 PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_WRONG_CONTENT_TYPE);
443 return(0);
444 }
445
446 ec->algorithm->algorithm=OBJ_nid2obj(EVP_CIPHER_nid(cipher));
447 return(ec->algorithm->algorithm != NULL);
448 }
449
diff --git a/src/lib/libcrypto/pkcs7/pkcs7.h b/src/lib/libcrypto/pkcs7/pkcs7.h
new file mode 100644
index 0000000000..ee12f670a8
--- /dev/null
+++ b/src/lib/libcrypto/pkcs7/pkcs7.h
@@ -0,0 +1,449 @@
1/* crypto/pkcs7/pkcs7.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_PKCS7_H
60#define HEADER_PKCS7_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "bio.h"
67#include "x509.h"
68
69/*
70Encryption_ID DES-CBC
71Digest_ID MD5
72Digest_Encryption_ID rsaEncryption
73Key_Encryption_ID rsaEncryption
74*/
75
76typedef struct pkcs7_issuer_and_serial_st
77 {
78 X509_NAME *issuer;
79 ASN1_INTEGER *serial;
80 } PKCS7_ISSUER_AND_SERIAL;
81
82typedef struct pkcs7_signer_info_st
83 {
84 ASN1_INTEGER *version; /* version 1 */
85 PKCS7_ISSUER_AND_SERIAL *issuer_and_serial;
86 X509_ALGOR *digest_alg;
87 STACK /* X509_ATTRIBUTE */ *auth_attr; /* [ 0 ] */
88 X509_ALGOR *digest_enc_alg;
89 ASN1_OCTET_STRING *enc_digest;
90 STACK /* X509_ATTRIBUTE */ *unauth_attr; /* [ 1 ] */
91
92 /* The private key to sign with */
93 EVP_PKEY *pkey;
94 } PKCS7_SIGNER_INFO;
95
96typedef struct pkcs7_recip_info_st
97 {
98 ASN1_INTEGER *version; /* version 0 */
99 PKCS7_ISSUER_AND_SERIAL *issuer_and_serial;
100 X509_ALGOR *key_enc_algor;
101 ASN1_OCTET_STRING *enc_key;
102 X509 *cert; /* get the pub-key from this */
103 } PKCS7_RECIP_INFO;
104
105typedef struct pkcs7_signed_st
106 {
107 ASN1_INTEGER *version; /* version 1 */
108 STACK /* X509_ALGOR's */ *md_algs; /* md used */
109 STACK /* X509 */ *cert; /* [ 0 ] */
110 STACK /* X509_CRL */ *crl; /* [ 1 ] */
111 STACK /* PKCS7_SIGNER_INFO */ *signer_info;
112
113 struct pkcs7_st *contents;
114 } PKCS7_SIGNED;
115/* The above structure is very very similar to PKCS7_SIGN_ENVELOPE.
116 * How about merging the two */
117
118typedef struct pkcs7_enc_content_st
119 {
120 ASN1_OBJECT *content_type;
121 X509_ALGOR *algorithm;
122 ASN1_OCTET_STRING *enc_data; /* [ 0 ] */
123 } PKCS7_ENC_CONTENT;
124
125typedef struct pkcs7_enveloped_st
126 {
127 ASN1_INTEGER *version; /* version 0 */
128 STACK /* PKCS7_RECIP_INFO */ *recipientinfo;
129 PKCS7_ENC_CONTENT *enc_data;
130 } PKCS7_ENVELOPE;
131
132typedef struct pkcs7_signedandenveloped_st
133 {
134 ASN1_INTEGER *version; /* version 1 */
135 STACK /* X509_ALGOR's */ *md_algs; /* md used */
136 STACK /* X509 */ *cert; /* [ 0 ] */
137 STACK /* X509_CRL */ *crl; /* [ 1 ] */
138 STACK /* PKCS7_SIGNER_INFO */ *signer_info;
139
140 PKCS7_ENC_CONTENT *enc_data;
141 STACK /* PKCS7_RECIP_INFO */ *recipientinfo;
142 } PKCS7_SIGN_ENVELOPE;
143
144typedef struct pkcs7_digest_st
145 {
146 ASN1_INTEGER *version; /* version 0 */
147 X509_ALGOR *md; /* md used */
148 struct pkcs7_st *contents;
149 ASN1_OCTET_STRING *digest;
150 } PKCS7_DIGEST;
151
152typedef struct pkcs7_encrypted_st
153 {
154 ASN1_INTEGER *version; /* version 0 */
155 PKCS7_ENC_CONTENT *enc_data;
156 } PKCS7_ENCRYPT;
157
158typedef struct pkcs7_st
159 {
160 /* The following is non NULL if it contains ASN1 encoding of
161 * this structure */
162 unsigned char *asn1;
163 long length;
164
165#define PKCS7_S_HEADER 0
166#define PKCS7_S_BODY 1
167#define PKCS7_S_TAIL 2
168 int state; /* used during processing */
169
170 int detached;
171
172 ASN1_OBJECT *type;
173 /* content as defined by the type */
174 /* all encryption/message digests are applied to the 'contents',
175 * leaving out the 'type' field. */
176 union {
177 char *ptr;
178
179 /* NID_pkcs7_data */
180 ASN1_OCTET_STRING *data;
181
182 /* NID_pkcs7_signed */
183 PKCS7_SIGNED *sign;
184
185 /* NID_pkcs7_enveloped */
186 PKCS7_ENVELOPE *enveloped;
187
188 /* NID_pkcs7_signedAndEnveloped */
189 PKCS7_SIGN_ENVELOPE *signed_and_enveloped;
190
191 /* NID_pkcs7_digest */
192 PKCS7_DIGEST *digest;
193
194 /* NID_pkcs7_encrypted */
195 PKCS7_ENCRYPT *encrypted;
196 } d;
197 } PKCS7;
198
199#define PKCS7_OP_SET_DETACHED_SIGNATURE 1
200#define PKCS7_OP_GET_DETACHED_SIGNATURE 2
201
202#define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed)
203#define PKCS7_type_is_data(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_data)
204
205#define PKCS7_set_detached(p,v) \
206 PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL)
207#define PKCS7_get_detached(p) \
208 PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL)
209
210#ifdef SSLEAY_MACROS
211
212#define PKCS7_ISSUER_AND_SERIAL_digest(data,type,md,len) \
213 ASN1_digest((int (*)())i2d_PKCS7_ISSUER_AND_SERIAL,type,\
214 (char *)data,md,len)
215#endif
216
217
218#ifndef NOPROTO
219PKCS7_ISSUER_AND_SERIAL *PKCS7_ISSUER_AND_SERIAL_new(void );
220void PKCS7_ISSUER_AND_SERIAL_free(
221 PKCS7_ISSUER_AND_SERIAL *a);
222int i2d_PKCS7_ISSUER_AND_SERIAL(
223 PKCS7_ISSUER_AND_SERIAL *a,unsigned char **pp);
224PKCS7_ISSUER_AND_SERIAL *d2i_PKCS7_ISSUER_AND_SERIAL(
225 PKCS7_ISSUER_AND_SERIAL **a,
226 unsigned char **pp, long length);
227
228#ifndef SSLEAY_MACROS
229int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,EVP_MD *type,
230 unsigned char *md,unsigned int *len);
231#ifndef NO_FP_API
232PKCS7 *d2i_PKCS7_fp(FILE *fp,PKCS7 *p7);
233int i2d_PKCS7_fp(FILE *fp,PKCS7 *p7);
234#endif
235PKCS7 *PKCS7_dup(PKCS7 *p7);
236PKCS7 *d2i_PKCS7_bio(BIO *bp,PKCS7 *p7);
237int i2d_PKCS7_bio(BIO *bp,PKCS7 *p7);
238#endif
239
240PKCS7_SIGNER_INFO *PKCS7_SIGNER_INFO_new(void);
241void PKCS7_SIGNER_INFO_free(PKCS7_SIGNER_INFO *a);
242int i2d_PKCS7_SIGNER_INFO(PKCS7_SIGNER_INFO *a,
243 unsigned char **pp);
244PKCS7_SIGNER_INFO *d2i_PKCS7_SIGNER_INFO(PKCS7_SIGNER_INFO **a,
245 unsigned char **pp,long length);
246
247PKCS7_RECIP_INFO *PKCS7_RECIP_INFO_new(void);
248void PKCS7_RECIP_INFO_free(PKCS7_RECIP_INFO *a);
249int i2d_PKCS7_RECIP_INFO(PKCS7_RECIP_INFO *a,
250 unsigned char **pp);
251PKCS7_RECIP_INFO *d2i_PKCS7_RECIP_INFO(PKCS7_RECIP_INFO **a,
252 unsigned char **pp,long length);
253
254PKCS7_SIGNED *PKCS7_SIGNED_new(void);
255void PKCS7_SIGNED_free(PKCS7_SIGNED *a);
256int i2d_PKCS7_SIGNED(PKCS7_SIGNED *a,
257 unsigned char **pp);
258PKCS7_SIGNED *d2i_PKCS7_SIGNED(PKCS7_SIGNED **a,
259 unsigned char **pp,long length);
260
261PKCS7_ENC_CONTENT *PKCS7_ENC_CONTENT_new(void);
262void PKCS7_ENC_CONTENT_free(PKCS7_ENC_CONTENT *a);
263int i2d_PKCS7_ENC_CONTENT(PKCS7_ENC_CONTENT *a,
264 unsigned char **pp);
265PKCS7_ENC_CONTENT *d2i_PKCS7_ENC_CONTENT(PKCS7_ENC_CONTENT **a,
266 unsigned char **pp,long length);
267
268PKCS7_ENVELOPE *PKCS7_ENVELOPE_new(void);
269void PKCS7_ENVELOPE_free(PKCS7_ENVELOPE *a);
270int i2d_PKCS7_ENVELOPE(PKCS7_ENVELOPE *a,
271 unsigned char **pp);
272PKCS7_ENVELOPE *d2i_PKCS7_ENVELOPE(PKCS7_ENVELOPE **a,
273 unsigned char **pp,long length);
274
275PKCS7_SIGN_ENVELOPE *PKCS7_SIGN_ENVELOPE_new(void);
276void PKCS7_SIGN_ENVELOPE_free(PKCS7_SIGN_ENVELOPE *a);
277int i2d_PKCS7_SIGN_ENVELOPE(PKCS7_SIGN_ENVELOPE *a,
278 unsigned char **pp);
279PKCS7_SIGN_ENVELOPE *d2i_PKCS7_SIGN_ENVELOPE(PKCS7_SIGN_ENVELOPE **a,
280 unsigned char **pp,long length);
281
282PKCS7_DIGEST *PKCS7_DIGEST_new(void);
283void PKCS7_DIGEST_free(PKCS7_DIGEST *a);
284int i2d_PKCS7_DIGEST(PKCS7_DIGEST *a,
285 unsigned char **pp);
286PKCS7_DIGEST *d2i_PKCS7_DIGEST(PKCS7_DIGEST **a,
287 unsigned char **pp,long length);
288
289PKCS7_ENCRYPT *PKCS7_ENCRYPT_new(void);
290void PKCS7_ENCRYPT_free(PKCS7_ENCRYPT *a);
291int i2d_PKCS7_ENCRYPT(PKCS7_ENCRYPT *a,
292 unsigned char **pp);
293PKCS7_ENCRYPT *d2i_PKCS7_ENCRYPT(PKCS7_ENCRYPT **a,
294 unsigned char **pp,long length);
295
296PKCS7 *PKCS7_new(void);
297void PKCS7_free(PKCS7 *a);
298void PKCS7_content_free(PKCS7 *a);
299int i2d_PKCS7(PKCS7 *a,
300 unsigned char **pp);
301PKCS7 *d2i_PKCS7(PKCS7 **a,
302 unsigned char **pp,long length);
303
304void ERR_load_PKCS7_strings(void);
305
306
307long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg);
308
309int PKCS7_set_type(PKCS7 *p7, int type);
310int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data);
311int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
312 EVP_MD *dgst);
313int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i);
314int PKCS7_add_certificate(PKCS7 *p7, X509 *x509);
315int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509);
316int PKCS7_content_new(PKCS7 *p7, int nid);
317int PKCS7_dataSign(PKCS7 *p7, BIO *bio);
318int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx,
319 BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si);
320
321BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio);
322/*int PKCS7_DataFinal(PKCS7 *p7, BIO *bio); */
323
324PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509,
325 EVP_PKEY *pkey, EVP_MD *dgst);
326X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
327STACK *PKCS7_get_signer_info(PKCS7 *p7);
328
329PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509);
330int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri);
331int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509);
332int PKCS7_set_cipher(PKCS7 *p7, EVP_CIPHER *cipher);
333
334
335
336#else
337
338PKCS7_ISSUER_AND_SERIAL *PKCS7_ISSUER_AND_SERIAL_new();
339void PKCS7_ISSUER_AND_SERIAL_free();
340int i2d_PKCS7_ISSUER_AND_SERIAL();
341PKCS7_ISSUER_AND_SERIAL *d2i_PKCS7_ISSUER_AND_SERIAL();
342
343#ifndef SSLEAY_MACROS
344int PKCS7_ISSUER_AND_SERIAL_digest();
345#ifndef NO_FP_API
346PKCS7 *d2i_PKCS7_fp();
347int i2d_PKCS7_fp();
348#endif
349PKCS7 *PKCS7_dup();
350PKCS7 *d2i_PKCS7_bio();
351int i2d_PKCS7_bio();
352
353#endif
354
355PKCS7_SIGNER_INFO *PKCS7_SIGNER_INFO_new();
356void PKCS7_SIGNER_INFO_free();
357int i2d_PKCS7_SIGNER_INFO();
358PKCS7_SIGNER_INFO *d2i_PKCS7_SIGNER_INFO();
359PKCS7_RECIP_INFO *PKCS7_RECIP_INFO_new();
360void PKCS7_RECIP_INFO_free();
361int i2d_PKCS7_RECIP_INFO();
362PKCS7_RECIP_INFO *d2i_PKCS7_RECIP_INFO();
363PKCS7_SIGNED *PKCS7_SIGNED_new();
364void PKCS7_SIGNED_free();
365int i2d_PKCS7_SIGNED();
366PKCS7_SIGNED *d2i_PKCS7_SIGNED();
367PKCS7_ENC_CONTENT *PKCS7_ENC_CONTENT_new();
368void PKCS7_ENC_CONTENT_free();
369int i2d_PKCS7_ENC_CONTENT();
370PKCS7_ENC_CONTENT *d2i_PKCS7_ENC_CONTENT();
371PKCS7_ENVELOPE *PKCS7_ENVELOPE_new();
372void PKCS7_ENVELOPE_free();
373int i2d_PKCS7_ENVELOPE();
374PKCS7_ENVELOPE *d2i_PKCS7_ENVELOPE();
375PKCS7_SIGN_ENVELOPE *PKCS7_SIGN_ENVELOPE_new();
376void PKCS7_SIGN_ENVELOPE_free();
377int i2d_PKCS7_SIGN_ENVELOPE();
378PKCS7_SIGN_ENVELOPE *d2i_PKCS7_SIGN_ENVELOPE();
379PKCS7_DIGEST *PKCS7_DIGEST_new();
380void PKCS7_DIGEST_free();
381int i2d_PKCS7_DIGEST();
382PKCS7_DIGEST *d2i_PKCS7_DIGEST();
383PKCS7_ENCRYPT *PKCS7_ENCRYPT_new();
384void PKCS7_ENCRYPT_free();
385int i2d_PKCS7_ENCRYPT();
386PKCS7_ENCRYPT *d2i_PKCS7_ENCRYPT();
387PKCS7 *PKCS7_new();
388void PKCS7_free();
389void PKCS7_content_free();
390int i2d_PKCS7();
391PKCS7 *d2i_PKCS7();
392
393void ERR_load_PKCS7_strings();
394
395long PKCS7_ctrl();
396int PKCS7_set_type();
397int PKCS7_set_content();
398int PKCS7_SIGNER_INFO_set();
399int PKCS7_add_signer();
400int PKCS7_add_certificate();
401int PKCS7_add_crl();
402int PKCS7_content_new();
403int PKCS7_dataSign();
404int PKCS7_dataVerify();
405BIO *PKCS7_dataInit();
406PKCS7_SIGNER_INFO *PKCS7_add_signature();
407X509 *PKCS7_cert_from_signer_info();
408STACK *PKCS7_get_signer_info();
409
410PKCS7_RECIP_INFO *PKCS7_add_recipient();
411int PKCS7_add_recipient_info();
412int PKCS7_RECIP_INFO_set();
413int PKCS7_set_cipher();
414
415#endif
416
417/* BEGIN ERROR CODES */
418/* Error codes for the PKCS7 functions. */
419
420/* Function codes. */
421#define PKCS7_F_PKCS7_ADD_CERTIFICATE 100
422#define PKCS7_F_PKCS7_ADD_CRL 101
423#define PKCS7_F_PKCS7_ADD_RECIPIENT_INFO 102
424#define PKCS7_F_PKCS7_ADD_SIGNER 103
425#define PKCS7_F_PKCS7_CTRL 104
426#define PKCS7_F_PKCS7_DATAINIT 105
427#define PKCS7_F_PKCS7_DATASIGN 106
428#define PKCS7_F_PKCS7_DATAVERIFY 107
429#define PKCS7_F_PKCS7_SET_CIPHER 108
430#define PKCS7_F_PKCS7_SET_CONTENT 109
431#define PKCS7_F_PKCS7_SET_TYPE 110
432
433/* Reason codes. */
434#define PKCS7_R_INTERNAL_ERROR 100
435#define PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE 101
436#define PKCS7_R_SIGNATURE_FAILURE 102
437#define PKCS7_R_UNABLE_TO_FIND_CERTIFICATE 103
438#define PKCS7_R_UNABLE_TO_FIND_MEM_BIO 104
439#define PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST 105
440#define PKCS7_R_UNKNOWN_DIGEST_TYPE 106
441#define PKCS7_R_UNSUPPORTED_CIPHER_TYPE 107
442#define PKCS7_R_UNSUPPORTED_CONTENT_TYPE 108
443#define PKCS7_R_WRONG_CONTENT_TYPE 109
444
445#ifdef __cplusplus
446}
447#endif
448#endif
449
diff --git a/src/lib/libcrypto/pkcs7/pkcs7err.c b/src/lib/libcrypto/pkcs7/pkcs7err.c
new file mode 100644
index 0000000000..f851057422
--- /dev/null
+++ b/src/lib/libcrypto/pkcs7/pkcs7err.c
@@ -0,0 +1,110 @@
1/* lib/pkcs7/pkcs7_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "pkcs7.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA PKCS7_str_functs[]=
65 {
66{ERR_PACK(0,PKCS7_F_PKCS7_ADD_CERTIFICATE,0), "PKCS7_add_certificate"},
67{ERR_PACK(0,PKCS7_F_PKCS7_ADD_CRL,0), "PKCS7_add_crl"},
68{ERR_PACK(0,PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,0), "PKCS7_add_recipient_info"},
69{ERR_PACK(0,PKCS7_F_PKCS7_ADD_SIGNER,0), "PKCS7_add_signer"},
70{ERR_PACK(0,PKCS7_F_PKCS7_CTRL,0), "PKCS7_ctrl"},
71{ERR_PACK(0,PKCS7_F_PKCS7_DATAINIT,0), "PKCS7_dataInit"},
72{ERR_PACK(0,PKCS7_F_PKCS7_DATASIGN,0), "PKCS7_dataSign"},
73{ERR_PACK(0,PKCS7_F_PKCS7_DATAVERIFY,0), "PKCS7_dataVerify"},
74{ERR_PACK(0,PKCS7_F_PKCS7_SET_CIPHER,0), "PKCS7_set_cipher"},
75{ERR_PACK(0,PKCS7_F_PKCS7_SET_CONTENT,0), "PKCS7_set_content"},
76{ERR_PACK(0,PKCS7_F_PKCS7_SET_TYPE,0), "PKCS7_set_type"},
77{0,NULL},
78 };
79
80static ERR_STRING_DATA PKCS7_str_reasons[]=
81 {
82{PKCS7_R_INTERNAL_ERROR ,"internal error"},
83{PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE,"operation not supported on this type"},
84{PKCS7_R_SIGNATURE_FAILURE ,"signature failure"},
85{PKCS7_R_UNABLE_TO_FIND_CERTIFICATE ,"unable to find certificate"},
86{PKCS7_R_UNABLE_TO_FIND_MEM_BIO ,"unable to find mem bio"},
87{PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST ,"unable to find message digest"},
88{PKCS7_R_UNKNOWN_DIGEST_TYPE ,"unknown digest type"},
89{PKCS7_R_UNSUPPORTED_CIPHER_TYPE ,"unsupported cipher type"},
90{PKCS7_R_UNSUPPORTED_CONTENT_TYPE ,"unsupported content type"},
91{PKCS7_R_WRONG_CONTENT_TYPE ,"wrong content type"},
92{0,NULL},
93 };
94
95#endif
96
97void ERR_load_PKCS7_strings()
98 {
99 static int init=1;
100
101 if (init);
102 {;
103 init=0;
104#ifndef NO_ERR
105 ERR_load_strings(ERR_LIB_PKCS7,PKCS7_str_functs);
106 ERR_load_strings(ERR_LIB_PKCS7,PKCS7_str_reasons);
107#endif
108
109 }
110 }
diff --git a/src/lib/libcrypto/rand/rand.h b/src/lib/libcrypto/rand/rand.h
new file mode 100644
index 0000000000..477d7a150a
--- /dev/null
+++ b/src/lib/libcrypto/rand/rand.h
@@ -0,0 +1,92 @@
1/* crypto/rand/rand.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_RAND_H
60#define HEADER_RAND_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#ifndef NOPROTO
67void RAND_cleanup(void );
68void RAND_bytes( unsigned char *buf,int num);
69void RAND_seed( unsigned char *buf,int num);
70int RAND_load_file(char *file,long max_bytes);
71int RAND_write_file(char *file);
72char *RAND_file_name(char *file,int num);
73#ifdef WINDOWS
74void RAND_screen(void);
75#endif
76#else
77void RAND_cleanup();
78void RAND_bytes();
79void RAND_seed();
80int RAND_load_file();
81int RAND_write_file();
82char *RAND_file_name();
83#ifdef WINDOWS
84void RAND_screen();
85#endif
86#endif
87
88#ifdef __cplusplus
89}
90#endif
91
92#endif
diff --git a/src/lib/libcrypto/rand/randfile.c b/src/lib/libcrypto/rand/randfile.c
new file mode 100644
index 0000000000..f2b3746363
--- /dev/null
+++ b/src/lib/libcrypto/rand/randfile.c
@@ -0,0 +1,166 @@
1/* crypto/rand/randfile.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <sys/stat.h>
62#include <sys/types.h>
63#include "rand.h"
64
65#undef BUFSIZE
66#define BUFSIZE 1024
67#define RAND_DATA 1024
68
69/* #define RFILE ".rand" - defined in ../../e_os.h */
70
71int RAND_load_file(file,bytes)
72char *file;
73long bytes;
74 {
75 MS_STATIC unsigned char buf[BUFSIZE];
76 struct stat sb;
77 int i,ret=0,n;
78 FILE *in;
79
80 if (file == NULL) return(0);
81
82 i=stat(file,&sb);
83 /* If the state fails, put some crap in anyway */
84 RAND_seed((unsigned char *)&sb,sizeof(sb));
85 ret+=sizeof(sb);
86 if (i < 0) return(0);
87 if (bytes <= 0) return(ret);
88
89 in=fopen(file,"r");
90 if (in == NULL) goto err;
91 for (;;)
92 {
93 n=(bytes < BUFSIZE)?(int)bytes:BUFSIZE;
94 i=fread(buf,1,n,in);
95 if (i <= 0) break;
96 /* even if n != i, use the full array */
97 RAND_seed(buf,n);
98 ret+=i;
99 bytes-=n;
100 if (bytes <= 0) break;
101 }
102 fclose(in);
103 memset(buf,0,BUFSIZE);
104err:
105 return(ret);
106 }
107
108int RAND_write_file(file)
109char *file;
110 {
111 unsigned char buf[BUFSIZE];
112 int i,ret=0;
113 FILE *out;
114 int n;
115
116 out=fopen(file,"w");
117 if (out == NULL) goto err;
118 chmod(file,0600);
119 n=RAND_DATA;
120 for (;;)
121 {
122 i=(n > BUFSIZE)?BUFSIZE:n;
123 n-=BUFSIZE;
124 RAND_bytes(buf,i);
125 i=fwrite(buf,1,i,out);
126 if (i <= 0)
127 {
128 ret=0;
129 break;
130 }
131 ret+=i;
132 if (n <= 0) break;
133 }
134 fclose(out);
135 memset(buf,0,BUFSIZE);
136err:
137 return(ret);
138 }
139
140char *RAND_file_name(buf,size)
141char *buf;
142int size;
143 {
144 char *s;
145 char *ret=NULL;
146
147 s=getenv("RANDFILE");
148 if (s != NULL)
149 {
150 strncpy(buf,s,size-1);
151 buf[size-1]='\0';
152 ret=buf;
153 }
154 else
155 {
156 s=getenv("HOME");
157 if (s == NULL) return(RFILE);
158 if (((int)(strlen(s)+strlen(RFILE)+2)) > size)
159 return(RFILE);
160 strcpy(buf,s);
161 strcat(buf,"/");
162 strcat(buf,RFILE);
163 ret=buf;
164 }
165 return(ret);
166 }
diff --git a/src/lib/libcrypto/rc2/rc2_cbc.c b/src/lib/libcrypto/rc2/rc2_cbc.c
new file mode 100644
index 0000000000..22e89f0441
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rc2_cbc.c
@@ -0,0 +1,235 @@
1/* crypto/rc2/rc2_cbc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "rc2.h"
60#include "rc2_locl.h"
61
62void RC2_cbc_encrypt(in, out, length, ks, iv, encrypt)
63unsigned char *in;
64unsigned char *out;
65long length;
66RC2_KEY *ks;
67unsigned char *iv;
68int encrypt;
69 {
70 register unsigned long tin0,tin1;
71 register unsigned long tout0,tout1,xor0,xor1;
72 register long l=length;
73 unsigned long tin[2];
74
75 if (encrypt)
76 {
77 c2l(iv,tout0);
78 c2l(iv,tout1);
79 iv-=8;
80 for (l-=8; l>=0; l-=8)
81 {
82 c2l(in,tin0);
83 c2l(in,tin1);
84 tin0^=tout0;
85 tin1^=tout1;
86 tin[0]=tin0;
87 tin[1]=tin1;
88 RC2_encrypt(tin,ks);
89 tout0=tin[0]; l2c(tout0,out);
90 tout1=tin[1]; l2c(tout1,out);
91 }
92 if (l != -8)
93 {
94 c2ln(in,tin0,tin1,l+8);
95 tin0^=tout0;
96 tin1^=tout1;
97 tin[0]=tin0;
98 tin[1]=tin1;
99 RC2_encrypt(tin,ks);
100 tout0=tin[0]; l2c(tout0,out);
101 tout1=tin[1]; l2c(tout1,out);
102 }
103 l2c(tout0,iv);
104 l2c(tout1,iv);
105 }
106 else
107 {
108 c2l(iv,xor0);
109 c2l(iv,xor1);
110 iv-=8;
111 for (l-=8; l>=0; l-=8)
112 {
113 c2l(in,tin0); tin[0]=tin0;
114 c2l(in,tin1); tin[1]=tin1;
115 RC2_decrypt(tin,ks);
116 tout0=tin[0]^xor0;
117 tout1=tin[1]^xor1;
118 l2c(tout0,out);
119 l2c(tout1,out);
120 xor0=tin0;
121 xor1=tin1;
122 }
123 if (l != -8)
124 {
125 c2l(in,tin0); tin[0]=tin0;
126 c2l(in,tin1); tin[1]=tin1;
127 RC2_decrypt(tin,ks);
128 tout0=tin[0]^xor0;
129 tout1=tin[1]^xor1;
130 l2cn(tout0,tout1,out,l+8);
131 xor0=tin0;
132 xor1=tin1;
133 }
134 l2c(xor0,iv);
135 l2c(xor1,iv);
136 }
137 tin0=tin1=tout0=tout1=xor0=xor1=0;
138 tin[0]=tin[1]=0;
139 }
140
141void RC2_encrypt(d,key)
142unsigned long *d;
143RC2_KEY *key;
144 {
145 int i,n;
146 register RC2_INT *p0,*p1;
147 register RC2_INT x0,x1,x2,x3,t;
148 unsigned long l;
149
150 l=d[0];
151 x0=(RC2_INT)l&0xffff;
152 x1=(RC2_INT)(l>>16L);
153 l=d[1];
154 x2=(RC2_INT)l&0xffff;
155 x3=(RC2_INT)(l>>16L);
156
157 n=3;
158 i=5;
159
160 p0=p1= &(key->data[0]);
161 for (;;)
162 {
163 t=(x0+(x1& ~x3)+(x2&x3)+ *(p0++))&0xffff;
164 x0=(t<<1)|(t>>15);
165 t=(x1+(x2& ~x0)+(x3&x0)+ *(p0++))&0xffff;
166 x1=(t<<2)|(t>>14);
167 t=(x2+(x3& ~x1)+(x0&x1)+ *(p0++))&0xffff;
168 x2=(t<<3)|(t>>13);
169 t=(x3+(x0& ~x2)+(x1&x2)+ *(p0++))&0xffff;
170 x3=(t<<5)|(t>>11);
171
172 if (--i == 0)
173 {
174 if (--n == 0) break;
175 i=(n == 2)?6:5;
176
177 x0+=p1[x3&0x3f];
178 x1+=p1[x0&0x3f];
179 x2+=p1[x1&0x3f];
180 x3+=p1[x2&0x3f];
181 }
182 }
183
184 d[0]=(unsigned long)(x0&0xffff)|((unsigned long)(x1&0xffff)<<16L);
185 d[1]=(unsigned long)(x2&0xffff)|((unsigned long)(x3&0xffff)<<16L);
186 }
187
188void RC2_decrypt(d,key)
189unsigned long *d;
190RC2_KEY *key;
191 {
192 int i,n;
193 register RC2_INT *p0,*p1;
194 register RC2_INT x0,x1,x2,x3,t;
195 unsigned long l;
196
197 l=d[0];
198 x0=(RC2_INT)l&0xffff;
199 x1=(RC2_INT)(l>>16L);
200 l=d[1];
201 x2=(RC2_INT)l&0xffff;
202 x3=(RC2_INT)(l>>16L);
203
204 n=3;
205 i=5;
206
207 p0= &(key->data[63]);
208 p1= &(key->data[0]);
209 for (;;)
210 {
211 t=((x3<<11)|(x3>>5))&0xffff;
212 x3=(t-(x0& ~x2)-(x1&x2)- *(p0--))&0xffff;
213 t=((x2<<13)|(x2>>3))&0xffff;
214 x2=(t-(x3& ~x1)-(x0&x1)- *(p0--))&0xffff;
215 t=((x1<<14)|(x1>>2))&0xffff;
216 x1=(t-(x2& ~x0)-(x3&x0)- *(p0--))&0xffff;
217 t=((x0<<15)|(x0>>1))&0xffff;
218 x0=(t-(x1& ~x3)-(x2&x3)- *(p0--))&0xffff;
219
220 if (--i == 0)
221 {
222 if (--n == 0) break;
223 i=(n == 2)?6:5;
224
225 x3=(x3-p1[x2&0x3f])&0xffff;
226 x2=(x2-p1[x1&0x3f])&0xffff;
227 x1=(x1-p1[x0&0x3f])&0xffff;
228 x0=(x0-p1[x3&0x3f])&0xffff;
229 }
230 }
231
232 d[0]=(unsigned long)(x0&0xffff)|((unsigned long)(x1&0xffff)<<16L);
233 d[1]=(unsigned long)(x2&0xffff)|((unsigned long)(x3&0xffff)<<16L);
234 }
235
diff --git a/src/lib/libcrypto/rc2/rc2_ecb.c b/src/lib/libcrypto/rc2/rc2_ecb.c
new file mode 100644
index 0000000000..96239cd4e0
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rc2_ecb.c
@@ -0,0 +1,90 @@
1/* crypto/rc2/rc2_ecb.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "rc2.h"
60#include "rc2_locl.h"
61
62char *RC2_version="RC2 part of SSLeay 0.9.0b 29-Jun-1998";
63
64/* RC2 as implemented frm a posting from
65 * Newsgroups: sci.crypt
66 * Sender: pgut01@cs.auckland.ac.nz (Peter Gutmann)
67 * Subject: Specification for Ron Rivests Cipher No.2
68 * Message-ID: <4fk39f$f70@net.auckland.ac.nz>
69 * Date: 11 Feb 1996 06:45:03 GMT
70 */
71
72void RC2_ecb_encrypt(in, out, ks, encrypt)
73unsigned char *in;
74unsigned char *out;
75RC2_KEY *ks;
76int encrypt;
77 {
78 unsigned long l,d[2];
79
80 c2l(in,l); d[0]=l;
81 c2l(in,l); d[1]=l;
82 if (encrypt)
83 RC2_encrypt(d,ks);
84 else
85 RC2_decrypt(d,ks);
86 l=d[0]; l2c(l,out);
87 l=d[1]; l2c(l,out);
88 l=d[0]=d[1]=0;
89 }
90
diff --git a/src/lib/libcrypto/rc2/rc2_locl.h b/src/lib/libcrypto/rc2/rc2_locl.h
new file mode 100644
index 0000000000..565cd17619
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rc2_locl.h
@@ -0,0 +1,156 @@
1/* crypto/rc2/rc2_locl.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#undef c2l
60#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
61 l|=((unsigned long)(*((c)++)))<< 8L, \
62 l|=((unsigned long)(*((c)++)))<<16L, \
63 l|=((unsigned long)(*((c)++)))<<24L)
64
65/* NOTE - c is not incremented as per c2l */
66#undef c2ln
67#define c2ln(c,l1,l2,n) { \
68 c+=n; \
69 l1=l2=0; \
70 switch (n) { \
71 case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
72 case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
73 case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
74 case 5: l2|=((unsigned long)(*(--(c)))); \
75 case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
76 case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
77 case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
78 case 1: l1|=((unsigned long)(*(--(c)))); \
79 } \
80 }
81
82#undef l2c
83#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
84 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
85 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
86 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
87
88/* NOTE - c is not incremented as per l2c */
89#undef l2cn
90#define l2cn(l1,l2,c,n) { \
91 c+=n; \
92 switch (n) { \
93 case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
94 case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
95 case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
96 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
97 case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
98 case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
99 case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
100 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
101 } \
102 }
103
104/* NOTE - c is not incremented as per n2l */
105#define n2ln(c,l1,l2,n) { \
106 c+=n; \
107 l1=l2=0; \
108 switch (n) { \
109 case 8: l2 =((unsigned long)(*(--(c)))) ; \
110 case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
111 case 6: l2|=((unsigned long)(*(--(c))))<<16; \
112 case 5: l2|=((unsigned long)(*(--(c))))<<24; \
113 case 4: l1 =((unsigned long)(*(--(c)))) ; \
114 case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
115 case 2: l1|=((unsigned long)(*(--(c))))<<16; \
116 case 1: l1|=((unsigned long)(*(--(c))))<<24; \
117 } \
118 }
119
120/* NOTE - c is not incremented as per l2n */
121#define l2nn(l1,l2,c,n) { \
122 c+=n; \
123 switch (n) { \
124 case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \
125 case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
126 case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
127 case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
128 case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \
129 case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
130 case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
131 case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
132 } \
133 }
134
135#undef n2l
136#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
137 l|=((unsigned long)(*((c)++)))<<16L, \
138 l|=((unsigned long)(*((c)++)))<< 8L, \
139 l|=((unsigned long)(*((c)++))))
140
141#undef l2n
142#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
143 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
144 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
145 *((c)++)=(unsigned char)(((l) )&0xff))
146
147#define C_RC2(n) \
148 t=(x0+(x1& ~x3)+(x2&x3)+ *(p0++))&0xffff; \
149 x0=(t<<1)|(t>>15); \
150 t=(x1+(x2& ~x0)+(x3&x0)+ *(p0++))&0xffff; \
151 x1=(t<<2)|(t>>14); \
152 t=(x2+(x3& ~x1)+(x0&x1)+ *(p0++))&0xffff; \
153 x2=(t<<3)|(t>>13); \
154 t=(x3+(x0& ~x2)+(x1&x2)+ *(p0++))&0xffff; \
155 x3=(t<<5)|(t>>11);
156
diff --git a/src/lib/libcrypto/rc2/rc2_skey.c b/src/lib/libcrypto/rc2/rc2_skey.c
new file mode 100644
index 0000000000..0f1f253395
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rc2_skey.c
@@ -0,0 +1,142 @@
1/* crypto/rc2/rc2_skey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "rc2.h"
60#include "rc2_locl.h"
61
62static unsigned char key_table[256]={
63 0xd9,0x78,0xf9,0xc4,0x19,0xdd,0xb5,0xed,0x28,0xe9,0xfd,0x79,
64 0x4a,0xa0,0xd8,0x9d,0xc6,0x7e,0x37,0x83,0x2b,0x76,0x53,0x8e,
65 0x62,0x4c,0x64,0x88,0x44,0x8b,0xfb,0xa2,0x17,0x9a,0x59,0xf5,
66 0x87,0xb3,0x4f,0x13,0x61,0x45,0x6d,0x8d,0x09,0x81,0x7d,0x32,
67 0xbd,0x8f,0x40,0xeb,0x86,0xb7,0x7b,0x0b,0xf0,0x95,0x21,0x22,
68 0x5c,0x6b,0x4e,0x82,0x54,0xd6,0x65,0x93,0xce,0x60,0xb2,0x1c,
69 0x73,0x56,0xc0,0x14,0xa7,0x8c,0xf1,0xdc,0x12,0x75,0xca,0x1f,
70 0x3b,0xbe,0xe4,0xd1,0x42,0x3d,0xd4,0x30,0xa3,0x3c,0xb6,0x26,
71 0x6f,0xbf,0x0e,0xda,0x46,0x69,0x07,0x57,0x27,0xf2,0x1d,0x9b,
72 0xbc,0x94,0x43,0x03,0xf8,0x11,0xc7,0xf6,0x90,0xef,0x3e,0xe7,
73 0x06,0xc3,0xd5,0x2f,0xc8,0x66,0x1e,0xd7,0x08,0xe8,0xea,0xde,
74 0x80,0x52,0xee,0xf7,0x84,0xaa,0x72,0xac,0x35,0x4d,0x6a,0x2a,
75 0x96,0x1a,0xd2,0x71,0x5a,0x15,0x49,0x74,0x4b,0x9f,0xd0,0x5e,
76 0x04,0x18,0xa4,0xec,0xc2,0xe0,0x41,0x6e,0x0f,0x51,0xcb,0xcc,
77 0x24,0x91,0xaf,0x50,0xa1,0xf4,0x70,0x39,0x99,0x7c,0x3a,0x85,
78 0x23,0xb8,0xb4,0x7a,0xfc,0x02,0x36,0x5b,0x25,0x55,0x97,0x31,
79 0x2d,0x5d,0xfa,0x98,0xe3,0x8a,0x92,0xae,0x05,0xdf,0x29,0x10,
80 0x67,0x6c,0xba,0xc9,0xd3,0x00,0xe6,0xcf,0xe1,0x9e,0xa8,0x2c,
81 0x63,0x16,0x01,0x3f,0x58,0xe2,0x89,0xa9,0x0d,0x38,0x34,0x1b,
82 0xab,0x33,0xff,0xb0,0xbb,0x48,0x0c,0x5f,0xb9,0xb1,0xcd,0x2e,
83 0xc5,0xf3,0xdb,0x47,0xe5,0xa5,0x9c,0x77,0x0a,0xa6,0x20,0x68,
84 0xfe,0x7f,0xc1,0xad,
85 };
86
87/* It has come to my attention that there are 2 versions of the RC2
88 * key schedule. One which is normal, and anther which has a hook to
89 * use a reduced key length.
90 * BSAFE uses the 'retarded' version. What I previously shipped is
91 * the same as specifying 1024 for the 'bits' parameter. Bsafe uses
92 * a version where the bits parameter is the same as len*8 */
93void RC2_set_key(key,len,data,bits)
94RC2_KEY *key;
95int len;
96unsigned char *data;
97int bits;
98 {
99 int i,j;
100 unsigned char *k;
101 RC2_INT *ki;
102 unsigned int c,d;
103
104 k= (unsigned char *)&(key->data[0]);
105 *k=0; /* for if there is a zero length key */
106
107 if (len > 128) len=128;
108 if (bits <= 0) bits=1024;
109 if (bits > 1024) bits=1024;
110
111 for (i=0; i<len; i++)
112 k[i]=data[i];
113
114 /* expand table */
115 d=k[len-1];
116 j=0;
117 for (i=len; i < 128; i++,j++)
118 {
119 d=key_table[(k[j]+d)&0xff];
120 k[i]=d;
121 }
122
123 /* hmm.... key reduction to 'bits' bits */
124
125 j=(bits+7)>>3;
126 i=128-j;
127 c= (0xff>>(-bits & 0x07));
128
129 d=key_table[k[i]&c];
130 k[i]=d;
131 while (i--)
132 {
133 d=key_table[k[i+j]^d];
134 k[i]=d;
135 }
136
137 /* copy from bytes into RC2_INT's */
138 ki= &(key->data[63]);
139 for (i=127; i>=0; i-=2)
140 *(ki--)=((k[i]<<8)|k[i-1])&0xffff;
141 }
142
diff --git a/src/lib/libcrypto/rc2/rc2cfb64.c b/src/lib/libcrypto/rc2/rc2cfb64.c
new file mode 100644
index 0000000000..d409fb77e9
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rc2cfb64.c
@@ -0,0 +1,127 @@
1/* crypto/rc2/rc2cfb64.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "rc2.h"
60#include "rc2_locl.h"
61
62/* The input and output encrypted as though 64bit cfb mode is being
63 * used. The extra state information to record how much of the
64 * 64bit block we have used is contained in *num;
65 */
66
67void RC2_cfb64_encrypt(in, out, length, schedule, ivec, num, encrypt)
68unsigned char *in;
69unsigned char *out;
70long length;
71RC2_KEY *schedule;
72unsigned char *ivec;
73int *num;
74int encrypt;
75 {
76 register unsigned long v0,v1,t;
77 register int n= *num;
78 register long l=length;
79 unsigned long ti[2];
80 unsigned char *iv,c,cc;
81
82 iv=(unsigned char *)ivec;
83 if (encrypt)
84 {
85 while (l--)
86 {
87 if (n == 0)
88 {
89 c2l(iv,v0); ti[0]=v0;
90 c2l(iv,v1); ti[1]=v1;
91 RC2_encrypt((unsigned long *)ti,schedule);
92 iv=(unsigned char *)ivec;
93 t=ti[0]; l2c(t,iv);
94 t=ti[1]; l2c(t,iv);
95 iv=(unsigned char *)ivec;
96 }
97 c= *(in++)^iv[n];
98 *(out++)=c;
99 iv[n]=c;
100 n=(n+1)&0x07;
101 }
102 }
103 else
104 {
105 while (l--)
106 {
107 if (n == 0)
108 {
109 c2l(iv,v0); ti[0]=v0;
110 c2l(iv,v1); ti[1]=v1;
111 RC2_encrypt((unsigned long *)ti,schedule);
112 iv=(unsigned char *)ivec;
113 t=ti[0]; l2c(t,iv);
114 t=ti[1]; l2c(t,iv);
115 iv=(unsigned char *)ivec;
116 }
117 cc= *(in++);
118 c=iv[n];
119 iv[n]=cc;
120 *(out++)=c^cc;
121 n=(n+1)&0x07;
122 }
123 }
124 v0=v1=ti[0]=ti[1]=t=c=cc=0;
125 *num=n;
126 }
127
diff --git a/src/lib/libcrypto/rc2/rc2ofb64.c b/src/lib/libcrypto/rc2/rc2ofb64.c
new file mode 100644
index 0000000000..4f09167447
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rc2ofb64.c
@@ -0,0 +1,115 @@
1/* crypto/rc2/rc2ofb64.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "rc2.h"
60#include "rc2_locl.h"
61
62/* The input and output encrypted as though 64bit ofb mode is being
63 * used. The extra state information to record how much of the
64 * 64bit block we have used is contained in *num;
65 */
66void RC2_ofb64_encrypt(in, out, length, schedule, ivec, num)
67unsigned char *in;
68unsigned char *out;
69long length;
70RC2_KEY *schedule;
71unsigned char *ivec;
72int *num;
73 {
74 register unsigned long v0,v1,t;
75 register int n= *num;
76 register long l=length;
77 unsigned char d[8];
78 register char *dp;
79 unsigned long ti[2];
80 unsigned char *iv;
81 int save=0;
82
83 iv=(unsigned char *)ivec;
84 c2l(iv,v0);
85 c2l(iv,v1);
86 ti[0]=v0;
87 ti[1]=v1;
88 dp=(char *)d;
89 l2c(v0,dp);
90 l2c(v1,dp);
91 while (l--)
92 {
93 if (n == 0)
94 {
95 RC2_encrypt((unsigned long *)ti,schedule);
96 dp=(char *)d;
97 t=ti[0]; l2c(t,dp);
98 t=ti[1]; l2c(t,dp);
99 save++;
100 }
101 *(out++)= *(in++)^d[n];
102 n=(n+1)&0x07;
103 }
104 if (save)
105 {
106 v0=ti[0];
107 v1=ti[1];
108 iv=(unsigned char *)ivec;
109 l2c(v0,iv);
110 l2c(v1,iv);
111 }
112 t=v0=v1=ti[0]=ti[1]=0;
113 *num=n;
114 }
115
diff --git a/src/lib/libcrypto/rc2/rrc2.doc b/src/lib/libcrypto/rc2/rrc2.doc
new file mode 100644
index 0000000000..f93ee003d2
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rrc2.doc
@@ -0,0 +1,219 @@
1>From cygnus.mincom.oz.au!minbne.mincom.oz.au!bunyip.cc.uq.oz.au!munnari.OZ.AU!comp.vuw.ac.nz!waikato!auckland.ac.nz!news Mon Feb 12 18:48:17 EST 1996
2Article 23601 of sci.crypt:
3Path: cygnus.mincom.oz.au!minbne.mincom.oz.au!bunyip.cc.uq.oz.au!munnari.OZ.AU!comp.vuw.ac.nz!waikato!auckland.ac.nz!news
4>From: pgut01@cs.auckland.ac.nz (Peter Gutmann)
5Newsgroups: sci.crypt
6Subject: Specification for Ron Rivests Cipher No.2
7Date: 11 Feb 1996 06:45:03 GMT
8Organization: University of Auckland
9Lines: 203
10Sender: pgut01@cs.auckland.ac.nz (Peter Gutmann)
11Message-ID: <4fk39f$f70@net.auckland.ac.nz>
12NNTP-Posting-Host: cs26.cs.auckland.ac.nz
13X-Newsreader: NN version 6.5.0 #3 (NOV)
14
15
16
17
18 Ron Rivest's Cipher No.2
19 ------------------------
20
21Ron Rivest's Cipher No.2 (hereafter referred to as RRC.2, other people may
22refer to it by other names) is word oriented, operating on a block of 64 bits
23divided into four 16-bit words, with a key table of 64 words. All data units
24are little-endian. This functional description of the algorithm is based in
25the paper "The RC5 Encryption Algorithm" (RC5 is a trademark of RSADSI), using
26the same general layout, terminology, and pseudocode style.
27
28
29Notation and RRC.2 Primitive Operations
30
31RRC.2 uses the following primitive operations:
32
331. Two's-complement addition of words, denoted by "+". The inverse operation,
34 subtraction, is denoted by "-".
352. Bitwise exclusive OR, denoted by "^".
363. Bitwise AND, denoted by "&".
374. Bitwise NOT, denoted by "~".
385. A left-rotation of words; the rotation of word x left by y is denoted
39 x <<< y. The inverse operation, right-rotation, is denoted x >>> y.
40
41These operations are directly and efficiently supported by most processors.
42
43
44The RRC.2 Algorithm
45
46RRC.2 consists of three components, a *key expansion* algorithm, an
47*encryption* algorithm, and a *decryption* algorithm.
48
49
50Key Expansion
51
52The purpose of the key-expansion routine is to expand the user's key K to fill
53the expanded key array S, so S resembles an array of random binary words
54determined by the user's secret key K.
55
56Initialising the S-box
57
58RRC.2 uses a single 256-byte S-box derived from the ciphertext contents of
59Beale Cipher No.1 XOR'd with a one-time pad. The Beale Ciphers predate modern
60cryptography by enough time that there should be no concerns about trapdoors
61hidden in the data. They have been published widely, and the S-box can be
62easily recreated from the one-time pad values and the Beale Cipher data taken
63from a standard source. To initialise the S-box:
64
65 for i = 0 to 255 do
66 sBox[ i ] = ( beale[ i ] mod 256 ) ^ pad[ i ]
67
68The contents of Beale Cipher No.1 and the necessary one-time pad are given as
69an appendix at the end of this document. For efficiency, implementors may wish
70to skip the Beale Cipher expansion and store the sBox table directly.
71
72Expanding the Secret Key to 128 Bytes
73
74The secret key is first expanded to fill 128 bytes (64 words). The expansion
75consists of taking the sum of the first and last bytes in the user key, looking
76up the sum (modulo 256) in the S-box, and appending the result to the key. The
77operation is repeated with the second byte and new last byte of the key until
78all 128 bytes have been generated. Note that the following pseudocode treats
79the S array as an array of 128 bytes rather than 64 words.
80
81 for j = 0 to length-1 do
82 S[ j ] = K[ j ]
83 for j = length to 127 do
84 s[ j ] = sBox[ ( S[ j-length ] + S[ j-1 ] ) mod 256 ];
85
86At this point it is possible to perform a truncation of the effective key
87length to ease the creation of espionage-enabled software products. However
88since the author cannot conceive why anyone would want to do this, it will not
89be considered further.
90
91The final phase of the key expansion involves replacing the first byte of S
92with the entry selected from the S-box:
93
94 S[ 0 ] = sBox[ S[ 0 ] ]
95
96
97Encryption
98
99The cipher has 16 full rounds, each divided into 4 subrounds. Two of the full
100rounds perform an additional transformation on the data. Note that the
101following pseudocode treats the S array as an array of 64 words rather than 128
102bytes.
103
104 for i = 0 to 15 do
105 j = i * 4;
106 word0 = ( word0 + ( word1 & ~word3 ) + ( word2 & word3 ) + S[ j+0 ] ) <<< 1
107 word1 = ( word1 + ( word2 & ~word0 ) + ( word3 & word0 ) + S[ j+1 ] ) <<< 2
108 word2 = ( word2 + ( word3 & ~word1 ) + ( word0 & word1 ) + S[ j+2 ] ) <<< 3
109 word3 = ( word3 + ( word0 & ~word2 ) + ( word1 & word2 ) + S[ j+3 ] ) <<< 5
110
111In addition the fifth and eleventh rounds add the contents of the S-box indexed
112by one of the data words to another of the data words following the four
113subrounds as follows:
114
115 word0 = word0 + S[ word3 & 63 ];
116 word1 = word1 + S[ word0 & 63 ];
117 word2 = word2 + S[ word1 & 63 ];
118 word3 = word3 + S[ word2 & 63 ];
119
120
121Decryption
122
123The decryption operation is simply the inverse of the encryption operation.
124Note that the following pseudocode treats the S array as an array of 64 words
125rather than 128 bytes.
126
127 for i = 15 downto 0 do
128 j = i * 4;
129 word3 = ( word3 >>> 5 ) - ( word0 & ~word2 ) - ( word1 & word2 ) - S[ j+3 ]
130 word2 = ( word2 >>> 3 ) - ( word3 & ~word1 ) - ( word0 & word1 ) - S[ j+2 ]
131 word1 = ( word1 >>> 2 ) - ( word2 & ~word0 ) - ( word3 & word0 ) - S[ j+1 ]
132 word0 = ( word0 >>> 1 ) - ( word1 & ~word3 ) - ( word2 & word3 ) - S[ j+0 ]
133
134In addition the fifth and eleventh rounds subtract the contents of the S-box
135indexed by one of the data words from another one of the data words following
136the four subrounds as follows:
137
138 word3 = word3 - S[ word2 & 63 ]
139 word2 = word2 - S[ word1 & 63 ]
140 word1 = word1 - S[ word0 & 63 ]
141 word0 = word0 - S[ word3 & 63 ]
142
143
144Test Vectors
145
146The following test vectors may be used to test the correctness of an RRC.2
147implementation:
148
149 Key: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
151 Plain: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
152 Cipher: 0x1C, 0x19, 0x8A, 0x83, 0x8D, 0xF0, 0x28, 0xB7
153
154 Key: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
156 Plain: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
157 Cipher: 0x21, 0x82, 0x9C, 0x78, 0xA9, 0xF9, 0xC0, 0x74
158
159 Key: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
161 Plain: 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
162 Cipher: 0x13, 0xDB, 0x35, 0x17, 0xD3, 0x21, 0x86, 0x9E
163
164 Key: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
165 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
166 Plain: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
167 Cipher: 0x50, 0xDC, 0x01, 0x62, 0xBD, 0x75, 0x7F, 0x31
168
169
170Appendix: Beale Cipher No.1, "The Locality of the Vault", and One-time Pad for
171 Creating the S-Box
172
173Beale Cipher No.1.
174
175 71, 194, 38,1701, 89, 76, 11, 83,1629, 48, 94, 63, 132, 16, 111, 95,
176 84, 341, 975, 14, 40, 64, 27, 81, 139, 213, 63, 90,1120, 8, 15, 3,
177 126,2018, 40, 74, 758, 485, 604, 230, 436, 664, 582, 150, 251, 284, 308, 231,
178 124, 211, 486, 225, 401, 370, 11, 101, 305, 139, 189, 17, 33, 88, 208, 193,
179 145, 1, 94, 73, 416, 918, 263, 28, 500, 538, 356, 117, 136, 219, 27, 176,
180 130, 10, 460, 25, 485, 18, 436, 65, 84, 200, 283, 118, 320, 138, 36, 416,
181 280, 15, 71, 224, 961, 44, 16, 401, 39, 88, 61, 304, 12, 21, 24, 283,
182 134, 92, 63, 246, 486, 682, 7, 219, 184, 360, 780, 18, 64, 463, 474, 131,
183 160, 79, 73, 440, 95, 18, 64, 581, 34, 69, 128, 367, 460, 17, 81, 12,
184 103, 820, 62, 110, 97, 103, 862, 70, 60,1317, 471, 540, 208, 121, 890, 346,
185 36, 150, 59, 568, 614, 13, 120, 63, 219, 812,2160,1780, 99, 35, 18, 21,
186 136, 872, 15, 28, 170, 88, 4, 30, 44, 112, 18, 147, 436, 195, 320, 37,
187 122, 113, 6, 140, 8, 120, 305, 42, 58, 461, 44, 106, 301, 13, 408, 680,
188 93, 86, 116, 530, 82, 568, 9, 102, 38, 416, 89, 71, 216, 728, 965, 818,
189 2, 38, 121, 195, 14, 326, 148, 234, 18, 55, 131, 234, 361, 824, 5, 81,
190 623, 48, 961, 19, 26, 33, 10,1101, 365, 92, 88, 181, 275, 346, 201, 206
191
192One-time Pad.
193
194 158, 186, 223, 97, 64, 145, 190, 190, 117, 217, 163, 70, 206, 176, 183, 194,
195 146, 43, 248, 141, 3, 54, 72, 223, 233, 153, 91, 210, 36, 131, 244, 161,
196 105, 120, 113, 191, 113, 86, 19, 245, 213, 221, 43, 27, 242, 157, 73, 213,
197 193, 92, 166, 10, 23, 197, 112, 110, 193, 30, 156, 51, 125, 51, 158, 67,
198 197, 215, 59, 218, 110, 246, 181, 0, 135, 76, 164, 97, 47, 87, 234, 108,
199 144, 127, 6, 6, 222, 172, 80, 144, 22, 245, 207, 70, 227, 182, 146, 134,
200 119, 176, 73, 58, 135, 69, 23, 198, 0, 170, 32, 171, 176, 129, 91, 24,
201 126, 77, 248, 0, 118, 69, 57, 60, 190, 171, 217, 61, 136, 169, 196, 84,
202 168, 167, 163, 102, 223, 64, 174, 178, 166, 239, 242, 195, 249, 92, 59, 38,
203 241, 46, 236, 31, 59, 114, 23, 50, 119, 186, 7, 66, 212, 97, 222, 182,
204 230, 118, 122, 86, 105, 92, 179, 243, 255, 189, 223, 164, 194, 215, 98, 44,
205 17, 20, 53, 153, 137, 224, 176, 100, 208, 114, 36, 200, 145, 150, 215, 20,
206 87, 44, 252, 20, 235, 242, 163, 132, 63, 18, 5, 122, 74, 97, 34, 97,
207 142, 86, 146, 221, 179, 166, 161, 74, 69, 182, 88, 120, 128, 58, 76, 155,
208 15, 30, 77, 216, 165, 117, 107, 90, 169, 127, 143, 181, 208, 137, 200, 127,
209 170, 195, 26, 84, 255, 132, 150, 58, 103, 250, 120, 221, 237, 37, 8, 99
210
211
212Implementation
213
214A non-US based programmer who has never seen any encryption code before will
215shortly be implementing RRC.2 based solely on this specification and not on
216knowledge of any other encryption algorithms. Stand by.
217
218
219
diff --git a/src/lib/libcrypto/rc2/version b/src/lib/libcrypto/rc2/version
new file mode 100644
index 0000000000..6f89d595f1
--- /dev/null
+++ b/src/lib/libcrypto/rc2/version
@@ -0,0 +1,22 @@
11.1 23/08/96 - eay
2 Changed RC2_set_key() so it now takes another argument. Many
3 thanks to Peter Gutmann <pgut01@cs.auckland.ac.nz> for the
4 clarification and origional specification of RC2. BSAFE uses
5 this last parameter, 'bits'. It the key is 128 bits, BSAFE
6 also sets this parameter to 128. The old behaviour can be
7 duplicated by setting this parameter to 1024.
8
91.0 08/04/96 - eay
10 First version of SSLeay with rc2. This has been written from the spec
11 posted sci.crypt. It is in this directory under rrc2.doc
12 I have no test values for any mode other than ecb, my wrappers for the
13 other modes should be ok since they are basically the same as
14 the ones taken from idea and des :-). I have implemented them as
15 little-endian operators.
16 While rc2 is included because it is used with SSL, I don't know how
17 far I trust it. It is about the same speed as IDEA and DES.
18 So if you are paranoid, used Tripple DES, else IDEA. If RC2
19 does get used more, perhaps more people will look for weaknesses in
20 it.
21
22
diff --git a/src/lib/libcrypto/rc4/asm/rc4-586.pl b/src/lib/libcrypto/rc4/asm/rc4-586.pl
new file mode 100644
index 0000000000..0dd8eb1ba9
--- /dev/null
+++ b/src/lib/libcrypto/rc4/asm/rc4-586.pl
@@ -0,0 +1,173 @@
1#!/usr/bin/perl
2
3# define for pentium pro friendly version
4
5push(@INC,"perlasm","../../perlasm");
6require "x86asm.pl";
7
8&asm_init($ARGV[0],"rc4-586.pl");
9
10$tx="eax";
11$ty="ebx";
12$x="ecx";
13$y="edx";
14$in="esi";
15$out="edi";
16$d="ebp";
17
18&RC4("RC4");
19
20&asm_finish();
21
22sub RC4_loop
23 {
24 local($n,$p,$char)=@_;
25
26 &comment("Round $n");
27
28 if ($char)
29 {
30 if ($p >= 0)
31 {
32 &mov($ty, &swtmp(2));
33 &cmp($ty, $in);
34 &jle(&label("finished"));
35 &inc($in);
36 }
37 else
38 {
39 &add($ty, 8);
40 &inc($in);
41 &cmp($ty, $in);
42 &jl(&label("finished"));
43 &mov(&swtmp(2), $ty);
44 }
45 }
46 # Moved out
47 # &mov( $tx, &DWP(0,$d,$x,4)) if $p < 0;
48
49 &add( $y, $tx);
50 &and( $y, 0xff);
51 &inc( $x); # NEXT ROUND
52 &mov( $ty, &DWP(0,$d,$y,4));
53 # XXX
54 &mov( &DWP(-4,$d,$x,4),$ty); # AGI
55 &add( $ty, $tx);
56 &and( $x, 0xff); # NEXT ROUND
57 &and( $ty, 0xff);
58 &mov( &DWP(0,$d,$y,4),$tx);
59 &nop();
60 &mov( $ty, &DWP(0,$d,$ty,4));
61 &mov( $tx, &DWP(0,$d,$x,4)) if $p < 1; # NEXT ROUND
62 # XXX
63
64 if (!$char)
65 {
66 #moved up into last round
67 if ($p >= 1)
68 {
69 &add( $out, 8)
70 }
71 &movb( &BP($n,"esp","",0), &LB($ty));
72 }
73 else
74 {
75 # Note in+=8 has occured
76 &movb( &HB($ty), &BP(-1,$in,"",0));
77 # XXX
78 &xorb(&LB($ty), &HB($ty));
79 # XXX
80 &movb(&BP($n,$out,"",0),&LB($ty));
81 }
82 }
83
84
85sub RC4
86 {
87 local($name)=@_;
88
89 &function_begin_B($name,"");
90
91 &comment("");
92
93 &push("ebp");
94 &push("ebx");
95 &mov( $d, &wparam(0)); # key
96 &mov( $ty, &wparam(1)); # num
97 &push("esi");
98 &push("edi");
99
100 &mov( $x, &DWP(0,$d,"",1));
101 &mov( $y, &DWP(4,$d,"",1));
102
103 &mov( $in, &wparam(2));
104 &inc( $x);
105
106 &stack_push(3); # 3 temp variables
107 &add( $d, 8);
108 &and( $x, 0xff);
109
110 &lea( $ty, &DWP(-8,$ty,$in));
111
112 # check for 0 length input
113
114 &mov( $out, &wparam(3));
115 &mov( &swtmp(2), $ty); # this is now address to exit at
116 &mov( $tx, &DWP(0,$d,$x,4));
117
118 &cmp( $ty, $in);
119 &jl( &label("end")); # less than 8 bytes
120
121 &set_label("start");
122
123 # filling DELAY SLOT
124 &add( $in, 8);
125
126 &RC4_loop(0,-1,0);
127 &RC4_loop(1,0,0);
128 &RC4_loop(2,0,0);
129 &RC4_loop(3,0,0);
130 &RC4_loop(4,0,0);
131 &RC4_loop(5,0,0);
132 &RC4_loop(6,0,0);
133 &RC4_loop(7,1,0);
134
135 &comment("apply the cipher text");
136 # xor the cipher data with input
137
138 #&add( $out, 8); #moved up into last round
139
140 &mov( $tx, &swtmp(0));
141 &mov( $ty, &DWP(-8,$in,"",0));
142 &xor( $tx, $ty);
143 &mov( $ty, &DWP(-4,$in,"",0));
144 &mov( &DWP(-8,$out,"",0), $tx);
145 &mov( $tx, &swtmp(1));
146 &xor( $tx, $ty);
147 &mov( $ty, &swtmp(2)); # load end ptr;
148 &mov( &DWP(-4,$out,"",0), $tx);
149 &mov( $tx, &DWP(0,$d,$x,4));
150 &cmp($in, $ty);
151 &jle(&label("start"));
152
153 &set_label("end");
154
155 # There is quite a bit of extra crap in RC4_loop() for this
156 # first round
157 &RC4_loop(0,-1,1);
158 &RC4_loop(1,0,1);
159 &RC4_loop(2,0,1);
160 &RC4_loop(3,0,1);
161 &RC4_loop(4,0,1);
162 &RC4_loop(5,0,1);
163 &RC4_loop(6,1,1);
164
165 &set_label("finished");
166 &dec( $x);
167 &stack_pop(3);
168 &mov( &DWP(-4,$d,"",0),$y);
169 &movb( &BP(-8,$d,"",0),&LB($x));
170
171 &function_end($name);
172 }
173
diff --git a/src/lib/libcrypto/rc4/rc4_enc.c b/src/lib/libcrypto/rc4/rc4_enc.c
new file mode 100644
index 0000000000..ab8a111b52
--- /dev/null
+++ b/src/lib/libcrypto/rc4/rc4_enc.c
@@ -0,0 +1,135 @@
1/* crypto/rc4/rc4_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "rc4.h"
60#include "rc4_locl.h"
61
62/* RC4 as implemented from a posting from
63 * Newsgroups: sci.crypt
64 * From: sterndark@netcom.com (David Sterndark)
65 * Subject: RC4 Algorithm revealed.
66 * Message-ID: <sternCvKL4B.Hyy@netcom.com>
67 * Date: Wed, 14 Sep 1994 06:35:31 GMT
68 */
69
70void RC4(key, len, indata, outdata)
71RC4_KEY *key;
72unsigned long len;
73unsigned char *indata;
74unsigned char *outdata;
75 {
76 register RC4_INT *d;
77 register RC4_INT x,y,tx,ty;
78 int i;
79
80 x=key->x;
81 y=key->y;
82 d=key->data;
83
84#define LOOP(in,out) \
85 x=((x+1)&0xff); \
86 tx=d[x]; \
87 y=(tx+y)&0xff; \
88 d[x]=ty=d[y]; \
89 d[y]=tx; \
90 (out) = d[(tx+ty)&0xff]^ (in);
91
92#ifndef RC4_INDEX
93#define RC4_LOOP(a,b,i) LOOP(*((a)++),*((b)++))
94#else
95#define RC4_LOOP(a,b,i) LOOP(a[i],b[i])
96#endif
97
98 i= -(int)len;
99 i=(int)(len>>3L);
100 if (i)
101 {
102 for (;;)
103 {
104 RC4_LOOP(indata,outdata,0);
105 RC4_LOOP(indata,outdata,1);
106 RC4_LOOP(indata,outdata,2);
107 RC4_LOOP(indata,outdata,3);
108 RC4_LOOP(indata,outdata,4);
109 RC4_LOOP(indata,outdata,5);
110 RC4_LOOP(indata,outdata,6);
111 RC4_LOOP(indata,outdata,7);
112#ifdef RC4_INDEX
113 indata+=8;
114 outdata+=8;
115#endif
116 if (--i == 0) break;
117 }
118 }
119 i=(int)len&0x07;
120 if (i)
121 {
122 for (;;)
123 {
124 RC4_LOOP(indata,outdata,0); if (--i == 0) break;
125 RC4_LOOP(indata,outdata,1); if (--i == 0) break;
126 RC4_LOOP(indata,outdata,2); if (--i == 0) break;
127 RC4_LOOP(indata,outdata,3); if (--i == 0) break;
128 RC4_LOOP(indata,outdata,4); if (--i == 0) break;
129 RC4_LOOP(indata,outdata,5); if (--i == 0) break;
130 RC4_LOOP(indata,outdata,6); if (--i == 0) break;
131 }
132 }
133 key->x=x;
134 key->y=y;
135 }
diff --git a/src/lib/libcrypto/rc4/rc4_skey.c b/src/lib/libcrypto/rc4/rc4_skey.c
new file mode 100644
index 0000000000..0be5fde67b
--- /dev/null
+++ b/src/lib/libcrypto/rc4/rc4_skey.c
@@ -0,0 +1,119 @@
1/* crypto/rc4/rc4_skey.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include "rc4.h"
60#include "rc4_locl.h"
61
62char *RC4_version="RC4 part of SSLeay 0.9.0b 29-Jun-1998";
63
64char *RC4_options()
65 {
66#ifdef RC4_INDEX
67 if (sizeof(RC4_INT) == 1)
68 return("rc4(idx,char)");
69 else
70 return("rc4(idx,int)");
71#else
72 if (sizeof(RC4_INT) == 1)
73 return("rc4(ptr,char)");
74 else
75 return("rc4(ptr,int)");
76#endif
77 }
78
79/* RC4 as implemented from a posting from
80 * Newsgroups: sci.crypt
81 * From: sterndark@netcom.com (David Sterndark)
82 * Subject: RC4 Algorithm revealed.
83 * Message-ID: <sternCvKL4B.Hyy@netcom.com>
84 * Date: Wed, 14 Sep 1994 06:35:31 GMT
85 */
86
87void RC4_set_key(key, len, data)
88RC4_KEY *key;
89int len;
90register unsigned char *data;
91 {
92 register RC4_INT tmp;
93 register int id1,id2;
94 register RC4_INT *d;
95 unsigned int i;
96
97 d= &(key->data[0]);
98 for (i=0; i<256; i++)
99 d[i]=i;
100 key->x = 0;
101 key->y = 0;
102 id1=id2=0;
103
104#define SK_LOOP(n) { \
105 tmp=d[(n)]; \
106 id2 = (data[id1] + tmp + id2) & 0xff; \
107 if (++id1 == len) id1=0; \
108 d[(n)]=d[id2]; \
109 d[id2]=tmp; }
110
111 for (i=0; i < 256; i+=4)
112 {
113 SK_LOOP(i+0);
114 SK_LOOP(i+1);
115 SK_LOOP(i+2);
116 SK_LOOP(i+3);
117 }
118 }
119
diff --git a/src/lib/libcrypto/ripemd/README b/src/lib/libcrypto/ripemd/README
new file mode 100644
index 0000000000..7097707264
--- /dev/null
+++ b/src/lib/libcrypto/ripemd/README
@@ -0,0 +1,15 @@
1RIPEMD-160
2http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html
3
4This is my implementation of RIPEMD-160. The pentium assember is a little
5off the pace since I only get 1050 cycles, while the best is 1013.
6I have a few ideas for how to get another 20 or so cycles, but at
7this point I will not bother right now. I belive the trick will be
8to remove my 'copy X array onto stack' until inside the RIP1() finctions the
9first time round. To do this I need another register and will only have one
10temporary one. A bit tricky.... I can also cleanup the saving of the 5 words
11after the first half of the calculation. I should read the origional
12value, add then write. Currently I just save the new and read the origioal.
13I then read both at the end. Bad.
14
15eric (20-Jan-1998)
diff --git a/src/lib/libcrypto/ripemd/asm/rmd-586.pl b/src/lib/libcrypto/ripemd/asm/rmd-586.pl
new file mode 100644
index 0000000000..dc3f6c792e
--- /dev/null
+++ b/src/lib/libcrypto/ripemd/asm/rmd-586.pl
@@ -0,0 +1,582 @@
1#!/usr/bin/perl
2
3# Normal is the
4# ripemd160_block_x86(MD5_CTX *c, ULONG *X);
5# version, non-normal is the
6# ripemd160_block_x86(MD5_CTX *c, ULONG *X,int blocks);
7
8$normal=0;
9
10push(@INC,"perlasm","../../perlasm");
11require "x86asm.pl";
12
13&asm_init($ARGV[0],$0);
14
15$A="eax";
16$B="ebx";
17$C="ecx";
18$D="edx";
19$E="ebp";
20$tmp1="esi";
21$tmp2="edi";
22
23$KL1=0x5A827999;
24$KL2=0x6ED9EBA1;
25$KL3=0x8F1BBCDC;
26$KL4=0xA953FD4E;
27$KR0=0x50A28BE6;
28$KR1=0x5C4DD124;
29$KR2=0x6D703EF3;
30$KR3=0x7A6D76E9;
31
32
33@wl=( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
34 7, 4,13, 1,10, 6,15, 3,12, 0, 9, 5, 2,14,11, 8,
35 3,10,14, 4, 9,15, 8, 1, 2, 7, 0, 6,13,11, 5,12,
36 1, 9,11,10, 0, 8,12, 4,13, 3, 7,15,14, 5, 6, 2,
37 4, 0, 5, 9, 7,12, 2,10,14, 1, 3, 8,11, 6,15,13,
38 );
39
40@wr=( 5,14, 7, 0, 9, 2,11, 4,13, 6,15, 8, 1,10, 3,12,
41 6,11, 3, 7, 0,13, 5,10,14,15, 8,12, 4, 9, 1, 2,
42 15, 5, 1, 3, 7,14, 6, 9,11, 8,12, 2,10, 0, 4,13,
43 8, 6, 4, 1, 3,11,15, 0, 5,12, 2,13, 9, 7,10,14,
44 12,15,10, 4, 1, 5, 8, 7, 6, 2,13,14, 0, 3, 9,11,
45 );
46
47@sl=( 11,14,15,12, 5, 8, 7, 9,11,13,14,15, 6, 7, 9, 8,
48 7, 6, 8,13,11, 9, 7,15, 7,12,15, 9,11, 7,13,12,
49 11,13, 6, 7,14, 9,13,15,14, 8,13, 6, 5,12, 7, 5,
50 11,12,14,15,14,15, 9, 8, 9,14, 5, 6, 8, 6, 5,12,
51 9,15, 5,11, 6, 8,13,12, 5,12,13,14,11, 8, 5, 6,
52 );
53
54@sr=( 8, 9, 9,11,13,15,15, 5, 7, 7, 8,11,14,14,12, 6,
55 9,13,15, 7,12, 8, 9,11, 7, 7,12, 7, 6,15,13,11,
56 9, 7,15,11, 8, 6, 6,14,12,13, 5,14,13,13, 7, 5,
57 15, 5, 8,11,14,14, 6,14, 6, 9,12, 9,12, 5,15, 8,
58 8, 5,12, 9,12, 5,14, 6, 8,13, 6, 5,15,13,11,11,
59 );
60
61&ripemd160_block("ripemd160_block_x86");
62&asm_finish();
63
64sub Xv
65 {
66 local($n)=@_;
67 return(&swtmp($n+1));
68 # tmp on stack
69 }
70
71sub Np
72 {
73 local($p)=@_;
74 local(%n)=($A,$E,$B,$A,$C,$B,$D,$C,$E,$D);
75 return($n{$p});
76 }
77
78sub RIP1
79 {
80 local($a,$b,$c,$d,$e,$pos,$s,$o,$pos2)=@_;
81
82 &comment($p++);
83 if ($p & 1)
84 {
85 &mov($tmp1, $c) if $o == -1;
86 &xor($tmp1, $d) if $o == -1;
87 &mov($tmp2, &Xv($pos));
88 &xor($tmp1, $b);
89 &add($a, $tmp2);
90 &rotl($c, 10);
91 &add($a, $tmp1);
92 &mov($tmp1, &Np($c)); # NEXT
93 # XXX
94 &rotl($a, $s);
95 &add($a, $e);
96 }
97 else
98 {
99 &xor($tmp1, $d);
100 &mov($tmp2, &Xv($pos));
101 &xor($tmp1, $b);
102 &add($a, $tmp1);
103 &mov($tmp1, &Np($c)) if $o <= 0;
104 &mov($tmp1, -1) if $o == 1;
105 # XXX if $o == 2;
106 &rotl($c, 10);
107 &add($a, $tmp2);
108 &xor($tmp1, &Np($d)) if $o <= 0;
109 &mov($tmp2, &Xv($pos2)) if $o == 1;
110 &mov($tmp2, &wparam(0)) if $o == 2;
111 &rotl($a, $s);
112 &add($a, $e);
113 }
114 }
115
116sub RIP2
117 {
118 local($a,$b,$c,$d,$e,$pos,$pos2,$s,$K,$o)=@_;
119
120# XXXXXX
121 &comment($p++);
122 if ($p & 1)
123 {
124# &mov($tmp2, &Xv($pos)) if $o < -1;
125# &mov($tmp1, -1) if $o < -1;
126
127 &add($a, $tmp2);
128 &mov($tmp2, $c);
129 &sub($tmp1, $b);
130 &and($tmp2, $b);
131 &and($tmp1, $d);
132 &or($tmp2, $tmp1);
133 &mov($tmp1, &Xv($pos2)) if $o <= 0; # XXXXXXXXXXXXXX
134 # XXX
135 &rotl($c, 10);
136 &lea($a, &DWP($K,$a,$tmp2,1));
137 &mov($tmp2, -1) if $o <= 0;
138 # XXX
139 &rotl($a, $s);
140 &add($a, $e);
141 }
142 else
143 {
144 # XXX
145 &add($a, $tmp1);
146 &mov($tmp1, $c);
147 &sub($tmp2, $b);
148 &and($tmp1, $b);
149 &and($tmp2, $d);
150 if ($o != 2)
151 {
152 &or($tmp1, $tmp2);
153 &mov($tmp2, &Xv($pos2)) if $o <= 0;
154 &mov($tmp2, -1) if $o == 1;
155 &rotl($c, 10);
156 &lea($a, &DWP($K,$a,$tmp1,1));
157 &mov($tmp1, -1) if $o <= 0;
158 &sub($tmp2, &Np($c)) if $o == 1;
159 } else {
160 &or($tmp2, $tmp1);
161 &mov($tmp1, &Np($c));
162 &rotl($c, 10);
163 &lea($a, &DWP($K,$a,$tmp2,1));
164 &xor($tmp1, &Np($d));
165 }
166 &rotl($a, $s);
167 &add($a, $e);
168 }
169 }
170
171sub RIP3
172 {
173 local($a,$b,$c,$d,$e,$pos,$s,$K,$o,$pos2)=@_;
174
175 &comment($p++);
176 if ($p & 1)
177 {
178# &mov($tmp2, -1) if $o < -1;
179# &sub($tmp2, $c) if $o < -1;
180 &mov($tmp1, &Xv($pos));
181 &or($tmp2, $b);
182 &add($a, $tmp1);
183 &xor($tmp2, $d);
184 &mov($tmp1, -1) if $o <= 0; # NEXT
185 # XXX
186 &rotl($c, 10);
187 &lea($a, &DWP($K,$a,$tmp2,1));
188 &sub($tmp1, &Np($c)) if $o <= 0; # NEXT
189 # XXX
190 &rotl($a, $s);
191 &add($a, $e);
192 }
193 else
194 {
195 &mov($tmp2, &Xv($pos));
196 &or($tmp1, $b);
197 &add($a, $tmp2);
198 &xor($tmp1, $d);
199 &mov($tmp2, -1) if $o <= 0; # NEXT
200 &mov($tmp2, -1) if $o == 1;
201 &mov($tmp2, &Xv($pos2)) if $o == 2;
202 &rotl($c, 10);
203 &lea($a, &DWP($K,$a,$tmp1,1));
204 &sub($tmp2, &Np($c)) if $o <= 0; # NEXT
205 &mov($tmp1, &Np($d)) if $o == 1;
206 &mov($tmp1, -1) if $o == 2;
207 &rotl($a, $s);
208 &add($a, $e);
209 }
210 }
211
212sub RIP4
213 {
214 local($a,$b,$c,$d,$e,$pos,$s,$K,$o)=@_;
215
216 &comment($p++);
217 if ($p & 1)
218 {
219# &mov($tmp2, -1) if $o == -2;
220# &mov($tmp1, $d) if $o == -2;
221 &sub($tmp2, $d);
222 &and($tmp1, $b);
223 &and($tmp2, $c);
224 &or($tmp2, $tmp1);
225 &mov($tmp1, &Xv($pos));
226 &rotl($c, 10);
227 &lea($a, &DWP($K,$a,$tmp2));
228 &mov($tmp2, -1) unless $o > 0; # NEXT
229 # XXX
230 &add($a, $tmp1);
231 &mov($tmp1, &Np($d)) unless $o > 0; # NEXT
232 # XXX
233 &rotl($a, $s);
234 &add($a, $e);
235 }
236 else
237 {
238 &sub($tmp2, $d);
239 &and($tmp1, $b);
240 &and($tmp2, $c);
241 &or($tmp2, $tmp1);
242 &mov($tmp1, &Xv($pos));
243 &rotl($c, 10);
244 &lea($a, &DWP($K,$a,$tmp2));
245 &mov($tmp2, -1) if $o == 0; # NEXT
246 &mov($tmp2, -1) if $o == 1;
247 &mov($tmp2, -1) if $o == 2;
248 # XXX
249 &add($a, $tmp1);
250 &mov($tmp1, &Np($d)) if $o == 0; # NEXT
251 &sub($tmp2, &Np($d)) if $o == 1;
252 &sub($tmp2, &Np($c)) if $o == 2;
253 # XXX
254 &rotl($a, $s);
255 &add($a, $e);
256 }
257 }
258
259sub RIP5
260 {
261 local($a,$b,$c,$d,$e,$pos,$s,$K,$o)=@_;
262
263 &comment($p++);
264 if ($p & 1)
265 {
266 &mov($tmp2, -1) if $o == -2;
267 &sub($tmp2, $d) if $o == -2;
268 &mov($tmp1, &Xv($pos));
269 &or($tmp2, $c);
270 &add($a, $tmp1);
271 &xor($tmp2, $b);
272 &mov($tmp1, -1) if $o <= 0;
273 # XXX
274 &rotl($c, 10);
275 &lea($a, &DWP($K,$a,$tmp2,1));
276 &sub($tmp1, &Np($d)) if $o <= 0;
277 # XXX
278 &rotl($a, $s);
279 &add($a, $e);
280 }
281 else
282 {
283 &mov($tmp2, &Xv($pos));
284 &or($tmp1, $c);
285 &add($a, $tmp2);
286 &xor($tmp1, $b);
287 &mov($tmp2, -1) if $o <= 0;
288 &mov($tmp2, &wparam(0)) if $o == 1; # Middle code
289 &mov($tmp2, -1) if $o == 2;
290 &rotl($c, 10);
291 &lea($a, &DWP($K,$a,$tmp1,1));
292 &sub($tmp2, &Np($d)) if $o <= 0;
293 &mov(&swtmp(1+16), $A) if $o == 1;
294 &mov($tmp1, &Np($d)) if $o == 2;
295 &rotl($a, $s);
296 &add($a, $e);
297 }
298 }
299
300sub ripemd160_block
301 {
302 local($name)=@_;
303
304 &function_begin_B($name,"",3);
305
306 # parameter 1 is the RIPEMD160_CTX structure.
307 # A 0
308 # B 4
309 # C 8
310 # D 12
311 # E 16
312
313 &push("esi");
314 &mov($C, &wparam(2));
315 &push("edi");
316 &mov($tmp1, &wparam(1)); # edi
317 &push("ebp");
318 &add($C, $tmp1); # offset we end at
319 &push("ebx");
320 &sub($C, 64);
321 &stack_push(16+5+1);
322 # XXX
323
324 &mov(&swtmp(0), $C);
325 &mov($tmp2, &wparam(0)); # Done at end of loop
326
327 &set_label("start") unless $normal;
328 &comment("");
329
330 # &mov($tmp1, &wparam(1)); # Done at end of loop
331 # &mov($tmp2, &wparam(0)); # Done at end of loop
332
333 for ($z=0; $z<16; $z+=2)
334 {
335 &mov($A, &DWP( $z*4,$tmp1,"",0));
336 &mov($B, &DWP( ($z+1)*4,$tmp1,"",0));
337 &mov(&swtmp(1+$z), $A);
338 &mov(&swtmp(1+$z+1), $B);
339 }
340 &add($tmp1, 64);
341 &mov($A, &DWP( 0,$tmp2,"",0));
342 &mov(&wparam(1),$tmp1);
343 &mov($B, &DWP( 4,$tmp2,"",0));
344 &mov($C, &DWP( 8,$tmp2,"",0));
345 &mov($D, &DWP(12,$tmp2,"",0));
346 &mov($E, &DWP(16,$tmp2,"",0));
347
348 &RIP1($A,$B,$C,$D,$E,$wl[ 0],$sl[ 0],-1);
349 &RIP1($E,$A,$B,$C,$D,$wl[ 1],$sl[ 1],0);
350 &RIP1($D,$E,$A,$B,$C,$wl[ 2],$sl[ 2],0);
351 &RIP1($C,$D,$E,$A,$B,$wl[ 3],$sl[ 3],0);
352 &RIP1($B,$C,$D,$E,$A,$wl[ 4],$sl[ 4],0);
353 &RIP1($A,$B,$C,$D,$E,$wl[ 5],$sl[ 5],0);
354 &RIP1($E,$A,$B,$C,$D,$wl[ 6],$sl[ 6],0);
355 &RIP1($D,$E,$A,$B,$C,$wl[ 7],$sl[ 7],0);
356 &RIP1($C,$D,$E,$A,$B,$wl[ 8],$sl[ 8],0);
357 &RIP1($B,$C,$D,$E,$A,$wl[ 9],$sl[ 9],0);
358 &RIP1($A,$B,$C,$D,$E,$wl[10],$sl[10],0);
359 &RIP1($E,$A,$B,$C,$D,$wl[11],$sl[11],0);
360 &RIP1($D,$E,$A,$B,$C,$wl[12],$sl[12],0);
361 &RIP1($C,$D,$E,$A,$B,$wl[13],$sl[13],0);
362 &RIP1($B,$C,$D,$E,$A,$wl[14],$sl[14],0);
363 &RIP1($A,$B,$C,$D,$E,$wl[15],$sl[15],1,$wl[16]);
364
365 &RIP2($E,$A,$B,$C,$D,$wl[16],$wl[17],$sl[16],$KL1,-1);
366 &RIP2($D,$E,$A,$B,$C,$wl[17],$wl[18],$sl[17],$KL1,0);
367 &RIP2($C,$D,$E,$A,$B,$wl[18],$wl[19],$sl[18],$KL1,0);
368 &RIP2($B,$C,$D,$E,$A,$wl[19],$wl[20],$sl[19],$KL1,0);
369 &RIP2($A,$B,$C,$D,$E,$wl[20],$wl[21],$sl[20],$KL1,0);
370 &RIP2($E,$A,$B,$C,$D,$wl[21],$wl[22],$sl[21],$KL1,0);
371 &RIP2($D,$E,$A,$B,$C,$wl[22],$wl[23],$sl[22],$KL1,0);
372 &RIP2($C,$D,$E,$A,$B,$wl[23],$wl[24],$sl[23],$KL1,0);
373 &RIP2($B,$C,$D,$E,$A,$wl[24],$wl[25],$sl[24],$KL1,0);
374 &RIP2($A,$B,$C,$D,$E,$wl[25],$wl[26],$sl[25],$KL1,0);
375 &RIP2($E,$A,$B,$C,$D,$wl[26],$wl[27],$sl[26],$KL1,0);
376 &RIP2($D,$E,$A,$B,$C,$wl[27],$wl[28],$sl[27],$KL1,0);
377 &RIP2($C,$D,$E,$A,$B,$wl[28],$wl[29],$sl[28],$KL1,0);
378 &RIP2($B,$C,$D,$E,$A,$wl[29],$wl[30],$sl[29],$KL1,0);
379 &RIP2($A,$B,$C,$D,$E,$wl[30],$wl[31],$sl[30],$KL1,0);
380 &RIP2($E,$A,$B,$C,$D,$wl[31],$wl[32],$sl[31],$KL1,1);
381
382 &RIP3($D,$E,$A,$B,$C,$wl[32],$sl[32],$KL2,-1);
383 &RIP3($C,$D,$E,$A,$B,$wl[33],$sl[33],$KL2,0);
384 &RIP3($B,$C,$D,$E,$A,$wl[34],$sl[34],$KL2,0);
385 &RIP3($A,$B,$C,$D,$E,$wl[35],$sl[35],$KL2,0);
386 &RIP3($E,$A,$B,$C,$D,$wl[36],$sl[36],$KL2,0);
387 &RIP3($D,$E,$A,$B,$C,$wl[37],$sl[37],$KL2,0);
388 &RIP3($C,$D,$E,$A,$B,$wl[38],$sl[38],$KL2,0);
389 &RIP3($B,$C,$D,$E,$A,$wl[39],$sl[39],$KL2,0);
390 &RIP3($A,$B,$C,$D,$E,$wl[40],$sl[40],$KL2,0);
391 &RIP3($E,$A,$B,$C,$D,$wl[41],$sl[41],$KL2,0);
392 &RIP3($D,$E,$A,$B,$C,$wl[42],$sl[42],$KL2,0);
393 &RIP3($C,$D,$E,$A,$B,$wl[43],$sl[43],$KL2,0);
394 &RIP3($B,$C,$D,$E,$A,$wl[44],$sl[44],$KL2,0);
395 &RIP3($A,$B,$C,$D,$E,$wl[45],$sl[45],$KL2,0);
396 &RIP3($E,$A,$B,$C,$D,$wl[46],$sl[46],$KL2,0);
397 &RIP3($D,$E,$A,$B,$C,$wl[47],$sl[47],$KL2,1);
398
399 &RIP4($C,$D,$E,$A,$B,$wl[48],$sl[48],$KL3,-1);
400 &RIP4($B,$C,$D,$E,$A,$wl[49],$sl[49],$KL3,0);
401 &RIP4($A,$B,$C,$D,$E,$wl[50],$sl[50],$KL3,0);
402 &RIP4($E,$A,$B,$C,$D,$wl[51],$sl[51],$KL3,0);
403 &RIP4($D,$E,$A,$B,$C,$wl[52],$sl[52],$KL3,0);
404 &RIP4($C,$D,$E,$A,$B,$wl[53],$sl[53],$KL3,0);
405 &RIP4($B,$C,$D,$E,$A,$wl[54],$sl[54],$KL3,0);
406 &RIP4($A,$B,$C,$D,$E,$wl[55],$sl[55],$KL3,0);
407 &RIP4($E,$A,$B,$C,$D,$wl[56],$sl[56],$KL3,0);
408 &RIP4($D,$E,$A,$B,$C,$wl[57],$sl[57],$KL3,0);
409 &RIP4($C,$D,$E,$A,$B,$wl[58],$sl[58],$KL3,0);
410 &RIP4($B,$C,$D,$E,$A,$wl[59],$sl[59],$KL3,0);
411 &RIP4($A,$B,$C,$D,$E,$wl[60],$sl[60],$KL3,0);
412 &RIP4($E,$A,$B,$C,$D,$wl[61],$sl[61],$KL3,0);
413 &RIP4($D,$E,$A,$B,$C,$wl[62],$sl[62],$KL3,0);
414 &RIP4($C,$D,$E,$A,$B,$wl[63],$sl[63],$KL3,1);
415
416 &RIP5($B,$C,$D,$E,$A,$wl[64],$sl[64],$KL4,-1);
417 &RIP5($A,$B,$C,$D,$E,$wl[65],$sl[65],$KL4,0);
418 &RIP5($E,$A,$B,$C,$D,$wl[66],$sl[66],$KL4,0);
419 &RIP5($D,$E,$A,$B,$C,$wl[67],$sl[67],$KL4,0);
420 &RIP5($C,$D,$E,$A,$B,$wl[68],$sl[68],$KL4,0);
421 &RIP5($B,$C,$D,$E,$A,$wl[69],$sl[69],$KL4,0);
422 &RIP5($A,$B,$C,$D,$E,$wl[70],$sl[70],$KL4,0);
423 &RIP5($E,$A,$B,$C,$D,$wl[71],$sl[71],$KL4,0);
424 &RIP5($D,$E,$A,$B,$C,$wl[72],$sl[72],$KL4,0);
425 &RIP5($C,$D,$E,$A,$B,$wl[73],$sl[73],$KL4,0);
426 &RIP5($B,$C,$D,$E,$A,$wl[74],$sl[74],$KL4,0);
427 &RIP5($A,$B,$C,$D,$E,$wl[75],$sl[75],$KL4,0);
428 &RIP5($E,$A,$B,$C,$D,$wl[76],$sl[76],$KL4,0);
429 &RIP5($D,$E,$A,$B,$C,$wl[77],$sl[77],$KL4,0);
430 &RIP5($C,$D,$E,$A,$B,$wl[78],$sl[78],$KL4,0);
431 &RIP5($B,$C,$D,$E,$A,$wl[79],$sl[79],$KL4,1);
432
433 # &mov($tmp2, &wparam(0)); # moved into last RIP5
434 # &mov(&swtmp(1+16), $A);
435 &mov($A, &DWP( 0,$tmp2,"",0));
436 &mov(&swtmp(1+17), $B);
437 &mov(&swtmp(1+18), $C);
438 &mov($B, &DWP( 4,$tmp2,"",0));
439 &mov(&swtmp(1+19), $D);
440 &mov($C, &DWP( 8,$tmp2,"",0));
441 &mov(&swtmp(1+20), $E);
442 &mov($D, &DWP(12,$tmp2,"",0));
443 &mov($E, &DWP(16,$tmp2,"",0));
444
445 &RIP5($A,$B,$C,$D,$E,$wr[ 0],$sr[ 0],$KR0,-2);
446 &RIP5($E,$A,$B,$C,$D,$wr[ 1],$sr[ 1],$KR0,0);
447 &RIP5($D,$E,$A,$B,$C,$wr[ 2],$sr[ 2],$KR0,0);
448 &RIP5($C,$D,$E,$A,$B,$wr[ 3],$sr[ 3],$KR0,0);
449 &RIP5($B,$C,$D,$E,$A,$wr[ 4],$sr[ 4],$KR0,0);
450 &RIP5($A,$B,$C,$D,$E,$wr[ 5],$sr[ 5],$KR0,0);
451 &RIP5($E,$A,$B,$C,$D,$wr[ 6],$sr[ 6],$KR0,0);
452 &RIP5($D,$E,$A,$B,$C,$wr[ 7],$sr[ 7],$KR0,0);
453 &RIP5($C,$D,$E,$A,$B,$wr[ 8],$sr[ 8],$KR0,0);
454 &RIP5($B,$C,$D,$E,$A,$wr[ 9],$sr[ 9],$KR0,0);
455 &RIP5($A,$B,$C,$D,$E,$wr[10],$sr[10],$KR0,0);
456 &RIP5($E,$A,$B,$C,$D,$wr[11],$sr[11],$KR0,0);
457 &RIP5($D,$E,$A,$B,$C,$wr[12],$sr[12],$KR0,0);
458 &RIP5($C,$D,$E,$A,$B,$wr[13],$sr[13],$KR0,0);
459 &RIP5($B,$C,$D,$E,$A,$wr[14],$sr[14],$KR0,0);
460 &RIP5($A,$B,$C,$D,$E,$wr[15],$sr[15],$KR0,2);
461
462 &RIP4($E,$A,$B,$C,$D,$wr[16],$sr[16],$KR1,-2);
463 &RIP4($D,$E,$A,$B,$C,$wr[17],$sr[17],$KR1,0);
464 &RIP4($C,$D,$E,$A,$B,$wr[18],$sr[18],$KR1,0);
465 &RIP4($B,$C,$D,$E,$A,$wr[19],$sr[19],$KR1,0);
466 &RIP4($A,$B,$C,$D,$E,$wr[20],$sr[20],$KR1,0);
467 &RIP4($E,$A,$B,$C,$D,$wr[21],$sr[21],$KR1,0);
468 &RIP4($D,$E,$A,$B,$C,$wr[22],$sr[22],$KR1,0);
469 &RIP4($C,$D,$E,$A,$B,$wr[23],$sr[23],$KR1,0);
470 &RIP4($B,$C,$D,$E,$A,$wr[24],$sr[24],$KR1,0);
471 &RIP4($A,$B,$C,$D,$E,$wr[25],$sr[25],$KR1,0);
472 &RIP4($E,$A,$B,$C,$D,$wr[26],$sr[26],$KR1,0);
473 &RIP4($D,$E,$A,$B,$C,$wr[27],$sr[27],$KR1,0);
474 &RIP4($C,$D,$E,$A,$B,$wr[28],$sr[28],$KR1,0);
475 &RIP4($B,$C,$D,$E,$A,$wr[29],$sr[29],$KR1,0);
476 &RIP4($A,$B,$C,$D,$E,$wr[30],$sr[30],$KR1,0);
477 &RIP4($E,$A,$B,$C,$D,$wr[31],$sr[31],$KR1,2);
478
479 &RIP3($D,$E,$A,$B,$C,$wr[32],$sr[32],$KR2,-2);
480 &RIP3($C,$D,$E,$A,$B,$wr[33],$sr[33],$KR2,0);
481 &RIP3($B,$C,$D,$E,$A,$wr[34],$sr[34],$KR2,0);
482 &RIP3($A,$B,$C,$D,$E,$wr[35],$sr[35],$KR2,0);
483 &RIP3($E,$A,$B,$C,$D,$wr[36],$sr[36],$KR2,0);
484 &RIP3($D,$E,$A,$B,$C,$wr[37],$sr[37],$KR2,0);
485 &RIP3($C,$D,$E,$A,$B,$wr[38],$sr[38],$KR2,0);
486 &RIP3($B,$C,$D,$E,$A,$wr[39],$sr[39],$KR2,0);
487 &RIP3($A,$B,$C,$D,$E,$wr[40],$sr[40],$KR2,0);
488 &RIP3($E,$A,$B,$C,$D,$wr[41],$sr[41],$KR2,0);
489 &RIP3($D,$E,$A,$B,$C,$wr[42],$sr[42],$KR2,0);
490 &RIP3($C,$D,$E,$A,$B,$wr[43],$sr[43],$KR2,0);
491 &RIP3($B,$C,$D,$E,$A,$wr[44],$sr[44],$KR2,0);
492 &RIP3($A,$B,$C,$D,$E,$wr[45],$sr[45],$KR2,0);
493 &RIP3($E,$A,$B,$C,$D,$wr[46],$sr[46],$KR2,0);
494 &RIP3($D,$E,$A,$B,$C,$wr[47],$sr[47],$KR2,2,$wr[48]);
495
496 &RIP2($C,$D,$E,$A,$B,$wr[48],$wr[49],$sr[48],$KR3,-2);
497 &RIP2($B,$C,$D,$E,$A,$wr[49],$wr[50],$sr[49],$KR3,0);
498 &RIP2($A,$B,$C,$D,$E,$wr[50],$wr[51],$sr[50],$KR3,0);
499 &RIP2($E,$A,$B,$C,$D,$wr[51],$wr[52],$sr[51],$KR3,0);
500 &RIP2($D,$E,$A,$B,$C,$wr[52],$wr[53],$sr[52],$KR3,0);
501 &RIP2($C,$D,$E,$A,$B,$wr[53],$wr[54],$sr[53],$KR3,0);
502 &RIP2($B,$C,$D,$E,$A,$wr[54],$wr[55],$sr[54],$KR3,0);
503 &RIP2($A,$B,$C,$D,$E,$wr[55],$wr[56],$sr[55],$KR3,0);
504 &RIP2($E,$A,$B,$C,$D,$wr[56],$wr[57],$sr[56],$KR3,0);
505 &RIP2($D,$E,$A,$B,$C,$wr[57],$wr[58],$sr[57],$KR3,0);
506 &RIP2($C,$D,$E,$A,$B,$wr[58],$wr[59],$sr[58],$KR3,0);
507 &RIP2($B,$C,$D,$E,$A,$wr[59],$wr[60],$sr[59],$KR3,0);
508 &RIP2($A,$B,$C,$D,$E,$wr[60],$wr[61],$sr[60],$KR3,0);
509 &RIP2($E,$A,$B,$C,$D,$wr[61],$wr[62],$sr[61],$KR3,0);
510 &RIP2($D,$E,$A,$B,$C,$wr[62],$wr[63],$sr[62],$KR3,0);
511 &RIP2($C,$D,$E,$A,$B,$wr[63],$wr[64],$sr[63],$KR3,2);
512
513 &RIP1($B,$C,$D,$E,$A,$wr[64],$sr[64],-2);
514 &RIP1($A,$B,$C,$D,$E,$wr[65],$sr[65],0);
515 &RIP1($E,$A,$B,$C,$D,$wr[66],$sr[66],0);
516 &RIP1($D,$E,$A,$B,$C,$wr[67],$sr[67],0);
517 &RIP1($C,$D,$E,$A,$B,$wr[68],$sr[68],0);
518 &RIP1($B,$C,$D,$E,$A,$wr[69],$sr[69],0);
519 &RIP1($A,$B,$C,$D,$E,$wr[70],$sr[70],0);
520 &RIP1($E,$A,$B,$C,$D,$wr[71],$sr[71],0);
521 &RIP1($D,$E,$A,$B,$C,$wr[72],$sr[72],0);
522 &RIP1($C,$D,$E,$A,$B,$wr[73],$sr[73],0);
523 &RIP1($B,$C,$D,$E,$A,$wr[74],$sr[74],0);
524 &RIP1($A,$B,$C,$D,$E,$wr[75],$sr[75],0);
525 &RIP1($E,$A,$B,$C,$D,$wr[76],$sr[76],0);
526 &RIP1($D,$E,$A,$B,$C,$wr[77],$sr[77],0);
527 &RIP1($C,$D,$E,$A,$B,$wr[78],$sr[78],0);
528 &RIP1($B,$C,$D,$E,$A,$wr[79],$sr[79],2);
529
530 # &mov($tmp2, &wparam(0)); # Moved into last round
531
532 &mov($tmp1, &DWP( 4,$tmp2,"",0)); # ctx->B
533 &add($D, $tmp1);
534 &mov($tmp1, &swtmp(1+18)); # $c
535 &add($D, $tmp1);
536
537 &mov($tmp1, &DWP( 8,$tmp2,"",0)); # ctx->C
538 &add($E, $tmp1);
539 &mov($tmp1, &swtmp(1+19)); # $d
540 &add($E, $tmp1);
541
542 &mov($tmp1, &DWP(12,$tmp2,"",0)); # ctx->D
543 &add($A, $tmp1);
544 &mov($tmp1, &swtmp(1+20)); # $e
545 &add($A, $tmp1);
546
547
548 &mov($tmp1, &DWP(16,$tmp2,"",0)); # ctx->E
549 &add($B, $tmp1);
550 &mov($tmp1, &swtmp(1+16)); # $a
551 &add($B, $tmp1);
552
553 &mov($tmp1, &DWP( 0,$tmp2,"",0)); # ctx->A
554 &add($C, $tmp1);
555 &mov($tmp1, &swtmp(1+17)); # $b
556 &add($C, $tmp1);
557
558 &mov(&DWP( 0,$tmp2,"",0), $D);
559 &mov(&DWP( 4,$tmp2,"",0), $E);
560 &mov(&DWP( 8,$tmp2,"",0), $A);
561 &mov(&DWP(12,$tmp2,"",0), $B);
562 &mov(&DWP(16,$tmp2,"",0), $C);
563
564 &mov($tmp2, &swtmp(0));
565 &mov($tmp1, &wparam(1));
566
567 &cmp($tmp2,$tmp1);
568 &mov($tmp2, &wparam(0));
569
570 # XXX
571 &jge(&label("start"));
572
573 &stack_pop(16+5+1);
574
575 &pop("ebx");
576 &pop("ebp");
577 &pop("edi");
578 &pop("esi");
579 &ret();
580 &function_end_B($name);
581 }
582
diff --git a/src/lib/libcrypto/ripemd/ripemd.h b/src/lib/libcrypto/ripemd/ripemd.h
new file mode 100644
index 0000000000..a3bc6e3ab2
--- /dev/null
+++ b/src/lib/libcrypto/ripemd/ripemd.h
@@ -0,0 +1,99 @@
1/* crypto/ripemd/ripemd.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_RIPEMD_H
60#define HEADER_RIPEMD_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#define RIPEMD160_CBLOCK 64
67#define RIPEMD160_LBLOCK 16
68#define RIPEMD160_BLOCK 16
69#define RIPEMD160_LAST_BLOCK 56
70#define RIPEMD160_LENGTH_BLOCK 8
71#define RIPEMD160_DIGEST_LENGTH 20
72
73typedef struct RIPEMD160state_st
74 {
75 unsigned long A,B,C,D,E;
76 unsigned long Nl,Nh;
77 unsigned long data[RIPEMD160_LBLOCK];
78 int num;
79 } RIPEMD160_CTX;
80
81#ifndef NOPROTO
82void RIPEMD160_Init(RIPEMD160_CTX *c);
83void RIPEMD160_Update(RIPEMD160_CTX *c, unsigned char *data, unsigned long len);
84void RIPEMD160_Final(unsigned char *md, RIPEMD160_CTX *c);
85unsigned char *RIPEMD160(unsigned char *d, unsigned long n, unsigned char *md);
86void RIPEMD160_Transform(RIPEMD160_CTX *c, unsigned char *b);
87#else
88void RIPEMD160_Init();
89void RIPEMD160_Update();
90void RIPEMD160_Final();
91unsigned char *RIPEMD160();
92void RIPEMD160_Transform();
93#endif
94
95#ifdef __cplusplus
96}
97#endif
98
99#endif
diff --git a/src/lib/libcrypto/ripemd/rmd_dgst.c b/src/lib/libcrypto/ripemd/rmd_dgst.c
new file mode 100644
index 0000000000..210de1977d
--- /dev/null
+++ b/src/lib/libcrypto/ripemd/rmd_dgst.c
@@ -0,0 +1,535 @@
1/* crypto/ripemd/rmd_dgst.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "rmd_locl.h"
61
62char *RMD160_version="RIPEMD160 part of SSLeay 0.9.0b 29-Jun-1998";
63
64#ifndef NOPROTO
65# ifdef RMD160_ASM
66 void ripemd160_block_x86(RIPEMD160_CTX *c, unsigned long *p,int num);
67# define ripemd160_block ripemd160_block_x86
68# else
69 void ripemd160_block(RIPEMD160_CTX *c, unsigned long *p,int num);
70# endif
71#else
72# ifdef RMD160_ASM
73 void ripemd160_block_x86();
74# define ripemd160_block ripemd160_block_x86
75# else
76 static void ripemd160_block();
77# endif
78#endif
79
80void RIPEMD160_Init(c)
81RIPEMD160_CTX *c;
82 {
83 c->A=RIPEMD160_A;
84 c->B=RIPEMD160_B;
85 c->C=RIPEMD160_C;
86 c->D=RIPEMD160_D;
87 c->E=RIPEMD160_E;
88 c->Nl=0;
89 c->Nh=0;
90 c->num=0;
91 }
92
93void RIPEMD160_Update(c, data, len)
94RIPEMD160_CTX *c;
95register unsigned char *data;
96unsigned long len;
97 {
98 register ULONG *p;
99 int sw,sc;
100 ULONG l;
101
102 if (len == 0) return;
103
104 l=(c->Nl+(len<<3))&0xffffffffL;
105 if (l < c->Nl) /* overflow */
106 c->Nh++;
107 c->Nh+=(len>>29);
108 c->Nl=l;
109
110 if (c->num != 0)
111 {
112 p=c->data;
113 sw=c->num>>2;
114 sc=c->num&0x03;
115
116 if ((c->num+len) >= RIPEMD160_CBLOCK)
117 {
118 l= p[sw];
119 p_c2l(data,l,sc);
120 p[sw++]=l;
121 for (; sw<RIPEMD160_LBLOCK; sw++)
122 {
123 c2l(data,l);
124 p[sw]=l;
125 }
126 len-=(RIPEMD160_CBLOCK-c->num);
127
128 ripemd160_block(c,p,64);
129 c->num=0;
130 /* drop through and do the rest */
131 }
132 else
133 {
134 int ew,ec;
135
136 c->num+=(int)len;
137 if ((sc+len) < 4) /* ugly, add char's to a word */
138 {
139 l= p[sw];
140 p_c2l_p(data,l,sc,len);
141 p[sw]=l;
142 }
143 else
144 {
145 ew=(c->num>>2);
146 ec=(c->num&0x03);
147 l= p[sw];
148 p_c2l(data,l,sc);
149 p[sw++]=l;
150 for (; sw < ew; sw++)
151 { c2l(data,l); p[sw]=l; }
152 if (ec)
153 {
154 c2l_p(data,l,ec);
155 p[sw]=l;
156 }
157 }
158 return;
159 }
160 }
161 /* we now can process the input data in blocks of RIPEMD160_CBLOCK
162 * chars and save the leftovers to c->data. */
163#ifdef L_ENDIAN
164 if ((((unsigned long)data)%sizeof(ULONG)) == 0)
165 {
166 sw=(int)len/RIPEMD160_CBLOCK;
167 if (sw > 0)
168 {
169 sw*=RIPEMD160_CBLOCK;
170 ripemd160_block(c,(ULONG *)data,sw);
171 data+=sw;
172 len-=sw;
173 }
174 }
175#endif
176 p=c->data;
177 while (len >= RIPEMD160_CBLOCK)
178 {
179#if defined(L_ENDIAN) || defined(B_ENDIAN)
180 if (p != (unsigned long *)data)
181 memcpy(p,data,RIPEMD160_CBLOCK);
182 data+=RIPEMD160_CBLOCK;
183#ifdef B_ENDIAN
184 for (sw=(RIPEMD160_LBLOCK/4); sw; sw--)
185 {
186 Endian_Reverse32(p[0]);
187 Endian_Reverse32(p[1]);
188 Endian_Reverse32(p[2]);
189 Endian_Reverse32(p[3]);
190 p+=4;
191 }
192#endif
193#else
194 for (sw=(RIPEMD160_LBLOCK/4); sw; sw--)
195 {
196 c2l(data,l); *(p++)=l;
197 c2l(data,l); *(p++)=l;
198 c2l(data,l); *(p++)=l;
199 c2l(data,l); *(p++)=l;
200 }
201#endif
202 p=c->data;
203 ripemd160_block(c,p,64);
204 len-=RIPEMD160_CBLOCK;
205 }
206 sc=(int)len;
207 c->num=sc;
208 if (sc)
209 {
210 sw=sc>>2; /* words to copy */
211#ifdef L_ENDIAN
212 p[sw]=0;
213 memcpy(p,data,sc);
214#else
215 sc&=0x03;
216 for ( ; sw; sw--)
217 { c2l(data,l); *(p++)=l; }
218 c2l_p(data,l,sc);
219 *p=l;
220#endif
221 }
222 }
223
224void RIPEMD160_Transform(c,b)
225RIPEMD160_CTX *c;
226unsigned char *b;
227 {
228 ULONG p[16];
229#if !defined(L_ENDIAN)
230 ULONG *q;
231 int i;
232#endif
233
234#if defined(B_ENDIAN) || defined(L_ENDIAN)
235 memcpy(p,b,64);
236#ifdef B_ENDIAN
237 q=p;
238 for (i=(RIPEMD160_LBLOCK/4); i; i--)
239 {
240 Endian_Reverse32(q[0]);
241 Endian_Reverse32(q[1]);
242 Endian_Reverse32(q[2]);
243 Endian_Reverse32(q[3]);
244 q+=4;
245 }
246#endif
247#else
248 q=p;
249 for (i=(RIPEMD160_LBLOCK/4); i; i--)
250 {
251 ULONG l;
252 c2l(b,l); *(q++)=l;
253 c2l(b,l); *(q++)=l;
254 c2l(b,l); *(q++)=l;
255 c2l(b,l); *(q++)=l;
256 }
257#endif
258 ripemd160_block(c,p,64);
259 }
260
261#ifndef RMD160_ASM
262
263void ripemd160_block(ctx, X, num)
264RIPEMD160_CTX *ctx;
265register ULONG *X;
266int num;
267 {
268 register ULONG A,B,C,D,E;
269 ULONG a,b,c,d,e;
270
271 for (;;)
272 {
273 A=ctx->A; B=ctx->B; C=ctx->C; D=ctx->D; E=ctx->E;
274
275 RIP1(A,B,C,D,E,WL00,SL00);
276 RIP1(E,A,B,C,D,WL01,SL01);
277 RIP1(D,E,A,B,C,WL02,SL02);
278 RIP1(C,D,E,A,B,WL03,SL03);
279 RIP1(B,C,D,E,A,WL04,SL04);
280 RIP1(A,B,C,D,E,WL05,SL05);
281 RIP1(E,A,B,C,D,WL06,SL06);
282 RIP1(D,E,A,B,C,WL07,SL07);
283 RIP1(C,D,E,A,B,WL08,SL08);
284 RIP1(B,C,D,E,A,WL09,SL09);
285 RIP1(A,B,C,D,E,WL10,SL10);
286 RIP1(E,A,B,C,D,WL11,SL11);
287 RIP1(D,E,A,B,C,WL12,SL12);
288 RIP1(C,D,E,A,B,WL13,SL13);
289 RIP1(B,C,D,E,A,WL14,SL14);
290 RIP1(A,B,C,D,E,WL15,SL15);
291
292 RIP2(E,A,B,C,D,WL16,SL16,KL1);
293 RIP2(D,E,A,B,C,WL17,SL17,KL1);
294 RIP2(C,D,E,A,B,WL18,SL18,KL1);
295 RIP2(B,C,D,E,A,WL19,SL19,KL1);
296 RIP2(A,B,C,D,E,WL20,SL20,KL1);
297 RIP2(E,A,B,C,D,WL21,SL21,KL1);
298 RIP2(D,E,A,B,C,WL22,SL22,KL1);
299 RIP2(C,D,E,A,B,WL23,SL23,KL1);
300 RIP2(B,C,D,E,A,WL24,SL24,KL1);
301 RIP2(A,B,C,D,E,WL25,SL25,KL1);
302 RIP2(E,A,B,C,D,WL26,SL26,KL1);
303 RIP2(D,E,A,B,C,WL27,SL27,KL1);
304 RIP2(C,D,E,A,B,WL28,SL28,KL1);
305 RIP2(B,C,D,E,A,WL29,SL29,KL1);
306 RIP2(A,B,C,D,E,WL30,SL30,KL1);
307 RIP2(E,A,B,C,D,WL31,SL31,KL1);
308
309 RIP3(D,E,A,B,C,WL32,SL32,KL2);
310 RIP3(C,D,E,A,B,WL33,SL33,KL2);
311 RIP3(B,C,D,E,A,WL34,SL34,KL2);
312 RIP3(A,B,C,D,E,WL35,SL35,KL2);
313 RIP3(E,A,B,C,D,WL36,SL36,KL2);
314 RIP3(D,E,A,B,C,WL37,SL37,KL2);
315 RIP3(C,D,E,A,B,WL38,SL38,KL2);
316 RIP3(B,C,D,E,A,WL39,SL39,KL2);
317 RIP3(A,B,C,D,E,WL40,SL40,KL2);
318 RIP3(E,A,B,C,D,WL41,SL41,KL2);
319 RIP3(D,E,A,B,C,WL42,SL42,KL2);
320 RIP3(C,D,E,A,B,WL43,SL43,KL2);
321 RIP3(B,C,D,E,A,WL44,SL44,KL2);
322 RIP3(A,B,C,D,E,WL45,SL45,KL2);
323 RIP3(E,A,B,C,D,WL46,SL46,KL2);
324 RIP3(D,E,A,B,C,WL47,SL47,KL2);
325
326 RIP4(C,D,E,A,B,WL48,SL48,KL3);
327 RIP4(B,C,D,E,A,WL49,SL49,KL3);
328 RIP4(A,B,C,D,E,WL50,SL50,KL3);
329 RIP4(E,A,B,C,D,WL51,SL51,KL3);
330 RIP4(D,E,A,B,C,WL52,SL52,KL3);
331 RIP4(C,D,E,A,B,WL53,SL53,KL3);
332 RIP4(B,C,D,E,A,WL54,SL54,KL3);
333 RIP4(A,B,C,D,E,WL55,SL55,KL3);
334 RIP4(E,A,B,C,D,WL56,SL56,KL3);
335 RIP4(D,E,A,B,C,WL57,SL57,KL3);
336 RIP4(C,D,E,A,B,WL58,SL58,KL3);
337 RIP4(B,C,D,E,A,WL59,SL59,KL3);
338 RIP4(A,B,C,D,E,WL60,SL60,KL3);
339 RIP4(E,A,B,C,D,WL61,SL61,KL3);
340 RIP4(D,E,A,B,C,WL62,SL62,KL3);
341 RIP4(C,D,E,A,B,WL63,SL63,KL3);
342
343 RIP5(B,C,D,E,A,WL64,SL64,KL4);
344 RIP5(A,B,C,D,E,WL65,SL65,KL4);
345 RIP5(E,A,B,C,D,WL66,SL66,KL4);
346 RIP5(D,E,A,B,C,WL67,SL67,KL4);
347 RIP5(C,D,E,A,B,WL68,SL68,KL4);
348 RIP5(B,C,D,E,A,WL69,SL69,KL4);
349 RIP5(A,B,C,D,E,WL70,SL70,KL4);
350 RIP5(E,A,B,C,D,WL71,SL71,KL4);
351 RIP5(D,E,A,B,C,WL72,SL72,KL4);
352 RIP5(C,D,E,A,B,WL73,SL73,KL4);
353 RIP5(B,C,D,E,A,WL74,SL74,KL4);
354 RIP5(A,B,C,D,E,WL75,SL75,KL4);
355 RIP5(E,A,B,C,D,WL76,SL76,KL4);
356 RIP5(D,E,A,B,C,WL77,SL77,KL4);
357 RIP5(C,D,E,A,B,WL78,SL78,KL4);
358 RIP5(B,C,D,E,A,WL79,SL79,KL4);
359
360 a=A; b=B; c=C; d=D; e=E;
361 /* Do other half */
362 A=ctx->A; B=ctx->B; C=ctx->C; D=ctx->D; E=ctx->E;
363
364 RIP5(A,B,C,D,E,WR00,SR00,KR0);
365 RIP5(E,A,B,C,D,WR01,SR01,KR0);
366 RIP5(D,E,A,B,C,WR02,SR02,KR0);
367 RIP5(C,D,E,A,B,WR03,SR03,KR0);
368 RIP5(B,C,D,E,A,WR04,SR04,KR0);
369 RIP5(A,B,C,D,E,WR05,SR05,KR0);
370 RIP5(E,A,B,C,D,WR06,SR06,KR0);
371 RIP5(D,E,A,B,C,WR07,SR07,KR0);
372 RIP5(C,D,E,A,B,WR08,SR08,KR0);
373 RIP5(B,C,D,E,A,WR09,SR09,KR0);
374 RIP5(A,B,C,D,E,WR10,SR10,KR0);
375 RIP5(E,A,B,C,D,WR11,SR11,KR0);
376 RIP5(D,E,A,B,C,WR12,SR12,KR0);
377 RIP5(C,D,E,A,B,WR13,SR13,KR0);
378 RIP5(B,C,D,E,A,WR14,SR14,KR0);
379 RIP5(A,B,C,D,E,WR15,SR15,KR0);
380
381 RIP4(E,A,B,C,D,WR16,SR16,KR1);
382 RIP4(D,E,A,B,C,WR17,SR17,KR1);
383 RIP4(C,D,E,A,B,WR18,SR18,KR1);
384 RIP4(B,C,D,E,A,WR19,SR19,KR1);
385 RIP4(A,B,C,D,E,WR20,SR20,KR1);
386 RIP4(E,A,B,C,D,WR21,SR21,KR1);
387 RIP4(D,E,A,B,C,WR22,SR22,KR1);
388 RIP4(C,D,E,A,B,WR23,SR23,KR1);
389 RIP4(B,C,D,E,A,WR24,SR24,KR1);
390 RIP4(A,B,C,D,E,WR25,SR25,KR1);
391 RIP4(E,A,B,C,D,WR26,SR26,KR1);
392 RIP4(D,E,A,B,C,WR27,SR27,KR1);
393 RIP4(C,D,E,A,B,WR28,SR28,KR1);
394 RIP4(B,C,D,E,A,WR29,SR29,KR1);
395 RIP4(A,B,C,D,E,WR30,SR30,KR1);
396 RIP4(E,A,B,C,D,WR31,SR31,KR1);
397
398 RIP3(D,E,A,B,C,WR32,SR32,KR2);
399 RIP3(C,D,E,A,B,WR33,SR33,KR2);
400 RIP3(B,C,D,E,A,WR34,SR34,KR2);
401 RIP3(A,B,C,D,E,WR35,SR35,KR2);
402 RIP3(E,A,B,C,D,WR36,SR36,KR2);
403 RIP3(D,E,A,B,C,WR37,SR37,KR2);
404 RIP3(C,D,E,A,B,WR38,SR38,KR2);
405 RIP3(B,C,D,E,A,WR39,SR39,KR2);
406 RIP3(A,B,C,D,E,WR40,SR40,KR2);
407 RIP3(E,A,B,C,D,WR41,SR41,KR2);
408 RIP3(D,E,A,B,C,WR42,SR42,KR2);
409 RIP3(C,D,E,A,B,WR43,SR43,KR2);
410 RIP3(B,C,D,E,A,WR44,SR44,KR2);
411 RIP3(A,B,C,D,E,WR45,SR45,KR2);
412 RIP3(E,A,B,C,D,WR46,SR46,KR2);
413 RIP3(D,E,A,B,C,WR47,SR47,KR2);
414
415 RIP2(C,D,E,A,B,WR48,SR48,KR3);
416 RIP2(B,C,D,E,A,WR49,SR49,KR3);
417 RIP2(A,B,C,D,E,WR50,SR50,KR3);
418 RIP2(E,A,B,C,D,WR51,SR51,KR3);
419 RIP2(D,E,A,B,C,WR52,SR52,KR3);
420 RIP2(C,D,E,A,B,WR53,SR53,KR3);
421 RIP2(B,C,D,E,A,WR54,SR54,KR3);
422 RIP2(A,B,C,D,E,WR55,SR55,KR3);
423 RIP2(E,A,B,C,D,WR56,SR56,KR3);
424 RIP2(D,E,A,B,C,WR57,SR57,KR3);
425 RIP2(C,D,E,A,B,WR58,SR58,KR3);
426 RIP2(B,C,D,E,A,WR59,SR59,KR3);
427 RIP2(A,B,C,D,E,WR60,SR60,KR3);
428 RIP2(E,A,B,C,D,WR61,SR61,KR3);
429 RIP2(D,E,A,B,C,WR62,SR62,KR3);
430 RIP2(C,D,E,A,B,WR63,SR63,KR3);
431
432 RIP1(B,C,D,E,A,WR64,SR64);
433 RIP1(A,B,C,D,E,WR65,SR65);
434 RIP1(E,A,B,C,D,WR66,SR66);
435 RIP1(D,E,A,B,C,WR67,SR67);
436 RIP1(C,D,E,A,B,WR68,SR68);
437 RIP1(B,C,D,E,A,WR69,SR69);
438 RIP1(A,B,C,D,E,WR70,SR70);
439 RIP1(E,A,B,C,D,WR71,SR71);
440 RIP1(D,E,A,B,C,WR72,SR72);
441 RIP1(C,D,E,A,B,WR73,SR73);
442 RIP1(B,C,D,E,A,WR74,SR74);
443 RIP1(A,B,C,D,E,WR75,SR75);
444 RIP1(E,A,B,C,D,WR76,SR76);
445 RIP1(D,E,A,B,C,WR77,SR77);
446 RIP1(C,D,E,A,B,WR78,SR78);
447 RIP1(B,C,D,E,A,WR79,SR79);
448
449 D =ctx->B+c+D;
450 ctx->B=ctx->C+d+E;
451 ctx->C=ctx->D+e+A;
452 ctx->D=ctx->E+a+B;
453 ctx->E=ctx->A+b+C;
454 ctx->A=D;
455
456 X+=16;
457 num-=64;
458 if (num <= 0) break;
459 }
460 }
461#endif
462
463void RIPEMD160_Final(md, c)
464unsigned char *md;
465RIPEMD160_CTX *c;
466 {
467 register int i,j;
468 register ULONG l;
469 register ULONG *p;
470 static unsigned char end[4]={0x80,0x00,0x00,0x00};
471 unsigned char *cp=end;
472
473 /* c->num should definitly have room for at least one more byte. */
474 p=c->data;
475 j=c->num;
476 i=j>>2;
477
478 /* purify often complains about the following line as an
479 * Uninitialized Memory Read. While this can be true, the
480 * following p_c2l macro will reset l when that case is true.
481 * This is because j&0x03 contains the number of 'valid' bytes
482 * already in p[i]. If and only if j&0x03 == 0, the UMR will
483 * occur but this is also the only time p_c2l will do
484 * l= *(cp++) instead of l|= *(cp++)
485 * Many thanks to Alex Tang <altitude@cic.net> for pickup this
486 * 'potential bug' */
487#ifdef PURIFY
488 if ((j&0x03) == 0) p[i]=0;
489#endif
490 l=p[i];
491 p_c2l(cp,l,j&0x03);
492 p[i]=l;
493 i++;
494 /* i is the next 'undefined word' */
495 if (c->num >= RIPEMD160_LAST_BLOCK)
496 {
497 for (; i<RIPEMD160_LBLOCK; i++)
498 p[i]=0;
499 ripemd160_block(c,p,64);
500 i=0;
501 }
502 for (; i<(RIPEMD160_LBLOCK-2); i++)
503 p[i]=0;
504 p[RIPEMD160_LBLOCK-2]=c->Nl;
505 p[RIPEMD160_LBLOCK-1]=c->Nh;
506 ripemd160_block(c,p,64);
507 cp=md;
508 l=c->A; l2c(l,cp);
509 l=c->B; l2c(l,cp);
510 l=c->C; l2c(l,cp);
511 l=c->D; l2c(l,cp);
512 l=c->E; l2c(l,cp);
513
514 /* clear stuff, ripemd160_block may be leaving some stuff on the stack
515 * but I'm not worried :-) */
516 c->num=0;
517/* memset((char *)&c,0,sizeof(c));*/
518 }
519
520#ifdef undef
521int printit(l)
522unsigned long *l;
523 {
524 int i,ii;
525
526 for (i=0; i<2; i++)
527 {
528 for (ii=0; ii<8; ii++)
529 {
530 fprintf(stderr,"%08lx ",l[i*8+ii]);
531 }
532 fprintf(stderr,"\n");
533 }
534 }
535#endif
diff --git a/src/lib/libcrypto/ripemd/rmd_locl.h b/src/lib/libcrypto/ripemd/rmd_locl.h
new file mode 100644
index 0000000000..a1feccf7c1
--- /dev/null
+++ b/src/lib/libcrypto/ripemd/rmd_locl.h
@@ -0,0 +1,226 @@
1/* crypto/ripemd/rmd_locl.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdlib.h>
60#include <string.h>
61#include "ripemd.h"
62
63#define ULONG unsigned long
64#define UCHAR unsigned char
65#define UINT unsigned int
66
67#ifdef NOCONST
68#define const
69#endif
70
71#undef c2nl
72#define c2nl(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
73 l|=(((unsigned long)(*((c)++)))<<16), \
74 l|=(((unsigned long)(*((c)++)))<< 8), \
75 l|=(((unsigned long)(*((c)++))) ))
76
77#undef p_c2nl
78#define p_c2nl(c,l,n) { \
79 switch (n) { \
80 case 0: l =((unsigned long)(*((c)++)))<<24; \
81 case 1: l|=((unsigned long)(*((c)++)))<<16; \
82 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
83 case 3: l|=((unsigned long)(*((c)++))); \
84 } \
85 }
86
87#undef c2nl_p
88/* NOTE the pointer is not incremented at the end of this */
89#define c2nl_p(c,l,n) { \
90 l=0; \
91 (c)+=n; \
92 switch (n) { \
93 case 3: l =((unsigned long)(*(--(c))))<< 8; \
94 case 2: l|=((unsigned long)(*(--(c))))<<16; \
95 case 1: l|=((unsigned long)(*(--(c))))<<24; \
96 } \
97 }
98
99#undef p_c2nl_p
100#define p_c2nl_p(c,l,sc,len) { \
101 switch (sc) \
102 { \
103 case 0: l =((unsigned long)(*((c)++)))<<24; \
104 if (--len == 0) break; \
105 case 1: l|=((unsigned long)(*((c)++)))<<16; \
106 if (--len == 0) break; \
107 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
108 } \
109 }
110
111#undef nl2c
112#define nl2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
113 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
114 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
115 *((c)++)=(unsigned char)(((l) )&0xff))
116
117#undef c2l
118#define c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
119 l|=(((unsigned long)(*((c)++)))<< 8), \
120 l|=(((unsigned long)(*((c)++)))<<16), \
121 l|=(((unsigned long)(*((c)++)))<<24))
122
123#undef p_c2l
124#define p_c2l(c,l,n) { \
125 switch (n) { \
126 case 0: l =((unsigned long)(*((c)++))); \
127 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
128 case 2: l|=((unsigned long)(*((c)++)))<<16; \
129 case 3: l|=((unsigned long)(*((c)++)))<<24; \
130 } \
131 }
132
133#undef c2l_p
134/* NOTE the pointer is not incremented at the end of this */
135#define c2l_p(c,l,n) { \
136 l=0; \
137 (c)+=n; \
138 switch (n) { \
139 case 3: l =((unsigned long)(*(--(c))))<<16; \
140 case 2: l|=((unsigned long)(*(--(c))))<< 8; \
141 case 1: l|=((unsigned long)(*(--(c)))); \
142 } \
143 }
144
145#undef p_c2l_p
146#define p_c2l_p(c,l,sc,len) { \
147 switch (sc) \
148 { \
149 case 0: l =((unsigned long)(*((c)++))); \
150 if (--len == 0) break; \
151 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
152 if (--len == 0) break; \
153 case 2: l|=((unsigned long)(*((c)++)))<<16; \
154 } \
155 }
156
157#undef l2c
158#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
159 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
160 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
161 *((c)++)=(unsigned char)(((l)>>24)&0xff))
162
163#undef ROTATE
164#if defined(WIN32)
165#define ROTATE(a,n) _lrotl(a,n)
166#else
167#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
168#endif
169
170/* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
171#if defined(WIN32)
172/* 5 instructions with rotate instruction, else 9 */
173#define Endian_Reverse32(a) \
174 { \
175 unsigned long l=(a); \
176 (a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \
177 }
178#else
179/* 6 instructions with rotate instruction, else 8 */
180#define Endian_Reverse32(a) \
181 { \
182 unsigned long l=(a); \
183 l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
184 (a)=ROTATE(l,16L); \
185 }
186#endif
187
188#define F1(x,y,z) ((x)^(y)^(z))
189#define F2(x,y,z) (((x)&(y))|((~x)&z))
190#define F3(x,y,z) (((x)|(~y))^(z))
191#define F4(x,y,z) (((x)&(z))|((y)&(~(z))))
192#define F5(x,y,z) ((x)^((y)|(~(z))))
193
194#define RIPEMD160_A 0x67452301L
195#define RIPEMD160_B 0xEFCDAB89L
196#define RIPEMD160_C 0x98BADCFEL
197#define RIPEMD160_D 0x10325476L
198#define RIPEMD160_E 0xC3D2E1F0L
199
200#include "rmdconst.h"
201
202#define RIP1(a,b,c,d,e,w,s) { \
203 a+=F1(b,c,d)+X[w]; \
204 a=ROTATE(a,s)+e; \
205 c=ROTATE(c,10); }
206
207#define RIP2(a,b,c,d,e,w,s,K) { \
208 a+=F2(b,c,d)+X[w]+K; \
209 a=ROTATE(a,s)+e; \
210 c=ROTATE(c,10); }
211
212#define RIP3(a,b,c,d,e,w,s,K) { \
213 a+=F3(b,c,d)+X[w]+K; \
214 a=ROTATE(a,s)+e; \
215 c=ROTATE(c,10); }
216
217#define RIP4(a,b,c,d,e,w,s,K) { \
218 a+=F4(b,c,d)+X[w]+K; \
219 a=ROTATE(a,s)+e; \
220 c=ROTATE(c,10); }
221
222#define RIP5(a,b,c,d,e,w,s,K) { \
223 a+=F5(b,c,d)+X[w]+K; \
224 a=ROTATE(a,s)+e; \
225 c=ROTATE(c,10); }
226
diff --git a/src/lib/libcrypto/ripemd/rmd_one.c b/src/lib/libcrypto/ripemd/rmd_one.c
new file mode 100644
index 0000000000..a7626dbcda
--- /dev/null
+++ b/src/lib/libcrypto/ripemd/rmd_one.c
@@ -0,0 +1,77 @@
1/* crypto/ripemd/rmd_one.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "rmd_locl.h"
61
62unsigned char *RIPEMD160(d, n, md)
63unsigned char *d;
64unsigned long n;
65unsigned char *md;
66 {
67 RIPEMD160_CTX c;
68 static unsigned char m[RIPEMD160_DIGEST_LENGTH];
69
70 if (md == NULL) md=m;
71 RIPEMD160_Init(&c);
72 RIPEMD160_Update(&c,d,n);
73 RIPEMD160_Final(md,&c);
74 memset(&c,0,sizeof(c)); /* security consideration */
75 return(md);
76 }
77
diff --git a/src/lib/libcrypto/ripemd/rmdconst.h b/src/lib/libcrypto/ripemd/rmdconst.h
new file mode 100644
index 0000000000..59c48dead1
--- /dev/null
+++ b/src/lib/libcrypto/ripemd/rmdconst.h
@@ -0,0 +1,399 @@
1/* crypto/ripemd/rmdconst.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#define KL0 0x00000000L
59#define KL1 0x5A827999L
60#define KL2 0x6ED9EBA1L
61#define KL3 0x8F1BBCDCL
62#define KL4 0xA953FD4EL
63
64#define KR0 0x50A28BE6L
65#define KR1 0x5C4DD124L
66#define KR2 0x6D703EF3L
67#define KR3 0x7A6D76E9L
68#define KR4 0x00000000L
69
70#define WL00 0
71#define SL00 11
72#define WL01 1
73#define SL01 14
74#define WL02 2
75#define SL02 15
76#define WL03 3
77#define SL03 12
78#define WL04 4
79#define SL04 5
80#define WL05 5
81#define SL05 8
82#define WL06 6
83#define SL06 7
84#define WL07 7
85#define SL07 9
86#define WL08 8
87#define SL08 11
88#define WL09 9
89#define SL09 13
90#define WL10 10
91#define SL10 14
92#define WL11 11
93#define SL11 15
94#define WL12 12
95#define SL12 6
96#define WL13 13
97#define SL13 7
98#define WL14 14
99#define SL14 9
100#define WL15 15
101#define SL15 8
102
103#define WL16 7
104#define SL16 7
105#define WL17 4
106#define SL17 6
107#define WL18 13
108#define SL18 8
109#define WL19 1
110#define SL19 13
111#define WL20 10
112#define SL20 11
113#define WL21 6
114#define SL21 9
115#define WL22 15
116#define SL22 7
117#define WL23 3
118#define SL23 15
119#define WL24 12
120#define SL24 7
121#define WL25 0
122#define SL25 12
123#define WL26 9
124#define SL26 15
125#define WL27 5
126#define SL27 9
127#define WL28 2
128#define SL28 11
129#define WL29 14
130#define SL29 7
131#define WL30 11
132#define SL30 13
133#define WL31 8
134#define SL31 12
135
136#define WL32 3
137#define SL32 11
138#define WL33 10
139#define SL33 13
140#define WL34 14
141#define SL34 6
142#define WL35 4
143#define SL35 7
144#define WL36 9
145#define SL36 14
146#define WL37 15
147#define SL37 9
148#define WL38 8
149#define SL38 13
150#define WL39 1
151#define SL39 15
152#define WL40 2
153#define SL40 14
154#define WL41 7
155#define SL41 8
156#define WL42 0
157#define SL42 13
158#define WL43 6
159#define SL43 6
160#define WL44 13
161#define SL44 5
162#define WL45 11
163#define SL45 12
164#define WL46 5
165#define SL46 7
166#define WL47 12
167#define SL47 5
168
169#define WL48 1
170#define SL48 11
171#define WL49 9
172#define SL49 12
173#define WL50 11
174#define SL50 14
175#define WL51 10
176#define SL51 15
177#define WL52 0
178#define SL52 14
179#define WL53 8
180#define SL53 15
181#define WL54 12
182#define SL54 9
183#define WL55 4
184#define SL55 8
185#define WL56 13
186#define SL56 9
187#define WL57 3
188#define SL57 14
189#define WL58 7
190#define SL58 5
191#define WL59 15
192#define SL59 6
193#define WL60 14
194#define SL60 8
195#define WL61 5
196#define SL61 6
197#define WL62 6
198#define SL62 5
199#define WL63 2
200#define SL63 12
201
202#define WL64 4
203#define SL64 9
204#define WL65 0
205#define SL65 15
206#define WL66 5
207#define SL66 5
208#define WL67 9
209#define SL67 11
210#define WL68 7
211#define SL68 6
212#define WL69 12
213#define SL69 8
214#define WL70 2
215#define SL70 13
216#define WL71 10
217#define SL71 12
218#define WL72 14
219#define SL72 5
220#define WL73 1
221#define SL73 12
222#define WL74 3
223#define SL74 13
224#define WL75 8
225#define SL75 14
226#define WL76 11
227#define SL76 11
228#define WL77 6
229#define SL77 8
230#define WL78 15
231#define SL78 5
232#define WL79 13
233#define SL79 6
234
235#define WR00 5
236#define SR00 8
237#define WR01 14
238#define SR01 9
239#define WR02 7
240#define SR02 9
241#define WR03 0
242#define SR03 11
243#define WR04 9
244#define SR04 13
245#define WR05 2
246#define SR05 15
247#define WR06 11
248#define SR06 15
249#define WR07 4
250#define SR07 5
251#define WR08 13
252#define SR08 7
253#define WR09 6
254#define SR09 7
255#define WR10 15
256#define SR10 8
257#define WR11 8
258#define SR11 11
259#define WR12 1
260#define SR12 14
261#define WR13 10
262#define SR13 14
263#define WR14 3
264#define SR14 12
265#define WR15 12
266#define SR15 6
267
268#define WR16 6
269#define SR16 9
270#define WR17 11
271#define SR17 13
272#define WR18 3
273#define SR18 15
274#define WR19 7
275#define SR19 7
276#define WR20 0
277#define SR20 12
278#define WR21 13
279#define SR21 8
280#define WR22 5
281#define SR22 9
282#define WR23 10
283#define SR23 11
284#define WR24 14
285#define SR24 7
286#define WR25 15
287#define SR25 7
288#define WR26 8
289#define SR26 12
290#define WR27 12
291#define SR27 7
292#define WR28 4
293#define SR28 6
294#define WR29 9
295#define SR29 15
296#define WR30 1
297#define SR30 13
298#define WR31 2
299#define SR31 11
300
301#define WR32 15
302#define SR32 9
303#define WR33 5
304#define SR33 7
305#define WR34 1
306#define SR34 15
307#define WR35 3
308#define SR35 11
309#define WR36 7
310#define SR36 8
311#define WR37 14
312#define SR37 6
313#define WR38 6
314#define SR38 6
315#define WR39 9
316#define SR39 14
317#define WR40 11
318#define SR40 12
319#define WR41 8
320#define SR41 13
321#define WR42 12
322#define SR42 5
323#define WR43 2
324#define SR43 14
325#define WR44 10
326#define SR44 13
327#define WR45 0
328#define SR45 13
329#define WR46 4
330#define SR46 7
331#define WR47 13
332#define SR47 5
333
334#define WR48 8
335#define SR48 15
336#define WR49 6
337#define SR49 5
338#define WR50 4
339#define SR50 8
340#define WR51 1
341#define SR51 11
342#define WR52 3
343#define SR52 14
344#define WR53 11
345#define SR53 14
346#define WR54 15
347#define SR54 6
348#define WR55 0
349#define SR55 14
350#define WR56 5
351#define SR56 6
352#define WR57 12
353#define SR57 9
354#define WR58 2
355#define SR58 12
356#define WR59 13
357#define SR59 9
358#define WR60 9
359#define SR60 12
360#define WR61 7
361#define SR61 5
362#define WR62 10
363#define SR62 15
364#define WR63 14
365#define SR63 8
366
367#define WR64 12
368#define SR64 8
369#define WR65 15
370#define SR65 5
371#define WR66 10
372#define SR66 12
373#define WR67 4
374#define SR67 9
375#define WR68 1
376#define SR68 12
377#define WR69 5
378#define SR69 5
379#define WR70 8
380#define SR70 14
381#define WR71 7
382#define SR71 6
383#define WR72 6
384#define SR72 8
385#define WR73 2
386#define SR73 13
387#define WR74 13
388#define SR74 6
389#define WR75 14
390#define SR75 5
391#define WR76 0
392#define SR76 15
393#define WR77 3
394#define SR77 13
395#define WR78 9
396#define SR78 11
397#define WR79 11
398#define SR79 11
399
diff --git a/src/lib/libcrypto/rsa/rsa.h b/src/lib/libcrypto/rsa/rsa.h
new file mode 100644
index 0000000000..aeb78ffcd3
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa.h
@@ -0,0 +1,324 @@
1/* crypto/rsa/rsa.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_RSA_H
60#define HEADER_RSA_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "bn.h"
67#include "crypto.h"
68
69typedef struct rsa_meth_st
70 {
71 char *name;
72 int (*rsa_pub_enc)();
73 int (*rsa_pub_dec)();
74 int (*rsa_priv_enc)();
75 int (*rsa_priv_dec)();
76 int (*rsa_mod_exp)(); /* Can be null */
77 int (*bn_mod_exp)(); /* Can be null */
78 int (*init)(/* RSA * */); /* called at new */
79 int (*finish)(/* RSA * */); /* called at free */
80
81 int flags; /* RSA_METHOD_FLAG_* things */
82 char *app_data; /* may be needed! */
83 } RSA_METHOD;
84
85typedef struct rsa_st
86 {
87 /* The first parameter is used to pickup errors where
88 * this is passed instead of aEVP_PKEY, it is set to 0 */
89 int pad;
90 int version;
91 RSA_METHOD *meth;
92 BIGNUM *n;
93 BIGNUM *e;
94 BIGNUM *d;
95 BIGNUM *p;
96 BIGNUM *q;
97 BIGNUM *dmp1;
98 BIGNUM *dmq1;
99 BIGNUM *iqmp;
100 /* be carefull using this if the RSA structure is shared */
101 CRYPTO_EX_DATA ex_data;
102 int references;
103 int flags;
104
105 /* Normally used to cached montgomery values */
106 char *method_mod_n;
107 char *method_mod_p;
108 char *method_mod_q;
109
110 BN_BLINDING *blinding;
111 } RSA;
112
113#define RSA_3 0x3L
114#define RSA_F4 0x10001L
115
116#define RSA_METHOD_FLAG_NO_CHECK 0x01 /* don't check pub/private match */
117#define RSA_FLAG_CACHE_PUBLIC 0x02
118#define RSA_FLAG_CACHE_PRIVATE 0x04
119#define RSA_FLAG_BLINDING 0x08
120#define RSA_FLAG_THREAD_SAFE 0x10
121
122#define RSA_PKCS1_PADDING 1
123#define RSA_SSLV23_PADDING 2
124#define RSA_NO_PADDING 3
125
126#define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,(char *)arg)
127#define RSA_get_app_data(s) RSA_get_ex_data(s,0)
128
129#ifndef NOPROTO
130RSA * RSA_new(void);
131RSA * RSA_new_method(RSA_METHOD *method);
132int RSA_size(RSA *);
133RSA * RSA_generate_key(int bits, unsigned long e,void
134 (*callback)(int,int,char *),char *cb_arg);
135 /* next 4 return -1 on error */
136int RSA_public_encrypt(int flen, unsigned char *from,
137 unsigned char *to, RSA *rsa,int padding);
138int RSA_private_encrypt(int flen, unsigned char *from,
139 unsigned char *to, RSA *rsa,int padding);
140int RSA_public_decrypt(int flen, unsigned char *from,
141 unsigned char *to, RSA *rsa,int padding);
142int RSA_private_decrypt(int flen, unsigned char *from,
143 unsigned char *to, RSA *rsa,int padding);
144void RSA_free (RSA *r);
145
146int RSA_flags(RSA *r);
147
148void RSA_set_default_method(RSA_METHOD *meth);
149
150/* If you have RSAref compiled in. */
151RSA_METHOD *RSA_PKCS1_RSAref(void);
152
153/* these are the actual SSLeay RSA functions */
154RSA_METHOD *RSA_PKCS1_SSLeay(void);
155
156void ERR_load_RSA_strings(void );
157
158RSA * d2i_RSAPublicKey(RSA **a, unsigned char **pp, long length);
159int i2d_RSAPublicKey(RSA *a, unsigned char **pp);
160RSA * d2i_RSAPrivateKey(RSA **a, unsigned char **pp, long length);
161int i2d_RSAPrivateKey(RSA *a, unsigned char **pp);
162#ifndef NO_FP_API
163int RSA_print_fp(FILE *fp, RSA *r,int offset);
164#endif
165
166#ifdef HEADER_BIO_H
167int RSA_print(BIO *bp, RSA *r,int offset);
168#endif
169
170int i2d_Netscape_RSA(RSA *a, unsigned char **pp, int (*cb)());
171RSA *d2i_Netscape_RSA(RSA **a, unsigned char **pp, long length, int (*cb)());
172/* Naughty internal function required elsewhere, to handle a MS structure
173 * that is the same as the netscape one :-) */
174RSA *d2i_Netscape_RSA_2(RSA **a, unsigned char **pp, long length, int (*cb)());
175
176/* The following 2 functions sign and verify a X509_SIG ASN1 object
177 * inside PKCS#1 padded RSA encryption */
178int RSA_sign(int type, unsigned char *m, unsigned int m_len,
179 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
180int RSA_verify(int type, unsigned char *m, unsigned int m_len,
181 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
182
183/* The following 2 function sign and verify a ASN1_OCTET_STRING
184 * object inside PKCS#1 padded RSA encryption */
185int RSA_sign_ASN1_OCTET_STRING(int type, unsigned char *m, unsigned int m_len,
186 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
187int RSA_verify_ASN1_OCTET_STRING(int type, unsigned char *m, unsigned int m_len,
188 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
189
190int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
191void RSA_blinding_off(RSA *rsa);
192
193int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen,
194 unsigned char *f,int fl);
195int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen,
196 unsigned char *f,int fl);
197int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen,
198 unsigned char *f,int fl);
199int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen,
200 unsigned char *f,int fl);
201int RSA_padding_add_SSLv23(unsigned char *to,int tlen,
202 unsigned char *f,int fl);
203int RSA_padding_check_SSLv23(unsigned char *to,int tlen,
204 unsigned char *f,int fl);
205int RSA_padding_add_none(unsigned char *to,int tlen,
206 unsigned char *f,int fl);
207int RSA_padding_check_none(unsigned char *to,int tlen,
208 unsigned char *f,int fl);
209
210int RSA_get_ex_new_index(long argl, char *argp, int (*new_func)(),
211 int (*dup_func)(), void (*free_func)());
212int RSA_set_ex_data(RSA *r,int idx,char *arg);
213char *RSA_get_ex_data(RSA *r, int idx);
214
215#else
216
217RSA * RSA_new();
218RSA * RSA_new_method();
219int RSA_size();
220RSA * RSA_generate_key();
221int RSA_public_encrypt();
222int RSA_private_encrypt();
223int RSA_public_decrypt();
224int RSA_private_decrypt();
225void RSA_free ();
226
227int RSA_flags();
228
229void RSA_set_default_method();
230
231/* RSA_METHOD *RSA_PKCS1_RSAref(); */
232RSA_METHOD *RSA_PKCS1_SSLeay();
233
234void ERR_load_RSA_strings();
235
236RSA * d2i_RSAPublicKey();
237int i2d_RSAPublicKey();
238RSA * d2i_RSAPrivateKey();
239int i2d_RSAPrivateKey();
240#ifndef NO_FP_API
241int RSA_print_fp();
242#endif
243
244int RSA_print();
245
246int i2d_Netscape_RSA();
247RSA *d2i_Netscape_RSA();
248RSA *d2i_Netscape_RSA_2();
249
250int RSA_sign();
251int RSA_verify();
252
253int RSA_sign_ASN1_OCTET_STRING();
254int RSA_verify_ASN1_OCTET_STRING();
255int RSA_blinding_on();
256void RSA_blinding_off();
257
258int RSA_padding_add_PKCS1_type_1();
259int RSA_padding_check_PKCS1_type_1();
260int RSA_padding_add_PKCS1_type_2();
261int RSA_padding_check_PKCS1_type_2();
262int RSA_padding_add_SSLv23();
263int RSA_padding_check_SSLv23();
264int RSA_padding_add_none();
265int RSA_padding_check_none();
266
267int RSA_get_ex_new_index();
268int RSA_set_ex_data();
269char *RSA_get_ex_data();
270
271#endif
272
273/* BEGIN ERROR CODES */
274/* Error codes for the RSA functions. */
275
276/* Function codes. */
277#define RSA_F_RSA_EAY_PRIVATE_DECRYPT 100
278#define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 101
279#define RSA_F_RSA_EAY_PUBLIC_DECRYPT 102
280#define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 103
281#define RSA_F_RSA_GENERATE_KEY 104
282#define RSA_F_RSA_NEW_METHOD 105
283#define RSA_F_RSA_PADDING_ADD_NONE 106
284#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 107
285#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 108
286#define RSA_F_RSA_PADDING_ADD_SSLV23 109
287#define RSA_F_RSA_PADDING_CHECK_NONE 110
288#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 111
289#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 112
290#define RSA_F_RSA_PADDING_CHECK_SSLV23 113
291#define RSA_F_RSA_PRINT 114
292#define RSA_F_RSA_PRINT_FP 115
293#define RSA_F_RSA_SIGN 116
294#define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 117
295#define RSA_F_RSA_VERIFY 118
296#define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 119
297
298/* Reason codes. */
299#define RSA_R_ALGORITHM_MISMATCH 100
300#define RSA_R_BAD_E_VALUE 101
301#define RSA_R_BAD_FIXED_HEADER_DECRYPT 102
302#define RSA_R_BAD_PAD_BYTE_COUNT 103
303#define RSA_R_BAD_SIGNATURE 104
304#define RSA_R_BAD_ZERO_BYTE 105
305#define RSA_R_BLOCK_TYPE_IS_NOT_01 106
306#define RSA_R_BLOCK_TYPE_IS_NOT_02 107
307#define RSA_R_DATA_GREATER_THAN_MOD_LEN 108
308#define RSA_R_DATA_TOO_LARGE 109
309#define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110
310#define RSA_R_DATA_TOO_SMALL 111
311#define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112
312#define RSA_R_NULL_BEFORE_BLOCK_MISSING 113
313#define RSA_R_PADDING_CHECK_FAILED 114
314#define RSA_R_SSLV3_ROLLBACK_ATTACK 115
315#define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116
316#define RSA_R_UNKNOWN_ALGORITHM_TYPE 117
317#define RSA_R_UNKNOWN_PADDING_TYPE 118
318#define RSA_R_WRONG_SIGNATURE_LENGTH 119
319
320#ifdef __cplusplus
321}
322#endif
323#endif
324
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c
new file mode 100644
index 0000000000..42a77f11cd
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_eay.c
@@ -0,0 +1,274 @@
1
2/* This file has been explicitly broken by ryker for OpenBSD, July
3 * 1, 1998. In spite of the title, there is no implementation of the
4 * RSA algorithm left in this file. All these routines will return an
5 * error and fail when called. They exist as stubs and can be
6 * ressurected from the bit bucket by someone in the free world once
7 * the RSA algorithm is no longer subject to patent problems. Eric
8 * Young's original copyright is below.
9 */
10
11/* crypto/rsa/rsa_eay.c */
12/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
13 * All rights reserved.
14 *
15 * This package is an SSL implementation written
16 * by Eric Young (eay@cryptsoft.com).
17 * The implementation was written so as to conform with Netscapes SSL.
18 *
19 * This library is free for commercial and non-commercial use as long as
20 * the following conditions are aheared to. The following conditions
21 * apply to all code found in this distribution, be it the RC4, RSA,
22 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
23 * included with this distribution is covered by the same copyright terms
24 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
25 *
26 * Copyright remains Eric Young's, and as such any Copyright notices in
27 * the code are not to be removed.
28 * If this package is used in a product, Eric Young should be given attribution
29 * as the author of the parts of the library used.
30 * This can be in the form of a textual message at program startup or
31 * in documentation (online or textual) provided with the package.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * "This product includes cryptographic software written by
44 * Eric Young (eay@cryptsoft.com)"
45 * The word 'cryptographic' can be left out if the rouines from the library
46 * being used are not cryptographic related :-).
47 * 4. If you include any Windows specific code (or a derivative thereof) from
48 * the apps directory (application code) you must include an acknowledgement:
49 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
50 *
51 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * The licence and distribution terms for any publically available version or
64 * derivative of this code cannot be changed. i.e. this code cannot simply be
65 * copied and put under another distribution licence
66 * [including the GNU Public Licence.]
67 */
68
69#include <stdio.h>
70#include "cryptlib.h"
71#include "bn.h"
72#include "rsa.h"
73#include "rand.h"
74
75#ifndef NOPROTO
76static int RSA_eay_public_encrypt(int flen, unsigned char *from,
77 unsigned char *to, RSA *rsa,int padding);
78static int RSA_eay_private_encrypt(int flen, unsigned char *from,
79 unsigned char *to, RSA *rsa,int padding);
80static int RSA_eay_public_decrypt(int flen, unsigned char *from,
81 unsigned char *to, RSA *rsa,int padding);
82static int RSA_eay_private_decrypt(int flen, unsigned char *from,
83 unsigned char *to, RSA *rsa,int padding);
84static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *i, RSA *rsa);
85static int RSA_eay_init(RSA *rsa);
86static int RSA_eay_finish(RSA *rsa);
87#else
88static int RSA_eay_public_encrypt();
89static int RSA_eay_private_encrypt();
90static int RSA_eay_public_decrypt();
91static int RSA_eay_private_decrypt();
92static int RSA_eay_mod_exp();
93static int RSA_eay_init();
94static int RSA_eay_finish();
95#endif
96
97static RSA_METHOD rsa_pkcs1_eay_meth={
98 "Eric Young's PKCS#1 RSA",
99 RSA_eay_public_encrypt,
100 RSA_eay_public_decrypt,
101 RSA_eay_private_encrypt,
102 RSA_eay_private_decrypt,
103 RSA_eay_mod_exp,
104 BN_mod_exp_mont,
105 RSA_eay_init,
106 RSA_eay_finish,
107 0,
108 NULL,
109 };
110
111RSA_METHOD *RSA_PKCS1_SSLeay()
112 {
113 return(&rsa_pkcs1_eay_meth);
114 }
115
116static int RSA_eay_public_encrypt(flen, from, to, rsa, padding)
117int flen;
118unsigned char *from;
119unsigned char *to;
120RSA *rsa;
121int padding;
122 {
123 BIGNUM *f=NULL,*ret=NULL;
124 int i,j,k,num=0,r= -1;
125 unsigned char *buf=NULL;
126 BN_CTX *ctx=NULL;
127
128 /* Body of this routine removed for OpenBSD - will return
129 * when the RSA patent expires
130 */
131
132err:
133 if (ctx != NULL) BN_CTX_free(ctx);
134 if (f != NULL) BN_free(f);
135 if (ret != NULL) BN_free(ret);
136 if (buf != NULL)
137 {
138 memset(buf,0,num);
139 Free(buf);
140 }
141 return(r);
142 }
143
144static int RSA_eay_private_encrypt(flen, from, to, rsa, padding)
145int flen;
146unsigned char *from;
147unsigned char *to;
148RSA *rsa;
149int padding;
150 {
151 BIGNUM *f=NULL,*ret=NULL;
152 int i,j,k,num=0,r= -1;
153 unsigned char *buf=NULL;
154 BN_CTX *ctx=NULL;
155
156 /* Body of this routine removed for OpenBSD - will return
157 * when the RSA patent expires
158 */
159
160err:
161 if (ctx != NULL) BN_CTX_free(ctx);
162 if (ret != NULL) BN_free(ret);
163 if (f != NULL) BN_free(f);
164 if (buf != NULL)
165 {
166 memset(buf,0,num);
167 Free(buf);
168 }
169 return(r);
170 }
171
172static int RSA_eay_private_decrypt(flen, from, to, rsa,padding)
173int flen;
174unsigned char *from;
175unsigned char *to;
176RSA *rsa;
177int padding;
178 {
179 BIGNUM *f=NULL,*ret=NULL;
180 int j,num=0,r= -1;
181 unsigned char *p;
182 unsigned char *buf=NULL;
183 BN_CTX *ctx=NULL;
184
185 /* Body of this routine removed for OpenBSD - will return
186 * when the RSA patent expires
187 */
188
189err:
190 if (ctx != NULL) BN_CTX_free(ctx);
191 if (f != NULL) BN_free(f);
192 if (ret != NULL) BN_free(ret);
193 if (buf != NULL)
194 {
195 memset(buf,0,num);
196 Free(buf);
197 }
198 return(r);
199 }
200
201static int RSA_eay_public_decrypt(flen, from, to, rsa, padding)
202int flen;
203unsigned char *from;
204unsigned char *to;
205RSA *rsa;
206int padding;
207 {
208 BIGNUM *f=NULL,*ret=NULL;
209 int i,num=0,r= -1;
210 unsigned char *p;
211 unsigned char *buf=NULL;
212 BN_CTX *ctx=NULL;
213
214
215 /* Body of this routine removed for OpenBSD - will return
216 * when the RSA patent expires
217 */
218
219err:
220 if (ctx != NULL) BN_CTX_free(ctx);
221 if (f != NULL) BN_free(f);
222 if (ret != NULL) BN_free(ret);
223 if (buf != NULL)
224 {
225 memset(buf,0,num);
226 Free(buf);
227 }
228 return(r);
229 }
230
231static int RSA_eay_mod_exp(r0, I, rsa)
232BIGNUM *r0;
233BIGNUM *I;
234RSA *rsa;
235 {
236 BIGNUM *r1=NULL,*m1=NULL;
237 int ret=0;
238 BN_CTX *ctx;
239
240 if ((ctx=BN_CTX_new()) == NULL) goto err;
241 m1=BN_new();
242 r1=BN_new();
243 if ((m1 == NULL) || (r1 == NULL)) goto err;
244
245 /* Body of this routine removed for OpenBSD - will return
246 * when the RSA patent expires
247 */
248err:
249 if (m1 != NULL) BN_free(m1);
250 if (r1 != NULL) BN_free(r1);
251 BN_CTX_free(ctx);
252 return(ret);
253 }
254
255static int RSA_eay_init(rsa)
256RSA *rsa;
257 {
258 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
259 return(1);
260 }
261
262static int RSA_eay_finish(rsa)
263RSA *rsa;
264 {
265 if (rsa->method_mod_n != NULL)
266 BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_n);
267 if (rsa->method_mod_p != NULL)
268 BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_p);
269 if (rsa->method_mod_q != NULL)
270 BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_q);
271 return(1);
272 }
273
274
diff --git a/src/lib/libcrypto/rsa/rsa_err.c b/src/lib/libcrypto/rsa/rsa_err.c
new file mode 100644
index 0000000000..796b3afd47
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_err.c
@@ -0,0 +1,129 @@
1/* lib/rsa/rsa_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "rsa.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA RSA_str_functs[]=
65 {
66{ERR_PACK(0,RSA_F_RSA_EAY_PRIVATE_DECRYPT,0), "RSA_EAY_PRIVATE_DECRYPT"},
67{ERR_PACK(0,RSA_F_RSA_EAY_PRIVATE_ENCRYPT,0), "RSA_EAY_PRIVATE_ENCRYPT"},
68{ERR_PACK(0,RSA_F_RSA_EAY_PUBLIC_DECRYPT,0), "RSA_EAY_PUBLIC_DECRYPT"},
69{ERR_PACK(0,RSA_F_RSA_EAY_PUBLIC_ENCRYPT,0), "RSA_EAY_PUBLIC_ENCRYPT"},
70{ERR_PACK(0,RSA_F_RSA_GENERATE_KEY,0), "RSA_generate_key"},
71{ERR_PACK(0,RSA_F_RSA_NEW_METHOD,0), "RSA_new_method"},
72{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_NONE,0), "RSA_padding_add_none"},
73{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,0), "RSA_padding_add_PKCS1_type_1"},
74{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,0), "RSA_padding_add_PKCS1_type_2"},
75{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_SSLV23,0), "RSA_padding_add_SSLv23"},
76{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_NONE,0), "RSA_padding_check_none"},
77{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,0), "RSA_padding_check_PKCS1_type_1"},
78{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,0), "RSA_padding_check_PKCS1_type_2"},
79{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_SSLV23,0), "RSA_padding_check_SSLv23"},
80{ERR_PACK(0,RSA_F_RSA_PRINT,0), "RSA_print"},
81{ERR_PACK(0,RSA_F_RSA_PRINT_FP,0), "RSA_print_fp"},
82{ERR_PACK(0,RSA_F_RSA_SIGN,0), "RSA_sign"},
83{ERR_PACK(0,RSA_F_RSA_SIGN_ASN1_OCTET_STRING,0), "RSA_sign_ASN1_OCTET_STRING"},
84{ERR_PACK(0,RSA_F_RSA_VERIFY,0), "RSA_verify"},
85{ERR_PACK(0,RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,0), "RSA_verify_ASN1_OCTET_STRING"},
86{0,NULL},
87 };
88
89static ERR_STRING_DATA RSA_str_reasons[]=
90 {
91{RSA_R_ALGORITHM_MISMATCH ,"algorithm mismatch"},
92{RSA_R_BAD_E_VALUE ,"bad e value"},
93{RSA_R_BAD_FIXED_HEADER_DECRYPT ,"bad fixed header decrypt"},
94{RSA_R_BAD_PAD_BYTE_COUNT ,"bad pad byte count"},
95{RSA_R_BAD_SIGNATURE ,"bad signature"},
96{RSA_R_BAD_ZERO_BYTE ,"bad zero byte"},
97{RSA_R_BLOCK_TYPE_IS_NOT_01 ,"block type is not 01"},
98{RSA_R_BLOCK_TYPE_IS_NOT_02 ,"block type is not 02"},
99{RSA_R_DATA_GREATER_THAN_MOD_LEN ,"data greater than mod len"},
100{RSA_R_DATA_TOO_LARGE ,"data too large"},
101{RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE ,"data too large for key size"},
102{RSA_R_DATA_TOO_SMALL ,"data too small"},
103{RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY ,"digest too big for rsa key"},
104{RSA_R_NULL_BEFORE_BLOCK_MISSING ,"null before block missing"},
105{RSA_R_PADDING_CHECK_FAILED ,"padding check failed"},
106{RSA_R_SSLV3_ROLLBACK_ATTACK ,"sslv3 rollback attack"},
107{RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD,"the asn1 object identifier is not known for this md"},
108{RSA_R_UNKNOWN_ALGORITHM_TYPE ,"unknown algorithm type"},
109{RSA_R_UNKNOWN_PADDING_TYPE ,"unknown padding type"},
110{RSA_R_WRONG_SIGNATURE_LENGTH ,"wrong signature length"},
111{0,NULL},
112 };
113
114#endif
115
116void ERR_load_RSA_strings()
117 {
118 static int init=1;
119
120 if (init);
121 {;
122 init=0;
123#ifndef NO_ERR
124 ERR_load_strings(ERR_LIB_RSA,RSA_str_functs);
125 ERR_load_strings(ERR_LIB_RSA,RSA_str_reasons);
126#endif
127
128 }
129 }
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c
new file mode 100644
index 0000000000..4cbd373829
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_gen.c
@@ -0,0 +1,101 @@
1/* crypto/rsa/rsa_gen.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include "cryptlib.h"
62#include "bn.h"
63#include "rsa.h"
64
65RSA *RSA_generate_key(bits, e_value, callback,cb_arg)
66int bits;
67unsigned long e_value;
68void (*callback)(P_I_I_P);
69char *cb_arg;
70 {
71 RSA *rsa=NULL;
72 BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp;
73 int bitsp,bitsq,ok= -1,n=0;
74 BN_CTX *ctx=NULL,*ctx2=NULL;
75
76 ctx=BN_CTX_new();
77 if (ctx == NULL) goto err;
78 ctx2=BN_CTX_new();
79 if (ctx2 == NULL) goto err;
80
81 /* Body of this routine removed for OpenBSD - will return
82 * when the RSA patent expires
83 */
84err:
85 if (ok == -1)
86 {
87 RSAerr(RSA_F_RSA_GENERATE_KEY,ERR_LIB_BN);
88 ok=0;
89 }
90 BN_CTX_free(ctx);
91 BN_CTX_free(ctx2);
92
93 if (!ok)
94 {
95 if (rsa != NULL) RSA_free(rsa);
96 return(NULL);
97 }
98 else
99 return(rsa);
100 }
101
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c
new file mode 100644
index 0000000000..95a56f8a28
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_lib.c
@@ -0,0 +1,294 @@
1/* crypto/rsa/rsa_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "crypto.h"
61#include "cryptlib.h"
62#include "lhash.h"
63#include "bn.h"
64#include "rsa.h"
65
66char *RSA_version="RSA part of SSLeay 0.9.0b 29-Jun-1998";
67
68static RSA_METHOD *default_RSA_meth=NULL;
69static int rsa_meth_num=0;
70static STACK *rsa_meth=NULL;
71
72RSA *RSA_new()
73 {
74 return(RSA_new_method(NULL));
75 }
76
77void RSA_set_default_method(meth)
78RSA_METHOD *meth;
79 {
80 default_RSA_meth=meth;
81 }
82
83RSA *RSA_new_method(meth)
84RSA_METHOD *meth;
85 {
86 RSA *ret;
87
88 if (default_RSA_meth == NULL)
89 {
90#ifdef RSAref
91 default_RSA_meth=RSA_PKCS1_RSAref();
92#else
93 default_RSA_meth=RSA_PKCS1_SSLeay();
94#endif
95 }
96 ret=(RSA *)Malloc(sizeof(RSA));
97 if (ret == NULL)
98 {
99 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
100 return(NULL);
101 }
102
103 if (meth == NULL)
104 ret->meth=default_RSA_meth;
105 else
106 ret->meth=meth;
107
108 ret->pad=0;
109 ret->version=0;
110 ret->n=NULL;
111 ret->e=NULL;
112 ret->d=NULL;
113 ret->p=NULL;
114 ret->q=NULL;
115 ret->dmp1=NULL;
116 ret->dmq1=NULL;
117 ret->iqmp=NULL;
118 ret->references=1;
119 ret->method_mod_n=NULL;
120 ret->method_mod_p=NULL;
121 ret->method_mod_q=NULL;
122 ret->blinding=NULL;
123 ret->flags=ret->meth->flags;
124 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
125 {
126 Free(ret);
127 ret=NULL;
128 }
129 CRYPTO_new_ex_data(rsa_meth,(char *)ret,&ret->ex_data);
130 return(ret);
131 }
132
133void RSA_free(r)
134RSA *r;
135 {
136 int i;
137
138 if (r == NULL) return;
139
140 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
141#ifdef REF_PRINT
142 REF_PRINT("RSA",r);
143#endif
144 if (i > 0) return;
145#ifdef REF_CHECK
146 if (i < 0)
147 {
148 fprintf(stderr,"RSA_free, bad reference count\n");
149 abort();
150 }
151#endif
152
153 CRYPTO_free_ex_data(rsa_meth,(char *)r,&r->ex_data);
154
155 if (r->meth->finish != NULL)
156 r->meth->finish(r);
157
158 if (r->n != NULL) BN_clear_free(r->n);
159 if (r->e != NULL) BN_clear_free(r->e);
160 if (r->d != NULL) BN_clear_free(r->d);
161 if (r->p != NULL) BN_clear_free(r->p);
162 if (r->q != NULL) BN_clear_free(r->q);
163 if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
164 if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
165 if (r->iqmp != NULL) BN_clear_free(r->iqmp);
166 if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
167 Free(r);
168 }
169
170int RSA_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
171long argl;
172char *argp;
173int (*new_func)();
174int (*dup_func)();
175void (*free_func)();
176 {
177 rsa_meth_num++;
178 return(CRYPTO_get_ex_new_index(rsa_meth_num-1,
179 &rsa_meth,argl,argp,new_func,dup_func,free_func));
180 }
181
182int RSA_set_ex_data(r,idx,arg)
183RSA *r;
184int idx;
185char *arg;
186 {
187 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
188 }
189
190char *RSA_get_ex_data(r,idx)
191RSA *r;
192int idx;
193 {
194 return(CRYPTO_get_ex_data(&r->ex_data,idx));
195 }
196
197int RSA_size(r)
198RSA *r;
199 {
200 return(BN_num_bytes(r->n));
201 }
202
203int RSA_public_encrypt(flen, from, to, rsa, padding)
204int flen;
205unsigned char *from;
206unsigned char *to;
207RSA *rsa;
208int padding;
209 {
210 return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
211 }
212
213int RSA_private_encrypt(flen, from, to, rsa, padding)
214int flen;
215unsigned char *from;
216unsigned char *to;
217RSA *rsa;
218int padding;
219 {
220 return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
221 }
222
223int RSA_private_decrypt(flen, from, to, rsa, padding)
224int flen;
225unsigned char *from;
226unsigned char *to;
227RSA *rsa;
228int padding;
229 {
230 return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
231 }
232
233int RSA_public_decrypt(flen, from, to, rsa, padding)
234int flen;
235unsigned char *from;
236unsigned char *to;
237RSA *rsa;
238int padding;
239 {
240 return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
241 }
242
243int RSA_flags(r)
244RSA *r;
245 {
246 return((r == NULL)?0:r->meth->flags);
247 }
248
249void RSA_blinding_off(rsa)
250RSA *rsa;
251 {
252 if (rsa->blinding != NULL)
253 {
254 BN_BLINDING_free(rsa->blinding);
255 rsa->blinding=NULL;
256 }
257 rsa->flags&= ~RSA_FLAG_BLINDING;
258 }
259
260int RSA_blinding_on(rsa,p_ctx)
261RSA *rsa;
262BN_CTX *p_ctx;
263 {
264 BIGNUM *A,*Ai;
265 BN_CTX *ctx;
266 int ret=0;
267
268 if (p_ctx == NULL)
269 {
270 if ((ctx=BN_CTX_new()) == NULL) goto err;
271 }
272 else
273 ctx=p_ctx;
274
275 if (rsa->blinding != NULL)
276 BN_BLINDING_free(rsa->blinding);
277
278 A=ctx->bn[0];
279 ctx->tos++;
280 if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err;
281 if ((Ai=BN_mod_inverse(A,rsa->n,ctx)) == NULL) goto err;
282
283 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,
284 (char *)rsa->method_mod_n)) goto err;
285 rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n);
286 ctx->tos--;
287 rsa->flags|=RSA_FLAG_BLINDING;
288 BN_free(Ai);
289 ret=1;
290err:
291 if (ctx != p_ctx) BN_CTX_free(ctx);
292 return(ret);
293 }
294
diff --git a/src/lib/libcrypto/rsa/rsa_none.c b/src/lib/libcrypto/rsa/rsa_none.c
new file mode 100644
index 0000000000..f0dd943657
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_none.c
@@ -0,0 +1,109 @@
1/* crypto/rsa/rsa_none.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "rsa.h"
63#include "rand.h"
64
65int RSA_padding_add_none(to,tlen,from,flen)
66unsigned char *to;
67int tlen;
68unsigned char *from;
69int flen;
70 {
71 if (flen >= tlen)
72 {
73 RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
74 return(0);
75 }
76
77 *(to++)=0;
78 memcpy(to,from,(unsigned int)flen);
79 return(1);
80 }
81
82int RSA_padding_check_none(to,tlen,from,flen)
83unsigned char *to;
84int tlen;
85unsigned char *from;
86int flen;
87 {
88 int j;
89
90 from++;
91 if (flen+1 > tlen)
92 {
93 RSAerr(RSA_F_RSA_PADDING_CHECK_NONE,RSA_R_DATA_TOO_LARGE);
94 return(-1);
95 }
96 if (*(from++) != 0)
97 {
98 RSAerr(RSA_F_RSA_PADDING_CHECK_NONE,RSA_R_BAD_ZERO_BYTE);
99 return(-1);
100 }
101
102 /* scan over padding data */
103 j=flen-1; /* one for type and one for the prepended 0. */
104 memset(to,0,tlen-j);
105 to+=(tlen-j);
106 memcpy(to,from,j);
107 return(j);
108 }
109
diff --git a/src/lib/libcrypto/rsa/rsa_pk1.c b/src/lib/libcrypto/rsa/rsa_pk1.c
new file mode 100644
index 0000000000..2791291b94
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_pk1.c
@@ -0,0 +1,233 @@
1/* crypto/rsa/rsa_pk1.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "rsa.h"
63#include "rand.h"
64
65#ifndef NOPROTO
66int RSA_padding_add_PKCS1_type_1();
67int RSA_padding_check_PKCS1_type_1();
68int RSA_padding_add_PKCS1_type_2();
69int RSA_padding_check_PKCS1_type_2();
70int RSA_padding_add_SSLv23();
71int RSA_padding_check_SSLv23();
72int RSA_padding_add_none();
73int RSA_padding_check_none();
74
75#endif
76
77int RSA_padding_add_PKCS1_type_1(to,tlen,from,flen)
78unsigned char *to;
79int tlen;
80unsigned char *from;
81int flen;
82 {
83 int j;
84 unsigned char *p;
85
86 if (flen > (tlen-11))
87 {
88 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
89 return(0);
90 }
91
92 p=(unsigned char *)to;
93
94 *(p++)=0;
95 *(p++)=1; /* Private Key BT (Block Type) */
96
97 /* padd out with 0xff data */
98 j=tlen-3-flen;
99 memset(p,0xff,j);
100 p+=j;
101 *(p++)='\0';
102 memcpy(p,from,(unsigned int)flen);
103 return(1);
104 }
105
106int RSA_padding_check_PKCS1_type_1(to,tlen,from,flen)
107unsigned char *to;
108int tlen;
109unsigned char *from;
110int flen;
111 {
112 int i,j;
113 unsigned char *p;
114
115 p=from;
116 if (*(p++) != 01)
117 {
118 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BLOCK_TYPE_IS_NOT_01);
119 return(-1);
120 }
121
122 /* scan over padding data */
123 j=flen-1; /* one for type. */
124 for (i=0; i<j; i++)
125 {
126 if (*p != 0xff) /* should decrypt to 0xff */
127 {
128 if (*p == 0)
129 { p++; break; }
130 else {
131 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_FIXED_HEADER_DECRYPT);
132 return(-1);
133 }
134 }
135 p++;
136 }
137
138 if (i == j)
139 {
140 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_NULL_BEFORE_BLOCK_MISSING);
141 return(-1);
142 }
143
144 if (i < 8)
145 {
146 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_PAD_BYTE_COUNT);
147 return(-1);
148 }
149 i++; /* Skip over the '\0' */
150 j-=i;
151 memcpy(to,p,(unsigned int)j);
152
153 return(j);
154 }
155
156int RSA_padding_add_PKCS1_type_2(to,tlen,from,flen)
157unsigned char *to;
158int tlen;
159unsigned char *from;
160int flen;
161 {
162 int i,j;
163 unsigned char *p;
164
165 if (flen > (tlen-11))
166 {
167 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
168 return(0);
169 }
170
171 p=(unsigned char *)to;
172
173 *(p++)=0;
174 *(p++)=2; /* Public Key BT (Block Type) */
175
176 /* pad out with non-zero random data */
177 j=tlen-3-flen;
178
179 RAND_bytes(p,j);
180 for (i=0; i<j; i++)
181 {
182 if (*p == '\0')
183 do {
184 RAND_bytes(p,1);
185 } while (*p == '\0');
186 p++;
187 }
188
189 *(p++)='\0';
190
191 memcpy(p,from,(unsigned int)flen);
192 return(1);
193 }
194
195int RSA_padding_check_PKCS1_type_2(to,tlen,from,flen)
196unsigned char *to;
197int tlen;
198unsigned char *from;
199int flen;
200 {
201 int i,j;
202 unsigned char *p;
203
204 p=from;
205 if (*(p++) != 02)
206 {
207 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BLOCK_TYPE_IS_NOT_02);
208 return(-1);
209 }
210
211 /* scan over padding data */
212 j=flen-1; /* one for type. */
213 for (i=0; i<j; i++)
214 if (*(p++) == 0) break;
215
216 if (i == j)
217 {
218 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_NULL_BEFORE_BLOCK_MISSING);
219 return(-1);
220 }
221
222 if (i < 8)
223 {
224 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BAD_PAD_BYTE_COUNT);
225 return(-1);
226 }
227 i++; /* Skip over the '\0' */
228 j-=i;
229 memcpy(to,p,(unsigned int)j);
230
231 return(j);
232 }
233
diff --git a/src/lib/libcrypto/rsa/rsa_saos.c b/src/lib/libcrypto/rsa/rsa_saos.c
new file mode 100644
index 0000000000..fb0fae5a43
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_saos.c
@@ -0,0 +1,153 @@
1/* crypto/rsa/rsa_saos.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "rsa.h"
63#include "objects.h"
64#include "x509.h"
65
66int RSA_sign_ASN1_OCTET_STRING(type,m,m_len,sigret,siglen,rsa)
67int type;
68unsigned char *m;
69unsigned int m_len;
70unsigned char *sigret;
71unsigned int *siglen;
72RSA *rsa;
73 {
74 ASN1_OCTET_STRING sig;
75 int i,j,ret=1;
76 unsigned char *p,*s;
77
78 sig.type=V_ASN1_OCTET_STRING;
79 sig.length=m_len;
80 sig.data=m;
81
82 i=i2d_ASN1_OCTET_STRING(&sig,NULL);
83 j=RSA_size(rsa);
84 if ((i-RSA_PKCS1_PADDING) > j)
85 {
86 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
87 return(0);
88 }
89 s=(unsigned char *)Malloc((unsigned int)j+1);
90 if (s == NULL)
91 {
92 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
93 return(0);
94 }
95 p=s;
96 i2d_ASN1_OCTET_STRING(&sig,&p);
97 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
98 if (i <= 0)
99 ret=0;
100 else
101 *siglen=i;
102
103 memset(s,0,(unsigned int)j+1);
104 Free(s);
105 return(ret);
106 }
107
108int RSA_verify_ASN1_OCTET_STRING(dtype, m, m_len, sigbuf, siglen, rsa)
109int dtype;
110unsigned char *m;
111unsigned int m_len;
112unsigned char *sigbuf;
113unsigned int siglen;
114RSA *rsa;
115 {
116 int i,ret=0;
117 unsigned char *p,*s;
118 ASN1_OCTET_STRING *sig=NULL;
119
120 if (siglen != (unsigned int)RSA_size(rsa))
121 {
122 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH);
123 return(0);
124 }
125
126 s=(unsigned char *)Malloc((unsigned int)siglen);
127 if (s == NULL)
128 {
129 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
130 goto err;
131 }
132 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
133
134 if (i <= 0) goto err;
135
136 p=s;
137 sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i);
138 if (sig == NULL) goto err;
139
140 if ( ((unsigned int)sig->length != m_len) ||
141 (memcmp(m,sig->data,m_len) != 0))
142 {
143 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE);
144 }
145 else
146 ret=1;
147err:
148 if (sig != NULL) ASN1_OCTET_STRING_free(sig);
149 memset(s,0,(unsigned int)siglen);
150 Free(s);
151 return(ret);
152 }
153
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c
new file mode 100644
index 0000000000..28c5571e74
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_sign.c
@@ -0,0 +1,196 @@
1/* crypto/rsa/rsa_sign.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "rsa.h"
63#include "objects.h"
64#include "x509.h"
65
66int RSA_sign(type,m,m_len,sigret,siglen,rsa)
67int type;
68unsigned char *m;
69unsigned int m_len;
70unsigned char *sigret;
71unsigned int *siglen;
72RSA *rsa;
73 {
74 X509_SIG sig;
75 ASN1_TYPE parameter;
76 int i,j,ret=1;
77 unsigned char *p,*s;
78 X509_ALGOR algor;
79 ASN1_OCTET_STRING digest;
80
81 sig.algor= &algor;
82 sig.algor->algorithm=OBJ_nid2obj(type);
83 if (sig.algor->algorithm == NULL)
84 {
85 RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE);
86 return(0);
87 }
88 if (sig.algor->algorithm->length == 0)
89 {
90 RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
91 return(0);
92 }
93 parameter.type=V_ASN1_NULL;
94 parameter.value.ptr=NULL;
95 sig.algor->parameter= &parameter;
96
97 sig.digest= &digest;
98 sig.digest->data=m;
99 sig.digest->length=m_len;
100
101 i=i2d_X509_SIG(&sig,NULL);
102 j=RSA_size(rsa);
103 if ((i-RSA_PKCS1_PADDING) > j)
104 {
105 RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
106 return(0);
107 }
108 s=(unsigned char *)Malloc((unsigned int)j+1);
109 if (s == NULL)
110 {
111 RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE);
112 return(0);
113 }
114 p=s;
115 i2d_X509_SIG(&sig,&p);
116 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
117 if (i <= 0)
118 ret=0;
119 else
120 *siglen=i;
121
122 memset(s,0,(unsigned int)j+1);
123 Free(s);
124 return(ret);
125 }
126
127int RSA_verify(dtype, m, m_len, sigbuf, siglen, rsa)
128int dtype;
129unsigned char *m;
130unsigned int m_len;
131unsigned char *sigbuf;
132unsigned int siglen;
133RSA *rsa;
134 {
135 int i,ret=0,sigtype;
136 unsigned char *p,*s;
137 X509_SIG *sig=NULL;
138
139 if (siglen != (unsigned int)RSA_size(rsa))
140 {
141 RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH);
142 return(0);
143 }
144
145 s=(unsigned char *)Malloc((unsigned int)siglen);
146 if (s == NULL)
147 {
148 RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE);
149 goto err;
150 }
151 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
152
153 if (i <= 0) goto err;
154
155 p=s;
156 sig=d2i_X509_SIG(NULL,&p,(long)i);
157 if (sig == NULL) goto err;
158 sigtype=OBJ_obj2nid(sig->algor->algorithm);
159
160#ifdef RSA_DEBUG
161 /* put a backward compatability flag in EAY */
162 fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype),
163 OBJ_nid2ln(dtype));
164#endif
165 if (sigtype != dtype)
166 {
167 if (((dtype == NID_md5) &&
168 (sigtype == NID_md5WithRSAEncryption)) ||
169 ((dtype == NID_md2) &&
170 (sigtype == NID_md2WithRSAEncryption)))
171 {
172 /* ok, we will let it through */
173#if !defined(NO_STDIO) && !defined(WIN16)
174 fprintf(stderr,"signature has problems, re-make with post SSLeay045\n");
175#endif
176 }
177 else
178 {
179 RSAerr(RSA_F_RSA_VERIFY,RSA_R_ALGORITHM_MISMATCH);
180 goto err;
181 }
182 }
183 if ( ((unsigned int)sig->digest->length != m_len) ||
184 (memcmp(m,sig->digest->data,m_len) != 0))
185 {
186 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
187 }
188 else
189 ret=1;
190err:
191 if (sig != NULL) X509_SIG_free(sig);
192 memset(s,0,(unsigned int)siglen);
193 Free(s);
194 return(ret);
195 }
196
diff --git a/src/lib/libcrypto/rsa/rsa_ssl.c b/src/lib/libcrypto/rsa/rsa_ssl.c
new file mode 100644
index 0000000000..9bcd4b2c03
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_ssl.c
@@ -0,0 +1,153 @@
1/* crypto/rsa/rsa_ssl.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "rsa.h"
63#include "rand.h"
64
65int RSA_padding_add_SSLv23(to,tlen,from,flen)
66unsigned char *to;
67int tlen;
68unsigned char *from;
69int flen;
70 {
71 int i,j;
72 unsigned char *p;
73
74 if (flen > (tlen-11))
75 {
76 RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
77 return(0);
78 }
79
80 p=(unsigned char *)to;
81
82 *(p++)=0;
83 *(p++)=2; /* Public Key BT (Block Type) */
84
85 /* pad out with non-zero random data */
86 j=tlen-3-8-flen;
87
88 RAND_bytes(p,j);
89 for (i=0; i<j; i++)
90 {
91 if (*p == '\0')
92 do {
93 RAND_bytes(p,1);
94 } while (*p == '\0');
95 p++;
96 }
97
98 memset(p,3,8);
99 p+=8;
100 *(p++)='\0';
101
102 memcpy(p,from,(unsigned int)flen);
103 return(1);
104 }
105
106int RSA_padding_check_SSLv23(to,tlen,from,flen)
107unsigned char *to;
108int tlen;
109unsigned char *from;
110int flen;
111 {
112 int i,j,k;
113 unsigned char *p;
114
115 p=from;
116 if (flen < 10)
117 {
118 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_SMALL);
119 return(-1);
120 }
121 if (*(p++) != 02)
122 {
123 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_BLOCK_TYPE_IS_NOT_02);
124 return(-1);
125 }
126
127 /* scan over padding data */
128 j=flen-1; /* one for type */
129 for (i=0; i<j; i++)
130 if (*(p++) == 0) break;
131
132 if ((i == j) || (i < 8))
133 {
134 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_NULL_BEFORE_BLOCK_MISSING);
135 return(-1);
136 }
137 for (k= -8; k<0; k++)
138 {
139 if (p[k] != 0x03) break;
140 }
141 if (k == 0)
142 {
143 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_SSLV3_ROLLBACK_ATTACK);
144 return(-1);
145 }
146
147 i++; /* Skip over the '\0' */
148 j-=i;
149 memcpy(to,p,(unsigned int)j);
150
151 return(j);
152 }
153
diff --git a/src/lib/libcrypto/sha/asm/sha1-586.pl b/src/lib/libcrypto/sha/asm/sha1-586.pl
new file mode 100644
index 0000000000..d6d998f8ee
--- /dev/null
+++ b/src/lib/libcrypto/sha/asm/sha1-586.pl
@@ -0,0 +1,491 @@
1#!/usr/bin/perl
2
3$normal=0;
4
5push(@INC,"perlasm","../../perlasm");
6require "x86asm.pl";
7
8&asm_init($ARGV[0],"sha1-586.pl");
9
10$A="eax";
11$B="ebx";
12$C="ecx";
13$D="edx";
14$E="edi";
15$T="esi";
16$tmp1="ebp";
17
18$off=9*4;
19
20@K=(0x5a827999,0x6ed9eba1,0x8f1bbcdc,0xca62c1d6);
21
22&sha1_block("sha1_block_x86");
23
24&asm_finish();
25
26sub Nn
27 {
28 local($p)=@_;
29 local(%n)=($A,$T,$B,$A,$C,$B,$D,$C,$E,$D,$T,$E);
30 return($n{$p});
31 }
32
33sub Np
34 {
35 local($p)=@_;
36 local(%n)=($A,$T,$B,$A,$C,$B,$D,$C,$E,$D,$T,$E);
37 local(%n)=($A,$B,$B,$C,$C,$D,$D,$E,$E,$T,$T,$A);
38 return($n{$p});
39 }
40
41sub Na
42 {
43 local($n)=@_;
44 return( (($n )&0x0f),
45 (($n+ 2)&0x0f),
46 (($n+ 8)&0x0f),
47 (($n+13)&0x0f),
48 (($n+ 1)&0x0f));
49 }
50
51sub X_expand
52 {
53 local($in)=@_;
54
55 &comment("First, load the words onto the stack in network byte order");
56 for ($i=0; $i<16; $i++)
57 {
58 &mov("eax",&DWP(($i+0)*4,$in,"",0)) unless $i == 0;
59 &bswap("eax");
60 &mov(&swtmp($i+0),"eax");
61 }
62
63 &comment("We now have the X array on the stack");
64 &comment("starting at sp-4");
65 }
66
67# Rules of engagement
68# F is always trashable at the start, the running total.
69# E becomes the next F so it can be trashed after it has been 'accumulated'
70# F becomes A in the next round. We don't need to access it much.
71# During the X update part, the result ends up in $X[$n0].
72
73sub BODY_00_15
74 {
75 local($pos,$K,$X,$n,$a,$b,$c,$d,$e,$f)=@_;
76
77return if $n & 1;
78 &comment("00_15 $n");
79
80 &mov($f,$c);
81
82 &mov($tmp1,$a);
83 &xor($f,$d); # F2
84
85 &rotl($tmp1,5); # A2
86
87 &and($f,$b); # F3
88 &add($tmp1,$e);
89
90 &rotr($b,1); # B1 <- F
91 &mov($e,&swtmp($n)); # G1
92
93 &rotr($b,1); # B1 <- F
94 &xor($f,$d); # F4
95
96 &lea($tmp1,&DWP($K,$tmp1,$e,1));
97
98############################
99# &BODY_40_59( 0,$K[2],$X,42,$A,$B,$C,$D,$E,$T);
100# &BODY_40_59( 0,$K[2],$X,43,$T,$A,$B,$C,$D,$E);
101$n++;
102 local($n0,$n1,$n2,$n3,$np)=&Na($n);
103 ($b,$c,$d,$e,$f,$a)=($a,$b,$c,$d,$e,$f);
104
105 &mov($f,$c);
106
107 &add($a,$tmp1); # MOVED DOWN
108 &xor($f,$d); # F2
109
110 &mov($tmp1,$a);
111 &and($f,$b); # F3
112
113 &rotl($tmp1,5); # A2
114
115 &add($tmp1,$e);
116 &mov($e,&swtmp($n)); # G1
117
118 &rotr($b,1); # B1 <- F
119 &xor($f,$d); # F4
120
121 &rotr($b,1); # B1 <- F
122 &lea($tmp1,&DWP($K,$tmp1,$e,1));
123
124 &add($f,$tmp1);
125 }
126
127sub BODY_16_19
128 {
129 local($pos,$K,$X,$n,$a,$b,$c,$d,$e,$f)=@_;
130 local($n0,$n1,$n2,$n3,$np)=&Na($n);
131
132return if $n & 1;
133 &comment("16_19 $n");
134
135 &nop() if ($pos < 0);
136&mov($tmp1,&swtmp($n0)); # X1
137 &mov($f,&swtmp($n1)); # X2
138&xor($f,$tmp1); # X3
139 &mov($tmp1,&swtmp($n2)); # X4
140&xor($f,$tmp1); # X5
141 &mov($tmp1,&swtmp($n3)); # X6
142&xor($f,$tmp1); # X7 - slot
143 &mov($tmp1,$c); # F1
144&rotl($f,1); # X8 - slot
145 &xor($tmp1,$d); # F2
146&mov(&swtmp($n0),$f); # X9 - anytime
147 &and($tmp1,$b); # F3
148&lea($f,&DWP($K,$f,$e,1)); # tot=X+K+e
149 &xor($tmp1,$d); # F4
150&mov($e,$a); # A1
151 &add($f,$tmp1); # tot+=F();
152
153&rotl($e,5); # A2
154
155&rotr($b,1); # B1 <- F
156 &add($f,$e); # tot+=a
157
158############################
159# &BODY_40_59( 0,$K[2],$X,42,$A,$B,$C,$D,$E,$T);
160# &BODY_40_59( 0,$K[2],$X,43,$T,$A,$B,$C,$D,$E);
161$n++;
162 local($n0,$n1,$n2,$n3,$np)=&Na($n);
163 ($b,$c,$d,$e,$f,$a)=($a,$b,$c,$d,$e,$f);
164
165
166&mov($f,&swtmp($n0)); # X1
167 &mov($tmp1,&swtmp($n1)); # X2
168&xor($f,$tmp1); # X3
169 &mov($tmp1,&swtmp($n2)); # X4
170&xor($f,$tmp1); # X5
171 &mov($tmp1,&swtmp($n3)); # X6
172&rotr($c,1); #&rotr($b,1); # B1 <- F # MOVED DOWN
173 &xor($f,$tmp1); # X7 - slot
174&rotl($f,1); # X8 - slot
175 &mov($tmp1,$c); # F1
176&xor($tmp1,$d); # F2
177 &mov(&swtmp($n0),$f); # X9 - anytime
178&and($tmp1,$b); # F3
179 &lea($f,&DWP($K,$f,$e,1)); # tot=X+K+e
180
181&xor($tmp1,$d); # F4
182 &mov($e,$a); # A1
183
184&rotl($e,5); # A2
185
186&rotr($b,1); # B1 <- F
187 &add($f,$e); # tot+=a
188
189&rotr($b,1); # B1 <- F
190 &add($f,$tmp1); # tot+=F();
191
192 }
193
194sub BODY_20_39
195 {
196 local($pos,$K,$X,$n,$a,$b,$c,$d,$e,$f)=@_;
197
198 &comment("20_39 $n");
199 local($n0,$n1,$n2,$n3,$np)=&Na($n);
200
201&mov($f,&swtmp($n0)); # X1
202 &mov($tmp1,&swtmp($n1)); # X2
203&xor($f,$tmp1); # X3
204 &mov($tmp1,&swtmp($n2)); # X4
205&xor($f,$tmp1); # X5
206 &mov($tmp1,&swtmp($n3)); # X6
207&xor($f,$tmp1); # X7 - slot
208 &mov($tmp1,$b); # F1
209&rotl($f,1); # X8 - slot
210 &xor($tmp1,$c); # F2
211&mov(&swtmp($n0),$f); # X9 - anytime
212 &xor($tmp1,$d); # F3
213
214&lea($f,&DWP($K,$f,$e,1)); # tot=X+K+e
215 &mov($e,$a); # A1
216
217&rotl($e,5); # A2
218
219if ($n != 79) # last loop
220 {
221 &rotr($b,1); # B1 <- F
222 &add($e,$tmp1); # tmp1=F()+a
223
224 &rotr($b,1); # B2 <- F
225 &add($f,$e); # tot+=tmp1;
226 }
227else
228 {
229 &add($e,$tmp1); # tmp1=F()+a
230 &mov($tmp1,&wparam(0));
231
232 &rotr($b,1); # B1 <- F
233 &add($f,$e); # tot+=tmp1;
234
235 &rotr($b,1); # B2 <- F
236 }
237 }
238
239sub BODY_40_59
240 {
241 local($pos,$K,$X,$n,$a,$b,$c,$d,$e,$f)=@_;
242
243 &comment("40_59 $n");
244 return if $n & 1;
245 local($n0,$n1,$n2,$n3,$np)=&Na($n);
246
247&mov($f,&swtmp($n0)); # X1
248 &mov($tmp1,&swtmp($n1)); # X2
249&xor($f,$tmp1); # X3
250 &mov($tmp1,&swtmp($n2)); # X4
251&xor($f,$tmp1); # X5
252 &mov($tmp1,&swtmp($n3)); # X6
253&xor($f,$tmp1); # X7 - slot
254 &mov($tmp1,$b); # F1
255&rotl($f,1); # X8 - slot
256 &or($tmp1,$c); # F2
257&mov(&swtmp($n0),$f); # X9 - anytime
258 &and($tmp1,$d); # F3
259
260&lea($f,&DWP($K,$f,$e,1)); # tot=X+K+e
261 &mov($e,$b); # F4
262
263&rotr($b,1); # B1 <- F
264 &and($e,$c); # F5
265
266&or($tmp1,$e); # F6
267 &mov($e,$a); # A1
268
269&rotl($e,5); # A2
270
271&add($tmp1,$e); # tmp1=F()+a
272
273############################
274# &BODY_40_59( 0,$K[2],$X,42,$A,$B,$C,$D,$E,$T);
275# &BODY_40_59( 0,$K[2],$X,43,$T,$A,$B,$C,$D,$E);
276$n++;
277 local($n0,$n1,$n2,$n3,$np)=&Na($n);
278 ($b,$c,$d,$e,$f,$a)=($a,$b,$c,$d,$e,$f);
279
280 &mov($f,&swtmp($n0)); # X1
281&add($a,$tmp1); # tot+=tmp1; # moved was add f,tmp1
282 &mov($tmp1,&swtmp($n1)); # X2
283&xor($f,$tmp1); # X3
284 &mov($tmp1,&swtmp($n2)); # X4
285&xor($f,$tmp1); # X5
286 &mov($tmp1,&swtmp($n3)); # X6
287&rotr($c,1); # B2 <- F # moved was rotr b,1
288 &xor($f,$tmp1); # X7 - slot
289&rotl($f,1); # X8 - slot
290 &mov($tmp1,$b); # F1
291&mov(&swtmp($n0),$f); # X9 - anytime
292 &or($tmp1,$c); # F2
293&lea($f,&DWP($K,$f,$e,1)); # tot=X+K+e
294 &mov($e,$b); # F4
295&and($tmp1,$d); # F3
296 &and($e,$c); # F5
297
298&or($tmp1,$e); # F6
299 &mov($e,$a); # A1
300
301&rotl($e,5); # A2
302
303&rotr($b,1); # B1 <- F
304 &add($tmp1,$e); # tmp1=F()+a
305
306&rotr($b,1); # B2 <- F
307 &add($f,$tmp1); # tot+=tmp1;
308 }
309
310sub BODY_60_79
311 {
312 &BODY_20_39(@_);
313 }
314
315sub sha1_block
316 {
317 local($name)=@_;
318
319 &function_begin_B($name,"");
320
321 # parameter 1 is the MD5_CTX structure.
322 # A 0
323 # B 4
324 # C 8
325 # D 12
326 # E 16
327
328 &push("esi");
329 &push("ebp");
330 &mov("eax", &wparam(2));
331 &mov("esi", &wparam(1));
332 &add("eax", "esi"); # offset to leave on
333 &mov("ebp", &wparam(0));
334 &push("ebx");
335 &sub("eax", 64);
336 &push("edi");
337 &mov($B, &DWP( 4,"ebp","",0));
338 &stack_push(18);
339 &mov($D, &DWP(12,"ebp","",0));
340 &mov($E, &DWP(16,"ebp","",0));
341 &mov($C, &DWP( 8,"ebp","",0));
342 &mov(&swtmp(17),"eax");
343
344 &comment("First we need to setup the X array");
345 &mov("eax",&DWP(0,"esi","",0)); # pulled out of X_expand
346
347 &set_label("start") unless $normal;
348
349 &X_expand("esi");
350 &mov(&swtmp(16),"esi");
351
352 &comment("");
353 &comment("Start processing");
354
355 # odd start
356 &mov($A, &DWP( 0,"ebp","",0));
357 $X="esp";
358 &BODY_00_15(-2,$K[0],$X, 0,$A,$B,$C,$D,$E,$T);
359 &BODY_00_15( 0,$K[0],$X, 1,$T,$A,$B,$C,$D,$E);
360 &BODY_00_15( 0,$K[0],$X, 2,$E,$T,$A,$B,$C,$D);
361 &BODY_00_15( 0,$K[0],$X, 3,$D,$E,$T,$A,$B,$C);
362 &BODY_00_15( 0,$K[0],$X, 4,$C,$D,$E,$T,$A,$B);
363 &BODY_00_15( 0,$K[0],$X, 5,$B,$C,$D,$E,$T,$A);
364 &BODY_00_15( 0,$K[0],$X, 6,$A,$B,$C,$D,$E,$T);
365 &BODY_00_15( 0,$K[0],$X, 7,$T,$A,$B,$C,$D,$E);
366 &BODY_00_15( 0,$K[0],$X, 8,$E,$T,$A,$B,$C,$D);
367 &BODY_00_15( 0,$K[0],$X, 9,$D,$E,$T,$A,$B,$C);
368 &BODY_00_15( 0,$K[0],$X,10,$C,$D,$E,$T,$A,$B);
369 &BODY_00_15( 0,$K[0],$X,11,$B,$C,$D,$E,$T,$A);
370 &BODY_00_15( 0,$K[0],$X,12,$A,$B,$C,$D,$E,$T);
371 &BODY_00_15( 0,$K[0],$X,13,$T,$A,$B,$C,$D,$E);
372 &BODY_00_15( 0,$K[0],$X,14,$E,$T,$A,$B,$C,$D);
373 &BODY_00_15( 1,$K[0],$X,15,$D,$E,$T,$A,$B,$C);
374 &BODY_16_19(-1,$K[0],$X,16,$C,$D,$E,$T,$A,$B);
375 &BODY_16_19( 0,$K[0],$X,17,$B,$C,$D,$E,$T,$A);
376 &BODY_16_19( 0,$K[0],$X,18,$A,$B,$C,$D,$E,$T);
377 &BODY_16_19( 1,$K[0],$X,19,$T,$A,$B,$C,$D,$E);
378
379 &BODY_20_39(-1,$K[1],$X,20,$E,$T,$A,$B,$C,$D);
380 &BODY_20_39( 0,$K[1],$X,21,$D,$E,$T,$A,$B,$C);
381 &BODY_20_39( 0,$K[1],$X,22,$C,$D,$E,$T,$A,$B);
382 &BODY_20_39( 0,$K[1],$X,23,$B,$C,$D,$E,$T,$A);
383 &BODY_20_39( 0,$K[1],$X,24,$A,$B,$C,$D,$E,$T);
384 &BODY_20_39( 0,$K[1],$X,25,$T,$A,$B,$C,$D,$E);
385 &BODY_20_39( 0,$K[1],$X,26,$E,$T,$A,$B,$C,$D);
386 &BODY_20_39( 0,$K[1],$X,27,$D,$E,$T,$A,$B,$C);
387 &BODY_20_39( 0,$K[1],$X,28,$C,$D,$E,$T,$A,$B);
388 &BODY_20_39( 0,$K[1],$X,29,$B,$C,$D,$E,$T,$A);
389 &BODY_20_39( 0,$K[1],$X,30,$A,$B,$C,$D,$E,$T);
390 &BODY_20_39( 0,$K[1],$X,31,$T,$A,$B,$C,$D,$E);
391 &BODY_20_39( 0,$K[1],$X,32,$E,$T,$A,$B,$C,$D);
392 &BODY_20_39( 0,$K[1],$X,33,$D,$E,$T,$A,$B,$C);
393 &BODY_20_39( 0,$K[1],$X,34,$C,$D,$E,$T,$A,$B);
394 &BODY_20_39( 0,$K[1],$X,35,$B,$C,$D,$E,$T,$A);
395 &BODY_20_39( 0,$K[1],$X,36,$A,$B,$C,$D,$E,$T);
396 &BODY_20_39( 0,$K[1],$X,37,$T,$A,$B,$C,$D,$E);
397 &BODY_20_39( 0,$K[1],$X,38,$E,$T,$A,$B,$C,$D);
398 &BODY_20_39( 1,$K[1],$X,39,$D,$E,$T,$A,$B,$C);
399
400 &BODY_40_59(-1,$K[2],$X,40,$C,$D,$E,$T,$A,$B);
401 &BODY_40_59( 0,$K[2],$X,41,$B,$C,$D,$E,$T,$A);
402 &BODY_40_59( 0,$K[2],$X,42,$A,$B,$C,$D,$E,$T);
403 &BODY_40_59( 0,$K[2],$X,43,$T,$A,$B,$C,$D,$E);
404 &BODY_40_59( 0,$K[2],$X,44,$E,$T,$A,$B,$C,$D);
405 &BODY_40_59( 0,$K[2],$X,45,$D,$E,$T,$A,$B,$C);
406 &BODY_40_59( 0,$K[2],$X,46,$C,$D,$E,$T,$A,$B);
407 &BODY_40_59( 0,$K[2],$X,47,$B,$C,$D,$E,$T,$A);
408 &BODY_40_59( 0,$K[2],$X,48,$A,$B,$C,$D,$E,$T);
409 &BODY_40_59( 0,$K[2],$X,49,$T,$A,$B,$C,$D,$E);
410 &BODY_40_59( 0,$K[2],$X,50,$E,$T,$A,$B,$C,$D);
411 &BODY_40_59( 0,$K[2],$X,51,$D,$E,$T,$A,$B,$C);
412 &BODY_40_59( 0,$K[2],$X,52,$C,$D,$E,$T,$A,$B);
413 &BODY_40_59( 0,$K[2],$X,53,$B,$C,$D,$E,$T,$A);
414 &BODY_40_59( 0,$K[2],$X,54,$A,$B,$C,$D,$E,$T);
415 &BODY_40_59( 0,$K[2],$X,55,$T,$A,$B,$C,$D,$E);
416 &BODY_40_59( 0,$K[2],$X,56,$E,$T,$A,$B,$C,$D);
417 &BODY_40_59( 0,$K[2],$X,57,$D,$E,$T,$A,$B,$C);
418 &BODY_40_59( 0,$K[2],$X,58,$C,$D,$E,$T,$A,$B);
419 &BODY_40_59( 1,$K[2],$X,59,$B,$C,$D,$E,$T,$A);
420
421 &BODY_60_79(-1,$K[3],$X,60,$A,$B,$C,$D,$E,$T);
422 &BODY_60_79( 0,$K[3],$X,61,$T,$A,$B,$C,$D,$E);
423 &BODY_60_79( 0,$K[3],$X,62,$E,$T,$A,$B,$C,$D);
424 &BODY_60_79( 0,$K[3],$X,63,$D,$E,$T,$A,$B,$C);
425 &BODY_60_79( 0,$K[3],$X,64,$C,$D,$E,$T,$A,$B);
426 &BODY_60_79( 0,$K[3],$X,65,$B,$C,$D,$E,$T,$A);
427 &BODY_60_79( 0,$K[3],$X,66,$A,$B,$C,$D,$E,$T);
428 &BODY_60_79( 0,$K[3],$X,67,$T,$A,$B,$C,$D,$E);
429 &BODY_60_79( 0,$K[3],$X,68,$E,$T,$A,$B,$C,$D);
430 &BODY_60_79( 0,$K[3],$X,69,$D,$E,$T,$A,$B,$C);
431 &BODY_60_79( 0,$K[3],$X,70,$C,$D,$E,$T,$A,$B);
432 &BODY_60_79( 0,$K[3],$X,71,$B,$C,$D,$E,$T,$A);
433 &BODY_60_79( 0,$K[3],$X,72,$A,$B,$C,$D,$E,$T);
434 &BODY_60_79( 0,$K[3],$X,73,$T,$A,$B,$C,$D,$E);
435 &BODY_60_79( 0,$K[3],$X,74,$E,$T,$A,$B,$C,$D);
436 &BODY_60_79( 0,$K[3],$X,75,$D,$E,$T,$A,$B,$C);
437 &BODY_60_79( 0,$K[3],$X,76,$C,$D,$E,$T,$A,$B);
438 &BODY_60_79( 0,$K[3],$X,77,$B,$C,$D,$E,$T,$A);
439 &BODY_60_79( 0,$K[3],$X,78,$A,$B,$C,$D,$E,$T);
440 &BODY_60_79( 2,$K[3],$X,79,$T,$A,$B,$C,$D,$E);
441
442 &comment("End processing");
443 &comment("");
444 # D is the tmp value
445
446 # E -> A
447 # T -> B
448 # A -> C
449 # B -> D
450 # C -> E
451 # D -> T
452
453 # The last 2 have been moved into the last loop
454 # &mov($tmp1,&wparam(0));
455
456 &mov($D, &DWP(12,$tmp1,"",0));
457 &add($D,$B);
458 &mov($B, &DWP( 4,$tmp1,"",0));
459 &add($B,$T);
460 &mov($T, $A);
461 &mov($A, &DWP( 0,$tmp1,"",0));
462 &mov(&DWP(12,$tmp1,"",0),$D);
463
464 &add($A,$E);
465 &mov($E, &DWP(16,$tmp1,"",0));
466 &add($E,$C);
467 &mov($C, &DWP( 8,$tmp1,"",0));
468 &add($C,$T);
469
470 &mov(&DWP( 0,$tmp1,"",0),$A);
471 &mov("esi",&swtmp(16));
472 &mov(&DWP( 8,$tmp1,"",0),$C); # This is for looping
473 &add("esi",64);
474 &mov("eax",&swtmp(17));
475 &mov(&DWP(16,$tmp1,"",0),$E);
476 &cmp("eax","esi");
477 &mov(&DWP( 4,$tmp1,"",0),$B); # This is for looping
478 &jl(&label("end"));
479 &mov("eax",&DWP(0,"esi","",0)); # Pulled down from
480 &jmp(&label("start"));
481
482 &set_label("end");
483 &stack_pop(18);
484 &pop("edi");
485 &pop("ebx");
486 &pop("ebp");
487 &pop("esi");
488 &ret();
489 &function_end_B($name);
490 }
491
diff --git a/src/lib/libcrypto/sha/sha.h b/src/lib/libcrypto/sha/sha.h
new file mode 100644
index 0000000000..4cf0ea0225
--- /dev/null
+++ b/src/lib/libcrypto/sha/sha.h
@@ -0,0 +1,109 @@
1/* crypto/sha/sha.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_SHA_H
60#define HEADER_SHA_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#define SHA_CBLOCK 64
67#define SHA_LBLOCK 16
68#define SHA_BLOCK 16
69#define SHA_LAST_BLOCK 56
70#define SHA_LENGTH_BLOCK 8
71#define SHA_DIGEST_LENGTH 20
72
73typedef struct SHAstate_st
74 {
75 unsigned long h0,h1,h2,h3,h4;
76 unsigned long Nl,Nh;
77 unsigned long data[SHA_LBLOCK];
78 int num;
79 } SHA_CTX;
80
81#ifndef NOPROTO
82void SHA_Init(SHA_CTX *c);
83void SHA_Update(SHA_CTX *c, unsigned char *data, unsigned long len);
84void SHA_Final(unsigned char *md, SHA_CTX *c);
85unsigned char *SHA(unsigned char *d, unsigned long n,unsigned char *md);
86void SHA_Transform(SHA_CTX *c, unsigned char *data);
87void SHA1_Init(SHA_CTX *c);
88void SHA1_Update(SHA_CTX *c, unsigned char *data, unsigned long len);
89void SHA1_Final(unsigned char *md, SHA_CTX *c);
90unsigned char *SHA1(unsigned char *d, unsigned long n,unsigned char *md);
91void SHA1_Transform(SHA_CTX *c, unsigned char *data);
92#else
93void SHA_Init();
94void SHA_Update();
95void SHA_Final();
96unsigned char *SHA();
97void SHA_Transform();
98void SHA1_Init();
99void SHA1_Update();
100void SHA1_Final();
101unsigned char *SHA1();
102void SHA1_Transform();
103#endif
104
105#ifdef __cplusplus
106}
107#endif
108
109#endif
diff --git a/src/lib/libcrypto/sha/sha1_one.c b/src/lib/libcrypto/sha/sha1_one.c
new file mode 100644
index 0000000000..fe5770d601
--- /dev/null
+++ b/src/lib/libcrypto/sha/sha1_one.c
@@ -0,0 +1,77 @@
1/* crypto/sha/sha1_one.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include "sha.h"
62
63unsigned char *SHA1(d, n, md)
64unsigned char *d;
65unsigned long n;
66unsigned char *md;
67 {
68 SHA_CTX c;
69 static unsigned char m[SHA_DIGEST_LENGTH];
70
71 if (md == NULL) md=m;
72 SHA1_Init(&c);
73 SHA1_Update(&c,d,n);
74 SHA1_Final(md,&c);
75 memset(&c,0,sizeof(c));
76 return(md);
77 }
diff --git a/src/lib/libcrypto/sha/sha1dgst.c b/src/lib/libcrypto/sha/sha1dgst.c
new file mode 100644
index 0000000000..2b0ae1f0d4
--- /dev/null
+++ b/src/lib/libcrypto/sha/sha1dgst.c
@@ -0,0 +1,468 @@
1/* crypto/sha/sha1dgst.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#undef SHA_0
62#define SHA_1
63#include "sha.h"
64#include "sha_locl.h"
65
66char *SHA1_version="SHA1 part of SSLeay 0.9.0b 29-Jun-1998";
67
68/* Implemented from SHA-1 document - The Secure Hash Algorithm
69 */
70
71#define INIT_DATA_h0 (unsigned long)0x67452301L
72#define INIT_DATA_h1 (unsigned long)0xefcdab89L
73#define INIT_DATA_h2 (unsigned long)0x98badcfeL
74#define INIT_DATA_h3 (unsigned long)0x10325476L
75#define INIT_DATA_h4 (unsigned long)0xc3d2e1f0L
76
77#define K_00_19 0x5a827999L
78#define K_20_39 0x6ed9eba1L
79#define K_40_59 0x8f1bbcdcL
80#define K_60_79 0xca62c1d6L
81
82#ifndef NOPROTO
83# ifdef SHA1_ASM
84 void sha1_block_x86(SHA_CTX *c, register unsigned long *p, int num);
85# define sha1_block sha1_block_x86
86# else
87 void sha1_block(SHA_CTX *c, register unsigned long *p, int num);
88# endif
89#else
90# ifdef SHA1_ASM
91 void sha1_block_x86();
92# define sha1_block sha1_block_x86
93# else
94 void sha1_block();
95# endif
96#endif
97
98
99#if defined(L_ENDIAN) && defined(SHA1_ASM)
100# define M_c2nl c2l
101# define M_p_c2nl p_c2l
102# define M_c2nl_p c2l_p
103# define M_p_c2nl_p p_c2l_p
104# define M_nl2c l2c
105#else
106# define M_c2nl c2nl
107# define M_p_c2nl p_c2nl
108# define M_c2nl_p c2nl_p
109# define M_p_c2nl_p p_c2nl_p
110# define M_nl2c nl2c
111#endif
112
113void SHA1_Init(c)
114SHA_CTX *c;
115 {
116 c->h0=INIT_DATA_h0;
117 c->h1=INIT_DATA_h1;
118 c->h2=INIT_DATA_h2;
119 c->h3=INIT_DATA_h3;
120 c->h4=INIT_DATA_h4;
121 c->Nl=0;
122 c->Nh=0;
123 c->num=0;
124 }
125
126void SHA1_Update(c, data, len)
127SHA_CTX *c;
128register unsigned char *data;
129unsigned long len;
130 {
131 register ULONG *p;
132 int ew,ec,sw,sc;
133 ULONG l;
134
135 if (len == 0) return;
136
137 l=(c->Nl+(len<<3))&0xffffffffL;
138 if (l < c->Nl) /* overflow */
139 c->Nh++;
140 c->Nh+=(len>>29);
141 c->Nl=l;
142
143 if (c->num != 0)
144 {
145 p=c->data;
146 sw=c->num>>2;
147 sc=c->num&0x03;
148
149 if ((c->num+len) >= SHA_CBLOCK)
150 {
151 l= p[sw];
152 M_p_c2nl(data,l,sc);
153 p[sw++]=l;
154 for (; sw<SHA_LBLOCK; sw++)
155 {
156 M_c2nl(data,l);
157 p[sw]=l;
158 }
159 len-=(SHA_CBLOCK-c->num);
160
161 sha1_block(c,p,64);
162 c->num=0;
163 /* drop through and do the rest */
164 }
165 else
166 {
167 c->num+=(int)len;
168 if ((sc+len) < 4) /* ugly, add char's to a word */
169 {
170 l= p[sw];
171 M_p_c2nl_p(data,l,sc,len);
172 p[sw]=l;
173 }
174 else
175 {
176 ew=(c->num>>2);
177 ec=(c->num&0x03);
178 l= p[sw];
179 M_p_c2nl(data,l,sc);
180 p[sw++]=l;
181 for (; sw < ew; sw++)
182 { M_c2nl(data,l); p[sw]=l; }
183 if (ec)
184 {
185 M_c2nl_p(data,l,ec);
186 p[sw]=l;
187 }
188 }
189 return;
190 }
191 }
192 /* We can only do the following code for assember, the reason
193 * being that the sha1_block 'C' version changes the values
194 * in the 'data' array. The assember code avoids this and
195 * copies it to a local array. I should be able to do this for
196 * the C version as well....
197 */
198#if 1
199#if defined(B_ENDIAN) || defined(SHA1_ASM)
200 if ((((unsigned int)data)%sizeof(ULONG)) == 0)
201 {
202 sw=len/SHA_CBLOCK;
203 if (sw)
204 {
205 sw*=SHA_CBLOCK;
206 sha1_block(c,(ULONG *)data,sw);
207 data+=sw;
208 len-=sw;
209 }
210 }
211#endif
212#endif
213 /* we now can process the input data in blocks of SHA_CBLOCK
214 * chars and save the leftovers to c->data. */
215 p=c->data;
216 while (len >= SHA_CBLOCK)
217 {
218#if defined(B_ENDIAN) || defined(L_ENDIAN)
219 if (p != (unsigned long *)data)
220 memcpy(p,data,SHA_CBLOCK);
221 data+=SHA_CBLOCK;
222# ifdef L_ENDIAN
223# ifndef SHA1_ASM /* Will not happen */
224 for (sw=(SHA_LBLOCK/4); sw; sw--)
225 {
226 Endian_Reverse32(p[0]);
227 Endian_Reverse32(p[1]);
228 Endian_Reverse32(p[2]);
229 Endian_Reverse32(p[3]);
230 p+=4;
231 }
232 p=c->data;
233# endif
234# endif
235#else
236 for (sw=(SHA_BLOCK/4); sw; sw--)
237 {
238 M_c2nl(data,l); *(p++)=l;
239 M_c2nl(data,l); *(p++)=l;
240 M_c2nl(data,l); *(p++)=l;
241 M_c2nl(data,l); *(p++)=l;
242 }
243 p=c->data;
244#endif
245 sha1_block(c,p,64);
246 len-=SHA_CBLOCK;
247 }
248 ec=(int)len;
249 c->num=ec;
250 ew=(ec>>2);
251 ec&=0x03;
252
253 for (sw=0; sw < ew; sw++)
254 { M_c2nl(data,l); p[sw]=l; }
255 M_c2nl_p(data,l,ec);
256 p[sw]=l;
257 }
258
259void SHA1_Transform(c,b)
260SHA_CTX *c;
261unsigned char *b;
262 {
263 ULONG p[16];
264#ifndef B_ENDIAN
265 ULONG *q;
266 int i;
267#endif
268
269#if defined(B_ENDIAN) || defined(L_ENDIAN)
270 memcpy(p,b,64);
271#ifdef L_ENDIAN
272 q=p;
273 for (i=(SHA_LBLOCK/4); i; i--)
274 {
275 Endian_Reverse32(q[0]);
276 Endian_Reverse32(q[1]);
277 Endian_Reverse32(q[2]);
278 Endian_Reverse32(q[3]);
279 q+=4;
280 }
281#endif
282#else
283 q=p;
284 for (i=(SHA_LBLOCK/4); i; i--)
285 {
286 ULONG l;
287 c2nl(b,l); *(q++)=l;
288 c2nl(b,l); *(q++)=l;
289 c2nl(b,l); *(q++)=l;
290 c2nl(b,l); *(q++)=l;
291 }
292#endif
293 sha1_block(c,p,64);
294 }
295
296#ifndef SHA1_ASM
297
298void sha1_block(c, W, num)
299SHA_CTX *c;
300register unsigned long *W;
301int num;
302 {
303 register ULONG A,B,C,D,E,T;
304 ULONG X[16];
305
306 A=c->h0;
307 B=c->h1;
308 C=c->h2;
309 D=c->h3;
310 E=c->h4;
311
312 for (;;)
313 {
314 BODY_00_15( 0,A,B,C,D,E,T,W);
315 BODY_00_15( 1,T,A,B,C,D,E,W);
316 BODY_00_15( 2,E,T,A,B,C,D,W);
317 BODY_00_15( 3,D,E,T,A,B,C,W);
318 BODY_00_15( 4,C,D,E,T,A,B,W);
319 BODY_00_15( 5,B,C,D,E,T,A,W);
320 BODY_00_15( 6,A,B,C,D,E,T,W);
321 BODY_00_15( 7,T,A,B,C,D,E,W);
322 BODY_00_15( 8,E,T,A,B,C,D,W);
323 BODY_00_15( 9,D,E,T,A,B,C,W);
324 BODY_00_15(10,C,D,E,T,A,B,W);
325 BODY_00_15(11,B,C,D,E,T,A,W);
326 BODY_00_15(12,A,B,C,D,E,T,W);
327 BODY_00_15(13,T,A,B,C,D,E,W);
328 BODY_00_15(14,E,T,A,B,C,D,W);
329 BODY_00_15(15,D,E,T,A,B,C,W);
330 BODY_16_19(16,C,D,E,T,A,B,W,W,W,W);
331 BODY_16_19(17,B,C,D,E,T,A,W,W,W,W);
332 BODY_16_19(18,A,B,C,D,E,T,W,W,W,W);
333 BODY_16_19(19,T,A,B,C,D,E,W,W,W,X);
334
335 BODY_20_31(20,E,T,A,B,C,D,W,W,W,X);
336 BODY_20_31(21,D,E,T,A,B,C,W,W,W,X);
337 BODY_20_31(22,C,D,E,T,A,B,W,W,W,X);
338 BODY_20_31(23,B,C,D,E,T,A,W,W,W,X);
339 BODY_20_31(24,A,B,C,D,E,T,W,W,X,X);
340 BODY_20_31(25,T,A,B,C,D,E,W,W,X,X);
341 BODY_20_31(26,E,T,A,B,C,D,W,W,X,X);
342 BODY_20_31(27,D,E,T,A,B,C,W,W,X,X);
343 BODY_20_31(28,C,D,E,T,A,B,W,W,X,X);
344 BODY_20_31(29,B,C,D,E,T,A,W,W,X,X);
345 BODY_20_31(30,A,B,C,D,E,T,W,X,X,X);
346 BODY_20_31(31,T,A,B,C,D,E,W,X,X,X);
347 BODY_32_39(32,E,T,A,B,C,D,X);
348 BODY_32_39(33,D,E,T,A,B,C,X);
349 BODY_32_39(34,C,D,E,T,A,B,X);
350 BODY_32_39(35,B,C,D,E,T,A,X);
351 BODY_32_39(36,A,B,C,D,E,T,X);
352 BODY_32_39(37,T,A,B,C,D,E,X);
353 BODY_32_39(38,E,T,A,B,C,D,X);
354 BODY_32_39(39,D,E,T,A,B,C,X);
355
356 BODY_40_59(40,C,D,E,T,A,B,X);
357 BODY_40_59(41,B,C,D,E,T,A,X);
358 BODY_40_59(42,A,B,C,D,E,T,X);
359 BODY_40_59(43,T,A,B,C,D,E,X);
360 BODY_40_59(44,E,T,A,B,C,D,X);
361 BODY_40_59(45,D,E,T,A,B,C,X);
362 BODY_40_59(46,C,D,E,T,A,B,X);
363 BODY_40_59(47,B,C,D,E,T,A,X);
364 BODY_40_59(48,A,B,C,D,E,T,X);
365 BODY_40_59(49,T,A,B,C,D,E,X);
366 BODY_40_59(50,E,T,A,B,C,D,X);
367 BODY_40_59(51,D,E,T,A,B,C,X);
368 BODY_40_59(52,C,D,E,T,A,B,X);
369 BODY_40_59(53,B,C,D,E,T,A,X);
370 BODY_40_59(54,A,B,C,D,E,T,X);
371 BODY_40_59(55,T,A,B,C,D,E,X);
372 BODY_40_59(56,E,T,A,B,C,D,X);
373 BODY_40_59(57,D,E,T,A,B,C,X);
374 BODY_40_59(58,C,D,E,T,A,B,X);
375 BODY_40_59(59,B,C,D,E,T,A,X);
376
377 BODY_60_79(60,A,B,C,D,E,T,X);
378 BODY_60_79(61,T,A,B,C,D,E,X);
379 BODY_60_79(62,E,T,A,B,C,D,X);
380 BODY_60_79(63,D,E,T,A,B,C,X);
381 BODY_60_79(64,C,D,E,T,A,B,X);
382 BODY_60_79(65,B,C,D,E,T,A,X);
383 BODY_60_79(66,A,B,C,D,E,T,X);
384 BODY_60_79(67,T,A,B,C,D,E,X);
385 BODY_60_79(68,E,T,A,B,C,D,X);
386 BODY_60_79(69,D,E,T,A,B,C,X);
387 BODY_60_79(70,C,D,E,T,A,B,X);
388 BODY_60_79(71,B,C,D,E,T,A,X);
389 BODY_60_79(72,A,B,C,D,E,T,X);
390 BODY_60_79(73,T,A,B,C,D,E,X);
391 BODY_60_79(74,E,T,A,B,C,D,X);
392 BODY_60_79(75,D,E,T,A,B,C,X);
393 BODY_60_79(76,C,D,E,T,A,B,X);
394 BODY_60_79(77,B,C,D,E,T,A,X);
395 BODY_60_79(78,A,B,C,D,E,T,X);
396 BODY_60_79(79,T,A,B,C,D,E,X);
397
398 c->h0=(c->h0+E)&0xffffffffL;
399 c->h1=(c->h1+T)&0xffffffffL;
400 c->h2=(c->h2+A)&0xffffffffL;
401 c->h3=(c->h3+B)&0xffffffffL;
402 c->h4=(c->h4+C)&0xffffffffL;
403
404 num-=64;
405 if (num <= 0) break;
406
407 A=c->h0;
408 B=c->h1;
409 C=c->h2;
410 D=c->h3;
411 E=c->h4;
412
413 W+=16;
414 }
415 }
416#endif
417
418void SHA1_Final(md, c)
419unsigned char *md;
420SHA_CTX *c;
421 {
422 register int i,j;
423 register ULONG l;
424 register ULONG *p;
425 static unsigned char end[4]={0x80,0x00,0x00,0x00};
426 unsigned char *cp=end;
427
428 /* c->num should definitly have room for at least one more byte. */
429 p=c->data;
430 j=c->num;
431 i=j>>2;
432#ifdef PURIFY
433 if ((j&0x03) == 0) p[i]=0;
434#endif
435 l=p[i];
436 M_p_c2nl(cp,l,j&0x03);
437 p[i]=l;
438 i++;
439 /* i is the next 'undefined word' */
440 if (c->num >= SHA_LAST_BLOCK)
441 {
442 for (; i<SHA_LBLOCK; i++)
443 p[i]=0;
444 sha1_block(c,p,64);
445 i=0;
446 }
447 for (; i<(SHA_LBLOCK-2); i++)
448 p[i]=0;
449 p[SHA_LBLOCK-2]=c->Nh;
450 p[SHA_LBLOCK-1]=c->Nl;
451#if defined(L_ENDIAN) && defined(SHA1_ASM)
452 Endian_Reverse32(p[SHA_LBLOCK-2]);
453 Endian_Reverse32(p[SHA_LBLOCK-1]);
454#endif
455 sha1_block(c,p,64);
456 cp=md;
457 l=c->h0; nl2c(l,cp);
458 l=c->h1; nl2c(l,cp);
459 l=c->h2; nl2c(l,cp);
460 l=c->h3; nl2c(l,cp);
461 l=c->h4; nl2c(l,cp);
462
463 /* clear stuff, sha1_block may be leaving some stuff on the stack
464 * but I'm not worried :-) */
465 c->num=0;
466/* memset((char *)&c,0,sizeof(c));*/
467 }
468
diff --git a/src/lib/libcrypto/sha/sha_locl.h b/src/lib/libcrypto/sha/sha_locl.h
new file mode 100644
index 0000000000..2814ad15fa
--- /dev/null
+++ b/src/lib/libcrypto/sha/sha_locl.h
@@ -0,0 +1,246 @@
1/* crypto/sha/sha_locl.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdlib.h>
60#include <string.h>
61
62#ifdef undef
63/* one or the other needs to be defined */
64#ifndef SHA_1 /* FIPE 180-1 */
65#define SHA_0 /* FIPS 180 */
66#endif
67#endif
68
69#define ULONG unsigned long
70#define UCHAR unsigned char
71#define UINT unsigned int
72
73#ifdef NOCONST
74#define const
75#endif
76
77#undef c2nl
78#define c2nl(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
79 l|=(((unsigned long)(*((c)++)))<<16), \
80 l|=(((unsigned long)(*((c)++)))<< 8), \
81 l|=(((unsigned long)(*((c)++))) ))
82
83#undef p_c2nl
84#define p_c2nl(c,l,n) { \
85 switch (n) { \
86 case 0: l =((unsigned long)(*((c)++)))<<24; \
87 case 1: l|=((unsigned long)(*((c)++)))<<16; \
88 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
89 case 3: l|=((unsigned long)(*((c)++))); \
90 } \
91 }
92
93#undef c2nl_p
94/* NOTE the pointer is not incremented at the end of this */
95#define c2nl_p(c,l,n) { \
96 l=0; \
97 (c)+=n; \
98 switch (n) { \
99 case 3: l =((unsigned long)(*(--(c))))<< 8; \
100 case 2: l|=((unsigned long)(*(--(c))))<<16; \
101 case 1: l|=((unsigned long)(*(--(c))))<<24; \
102 } \
103 }
104
105#undef p_c2nl_p
106#define p_c2nl_p(c,l,sc,len) { \
107 switch (sc) \
108 { \
109 case 0: l =((unsigned long)(*((c)++)))<<24; \
110 if (--len == 0) break; \
111 case 1: l|=((unsigned long)(*((c)++)))<<16; \
112 if (--len == 0) break; \
113 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
114 } \
115 }
116
117#undef nl2c
118#define nl2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
119 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
120 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
121 *((c)++)=(unsigned char)(((l) )&0xff))
122
123#undef c2l
124#define c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
125 l|=(((unsigned long)(*((c)++)))<< 8), \
126 l|=(((unsigned long)(*((c)++)))<<16), \
127 l|=(((unsigned long)(*((c)++)))<<24))
128
129#undef p_c2l
130#define p_c2l(c,l,n) { \
131 switch (n) { \
132 case 0: l =((unsigned long)(*((c)++))); \
133 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
134 case 2: l|=((unsigned long)(*((c)++)))<<16; \
135 case 3: l|=((unsigned long)(*((c)++)))<<24; \
136 } \
137 }
138
139#undef c2l_p
140/* NOTE the pointer is not incremented at the end of this */
141#define c2l_p(c,l,n) { \
142 l=0; \
143 (c)+=n; \
144 switch (n) { \
145 case 3: l =((unsigned long)(*(--(c))))<<16; \
146 case 2: l|=((unsigned long)(*(--(c))))<< 8; \
147 case 1: l|=((unsigned long)(*(--(c)))); \
148 } \
149 }
150
151#undef p_c2l_p
152#define p_c2l_p(c,l,sc,len) { \
153 switch (sc) \
154 { \
155 case 0: l =((unsigned long)(*((c)++))); \
156 if (--len == 0) break; \
157 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
158 if (--len == 0) break; \
159 case 2: l|=((unsigned long)(*((c)++)))<<16; \
160 } \
161 }
162
163#undef l2c
164#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
165 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
166 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
167 *((c)++)=(unsigned char)(((l)>>24)&0xff))
168
169#undef ROTATE
170#if defined(WIN32)
171#define ROTATE(a,n) _lrotl(a,n)
172#else
173#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
174#endif
175
176/* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
177#if defined(WIN32)
178/* 5 instructions with rotate instruction, else 9 */
179#define Endian_Reverse32(a) \
180 { \
181 unsigned long l=(a); \
182 (a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \
183 }
184#else
185/* 6 instructions with rotate instruction, else 8 */
186#define Endian_Reverse32(a) \
187 { \
188 unsigned long l=(a); \
189 l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
190 (a)=ROTATE(l,16L); \
191 }
192#endif
193
194/* As pointed out by Wei Dai <weidai@eskimo.com>, F() below can be
195 * simplified to the code in F_00_19. Wei attributes these optimisations
196 * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
197 * #define F(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
198 * I've just become aware of another tweak to be made, again from Wei Dai,
199 * in F_40_59, (x&a)|(y&a) -> (x|y)&a
200 */
201#define F_00_19(b,c,d) ((((c) ^ (d)) & (b)) ^ (d))
202#define F_20_39(b,c,d) ((b) ^ (c) ^ (d))
203#define F_40_59(b,c,d) (((b) & (c)) | (((b)|(c)) & (d)))
204#define F_60_79(b,c,d) F_20_39(b,c,d)
205
206#ifdef SHA_0
207#undef Xupdate
208#define Xupdate(a,i,ia,ib,ic,id) X[(i)&0x0f]=(a)=\
209 (ia[(i)&0x0f]^ib[((i)+2)&0x0f]^ic[((i)+8)&0x0f]^id[((i)+13)&0x0f]);
210#endif
211#ifdef SHA_1
212#undef Xupdate
213#define Xupdate(a,i,ia,ib,ic,id) (a)=\
214 (ia[(i)&0x0f]^ib[((i)+2)&0x0f]^ic[((i)+8)&0x0f]^id[((i)+13)&0x0f]);\
215 X[(i)&0x0f]=(a)=ROTATE((a),1);
216#endif
217
218#define BODY_00_15(i,a,b,c,d,e,f,xa) \
219 (f)=xa[i]+(e)+K_00_19+ROTATE((a),5)+F_00_19((b),(c),(d)); \
220 (b)=ROTATE((b),30);
221
222#define BODY_16_19(i,a,b,c,d,e,f,xa,xb,xc,xd) \
223 Xupdate(f,i,xa,xb,xc,xd); \
224 (f)+=(e)+K_00_19+ROTATE((a),5)+F_00_19((b),(c),(d)); \
225 (b)=ROTATE((b),30);
226
227#define BODY_20_31(i,a,b,c,d,e,f,xa,xb,xc,xd) \
228 Xupdate(f,i,xa,xb,xc,xd); \
229 (f)+=(e)+K_20_39+ROTATE((a),5)+F_20_39((b),(c),(d)); \
230 (b)=ROTATE((b),30);
231
232#define BODY_32_39(i,a,b,c,d,e,f,xa) \
233 Xupdate(f,i,xa,xa,xa,xa); \
234 (f)+=(e)+K_20_39+ROTATE((a),5)+F_20_39((b),(c),(d)); \
235 (b)=ROTATE((b),30);
236
237#define BODY_40_59(i,a,b,c,d,e,f,xa) \
238 Xupdate(f,i,xa,xa,xa,xa); \
239 (f)+=(e)+K_40_59+ROTATE((a),5)+F_40_59((b),(c),(d)); \
240 (b)=ROTATE((b),30);
241
242#define BODY_60_79(i,a,b,c,d,e,f,xa) \
243 Xupdate(f,i,xa,xa,xa,xa); \
244 (f)=X[(i)&0x0f]+(e)+K_60_79+ROTATE((a),5)+F_60_79((b),(c),(d)); \
245 (b)=ROTATE((b),30);
246
diff --git a/src/lib/libcrypto/stack/stack.c b/src/lib/libcrypto/stack/stack.c
new file mode 100644
index 0000000000..610ccbb756
--- /dev/null
+++ b/src/lib/libcrypto/stack/stack.c
@@ -0,0 +1,307 @@
1/* crypto/stack/stack.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Code for stacks
60 * Author - Eric Young v 1.0
61 * 1.2 eay 12-Mar-97 - Modified sk_find so that it _DOES_ return the
62 * lowest index for the seached item.
63 *
64 * 1.1 eay - Take from netdb and added to SSLeay
65 *
66 * 1.0 eay - First version 29/07/92
67 */
68#include <stdio.h>
69#include "cryptlib.h"
70#include "stack.h"
71
72#undef MIN_NODES
73#define MIN_NODES 4
74
75char *STACK_version="STACK part of SSLeay 0.9.0b 29-Jun-1998";
76
77#ifndef NOPROTO
78#define FP_ICC (int (*)(const void *,const void *))
79#else
80#define FP_ICC
81#endif
82
83#include <errno.h>
84
85void sk_set_cmp_func(sk,c)
86STACK *sk;
87int (*c)();
88 {
89 if (sk->comp != c)
90 sk->sorted=0;
91 sk->comp=c;
92 }
93
94STACK *sk_dup(sk)
95STACK *sk;
96 {
97 STACK *ret;
98 char **s;
99
100 if ((ret=sk_new(sk->comp)) == NULL) goto err;
101 s=(char **)Realloc((char *)ret->data,
102 (unsigned int)sizeof(char *)*sk->num_alloc);
103 if (s == NULL) goto err;
104 ret->data=s;
105
106 ret->num=sk->num;
107 memcpy(ret->data,sk->data,sizeof(char *)*sk->num);
108 ret->sorted=sk->sorted;
109 ret->num_alloc=sk->num_alloc;
110 ret->comp=sk->comp;
111 return(ret);
112err:
113 return(NULL);
114 }
115
116STACK *sk_new(c)
117int (*c)();
118 {
119 STACK *ret;
120 int i;
121
122 if ((ret=(STACK *)Malloc(sizeof(STACK))) == NULL)
123 goto err0;
124 if ((ret->data=(char **)Malloc(sizeof(char *)*MIN_NODES)) == NULL)
125 goto err1;
126 for (i=0; i<MIN_NODES; i++)
127 ret->data[i]=NULL;
128 ret->comp=c;
129 ret->num_alloc=MIN_NODES;
130 ret->num=0;
131 ret->sorted=0;
132 return(ret);
133err1:
134 Free((char *)ret);
135err0:
136 return(NULL);
137 }
138
139int sk_insert(st,data,loc)
140STACK *st;
141char *data;
142int loc;
143 {
144 char **s;
145
146 if (st->num_alloc <= st->num+1)
147 {
148 s=(char **)Realloc((char *)st->data,
149 (unsigned int)sizeof(char *)*st->num_alloc*2);
150 if (s == NULL)
151 return(0);
152 st->data=s;
153 st->num_alloc*=2;
154 }
155 if ((loc >= (int)st->num) || (loc < 0))
156 st->data[st->num]=data;
157 else
158 {
159 int i;
160 char **f,**t;
161
162 f=(char **)st->data;
163 t=(char **)&(st->data[1]);
164 for (i=st->num; i>loc; i--)
165 t[i]=f[i];
166
167#ifdef undef /* no memmove on sunos :-( */
168 memmove( (char *)&(st->data[loc+1]),
169 (char *)&(st->data[loc]),
170 sizeof(char *)*(st->num-loc));
171#endif
172 st->data[loc]=data;
173 }
174 st->num++;
175 st->sorted=0;
176 return(st->num);
177 }
178
179char *sk_delete_ptr(st,p)
180STACK *st;
181char *p;
182 {
183 int i;
184
185 for (i=0; i<st->num; i++)
186 if (st->data[i] == p)
187 return(sk_delete(st,i));
188 return(NULL);
189 }
190
191char *sk_delete(st,loc)
192STACK *st;
193int loc;
194 {
195 char *ret;
196 int i,j;
197
198 if ((st->num == 0) || (loc < 0) || (loc >= st->num)) return(NULL);
199
200 ret=st->data[loc];
201 if (loc != st->num-1)
202 {
203 j=st->num-1;
204 for (i=loc; i<j; i++)
205 st->data[i]=st->data[i+1];
206 /* In theory memcpy is not safe for this
207 * memcpy( &(st->data[loc]),
208 * &(st->data[loc+1]),
209 * sizeof(char *)*(st->num-loc-1));
210 */
211 }
212 st->num--;
213 return(ret);
214 }
215
216int sk_find(st,data)
217STACK *st;
218char *data;
219 {
220 char **r;
221 int i;
222 int (*comp_func)();
223
224 if (st->comp == NULL)
225 {
226 for (i=0; i<st->num; i++)
227 if (st->data[i] == data)
228 return(i);
229 return(-1);
230 }
231 comp_func=(int (*)())st->comp;
232 if (!st->sorted)
233 {
234 qsort((char *)st->data,st->num,sizeof(char *),FP_ICC comp_func);
235 st->sorted=1;
236 }
237 if (data == NULL) return(-1);
238 r=(char **)bsearch(&data,(char *)st->data,
239 st->num,sizeof(char *),FP_ICC comp_func);
240 if (r == NULL) return(-1);
241 i=(int)(r-st->data);
242 for ( ; i>0; i--)
243 if ((*st->comp)(&(st->data[i-1]),&data) < 0)
244 break;
245 return(i);
246 }
247
248int sk_push(st,data)
249STACK *st;
250char *data;
251 {
252 return(sk_insert(st,data,st->num));
253 }
254
255int sk_unshift(st,data)
256STACK *st;
257char *data;
258 {
259 return(sk_insert(st,data,0));
260 }
261
262char *sk_shift(st)
263STACK *st;
264 {
265 if (st == NULL) return(NULL);
266 if (st->num <= 0) return(NULL);
267 return(sk_delete(st,0));
268 }
269
270char *sk_pop(st)
271STACK *st;
272 {
273 if (st == NULL) return(NULL);
274 if (st->num <= 0) return(NULL);
275 return(sk_delete(st,st->num-1));
276 }
277
278void sk_zero(st)
279STACK *st;
280 {
281 if (st == NULL) return;
282 if (st->num <= 0) return;
283 memset((char *)st->data,0,sizeof(st->data)*st->num);
284 st->num=0;
285 }
286
287void sk_pop_free(st,func)
288STACK *st;
289void (*func)();
290 {
291 int i;
292
293 if (st == NULL) return;
294 for (i=0; i<st->num; i++)
295 if (st->data[i] != NULL)
296 func(st->data[i]);
297 sk_free(st);
298 }
299
300void sk_free(st)
301STACK *st;
302 {
303 if (st == NULL) return;
304 if (st->data != NULL) Free((char *)st->data);
305 Free((char *)st);
306 }
307
diff --git a/src/lib/libcrypto/stack/stack.h b/src/lib/libcrypto/stack/stack.h
new file mode 100644
index 0000000000..615eb6ff94
--- /dev/null
+++ b/src/lib/libcrypto/stack/stack.h
@@ -0,0 +1,120 @@
1/* crypto/stack/stack.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_STACK_H
60#define HEADER_STACK_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66typedef struct stack_st
67 {
68 int num;
69 char **data;
70 int sorted;
71
72 int num_alloc;
73 int (*comp)();
74 } STACK;
75
76#define sk_num(sk) ((sk)->num)
77#define sk_value(sk,n) ((sk)->data[n])
78
79#define sk_new_null() sk_new(NULL)
80#ifndef NOPROTO
81
82STACK *sk_new(int (*cmp)());
83void sk_free(STACK *);
84void sk_pop_free(STACK *st, void (*func)());
85int sk_insert(STACK *sk,char *data,int where);
86char *sk_delete(STACK *st,int loc);
87char *sk_delete_ptr(STACK *st, char *p);
88int sk_find(STACK *st,char *data);
89int sk_push(STACK *st,char *data);
90int sk_unshift(STACK *st,char *data);
91char *sk_shift(STACK *st);
92char *sk_pop(STACK *st);
93void sk_zero(STACK *st);
94void sk_set_cmp_func(STACK *sk, int (*c)());
95STACK *sk_dup(STACK *st);
96
97#else
98
99STACK *sk_new();
100void sk_free();
101void sk_pop_free();
102int sk_insert();
103char *sk_delete();
104char *sk_delete_ptr();
105int sk_find();
106int sk_push();
107int sk_unshift();
108char *sk_shift();
109char *sk_pop();
110void sk_zero();
111void sk_set_cmp_func();
112STACK *sk_dup();
113
114#endif
115
116#ifdef __cplusplus
117}
118#endif
119
120#endif
diff --git a/src/lib/libcrypto/txt_db/txt_db.c b/src/lib/libcrypto/txt_db/txt_db.c
new file mode 100644
index 0000000000..e34ce4efa9
--- /dev/null
+++ b/src/lib/libcrypto/txt_db/txt_db.c
@@ -0,0 +1,394 @@
1/* crypto/txt_db/txt_db.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include "cryptlib.h"
63#include "buffer.h"
64#include "txt_db.h"
65
66#undef BUFSIZE
67#define BUFSIZE 512
68
69char *TXT_DB_version="TXT_DB part of SSLeay 0.9.0b 29-Jun-1998";
70
71TXT_DB *TXT_DB_read(in,num)
72BIO *in;
73int num;
74 {
75 TXT_DB *ret=NULL;
76 int er=1;
77 int esc=0;
78 long ln=0;
79 int i,add,n;
80 int size=BUFSIZE;
81 int offset=0;
82 char *p,**pp,*f;
83 BUF_MEM *buf=NULL;
84
85 if ((buf=BUF_MEM_new()) == NULL) goto err;
86 if (!BUF_MEM_grow(buf,size)) goto err;
87
88 if ((ret=(TXT_DB *)Malloc(sizeof(TXT_DB))) == NULL)
89 goto err;
90 ret->num_fields=num;
91 ret->index=NULL;
92 ret->qual=NULL;
93 if ((ret->data=sk_new_null()) == NULL)
94 goto err;
95 if ((ret->index=(LHASH **)Malloc(sizeof(LHASH *)*num)) == NULL)
96 goto err;
97 if ((ret->qual=(int (**)())Malloc(sizeof(int (**)())*num)) == NULL)
98 goto err;
99 for (i=0; i<num; i++)
100 {
101 ret->index[i]=NULL;
102 ret->qual[i]=NULL;
103 }
104
105 add=(num+1)*sizeof(char *);
106 buf->data[size-1]='\0';
107 offset=0;
108 for (;;)
109 {
110 if (offset != 0)
111 {
112 size+=BUFSIZE;
113 if (!BUF_MEM_grow(buf,size)) goto err;
114 }
115 buf->data[offset]='\0';
116 BIO_gets(in,&(buf->data[offset]),size-offset);
117 ln++;
118 if (buf->data[offset] == '\0') break;
119 if ((offset == 0) && (buf->data[0] == '#')) continue;
120 i=strlen(&(buf->data[offset]));
121 offset+=i;
122 if (buf->data[offset-1] != '\n')
123 continue;
124 else
125 {
126 buf->data[offset-1]='\0'; /* blat the '\n' */
127 p=(char *)Malloc(add+offset);
128 offset=0;
129 }
130 pp=(char **)p;
131 p+=add;
132 n=0;
133 pp[n++]=p;
134 i=0;
135 f=buf->data;
136
137 esc=0;
138 for (;;)
139 {
140 if (*f == '\0') break;
141 if (*f == '\t')
142 {
143 if (esc)
144 p--;
145 else
146 {
147 *(p++)='\0';
148 f++;
149 if (n >= num) break;
150 pp[n++]=p;
151 continue;
152 }
153 }
154 esc=(*f == '\\');
155 *(p++)= *(f++);
156 }
157 *(p++)='\0';
158 if ((n != num) || (*f != '\0'))
159 {
160#if !defined(NO_STDIO) && !defined(WIN16) /* temporaty fix :-( */
161 fprintf(stderr,"wrong number of fields on line %ld\n",ln);
162#endif
163 er=2;
164 goto err;
165 }
166 pp[n]=p;
167 if (!sk_push(ret->data,(char *)pp))
168 {
169#if !defined(NO_STDIO) && !defined(WIN16) /* temporaty fix :-( */
170 fprintf(stderr,"failure in sk_push\n");
171#endif
172 er=2;
173 goto err;
174 }
175 }
176 er=0;
177err:
178 BUF_MEM_free(buf);
179 if (er)
180 {
181#if !defined(NO_STDIO) && !defined(WIN16)
182 if (er == 1) fprintf(stderr,"Malloc failure\n");
183#endif
184 if (ret->data != NULL) sk_free(ret->data);
185 if (ret->index != NULL) Free(ret->index);
186 if (ret->qual != NULL) Free((char *)ret->qual);
187 if (ret != NULL) Free(ret);
188 return(NULL);
189 }
190 else
191 return(ret);
192 }
193
194char **TXT_DB_get_by_index(db,idx,value)
195TXT_DB *db;
196int idx;
197char **value;
198 {
199 char **ret;
200 LHASH *lh;
201
202 if (idx >= db->num_fields)
203 {
204 db->error=DB_ERROR_INDEX_OUT_OF_RANGE;
205 return(NULL);
206 }
207 lh=db->index[idx];
208 if (lh == NULL)
209 {
210 db->error=DB_ERROR_NO_INDEX;
211 return(NULL);
212 }
213 ret=(char **)lh_retrieve(lh,(char *)value);
214 db->error=DB_ERROR_OK;
215 return(ret);
216 }
217
218int TXT_DB_create_index(db,field,qual,hash,cmp)
219TXT_DB *db;
220int field;
221int (*qual)();
222unsigned long (*hash)();
223int (*cmp)();
224 {
225 LHASH *idx;
226 char *r;
227 int i,n;
228
229 if (field >= db->num_fields)
230 {
231 db->error=DB_ERROR_INDEX_OUT_OF_RANGE;
232 return(0);
233 }
234 if ((idx=lh_new(hash,cmp)) == NULL)
235 {
236 db->error=DB_ERROR_MALLOC;
237 return(0);
238 }
239 n=sk_num(db->data);
240 for (i=0; i<n; i++)
241 {
242 r=(char *)sk_value(db->data,i);
243 if ((qual != NULL) && (qual(r) == 0)) continue;
244 if ((r=lh_insert(idx,r)) != NULL)
245 {
246 db->error=DB_ERROR_INDEX_CLASH;
247 db->arg1=sk_find(db->data,r);
248 db->arg2=i;
249 lh_free(idx);
250 return(0);
251 }
252 }
253 if (db->index[field] != NULL) lh_free(db->index[field]);
254 db->index[field]=idx;
255 db->qual[field]=qual;
256 return(1);
257 }
258
259long TXT_DB_write(out,db)
260BIO *out;
261TXT_DB *db;
262 {
263 long i,j,n,nn,l,tot=0;
264 char *p,**pp,*f;
265 BUF_MEM *buf=NULL;
266 long ret= -1;
267
268 if ((buf=BUF_MEM_new()) == NULL)
269 goto err;
270 n=sk_num(db->data);
271 nn=db->num_fields;
272 for (i=0; i<n; i++)
273 {
274 pp=(char **)sk_value(db->data,i);
275
276 l=0;
277 for (j=0; j<nn; j++)
278 {
279 if (pp[j] != NULL)
280 l+=strlen(pp[j]);
281 }
282 if (!BUF_MEM_grow(buf,(int)(l*2+nn))) goto err;
283
284 p=buf->data;
285 for (j=0; j<nn; j++)
286 {
287 f=pp[j];
288 if (f != NULL)
289 for (;;)
290 {
291 if (*f == '\0') break;
292 if (*f == '\t') *(p++)='\\';
293 *(p++)= *(f++);
294 }
295 *(p++)='\t';
296 }
297 p[-1]='\n';
298 j=p-buf->data;
299 if (BIO_write(out,buf->data,(int)j) != j)
300 goto err;
301 tot+=j;
302 }
303 ret=tot;
304err:
305 if (buf != NULL) BUF_MEM_free(buf);
306 return(ret);
307 }
308
309int TXT_DB_insert(db,row)
310TXT_DB *db;
311char **row;
312 {
313 int i;
314 char **r;
315
316 for (i=0; i<db->num_fields; i++)
317 {
318 if (db->index[i] != NULL)
319 {
320 if ((db->qual[i] != NULL) &&
321 (db->qual[i](row) == 0)) continue;
322 r=(char **)lh_retrieve(db->index[i],(char *)row);
323 if (r != NULL)
324 {
325 db->error=DB_ERROR_INDEX_CLASH;
326 db->arg1=i;
327 db->arg_row=r;
328 goto err;
329 }
330 }
331 }
332 /* We have passed the index checks, now just append and insert */
333 if (!sk_push(db->data,(char *)row))
334 {
335 db->error=DB_ERROR_MALLOC;
336 goto err;
337 }
338
339 for (i=0; i<db->num_fields; i++)
340 {
341 if (db->index[i] != NULL)
342 {
343 if ((db->qual[i] != NULL) &&
344 (db->qual[i](row) == 0)) continue;
345 lh_insert(db->index[i],(char *)row);
346 }
347 }
348 return(1);
349err:
350 return(0);
351 }
352
353void TXT_DB_free(db)
354TXT_DB *db;
355 {
356 int i,n;
357 char **p,*max;
358
359 if (db->index != NULL)
360 {
361 for (i=db->num_fields-1; i>=0; i--)
362 if (db->index[i] != NULL) lh_free(db->index[i]);
363 Free(db->index);
364 }
365 if (db->qual != NULL)
366 Free(db->qual);
367 if (db->data != NULL)
368 {
369 for (i=sk_num(db->data)-1; i>=0; i--)
370 {
371 /* check if any 'fields' have been allocated
372 * from outside of the initial block */
373 p=(char **)sk_value(db->data,i);
374 max=p[db->num_fields]; /* last address */
375 if (max == NULL) /* new row */
376 {
377 for (n=0; n<db->num_fields; n++)
378 if (p[n] != NULL) Free(p[n]);
379 }
380 else
381 {
382 for (n=0; n<db->num_fields; n++)
383 {
384 if (((p[n] < (char *)p) || (p[n] > max))
385 && (p[n] != NULL))
386 Free(p[n]);
387 }
388 }
389 Free(sk_value(db->data,i));
390 }
391 sk_free(db->data);
392 }
393 Free(db);
394 }
diff --git a/src/lib/libcrypto/txt_db/txt_db.h b/src/lib/libcrypto/txt_db/txt_db.h
new file mode 100644
index 0000000000..aca6dae393
--- /dev/null
+++ b/src/lib/libcrypto/txt_db/txt_db.h
@@ -0,0 +1,117 @@
1/* crypto/txt_db/txt_db.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_TXT_DB_H
60#define HEADER_TXT_DB_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "stack.h"
67#include "lhash.h"
68
69#define DB_ERROR_OK 0
70#define DB_ERROR_MALLOC 1
71#define DB_ERROR_INDEX_CLASH 2
72#define DB_ERROR_INDEX_OUT_OF_RANGE 3
73#define DB_ERROR_NO_INDEX 4
74#define DB_ERROR_INSERT_INDEX_CLASH 5
75
76typedef struct txt_db_st
77 {
78 int num_fields;
79 STACK /* char ** */ *data;
80 LHASH **index;
81 int (**qual)();
82 long error;
83 long arg1;
84 long arg2;
85 char **arg_row;
86 } TXT_DB;
87
88#ifndef NOPROTO
89#ifdef HEADER_BIO_H
90TXT_DB *TXT_DB_read(BIO *in, int num);
91long TXT_DB_write(BIO *out, TXT_DB *db);
92#else
93TXT_DB *TXT_DB_read(char *in, int num);
94long TXT_DB_write(char *out, TXT_DB *db);
95#endif
96int TXT_DB_create_index(TXT_DB *db,int field,int (*qual)(),
97 unsigned long (*hash)(),int (*cmp)());
98void TXT_DB_free(TXT_DB *db);
99char **TXT_DB_get_by_index(TXT_DB *db, int idx, char **value);
100int TXT_DB_insert(TXT_DB *db,char **value);
101
102#else
103
104TXT_DB *TXT_DB_read();
105long TXT_DB_write();
106int TXT_DB_create_index();
107void TXT_DB_free();
108char **TXT_DB_get_by_index();
109int TXT_DB_insert();
110
111#endif
112
113#ifdef __cplusplus
114}
115#endif
116
117#endif
diff --git a/src/lib/libcrypto/x509/by_dir.c b/src/lib/libcrypto/x509/by_dir.c
new file mode 100644
index 0000000000..11725ec94c
--- /dev/null
+++ b/src/lib/libcrypto/x509/by_dir.c
@@ -0,0 +1,359 @@
1/* crypto/x509/by_dir.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include <errno.h>
62#include <sys/types.h>
63#include <sys/stat.h>
64
65#include "cryptlib.h"
66#include "lhash.h"
67#include "x509.h"
68#include "pem.h"
69
70typedef struct lookup_dir_st
71 {
72 BUF_MEM *buffer;
73 int num_dirs;
74 char **dirs;
75 int *dirs_type;
76 int num_dirs_alloced;
77 } BY_DIR;
78
79#ifndef NOPROTO
80static int dir_ctrl(X509_LOOKUP *ctx,int cmd,char *argp,long argl,char **ret);
81static int new_dir(X509_LOOKUP *lu);
82static void free_dir(X509_LOOKUP *lu);
83static int add_cert_dir(BY_DIR *ctx,char *dir,int type);
84static int get_cert_by_subject(X509_LOOKUP *xl,int type,X509_NAME *name,
85 X509_OBJECT *ret);
86#else
87static int dir_ctrl();
88static int new_dir();
89static void free_dir();
90static int add_cert_dir();
91static int get_cert_by_subject();
92#endif
93
94X509_LOOKUP_METHOD x509_dir_lookup=
95 {
96 "Load certs from files in a directory",
97 new_dir, /* new */
98 free_dir, /* free */
99 NULL, /* init */
100 NULL, /* shutdown */
101 dir_ctrl, /* ctrl */
102 get_cert_by_subject, /* get_by_subject */
103 NULL, /* get_by_issuer_serial */
104 NULL, /* get_by_fingerprint */
105 NULL, /* get_by_alias */
106 };
107
108X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir()
109 {
110 return(&x509_dir_lookup);
111 }
112
113static int dir_ctrl(ctx,cmd,argp,argl,retp)
114X509_LOOKUP *ctx;
115int cmd;
116long argl;
117char *argp;
118char **retp;
119 {
120 int ret=0;
121 BY_DIR *ld;
122 char *dir;
123
124 ld=(BY_DIR *)ctx->method_data;
125
126 switch (cmd)
127 {
128 case X509_L_ADD_DIR:
129 if (argl == X509_FILETYPE_DEFAULT)
130 {
131 ret=add_cert_dir(ld,X509_get_default_cert_dir(),
132 X509_FILETYPE_PEM);
133 if (!ret)
134 {
135 X509err(X509_F_DIR_CTRL,X509_R_LOADING_CERT_DIR);
136 }
137 else
138 {
139 dir=(char *)Getenv(X509_get_default_cert_dir_env());
140 ret=add_cert_dir(ld,dir,X509_FILETYPE_PEM);
141 }
142 }
143 else
144 ret=add_cert_dir(ld,argp,(int)argl);
145 break;
146 }
147 return(ret);
148 }
149
150static int new_dir(lu)
151X509_LOOKUP *lu;
152 {
153 BY_DIR *a;
154
155 if ((a=(BY_DIR *)Malloc(sizeof(BY_DIR))) == NULL)
156 return(0);
157 if ((a->buffer=BUF_MEM_new()) == NULL)
158 {
159 Free(a);
160 return(0);
161 }
162 a->num_dirs=0;
163 a->dirs=NULL;
164 a->dirs_type=NULL;
165 a->num_dirs_alloced=0;
166 lu->method_data=(char *)a;
167 return(1);
168 }
169
170static void free_dir(lu)
171X509_LOOKUP *lu;
172 {
173 BY_DIR *a;
174 int i;
175
176 a=(BY_DIR *)lu->method_data;
177 for (i=0; i<a->num_dirs; i++)
178 if (a->dirs[i] != NULL) Free(a->dirs[i]);
179 if (a->dirs != NULL) Free(a->dirs);
180 if (a->dirs_type != NULL) Free(a->dirs_type);
181 if (a->buffer != NULL) BUF_MEM_free(a->buffer);
182 Free(a);
183 }
184
185static int add_cert_dir(ctx,dir, type)
186BY_DIR *ctx;
187char *dir;
188int type;
189 {
190 int j,len;
191 int *ip;
192 char *s,*ss,*p;
193 char **pp;
194
195 if (dir == NULL) return(0);
196
197 s=dir;
198 p=s;
199 for (;;)
200 {
201 if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0'))
202 {
203 ss=s;
204 s=p+1;
205 len=(int)(p-ss);
206 if (len == 0) continue;
207 for (j=0; j<ctx->num_dirs; j++)
208 if (strncmp(ctx->dirs[j],ss,(unsigned int)len) == 0)
209 continue;
210 if (ctx->num_dirs_alloced < (ctx->num_dirs+1))
211 {
212 ctx->num_dirs_alloced+=10;
213 pp=(char **)Malloc(ctx->num_dirs_alloced*
214 sizeof(char *));
215 ip=(int *)Malloc(ctx->num_dirs_alloced*
216 sizeof(int));
217 if ((pp == NULL) || (ip == NULL))
218 {
219 X509err(X509_F_ADD_CERT_DIR,ERR_R_MALLOC_FAILURE);
220 return(0);
221 }
222 memcpy(pp,ctx->dirs,(ctx->num_dirs_alloced-10)*
223 sizeof(char *));
224 memcpy(ip,ctx->dirs_type,(ctx->num_dirs_alloced-10)*
225 sizeof(int));
226 if (ctx->dirs != NULL)
227 Free((char *)ctx->dirs);
228 if (ctx->dirs_type != NULL)
229 Free((char *)ctx->dirs_type);
230 ctx->dirs=pp;
231 ctx->dirs_type=ip;
232 }
233 ctx->dirs_type[ctx->num_dirs]=type;
234 ctx->dirs[ctx->num_dirs]=(char *)Malloc((unsigned int)len+1);
235 if (ctx->dirs[ctx->num_dirs] == NULL) return(0);
236 strncpy(ctx->dirs[ctx->num_dirs],ss,(unsigned int)len);
237 ctx->dirs[ctx->num_dirs][len]='\0';
238 ctx->num_dirs++;
239 }
240 if (*p == '\0') break;
241 p++;
242 }
243 return(1);
244 }
245
246static int get_cert_by_subject(xl,type,name,ret)
247X509_LOOKUP *xl;
248int type;
249X509_NAME *name;
250X509_OBJECT *ret;
251 {
252 BY_DIR *ctx;
253 union {
254 struct {
255 X509 st_x509;
256 X509_CINF st_x509_cinf;
257 } x509;
258 struct {
259 X509_CRL st_crl;
260 X509_CRL_INFO st_crl_info;
261 } crl;
262 } data;
263 int ok=0;
264 int i,j,k;
265 unsigned long h;
266 BUF_MEM *b=NULL;
267 struct stat st;
268 X509_OBJECT stmp,*tmp;
269 char *postfix="";
270
271 if (name == NULL) return(0);
272
273 stmp.type=type;
274 if (type == X509_LU_X509)
275 {
276 data.x509.st_x509.cert_info= &data.x509.st_x509_cinf;
277 data.x509.st_x509_cinf.subject=name;
278 stmp.data.x509= &data.x509.st_x509;
279 postfix="";
280 }
281 else if (type == X509_LU_CRL)
282 {
283 data.crl.st_crl.crl= &data.crl.st_crl_info;
284 data.crl.st_crl_info.issuer=name;
285 stmp.data.crl= &data.crl.st_crl;
286 postfix="r";
287 }
288 else
289 {
290 X509err(X509_F_GET_CERT_BY_SUBJECT,X509_R_WRONG_LOOKUP_TYPE);
291 goto finish;
292 }
293
294 if ((b=BUF_MEM_new()) == NULL)
295 {
296 X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_BUF_LIB);
297 goto finish;
298 }
299
300 ctx=(BY_DIR *)xl->method_data;
301
302 h=X509_NAME_hash(name);
303 for (i=0; i<ctx->num_dirs; i++)
304 {
305 j=strlen(ctx->dirs[i])+1+8+6+1+1;
306 if (!BUF_MEM_grow(b,j))
307 {
308 X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_MALLOC_FAILURE);
309 goto finish;
310 }
311 k=0;
312 for (;;)
313 {
314 sprintf(b->data,"%s/%08lx.%s%d",ctx->dirs[i],h,
315 postfix,k);
316 k++;
317 if (stat(b->data,&st) < 0)
318 break;
319 /* found one. */
320 if (type == X509_LU_X509)
321 {
322 if ((X509_load_cert_file(xl,b->data,
323 ctx->dirs_type[i])) == 0)
324 break;
325 }
326 else if (type == X509_LU_CRL)
327 {
328 if ((X509_load_crl_file(xl,b->data,
329 ctx->dirs_type[i])) == 0)
330 break;
331 }
332 /* else case will caught higher up */
333 }
334
335 /* we have added it to the cache so now pull
336 * it out again */
337 CRYPTO_r_lock(CRYPTO_LOCK_X509_STORE);
338 tmp=(X509_OBJECT *)lh_retrieve(xl->store_ctx->certs,
339 (char *)&stmp);
340 CRYPTO_r_unlock(CRYPTO_LOCK_X509_STORE);
341
342 if (tmp != NULL)
343 {
344 ok=1;
345 ret->type=tmp->type;
346 memcpy(&ret->data,&tmp->data,sizeof(ret->data));
347 /* If we were going to up the reference count,
348 * we would need to do it on a perl 'type'
349 * basis */
350 /* CRYPTO_add(&tmp->data.x509->references,1,
351 CRYPTO_LOCK_X509);*/
352 goto finish;
353 }
354 }
355finish:
356 if (b != NULL) BUF_MEM_free(b);
357 return(ok);
358 }
359
diff --git a/src/lib/libcrypto/x509/by_file.c b/src/lib/libcrypto/x509/by_file.c
new file mode 100644
index 0000000000..09ebb9bf08
--- /dev/null
+++ b/src/lib/libcrypto/x509/by_file.c
@@ -0,0 +1,282 @@
1/* crypto/x509/by_file.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include <errno.h>
62#include <sys/types.h>
63#include <sys/stat.h>
64
65#include "cryptlib.h"
66#include "lhash.h"
67#include "buffer.h"
68#include "x509.h"
69#include "pem.h"
70
71#ifndef NO_STDIO
72
73#ifndef NOPROTO
74static int by_file_ctrl(X509_LOOKUP *ctx,int cmd,char *argc,
75 long argl,char **ret);
76#else
77static int by_file_ctrl();
78#endif
79
80X509_LOOKUP_METHOD x509_file_lookup=
81 {
82 "Load file into cache",
83 NULL, /* new */
84 NULL, /* free */
85 NULL, /* init */
86 NULL, /* shutdown */
87 by_file_ctrl, /* ctrl */
88 NULL, /* get_by_subject */
89 NULL, /* get_by_issuer_serial */
90 NULL, /* get_by_fingerprint */
91 NULL, /* get_by_alias */
92 };
93
94X509_LOOKUP_METHOD *X509_LOOKUP_file()
95 {
96 return(&x509_file_lookup);
97 }
98
99static int by_file_ctrl(ctx,cmd,argp,argl,ret)
100X509_LOOKUP *ctx;
101int cmd;
102char *argp;
103long argl;
104char **ret;
105 {
106 int ok=0,ok2=0;
107 char *file;
108
109 switch (cmd)
110 {
111 case X509_L_FILE_LOAD:
112 if (argl == X509_FILETYPE_DEFAULT)
113 {
114 ok=X509_load_cert_file(ctx,X509_get_default_cert_file(),
115 X509_FILETYPE_PEM);
116 ok2=X509_load_crl_file(ctx,X509_get_default_cert_file(),
117 X509_FILETYPE_PEM);
118 if (!ok || !ok2)
119 {
120 X509err(X509_F_BY_FILE_CTRL,X509_R_LOADING_DEFAULTS);
121 }
122 else
123 {
124 file=(char *)Getenv(X509_get_default_cert_file_env());
125 ok=X509_load_cert_file(ctx,file,
126 X509_FILETYPE_PEM);
127 ok2=X509_load_crl_file(ctx,file,
128 X509_FILETYPE_PEM);
129 }
130 }
131 else
132 {
133 ok=X509_load_cert_file(ctx,argp,(int)argl);
134 ok2=X509_load_crl_file(ctx,argp,(int)argl);
135 }
136 break;
137 }
138 return((ok && ok2)?ok:0);
139 }
140
141int X509_load_cert_file(ctx,file,type)
142X509_LOOKUP *ctx;
143char *file;
144int type;
145 {
146 int ret=0;
147 BIO *in=NULL;
148 int i,count=0;
149 X509 *x=NULL;
150
151 if (file == NULL) return(1);
152 in=BIO_new(BIO_s_file_internal());
153
154 if ((in == NULL) || (BIO_read_filename(in,file) <= 0))
155 {
156 X509err(X509_F_X509_LOAD_CERT_FILE,ERR_R_SYS_LIB);
157 goto err;
158 }
159
160 if (type == X509_FILETYPE_PEM)
161 {
162 for (;;)
163 {
164 x=PEM_read_bio_X509(in,NULL,NULL);
165 if (x == NULL)
166 {
167 if ((ERR_GET_REASON(ERR_peek_error()) ==
168 PEM_R_NO_START_LINE) && (count > 0))
169 {
170 ERR_clear_error();
171 break;
172 }
173 else
174 {
175 X509err(X509_F_X509_LOAD_CERT_FILE,
176 ERR_R_PEM_LIB);
177 goto err;
178 }
179 }
180 i=X509_STORE_add_cert(ctx->store_ctx,x);
181 if (!i) goto err;
182 count++;
183 X509_free(x);
184 x=NULL;
185 }
186 ret=count;
187 }
188 else if (type == X509_FILETYPE_ASN1)
189 {
190 x=d2i_X509_bio(in,NULL);
191 if (x == NULL)
192 {
193 X509err(X509_F_X509_LOAD_CERT_FILE,ERR_R_ASN1_LIB);
194 goto err;
195 }
196 i=X509_STORE_add_cert(ctx->store_ctx,x);
197 if (!i) goto err;
198 ret=i;
199 }
200 else
201 {
202 X509err(X509_F_X509_LOAD_CERT_FILE,X509_R_BAD_X509_FILETYPE);
203 goto err;
204 }
205err:
206 if (x != NULL) X509_free(x);
207 if (in != NULL) BIO_free(in);
208 return(ret);
209 }
210
211int X509_load_crl_file(ctx,file,type)
212X509_LOOKUP *ctx;
213char *file;
214int type;
215 {
216 int ret=0;
217 BIO *in=NULL;
218 int i,count=0;
219 X509_CRL *x=NULL;
220
221 if (file == NULL) return(1);
222 in=BIO_new(BIO_s_file_internal());
223
224 if ((in == NULL) || (BIO_read_filename(in,file) <= 0))
225 {
226 X509err(X509_F_X509_LOAD_CRL_FILE,ERR_R_SYS_LIB);
227 goto err;
228 }
229
230 if (type == X509_FILETYPE_PEM)
231 {
232 for (;;)
233 {
234 x=PEM_read_bio_X509_CRL(in,NULL,NULL);
235 if (x == NULL)
236 {
237 if ((ERR_GET_REASON(ERR_peek_error()) ==
238 PEM_R_NO_START_LINE) && (count > 0))
239 {
240 ERR_clear_error();
241 break;
242 }
243 else
244 {
245 X509err(X509_F_X509_LOAD_CRL_FILE,
246 ERR_R_PEM_LIB);
247 goto err;
248 }
249 }
250 i=X509_STORE_add_crl(ctx->store_ctx,x);
251 if (!i) goto err;
252 count++;
253 X509_CRL_free(x);
254 x=NULL;
255 }
256 ret=count;
257 }
258 else if (type == X509_FILETYPE_ASN1)
259 {
260 x=d2i_X509_CRL_bio(in,NULL);
261 if (x == NULL)
262 {
263 X509err(X509_F_X509_LOAD_CRL_FILE,ERR_R_ASN1_LIB);
264 goto err;
265 }
266 i=X509_STORE_add_crl(ctx->store_ctx,x);
267 if (!i) goto err;
268 ret=i;
269 }
270 else
271 {
272 X509err(X509_F_X509_LOAD_CRL_FILE,X509_R_BAD_X509_FILETYPE);
273 goto err;
274 }
275err:
276 if (x != NULL) X509_CRL_free(x);
277 if (in != NULL) BIO_free(in);
278 return(ret);
279 }
280
281#endif /* NO_STDIO */
282
diff --git a/src/lib/libcrypto/x509/x509.h b/src/lib/libcrypto/x509/x509.h
new file mode 100644
index 0000000000..95114f7c43
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509.h
@@ -0,0 +1,1152 @@
1/* crypto/x509/x509.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_X509_H
60#define HEADER_X509_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "stack.h"
67#include "asn1.h"
68
69#ifndef NO_RSA
70#include "rsa.h"
71#else
72#define RSA long
73#endif
74
75#ifndef NO_DSA
76#include "dsa.h"
77#else
78#define DSA long
79#endif
80
81#ifndef NO_DH
82#include "dh.h"
83#else
84#define DH long
85#endif
86
87#include "evp.h"
88
89#define X509_FILETYPE_PEM 1
90#define X509_FILETYPE_ASN1 2
91#define X509_FILETYPE_DEFAULT 3
92
93#define X509v3_KU_DIGITAL_SIGNATURE 0x0080
94#define X509v3_KU_NON_REPUDIATION 0x0040
95#define X509v3_KU_KEY_ENCIPHERMENT 0x0020
96#define X509v3_KU_DATA_ENCIPHERMENT 0x0010
97#define X509v3_KU_KEY_AGREEMENT 0x0008
98#define X509v3_KU_KEY_CERT_SIGN 0x0004
99#define X509v3_KU_CRL_SIGN 0x0002
100#define X509v3_KU_ENCIPHER_ONLY 0x0001
101#define X509v3_KU_DECIPHER_ONLY 0x8000
102#define X509v3_KU_UNDEF 0xffff
103
104typedef struct X509_objects_st
105 {
106 int nid;
107 int (*a2i)();
108 int (*i2a)();
109 } X509_OBJECTS;
110
111typedef struct X509_algor_st
112 {
113 ASN1_OBJECT *algorithm;
114 ASN1_TYPE *parameter;
115 } X509_ALGOR;
116
117typedef struct X509_val_st
118 {
119 ASN1_UTCTIME *notBefore;
120 ASN1_UTCTIME *notAfter;
121 } X509_VAL;
122
123typedef struct X509_pubkey_st
124 {
125 X509_ALGOR *algor;
126 ASN1_BIT_STRING *public_key;
127 struct evp_pkey_st /* EVP_PKEY*/ *pkey;
128 } X509_PUBKEY;
129
130typedef struct X509_sig_st
131 {
132 X509_ALGOR *algor;
133 ASN1_OCTET_STRING *digest;
134 } X509_SIG;
135
136typedef struct X509_name_entry_st
137 {
138 ASN1_OBJECT *object;
139 ASN1_STRING *value;
140 int set;
141 int size; /* temp variable */
142 } X509_NAME_ENTRY;
143
144/* we always keep X509_NAMEs in 2 forms. */
145typedef struct X509_name_st
146 {
147 STACK *entries; /* of X509_NAME_ENTRY */
148 int modified; /* true if 'bytes' needs to be built */
149#ifdef HEADER_BUFFER_H
150 BUF_MEM *bytes;
151#else
152 char *bytes;
153#endif
154 unsigned long hash; /* Keep the hash around for lookups */
155 } X509_NAME;
156
157#define X509_EX_V_NETSCAPE_HACK 0x8000
158#define X509_EX_V_INIT 0x0001
159typedef struct X509_extension_st
160 {
161 ASN1_OBJECT *object;
162 short critical;
163 short netscape_hack;
164 ASN1_OCTET_STRING *value;
165 long argl; /* used when decoding */
166 char *argp; /* used when decoding */
167 void (*ex_free)(); /* clear argp stuff */
168 } X509_EXTENSION;
169
170/* #if 1 */
171typedef struct x509_extension_method_st
172 {
173 int nid;
174 int data_type;
175 int pack_type;
176 void (*ex_clear)();
177 int (*ex_get_bool)();
178 int (*ex_set_bool)();
179 int (*ex_get_str)();
180 int (*ex_set_str)();
181 char *(*ex_get_struct)();
182 int (*ex_set_struct)();
183 int (*a2i)();
184 int (*i2a)();
185 } X509_EXTENSION_METHOD;
186/* #endif */
187
188typedef struct X509_req_info_st
189 {
190 ASN1_INTEGER *version;
191 X509_NAME *subject;
192 X509_PUBKEY *pubkey;
193 /* d=2 hl=2 l= 0 cons: cont: 00 */
194 STACK /* X509_ATTRIBUTE */ *attributes; /* [ 0 ] */
195 int req_kludge;
196 } X509_REQ_INFO;
197
198typedef struct X509_req_st
199 {
200 X509_REQ_INFO *req_info;
201 X509_ALGOR *sig_alg;
202 ASN1_BIT_STRING *signature;
203 int references;
204 } X509_REQ;
205
206typedef struct x509_cinf_st
207 {
208 ASN1_INTEGER *version; /* [ 0 ] default of v1 */
209 ASN1_INTEGER *serialNumber;
210 X509_ALGOR *signature;
211 X509_NAME *issuer;
212 X509_VAL *validity;
213 X509_NAME *subject;
214 X509_PUBKEY *key;
215 ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */
216 ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */
217 STACK /* X509_EXTENSION */ *extensions; /* [ 3 ] optional in v3 */
218 } X509_CINF;
219
220typedef struct x509_st
221 {
222 X509_CINF *cert_info;
223 X509_ALGOR *sig_alg;
224 ASN1_BIT_STRING *signature;
225 int valid;
226 int references;
227 char *name;
228 } X509;
229
230typedef struct X509_revoked_st
231 {
232 ASN1_INTEGER *serialNumber;
233 ASN1_UTCTIME *revocationDate;
234 STACK /* optional X509_EXTENSION */ *extensions;
235 int sequence; /* load sequence */
236 } X509_REVOKED;
237
238typedef struct X509_crl_info_st
239 {
240 ASN1_INTEGER *version;
241 X509_ALGOR *sig_alg;
242 X509_NAME *issuer;
243 ASN1_UTCTIME *lastUpdate;
244 ASN1_UTCTIME *nextUpdate;
245 STACK /* X509_REVOKED */ *revoked;
246 STACK /* [0] X509_EXTENSION */ *extensions;
247 } X509_CRL_INFO;
248
249typedef struct X509_crl_st
250 {
251 /* actual signature */
252 X509_CRL_INFO *crl;
253 X509_ALGOR *sig_alg;
254 ASN1_BIT_STRING *signature;
255 int references;
256 } X509_CRL;
257
258/* a sequence of these are used */
259typedef struct x509_attributes_st
260 {
261 ASN1_OBJECT *object;
262 int set; /* 1 for a set, 0 for a single item (which is wrong) */
263 union {
264 char *ptr;
265/* 1 */ STACK /* ASN1_TYPE */ *set;
266/* 0 */ ASN1_TYPE *single;
267 } value;
268 } X509_ATTRIBUTE;
269
270typedef struct private_key_st
271 {
272 int version;
273 /* The PKCS#8 data types */
274 X509_ALGOR *enc_algor;
275 ASN1_OCTET_STRING *enc_pkey; /* encrypted pub key */
276
277 /* When decrypted, the following will not be NULL */
278 EVP_PKEY *dec_pkey;
279
280 /* used to encrypt and decrypt */
281 int key_length;
282 char *key_data;
283 int key_free; /* true if we should auto free key_data */
284
285 /* expanded version of 'enc_algor' */
286 EVP_CIPHER_INFO cipher;
287
288 int references;
289 } X509_PKEY;
290
291#ifdef HEADER_ENVELOPE_H
292typedef struct X509_info_st
293 {
294 X509 *x509;
295 X509_CRL *crl;
296 X509_PKEY *x_pkey;
297
298 EVP_CIPHER_INFO enc_cipher;
299 int enc_len;
300 char *enc_data;
301
302 int references;
303 } X509_INFO;
304#endif
305
306/* The next 2 structures and their 8 routines were sent to me by
307 * Pat Richard <patr@x509.com> and are used to manipulate
308 * Netscapes spki strucutres - usefull if you are writing a CA web page
309 */
310typedef struct Netscape_spkac_st
311 {
312 X509_PUBKEY *pubkey;
313 ASN1_IA5STRING *challenge; /* challenge sent in atlas >= PR2 */
314 } NETSCAPE_SPKAC;
315
316typedef struct Netscape_spki_st
317 {
318 NETSCAPE_SPKAC *spkac; /* signed public key and challenge */
319 X509_ALGOR *sig_algor;
320 ASN1_BIT_STRING *signature;
321 } NETSCAPE_SPKI;
322
323#ifndef HEADER_BN_H
324#define BIGNUM char
325#endif
326
327typedef struct CBCParameter_st
328 {
329 unsigned char iv[8];
330 } CBC_PARAM;
331
332#include "x509_vfy.h"
333#include "pkcs7.h"
334
335#ifdef SSLEAY_MACROS
336#define X509_verify(a,r) ASN1_verify((int (*)())i2d_X509_CINF,a->sig_alg,\
337 a->signature,(char *)a->cert_info,r)
338#define X509_REQ_verify(a,r) ASN1_verify((int (*)())i2d_X509_REQ_INFO, \
339 a->sig_alg,a->signature,(char *)a->req_info,r)
340#define X509_CRL_verify(a,r) ASN1_verify((int (*)())i2d_X509_CRL_INFO, \
341 a->sig_alg, a->signature,(char *)a->crl,r)
342
343#define X509_sign(x,pkey,md) \
344 ASN1_sign((int (*)())i2d_X509_CINF, x->cert_info->signature, \
345 x->sig_alg, x->signature, (char *)x->cert_info,pkey,md)
346#define X509_REQ_sign(x,pkey,md) \
347 ASN1_sign((int (*)())i2d_X509_REQ_INFO,x->sig_alg, NULL, \
348 x->signature, (char *)x->req_info,pkey,md)
349#define X509_CRL_sign(x,pkey,md) \
350 ASN1_sign((int (*)())i2d_X509_CRL_INFO,x->crl->sig_alg,x->sig_alg, \
351 x->signature, (char *)x->crl,pkey,md)
352#define NETSCAPE_SPKI_sign(x,pkey,md) \
353 ASN1_sign((int (*)())i2d_NETSCAPE_SPKAC, x->sig_algor,NULL, \
354 x->signature, (char *)x->spkac,pkey,md)
355
356#define X509_dup(x509) (X509 *)ASN1_dup((int (*)())i2d_X509, \
357 (char *(*)())d2i_X509,(char *)x509)
358#define X509_EXTENSION_dup(ex) (X509_EXTENSION *)ASN1_dup( \
359 (int (*)())i2d_X509_EXTENSION, \
360 (char *(*)())d2i_X509_EXTENSION,(char *)ex)
361#define d2i_X509_fp(fp,x509) (X509 *)ASN1_d2i_fp((char *(*)())X509_new, \
362 (char *(*)())d2i_X509, (fp),(unsigned char **)(x509))
363#define i2d_X509_fp(fp,x509) ASN1_i2d_fp(i2d_X509,fp,(unsigned char *)x509)
364#define d2i_X509_bio(bp,x509) (X509 *)ASN1_d2i_bio((char *(*)())X509_new, \
365 (char *(*)())d2i_X509, (bp),(unsigned char **)(x509))
366#define i2d_X509_bio(bp,x509) ASN1_i2d_bio(i2d_X509,bp,(unsigned char *)x509)
367
368#define X509_CRL_dup(crl) (X509_CRL *)ASN1_dup((int (*)())i2d_X509_CRL, \
369 (char *(*)())d2i_X509_CRL,(char *)crl)
370#define d2i_X509_CRL_fp(fp,crl) (X509_CRL *)ASN1_d2i_fp((char *(*)()) \
371 X509_CRL_new,(char *(*)())d2i_X509_CRL, (fp),\
372 (unsigned char **)(crl))
373#define i2d_X509_CRL_fp(fp,crl) ASN1_i2d_fp(i2d_X509_CRL,fp,\
374 (unsigned char *)crl)
375#define d2i_X509_CRL_bio(bp,crl) (X509_CRL *)ASN1_d2i_bio((char *(*)()) \
376 X509_CRL_new,(char *(*)())d2i_X509_CRL, (bp),\
377 (unsigned char **)(crl))
378#define i2d_X509_CRL_bio(bp,crl) ASN1_i2d_bio(i2d_X509_CRL,bp,\
379 (unsigned char *)crl)
380
381#define PKCS7_dup(p7) (PKCS7 *)ASN1_dup((int (*)())i2d_PKCS7, \
382 (char *(*)())d2i_PKCS7,(char *)p7)
383#define d2i_PKCS7_fp(fp,p7) (PKCS7 *)ASN1_d2i_fp((char *(*)()) \
384 PKCS7_new,(char *(*)())d2i_PKCS7, (fp),\
385 (unsigned char **)(p7))
386#define i2d_PKCS7_fp(fp,p7) ASN1_i2d_fp(i2d_PKCS7,fp,\
387 (unsigned char *)p7)
388#define d2i_PKCS7_bio(bp,p7) (PKCS7 *)ASN1_d2i_bio((char *(*)()) \
389 PKCS7_new,(char *(*)())d2i_PKCS7, (bp),\
390 (unsigned char **)(p7))
391#define i2d_PKCS7_bio(bp,p7) ASN1_i2d_bio(i2d_PKCS7,bp,\
392 (unsigned char *)p7)
393
394#define X509_REQ_dup(req) (X509_REQ *)ASN1_dup((int (*)())i2d_X509_REQ, \
395 (char *(*)())d2i_X509_REQ,(char *)req)
396#define d2i_X509_REQ_fp(fp,req) (X509_REQ *)ASN1_d2i_fp((char *(*)())\
397 X509_REQ_new, (char *(*)())d2i_X509_REQ, (fp),\
398 (unsigned char **)(req))
399#define i2d_X509_REQ_fp(fp,req) ASN1_i2d_fp(i2d_X509_REQ,fp,\
400 (unsigned char *)req)
401#define d2i_X509_REQ_bio(bp,req) (X509_REQ *)ASN1_d2i_bio((char *(*)())\
402 X509_REQ_new, (char *(*)())d2i_X509_REQ, (bp),\
403 (unsigned char **)(req))
404#define i2d_X509_REQ_bio(bp,req) ASN1_i2d_bio(i2d_X509_REQ,bp,\
405 (unsigned char *)req)
406
407#define RSAPublicKey_dup(rsa) (RSA *)ASN1_dup((int (*)())i2d_RSAPublicKey, \
408 (char *(*)())d2i_RSAPublicKey,(char *)rsa)
409#define RSAPrivateKey_dup(rsa) (RSA *)ASN1_dup((int (*)())i2d_RSAPrivateKey, \
410 (char *(*)())d2i_RSAPrivateKey,(char *)rsa)
411
412#define d2i_RSAPrivateKey_fp(fp,rsa) (RSA *)ASN1_d2i_fp((char *(*)())\
413 RSA_new,(char *(*)())d2i_RSAPrivateKey, (fp), \
414 (unsigned char **)(rsa))
415#define i2d_RSAPrivateKey_fp(fp,rsa) ASN1_i2d_fp(i2d_RSAPrivateKey,fp, \
416 (unsigned char *)rsa)
417#define d2i_RSAPrivateKey_bio(bp,rsa) (RSA *)ASN1_d2i_bio((char *(*)())\
418 RSA_new,(char *(*)())d2i_RSAPrivateKey, (bp), \
419 (unsigned char **)(rsa))
420#define i2d_RSAPrivateKey_bio(bp,rsa) ASN1_i2d_bio(i2d_RSAPrivateKey,bp, \
421 (unsigned char *)rsa)
422
423#define d2i_RSAPublicKey_fp(fp,rsa) (RSA *)ASN1_d2i_fp((char *(*)())\
424 RSA_new,(char *(*)())d2i_RSAPublicKey, (fp), \
425 (unsigned char **)(rsa))
426#define i2d_RSAPublicKey_fp(fp,rsa) ASN1_i2d_fp(i2d_RSAPublicKey,fp, \
427 (unsigned char *)rsa)
428#define d2i_RSAPublicKey_bio(bp,rsa) (RSA *)ASN1_d2i_bio((char *(*)())\
429 RSA_new,(char *(*)())d2i_RSAPublicKey, (bp), \
430 (unsigned char **)(rsa))
431#define i2d_RSAPublicKey_bio(bp,rsa) ASN1_i2d_bio(i2d_RSAPublicKey,bp, \
432 (unsigned char *)rsa)
433
434#define d2i_DSAPrivateKey_fp(fp,dsa) (DSA *)ASN1_d2i_fp((char *(*)())\
435 DSA_new,(char *(*)())d2i_DSAPrivateKey, (fp), \
436 (unsigned char **)(dsa))
437#define i2d_DSAPrivateKey_fp(fp,dsa) ASN1_i2d_fp(i2d_DSAPrivateKey,fp, \
438 (unsigned char *)dsa)
439#define d2i_DSAPrivateKey_bio(bp,dsa) (DSA *)ASN1_d2i_bio((char *(*)())\
440 DSA_new,(char *(*)())d2i_DSAPrivateKey, (bp), \
441 (unsigned char **)(dsa))
442#define i2d_DSAPrivateKey_bio(bp,dsa) ASN1_i2d_bio(i2d_DSAPrivateKey,bp, \
443 (unsigned char *)dsa)
444
445#define X509_NAME_dup(xn) (X509_NAME *)ASN1_dup((int (*)())i2d_X509_NAME, \
446 (char *(*)())d2i_X509_NAME,(char *)xn)
447#define X509_NAME_ENTRY_dup(ne) (X509_NAME_ENTRY *)ASN1_dup( \
448 (int (*)())i2d_X509_NAME_ENTRY, \
449 (char *(*)())d2i_X509_NAME_ENTRY,\
450 (char *)ne)
451
452#define X509_digest(data,type,md,len) \
453 ASN1_digest((int (*)())i2d_X509,type,(char *)data,md,len)
454#define X509_NAME_digest(data,type,md,len) \
455 ASN1_digest((int (*)())i2d_X509_NAME,type,(char *)data,md,len)
456#define PKCS7_ISSUER_AND_SERIAL_digest(data,type,md,len) \
457 ASN1_digest((int (*)())i2d_PKCS7_ISSUER_AND_SERIAL,type,\
458 (char *)data,md,len)
459#endif
460
461#define X509_EXT_PACK_UNKNOWN 1
462#define X509_EXT_PACK_STRING 2
463
464#define X509_get_version(x) ASN1_INTEGER_get((x)->cert_info->version)
465/* #define X509_get_serialNumber(x) ((x)->cert_info->serialNumber) */
466#define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore)
467#define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
468#define X509_extract_key(x) X509_get_pubkey(x) /*****/
469#define X509_REQ_get_version(x) ASN1_INTEGER_get((x)->req_info->version)
470#define X509_REQ_get_subject_name(x) ((x)->req_info->subject)
471#define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a)
472#define X509_name_cmp(a,b) X509_NAME_cmp((a),(b))
473#define X509_get_signature_type(x) EVP_PKEY_type(OBJ_obj2nid((x)->sig_alg->algorithm))
474
475/* This one is only used so that a binary form can output, as in
476 * i2d_X509_NAME(X509_get_X509_PUBKEY(x),&buf) */
477#define X509_get_X509_PUBKEY(x) ((x)->cert_info->key)
478
479#ifndef NOPROTO
480
481#ifndef SSLEAY_MACROS
482#ifdef HEADER_ENVELOPE_H
483int X509_verify(X509 *a, EVP_PKEY *r);
484char *X509_verify_cert_error_string(long n);
485
486int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r);
487int X509_CRL_verify(X509_CRL *a, EVP_PKEY *r);
488int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r);
489
490int X509_sign(X509 *x, EVP_PKEY *pkey, EVP_MD *md);
491int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, EVP_MD *md);
492int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, EVP_MD *md);
493int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, EVP_MD *md);
494
495int X509_digest(X509 *data,EVP_MD *type,unsigned char *md,unsigned int *len);
496int X509_NAME_digest(X509_NAME *data,EVP_MD *type,
497 unsigned char *md,unsigned int *len);
498#endif
499
500#ifndef NO_FP_API
501X509 *d2i_X509_fp(FILE *fp, X509 *x509);
502int i2d_X509_fp(FILE *fp,X509 *x509);
503X509_CRL *d2i_X509_CRL_fp(FILE *fp,X509_CRL *crl);
504int i2d_X509_CRL_fp(FILE *fp,X509_CRL *crl);
505X509_REQ *d2i_X509_REQ_fp(FILE *fp,X509_REQ *req);
506int i2d_X509_REQ_fp(FILE *fp,X509_REQ *req);
507RSA *d2i_RSAPrivateKey_fp(FILE *fp,RSA *rsa);
508int i2d_RSAPrivateKey_fp(FILE *fp,RSA *rsa);
509DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA *dsa);
510int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa);
511RSA *d2i_RSAPublicKey_fp(FILE *fp,RSA *rsa);
512int i2d_RSAPublicKey_fp(FILE *fp,RSA *rsa);
513#endif
514
515#ifdef HEADER_BIO_H
516X509 *d2i_X509_bio(BIO *bp,X509 *x509);
517int i2d_X509_bio(BIO *bp,X509 *x509);
518X509_CRL *d2i_X509_CRL_bio(BIO *bp,X509_CRL *crl);
519int i2d_X509_CRL_bio(BIO *bp,X509_CRL *crl);
520X509_REQ *d2i_X509_REQ_bio(BIO *bp,X509_REQ *req);
521int i2d_X509_REQ_bio(BIO *bp,X509_REQ *req);
522RSA *d2i_RSAPrivateKey_bio(BIO *bp,RSA *rsa);
523int i2d_RSAPrivateKey_bio(BIO *bp,RSA *rsa);
524DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA *dsa);
525int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa);
526RSA *d2i_RSAPublicKey_bio(BIO *bp,RSA *rsa);
527int i2d_RSAPublicKey_bio(BIO *bp,RSA *rsa);
528#endif
529
530X509 *X509_dup(X509 *x509);
531X509_EXTENSION *X509_EXTENSION_dup(X509_EXTENSION *ex);
532X509_CRL *X509_CRL_dup(X509_CRL *crl);
533X509_REQ *X509_REQ_dup(X509_REQ *req);
534X509_NAME *X509_NAME_dup(X509_NAME *xn);
535X509_NAME_ENTRY *X509_NAME_ENTRY_dup(X509_NAME_ENTRY *ne);
536RSA *RSAPublicKey_dup(RSA *rsa);
537RSA *RSAPrivateKey_dup(RSA *rsa);
538
539#endif /* !SSLEAY_MACROS */
540
541int X509_cmp_current_time(ASN1_UTCTIME *s);
542ASN1_UTCTIME * X509_gmtime_adj(ASN1_UTCTIME *s, long adj);
543
544char * X509_get_default_cert_area(void );
545char * X509_get_default_cert_dir(void );
546char * X509_get_default_cert_file(void );
547char * X509_get_default_cert_dir_env(void );
548char * X509_get_default_cert_file_env(void );
549char * X509_get_default_private_dir(void );
550
551X509_REQ * X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, EVP_MD *md);
552X509 * X509_REQ_to_X509(X509_REQ *r, int days,EVP_PKEY *pkey);
553void ERR_load_X509_strings(void );
554
555X509_ALGOR * X509_ALGOR_new(void );
556void X509_ALGOR_free(X509_ALGOR *a);
557int i2d_X509_ALGOR(X509_ALGOR *a,unsigned char **pp);
558X509_ALGOR * d2i_X509_ALGOR(X509_ALGOR **a,unsigned char **pp,
559 long length);
560
561X509_VAL * X509_VAL_new(void );
562void X509_VAL_free(X509_VAL *a);
563int i2d_X509_VAL(X509_VAL *a,unsigned char **pp);
564X509_VAL * d2i_X509_VAL(X509_VAL **a,unsigned char **pp,
565 long length);
566
567X509_PUBKEY * X509_PUBKEY_new(void );
568void X509_PUBKEY_free(X509_PUBKEY *a);
569int i2d_X509_PUBKEY(X509_PUBKEY *a,unsigned char **pp);
570X509_PUBKEY * d2i_X509_PUBKEY(X509_PUBKEY **a,unsigned char **pp,
571 long length);
572int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey);
573EVP_PKEY * X509_PUBKEY_get(X509_PUBKEY *key);
574int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK *chain);
575
576
577X509_SIG * X509_SIG_new(void );
578void X509_SIG_free(X509_SIG *a);
579int i2d_X509_SIG(X509_SIG *a,unsigned char **pp);
580X509_SIG * d2i_X509_SIG(X509_SIG **a,unsigned char **pp,long length);
581
582X509_REQ_INFO *X509_REQ_INFO_new(void);
583void X509_REQ_INFO_free(X509_REQ_INFO *a);
584int i2d_X509_REQ_INFO(X509_REQ_INFO *a,unsigned char **pp);
585X509_REQ_INFO *d2i_X509_REQ_INFO(X509_REQ_INFO **a,unsigned char **pp,
586 long length);
587
588X509_REQ * X509_REQ_new(void);
589void X509_REQ_free(X509_REQ *a);
590int i2d_X509_REQ(X509_REQ *a,unsigned char **pp);
591X509_REQ * d2i_X509_REQ(X509_REQ **a,unsigned char **pp,long length);
592
593X509_ATTRIBUTE *X509_ATTRIBUTE_new(void );
594void X509_ATTRIBUTE_free(X509_ATTRIBUTE *a);
595int i2d_X509_ATTRIBUTE(X509_ATTRIBUTE *a,unsigned char **pp);
596X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **a,unsigned char **pp,
597 long length);
598
599X509_EXTENSION *X509_EXTENSION_new(void );
600void X509_EXTENSION_free(X509_EXTENSION *a);
601int i2d_X509_EXTENSION(X509_EXTENSION *a,unsigned char **pp);
602X509_EXTENSION *d2i_X509_EXTENSION(X509_EXTENSION **a,unsigned char **pp,
603 long length);
604
605X509_NAME_ENTRY *X509_NAME_ENTRY_new(void);
606void X509_NAME_ENTRY_free(X509_NAME_ENTRY *a);
607int i2d_X509_NAME_ENTRY(X509_NAME_ENTRY *a,unsigned char **pp);
608X509_NAME_ENTRY *d2i_X509_NAME_ENTRY(X509_NAME_ENTRY **a,unsigned char **pp,
609 long length);
610
611X509_NAME * X509_NAME_new(void);
612void X509_NAME_free(X509_NAME *a);
613int i2d_X509_NAME(X509_NAME *a,unsigned char **pp);
614X509_NAME * d2i_X509_NAME(X509_NAME **a,unsigned char **pp,long length);
615int X509_NAME_set(X509_NAME **xn, X509_NAME *name);
616
617
618X509_CINF * X509_CINF_new(void);
619void X509_CINF_free(X509_CINF *a);
620int i2d_X509_CINF(X509_CINF *a,unsigned char **pp);
621X509_CINF * d2i_X509_CINF(X509_CINF **a,unsigned char **pp,long length);
622
623X509 * X509_new(void);
624void X509_free(X509 *a);
625int i2d_X509(X509 *a,unsigned char **pp);
626X509 * d2i_X509(X509 **a,unsigned char **pp,long length);
627
628X509_REVOKED * X509_REVOKED_new(void);
629void X509_REVOKED_free(X509_REVOKED *a);
630int i2d_X509_REVOKED(X509_REVOKED *a,unsigned char **pp);
631X509_REVOKED * d2i_X509_REVOKED(X509_REVOKED **a,unsigned char **pp,long length);
632
633X509_CRL_INFO *X509_CRL_INFO_new(void);
634void X509_CRL_INFO_free(X509_CRL_INFO *a);
635int i2d_X509_CRL_INFO(X509_CRL_INFO *a,unsigned char **pp);
636X509_CRL_INFO *d2i_X509_CRL_INFO(X509_CRL_INFO **a,unsigned char **pp,
637 long length);
638
639X509_CRL * X509_CRL_new(void);
640void X509_CRL_free(X509_CRL *a);
641int i2d_X509_CRL(X509_CRL *a,unsigned char **pp);
642X509_CRL * d2i_X509_CRL(X509_CRL **a,unsigned char **pp,long length);
643
644X509_PKEY * X509_PKEY_new(void );
645void X509_PKEY_free(X509_PKEY *a);
646int i2d_X509_PKEY(X509_PKEY *a,unsigned char **pp);
647X509_PKEY * d2i_X509_PKEY(X509_PKEY **a,unsigned char **pp,long length);
648
649NETSCAPE_SPKI * NETSCAPE_SPKI_new(void );
650void NETSCAPE_SPKI_free(NETSCAPE_SPKI *a);
651int i2d_NETSCAPE_SPKI(NETSCAPE_SPKI *a,unsigned char **pp);
652NETSCAPE_SPKI * d2i_NETSCAPE_SPKI(NETSCAPE_SPKI **a,unsigned char **pp,
653 long length);
654
655NETSCAPE_SPKAC *NETSCAPE_SPKAC_new(void );
656void NETSCAPE_SPKAC_free(NETSCAPE_SPKAC *a);
657int i2d_NETSCAPE_SPKAC(NETSCAPE_SPKAC *a,unsigned char **pp);
658NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(NETSCAPE_SPKAC **a,unsigned char **pp,
659 long length);
660
661#ifdef HEADER_ENVELOPE_H
662X509_INFO * X509_INFO_new(void);
663void X509_INFO_free(X509_INFO *a);
664char * X509_NAME_oneline(X509_NAME *a,char *buf,int size);
665
666int ASN1_verify(int (*i2d)(), X509_ALGOR *algor1,
667 ASN1_BIT_STRING *signature,char *data,EVP_PKEY *pkey);
668
669int ASN1_digest(int (*i2d)(),EVP_MD *type,char *data,
670 unsigned char *md,unsigned int *len);
671
672int ASN1_sign(int (*i2d)(), X509_ALGOR *algor1, X509_ALGOR *algor2,
673 ASN1_BIT_STRING *signature,
674 char *data,EVP_PKEY *pkey, EVP_MD *type);
675#endif
676
677int X509_set_version(X509 *x,long version);
678int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial);
679ASN1_INTEGER * X509_get_serialNumber(X509 *x);
680int X509_set_issuer_name(X509 *x, X509_NAME *name);
681X509_NAME * X509_get_issuer_name(X509 *a);
682int X509_set_subject_name(X509 *x, X509_NAME *name);
683X509_NAME * X509_get_subject_name(X509 *a);
684int X509_set_notBefore(X509 *x, ASN1_UTCTIME *tm);
685int X509_set_notAfter(X509 *x, ASN1_UTCTIME *tm);
686int X509_set_pubkey(X509 *x, EVP_PKEY *pkey);
687EVP_PKEY * X509_get_pubkey(X509 *x);
688int X509_certificate_type(X509 *x,EVP_PKEY *pubkey /* optional */);
689
690int X509_REQ_set_version(X509_REQ *x,long version);
691int X509_REQ_set_subject_name(X509_REQ *req,X509_NAME *name);
692int X509_REQ_set_pubkey(X509_REQ *x, EVP_PKEY *pkey);
693EVP_PKEY * X509_REQ_get_pubkey(X509_REQ *req);
694
695int X509_check_private_key(X509 *x509,EVP_PKEY *pkey);
696
697int X509_issuer_and_serial_cmp(X509 *a, X509 *b);
698unsigned long X509_issuer_and_serial_hash(X509 *a);
699
700int X509_issuer_name_cmp(X509 *a, X509 *b);
701unsigned long X509_issuer_name_hash(X509 *a);
702
703int X509_subject_name_cmp(X509 *a,X509 *b);
704unsigned long X509_subject_name_hash(X509 *x);
705
706int X509_NAME_cmp (X509_NAME *a, X509_NAME *b);
707unsigned long X509_NAME_hash(X509_NAME *x);
708
709int X509_CRL_cmp(X509_CRL *a,X509_CRL *b);
710#ifndef NO_FP_API
711int X509_print_fp(FILE *bp,X509 *x);
712int X509_REQ_print_fp(FILE *bp,X509_REQ *req);
713#endif
714
715#ifdef HEADER_BIO_H
716int X509_NAME_print(BIO *bp, X509_NAME *name, int obase);
717int X509_print(BIO *bp,X509 *x);
718int X509_REQ_print(BIO *bp,X509_REQ *req);
719#endif
720
721int X509_NAME_entry_count(X509_NAME *name);
722int X509_NAME_get_text_by_NID(X509_NAME *name, int nid,
723 char *buf,int len);
724int X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj,
725 char *buf,int len);
726
727/* NOTE: you should be passsing -1, not 0 as lastpos. The functions that use
728 * lastpos, seach after that position on. */
729int X509_NAME_get_index_by_NID(X509_NAME *name,int nid,int lastpos);
730int X509_NAME_get_index_by_OBJ(X509_NAME *name,ASN1_OBJECT *obj,
731 int lastpos);
732X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc);
733X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc);
734int X509_NAME_add_entry(X509_NAME *name,X509_NAME_ENTRY *ne,
735 int loc, int set);
736X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
737 int type,unsigned char *bytes, int len);
738X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
739 ASN1_OBJECT *obj, int type,unsigned char *bytes,
740 int len);
741int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne,
742 ASN1_OBJECT *obj);
743int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
744 unsigned char *bytes, int len);
745ASN1_OBJECT * X509_NAME_ENTRY_get_object(X509_NAME_ENTRY *ne);
746ASN1_STRING * X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne);
747
748int X509v3_get_ext_count(STACK *x);
749int X509v3_get_ext_by_NID(STACK *x, int nid, int lastpos);
750int X509v3_get_ext_by_OBJ(STACK *x,ASN1_OBJECT *obj,int lastpos);
751int X509v3_get_ext_by_critical(STACK *x, int crit, int lastpos);
752X509_EXTENSION *X509v3_get_ext(STACK *x, int loc);
753X509_EXTENSION *X509v3_delete_ext(STACK *x, int loc);
754STACK * X509v3_add_ext(STACK **x, X509_EXTENSION *ex, int loc);
755
756int X509v3_data_type_by_OBJ(ASN1_OBJECT *obj);
757int X509v3_data_type_by_NID(int nid);
758int X509v3_pack_type_by_OBJ(ASN1_OBJECT *obj);
759int X509v3_pack_type_by_NID(int nid);
760
761int X509_get_ext_count(X509 *x);
762int X509_get_ext_by_NID(X509 *x, int nid, int lastpos);
763int X509_get_ext_by_OBJ(X509 *x,ASN1_OBJECT *obj,int lastpos);
764int X509_get_ext_by_critical(X509 *x, int crit, int lastpos);
765X509_EXTENSION *X509_get_ext(X509 *x, int loc);
766X509_EXTENSION *X509_delete_ext(X509 *x, int loc);
767int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc);
768
769int X509_CRL_get_ext_count(X509_CRL *x);
770int X509_CRL_get_ext_by_NID(X509_CRL *x, int nid, int lastpos);
771int X509_CRL_get_ext_by_OBJ(X509_CRL *x,ASN1_OBJECT *obj,int lastpos);
772int X509_CRL_get_ext_by_critical(X509_CRL *x, int crit, int lastpos);
773X509_EXTENSION *X509_CRL_get_ext(X509_CRL *x, int loc);
774X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc);
775int X509_CRL_add_ext(X509_CRL *x, X509_EXTENSION *ex, int loc);
776
777int X509_REVOKED_get_ext_count(X509_REVOKED *x);
778int X509_REVOKED_get_ext_by_NID(X509_REVOKED *x, int nid, int lastpos);
779int X509_REVOKED_get_ext_by_OBJ(X509_REVOKED *x,ASN1_OBJECT *obj,int lastpos);
780int X509_REVOKED_get_ext_by_critical(X509_REVOKED *x, int crit, int lastpos);
781X509_EXTENSION *X509_REVOKED_get_ext(X509_REVOKED *x, int loc);
782X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x, int loc);
783int X509_REVOKED_add_ext(X509_REVOKED *x, X509_EXTENSION *ex, int loc);
784
785X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex,
786 int nid, int crit, ASN1_OCTET_STRING *data);
787X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
788 ASN1_OBJECT *obj,int crit,ASN1_OCTET_STRING *data);
789int X509_EXTENSION_set_object(X509_EXTENSION *ex,ASN1_OBJECT *obj);
790int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit);
791int X509_EXTENSION_set_data(X509_EXTENSION *ex,
792 ASN1_OCTET_STRING *data);
793ASN1_OBJECT * X509_EXTENSION_get_object(X509_EXTENSION *ex);
794ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ne);
795int X509_EXTENSION_get_critical(X509_EXTENSION *ex);
796ASN1_OCTET_STRING *X509v3_pack_string(ASN1_OCTET_STRING **ex,int type,
797 unsigned char *bytes, int len);
798ASN1_STRING * X509v3_unpack_string(ASN1_STRING **ex,int type,
799 ASN1_OCTET_STRING *os);
800
801int X509_verify_cert(X509_STORE_CTX *ctx);
802
803/* lookup a cert from a X509 STACK */
804X509 *X509_find_by_issuer_and_serial(STACK *sk,X509_NAME *name,
805 ASN1_INTEGER *serial);
806X509 *X509_find_by_subject(STACK *sk,X509_NAME *name);
807
808#else
809
810#ifndef SSLEAY_MACROS
811#ifdef HEADER_ENVELOPE_H
812int X509_verify();
813int X509_REQ_verify();
814int X509_CRL_verify();
815int NETSCAPE_SPKI_verify();
816
817int X509_sign();
818int X509_REQ_sign();
819int X509_CRL_sign();
820int NETSCAPE_SPKI_sign();
821
822int X509_digest();
823int X509_NAME_digest();
824#endif
825
826#ifndef NO_FP_API
827X509 *d2i_X509_fp();
828int i2d_X509_fp();
829X509_CRL *d2i_X509_CRL_fp();
830int i2d_X509_CRL_fp();
831X509_REQ *d2i_X509_REQ_fp();
832int i2d_X509_REQ_fp();
833RSA *d2i_RSAPrivateKey_fp();
834int i2d_RSAPrivateKey_fp();
835DSA *d2i_DSAPrivateKey_fp();
836int i2d_DSAPrivateKey_fp();
837RSA *d2i_RSAPublicKey_fp();
838int i2d_RSAPublicKey_fp();
839#endif
840
841X509 *d2i_X509_bio();
842int i2d_X509_bio();
843X509_CRL *d2i_X509_CRL_bio();
844int i2d_X509_CRL_bio();
845X509_REQ *d2i_X509_REQ_bio();
846int i2d_X509_REQ_bio();
847RSA *d2i_RSAPrivateKey_bio();
848int i2d_RSAPrivateKey_bio();
849DSA *d2i_DSAPrivateKey_bio();
850int i2d_DSAPrivateKey_bio();
851RSA *d2i_RSAPublicKey_bio();
852int i2d_RSAPublicKey_bio();
853
854X509 *X509_dup();
855X509_EXTENSION *X509_EXTENSION_dup();
856X509_CRL *X509_CRL_dup();
857X509_REQ *X509_REQ_dup();
858X509_NAME *X509_NAME_dup();
859X509_NAME_ENTRY *X509_NAME_ENTRY_dup();
860RSA *RSAPublicKey_dup();
861RSA *RSAPrivateKey_dup();
862
863#endif /* !SSLEAY_MACROS */
864
865int X509_cmp_current_time();
866ASN1_UTCTIME * X509_gmtime_adj();
867
868char * X509_get_default_cert_area();
869char * X509_get_default_cert_dir();
870char * X509_get_default_cert_file();
871char * X509_get_default_cert_dir_env();
872char * X509_get_default_cert_file_env();
873char * X509_get_default_private_dir();
874
875X509_REQ * X509_to_X509_REQ();
876X509 * X509_REQ_to_X509();
877void ERR_load_X509_strings();
878
879X509_ALGOR * X509_ALGOR_new();
880void X509_ALGOR_free();
881int i2d_X509_ALGOR();
882X509_ALGOR * d2i_X509_ALGOR();
883
884X509_VAL * X509_VAL_new();
885void X509_VAL_free();
886int i2d_X509_VAL();
887X509_VAL * d2i_X509_VAL();
888
889X509_PUBKEY * X509_PUBKEY_new();
890void X509_PUBKEY_free();
891int i2d_X509_PUBKEY();
892X509_PUBKEY * d2i_X509_PUBKEY();
893int X509_PUBKEY_set();
894EVP_PKEY * X509_PUBKEY_get();
895int X509_get_pubkey_parameters();
896
897X509_SIG * X509_SIG_new();
898void X509_SIG_free();
899int i2d_X509_SIG();
900X509_SIG * d2i_X509_SIG();
901
902X509_REQ_INFO *X509_REQ_INFO_new();
903void X509_REQ_INFO_free();
904int i2d_X509_REQ_INFO();
905X509_REQ_INFO *d2i_X509_REQ_INFO();
906
907X509_REQ * X509_REQ_new();
908void X509_REQ_free();
909int i2d_X509_REQ();
910X509_REQ * d2i_X509_REQ();
911
912X509_ATTRIBUTE *X509_ATTRIBUTE_new();
913void X509_ATTRIBUTE_free();
914int i2d_X509_ATTRIBUTE();
915X509_ATTRIBUTE *d2i_X509_ATTRIBUTE();
916
917X509_EXTENSION *X509_EXTENSION_new();
918void X509_EXTENSION_free();
919int i2d_X509_EXTENSION();
920X509_EXTENSION *d2i_X509_EXTENSION();
921
922X509_NAME_ENTRY *X509_NAME_ENTRY_new();
923void X509_NAME_ENTRY_free();
924int i2d_X509_NAME_ENTRY();
925X509_NAME_ENTRY *d2i_X509_NAME_ENTRY();
926
927X509_NAME * X509_NAME_new();
928void X509_NAME_free();
929int i2d_X509_NAME();
930X509_NAME * d2i_X509_NAME();
931int X509_NAME_set();
932
933
934X509_CINF * X509_CINF_new();
935void X509_CINF_free();
936int i2d_X509_CINF();
937X509_CINF * d2i_X509_CINF();
938
939X509 * X509_new();
940void X509_free();
941int i2d_X509();
942X509 * d2i_X509();
943
944X509_REVOKED * X509_REVOKED_new();
945void X509_REVOKED_free();
946int i2d_X509_REVOKED();
947X509_REVOKED * d2i_X509_REVOKED();
948
949X509_CRL_INFO *X509_CRL_INFO_new();
950void X509_CRL_INFO_free();
951int i2d_X509_CRL_INFO();
952X509_CRL_INFO *d2i_X509_CRL_INFO();
953
954X509_CRL * X509_CRL_new();
955void X509_CRL_free();
956int i2d_X509_CRL();
957X509_CRL * d2i_X509_CRL();
958
959X509_PKEY * X509_PKEY_new();
960void X509_PKEY_free();
961int i2d_X509_PKEY();
962X509_PKEY * d2i_X509_PKEY();
963
964NETSCAPE_SPKI * NETSCAPE_SPKI_new();
965void NETSCAPE_SPKI_free();
966int i2d_NETSCAPE_SPKI();
967NETSCAPE_SPKI * d2i_NETSCAPE_SPKI();
968
969NETSCAPE_SPKAC *NETSCAPE_SPKAC_new();
970void NETSCAPE_SPKAC_free();
971int i2d_NETSCAPE_SPKAC();
972NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC();
973
974#ifdef HEADER_ENVELOPE_H
975X509_INFO * X509_INFO_new();
976void X509_INFO_free();
977#endif
978
979char * X509_NAME_oneline();
980
981int ASN1_verify();
982int ASN1_digest();
983int ASN1_sign();
984
985int X509_set_version();
986int X509_set_serialNumber();
987ASN1_INTEGER * X509_get_serialNumber();
988int X509_set_issuer_name();
989X509_NAME * X509_get_issuer_name();
990int X509_set_subject_name();
991X509_NAME * X509_get_subject_name();
992int X509_set_notBefore();
993int X509_set_notAfter();
994int X509_set_pubkey();
995EVP_PKEY * X509_get_pubkey();
996int X509_certificate_type();
997
998int X509_REQ_set_version();
999int X509_REQ_set_subject_name();
1000int X509_REQ_set_pubkey();
1001EVP_PKEY * X509_REQ_get_pubkey();
1002
1003int X509_check_private_key();
1004
1005int X509_issuer_and_serial_cmp();
1006unsigned long X509_issuer_and_serial_hash();
1007
1008int X509_issuer_name_cmp();
1009unsigned long X509_issuer_name_hash();
1010
1011int X509_subject_name_cmp();
1012unsigned long X509_subject_name_hash();
1013
1014int X509_NAME_cmp ();
1015unsigned long X509_NAME_hash();
1016
1017int X509_CRL_cmp();
1018#ifndef NO_FP_API
1019int X509_print_fp();
1020int X509_REQ_print_fp();
1021#endif
1022
1023int X509_NAME_print();
1024int X509_print();
1025int X509_REQ_print();
1026
1027int X509_NAME_entry_count();
1028int X509_NAME_get_text_by_NID();
1029int X509_NAME_get_text_by_OBJ();
1030
1031int X509_NAME_get_index_by_NID();
1032int X509_NAME_get_index_by_OBJ();
1033X509_NAME_ENTRY *X509_NAME_get_entry();
1034X509_NAME_ENTRY *X509_NAME_delete_entry();
1035int X509_NAME_add_entry();
1036X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID();
1037X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ();
1038int X509_NAME_ENTRY_set_object();
1039int X509_NAME_ENTRY_set_data();
1040ASN1_OBJECT * X509_NAME_ENTRY_get_object();
1041ASN1_STRING * X509_NAME_ENTRY_get_data();
1042
1043int X509v3_get_ext_count();
1044int X509v3_get_ext_by_NID();
1045int X509v3_get_ext_by_OBJ();
1046int X509v3_get_ext_by_critical();
1047X509_EXTENSION *X509v3_get_ext();
1048X509_EXTENSION *X509v3_delete_ext();
1049STACK * X509v3_add_ext();
1050
1051int X509v3_data_type_by_OBJ();
1052int X509v3_data_type_by_NID();
1053int X509v3_pack_type_by_OBJ();
1054int X509v3_pack_type_by_NID();
1055
1056int X509_get_ext_count();
1057int X509_get_ext_by_NID();
1058int X509_get_ext_by_OBJ();
1059int X509_get_ext_by_critical();
1060X509_EXTENSION *X509_get_ext();
1061X509_EXTENSION *X509_delete_ext();
1062int X509_add_ext();
1063
1064int X509_CRL_get_ext_count();
1065int X509_CRL_get_ext_by_NID();
1066int X509_CRL_get_ext_by_OBJ();
1067int X509_CRL_get_ext_by_critical();
1068X509_EXTENSION *X509_CRL_get_ext();
1069X509_EXTENSION *X509_CRL_delete_ext();
1070int X509_CRL_add_ext();
1071
1072int X509_REVOKED_get_ext_count();
1073int X509_REVOKED_get_ext_by_NID();
1074int X509_REVOKED_get_ext_by_OBJ();
1075int X509_REVOKED_get_ext_by_critical();
1076X509_EXTENSION *X509_REVOKED_get_ext();
1077X509_EXTENSION *X509_REVOKED_delete_ext();
1078int X509_REVOKED_add_ext();
1079
1080X509_EXTENSION *X509_EXTENSION_create_by_NID();
1081X509_EXTENSION *X509_EXTENSION_create_by_OBJ();
1082int X509_EXTENSION_set_object();
1083int X509_EXTENSION_set_critical();
1084int X509_EXTENSION_set_data();
1085ASN1_OBJECT * X509_EXTENSION_get_object();
1086ASN1_OCTET_STRING *X509_EXTENSION_get_data();
1087int X509_EXTENSION_get_critical();
1088ASN1_OCTET_STRING *X509v3_pack_string();
1089ASN1_STRING * X509v3_unpack_string();
1090
1091int X509_verify_cert();
1092char * X509_verify_cert_error_string();
1093
1094/* lookup a cert from a X509 STACK */
1095X509 *X509_find_by_issuer_and_serial();
1096X509 *X509_find_by_subject();
1097
1098#endif
1099
1100/* BEGIN ERROR CODES */
1101/* Error codes for the X509 functions. */
1102
1103/* Function codes. */
1104#define X509_F_ADD_CERT_DIR 100
1105#define X509_F_BY_FILE_CTRL 101
1106#define X509_F_DIR_CTRL 102
1107#define X509_F_GET_CERT_BY_SUBJECT 103
1108#define X509_F_X509V3_ADD_EXT 104
1109#define X509_F_X509V3_ADD_EXTENSION 105
1110#define X509_F_X509V3_PACK_STRING 106
1111#define X509_F_X509V3_UNPACK_STRING 107
1112#define X509_F_X509_EXTENSION_CREATE_BY_NID 108
1113#define X509_F_X509_EXTENSION_CREATE_BY_OBJ 109
1114#define X509_F_X509_GET_PUBKEY_PARAMETERS 110
1115#define X509_F_X509_LOAD_CERT_FILE 111
1116#define X509_F_X509_LOAD_CRL_FILE 112
1117#define X509_F_X509_NAME_ADD_ENTRY 113
1118#define X509_F_X509_NAME_ENTRY_CREATE_BY_NID 114
1119#define X509_F_X509_NAME_ENTRY_SET_OBJECT 115
1120#define X509_F_X509_NAME_ONELINE 116
1121#define X509_F_X509_NAME_PRINT 117
1122#define X509_F_X509_PRINT_FP 118
1123#define X509_F_X509_PUBKEY_GET 119
1124#define X509_F_X509_PUBKEY_SET 120
1125#define X509_F_X509_REQ_PRINT 121
1126#define X509_F_X509_REQ_PRINT_FP 122
1127#define X509_F_X509_REQ_TO_X509 123
1128#define X509_F_X509_STORE_ADD_CERT 124
1129#define X509_F_X509_STORE_ADD_CRL 125
1130#define X509_F_X509_TO_X509_REQ 126
1131#define X509_F_X509_VERIFY_CERT 127
1132
1133/* Reason codes. */
1134#define X509_R_BAD_X509_FILETYPE 100
1135#define X509_R_CERT_ALREADY_IN_HASH_TABLE 101
1136#define X509_R_ERR_ASN1_LIB 102
1137#define X509_R_LOADING_CERT_DIR 103
1138#define X509_R_LOADING_DEFAULTS 104
1139#define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 105
1140#define X509_R_SHOULD_RETRY 106
1141#define X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN 107
1142#define X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY 108
1143#define X509_R_UNKNOWN_NID 109
1144#define X509_R_UNKNOWN_STRING_TYPE 110
1145#define X509_R_UNSUPPORTED_ALGORITHM 111
1146#define X509_R_WRONG_LOOKUP_TYPE 112
1147
1148#ifdef __cplusplus
1149}
1150#endif
1151#endif
1152
diff --git a/src/lib/libcrypto/x509/x509_cmp.c b/src/lib/libcrypto/x509/x509_cmp.c
new file mode 100644
index 0000000000..f9d9510ac5
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_cmp.c
@@ -0,0 +1,257 @@
1/* crypto/x509/x509_cmp.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <sys/types.h>
61#include <sys/stat.h>
62#include "cryptlib.h"
63#include "asn1.h"
64#include "objects.h"
65#include "x509.h"
66
67int X509_issuer_and_serial_cmp(a,b)
68X509 *a;
69X509 *b;
70 {
71 int i;
72 X509_CINF *ai,*bi;
73
74 ai=a->cert_info;
75 bi=b->cert_info;
76 i=ASN1_INTEGER_cmp(ai->serialNumber,bi->serialNumber);
77 if (i) return(i);
78 return(X509_NAME_cmp(ai->issuer,bi->issuer));
79 }
80
81#ifndef NO_MD5
82unsigned long X509_issuer_and_serial_hash(a)
83X509 *a;
84 {
85 unsigned long ret=0;
86 MD5_CTX ctx;
87 unsigned char md[16];
88 char str[256];
89
90 X509_NAME_oneline(a->cert_info->issuer,str,256);
91 ret=strlen(str);
92 MD5_Init(&ctx);
93 MD5_Update(&ctx,(unsigned char *)str,ret);
94 MD5_Update(&ctx,(unsigned char *)a->cert_info->serialNumber->data,
95 (unsigned long)a->cert_info->serialNumber->length);
96 MD5_Final(&(md[0]),&ctx);
97 ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)|
98 ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
99 )&0xffffffffL;
100 return(ret);
101 }
102#endif
103
104int X509_issuer_name_cmp(a, b)
105X509 *a;
106X509 *b;
107 {
108 return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer));
109 }
110
111int X509_subject_name_cmp(a, b)
112X509 *a;
113X509 *b;
114 {
115 return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject));
116 }
117
118int X509_CRL_cmp(a, b)
119X509_CRL *a;
120X509_CRL *b;
121 {
122 return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer));
123 }
124
125X509_NAME *X509_get_issuer_name(a)
126X509 *a;
127 {
128 return(a->cert_info->issuer);
129 }
130
131unsigned long X509_issuer_name_hash(x)
132X509 *x;
133 {
134 return(X509_NAME_hash(x->cert_info->issuer));
135 }
136
137X509_NAME *X509_get_subject_name(a)
138X509 *a;
139 {
140 return(a->cert_info->subject);
141 }
142
143ASN1_INTEGER *X509_get_serialNumber(a)
144X509 *a;
145 {
146 return(a->cert_info->serialNumber);
147 }
148
149unsigned long X509_subject_name_hash(x)
150X509 *x;
151 {
152 return(X509_NAME_hash(x->cert_info->subject));
153 }
154
155int X509_NAME_cmp(a, b)
156X509_NAME *a;
157X509_NAME *b;
158 {
159 int i,j;
160 X509_NAME_ENTRY *na,*nb;
161
162 if (sk_num(a->entries) != sk_num(b->entries))
163 return(sk_num(a->entries)-sk_num(b->entries));
164 for (i=sk_num(a->entries)-1; i>=0; i--)
165 {
166 na=(X509_NAME_ENTRY *)sk_value(a->entries,i);
167 nb=(X509_NAME_ENTRY *)sk_value(b->entries,i);
168 j=na->value->length-nb->value->length;
169 if (j) return(j);
170 j=memcmp(na->value->data,nb->value->data,
171 na->value->length);
172 if (j) return(j);
173 j=na->set-nb->set;
174 if (j) return(j);
175 }
176
177 /* We will check the object types after checking the values
178 * since the values will more often be different than the object
179 * types. */
180 for (i=sk_num(a->entries)-1; i>=0; i--)
181 {
182 na=(X509_NAME_ENTRY *)sk_value(a->entries,i);
183 nb=(X509_NAME_ENTRY *)sk_value(b->entries,i);
184 j=OBJ_cmp(na->object,nb->object);
185 if (j) return(j);
186 }
187 return(0);
188 }
189
190#ifndef NO_MD5
191/* I now DER encode the name and hash it. Since I cache the DER encoding,
192 * this is reasonably effiecent. */
193unsigned long X509_NAME_hash(x)
194X509_NAME *x;
195 {
196 unsigned long ret=0;
197 unsigned char md[16];
198 unsigned char str[256],*p,*pp;
199 int i;
200
201 i=i2d_X509_NAME(x,NULL);
202 if (i > sizeof(str))
203 p=Malloc(i);
204 else
205 p=str;
206
207 pp=p;
208 i2d_X509_NAME(x,&pp);
209 MD5((unsigned char *)p,i,&(md[0]));
210 if (p != str) Free(p);
211
212 ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)|
213 ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
214 )&0xffffffffL;
215 return(ret);
216 }
217#endif
218
219/* Search a stack of X509 for a match */
220X509 *X509_find_by_issuer_and_serial(sk,name,serial)
221STACK *sk;
222X509_NAME *name;
223ASN1_INTEGER *serial;
224 {
225 int i;
226 X509_CINF cinf;
227 X509 x,*x509=NULL;
228
229 x.cert_info= &cinf;
230 cinf.serialNumber=serial;
231 cinf.issuer=name;
232
233 for (i=0; i<sk_num(sk); i++)
234 {
235 x509=(X509 *)sk_value(sk,i);
236 if (X509_issuer_and_serial_cmp(x509,&x) == 0)
237 return(x509);
238 }
239 return(NULL);
240 }
241
242X509 *X509_find_by_subject(sk,name)
243STACK *sk;
244X509_NAME *name;
245 {
246 X509 *x509;
247 int i;
248
249 for (i=0; i<sk_num(sk); i++)
250 {
251 x509=(X509 *)sk_value(sk,i);
252 if (X509_NAME_cmp(X509_get_subject_name(x509),name) == 0)
253 return(x509);
254 }
255 return(NULL);
256 }
257
diff --git a/src/lib/libcrypto/x509/x509_d2.c b/src/lib/libcrypto/x509/x509_d2.c
new file mode 100644
index 0000000000..01e22f4cb4
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_d2.c
@@ -0,0 +1,110 @@
1/* crypto/x509/x509_d2.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <sys/types.h>
61#include <sys/stat.h>
62#include "cryptlib.h"
63#include "crypto.h"
64#include "x509.h"
65
66#ifndef NO_STDIO
67int X509_STORE_set_default_paths(ctx)
68X509_STORE *ctx;
69 {
70 X509_LOOKUP *lookup;
71
72 lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_file());
73 if (lookup == NULL) return(0);
74 X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
75
76 lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_hash_dir());
77 if (lookup == NULL) return(0);
78 X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
79
80 /* clear any errors */
81 ERR_clear_error();
82
83 return(1);
84 }
85
86int X509_STORE_load_locations(ctx,file,path)
87X509_STORE *ctx;
88char *file;
89char *path;
90 {
91 X509_LOOKUP *lookup;
92
93 if (file != NULL)
94 {
95 lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_file());
96 if (lookup == NULL) return(0);
97 X509_LOOKUP_load_file(lookup,file,X509_FILETYPE_PEM);
98 }
99 if (path != NULL)
100 {
101 lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_hash_dir());
102 if (lookup == NULL) return(0);
103 X509_LOOKUP_add_dir(lookup,path,X509_FILETYPE_PEM);
104 }
105 if ((path == NULL) && (file == NULL))
106 return(0);
107 return(1);
108 }
109
110#endif
diff --git a/src/lib/libcrypto/x509/x509_def.c b/src/lib/libcrypto/x509/x509_def.c
new file mode 100644
index 0000000000..d9ab39b15a
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_def.c
@@ -0,0 +1,83 @@
1/* crypto/x509/x509_def.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <sys/types.h>
61#include <sys/stat.h>
62#include "cryptlib.h"
63#include "crypto.h"
64#include "x509.h"
65
66char *X509_get_default_private_dir()
67 { return(X509_PRIVATE_DIR); }
68
69char *X509_get_default_cert_area()
70 { return(X509_CERT_AREA); }
71
72char *X509_get_default_cert_dir()
73 { return(X509_CERT_DIR); }
74
75char *X509_get_default_cert_file()
76 { return(X509_CERT_FILE); }
77
78char *X509_get_default_cert_dir_env()
79 { return(X509_CERT_DIR_EVP); }
80
81char *X509_get_default_cert_file_env()
82 { return(X509_CERT_FILE_EVP); }
83
diff --git a/src/lib/libcrypto/x509/x509_err.c b/src/lib/libcrypto/x509/x509_err.c
new file mode 100644
index 0000000000..9304721612
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_err.c
@@ -0,0 +1,130 @@
1/* lib/x509/x509_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "err.h"
60#include "x509.h"
61
62/* BEGIN ERROR CODES */
63#ifndef NO_ERR
64static ERR_STRING_DATA X509_str_functs[]=
65 {
66{ERR_PACK(0,X509_F_ADD_CERT_DIR,0), "ADD_CERT_DIR"},
67{ERR_PACK(0,X509_F_BY_FILE_CTRL,0), "BY_FILE_CTRL"},
68{ERR_PACK(0,X509_F_DIR_CTRL,0), "DIR_CTRL"},
69{ERR_PACK(0,X509_F_GET_CERT_BY_SUBJECT,0), "GET_CERT_BY_SUBJECT"},
70{ERR_PACK(0,X509_F_X509V3_ADD_EXT,0), "X509v3_add_ext"},
71{ERR_PACK(0,X509_F_X509V3_ADD_EXTENSION,0), "X509V3_ADD_EXTENSION"},
72{ERR_PACK(0,X509_F_X509V3_PACK_STRING,0), "X509v3_pack_string"},
73{ERR_PACK(0,X509_F_X509V3_UNPACK_STRING,0), "X509v3_unpack_string"},
74{ERR_PACK(0,X509_F_X509_EXTENSION_CREATE_BY_NID,0), "X509_EXTENSION_create_by_NID"},
75{ERR_PACK(0,X509_F_X509_EXTENSION_CREATE_BY_OBJ,0), "X509_EXTENSION_create_by_OBJ"},
76{ERR_PACK(0,X509_F_X509_GET_PUBKEY_PARAMETERS,0), "X509_get_pubkey_parameters"},
77{ERR_PACK(0,X509_F_X509_LOAD_CERT_FILE,0), "X509_LOAD_CERT_FILE"},
78{ERR_PACK(0,X509_F_X509_LOAD_CRL_FILE,0), "X509_LOAD_CRL_FILE"},
79{ERR_PACK(0,X509_F_X509_NAME_ADD_ENTRY,0), "X509_NAME_add_entry"},
80{ERR_PACK(0,X509_F_X509_NAME_ENTRY_CREATE_BY_NID,0), "X509_NAME_ENTRY_create_by_NID"},
81{ERR_PACK(0,X509_F_X509_NAME_ENTRY_SET_OBJECT,0), "X509_NAME_ENTRY_set_object"},
82{ERR_PACK(0,X509_F_X509_NAME_ONELINE,0), "X509_NAME_oneline"},
83{ERR_PACK(0,X509_F_X509_NAME_PRINT,0), "X509_NAME_print"},
84{ERR_PACK(0,X509_F_X509_PRINT_FP,0), "X509_print_fp"},
85{ERR_PACK(0,X509_F_X509_PUBKEY_GET,0), "X509_PUBKEY_get"},
86{ERR_PACK(0,X509_F_X509_PUBKEY_SET,0), "X509_PUBKEY_set"},
87{ERR_PACK(0,X509_F_X509_REQ_PRINT,0), "X509_REQ_print"},
88{ERR_PACK(0,X509_F_X509_REQ_PRINT_FP,0), "X509_REQ_print_fp"},
89{ERR_PACK(0,X509_F_X509_REQ_TO_X509,0), "X509_REQ_to_X509"},
90{ERR_PACK(0,X509_F_X509_STORE_ADD_CERT,0), "X509_STORE_ADD_CERT"},
91{ERR_PACK(0,X509_F_X509_STORE_ADD_CRL,0), "X509_STORE_ADD_CRL"},
92{ERR_PACK(0,X509_F_X509_TO_X509_REQ,0), "X509_to_X509_REQ"},
93{ERR_PACK(0,X509_F_X509_VERIFY_CERT,0), "X509_verify_cert"},
94{0,NULL},
95 };
96
97static ERR_STRING_DATA X509_str_reasons[]=
98 {
99{X509_R_BAD_X509_FILETYPE ,"bad x509 filetype"},
100{X509_R_CERT_ALREADY_IN_HASH_TABLE ,"cert already in hash table"},
101{X509_R_ERR_ASN1_LIB ,"err asn1 lib"},
102{X509_R_LOADING_CERT_DIR ,"loading cert dir"},
103{X509_R_LOADING_DEFAULTS ,"loading defaults"},
104{X509_R_NO_CERT_SET_FOR_US_TO_VERIFY ,"no cert set for us to verify"},
105{X509_R_SHOULD_RETRY ,"should retry"},
106{X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN,"unable to find parameters in chain"},
107{X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY ,"unable to get certs public key"},
108{X509_R_UNKNOWN_NID ,"unknown nid"},
109{X509_R_UNKNOWN_STRING_TYPE ,"unknown string type"},
110{X509_R_UNSUPPORTED_ALGORITHM ,"unsupported algorithm"},
111{X509_R_WRONG_LOOKUP_TYPE ,"wrong lookup type"},
112{0,NULL},
113 };
114
115#endif
116
117void ERR_load_X509_strings()
118 {
119 static int init=1;
120
121 if (init);
122 {;
123 init=0;
124#ifndef NO_ERR
125 ERR_load_strings(ERR_LIB_X509,X509_str_functs);
126 ERR_load_strings(ERR_LIB_X509,X509_str_reasons);
127#endif
128
129 }
130 }
diff --git a/src/lib/libcrypto/x509/x509_ext.c b/src/lib/libcrypto/x509/x509_ext.c
new file mode 100644
index 0000000000..1d76ecfcfd
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_ext.c
@@ -0,0 +1,222 @@
1/* crypto/x509/x509_ext.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "stack.h"
61#include "cryptlib.h"
62#include "asn1.h"
63#include "objects.h"
64#include "evp.h"
65#include "x509.h"
66
67int X509_CRL_get_ext_count(x)
68X509_CRL *x;
69 {
70 return(X509v3_get_ext_count(x->crl->extensions));
71 }
72
73int X509_CRL_get_ext_by_NID(x,nid,lastpos)
74X509_CRL *x;
75int nid;
76int lastpos;
77 {
78 return(X509v3_get_ext_by_NID(x->crl->extensions,nid,lastpos));
79 }
80
81int X509_CRL_get_ext_by_OBJ(x,obj,lastpos)
82X509_CRL *x;
83ASN1_OBJECT *obj;
84int lastpos;
85 {
86 return(X509v3_get_ext_by_OBJ(x->crl->extensions,obj,lastpos));
87 }
88
89int X509_CRL_get_ext_by_critical(x,crit,lastpos)
90X509_CRL *x;
91int crit;
92int lastpos;
93 {
94 return(X509v3_get_ext_by_critical(x->crl->extensions,crit,lastpos));
95 }
96
97X509_EXTENSION *X509_CRL_get_ext(x,loc)
98X509_CRL *x;
99int loc;
100 {
101 return(X509v3_get_ext(x->crl->extensions,loc));
102 }
103
104X509_EXTENSION *X509_CRL_delete_ext(x,loc)
105X509_CRL *x;
106int loc;
107 {
108 return(X509v3_delete_ext(x->crl->extensions,loc));
109 }
110
111int X509_CRL_add_ext(x,ex,loc)
112X509_CRL *x;
113X509_EXTENSION *ex;
114int loc;
115 {
116 return(X509v3_add_ext(&(x->crl->extensions),ex,loc) != NULL);
117 }
118
119int X509_get_ext_count(x)
120X509 *x;
121 {
122 return(X509v3_get_ext_count(x->cert_info->extensions));
123 }
124
125int X509_get_ext_by_NID(x,nid,lastpos)
126X509 *x;
127int nid;
128int lastpos;
129 {
130 return(X509v3_get_ext_by_NID(x->cert_info->extensions,nid,lastpos));
131 }
132
133int X509_get_ext_by_OBJ(x,obj,lastpos)
134X509 *x;
135ASN1_OBJECT *obj;
136int lastpos;
137 {
138 return(X509v3_get_ext_by_OBJ(x->cert_info->extensions,obj,lastpos));
139 }
140
141int X509_get_ext_by_critical(x,crit,lastpos)
142X509 *x;
143int crit;
144int lastpos;
145 {
146 return(X509v3_get_ext_by_critical(x->cert_info->extensions,crit,lastpos));
147 }
148
149X509_EXTENSION *X509_get_ext(x,loc)
150X509 *x;
151int loc;
152 {
153 return(X509v3_get_ext(x->cert_info->extensions,loc));
154 }
155
156X509_EXTENSION *X509_delete_ext(x,loc)
157X509 *x;
158int loc;
159 {
160 return(X509v3_delete_ext(x->cert_info->extensions,loc));
161 }
162
163int X509_add_ext(x,ex,loc)
164X509 *x;
165X509_EXTENSION *ex;
166int loc;
167 {
168 return(X509v3_add_ext(&(x->cert_info->extensions),ex,loc) != NULL);
169 }
170
171int X509_REVOKED_get_ext_count(x)
172X509_REVOKED *x;
173 {
174 return(X509v3_get_ext_count(x->extensions));
175 }
176
177int X509_REVOKED_get_ext_by_NID(x,nid,lastpos)
178X509_REVOKED *x;
179int nid;
180int lastpos;
181 {
182 return(X509v3_get_ext_by_NID(x->extensions,nid,lastpos));
183 }
184
185int X509_REVOKED_get_ext_by_OBJ(x,obj,lastpos)
186X509_REVOKED *x;
187ASN1_OBJECT *obj;
188int lastpos;
189 {
190 return(X509v3_get_ext_by_OBJ(x->extensions,obj,lastpos));
191 }
192
193int X509_REVOKED_get_ext_by_critical(x,crit,lastpos)
194X509_REVOKED *x;
195int crit;
196int lastpos;
197 {
198 return(X509v3_get_ext_by_critical(x->extensions,crit,lastpos));
199 }
200
201X509_EXTENSION *X509_REVOKED_get_ext(x,loc)
202X509_REVOKED *x;
203int loc;
204 {
205 return(X509v3_get_ext(x->extensions,loc));
206 }
207
208X509_EXTENSION *X509_REVOKED_delete_ext(x,loc)
209X509_REVOKED *x;
210int loc;
211 {
212 return(X509v3_delete_ext(x->extensions,loc));
213 }
214
215int X509_REVOKED_add_ext(x,ex,loc)
216X509_REVOKED *x;
217X509_EXTENSION *ex;
218int loc;
219 {
220 return(X509v3_add_ext(&(x->extensions),ex,loc) != NULL);
221 }
222
diff --git a/src/lib/libcrypto/x509/x509_lu.c b/src/lib/libcrypto/x509/x509_lu.c
new file mode 100644
index 0000000000..2c7e10a46e
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_lu.c
@@ -0,0 +1,446 @@
1/* crypto/x509/x509_lu.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "lhash.h"
62#include "x509.h"
63
64static STACK *x509_store_meth=NULL;
65static STACK *x509_store_ctx_meth=NULL;
66
67X509_LOOKUP *X509_LOOKUP_new(method)
68X509_LOOKUP_METHOD *method;
69 {
70 X509_LOOKUP *ret;
71
72 ret=(X509_LOOKUP *)Malloc(sizeof(X509_LOOKUP));
73 if (ret == NULL) return(NULL);
74
75 ret->init=0;
76 ret->skip=0;
77 ret->method=method;
78 ret->method_data=NULL;
79 ret->store_ctx=NULL;
80 if ((method->new_item != NULL) && !method->new_item(ret))
81 {
82 Free(ret);
83 return(NULL);
84 }
85 return(ret);
86 }
87
88void X509_LOOKUP_free(ctx)
89X509_LOOKUP *ctx;
90 {
91 if (ctx == NULL) return;
92 if ( (ctx->method != NULL) &&
93 (ctx->method->free != NULL))
94 ctx->method->free(ctx);
95 Free(ctx);
96 }
97
98int X509_LOOKUP_init(ctx)
99X509_LOOKUP *ctx;
100 {
101 if (ctx->method == NULL) return(0);
102 if (ctx->method->init != NULL)
103 return(ctx->method->init(ctx));
104 else
105 return(1);
106 }
107
108int X509_LOOKUP_shutdown(ctx)
109X509_LOOKUP *ctx;
110 {
111 if (ctx->method == NULL) return(0);
112 if (ctx->method->init != NULL)
113 return(ctx->method->shutdown(ctx));
114 else
115 return(1);
116 }
117
118int X509_LOOKUP_ctrl(ctx,cmd,argc,argl,ret)
119X509_LOOKUP *ctx;
120int cmd;
121char *argc;
122long argl;
123char **ret;
124 {
125 if (ctx->method == NULL) return(-1);
126 if (ctx->method->ctrl != NULL)
127 return(ctx->method->ctrl(ctx,cmd,argc,argl,ret));
128 else
129 return(1);
130 }
131
132int X509_LOOKUP_by_subject(ctx,type,name,ret)
133X509_LOOKUP *ctx;
134int type;
135X509_NAME *name;
136X509_OBJECT *ret;
137 {
138 if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))
139 return(X509_LU_FAIL);
140 if (ctx->skip) return(0);
141 return(ctx->method->get_by_subject(ctx,type,name,ret));
142 }
143
144int X509_LOOKUP_by_issuer_serial(ctx,type,name,serial,ret)
145X509_LOOKUP *ctx;
146int type;
147X509_NAME *name;
148ASN1_INTEGER *serial;
149X509_OBJECT *ret;
150 {
151 if ((ctx->method == NULL) ||
152 (ctx->method->get_by_issuer_serial == NULL))
153 return(X509_LU_FAIL);
154 return(ctx->method->get_by_issuer_serial(ctx,type,name,serial,ret));
155 }
156
157int X509_LOOKUP_by_fingerprint(ctx,type,bytes,len,ret)
158X509_LOOKUP *ctx;
159int type;
160unsigned char *bytes;
161int len;
162X509_OBJECT *ret;
163 {
164 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
165 return(X509_LU_FAIL);
166 return(ctx->method->get_by_fingerprint(ctx,type,bytes,len,ret));
167 }
168
169int X509_LOOKUP_by_alias(ctx,type,str,len,ret)
170X509_LOOKUP *ctx;
171int type;
172char *str;
173int len;
174X509_OBJECT *ret;
175 {
176 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
177 return(X509_LU_FAIL);
178 return(ctx->method->get_by_alias(ctx,str,len,ret));
179 }
180
181static unsigned long x509_object_hash(a)
182X509_OBJECT *a;
183 {
184 unsigned long h;
185
186 switch (a->type)
187 {
188 case X509_LU_X509:
189 h=X509_NAME_hash(a->data.x509->cert_info->subject);
190 break;
191 case X509_LU_CRL:
192 h=X509_NAME_hash(a->data.crl->crl->issuer);
193 break;
194 default:
195 abort();
196 }
197 return(h);
198 }
199
200static int x509_object_cmp(a,b)
201X509_OBJECT *a,*b;
202 {
203 int ret;
204
205 ret=(a->type - b->type);
206 if (ret) return(ret);
207 switch (a->type)
208 {
209 case X509_LU_X509:
210 ret=X509_subject_name_cmp(a->data.x509,b->data.x509);
211 break;
212 case X509_LU_CRL:
213 ret=X509_CRL_cmp(a->data.crl,b->data.crl);
214 break;
215 default:
216 abort();
217 }
218 return(ret);
219 }
220
221X509_STORE *X509_STORE_new()
222 {
223 X509_STORE *ret;
224
225 if ((ret=(X509_STORE *)Malloc(sizeof(X509_STORE))) == NULL)
226 return(NULL);
227 ret->certs=lh_new(x509_object_hash,x509_object_cmp);
228 ret->cache=1;
229 ret->get_cert_methods=sk_new_null();
230 ret->verify=NULL;
231 ret->verify_cb=NULL;
232 memset(&ret->ex_data,0,sizeof(CRYPTO_EX_DATA));
233 ret->references=1;
234 return(ret);
235 }
236
237static void cleanup(a)
238X509_OBJECT *a;
239 {
240 if (a->type == X509_LU_X509)
241 {
242 X509_free(a->data.x509);
243 }
244 else if (a->type == X509_LU_CRL)
245 {
246 X509_CRL_free(a->data.crl);
247 }
248 else
249 abort();
250
251 Free(a);
252 }
253
254void X509_STORE_free(vfy)
255X509_STORE *vfy;
256 {
257 int i;
258 STACK *sk;
259 X509_LOOKUP *lu;
260
261 sk=vfy->get_cert_methods;
262 for (i=0; i<sk_num(sk); i++)
263 {
264 lu=(X509_LOOKUP *)sk_value(sk,i);
265 X509_LOOKUP_shutdown(lu);
266 X509_LOOKUP_free(lu);
267 }
268 sk_free(sk);
269
270 CRYPTO_free_ex_data(x509_store_meth,(char *)vfy,&vfy->ex_data);
271 lh_doall(vfy->certs,cleanup);
272 lh_free(vfy->certs);
273 Free(vfy);
274 }
275
276X509_LOOKUP *X509_STORE_add_lookup(v,m)
277X509_STORE *v;
278X509_LOOKUP_METHOD *m;
279 {
280 int i;
281 STACK *sk;
282 X509_LOOKUP *lu;
283
284 sk=v->get_cert_methods;
285 for (i=0; i<sk_num(sk); i++)
286 {
287 lu=(X509_LOOKUP *)sk_value(sk,i);
288 if (m == lu->method)
289 {
290 return(lu);
291 }
292 }
293 /* a new one */
294 lu=X509_LOOKUP_new(m);
295 if (lu == NULL)
296 return(NULL);
297 else
298 {
299 lu->store_ctx=v;
300 if (sk_push(v->get_cert_methods,(char *)lu))
301 return(lu);
302 else
303 {
304 X509_LOOKUP_free(lu);
305 return(NULL);
306 }
307 }
308 }
309
310int X509_STORE_get_by_subject(vs,type,name,ret)
311X509_STORE_CTX *vs;
312int type;
313X509_NAME *name;
314X509_OBJECT *ret;
315 {
316 X509_STORE *ctx=vs->ctx;
317 X509_LOOKUP *lu;
318 X509_OBJECT stmp,*tmp;
319 int i,j;
320
321 tmp=X509_OBJECT_retrive_by_subject(ctx->certs,type,name);
322
323 if (tmp == NULL)
324 {
325 for (i=vs->current_method; i<sk_num(ctx->get_cert_methods); i++)
326 {
327 lu=(X509_LOOKUP *)sk_value(ctx->get_cert_methods,i);
328 j=X509_LOOKUP_by_subject(lu,type,name,&stmp);
329 if (j < 0)
330 {
331 vs->current_method=j;
332 return(j);
333 }
334 else if (j)
335 {
336 tmp= &stmp;
337 break;
338 }
339 }
340 vs->current_method=0;
341 if (tmp == NULL)
342 return(0);
343 }
344
345/* if (ret->data.ptr != NULL)
346 X509_OBJECT_free_contents(ret); */
347
348 ret->type=tmp->type;
349 ret->data.ptr=tmp->data.ptr;
350
351 X509_OBJECT_up_ref_count(ret);
352
353 return(1);
354 }
355
356void X509_OBJECT_up_ref_count(a)
357X509_OBJECT *a;
358 {
359 switch (a->type)
360 {
361 case X509_LU_X509:
362 CRYPTO_add(&a->data.x509->references,1,CRYPTO_LOCK_X509);
363 break;
364 case X509_LU_CRL:
365 CRYPTO_add(&a->data.crl->references,1,CRYPTO_LOCK_X509_CRL);
366 break;
367 }
368 }
369
370void X509_OBJECT_free_contents(a)
371X509_OBJECT *a;
372 {
373 switch (a->type)
374 {
375 case X509_LU_X509:
376 X509_free(a->data.x509);
377 break;
378 case X509_LU_CRL:
379 X509_CRL_free(a->data.crl);
380 break;
381 }
382 }
383
384X509_OBJECT *X509_OBJECT_retrive_by_subject(h,type,name)
385LHASH *h;
386int type;
387X509_NAME *name;
388 {
389 X509_OBJECT stmp,*tmp;
390 X509 x509_s;
391 X509_CINF cinf_s;
392 X509_CRL crl_s;
393 X509_CRL_INFO crl_info_s;
394
395 stmp.type=type;
396 switch (type)
397 {
398 case X509_LU_X509:
399 stmp.data.x509= &x509_s;
400 x509_s.cert_info= &cinf_s;
401 cinf_s.subject=name;
402 break;
403 case X509_LU_CRL:
404 stmp.data.crl= &crl_s;
405 crl_s.crl= &crl_info_s;
406 crl_info_s.issuer=name;
407 break;
408 default:
409 abort();
410 }
411
412 tmp=(X509_OBJECT *)lh_retrieve(h,(char *)&stmp);
413 return(tmp);
414 }
415
416void X509_STORE_CTX_init(ctx,store,x509,chain)
417X509_STORE_CTX *ctx;
418X509_STORE *store;
419X509 *x509;
420STACK *chain;
421 {
422 ctx->ctx=store;
423 ctx->current_method=0;
424 ctx->cert=x509;
425 ctx->untrusted=chain;
426 ctx->last_untrusted=0;
427 ctx->valid=0;
428 ctx->chain=NULL;
429 ctx->depth=10;
430 ctx->error=0;
431 ctx->current_cert=NULL;
432 memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA));
433 }
434
435void X509_STORE_CTX_cleanup(ctx)
436X509_STORE_CTX *ctx;
437 {
438 if (ctx->chain != NULL)
439 {
440 sk_pop_free(ctx->chain,X509_free);
441 ctx->chain=NULL;
442 }
443 CRYPTO_free_ex_data(x509_store_ctx_meth,(char *)ctx,&(ctx->ex_data));
444 memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));
445 }
446
diff --git a/src/lib/libcrypto/x509/x509_obj.c b/src/lib/libcrypto/x509/x509_obj.c
new file mode 100644
index 0000000000..c0576fd6f6
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_obj.c
@@ -0,0 +1,179 @@
1/* crypto/x509/x509_obj.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "lhash.h"
62#include "objects.h"
63#include "x509.h"
64#include "buffer.h"
65
66char *X509_NAME_oneline(a,buf,len)
67X509_NAME *a;
68char *buf;
69int len;
70 {
71 X509_NAME_ENTRY *ne;
72 unsigned int i;
73 int n,lold,l,l1,l2,num,j,type;
74 char *s,*p;
75 unsigned char *q;
76 BUF_MEM *b=NULL;
77 static char hex[17]="0123456789ABCDEF";
78 int gs_doit[4];
79 char tmp_buf[80];
80
81 if (a == NULL) return("NO X509_NAME");
82 if (buf == NULL)
83 {
84 if ((b=BUF_MEM_new()) == NULL) goto err;
85 if (!BUF_MEM_grow(b,200)) goto err;
86 b->data[0]='\0';
87 len=200;
88 }
89
90 len--; /* space for '\0' */
91 l=0;
92 for (i=0; (int)i<sk_num(a->entries); i++)
93 {
94 ne=(X509_NAME_ENTRY *)sk_value(a->entries,i);
95 n=OBJ_obj2nid(ne->object);
96 if ((n == NID_undef) || ((s=OBJ_nid2sn(n)) == NULL))
97 {
98 i2t_ASN1_OBJECT(tmp_buf,sizeof(tmp_buf),ne->object);
99 s=tmp_buf;
100 }
101 l1=strlen(s);
102
103 type=ne->value->type;
104 num=ne->value->length;
105 q=ne->value->data;
106
107 if ((type == V_ASN1_GENERALSTRING) && ((num%4) == 0))
108 {
109 gs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=0;
110 for (j=0; j<num; j++)
111 if (q[j] != 0) gs_doit[j&3]=1;
112
113 if (gs_doit[0]|gs_doit[1]|gs_doit[2])
114 gs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=1;
115 else
116 {
117 gs_doit[0]=gs_doit[1]=gs_doit[2]=0;
118 gs_doit[3]=1;
119 }
120 }
121 else
122 gs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=1;
123
124 for (l2=j=0; j<num; j++)
125 {
126 if (!gs_doit[j&3]) continue;
127 l2++;
128 if ((q[j] < ' ') || (q[j] > '~')) l2+=3;
129 }
130
131 lold=l;
132 l+=1+l1+1+l2;
133 if (b != NULL)
134 {
135 if (!BUF_MEM_grow(b,l+1)) goto err;
136 p= &(b->data[lold]);
137 }
138 else if (l > len)
139 {
140 break;
141 }
142 else
143 p= &(buf[lold]);
144 *(p++)='/';
145 memcpy(p,s,(unsigned int)l1); p+=l1;
146 *(p++)='=';
147
148 q=ne->value->data;
149
150 for (j=0; j<num; j++)
151 {
152 if (!gs_doit[j&3]) continue;
153 n=q[j];
154 if ((n < ' ') || (n > '~'))
155 {
156 *(p++)='\\';
157 *(p++)='x';
158 *(p++)=hex[(n>>4)&0x0f];
159 *(p++)=hex[n&0x0f];
160 }
161 else
162 *(p++)=n;
163 }
164 *p='\0';
165 }
166 if (b != NULL)
167 {
168 p=b->data;
169 Free((char *)b);
170 }
171 else
172 p=buf;
173 return(p);
174err:
175 X509err(X509_F_X509_NAME_ONELINE,ERR_R_MALLOC_FAILURE);
176 if (b != NULL) BUF_MEM_free(b);
177 return(NULL);
178 }
179
diff --git a/src/lib/libcrypto/x509/x509_r2x.c b/src/lib/libcrypto/x509/x509_r2x.c
new file mode 100644
index 0000000000..6aec2427f7
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_r2x.c
@@ -0,0 +1,122 @@
1/* crypto/x509/x509_r2x.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "evp.h"
63#include "asn1.h"
64#include "x509.h"
65#include "objects.h"
66#include "buffer.h"
67#include "pem.h"
68
69X509 *X509_REQ_to_X509(r,days,pkey)
70X509_REQ *r;
71int days;
72EVP_PKEY *pkey;
73 {
74 X509 *ret=NULL;
75 int er=1;
76 X509_REQ_INFO *ri=NULL;
77 X509_CINF *xi=NULL;
78 X509_NAME *xn;
79
80 if ((ret=X509_new()) == NULL)
81 {
82 X509err(X509_F_X509_REQ_TO_X509,ERR_R_MALLOC_FAILURE);
83 goto err;
84 }
85
86 /* duplicate the request */
87 ri=(X509_REQ_INFO *)ASN1_dup(i2d_X509_REQ_INFO,
88 (char *(*)())d2i_X509_REQ_INFO,(char *)r->req_info);
89 if (ri == NULL) goto err;
90
91 xi=ret->cert_info;
92
93 if (sk_num(ri->attributes) != 0)
94 {
95 if ((xi->version=ASN1_INTEGER_new()) == NULL) goto err;
96 if (!ASN1_INTEGER_set(xi->version,2)) goto err;
97/* xi->extensions=ri->attributes; <- bad, should not ever be done
98 ri->attributes=NULL; */
99 }
100
101 xn=X509_REQ_get_subject_name(r);
102 X509_set_subject_name(ret,X509_NAME_dup(xn));
103 X509_set_issuer_name(ret,X509_NAME_dup(xn));
104
105 X509_gmtime_adj(xi->validity->notBefore,0);
106 X509_gmtime_adj(xi->validity->notAfter,(long)60*60*24*days);
107
108 X509_set_pubkey(ret,X509_REQ_get_pubkey(r));
109
110 if (!X509_sign(ret,pkey,EVP_md5()))
111 goto err;
112 er=0;
113err:
114 if (er)
115 {
116 X509_free(ret);
117 X509_REQ_INFO_free(ri);
118 return(NULL);
119 }
120 return(ret);
121 }
122
diff --git a/src/lib/libcrypto/x509/x509_req.c b/src/lib/libcrypto/x509/x509_req.c
new file mode 100644
index 0000000000..5004365bad
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_req.c
@@ -0,0 +1,116 @@
1/* crypto/x509/x509_req.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn.h"
62#include "evp.h"
63#include "asn1.h"
64#include "x509.h"
65#include "objects.h"
66#include "buffer.h"
67#include "pem.h"
68
69X509_REQ *X509_to_X509_REQ(x,pkey,md)
70X509 *x;
71EVP_PKEY *pkey;
72EVP_MD *md;
73 {
74 X509_REQ *ret;
75 X509_REQ_INFO *ri;
76 int i;
77
78 ret=X509_REQ_new();
79 if (ret == NULL)
80 {
81 X509err(X509_F_X509_TO_X509_REQ,ERR_R_MALLOC_FAILURE);
82 goto err;
83 }
84
85 ri=ret->req_info;
86
87 ri->version->length=1;
88 ri->version->data=(unsigned char *)Malloc(1);
89 if (ri->version->data == NULL) goto err;
90 ri->version->data[0]=0; /* version == 0 */
91
92 if (!X509_REQ_set_subject_name(ret,X509_get_subject_name(x)))
93 goto err;
94
95 i=X509_REQ_set_pubkey(ret,X509_get_pubkey(x));
96 if (!i) goto err;
97
98 if (pkey != NULL)
99 {
100 if (!X509_REQ_sign(ret,pkey,md))
101 goto err;
102 }
103 return(ret);
104err:
105 X509_REQ_free(ret);
106 return(NULL);
107 }
108
109EVP_PKEY *X509_REQ_get_pubkey(req)
110X509_REQ *req;
111 {
112 if ((req == NULL) || (req->req_info == NULL))
113 return(NULL);
114 return(X509_PUBKEY_get(req->req_info->pubkey));
115 }
116
diff --git a/src/lib/libcrypto/x509/x509_set.c b/src/lib/libcrypto/x509/x509_set.c
new file mode 100644
index 0000000000..5d0a3a0c0e
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_set.c
@@ -0,0 +1,164 @@
1/* crypto/x509/x509_set.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62#include "objects.h"
63#include "evp.h"
64#include "x509.h"
65
66int X509_set_version(x,version)
67X509 *x;
68long version;
69 {
70 if (x == NULL) return(0);
71 if (x->cert_info->version == NULL)
72 {
73 if ((x->cert_info->version=ASN1_INTEGER_new()) == NULL)
74 return(0);
75 }
76 return(ASN1_INTEGER_set(x->cert_info->version,version));
77 }
78
79int X509_set_serialNumber(x,serial)
80X509 *x;
81ASN1_INTEGER *serial;
82 {
83 ASN1_INTEGER *in;
84
85 if (x == NULL) return(0);
86 in=x->cert_info->serialNumber;
87 if (in != serial)
88 {
89 in=ASN1_INTEGER_dup(serial);
90 if (in != NULL)
91 {
92 ASN1_INTEGER_free(x->cert_info->serialNumber);
93 x->cert_info->serialNumber=in;
94 }
95 }
96 return(in != NULL);
97 }
98
99int X509_set_issuer_name(x,name)
100X509 *x;
101X509_NAME *name;
102 {
103 if ((x == NULL) || (x->cert_info == NULL)) return(0);
104 return(X509_NAME_set(&x->cert_info->issuer,name));
105 }
106
107int X509_set_subject_name(x,name)
108X509 *x;
109X509_NAME *name;
110 {
111 if ((x == NULL) || (x->cert_info == NULL)) return(0);
112 return(X509_NAME_set(&x->cert_info->subject,name));
113 }
114
115int X509_set_notBefore(x,tm)
116X509 *x;
117ASN1_UTCTIME *tm;
118 {
119 ASN1_UTCTIME *in;
120
121 if ((x == NULL) || (x->cert_info->validity == NULL)) return(0);
122 in=x->cert_info->validity->notBefore;
123 if (in != tm)
124 {
125 in=ASN1_UTCTIME_dup(tm);
126 if (in != NULL)
127 {
128 ASN1_UTCTIME_free(x->cert_info->validity->notBefore);
129 x->cert_info->validity->notBefore=in;
130 }
131 }
132 return(in != NULL);
133 }
134
135int X509_set_notAfter(x,tm)
136X509 *x;
137ASN1_UTCTIME *tm;
138 {
139 ASN1_UTCTIME *in;
140
141 if ((x == NULL) || (x->cert_info->validity == NULL)) return(0);
142 in=x->cert_info->validity->notAfter;
143 if (in != tm)
144 {
145 in=ASN1_UTCTIME_dup(tm);
146 if (in != NULL)
147 {
148 ASN1_UTCTIME_free(x->cert_info->validity->notAfter);
149 x->cert_info->validity->notAfter=in;
150 }
151 }
152 return(in != NULL);
153 }
154
155int X509_set_pubkey(x,pkey)
156X509 *x;
157EVP_PKEY *pkey;
158 {
159 if ((x == NULL) || (x->cert_info == NULL)) return(0);
160 return(X509_PUBKEY_set(&(x->cert_info->key),pkey));
161 }
162
163
164
diff --git a/src/lib/libcrypto/x509/x509_txt.c b/src/lib/libcrypto/x509/x509_txt.c
new file mode 100644
index 0000000000..408d1c277c
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_txt.c
@@ -0,0 +1,132 @@
1/* crypto/x509/x509_txt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include <errno.h>
62#include <sys/types.h>
63
64#include "cryptlib.h"
65#include "lhash.h"
66#include "buffer.h"
67#include "evp.h"
68#include "asn1.h"
69#include "x509.h"
70#include "objects.h"
71#include "pem.h"
72
73char *X509_verify_cert_error_string(n)
74long n;
75 {
76 static char buf[100];
77
78 switch ((int)n)
79 {
80 case X509_V_OK:
81 return("ok");
82 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
83 return("unable to get issuer certificate");
84 case X509_V_ERR_UNABLE_TO_GET_CRL:
85 return("unable to get certificate CRL");
86 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
87 return("unable to decrypt certificate's signature");
88 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
89 return("unable to decrypt CRL's's signature");
90 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
91 return("unable to decode issuer public key");
92 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
93 return("certificate signature failure");
94 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
95 return("CRL signature failure");
96 case X509_V_ERR_CERT_NOT_YET_VALID:
97 return("certificate is not yet valid");
98 case X509_V_ERR_CRL_NOT_YET_VALID:
99 return("CRL is not yet valid");
100 case X509_V_ERR_CERT_HAS_EXPIRED:
101 return("Certificate has expired");
102 case X509_V_ERR_CRL_HAS_EXPIRED:
103 return("CRL has expired");
104 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
105 return("format error in certificate's notBefore field");
106 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
107 return("format error in certificate's notAfter field");
108 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
109 return("format error in CRL's lastUpdate field");
110 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
111 return("format error in CRL's nextUpdate field");
112 case X509_V_ERR_OUT_OF_MEM:
113 return("out of memory");
114 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
115 return("self signed certificate");
116 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
117 return("self signed certificate in certificate chain");
118 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
119 return("unable to get local issuer certificate");
120 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
121 return("unable to verify the first certificate");
122 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
123 return("certificate chain too long");
124 case X509_V_ERR_APPLICATION_VERIFICATION:
125 return("application verification failure");
126 default:
127 sprintf(buf,"error number %ld",n);
128 return(buf);
129 }
130 }
131
132
diff --git a/src/lib/libcrypto/x509/x509_v3.c b/src/lib/libcrypto/x509/x509_v3.c
new file mode 100644
index 0000000000..1c03602f0b
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_v3.c
@@ -0,0 +1,409 @@
1/* crypto/x509/x509_v3.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "stack.h"
61#include "cryptlib.h"
62#include "asn1.h"
63#include "objects.h"
64#include "evp.h"
65#include "x509.h"
66
67#ifndef NOPROTO
68static X509_EXTENSION_METHOD *find_by_nid(int nid);
69static int xem_cmp(X509_EXTENSION_METHOD **a, X509_EXTENSION_METHOD **b);
70#else
71static X509_EXTENSION_METHOD *find_by_nid();
72static int xem_cmp();
73#endif
74
75static STACK *extensions=NULL;
76
77int X509v3_get_ext_count(x)
78STACK *x;
79 {
80 if (x == NULL) return(0);
81 return(sk_num(x));
82 }
83
84int X509v3_get_ext_by_NID(x,nid,lastpos)
85STACK *x;
86int nid;
87int lastpos;
88 {
89 ASN1_OBJECT *obj;
90
91 obj=OBJ_nid2obj(nid);
92 if (obj == NULL) return(-2);
93 return(X509v3_get_ext_by_OBJ(x,obj,lastpos));
94 }
95
96int X509v3_get_ext_by_OBJ(sk,obj,lastpos)
97STACK *sk;
98ASN1_OBJECT *obj;
99int lastpos;
100 {
101 int n;
102 X509_EXTENSION *ex;
103
104 if (sk == NULL) return(-1);
105 lastpos++;
106 if (lastpos < 0)
107 lastpos=0;
108 n=sk_num(sk);
109 for ( ; lastpos < n; lastpos++)
110 {
111 ex=(X509_EXTENSION *)sk_value(sk,lastpos);
112 if (OBJ_cmp(ex->object,obj) == 0)
113 return(lastpos);
114 }
115 return(-1);
116 }
117
118int X509v3_get_ext_by_critical(sk,crit,lastpos)
119STACK *sk;
120int crit;
121int lastpos;
122 {
123 int n;
124 X509_EXTENSION *ex;
125
126 if (sk == NULL) return(-1);
127 lastpos++;
128 if (lastpos < 0)
129 lastpos=0;
130 n=sk_num(sk);
131 for ( ; lastpos < n; lastpos++)
132 {
133 ex=(X509_EXTENSION *)sk_value(sk,lastpos);
134 if ( (ex->critical && crit) ||
135 (!ex->critical && !crit))
136 return(lastpos);
137 }
138 return(-1);
139 }
140
141X509_EXTENSION *X509v3_get_ext(x,loc)
142STACK *x;
143int loc;
144 {
145 if ((x == NULL) || (sk_num(x) <= loc) || (loc < 0))
146 return(NULL);
147 else
148 return((X509_EXTENSION *)sk_value(x,loc));
149 }
150
151X509_EXTENSION *X509v3_delete_ext(x,loc)
152STACK *x;
153int loc;
154 {
155 X509_EXTENSION *ret;
156
157 if ((x == NULL) || (sk_num(x) <= loc) || (loc < 0))
158 return(NULL);
159 ret=(X509_EXTENSION *)sk_delete(x,loc);
160 return(ret);
161 }
162
163STACK *X509v3_add_ext(x,ex,loc)
164STACK **x;
165X509_EXTENSION *ex;
166int loc;
167 {
168 X509_EXTENSION *new_ex=NULL;
169 int n;
170 STACK *sk=NULL;
171
172 if ((x != NULL) && (*x == NULL))
173 {
174 if ((sk=sk_new_null()) == NULL)
175 goto err;
176 }
177 else
178 sk= *x;
179
180 n=sk_num(sk);
181 if (loc > n) loc=n;
182 else if (loc < 0) loc=n;
183
184 if ((new_ex=X509_EXTENSION_dup(ex)) == NULL)
185 goto err2;
186 if (!sk_insert(sk,(char *)new_ex,loc))
187 goto err;
188 if ((x != NULL) && (*x == NULL))
189 *x=sk;
190 return(sk);
191err:
192 X509err(X509_F_X509V3_ADD_EXT,ERR_R_MALLOC_FAILURE);
193err2:
194 if (new_ex != NULL) X509_EXTENSION_free(new_ex);
195 if (sk != NULL) sk_free(sk);
196 return(NULL);
197 }
198
199X509_EXTENSION *X509_EXTENSION_create_by_NID(ex,nid,crit,data)
200X509_EXTENSION **ex;
201int nid;
202int crit;
203ASN1_OCTET_STRING *data;
204 {
205 ASN1_OBJECT *obj;
206 X509_EXTENSION *ret;
207
208 obj=OBJ_nid2obj(nid);
209 if (obj == NULL)
210 {
211 X509err(X509_F_X509_EXTENSION_CREATE_BY_NID,X509_R_UNKNOWN_NID);
212 return(NULL);
213 }
214 ret=X509_EXTENSION_create_by_OBJ(ex,obj,crit,data);
215 if (ret == NULL) ASN1_OBJECT_free(obj);
216 return(ret);
217 }
218
219X509_EXTENSION *X509_EXTENSION_create_by_OBJ(ex,obj,crit,data)
220X509_EXTENSION **ex;
221ASN1_OBJECT *obj;
222int crit;
223ASN1_OCTET_STRING *data;
224 {
225 X509_EXTENSION *ret;
226
227 if ((ex == NULL) || (*ex == NULL))
228 {
229 if ((ret=X509_EXTENSION_new()) == NULL)
230 {
231 X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,ERR_R_MALLOC_FAILURE);
232 return(NULL);
233 }
234 }
235 else
236 ret= *ex;
237
238 if (!X509_EXTENSION_set_object(ret,obj))
239 goto err;
240 if (!X509_EXTENSION_set_critical(ret,crit))
241 goto err;
242 if (!X509_EXTENSION_set_data(ret,data))
243 goto err;
244
245 if ((ex != NULL) && (*ex == NULL)) *ex=ret;
246 return(ret);
247err:
248 if ((ex == NULL) || (ret != *ex))
249 X509_EXTENSION_free(ret);
250 return(NULL);
251 }
252
253int X509_EXTENSION_set_object(ex,obj)
254X509_EXTENSION *ex;
255ASN1_OBJECT *obj;
256 {
257 if ((ex == NULL) || (obj == NULL))
258 return(0);
259 ASN1_OBJECT_free(ex->object);
260 ex->object=OBJ_dup(obj);
261 return(1);
262 }
263
264int X509_EXTENSION_set_critical(ex,crit)
265X509_EXTENSION *ex;
266int crit;
267 {
268 if (ex == NULL) return(0);
269 ex->critical=(crit)?0xFF:0;
270 return(1);
271 }
272
273int X509_EXTENSION_set_data(ex,data)
274X509_EXTENSION *ex;
275ASN1_OCTET_STRING *data;
276 {
277 int i;
278
279 if (ex == NULL) return(0);
280 i=ASN1_OCTET_STRING_set(ex->value,data->data,data->length);
281 if (!i) return(0);
282 return(1);
283 }
284
285ASN1_OBJECT *X509_EXTENSION_get_object(ex)
286X509_EXTENSION *ex;
287 {
288 if (ex == NULL) return(NULL);
289 return(ex->object);
290 }
291
292ASN1_OCTET_STRING *X509_EXTENSION_get_data(ex)
293X509_EXTENSION *ex;
294 {
295 if (ex == NULL) return(NULL);
296 return(ex->value);
297 }
298
299int X509_EXTENSION_get_critical(ex)
300X509_EXTENSION *ex;
301 {
302 if (ex == NULL) return(0);
303 return(ex->critical);
304 }
305
306int X509v3_data_type_by_OBJ(obj)
307ASN1_OBJECT *obj;
308 {
309 int nid;
310
311 nid=OBJ_obj2nid(obj);
312 if (nid == V_ASN1_UNDEF) return(V_ASN1_UNDEF);
313 return(X509v3_data_type_by_NID(nid));
314 }
315
316int X509v3_data_type_by_NID(nid)
317int nid;
318 {
319 X509_EXTENSION_METHOD *x;
320
321 x=find_by_nid(nid);
322 if (x == NULL)
323 return(V_ASN1_UNDEF);
324 else
325 return(x->data_type);
326 }
327
328int X509v3_pack_type_by_OBJ(obj)
329ASN1_OBJECT *obj;
330 {
331 int nid;
332
333 nid=OBJ_obj2nid(obj);
334 if (nid == NID_undef) return(X509_EXT_PACK_UNKNOWN);
335 return(X509v3_pack_type_by_NID(nid));
336 }
337
338int X509v3_pack_type_by_NID(nid)
339int nid;
340 {
341 X509_EXTENSION_METHOD *x;
342
343 x=find_by_nid(nid);
344 if (x == NULL)
345 return(X509_EXT_PACK_UNKNOWN);
346 else
347 return(x->pack_type);
348 }
349
350static X509_EXTENSION_METHOD *find_by_nid(nid)
351int nid;
352 {
353 X509_EXTENSION_METHOD x;
354 int i;
355
356 x.nid=nid;
357 if (extensions == NULL) return(NULL);
358 i=sk_find(extensions,(char *)&x);
359 if (i < 0)
360 return(NULL);
361 else
362 return((X509_EXTENSION_METHOD *)sk_value(extensions,i));
363 }
364
365static int xem_cmp(a,b)
366X509_EXTENSION_METHOD **a,**b;
367 {
368 return((*a)->nid-(*b)->nid);
369 }
370
371void X509v3_cleanup_extensions()
372 {
373 int i;
374
375 if (extensions != NULL)
376 {
377 for (i=0; i<sk_num(extensions); i++)
378 Free(sk_value(extensions,i));
379 sk_free(extensions);
380 extensions=NULL;
381 }
382 }
383
384int X509v3_add_extension(x)
385X509_EXTENSION_METHOD *x;
386 {
387 X509_EXTENSION_METHOD *newx;
388
389 if (extensions == NULL)
390 {
391 extensions=sk_new(xem_cmp);
392 if (extensions == NULL) goto err;
393 }
394 newx=(X509_EXTENSION_METHOD *)Malloc(sizeof(X509_EXTENSION_METHOD));
395 if (newx == NULL) goto err;
396 newx->nid=x->nid;
397 newx->data_type=x->data_type;
398 newx->pack_type=x->pack_type;
399 if (!sk_push(extensions,(char *)newx))
400 {
401 Free(newx);
402 goto err;
403 }
404 return(1);
405err:
406 X509err(X509_F_X509V3_ADD_EXTENSION,ERR_R_MALLOC_FAILURE);
407 return(0);
408 }
409
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c
new file mode 100644
index 0000000000..c1be91edba
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_vfy.c
@@ -0,0 +1,704 @@
1/* crypto/x509/x509_vfy.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
61#include <errno.h>
62#include <sys/types.h>
63#include <sys/stat.h>
64
65#include "crypto.h"
66#include "cryptlib.h"
67#include "lhash.h"
68#include "buffer.h"
69#include "evp.h"
70#include "asn1.h"
71#include "x509.h"
72#include "objects.h"
73#include "pem.h"
74
75#ifndef NOPROTO
76static int null_callback(int ok,X509_STORE_CTX *e);
77static int internal_verify(X509_STORE_CTX *ctx);
78#else
79static int null_callback();
80static int internal_verify();
81#endif
82
83char *X509_version="X509 part of SSLeay 0.9.0b 29-Jun-1998";
84static STACK *x509_store_ctx_method=NULL;
85static int x509_store_ctx_num=0;
86#if 0
87static int x509_store_num=1;
88static STACK *x509_store_method=NULL;
89#endif
90
91static int null_callback(ok,e)
92int ok;
93X509_STORE_CTX *e;
94 {
95 return(ok);
96 }
97
98#if 0
99static int x509_subject_cmp(a,b)
100X509 **a,**b;
101 {
102 return(X509_subject_name_cmp(*a,*b));
103 }
104#endif
105
106int X509_verify_cert(ctx)
107X509_STORE_CTX *ctx;
108 {
109 X509 *x,*xtmp,*chain_ss=NULL;
110 X509_NAME *xn;
111 X509_OBJECT obj;
112 int depth,i,ok=0;
113 int num;
114 int (*cb)();
115 STACK *sktmp=NULL;
116
117 if (ctx->cert == NULL)
118 {
119 X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
120 return(-1);
121 }
122
123 cb=ctx->ctx->verify_cb;
124 if (cb == NULL) cb=null_callback;
125
126 /* first we make sure the chain we are going to build is
127 * present and that the first entry is in place */
128 if (ctx->chain == NULL)
129 {
130 if ( ((ctx->chain=sk_new_null()) == NULL) ||
131 (!sk_push(ctx->chain,(char *)ctx->cert)))
132 {
133 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
134 goto end;
135 }
136 CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);
137 ctx->last_untrusted=1;
138 }
139
140 /* We use a temporary so we can chop and hack at it */
141 if ((ctx->untrusted != NULL) && (sktmp=sk_dup(ctx->untrusted)) == NULL)
142 {
143 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
144 goto end;
145 }
146
147 num=sk_num(ctx->chain);
148 x=(X509 *)sk_value(ctx->chain,num-1);
149 depth=ctx->depth;
150
151
152 for (;;)
153 {
154 /* If we have enough, we break */
155 if (depth <= num) break;
156
157 /* If we are self signed, we break */
158 xn=X509_get_issuer_name(x);
159 if (X509_NAME_cmp(X509_get_subject_name(x),xn) == 0)
160 break;
161
162 /* If we were passed a cert chain, use it first */
163 if (ctx->untrusted != NULL)
164 {
165 xtmp=X509_find_by_subject(sktmp,xn);
166 if (xtmp != NULL)
167 {
168 if (!sk_push(ctx->chain,(char *)xtmp))
169 {
170 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
171 goto end;
172 }
173 CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
174 sk_delete_ptr(sktmp,(char *)xtmp);
175 ctx->last_untrusted++;
176 x=xtmp;
177 num++;
178 /* reparse the full chain for
179 * the next one */
180 continue;
181 }
182 }
183 break;
184 }
185
186 /* at this point, chain should contain a list of untrusted
187 * certificates. We now need to add at least one trusted one,
188 * if possible, otherwise we complain. */
189
190 i=sk_num(ctx->chain);
191 x=(X509 *)sk_value(ctx->chain,i-1);
192 if (X509_NAME_cmp(X509_get_subject_name(x),X509_get_issuer_name(x))
193 == 0)
194 {
195 /* we have a self signed certificate */
196 if (sk_num(ctx->chain) == 1)
197 {
198 ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
199 ctx->current_cert=x;
200 ctx->error_depth=i-1;
201 ok=cb(0,ctx);
202 if (!ok) goto end;
203 }
204 else
205 {
206 /* worry more about this one elsewhere */
207 chain_ss=(X509 *)sk_pop(ctx->chain);
208 ctx->last_untrusted--;
209 num--;
210 x=(X509 *)sk_value(ctx->chain,num-1);
211 }
212 }
213
214 /* We now lookup certs from the certificate store */
215 for (;;)
216 {
217 /* If we have enough, we break */
218 if (depth <= num) break;
219
220 /* If we are self signed, we break */
221 xn=X509_get_issuer_name(x);
222 if (X509_NAME_cmp(X509_get_subject_name(x),xn) == 0)
223 break;
224
225 ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj);
226 if (ok != X509_LU_X509)
227 {
228 if (ok == X509_LU_RETRY)
229 {
230 X509_OBJECT_free_contents(&obj);
231 X509err(X509_F_X509_VERIFY_CERT,X509_R_SHOULD_RETRY);
232 return(ok);
233 }
234 else if (ok != X509_LU_FAIL)
235 {
236 X509_OBJECT_free_contents(&obj);
237 /* not good :-(, break anyway */
238 return(ok);
239 }
240 break;
241 }
242 x=obj.data.x509;
243 if (!sk_push(ctx->chain,(char *)obj.data.x509))
244 {
245 X509_OBJECT_free_contents(&obj);
246 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
247 return(0);
248 }
249 num++;
250 }
251
252 /* we now have our chain, lets check it... */
253 xn=X509_get_issuer_name(x);
254 if (X509_NAME_cmp(X509_get_subject_name(x),xn) != 0)
255 {
256 if ((chain_ss == NULL) || (X509_NAME_cmp(X509_get_subject_name(chain_ss),xn) != 0))
257 {
258 if (ctx->last_untrusted >= num)
259 ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
260 else
261 ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
262 ctx->current_cert=x;
263 }
264 else
265 {
266
267 sk_push(ctx->chain,(char *)chain_ss);
268 num++;
269 ctx->last_untrusted=num;
270 ctx->current_cert=chain_ss;
271 ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
272 chain_ss=NULL;
273 }
274
275 ctx->error_depth=num-1;
276 ok=cb(0,ctx);
277 if (!ok) goto end;
278 }
279
280 /* We may as well copy down any DSA parameters that are required */
281 X509_get_pubkey_parameters(NULL,ctx->chain);
282
283 /* At this point, we have a chain and just need to verify it */
284 if (ctx->ctx->verify != NULL)
285 ok=ctx->ctx->verify(ctx);
286 else
287 ok=internal_verify(ctx);
288end:
289 if (sktmp != NULL) sk_free(sktmp);
290 if (chain_ss != NULL) X509_free(chain_ss);
291 return(ok);
292 }
293
294static int internal_verify(ctx)
295X509_STORE_CTX *ctx;
296 {
297 int i,ok=0,n;
298 X509 *xs,*xi;
299 EVP_PKEY *pkey=NULL;
300 int (*cb)();
301
302 cb=ctx->ctx->verify_cb;
303 if (cb == NULL) cb=null_callback;
304
305 n=sk_num(ctx->chain);
306 ctx->error_depth=n-1;
307 n--;
308 xi=(X509 *)sk_value(ctx->chain,n);
309 if (X509_NAME_cmp(X509_get_subject_name(xi),
310 X509_get_issuer_name(xi)) == 0)
311 xs=xi;
312 else
313 {
314 if (n <= 0)
315 {
316 ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
317 ctx->current_cert=xi;
318 ok=cb(0,ctx);
319 goto end;
320 }
321 else
322 {
323 n--;
324 ctx->error_depth=n;
325 xs=(X509 *)sk_value(ctx->chain,n);
326 }
327 }
328
329/* ctx->error=0; not needed */
330 while (n >= 0)
331 {
332 ctx->error_depth=n;
333 if (!xs->valid)
334 {
335 if ((pkey=X509_get_pubkey(xi)) == NULL)
336 {
337 ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
338 ctx->current_cert=xi;
339 ok=(*cb)(0,ctx);
340 if (!ok) goto end;
341 }
342 if (X509_verify(xs,pkey) <= 0)
343 {
344 ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
345 ctx->current_cert=xs;
346 ok=(*cb)(0,ctx);
347 if (!ok) goto end;
348 }
349 pkey=NULL;
350
351 i=X509_cmp_current_time(X509_get_notBefore(xs));
352 if (i == 0)
353 {
354 ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
355 ctx->current_cert=xs;
356 ok=(*cb)(0,ctx);
357 if (!ok) goto end;
358 }
359 if (i > 0)
360 {
361 ctx->error=X509_V_ERR_CERT_NOT_YET_VALID;
362 ctx->current_cert=xs;
363 ok=(*cb)(0,ctx);
364 if (!ok) goto end;
365 }
366 xs->valid=1;
367 }
368
369 i=X509_cmp_current_time(X509_get_notAfter(xs));
370 if (i == 0)
371 {
372 ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
373 ctx->current_cert=xs;
374 ok=(*cb)(0,ctx);
375 if (!ok) goto end;
376 }
377
378 if (i < 0)
379 {
380 ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
381 ctx->current_cert=xs;
382 ok=(*cb)(0,ctx);
383 if (!ok) goto end;
384 }
385
386 /* CRL CHECK */
387
388 /* The last error (if any) is still in the error value */
389 ctx->current_cert=xs;
390 ok=(*cb)(1,ctx);
391 if (!ok) goto end;
392
393 n--;
394 if (n >= 0)
395 {
396 xi=xs;
397 xs=(X509 *)sk_value(ctx->chain,n);
398 }
399 }
400 ok=1;
401end:
402 return(ok);
403 }
404
405int X509_cmp_current_time(ctm)
406ASN1_UTCTIME *ctm;
407 {
408 char *str;
409 ASN1_UTCTIME atm;
410 time_t offset;
411 char buff1[24],buff2[24],*p;
412 int i,j;
413
414 p=buff1;
415 i=ctm->length;
416 str=(char *)ctm->data;
417 if ((i < 11) || (i > 17)) return(0);
418 memcpy(p,str,10);
419 p+=10;
420 str+=10;
421
422 if ((*str == 'Z') || (*str == '-') || (*str == '+'))
423 { *(p++)='0'; *(p++)='0'; }
424 else { *(p++)= *(str++); *(p++)= *(str++); }
425 *(p++)='Z';
426 *(p++)='\0';
427
428 if (*str == 'Z')
429 offset=0;
430 else
431 {
432 if ((*str != '+') && (str[5] != '-'))
433 return(0);
434 offset=((str[1]-'0')*10+(str[2]-'0'))*60;
435 offset+=(str[3]-'0')*10+(str[4]-'0');
436 if (*str == '-')
437 offset=-offset;
438 }
439 atm.type=V_ASN1_UTCTIME;
440 atm.length=sizeof(buff2);
441 atm.data=(unsigned char *)buff2;
442
443 X509_gmtime_adj(&atm,-offset);
444
445 i=(buff1[0]-'0')*10+(buff1[1]-'0');
446 if (i < 70) i+=100;
447 j=(buff2[0]-'0')*10+(buff2[1]-'0');
448 if (j < 70) j+=100;
449
450 if (i < j) return (-1);
451 if (i > j) return (1);
452 i=strcmp(buff1,buff2);
453 if (i == 0) /* wait a second then return younger :-) */
454 return(-1);
455 else
456 return(i);
457 }
458
459ASN1_UTCTIME *X509_gmtime_adj(s, adj)
460ASN1_UTCTIME *s;
461long adj;
462 {
463 time_t t;
464
465 time(&t);
466 t+=adj;
467 return(ASN1_UTCTIME_set(s,t));
468 }
469
470int X509_get_pubkey_parameters(pkey,chain)
471EVP_PKEY *pkey;
472STACK *chain;
473 {
474 EVP_PKEY *ktmp=NULL,*ktmp2;
475 int i,j;
476
477 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return(1);
478
479 for (i=0; i<sk_num(chain); i++)
480 {
481 ktmp=X509_get_pubkey((X509 *)sk_value(chain,i));
482 if (ktmp == NULL)
483 {
484 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
485 return(0);
486 }
487 if (!EVP_PKEY_missing_parameters(ktmp))
488 break;
489 else
490 {
491 ktmp=NULL;
492 }
493 }
494 if (ktmp == NULL)
495 {
496 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
497 return(0);
498 }
499
500 /* first, populate the other certs */
501 for (j=i-1; j >= 0; j--)
502 {
503 ktmp2=X509_get_pubkey((X509 *)sk_value(chain,j));
504 EVP_PKEY_copy_parameters(ktmp2,ktmp);
505 }
506
507 if (pkey != NULL)
508 EVP_PKEY_copy_parameters(pkey,ktmp);
509 return(1);
510 }
511
512EVP_PKEY *X509_get_pubkey(x)
513X509 *x;
514 {
515 if ((x == NULL) || (x->cert_info == NULL))
516 return(NULL);
517 return(X509_PUBKEY_get(x->cert_info->key));
518 }
519
520int X509_check_private_key(x,k)
521X509 *x;
522EVP_PKEY *k;
523 {
524 EVP_PKEY *xk=NULL;
525 int ok=0;
526
527 xk=X509_get_pubkey(x);
528 if (xk->type != k->type) goto err;
529 switch (k->type)
530 {
531#ifndef NO_RSA
532 case EVP_PKEY_RSA:
533 if (BN_cmp(xk->pkey.rsa->n,k->pkey.rsa->n) != 0) goto err;
534 if (BN_cmp(xk->pkey.rsa->e,k->pkey.rsa->e) != 0) goto err;
535 break;
536#endif
537#ifndef NO_DSA
538 case EVP_PKEY_DSA:
539 if (BN_cmp(xk->pkey.dsa->pub_key,k->pkey.dsa->pub_key) != 0)
540 goto err;
541 break;
542#endif
543#ifndef NO_DH
544 case EVP_PKEY_DH:
545 /* No idea */
546 goto err;
547#endif
548 default:
549 goto err;
550 }
551
552 ok=1;
553err:
554 return(ok);
555 }
556
557int X509_STORE_add_cert(ctx,x)
558X509_STORE *ctx;
559X509 *x;
560 {
561 X509_OBJECT *obj,*r;
562 int ret=1;
563
564 if (x == NULL) return(0);
565 obj=(X509_OBJECT *)Malloc(sizeof(X509_OBJECT));
566 if (obj == NULL)
567 {
568 X509err(X509_F_X509_STORE_ADD_CERT,ERR_R_MALLOC_FAILURE);
569 return(0);
570 }
571 obj->type=X509_LU_X509;
572 obj->data.x509=x;
573
574 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
575
576 X509_OBJECT_up_ref_count(obj);
577
578 r=(X509_OBJECT *)lh_insert(ctx->certs,(char *)obj);
579 if (r != NULL)
580 { /* oops, put it back */
581 lh_delete(ctx->certs,(char *)obj);
582 X509_OBJECT_free_contents(obj);
583 Free(obj);
584 lh_insert(ctx->certs,(char *)r);
585 X509err(X509_F_X509_STORE_ADD_CERT,X509_R_CERT_ALREADY_IN_HASH_TABLE);
586 ret=0;
587 }
588
589 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
590
591 return(ret);
592 }
593
594int X509_STORE_add_crl(ctx,x)
595X509_STORE *ctx;
596X509_CRL *x;
597 {
598 X509_OBJECT *obj,*r;
599 int ret=1;
600
601 if (x == NULL) return(0);
602 obj=(X509_OBJECT *)Malloc(sizeof(X509_OBJECT));
603 if (obj == NULL)
604 {
605 X509err(X509_F_X509_STORE_ADD_CRL,ERR_R_MALLOC_FAILURE);
606 return(0);
607 }
608 obj->type=X509_LU_CRL;
609 obj->data.crl=x;
610
611 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
612
613 X509_OBJECT_up_ref_count(obj);
614
615 r=(X509_OBJECT *)lh_insert(ctx->certs,(char *)obj);
616 if (r != NULL)
617 { /* oops, put it back */
618 lh_delete(ctx->certs,(char *)obj);
619 X509_OBJECT_free_contents(obj);
620 Free(obj);
621 lh_insert(ctx->certs,(char *)r);
622 X509err(X509_F_X509_STORE_ADD_CRL,X509_R_CERT_ALREADY_IN_HASH_TABLE);
623 ret=0;
624 }
625
626 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
627
628 return(ret);
629 }
630
631int X509_STORE_CTX_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
632long argl;
633char *argp;
634int (*new_func)();
635int (*dup_func)();
636void (*free_func)();
637 {
638 x509_store_ctx_num++;
639 return(CRYPTO_get_ex_new_index(x509_store_ctx_num-1,
640 &x509_store_ctx_method,
641 argl,argp,new_func,dup_func,free_func));
642 }
643
644int X509_STORE_CTX_set_ex_data(ctx,idx,data)
645X509_STORE_CTX *ctx;
646int idx;
647char *data;
648 {
649 return(CRYPTO_set_ex_data(&ctx->ex_data,idx,data));
650 }
651
652char *X509_STORE_CTX_get_ex_data(ctx,idx)
653X509_STORE_CTX *ctx;
654int idx;
655 {
656 return(CRYPTO_get_ex_data(&ctx->ex_data,idx));
657 }
658
659int X509_STORE_CTX_get_error(ctx)
660X509_STORE_CTX *ctx;
661 {
662 return(ctx->error);
663 }
664
665void X509_STORE_CTX_set_error(ctx,err)
666X509_STORE_CTX *ctx;
667int err;
668 {
669 ctx->error=err;
670 }
671
672int X509_STORE_CTX_get_error_depth(ctx)
673X509_STORE_CTX *ctx;
674 {
675 return(ctx->error_depth);
676 }
677
678X509 *X509_STORE_CTX_get_current_cert(ctx)
679X509_STORE_CTX *ctx;
680 {
681 return(ctx->current_cert);
682 }
683
684STACK *X509_STORE_CTX_get_chain(ctx)
685X509_STORE_CTX *ctx;
686 {
687 return(ctx->chain);
688 }
689
690void X509_STORE_CTX_set_cert(ctx,x)
691X509_STORE_CTX *ctx;
692X509 *x;
693 {
694 ctx->cert=x;
695 }
696
697void X509_STORE_CTX_set_chain(ctx,sk)
698X509_STORE_CTX *ctx;
699STACK *sk;
700 {
701 ctx->untrusted=sk;
702 }
703
704
diff --git a/src/lib/libcrypto/x509/x509_vfy.h b/src/lib/libcrypto/x509/x509_vfy.h
new file mode 100644
index 0000000000..dfc060f899
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509_vfy.h
@@ -0,0 +1,378 @@
1/* crypto/x509/x509_vfy.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_X509_VFY_H
60#define HEADER_X509_VFY_H
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include "bio.h"
67#include "crypto.h"
68
69/* Outer object */
70typedef struct x509_hash_dir_st
71 {
72 int num_dirs;
73 char **dirs;
74 int *dirs_type;
75 int num_dirs_alloced;
76 } X509_HASH_DIR_CTX;
77
78typedef struct x509_file_st
79 {
80 int num_paths; /* number of paths to files or directories */
81 int num_alloced;
82 char **paths; /* the list of paths or directories */
83 int *path_type;
84 } X509_CERT_FILE_CTX;
85
86/*******************************/
87/*
88SSL_CTX -> X509_STORE
89 -> X509_LOOKUP
90 ->X509_LOOKUP_METHOD
91 -> X509_LOOKUP
92 ->X509_LOOKUP_METHOD
93
94SSL -> X509_STORE_CTX
95 ->X509_STORE
96
97The X509_STORE holds the tables etc for verification stuff.
98A X509_STORE_CTX is used while validating a single certificate.
99The X509_STORE has X509_LOOKUPs for looking up certs.
100The X509_STORE then calls a function to actually verify the
101certificate chain.
102*/
103
104#define X509_LU_RETRY -1
105#define X509_LU_FAIL 0
106#define X509_LU_X509 1
107#define X509_LU_CRL 2
108#define X509_LU_PKEY 3
109
110typedef struct x509_object_st
111 {
112 /* one of the above types */
113 int type;
114 union {
115 char *ptr;
116 X509 *x509;
117 X509_CRL *crl;
118 EVP_PKEY *pkey;
119 } data;
120 } X509_OBJECT;
121
122/* This is a static that defines the function interface */
123typedef struct x509_lookup_method_st
124 {
125 char *name;
126 int (*new_item)();
127 void (*free)();
128 int (*init)(/* meth, char ** */);
129 int (*shutdown)( /* meth, char ** */);
130 int (*ctrl)( /* meth, char **, int cmd, char *argp, int argi */);
131 int (*get_by_subject)(/* meth, char **, XNAME *, X509 **ret */);
132 int (*get_by_issuer_serial)();
133 int (*get_by_fingerprint)();
134 int (*get_by_alias)();
135 } X509_LOOKUP_METHOD;
136
137/* This is used to hold everything. It is used for all certificate
138 * validation. Once we have a certificate chain, the 'verify'
139 * function is then called to actually check the cert chain. */
140typedef struct x509_store_st
141 {
142 /* The following is a cache of trusted certs */
143 int cache; /* if true, stash any hits */
144#ifdef HEADER_LHASH_H
145 LHASH *certs; /* cached certs; */
146#else
147 char *certs;
148#endif
149
150 /* These are external lookup methods */
151 STACK *get_cert_methods;/* X509_LOOKUP */
152 int (*verify)(); /* called to verify a certificate */
153 int (*verify_cb)(); /* error callback */
154
155 CRYPTO_EX_DATA ex_data;
156 int references;
157 int depth; /* how deep to look */
158 } X509_STORE;
159
160#define X509_STORE_set_depth(ctx,d) ((ctx)->depth=(d))
161
162#define X509_STORE_set_verify_cb_func(ctx,func) ((ctx)->verify_cb=(func))
163#define X509_STORE_set_verify_func(ctx,func) ((ctx)->verify=(func))
164
165/* This is the functions plus an instance of the local variables. */
166typedef struct x509_lookup_st
167 {
168 int init; /* have we been started */
169 int skip; /* don't use us. */
170 X509_LOOKUP_METHOD *method; /* the functions */
171 char *method_data; /* method data */
172
173 X509_STORE *store_ctx; /* who owns us */
174 } X509_LOOKUP;
175
176/* This is a temporary used when processing cert chains. Since the
177 * gathering of the cert chain can take some time (and have to be
178 * 'retried', this needs to be kept and passed around. */
179typedef struct x509_store_state_st
180 {
181 X509_STORE *ctx;
182 int current_method; /* used when looking up certs */
183
184 /* The following are set by the caller */
185 X509 *cert; /* The cert to check */
186 STACK *untrusted; /* chain of X509s - untrusted - passed in */
187
188 /* The following is built up */
189 int depth; /* how far to go looking up certs */
190 int valid; /* if 0, rebuild chain */
191 int last_untrusted; /* index of last untrusted cert */
192 STACK *chain; /* chain of X509s - built up and trusted */
193
194 /* When something goes wrong, this is why */
195 int error_depth;
196 int error;
197 X509 *current_cert;
198
199 CRYPTO_EX_DATA ex_data;
200 } X509_STORE_CTX;
201
202#define X509_STORE_CTX_set_app_data(ctx,data) \
203 X509_STORE_CTX_set_ex_data(ctx,0,data)
204#define X509_STORE_CTX_get_app_data(ctx) \
205 X509_STORE_CTX_get_ex_data(ctx,0)
206
207#define X509_L_FILE_LOAD 1
208#define X509_L_ADD_DIR 2
209
210X509_LOOKUP_METHOD *X509_LOOKUP_file();
211#define X509_LOOKUP_load_file(x,name,type) \
212 X509_LOOKUP_ctrl((x),X509_L_FILE_LOAD,(name),(long)(type),NULL)
213
214X509_LOOKUP_METHOD *X509_LOOKUP_dir();
215#define X509_LOOKUP_add_dir(x,name,type) \
216 X509_LOOKUP_ctrl((x),X509_L_ADD_DIR,(name),(long)(type),NULL)
217
218#define X509_V_OK 0
219
220#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2
221#define X509_V_ERR_UNABLE_TO_GET_CRL 3
222#define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4
223#define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5
224#define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6
225#define X509_V_ERR_CERT_SIGNATURE_FAILURE 7
226#define X509_V_ERR_CRL_SIGNATURE_FAILURE 8
227#define X509_V_ERR_CERT_NOT_YET_VALID 9
228#define X509_V_ERR_CERT_HAS_EXPIRED 10
229#define X509_V_ERR_CRL_NOT_YET_VALID 11
230#define X509_V_ERR_CRL_HAS_EXPIRED 12
231#define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13
232#define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14
233#define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD 15
234#define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD 16
235#define X509_V_ERR_OUT_OF_MEM 17
236#define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18
237#define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19
238#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20
239#define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21
240#define X509_V_ERR_CERT_CHAIN_TOO_LONG 22
241#define X509_V_ERR_CERT_REVOKED 23
242
243/* The application is not happy */
244#define X509_V_ERR_APPLICATION_VERIFICATION 50
245
246#ifndef NOPROTO
247#ifdef HEADER_LHASH_H
248X509_OBJECT *X509_OBJECT_retrive_by_subject(LHASH *h,int type,X509_NAME *name);
249#endif
250void X509_OBJECT_up_ref_count(X509_OBJECT *a);
251void X509_OBJECT_free_contents(X509_OBJECT *a);
252X509_STORE *X509_STORE_new(void );
253void X509_STORE_free(X509_STORE *v);
254
255void X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store,
256 X509 *x509, STACK *chain);
257void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);
258
259X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m);
260
261X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void);
262X509_LOOKUP_METHOD *X509_LOOKUP_file(void);
263
264int X509_STORE_add_cert(X509_STORE *ctx, X509 *x);
265int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x);
266
267int X509_STORE_get_by_subject(X509_STORE_CTX *vs,int type,X509_NAME *name,
268 X509_OBJECT *ret);
269
270int X509_LOOKUP_ctrl(X509_LOOKUP *ctx,int cmd,char *argc,long argl,char **ret);
271
272#ifndef NO_STDIO
273int X509_load_cert_file(X509_LOOKUP *ctx, char *file, int type);
274int X509_load_crl_file(X509_LOOKUP *ctx, char *file, int type);
275#endif
276
277void X509v3_cleanup_extensions(void );
278int X509v3_add_extension(X509_EXTENSION_METHOD *x);
279int X509v3_add_netscape_extensions(void );
280int X509v3_add_standard_extensions(void );
281
282X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method);
283void X509_LOOKUP_free(X509_LOOKUP *ctx);
284int X509_LOOKUP_init(X509_LOOKUP *ctx);
285int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
286 X509_OBJECT *ret);
287int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, int type, X509_NAME *name,
288 ASN1_INTEGER *serial, X509_OBJECT *ret);
289int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, int type,
290 unsigned char *bytes, int len, X509_OBJECT *ret);
291int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, int type, char *str,
292 int len, X509_OBJECT *ret);
293int X509_LOOKUP_shutdown(X509_LOOKUP *ctx);
294
295#ifndef NO_STDIO
296int X509_STORE_load_locations (X509_STORE *ctx,
297 char *file, char *dir);
298int X509_STORE_set_default_paths(X509_STORE *ctx);
299#endif
300
301int X509_STORE_CTX_get_ex_new_index(long argl, char *argp, int (*new_func)(),
302 int (*dup_func)(), void (*free_func)());
303int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx,int idx,char *data);
304char * X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx,int idx);
305int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
306void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx,int s);
307int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx);
308X509 * X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx);
309STACK * X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx);
310void X509_STORE_CTX_set_cert(X509_STORE_CTX *c,X509 *x);
311void X509_STORE_CTX_set_chain(X509_STORE_CTX *c,STACK /* X509 */ *sk);
312
313#else
314
315#ifdef HEADER_LHASH_H
316X509_OBJECT *X509_OBJECT_retrive_by_subject();
317#endif
318void X509_OBJECT_up_ref_count();
319void X509_OBJECT_free_contents();
320X509_STORE *X509_STORE_new();
321void X509_STORE_free();
322
323void X509_STORE_CTX_init();
324void X509_STORE_CTX_cleanup();
325
326X509_LOOKUP *X509_STORE_add_lookup();
327
328X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir();
329X509_LOOKUP_METHOD *X509_LOOKUP_file();
330
331int X509_STORE_add_cert();
332int X509_STORE_add_crl();
333
334int X509_STORE_get_by_subject();
335
336int X509_LOOKUP_ctrl();
337
338#ifndef NO_STDIO
339int X509_load_cert_file();
340int X509_load_crl_file();
341#endif
342
343void X509v3_cleanup_extensions();
344int X509v3_add_extension();
345int X509v3_add_netscape_extensions();
346int X509v3_add_standard_extensions();
347
348X509_LOOKUP *X509_LOOKUP_new();
349void X509_LOOKUP_free();
350int X509_LOOKUP_init();
351int X509_LOOKUP_by_subject();
352int X509_LOOKUP_by_issuer_serial();
353int X509_LOOKUP_by_fingerprint();
354int X509_LOOKUP_by_alias();
355int X509_LOOKUP_shutdown();
356
357#ifndef NO_STDIO
358int X509_STORE_load_locations ();
359int X509_STORE_set_default_paths();
360#endif
361
362int X509_STORE_CTX_set_ex_data();
363char * X509_STORE_CTX_get_ex_data();
364int X509_STORE_CTX_get_error();
365void X509_STORE_CTX_set_error();
366int X509_STORE_CTX_get_error_depth();
367X509 * X509_STORE_CTX_get_current_cert();
368STACK * X509_STORE_CTX_get_chain();
369void X509_STORE_CTX_set_cert();
370void X509_STORE_CTX_set_chain();
371
372#endif
373
374#ifdef __cplusplus
375}
376#endif
377#endif
378
diff --git a/src/lib/libcrypto/x509/x509name.c b/src/lib/libcrypto/x509/x509name.c
new file mode 100644
index 0000000000..650e71b1b5
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509name.c
@@ -0,0 +1,358 @@
1/* crypto/x509/x509name.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "stack.h"
61#include "cryptlib.h"
62#include "asn1.h"
63#include "objects.h"
64#include "evp.h"
65#include "x509.h"
66
67int X509_NAME_get_text_by_NID(name,nid,buf,len)
68X509_NAME *name;
69int nid;
70char *buf;
71int len;
72 {
73 ASN1_OBJECT *obj;
74
75 obj=OBJ_nid2obj(nid);
76 if (obj == NULL) return(-1);
77 return(X509_NAME_get_text_by_OBJ(name,obj,buf,len));
78 }
79
80int X509_NAME_get_text_by_OBJ(name,obj,buf,len)
81X509_NAME *name;
82ASN1_OBJECT *obj;
83char *buf;
84int len;
85 {
86 int i;
87 ASN1_STRING *data;
88
89 i=X509_NAME_get_index_by_OBJ(name,obj,-1);
90 if (i < 0) return(-1);
91 data=X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name,i));
92 i=(data->length > (len-1))?(len-1):data->length;
93 if (buf == NULL) return(data->length);
94 memcpy(buf,data->data,i);
95 buf[i]='\0';
96 return(i);
97 }
98
99int X509_NAME_entry_count(name)
100X509_NAME *name;
101 {
102 if (name == NULL) return(0);
103 return(sk_num(name->entries));
104 }
105
106int X509_NAME_get_index_by_NID(name,nid,lastpos)
107X509_NAME *name;
108int nid;
109int lastpos;
110 {
111 ASN1_OBJECT *obj;
112
113 obj=OBJ_nid2obj(nid);
114 if (obj == NULL) return(-2);
115 return(X509_NAME_get_index_by_OBJ(name,obj,lastpos));
116 }
117
118/* NOTE: you should be passsing -1, not 0 as lastpos */
119int X509_NAME_get_index_by_OBJ(name,obj,lastpos)
120X509_NAME *name;
121ASN1_OBJECT *obj;
122int lastpos;
123 {
124 int n;
125 X509_NAME_ENTRY *ne;
126 STACK *sk;
127
128 if (name == NULL) return(-1);
129 if (lastpos < 0)
130 lastpos= -1;
131 sk=name->entries;
132 n=sk_num(sk);
133 for (lastpos++; lastpos < n; lastpos++)
134 {
135 ne=(X509_NAME_ENTRY *)sk_value(sk,lastpos);
136 if (OBJ_cmp(ne->object,obj) == 0)
137 return(lastpos);
138 }
139 return(-1);
140 }
141
142X509_NAME_ENTRY *X509_NAME_get_entry(name,loc)
143X509_NAME *name;
144int loc;
145 {
146 if ( (name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0))
147 return(NULL);
148 else
149 return((X509_NAME_ENTRY *)sk_value(name->entries,loc));
150 }
151
152X509_NAME_ENTRY *X509_NAME_delete_entry(name,loc)
153X509_NAME *name;
154int loc;
155 {
156 X509_NAME_ENTRY *ret;
157 int i,j,n,set_prev,set_next;
158 STACK *sk;
159
160 if ((name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0))
161 return(NULL);
162 sk=name->entries;
163 ret=(X509_NAME_ENTRY *)sk_delete(sk,loc);
164 n=sk_num(sk);
165 name->modified=1;
166 if (loc == n) return(ret);
167
168 /* else we need to fixup the set field */
169 if (loc != 0)
170 set_prev=((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set;
171 else
172 set_prev=ret->set-1;
173 set_next=((X509_NAME_ENTRY *)sk_value(sk,loc))->set;
174
175 /* set_prev is the previous set
176 * set is the current set
177 * set_next is the following
178 * prev 1 1 1 1 1 1 1 1
179 * set 1 1 2 2
180 * next 1 1 2 2 2 2 3 2
181 * so basically only if prev and next differ by 2, then
182 * re-number down by 1 */
183 if (set_prev+1 < set_next)
184 {
185 j=set_next-set_prev-1;
186 for (i=loc; i<n; i++)
187 ((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set-=j;
188 }
189 return(ret);
190 }
191
192/* if set is -1, append to previous set, 0 'a new one', and 1,
193 * prepend to the guy we are about to stomp on. */
194int X509_NAME_add_entry(name,ne,loc,set)
195X509_NAME *name;
196X509_NAME_ENTRY *ne;
197int loc;
198int set;
199 {
200 X509_NAME_ENTRY *new_name=NULL;
201 int n,i,inc;
202 STACK *sk;
203
204 if (name == NULL) return(0);
205 sk=name->entries;
206 n=sk_num(sk);
207 if (loc > n) loc=n;
208 else if (loc < 0) loc=n;
209
210 name->modified=1;
211
212 if (set == -1)
213 {
214 if (loc == 0)
215 {
216 set=0;
217 inc=1;
218 }
219 else
220 {
221 set=((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set;
222 inc=0;
223 }
224 }
225 else /* if (set >= 0) */
226 {
227 if (loc >= n)
228 {
229 if (loc != 0)
230 set=((X509_NAME_ENTRY *)
231 sk_value(sk,loc-1))->set+1;
232 else
233 set=0;
234 }
235 else
236 set=((X509_NAME_ENTRY *)sk_value(sk,loc))->set;
237 inc=(set == 0)?1:0;
238 }
239
240 if ((new_name=X509_NAME_ENTRY_dup(ne)) == NULL)
241 goto err;
242 new_name->set=set;
243 if (!sk_insert(sk,(char *)new_name,loc))
244 {
245 X509err(X509_F_X509_NAME_ADD_ENTRY,ERR_R_MALLOC_FAILURE);
246 goto err;
247 }
248 if (inc)
249 {
250 n=sk_num(sk);
251 for (i=loc+1; i<n; i++)
252 ((X509_NAME_ENTRY *)sk_value(sk,i-1))->set+=1;
253 }
254 return(1);
255err:
256 if (new_name != NULL)
257 X509_NAME_ENTRY_free(ne);
258 return(0);
259 }
260
261X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(ne,nid,type,bytes,len)
262X509_NAME_ENTRY **ne;
263int nid;
264int type;
265unsigned char *bytes;
266int len;
267 {
268 ASN1_OBJECT *obj;
269
270 obj=OBJ_nid2obj(nid);
271 if (obj == NULL)
272 {
273 X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID,X509_R_UNKNOWN_NID);
274 return(NULL);
275 }
276 return(X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len));
277 }
278
279X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len)
280X509_NAME_ENTRY **ne;
281ASN1_OBJECT *obj;
282int type;
283unsigned char *bytes;
284int len;
285 {
286 X509_NAME_ENTRY *ret;
287
288 if ((ne == NULL) || (*ne == NULL))
289 {
290 if ((ret=X509_NAME_ENTRY_new()) == NULL)
291 return(NULL);
292 }
293 else
294 ret= *ne;
295
296 if (!X509_NAME_ENTRY_set_object(ret,obj))
297 goto err;
298 if (!X509_NAME_ENTRY_set_data(ret,type,bytes,len))
299 goto err;
300
301 if ((ne != NULL) && (*ne == NULL)) *ne=ret;
302 return(ret);
303err:
304 if ((ne == NULL) || (ret != *ne))
305 X509_NAME_ENTRY_free(ret);
306 return(NULL);
307 }
308
309int X509_NAME_ENTRY_set_object(ne,obj)
310X509_NAME_ENTRY *ne;
311ASN1_OBJECT *obj;
312 {
313 if ((ne == NULL) || (obj == NULL))
314 {
315 X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,ERR_R_PASSED_NULL_PARAMETER);
316 return(0);
317 }
318 ASN1_OBJECT_free(ne->object);
319 ne->object=OBJ_dup(obj);
320 return((ne->object == NULL)?0:1);
321 }
322
323int X509_NAME_ENTRY_set_data(ne,type,bytes,len)
324X509_NAME_ENTRY *ne;
325int type;
326unsigned char *bytes;
327int len;
328 {
329 int i;
330
331 if ((ne == NULL) || ((bytes == NULL) && (len != 0))) return(0);
332 if (len < 0) len=strlen((char *)bytes);
333 i=ASN1_STRING_set(ne->value,bytes,len);
334 if (!i) return(0);
335 if (type != V_ASN1_UNDEF)
336 {
337 if (type == V_ASN1_APP_CHOOSE)
338 ne->value->type=ASN1_PRINTABLE_type(bytes,len);
339 else
340 ne->value->type=type;
341 }
342 return(1);
343 }
344
345ASN1_OBJECT *X509_NAME_ENTRY_get_object(ne)
346X509_NAME_ENTRY *ne;
347 {
348 if (ne == NULL) return(NULL);
349 return(ne->object);
350 }
351
352ASN1_STRING *X509_NAME_ENTRY_get_data(ne)
353X509_NAME_ENTRY *ne;
354 {
355 if (ne == NULL) return(NULL);
356 return(ne->value);
357 }
358
diff --git a/src/lib/libcrypto/x509/x509rset.c b/src/lib/libcrypto/x509/x509rset.c
new file mode 100644
index 0000000000..323b25470a
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509rset.c
@@ -0,0 +1,89 @@
1/* crypto/x509/x509rset.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1.h"
62#include "objects.h"
63#include "evp.h"
64#include "x509.h"
65
66int X509_REQ_set_version(x,version)
67X509_REQ *x;
68long version;
69 {
70 if (x == NULL) return(0);
71 return(ASN1_INTEGER_set(x->req_info->version,version));
72 }
73
74int X509_REQ_set_subject_name(x,name)
75X509_REQ *x;
76X509_NAME *name;
77 {
78 if ((x == NULL) || (x->req_info == NULL)) return(0);
79 return(X509_NAME_set(&x->req_info->subject,name));
80 }
81
82int X509_REQ_set_pubkey(x,pkey)
83X509_REQ *x;
84EVP_PKEY *pkey;
85 {
86 if ((x == NULL) || (x->req_info == NULL)) return(0);
87 return(X509_PUBKEY_set(&x->req_info->pubkey,pkey));
88 }
89
diff --git a/src/lib/libcrypto/x509/x509type.c b/src/lib/libcrypto/x509/x509type.c
new file mode 100644
index 0000000000..42c23bcfca
--- /dev/null
+++ b/src/lib/libcrypto/x509/x509type.c
@@ -0,0 +1,115 @@
1/* crypto/x509/x509type.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "evp.h"
62#include "objects.h"
63#include "x509.h"
64
65int X509_certificate_type(x,pkey)
66X509 *x;
67EVP_PKEY *pkey;
68 {
69 EVP_PKEY *pk;
70 int ret=0,i;
71
72 if (x == NULL) return(0);
73
74 if (pkey == NULL)
75 pk=X509_get_pubkey(x);
76 else
77 pk=pkey;
78
79 if (pk == NULL) return(0);
80
81 switch (pk->type)
82 {
83 case EVP_PKEY_RSA:
84 ret=EVP_PK_RSA|EVP_PKT_SIGN;
85/* if (!sign only extension) */
86 ret|=EVP_PKT_ENC;
87 break;
88 case EVP_PKEY_DSA:
89 ret=EVP_PK_DSA|EVP_PKT_SIGN;
90 break;
91 case EVP_PKEY_DH:
92 ret=EVP_PK_DH|EVP_PKT_EXCH;
93 break;
94 default:
95 break;
96 }
97
98 i=X509_get_signature_type(x);
99 switch (i)
100 {
101 case EVP_PKEY_RSA:
102 ret|=EVP_PKS_RSA;
103 break;
104 case EVP_PKS_DSA:
105 ret|=EVP_PKS_DSA;
106 break;
107 default:
108 break;
109 }
110
111 if (EVP_PKEY_size(pkey) <= 512)
112 ret|=EVP_PKT_EXP;
113 return(ret);
114 }
115
diff --git a/src/lib/libcrypto/x509/x_all.c b/src/lib/libcrypto/x509/x_all.c
new file mode 100644
index 0000000000..b7dde23e9a
--- /dev/null
+++ b/src/lib/libcrypto/x509/x_all.c
@@ -0,0 +1,465 @@
1/* crypto/x509/x_all.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#undef SSLEAY_MACROS
61#include "stack.h"
62#include "cryptlib.h"
63#include "buffer.h"
64#include "asn1.h"
65#include "evp.h"
66#include "x509.h"
67
68int X509_verify(a,r)
69X509 *a;
70EVP_PKEY *r;
71 {
72 return(ASN1_verify((int (*)())i2d_X509_CINF,a->sig_alg,
73 a->signature,(char *)a->cert_info,r));
74 }
75
76int X509_REQ_verify(a,r)
77X509_REQ *a;
78EVP_PKEY *r;
79 {
80 return( ASN1_verify((int (*)())i2d_X509_REQ_INFO,
81 a->sig_alg,a->signature,(char *)a->req_info,r));
82 }
83
84int X509_CRL_verify(a,r)
85X509_CRL *a;
86EVP_PKEY *r;
87 {
88 return(ASN1_verify((int (*)())i2d_X509_CRL_INFO,
89 a->sig_alg, a->signature,(char *)a->crl,r));
90 }
91
92int NETSCAPE_SPKI_verify(a,r)
93NETSCAPE_SPKI *a;
94EVP_PKEY *r;
95 {
96 return(ASN1_verify((int (*)())i2d_NETSCAPE_SPKAC,
97 a->sig_algor,a->signature, (char *)a->spkac,r));
98 }
99
100int X509_sign(x,pkey,md)
101X509 *x;
102EVP_PKEY *pkey;
103EVP_MD *md;
104 {
105 return(ASN1_sign((int (*)())i2d_X509_CINF, x->cert_info->signature,
106 x->sig_alg, x->signature, (char *)x->cert_info,pkey,md));
107 }
108
109int X509_REQ_sign(x,pkey,md)
110X509_REQ *x;
111EVP_PKEY *pkey;
112EVP_MD *md;
113 {
114 return(ASN1_sign((int (*)())i2d_X509_REQ_INFO,x->sig_alg, NULL,
115 x->signature, (char *)x->req_info,pkey,md));
116 }
117
118int X509_CRL_sign(x,pkey,md)
119X509_CRL *x;
120EVP_PKEY *pkey;
121EVP_MD *md;
122 {
123 return(ASN1_sign((int (*)())i2d_X509_CRL_INFO,x->crl->sig_alg,
124 x->sig_alg, x->signature, (char *)x->crl,pkey,md));
125 }
126
127int NETSCAPE_SPKI_sign(x,pkey,md)
128NETSCAPE_SPKI *x;
129EVP_PKEY *pkey;
130EVP_MD *md;
131 {
132 return(ASN1_sign((int (*)())i2d_NETSCAPE_SPKAC, x->sig_algor,NULL,
133 x->signature, (char *)x->spkac,pkey,md));
134 }
135
136X509 *X509_dup(x509)
137X509 *x509;
138 {
139 return((X509 *)ASN1_dup((int (*)())i2d_X509,
140 (char *(*)())d2i_X509,(char *)x509));
141 }
142
143X509_EXTENSION *X509_EXTENSION_dup(ex)
144X509_EXTENSION *ex;
145 {
146 return((X509_EXTENSION *)ASN1_dup(
147 (int (*)())i2d_X509_EXTENSION,
148 (char *(*)())d2i_X509_EXTENSION,(char *)ex));
149 }
150
151#ifndef NO_FP_API
152X509 *d2i_X509_fp(fp,x509)
153FILE *fp;
154X509 *x509;
155 {
156 return((X509 *)ASN1_d2i_fp((char *(*)())X509_new,
157 (char *(*)())d2i_X509, (fp),(unsigned char **)(x509)));
158 }
159
160int i2d_X509_fp(fp,x509)
161FILE *fp;
162X509 *x509;
163 {
164 return(ASN1_i2d_fp(i2d_X509,fp,(unsigned char *)x509));
165 }
166#endif
167
168X509 *d2i_X509_bio(bp,x509)
169BIO *bp;
170X509 *x509;
171 {
172 return((X509 *)ASN1_d2i_bio((char *(*)())X509_new,
173 (char *(*)())d2i_X509, (bp),(unsigned char **)(x509)));
174 }
175
176int i2d_X509_bio(bp,x509)
177BIO *bp;
178X509 *x509;
179 {
180 return(ASN1_i2d_bio(i2d_X509,bp,(unsigned char *)x509));
181 }
182
183X509_CRL *X509_CRL_dup(crl)
184X509_CRL *crl;
185 {
186 return((X509_CRL *)ASN1_dup((int (*)())i2d_X509_CRL,
187 (char *(*)())d2i_X509_CRL,(char *)crl));
188 }
189
190#ifndef NO_FP_API
191X509_CRL *d2i_X509_CRL_fp(fp,crl)
192FILE *fp;
193X509_CRL *crl;
194 {
195 return((X509_CRL *)ASN1_d2i_fp((char *(*)())
196 X509_CRL_new,(char *(*)())d2i_X509_CRL, (fp),
197 (unsigned char **)(crl)));
198 }
199
200int i2d_X509_CRL_fp(fp,crl)
201FILE *fp;
202X509_CRL *crl;
203 {
204 return(ASN1_i2d_fp(i2d_X509_CRL,fp,(unsigned char *)crl));
205 }
206#endif
207
208X509_CRL *d2i_X509_CRL_bio(bp,crl)
209BIO *bp;
210X509_CRL *crl;
211 {
212 return((X509_CRL *)ASN1_d2i_bio((char *(*)())
213 X509_CRL_new,(char *(*)())d2i_X509_CRL, (bp),
214 (unsigned char **)(crl)));
215 }
216
217int i2d_X509_CRL_bio(bp,crl)
218BIO *bp;
219X509_CRL *crl;
220 {
221 return(ASN1_i2d_bio(i2d_X509_CRL,bp,(unsigned char *)crl));
222 }
223
224PKCS7 *PKCS7_dup(p7)
225PKCS7 *p7;
226 {
227 return((PKCS7 *)ASN1_dup((int (*)())i2d_PKCS7,
228 (char *(*)())d2i_PKCS7,(char *)p7));
229 }
230
231#ifndef NO_FP_API
232PKCS7 *d2i_PKCS7_fp(fp,p7)
233FILE *fp;
234PKCS7 *p7;
235 {
236 return((PKCS7 *)ASN1_d2i_fp((char *(*)())
237 PKCS7_new,(char *(*)())d2i_PKCS7, (fp),
238 (unsigned char **)(p7)));
239 }
240
241int i2d_PKCS7_fp(fp,p7)
242FILE *fp;
243PKCS7 *p7;
244 {
245 return(ASN1_i2d_fp(i2d_PKCS7,fp,(unsigned char *)p7));
246 }
247#endif
248
249PKCS7 *d2i_PKCS7_bio(bp,p7)
250BIO *bp;
251PKCS7 *p7;
252 {
253 return((PKCS7 *)ASN1_d2i_bio((char *(*)())
254 PKCS7_new,(char *(*)())d2i_PKCS7, (bp),
255 (unsigned char **)(p7)));
256 }
257
258int i2d_PKCS7_bio(bp,p7)
259BIO *bp;
260PKCS7 *p7;
261 {
262 return(ASN1_i2d_bio(i2d_PKCS7,bp,(unsigned char *)p7));
263 }
264
265X509_REQ *X509_REQ_dup(req)
266X509_REQ *req;
267 {
268 return((X509_REQ *)ASN1_dup((int (*)())i2d_X509_REQ,
269 (char *(*)())d2i_X509_REQ,(char *)req));
270 }
271
272#ifndef NO_FP_API
273X509_REQ *d2i_X509_REQ_fp(fp,req)
274FILE *fp;
275X509_REQ *req;
276 {
277 return((X509_REQ *)ASN1_d2i_fp((char *(*)())
278 X509_REQ_new, (char *(*)())d2i_X509_REQ, (fp),
279 (unsigned char **)(req)));
280 }
281
282int i2d_X509_REQ_fp(fp,req)
283FILE *fp;
284X509_REQ *req;
285 {
286 return(ASN1_i2d_fp(i2d_X509_REQ,fp,(unsigned char *)req));
287 }
288#endif
289
290X509_REQ *d2i_X509_REQ_bio(bp,req)
291BIO *bp;
292X509_REQ *req;
293 {
294 return((X509_REQ *)ASN1_d2i_bio((char *(*)())
295 X509_REQ_new, (char *(*)())d2i_X509_REQ, (bp),
296 (unsigned char **)(req)));
297 }
298
299int i2d_X509_REQ_bio(bp,req)
300BIO *bp;
301X509_REQ *req;
302 {
303 return(ASN1_i2d_bio(i2d_X509_REQ,bp,(unsigned char *)req));
304 }
305
306#ifndef NO_RSA
307RSA *RSAPublicKey_dup(rsa)
308RSA *rsa;
309 {
310 return((RSA *)ASN1_dup((int (*)())i2d_RSAPublicKey,
311 (char *(*)())d2i_RSAPublicKey,(char *)rsa));
312 }
313
314RSA *RSAPrivateKey_dup(rsa)
315RSA *rsa;
316 {
317 return((RSA *)ASN1_dup((int (*)())i2d_RSAPrivateKey,
318 (char *(*)())d2i_RSAPrivateKey,(char *)rsa));
319 }
320
321#ifndef NO_FP_API
322RSA *d2i_RSAPrivateKey_fp(fp,rsa)
323FILE *fp;
324RSA *rsa;
325 {
326 return((RSA *)ASN1_d2i_fp((char *(*)())
327 RSA_new,(char *(*)())d2i_RSAPrivateKey, (fp),
328 (unsigned char **)(rsa)));
329 }
330
331int i2d_RSAPrivateKey_fp(fp,rsa)
332FILE *fp;
333RSA *rsa;
334 {
335 return(ASN1_i2d_fp(i2d_RSAPrivateKey,fp,(unsigned char *)rsa));
336 }
337
338RSA *d2i_RSAPublicKey_fp(fp,rsa)
339FILE *fp;
340RSA *rsa;
341 {
342 return((RSA *)ASN1_d2i_fp((char *(*)())
343 RSA_new,(char *(*)())d2i_RSAPublicKey, (fp),
344 (unsigned char **)(rsa)));
345 }
346
347int i2d_RSAPublicKey_fp(fp,rsa)
348FILE *fp;
349RSA *rsa;
350 {
351 return(ASN1_i2d_fp(i2d_RSAPublicKey,fp,(unsigned char *)rsa));
352 }
353#endif
354
355RSA *d2i_RSAPrivateKey_bio(bp,rsa)
356BIO *bp;
357RSA *rsa;
358 {
359 return((RSA *)ASN1_d2i_bio((char *(*)())
360 RSA_new,(char *(*)())d2i_RSAPrivateKey, (bp),
361 (unsigned char **)(rsa)));
362 }
363
364int i2d_RSAPrivateKey_bio(bp,rsa)
365BIO *bp;
366RSA *rsa;
367 {
368 return(ASN1_i2d_bio(i2d_RSAPrivateKey,bp,(unsigned char *)rsa));
369 }
370
371RSA *d2i_RSAPublicKey_bio(bp,rsa)
372BIO *bp;
373RSA *rsa;
374 {
375 return((RSA *)ASN1_d2i_bio((char *(*)())
376 RSA_new,(char *(*)())d2i_RSAPublicKey, (bp),
377 (unsigned char **)(rsa)));
378 }
379
380int i2d_RSAPublicKey_bio(bp,rsa)
381BIO *bp;
382RSA *rsa;
383 {
384 return(ASN1_i2d_bio(i2d_RSAPublicKey,bp,(unsigned char *)rsa));
385 }
386#endif
387
388#ifndef NO_DSA
389#ifndef NO_FP_API
390DSA *d2i_DSAPrivateKey_fp(fp,dsa)
391FILE *fp;
392DSA *dsa;
393 {
394 return((DSA *)ASN1_d2i_fp((char *(*)())
395 DSA_new,(char *(*)())d2i_DSAPrivateKey, (fp),
396 (unsigned char **)(dsa)));
397 }
398
399int i2d_DSAPrivateKey_fp(fp,dsa)
400FILE *fp;
401DSA *dsa;
402 {
403 return(ASN1_i2d_fp(i2d_DSAPrivateKey,fp,(unsigned char *)dsa));
404 }
405#endif
406
407DSA *d2i_DSAPrivateKey_bio(bp,dsa)
408BIO *bp;
409DSA *dsa;
410 {
411 return((DSA *)ASN1_d2i_bio((char *(*)())
412 DSA_new,(char *(*)())d2i_DSAPrivateKey, (bp),
413 (unsigned char **)(dsa)));
414 }
415
416int i2d_DSAPrivateKey_bio(bp,dsa)
417BIO *bp;
418DSA *dsa;
419 {
420 return(ASN1_i2d_bio(i2d_DSAPrivateKey,bp,(unsigned char *)dsa));
421 }
422#endif
423
424X509_NAME *X509_NAME_dup(xn)
425X509_NAME *xn;
426 {
427 return((X509_NAME *)ASN1_dup((int (*)())i2d_X509_NAME,
428 (char *(*)())d2i_X509_NAME,(char *)xn));
429 }
430
431X509_NAME_ENTRY *X509_NAME_ENTRY_dup(ne)
432X509_NAME_ENTRY *ne;
433 {
434 return((X509_NAME_ENTRY *)ASN1_dup((int (*)())i2d_X509_NAME_ENTRY,
435 (char *(*)())d2i_X509_NAME_ENTRY,(char *)ne));
436 }
437
438int X509_digest(data,type,md,len)
439X509 *data;
440EVP_MD *type;
441unsigned char *md;
442unsigned int *len;
443 {
444 return(ASN1_digest((int (*)())i2d_X509,type,(char *)data,md,len));
445 }
446
447int X509_NAME_digest(data,type,md,len)
448X509_NAME *data;
449EVP_MD *type;
450unsigned char *md;
451unsigned int *len;
452 {
453 return(ASN1_digest((int (*)())i2d_X509_NAME,type,(char *)data,md,len));
454 }
455
456int PKCS7_ISSUER_AND_SERIAL_digest(data,type,md,len)
457PKCS7_ISSUER_AND_SERIAL *data;
458EVP_MD *type;
459unsigned char *md;
460unsigned int *len;
461 {
462 return(ASN1_digest((int (*)())i2d_PKCS7_ISSUER_AND_SERIAL,type,
463 (char *)data,md,len));
464 }
465
diff --git a/src/lib/libcrypto/x509v3/x509v3.h b/src/lib/libcrypto/x509v3/x509v3.h
new file mode 100644
index 0000000000..d7945bc9cd
--- /dev/null
+++ b/src/lib/libcrypto/x509v3/x509v3.h
@@ -0,0 +1,87 @@
1/* crypto/x509v3/x509v3.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#define X509v3_N_KU_digitalSignature 0
59#define X509v3_N_KU_nonRepudiation 1
60#define X509v3_N_KU_keyEncipherment 2
61#define X509v3_N_KU_dataEncipherment 3
62#define X509v3_N_KU_keyAgreement 4
63#define X509v3_N_KU_keyCertSign 5
64#define X509v3_N_KU_cRLSign 6
65#define X509v3_N_KU_encipherOnly 7
66#define X509v3_N_KU_decipherOnly 8
67#define X509v3_N_KU_NUM 9
68#define X509v3_S_KU_digitalSignature "digitalSignature"
69#define X509v3_S_KU_nonRepudiation "nonRepudiation"
70#define X509v3_S_KU_keyEncipherment "keyEncipherment"
71#define X509v3_S_KU_dataEncipherment "dataEncipherment"
72#define X509v3_S_KU_keyAgreement "keyAgreement"
73#define X509v3_S_KU_keyCertSign "keyCertSign"
74#define X509v3_S_KU_cRLSign "cRLSign"
75#define X509v3_S_KU_encipherOnly "encipherOnly"
76#define X509v3_S_KU_decipherOnly "decipherOnly"
77
78
79void X509_ex_clear(X509_EXTENSION *a);
80int X509_ex_get_bool(X509_EXTENSION *a,int num);
81int X509_ex_set_bool(X509_EXTENSION *a,int num,int value);
82int X509_ex_get_str(X509_EXTENSION *a,int index,char **p,int *len);
83int X509_ex_set_str(X509_EXTENSION *a,int oid,int index,char *p,int len);
84char *X509_ex_get_struct(X509_EXTENSION *a,int oid,int index,char **p);
85int X509_ex_set_struct(X509_EXTENSION *a,int index,char *p);
86int a2i_X509_EXTENSION(BIO *bp,X509_EXTENSION *a,char *buf,int len);
87int i2a_X509_EXTENSION(BIO *bp,X509_EXTENSION *a);