1#if 0
2=pod
3#endif
4
5/* Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements.  See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License.  You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21/*                      _             _
22 *  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
23 * | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
24 * | | | | | | (_) | (_| |   \__ \__ \ |
25 * |_| |_| |_|\___/ \__,_|___|___/___/_|
26 *                      |_____|
27 * ssl_engine_dh.c
28 * Diffie-Hellman Built-in Temporary Parameters
29 */
30
31#include "ssl_private.h"
32
33/* ----BEGIN GENERATED SECTION-------- */
34
35/*
36** Diffie-Hellman-Parameters: (512 bit)
37**     prime:
38**         00:9f:db:8b:8a:00:45:44:f0:04:5f:17:37:d0:ba:
39**         2e:0b:27:4c:df:1a:9f:58:82:18:fb:43:53:16:a1:
40**         6e:37:41:71:fd:19:d8:d8:f3:7c:39:bf:86:3f:d6:
41**         0e:3e:30:06:80:a3:03:0c:6e:4c:37:57:d0:8f:70:
42**         e6:aa:87:10:33
43**     generator: 2 (0x2)
44** Diffie-Hellman-Parameters: (1024 bit)
45**     prime:
46**         00:d6:7d:e4:40:cb:bb:dc:19:36:d6:93:d3:4a:fd:
47**         0a:d5:0c:84:d2:39:a4:5f:52:0b:b8:81:74:cb:98:
48**         bc:e9:51:84:9f:91:2e:63:9c:72:fb:13:b4:b4:d7:
49**         17:7e:16:d5:5a:c1:79:ba:42:0b:2a:29:fe:32:4a:
50**         46:7a:63:5e:81:ff:59:01:37:7b:ed:dc:fd:33:16:
51**         8a:46:1a:ad:3b:72:da:e8:86:00:78:04:5b:07:a7:
52**         db:ca:78:74:08:7d:15:10:ea:9f:cc:9d:dd:33:05:
53**         07:dd:62:db:88:ae:aa:74:7d:e0:f4:d6:e2:bd:68:
54**         b0:e7:39:3e:0f:24:21:8e:b3
55**     generator: 2 (0x2)
56*/
57
58static unsigned char dh512_p[] = {
59    0x9F, 0xDB, 0x8B, 0x8A, 0x00, 0x45, 0x44, 0xF0, 0x04, 0x5F, 0x17, 0x37,
60    0xD0, 0xBA, 0x2E, 0x0B, 0x27, 0x4C, 0xDF, 0x1A, 0x9F, 0x58, 0x82, 0x18,
61    0xFB, 0x43, 0x53, 0x16, 0xA1, 0x6E, 0x37, 0x41, 0x71, 0xFD, 0x19, 0xD8,
62    0xD8, 0xF3, 0x7C, 0x39, 0xBF, 0x86, 0x3F, 0xD6, 0x0E, 0x3E, 0x30, 0x06,
63    0x80, 0xA3, 0x03, 0x0C, 0x6E, 0x4C, 0x37, 0x57, 0xD0, 0x8F, 0x70, 0xE6,
64    0xAA, 0x87, 0x10, 0x33,
65};
66static unsigned char dh512_g[] = {
67    0x02,
68};
69
70static DH *get_dh512(void)
71{
72    return modssl_dh_configure(dh512_p, sizeof(dh512_p),
73                               dh512_g, sizeof(dh512_g));
74}
75
76static unsigned char dh1024_p[] = {
77    0xD6, 0x7D, 0xE4, 0x40, 0xCB, 0xBB, 0xDC, 0x19, 0x36, 0xD6, 0x93, 0xD3,
78    0x4A, 0xFD, 0x0A, 0xD5, 0x0C, 0x84, 0xD2, 0x39, 0xA4, 0x5F, 0x52, 0x0B,
79    0xB8, 0x81, 0x74, 0xCB, 0x98, 0xBC, 0xE9, 0x51, 0x84, 0x9F, 0x91, 0x2E,
80    0x63, 0x9C, 0x72, 0xFB, 0x13, 0xB4, 0xB4, 0xD7, 0x17, 0x7E, 0x16, 0xD5,
81    0x5A, 0xC1, 0x79, 0xBA, 0x42, 0x0B, 0x2A, 0x29, 0xFE, 0x32, 0x4A, 0x46,
82    0x7A, 0x63, 0x5E, 0x81, 0xFF, 0x59, 0x01, 0x37, 0x7B, 0xED, 0xDC, 0xFD,
83    0x33, 0x16, 0x8A, 0x46, 0x1A, 0xAD, 0x3B, 0x72, 0xDA, 0xE8, 0x86, 0x00,
84    0x78, 0x04, 0x5B, 0x07, 0xA7, 0xDB, 0xCA, 0x78, 0x74, 0x08, 0x7D, 0x15,
85    0x10, 0xEA, 0x9F, 0xCC, 0x9D, 0xDD, 0x33, 0x05, 0x07, 0xDD, 0x62, 0xDB,
86    0x88, 0xAE, 0xAA, 0x74, 0x7D, 0xE0, 0xF4, 0xD6, 0xE2, 0xBD, 0x68, 0xB0,
87    0xE7, 0x39, 0x3E, 0x0F, 0x24, 0x21, 0x8E, 0xB3,
88};
89static unsigned char dh1024_g[] = {
90    0x02,
91};
92
93static DH *get_dh1024(void)
94{
95    return modssl_dh_configure(dh1024_p, sizeof(dh1024_p),
96                               dh1024_g, sizeof(dh1024_g));
97}
98
99/* ----END GENERATED SECTION---------- */
100
101DH *ssl_dh_GetTmpParam(int nKeyLen)
102{
103    DH *dh;
104
105    if (nKeyLen == 512)
106        dh = get_dh512();
107    else if (nKeyLen == 1024)
108        dh = get_dh1024();
109    else
110        dh = get_dh1024();
111    return dh;
112}
113
114DH *ssl_dh_GetParamFromFile(char *file)
115{
116    DH *dh = NULL;
117    BIO *bio;
118
119    if ((bio = BIO_new_file(file, "r")) == NULL)
120        return NULL;
121#if SSL_LIBRARY_VERSION < 0x00904000
122    dh = PEM_read_bio_DHparams(bio, NULL, NULL);
123#else
124    dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
125#endif
126    BIO_free(bio);
127    return (dh);
128}
129
130/*
131=cut
132##
133##  Embedded Perl script for generating the temporary DH parameters
134##
135
136require 5.003;
137use strict;
138
139#   configuration
140my $file  = $0;
141my $begin = '----BEGIN GENERATED SECTION--------';
142my $end   = '----END GENERATED SECTION----------';
143
144#   read ourself and keep a backup
145open(FP, "<$file") || die;
146my $source = '';
147$source .= $_ while (<FP>);
148close(FP);
149open(FP, ">$file.bak") || die;
150print FP $source;
151close(FP);
152
153#   generate the DH parameters
154print "1. Generate 512 and 1024 bit Diffie-Hellman parameters (p, g)\n";
155my $rand = '';
156foreach $file (qw(/var/log/messages /var/adm/messages
157                  /kernel /vmunix /vmlinuz /etc/hosts /etc/resolv.conf)) {
158    if (-f $file) {
159        $rand = $file     if ($rand eq '');
160        $rand .= ":$file" if ($rand ne '');
161    }
162}
163$rand = "-rand $rand" if ($rand ne '');
164system("openssl gendh $rand -out dh512.pem 512");
165system("openssl gendh $rand -out dh1024.pem 1024");
166
167#   generate DH param info
168my $dhinfo = '';
169open(FP, "openssl dh -noout -text -in dh512.pem |") || die;
170$dhinfo .= $_ while (<FP>);
171close(FP);
172open(FP, "openssl dh -noout -text -in dh1024.pem |") || die;
173$dhinfo .= $_ while (<FP>);
174close(FP);
175$dhinfo =~ s|^|** |mg;
176$dhinfo = "\n\/\*\n$dhinfo\*\/\n\n";
177
178#   generate C source from DH params
179my $dhsource = '';
180open(FP, "openssl dh -noout -C -in dh512.pem | indent | expand |") || die;
181$dhsource .= $_ while (<FP>);
182close(FP);
183open(FP, "openssl dh -noout -C -in dh1024.pem | indent | expand |") || die;
184$dhsource .= $_ while (<FP>);
185close(FP);
186$dhsource =~ s|(DH\s+\*get_dh)(\d+)[^}]*\n}|static $1$2(void)
187{
188    return modssl_dh_configure(dh$2_p, sizeof(dh$2_p),
189                               dh$2_g, sizeof(dh$2_g));
190}
191|sg;
192
193#   generate output
194my $o = $dhinfo . $dhsource;
195
196#   insert the generated code at the target location
197$source =~ s|(\/\* $begin.+?\n).*\n(.*?\/\* $end)|$1$o$2|s;
198
199#   and update the source on disk
200print "Updating file `$file'\n";
201open(FP, ">$file") || die;
202print FP $source;
203close(FP);
204
205#   cleanup
206unlink("dh512.pem");
207unlink("dh1024.pem");
208
209=pod
210*/
211