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