~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to ports/winnt/libntp/randfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-10-11 16:10:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041011161027-icyjbji8ujym633o
Tags: 1:4.2.0a-10ubuntu2
Use ntp.ubuntulinux.org instead of pool.ntp.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Make sure that there is a good source of random characters
 
3
 * so that OpenSSL can work properly and securely.
 
4
 */
 
5
 
 
6
/* Skip asynch rpc inclusion */
 
7
#ifndef __RPCASYNC_H__
 
8
#define __RPCASYNC_H__
 
9
#endif
 
10
 
 
11
#include <windows.h>
 
12
#include <wincrypt.h>
 
13
 
 
14
#include <stdio.h>
 
15
 
 
16
unsigned int    getrandom_chars(int desired, unsigned char *buf, int lenbuf);
 
17
BOOL            create_random_file(char *filename);
 
18
 
 
19
BOOL
 
20
init_randfile()
 
21
{
 
22
        FILE *rf;
 
23
        char *randfile;
 
24
        char *homedir;
 
25
        char tmp[256];
 
26
        /* See if the environmental variable RANDFILE is defined
 
27
         * and the file exists
 
28
         */
 
29
        randfile = getenv("RANDFILE");
 
30
        if (randfile != NULL) {
 
31
                rf = fopen(randfile, "rb");
 
32
                if (rf != NULL) {
 
33
                        fclose(rf);
 
34
                        return (TRUE);
 
35
                }
 
36
                else {
 
37
                        /* The environmental variable exists but not the file */
 
38
                        return (create_random_file(randfile));
 
39
                }
 
40
        }
 
41
        /*
 
42
         * If the RANDFILE environmental variable does not exist,
 
43
         * see if the HOME enviromental variable exists and
 
44
         * a .rnd file is in there.
 
45
         */
 
46
        homedir = getenv("HOME");
 
47
        if (homedir != NULL) {
 
48
                strcpy(tmp, homedir);
 
49
                strcat(tmp, "\\.rnd");
 
50
                rf = fopen(tmp, "rb");
 
51
                if (rf != NULL) {
 
52
                        fclose(rf);
 
53
                        return (TRUE);
 
54
                }
 
55
                else {
 
56
                        /* The HOME environmental variable exists but not the file */
 
57
                        return (create_random_file(tmp));
 
58
                }
 
59
        }
 
60
        /*
 
61
         * Final try. Look for it on the C:\ directory
 
62
         * NOTE: This is a really bad place for it security-wise
 
63
         * However, OpenSSL looks for it there if it can't find it elsewhere
 
64
         */
 
65
        rf = fopen("C:\\.rnd", "rb");
 
66
        if (rf != NULL) {
 
67
                fclose(rf);
 
68
                return (TRUE);
 
69
        }
 
70
        /* The file does not exist */
 
71
        return (create_random_file("C:\\.rnd"));
 
72
}
 
73
/*
 
74
 * Routine to create the random file with 1024 random characters
 
75
 */
 
76
BOOL
 
77
create_random_file(char *filename) {
 
78
        FILE *rf;
 
79
        int nchars;
 
80
        unsigned char buf[1025];
 
81
 
 
82
        nchars = getrandom_chars(1024, buf, sizeof(buf));
 
83
        rf = fopen(filename, "wb");
 
84
        if (rf == NULL)
 
85
                return (FALSE);
 
86
        fwrite(buf, sizeof(unsigned char), nchars, rf);
 
87
        fclose(rf);
 
88
        return (TRUE);
 
89
}
 
90
 
 
91
unsigned int
 
92
getrandom_chars(int desired, unsigned char *buf, int lenbuf) {
 
93
        HCRYPTPROV hcryptprov;
 
94
        BOOL err;
 
95
 
 
96
        if (buf == NULL || lenbuf <= 0 || desired > lenbuf)
 
97
                return (0);
 
98
        /*
 
99
         * The first time we just try to acquire the context
 
100
         */
 
101
        err = CryptAcquireContext(&hcryptprov, NULL, NULL, PROV_RSA_FULL,
 
102
                                  CRYPT_VERIFYCONTEXT);
 
103
        if (!err){
 
104
                return (0);
 
105
        }
 
106
        if (!CryptGenRandom(hcryptprov, desired, buf)) {
 
107
                CryptReleaseContext(hcryptprov, 0);
 
108
                return (0);
 
109
        }
 
110
 
 
111
        CryptReleaseContext(hcryptprov, 0);
 
112
        return (desired);
 
113
}
 
114