Enable tls psk
[libradsec.git] / radius / custom.c
1 /*
2 Copyright (c) 2011, Network RADIUS SARL
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of the <organization> nor the
13       names of its contributors may be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 /*
28  * Copyright (c) 2006 Kungliga Tekniska HAÎåÎÝgskolan
29  * (Royal Institute of Technology, Stockholm, Sweden).
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  *
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  *
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  *
43  * 3. Neither the name of the Institute nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59
60 /** \file custom.c
61  *  \brief Functions which should be customized for your local system.
62  */
63
64 #include "client.h"
65
66 #include        <unistd.h>
67 #include        <fcntl.h>
68
69 #ifdef WIN32
70 #include <wincrypt.h>
71
72 volatile static HCRYPTPROV nr_cryptprovider = 0;
73
74 static HCRYPTPROV
75 nr_CryptProvider(void)
76 {
77     BOOL rv;
78     HCRYPTPROV cryptprovider = 0;
79
80     if (nr_cryptprovider != 0)
81         return nr_cryptprovider;
82
83     rv = CryptAcquireContext(&cryptprovider, NULL,
84                               MS_ENHANCED_PROV, PROV_RSA_FULL,
85                               0);
86
87     if (GetLastError() == NTE_BAD_KEYSET) {
88         if(!rv)
89             rv = CryptAcquireContext(&cryptprovider, NULL,
90                                       MS_ENHANCED_PROV, PROV_RSA_FULL,
91                                       CRYPT_NEWKEYSET);
92     }
93
94     if (rv &&
95         InterlockedCompareExchangePointer((PVOID *) &nr_cryptprovider,
96                                           (PVOID) cryptprovider, 0) != 0) {
97
98         CryptReleaseContext(cryptprovider, 0);
99         cryptprovider = nr_cryptprovider;
100     }
101
102     return cryptprovider;
103 }
104
105 ssize_t nr_rand_bytes(uint8_t *data, size_t data_len)
106 {
107         if (CryptGenRandom(nr_CryptProvider(), data_len, data))
108                 return 0;
109         return data_len;
110 }
111 #else
112 ssize_t nr_rand_bytes(uint8_t *data, size_t data_len)
113 {
114         static int fd = -1;
115         
116         if (fd < 0) {
117                 fd = open("/dev/urandom", O_RDONLY);
118                 if (fd < 0) {
119                         nr_strerror_printf("Error opening randomness: %s",
120                                            strerror(errno));
121                         return 0;
122                 }
123         }
124
125         return read(fd, data, data_len);
126 }
127 #endif /* WIN32 */
128
129 uint32_t nr_rand(void)
130 {
131         uint32_t lvalue;
132
133         nr_rand_bytes((void *)&lvalue, sizeof(lvalue));
134         return lvalue;
135 }
136
137
138 #ifndef USEC
139 #define USEC (1000000)
140 #endif
141
142 void nr_timeval_add(struct timeval *t, unsigned int seconds, unsigned int usec)
143 {
144         t->tv_sec += seconds;
145         t->tv_sec += usec / USEC;
146         t->tv_usec += usec % USEC;
147         if (t->tv_usec > USEC) {
148                 t->tv_sec++;
149                 t->tv_usec -= USEC;
150         }
151 }
152
153 int nr_timeval_cmp(const struct timeval *a, const struct timeval *b)
154 {
155         if (a->tv_sec > b->tv_sec) return +1;
156         if (a->tv_sec < b->tv_sec) return -1;
157
158         if (a->tv_usec > b->tv_usec) return +1;
159         if (a->tv_usec < b->tv_usec) return -1;
160
161         return 0;
162 }
163