bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / ssl / ssl_engine_dh.c
1 #if 0
2 =pod
3 #endif
4 /* Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 /*                      _             _
21  *  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
22  * | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
23  * | | | | | | (_) | (_| |   \__ \__ \ |
24  * |_| |_| |_|\___/ \__,_|___|___/___/_|
25  *                      |_____|
26  * ssl_engine_dh.c 
27  * Diffie-Hellman Built-in Temporary Parameters
28  */
29
30 #include "mod_ssl.h"
31
32 /* ----BEGIN GENERATED SECTION-------- */
33
34 /*
35 ** Diffie-Hellman-Parameters: (512 bit)
36 **     prime:
37 **         00:d4:bc:d5:24:06:f6:9b:35:99:4b:88:de:5d:b8:
38 **         96:82:c8:15:7f:62:d8:f3:36:33:ee:57:72:f1:1f:
39 **         05:ab:22:d6:b5:14:5b:9f:24:1e:5a:cc:31:ff:09:
40 **         0a:4b:c7:11:48:97:6f:76:79:50:94:e7:1e:79:03:
41 **         52:9f:5a:82:4b
42 **     generator: 2 (0x2)
43 ** Diffie-Hellman-Parameters: (1024 bit)
44 **     prime:
45 **         00:e6:96:9d:3d:49:5b:e3:2c:7c:f1:80:c3:bd:d4:
46 **         79:8e:91:b7:81:82:51:bb:05:5e:2a:20:64:90:4a:
47 **         79:a7:70:fa:15:a2:59:cb:d5:23:a6:a6:ef:09:c4:
48 **         30:48:d5:a2:2f:97:1f:3c:20:12:9b:48:00:0e:6e:
49 **         dd:06:1c:bc:05:3e:37:1d:79:4e:53:27:df:61:1e:
50 **         bb:be:1b:ac:9b:5c:60:44:cf:02:3d:76:e0:5e:ea:
51 **         9b:ad:99:1b:13:a6:3c:97:4e:9e:f1:83:9e:b5:db:
52 **         12:51:36:f7:26:2e:56:a8:87:15:38:df:d8:23:c6:
53 **         50:50:85:e2:1f:0d:d5:c8:6b
54 **     generator: 2 (0x2)
55 */
56
57 static unsigned char dh512_p[] =
58 {
59     0xD4, 0xBC, 0xD5, 0x24, 0x06, 0xF6, 0x9B, 0x35, 0x99, 0x4B, 0x88, 0xDE,
60     0x5D, 0xB8, 0x96, 0x82, 0xC8, 0x15, 0x7F, 0x62, 0xD8, 0xF3, 0x36, 0x33,
61     0xEE, 0x57, 0x72, 0xF1, 0x1F, 0x05, 0xAB, 0x22, 0xD6, 0xB5, 0x14, 0x5B,
62     0x9F, 0x24, 0x1E, 0x5A, 0xCC, 0x31, 0xFF, 0x09, 0x0A, 0x4B, 0xC7, 0x11,
63     0x48, 0x97, 0x6F, 0x76, 0x79, 0x50, 0x94, 0xE7, 0x1E, 0x79, 0x03, 0x52,
64     0x9F, 0x5A, 0x82, 0x4B,
65 };
66 static unsigned char dh512_g[] =
67 {
68     0x02,
69 };
70
71 static DH *get_dh512(void)
72 {
73     return modssl_dh_configure(dh512_p, sizeof(dh512_p),
74                                dh512_g, sizeof(dh512_g));
75 }
76
77 static unsigned char dh1024_p[] =
78 {
79     0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
80     0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
81     0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
82     0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
83     0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
84     0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
85     0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
86     0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
87     0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
88     0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
89     0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
90 };
91 static unsigned char dh1024_g[] =
92 {
93     0x02,
94 };
95
96 static DH *get_dh1024(void)
97 {
98     return modssl_dh_configure(dh1024_p, sizeof(dh1024_p),
99                                dh1024_g, sizeof(dh1024_g));
100 }
101 /* ----END GENERATED SECTION---------- */
102
103 DH *ssl_dh_GetTmpParam(int nKeyLen)
104 {
105     DH *dh;
106
107     if (nKeyLen == 512)
108         dh = get_dh512();
109     else if (nKeyLen == 1024)
110         dh = get_dh1024();
111     else
112         dh = get_dh1024();
113     return dh;
114 }
115
116 DH *ssl_dh_GetParamFromFile(char *file)
117 {
118     DH *dh = NULL;
119     BIO *bio;
120
121     if ((bio = BIO_new_file(file, "r")) == NULL)
122         return NULL;
123 #if SSL_LIBRARY_VERSION < 0x00904000
124     dh = PEM_read_bio_DHparams(bio, NULL, NULL);
125 #else
126     dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
127 #endif
128     BIO_free(bio);
129     return (dh);
130 }
131
132 /*
133 =cut
134 ##
135 ##  Embedded Perl script for generating the temporary DH parameters
136 ##
137
138 require 5.003;
139 use strict;
140
141 #   configuration
142 my $file  = $0;
143 my $begin = '----BEGIN GENERATED SECTION--------';
144 my $end   = '----END GENERATED SECTION----------';
145
146 #   read ourself and keep a backup
147 open(FP, "<$file") || die;
148 my $source = '';
149 $source .= $_ while (<FP>);
150 close(FP);
151 open(FP, ">$file.bak") || die;
152 print FP $source;
153 close(FP);
154
155 #   generate the DH parameters
156 print "1. Generate 512 and 1024 bit Diffie-Hellman parameters (p, g)\n";
157 my $rand = '';
158 foreach $file (qw(/var/log/messages /var/adm/messages 
159                   /kernel /vmunix /vmlinuz /etc/hosts /etc/resolv.conf)) {
160     if (-f $file) {
161         $rand = $file     if ($rand eq '');
162         $rand .= ":$file" if ($rand ne '');
163     }
164 }
165 $rand = "-rand $rand" if ($rand ne '');
166 system("openssl gendh $rand -out dh512.pem 512");
167 system("openssl gendh $rand -out dh1024.pem 1024");
168
169 #   generate DH param info 
170 my $dhinfo = '';
171 open(FP, "openssl dh -noout -text -in dh512.pem |") || die;
172 $dhinfo .= $_ while (<FP>);
173 close(FP);
174 open(FP, "openssl dh -noout -text -in dh1024.pem |") || die;
175 $dhinfo .= $_ while (<FP>);
176 close(FP);
177 $dhinfo =~ s|^|** |mg;
178 $dhinfo = "\n\/\*\n$dhinfo\*\/\n\n";
179
180 #   generate C source from DH params
181 my $dhsource = '';
182 open(FP, "openssl dh -noout -C -in dh512.pem | indent | expand |") || die;
183 $dhsource .= $_ while (<FP>);
184 close(FP);
185 open(FP, "openssl dh -noout -C -in dh1024.pem | indent | expand |") || die;
186 $dhsource .= $_ while (<FP>);
187 close(FP);
188 $dhsource =~ s|(DH\s+\*get_dh)|static $1|sg;
189
190 #   generate output
191 my $o = $dhinfo . $dhsource;
192
193 #   insert the generated code at the target location
194 $source =~ s|(\/\* $begin.+?\n).*\n(.*?\/\* $end)|$1$o$2|s;
195
196 #   and update the source on disk
197 print "Updating file `$file'\n";
198 open(FP, ">$file") || die;
199 print FP $source;
200 close(FP);
201
202 #   cleanup
203 unlink("dh512.pem");
204 unlink("dh1024.pem");
205
206 =pod
207 */