1*0a6a1f1dSLionel Sambuc /* $NetBSD: test_engine_dso.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include <config.h>
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc #include <stdio.h>
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc #include <krb5/roken.h>
41ebfedea0SLionel Sambuc #include <krb5/getarg.h>
42ebfedea0SLionel Sambuc
43ebfedea0SLionel Sambuc #include <engine.h>
44ebfedea0SLionel Sambuc #include <evp.h>
45ebfedea0SLionel Sambuc
46ebfedea0SLionel Sambuc struct {
47ebfedea0SLionel Sambuc const char *cpriv;
48ebfedea0SLionel Sambuc const char *cpub;
49ebfedea0SLionel Sambuc const char *spriv;
50ebfedea0SLionel Sambuc const char *spub;
51ebfedea0SLionel Sambuc } dhtests[] = {
52ebfedea0SLionel Sambuc {
53ebfedea0SLionel Sambuc "5C0946275D07223AEAF04301D964498F3285946057B4C50D13B4FE12C88DFD8D499DD3CC00C1BC17C0D343F2FE053C9F53389110551715B1EDF261A0314485C4835D01F7B8894027D534A2D81D63619D2F58C9864AC9816086B3FF75C01B3FAFF355425AB7369A6ABDC8B633F0A0DC4D29B50F364E7594B297183D14E5CDC05D",
54ebfedea0SLionel Sambuc "2D66DC5998B7AEE3332DC1061C6E6F6CF0FCCD74534187E2CDC9ACBCADF0FC9D5900451F44832A762F01E9CEEF1CBD7D69D020AC524D09FAD087DFADEAC36C845157B83937B51C8DB7F500C3C54FB2A05E074E40BA982186E7FEB2534EDDB387D5480AAA355B398CCAD0886F3952C3718490B7884FA67BD8B6943CDDA20134C6",
55ebfedea0SLionel Sambuc "42644BA7CF74689E18BA72BF80FCA674D1A2ADF81795EB3828E67C30E42ABD07A8E90E27F046189FAC122D915276870B72427388EAAB5D06994FC38885BBACCEA1CFC45951B730D73C1A8F83208CD1351746601648C11D70BC95B817C86E4A5C40D633654615041C7934BB3CAF4E02754D542033DB024E94C7E561A29ED0C6EC",
56ebfedea0SLionel Sambuc "C233633AB116E2DB20B4E08DA42DE8766293E6D9042F7A2C2A2F34F18FE66010B074CCF3C9B03EF27B14F0746B738AF22776224161D767D96AEC230A1DFA6DECFFCE9FED23B96F50CCB0093E59817AD0CEAEB7993AB5764679948BFB1293C9560B07AA3DFA229E341EB17C9FAE0B1D483082461D2DDBCEEE6FE7C0A34D96F66D"
57ebfedea0SLionel Sambuc },
58ebfedea0SLionel Sambuc {
59ebfedea0SLionel Sambuc "76295C1280B890970F0F7EB01BBD9C5DF9BB8F590EB384A39EBF85CD141451407F955FD1D39012AA1F8BA53FD6A5A37CB2835CEDB27D1EBF1FE8AC9F2FFD628BD9BF7B8DD77CB80C8DC0A75F4567C7700442B26972833EB9738A8728A1FC274C59CED5E3ADA224B46711112AAA1CB831D2D6125E183ADA4F805A05024C9C6DDB",
60ebfedea0SLionel Sambuc "1E0AB5EBAAC7985FE67A574447FAE58AE4CB95416278D4C239A789D4532FA8E6F82BA10BE411D8A0A06B9E1DECE704466B3523496A8A4165B97FBCFB9CE9C4FF2DEEE786BA046E8C270FA8A9055D2F6E42EDDB32C73CF7875551A56EB69C0F14A3745745845B81C347401B27D074C60C5177BA9C14BBB1C8C219B78E15126EF8",
61ebfedea0SLionel Sambuc "68D84A8F92082F113542CFD990DEEFAD9C7EFA545268F8B3EBDF4CCBAF2865CF03EF60044EB4AF4154E6804CC2BDD673B801507446CEFC692DA577B6DC6E0272B7B081A1BEFDC2A4FAC83DB8845E3DA0D1B64DB33AA2164FEDB08A01E815336BD58F4E6DE6A265468E61C8C988B8AEC0D52DB714448DDC007E7C3382C07357DB",
62ebfedea0SLionel Sambuc "393815D507A2EF80DE2D0F2A55AAB1C25B870ACA3FC97438B4336CBF979BF9A4F8DA1B61C667129F9123045E07E24976040EC5E2368DD4EF70690102D74E900B260D3826256FD473733A7569BF514652AB78C48C334FDCA26C44ABF322643AF15BFF693A37BB2C19CA9FE5F1537FCFE2B24CF74D4E57060D35ABF115B4B6CD21"
63ebfedea0SLionel Sambuc },
64ebfedea0SLionel Sambuc {
65ebfedea0SLionel Sambuc "7307D6C3CB874327A95F7A6A91C336CEAA086736525DF3F8EC49497CF444C68D264EB70CD6904FE56E240EEF34E6C5177911C478A7F250A0F54183BCBE64B42BAB5D019E73E2F17C095C211E4815E6BA5FDD72786AF987ABBC9109ECEEF439AF9E2141D5222CE7DC0152D8E9A6CCCE301D21A7D1D6ACB9B91B5E28379C91890D",
66ebfedea0SLionel Sambuc "83FBD7BFFDF415BBB7E21D399CB2F36A61AFDBAFC542E428E444C66AA03617C0C55C639FE2428905B57035892AE1BD2C4060E807D9E003B0C204FFC8FDD69CC8ADE7A8E18DCBFFF64E3EF9DA2C117390374241466E48A020A1B2F575AE42C233F8BD357B8331CC203E0345DFC19C73E6F1F70B6C2786E681D73BF48B15FE9992",
67ebfedea0SLionel Sambuc "61BCF748BB05A48861578B8CB1855200B2E62A40E126BD7323E5B714645A54A2C8761EE39EE39BA6D2FE19B688168EDEA6DC5056400B5315ED299E7926176B887012E58634D78F05D7BCF0E1B81B1B41F5F8EF0B0711D3A64F9A317DD183AE039A4D3BE02A515892362F8C7BB6EB6434BB25418A438ED33D50C475122CBBE862",
68ebfedea0SLionel Sambuc "7DB8D69D1605D9812B7F2F3E92BCEEB3426FEEE3265A174D71B2B6E16B332B43DF0B3C2FA152E48DE2FAC110D8CECE122C3398558E7987B27CACE12722C0032AC7E7766A9BCC881BA35B9DB9E751BD4E51F7683DE092F6C1D4DD937CDCE9C16E6F7D77CC6AAD806E4082E8E22E28592C4D78256354393FE831E811E03ED0A81A"
69ebfedea0SLionel Sambuc },
70ebfedea0SLionel Sambuc {
71ebfedea0SLionel Sambuc "60C18B62F786DE6A4A8B13EB6DA2380B4C6731F861C715D9496DCF4A9F01CD33DDB52F1AB4D1F820FAF7AD4EFEB66586F7F08135714B13D77FE652B9EEAB2C543596A9ED307C1629CF535DD14AB22F081AE4ADF7A3E0BC7B33E0EC7A7306F9A737F55807974B5E1B7B6394BD0373917128B43A17757B34BAE1B600763E957F75",
72ebfedea0SLionel Sambuc "0DEDA337C38EA005D5B8567EAB681CE91892C2C62C9D42BF748FBFE681E11F25D98280E42E1539A10EEE9177EF2F40216987936AF19D9B5EBE22EEAC27242D77CE3A5061F2E5CFACF15CD0F80E736AE8642252FE91E129DE3C78CFB85A0B1BB87B059CBB24483444F8A07244F4E89370BA78D58BD409DFBB3D41921B8879B9C7",
73ebfedea0SLionel Sambuc "462C0707CF3366C2242A808CFDB79B77E8B3AF9D796583EB9CCD7BF4E8792AB0A818E49FFE53CA241F56988F825B366BF1E78481F8086A123259B9D83AC643E85845BF6B2C5412FFDDFAA8C9ED203CA4B3C1BFD777286099976472FA15B3CCC8418CF162F03C0C3E85D7EFC5CF5ACB9B2C039CCF3A1A9C6BB6B9C09C18D86CBD",
74ebfedea0SLionel Sambuc "56DB382EDB8C2D95934D20261CE1A37090B0802D451E647DB1DA3B73CDB5A878EAD598A8817302449370F9D45E34F5C45F73D02BF4EB2B3712A8665F446F5D2B774039E5444AB74807859FA58DF9EBA4B12BA4545ACED827E4ED64CC71F937D64A1033BC43403F2490C1B715A74822B8D50A72A102213F0CF7A1B98B771B34C4"
75ebfedea0SLionel Sambuc },
76ebfedea0SLionel Sambuc {
77ebfedea0SLionel Sambuc "61B7321207F4A73646E43E99221F902D2F38095E84CE7346A1510FE71BA7B9B34DCB6609E4DDDA8C82426E82D1C23F1E761130ECE4638D77554A7618E1608625049328FCC1F8845CA9A88E847106B01BD31EF6500E3C7EE81A048924BEAA3EDF367E5F4575341206C7A76427571898294B07BD918D4C2642854CC89D439042E5",
78ebfedea0SLionel Sambuc "29AA38E63E4DD7C651E25DEC7A5A53E48114F52813793D36A9DBDD4F7C06FC38406E330764E0B2AFD811C39D857EA5F904105360E06856DC0780C7D61C53165833F0AEA15CB54732DE113F44C8FCFB86F4A876DD42D7A55356D91C0173F2B012680FB54C13EF54B65DF4AEDE2E13419B1316435187CEF07D44DB3DF57C4703FD",
79ebfedea0SLionel Sambuc "5ED5AFB04CBFEE43EF3D9B60A57080831563648A2380D98F1EA4A96CF153903A40A2E564DED87E7254DF3270568AB952BF6F400681DD6AD919C9B06AC0F45F0646BCF37B217191AA0B7B7BED226B61F48B46DEA2E5A09E41F316583823A38A60FFD79085F43F60D98871ECA1A0F667701425094E88885A81DE9DA6C293E95060",
80ebfedea0SLionel Sambuc "4DE4F24EAA3E2790FBCB1B13C2ED0EFD846EC33154DBEBBEFD895E1399B3617D55EC2CE8D71CF380B55D93636FEF741328D6B1E224D46F8A8B60A41D08DD86E88DE806AA781791364E6D88BF68571BF5D8C35CB04BA302227B7E4CB6A67AB7510ACBCDBF2F8A95EB5DEE693CCA5CC425A0F1CA2D18C369A767906A2477E32704"
81ebfedea0SLionel Sambuc }
82ebfedea0SLionel Sambuc };
83ebfedea0SLionel Sambuc
84ebfedea0SLionel Sambuc static void
dh_test(DH * server,DH * client)85ebfedea0SLionel Sambuc dh_test(DH *server, DH *client)
86ebfedea0SLionel Sambuc {
87ebfedea0SLionel Sambuc void *skey, *ckey;
88ebfedea0SLionel Sambuc int ssize, csize;
89ebfedea0SLionel Sambuc
90ebfedea0SLionel Sambuc skey = emalloc(DH_size(server));
91ebfedea0SLionel Sambuc ckey = emalloc(DH_size(client));
92ebfedea0SLionel Sambuc
93ebfedea0SLionel Sambuc ssize = DH_compute_key(skey, client->pub_key, server);
94ebfedea0SLionel Sambuc if (ssize == -1)
95ebfedea0SLionel Sambuc errx(1, "DH_compute_key failed for server");
96ebfedea0SLionel Sambuc csize = DH_compute_key(ckey, server->pub_key, client);
97ebfedea0SLionel Sambuc if (csize == -1)
98ebfedea0SLionel Sambuc errx(1, "DH_compute_key failed for client");
99ebfedea0SLionel Sambuc
100ebfedea0SLionel Sambuc if (ssize != csize)
101ebfedea0SLionel Sambuc errx(1, "DH_compute_key size mismatch");
102ebfedea0SLionel Sambuc
103ebfedea0SLionel Sambuc if (memcmp(skey, ckey, csize) != 0)
104ebfedea0SLionel Sambuc errx(1, "DH_compute_key key mismatch");
105ebfedea0SLionel Sambuc
106ebfedea0SLionel Sambuc free(skey);
107ebfedea0SLionel Sambuc free(ckey);
108ebfedea0SLionel Sambuc }
109ebfedea0SLionel Sambuc
110ebfedea0SLionel Sambuc
111ebfedea0SLionel Sambuc static int version_flag;
112ebfedea0SLionel Sambuc static int help_flag;
113ebfedea0SLionel Sambuc static char *id_flag;
114ebfedea0SLionel Sambuc static char *rsa_flag;
115ebfedea0SLionel Sambuc static int dh_flag = 1;
116ebfedea0SLionel Sambuc static int test_random_flag;
117ebfedea0SLionel Sambuc
118ebfedea0SLionel Sambuc static struct getargs args[] = {
119ebfedea0SLionel Sambuc { "id", 0, arg_string, &id_flag,
120ebfedea0SLionel Sambuc "selects the engine id", "engine-id" },
121ebfedea0SLionel Sambuc { "rsa", 0, arg_string, &rsa_flag,
122ebfedea0SLionel Sambuc "tests RSA modes", "private-rsa-der-file" },
123ebfedea0SLionel Sambuc { "dh", 0, arg_negative_flag, &dh_flag,
124ebfedea0SLionel Sambuc "test dh", NULL },
125ebfedea0SLionel Sambuc { "test-random", 0, arg_flag, &test_random_flag,
126ebfedea0SLionel Sambuc "test if there is a random device", NULL },
127ebfedea0SLionel Sambuc { "version", 0, arg_flag, &version_flag,
128ebfedea0SLionel Sambuc "print version", NULL },
129ebfedea0SLionel Sambuc { "help", 0, arg_flag, &help_flag,
130ebfedea0SLionel Sambuc NULL, NULL }
131ebfedea0SLionel Sambuc };
132ebfedea0SLionel Sambuc
133ebfedea0SLionel Sambuc static void
usage(int ret)134ebfedea0SLionel Sambuc usage (int ret)
135ebfedea0SLionel Sambuc {
136ebfedea0SLionel Sambuc arg_printusage (args,
137ebfedea0SLionel Sambuc sizeof(args)/sizeof(*args),
138ebfedea0SLionel Sambuc NULL,
139ebfedea0SLionel Sambuc "filename.so");
140ebfedea0SLionel Sambuc exit (ret);
141ebfedea0SLionel Sambuc }
142ebfedea0SLionel Sambuc
143ebfedea0SLionel Sambuc int
main(int argc,char ** argv)144ebfedea0SLionel Sambuc main(int argc, char **argv)
145ebfedea0SLionel Sambuc {
146ebfedea0SLionel Sambuc ENGINE *engine = NULL;
147ebfedea0SLionel Sambuc int idx = 0;
148ebfedea0SLionel Sambuc int have_rsa, have_dh;
149ebfedea0SLionel Sambuc
150ebfedea0SLionel Sambuc setprogname(argv[0]);
151ebfedea0SLionel Sambuc
152ebfedea0SLionel Sambuc if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
153ebfedea0SLionel Sambuc usage(1);
154ebfedea0SLionel Sambuc
155ebfedea0SLionel Sambuc if (help_flag)
156ebfedea0SLionel Sambuc usage(0);
157ebfedea0SLionel Sambuc
158ebfedea0SLionel Sambuc if(version_flag){
159ebfedea0SLionel Sambuc print_version(NULL);
160ebfedea0SLionel Sambuc exit(0);
161ebfedea0SLionel Sambuc }
162ebfedea0SLionel Sambuc
163ebfedea0SLionel Sambuc argc -= idx;
164ebfedea0SLionel Sambuc argv += idx;
165ebfedea0SLionel Sambuc
166ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms();
167ebfedea0SLionel Sambuc
168ebfedea0SLionel Sambuc if (argc == 0) {
169ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms();
170ebfedea0SLionel Sambuc ENGINE_load_builtin_engines();
171ebfedea0SLionel Sambuc engine = ENGINE_by_id("builtin");
172ebfedea0SLionel Sambuc } else {
173ebfedea0SLionel Sambuc engine = ENGINE_by_dso(argv[0], id_flag);
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc if (engine == NULL)
176ebfedea0SLionel Sambuc errx(1, "ENGINE_by_dso failed");
177ebfedea0SLionel Sambuc
178ebfedea0SLionel Sambuc printf("name: %s\n", ENGINE_get_name(engine));
179ebfedea0SLionel Sambuc printf("id: %s\n", ENGINE_get_id(engine));
180ebfedea0SLionel Sambuc have_rsa = ENGINE_get_RSA(engine) != NULL;
181ebfedea0SLionel Sambuc have_dh = ENGINE_get_DH(engine) != NULL;
182ebfedea0SLionel Sambuc printf("RSA: %s", have_rsa ? "yes," : "no");
183ebfedea0SLionel Sambuc if (have_rsa)
184ebfedea0SLionel Sambuc printf(" %s", ENGINE_get_RSA(engine)->name);
185ebfedea0SLionel Sambuc printf("\n");
186ebfedea0SLionel Sambuc printf("DH: %s", have_dh ? "yes," : "no");
187ebfedea0SLionel Sambuc if (have_dh)
188ebfedea0SLionel Sambuc printf(" %s", ENGINE_get_DH(engine)->name);
189ebfedea0SLionel Sambuc printf("\n");
190ebfedea0SLionel Sambuc
191ebfedea0SLionel Sambuc if (RAND_status() != 1)
192ebfedea0SLionel Sambuc errx(77, "no functional random device, can't execute tests");
193ebfedea0SLionel Sambuc if (test_random_flag)
194ebfedea0SLionel Sambuc exit(0);
195ebfedea0SLionel Sambuc
196ebfedea0SLionel Sambuc if (rsa_flag && have_rsa) {
197ebfedea0SLionel Sambuc unsigned char buf[1024 * 4];
198ebfedea0SLionel Sambuc const unsigned char *p;
199ebfedea0SLionel Sambuc size_t size;
200ebfedea0SLionel Sambuc int keylen;
201ebfedea0SLionel Sambuc RSA *rsa;
202ebfedea0SLionel Sambuc FILE *f;
203ebfedea0SLionel Sambuc
204ebfedea0SLionel Sambuc f = fopen(rsa_flag, "rb");
205ebfedea0SLionel Sambuc if (f == NULL)
206ebfedea0SLionel Sambuc err(1, "could not open file %s", rsa_flag);
207ebfedea0SLionel Sambuc
208ebfedea0SLionel Sambuc size = fread(buf, 1, sizeof(buf), f);
209ebfedea0SLionel Sambuc if (size == 0)
210ebfedea0SLionel Sambuc err(1, "failed to read file %s", rsa_flag);
211ebfedea0SLionel Sambuc if (size == sizeof(buf))
212ebfedea0SLionel Sambuc err(1, "key too long in file %s!", rsa_flag);
213ebfedea0SLionel Sambuc fclose(f);
214ebfedea0SLionel Sambuc
215ebfedea0SLionel Sambuc p = buf;
216ebfedea0SLionel Sambuc rsa = d2i_RSAPrivateKey(NULL, &p, size);
217ebfedea0SLionel Sambuc if (rsa == NULL)
218ebfedea0SLionel Sambuc err(1, "failed to parse key in file %s", rsa_flag);
219ebfedea0SLionel Sambuc
220ebfedea0SLionel Sambuc RSA_set_method(rsa, ENGINE_get_RSA(engine));
221ebfedea0SLionel Sambuc
222ebfedea0SLionel Sambuc /*
223ebfedea0SLionel Sambuc * try rsa signing
224ebfedea0SLionel Sambuc */
225ebfedea0SLionel Sambuc
226ebfedea0SLionel Sambuc memcpy(buf, "hejsan", 7);
227ebfedea0SLionel Sambuc keylen = RSA_private_encrypt(7, buf, buf, rsa, RSA_PKCS1_PADDING);
228ebfedea0SLionel Sambuc if (keylen <= 0)
229ebfedea0SLionel Sambuc errx(1, "failed to private encrypt");
230ebfedea0SLionel Sambuc
231ebfedea0SLionel Sambuc keylen = RSA_public_decrypt(keylen, buf, buf, rsa, RSA_PKCS1_PADDING);
232ebfedea0SLionel Sambuc if (keylen <= 0)
233ebfedea0SLionel Sambuc errx(1, "failed to public decrypt");
234ebfedea0SLionel Sambuc
235ebfedea0SLionel Sambuc if (keylen != 7)
236ebfedea0SLionel Sambuc errx(1, "output buffer not same length: %d", (int)keylen);
237ebfedea0SLionel Sambuc
238ebfedea0SLionel Sambuc if (memcmp(buf, "hejsan", 7) != 0)
239ebfedea0SLionel Sambuc errx(1, "string not the same after decryption");
240ebfedea0SLionel Sambuc
241ebfedea0SLionel Sambuc /*
242ebfedea0SLionel Sambuc * try rsa encryption
243ebfedea0SLionel Sambuc */
244ebfedea0SLionel Sambuc
245ebfedea0SLionel Sambuc memcpy(buf, "hejsan", 7);
246ebfedea0SLionel Sambuc keylen = RSA_public_encrypt(7, buf, buf, rsa, RSA_PKCS1_PADDING);
247ebfedea0SLionel Sambuc if (keylen <= 0)
248ebfedea0SLionel Sambuc errx(1, "failed to public encrypt");
249ebfedea0SLionel Sambuc
250ebfedea0SLionel Sambuc keylen = RSA_private_decrypt(keylen, buf, buf, rsa, RSA_PKCS1_PADDING);
251ebfedea0SLionel Sambuc if (keylen <= 0)
252ebfedea0SLionel Sambuc errx(1, "failed to private decrypt");
253ebfedea0SLionel Sambuc
254ebfedea0SLionel Sambuc if (keylen != 7)
255ebfedea0SLionel Sambuc errx(1, "output buffer not same length: %d", (int)keylen);
256ebfedea0SLionel Sambuc
257ebfedea0SLionel Sambuc if (memcmp(buf, "hejsan", 7) != 0)
258ebfedea0SLionel Sambuc errx(1, "string not the same after decryption");
259ebfedea0SLionel Sambuc
260ebfedea0SLionel Sambuc RSA_free(rsa);
261ebfedea0SLionel Sambuc
262ebfedea0SLionel Sambuc printf("rsa test passed\n");
263ebfedea0SLionel Sambuc
264ebfedea0SLionel Sambuc }
265ebfedea0SLionel Sambuc
266ebfedea0SLionel Sambuc if (dh_flag) {
267ebfedea0SLionel Sambuc DH *server, *client;
268ebfedea0SLionel Sambuc int i;
269ebfedea0SLionel Sambuc
270ebfedea0SLionel Sambuc /* RFC2412-MODP-group2 */
271ebfedea0SLionel Sambuc const char *p =
272ebfedea0SLionel Sambuc "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
273ebfedea0SLionel Sambuc "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
274ebfedea0SLionel Sambuc "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
275ebfedea0SLionel Sambuc "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
276ebfedea0SLionel Sambuc "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
277ebfedea0SLionel Sambuc "FFFFFFFF" "FFFFFFFF";
278ebfedea0SLionel Sambuc const char *g = "02";
279ebfedea0SLionel Sambuc
280ebfedea0SLionel Sambuc /*
281ebfedea0SLionel Sambuc * Try generated keys
282ebfedea0SLionel Sambuc */
283ebfedea0SLionel Sambuc
284ebfedea0SLionel Sambuc for (i = 0; i < 10; i++) {
285ebfedea0SLionel Sambuc server = DH_new_method(engine);
286ebfedea0SLionel Sambuc client = DH_new_method(engine);
287ebfedea0SLionel Sambuc
288ebfedea0SLionel Sambuc BN_hex2bn(&server->p, p);
289ebfedea0SLionel Sambuc BN_hex2bn(&client->p, p);
290ebfedea0SLionel Sambuc BN_hex2bn(&server->g, g);
291ebfedea0SLionel Sambuc BN_hex2bn(&client->g, g);
292ebfedea0SLionel Sambuc
293ebfedea0SLionel Sambuc if (!DH_generate_key(server))
294ebfedea0SLionel Sambuc errx(1, "DH_generate_key failed for server");
295ebfedea0SLionel Sambuc if (!DH_generate_key(client))
296ebfedea0SLionel Sambuc errx(1, "DH_generate_key failed for client");
297ebfedea0SLionel Sambuc
298ebfedea0SLionel Sambuc dh_test(server, client);
299ebfedea0SLionel Sambuc
300ebfedea0SLionel Sambuc DH_free(server);
301ebfedea0SLionel Sambuc DH_free(client);
302ebfedea0SLionel Sambuc }
303ebfedea0SLionel Sambuc /*
304ebfedea0SLionel Sambuc * Try known result
305ebfedea0SLionel Sambuc */
306ebfedea0SLionel Sambuc
307ebfedea0SLionel Sambuc for (i = 0; i < sizeof(dhtests)/sizeof(dhtests[0]); i++) {
308ebfedea0SLionel Sambuc
309ebfedea0SLionel Sambuc server = DH_new_method(engine);
310ebfedea0SLionel Sambuc client = DH_new_method(engine);
311ebfedea0SLionel Sambuc
312ebfedea0SLionel Sambuc BN_hex2bn(&server->p, p);
313ebfedea0SLionel Sambuc BN_hex2bn(&client->p, p);
314ebfedea0SLionel Sambuc BN_hex2bn(&server->g, g);
315ebfedea0SLionel Sambuc BN_hex2bn(&client->g, g);
316ebfedea0SLionel Sambuc
317ebfedea0SLionel Sambuc BN_hex2bn(&client->priv_key, dhtests[i].cpriv);
318ebfedea0SLionel Sambuc BN_hex2bn(&client->pub_key, dhtests[i].cpub);
319ebfedea0SLionel Sambuc BN_hex2bn(&server->priv_key, dhtests[i].spriv);
320ebfedea0SLionel Sambuc BN_hex2bn(&server->pub_key, dhtests[i].spub);
321ebfedea0SLionel Sambuc
322ebfedea0SLionel Sambuc dh_test(server, client);
323ebfedea0SLionel Sambuc
324ebfedea0SLionel Sambuc DH_free(server);
325ebfedea0SLionel Sambuc DH_free(client);
326ebfedea0SLionel Sambuc }
327ebfedea0SLionel Sambuc
328ebfedea0SLionel Sambuc printf("DH test passed\n");
329ebfedea0SLionel Sambuc }
330ebfedea0SLionel Sambuc
331ebfedea0SLionel Sambuc ENGINE_finish(engine);
332ebfedea0SLionel Sambuc
333ebfedea0SLionel Sambuc return 0;
334ebfedea0SLionel Sambuc }
335