upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / misc / win32 / rand.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <windows.h>
18 #include <wincrypt.h>
19 #include "apr.h"
20 #include "apr_private.h"
21 #include "apr_general.h"
22 #include "apr_portable.h"
23 #include "apr_arch_misc.h"
24
25
26 APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char * buf,
27 #ifdef APR_ENABLE_FOR_1_0
28                                                     apr_size_t length)
29 #else
30                                                     int length)
31 #endif
32 {
33     HCRYPTPROV hProv;
34     apr_status_t res = APR_SUCCESS;
35
36     /* 0x40 bit = CRYPT_SILENT, only introduced in more recent PSDKs 
37      * and will only work for Win2K and later.
38      */
39     DWORD flags = CRYPT_VERIFYCONTEXT
40                 | ((apr_os_level >= APR_WIN_2000) ? 0x40 : 0);
41
42     if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, flags)) {
43         return apr_get_os_error();
44     }
45     if (!CryptGenRandom(hProv, length, buf)) {
46         res = apr_get_os_error();
47     }
48     CryptReleaseContext(hProv, 0);
49     return res;
50 }
51
52
53 APR_DECLARE(apr_status_t) apr_os_uuid_get(unsigned char *uuid_data)
54 {
55     /* Note: this call doesn't actually require CoInitialize() first 
56      *
57      * XXX: we should scramble the bytes or some such to eliminate the
58      * possible misuse/abuse since uuid is based on the NIC address, and
59      * is therefore not only a uniqifier, but an identity (which might not
60      * be appropriate in all cases.
61      *
62      * Note that Win2000, XP and later no longer suffer from this problem,
63      * a scrambling fix is only needed for (apr_os_level < APR_WIN_2000)
64      */
65     if (FAILED(UuidCreate((UUID *)uuid_data))) {
66         return APR_EGENERAL;
67     }
68     return APR_SUCCESS;
69 }