~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/misc/win32/rand.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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 "apr.h"
 
18
#include "apr_private.h"
 
19
#include "apr_general.h"
 
20
#include "apr_portable.h"
 
21
#include "apr_arch_misc.h"
 
22
#include <wincrypt.h>
 
23
 
 
24
 
 
25
APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char * buf,
 
26
                                                    apr_size_t length)
 
27
{
 
28
    HCRYPTPROV hProv;
 
29
    apr_status_t res = APR_SUCCESS;
 
30
 
 
31
    /* 0x40 bit = CRYPT_SILENT, only introduced in more recent PSDKs 
 
32
     * and will only work for Win2K and later.
 
33
     */
 
34
    DWORD flags = CRYPT_VERIFYCONTEXT
 
35
                | ((apr_os_level >= APR_WIN_2000) ? 0x40 : 0);
 
36
 
 
37
    if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, flags)) {
 
38
        return apr_get_os_error();
 
39
    }
 
40
    /* XXX: An ugly hack for Win64, randomness is such that noone should
 
41
     * ever expect > 2^31 bytes of data at once without the prng
 
42
     * coming to a complete halt.
 
43
     */
 
44
    if (!CryptGenRandom(hProv, (DWORD)length, buf)) {
 
45
        res = apr_get_os_error();
 
46
    }
 
47
    CryptReleaseContext(hProv, 0);
 
48
    return res;
 
49
}
 
50
 
 
51
 
 
52
APR_DECLARE(apr_status_t) apr_os_uuid_get(unsigned char *uuid_data)
 
53
{
 
54
    /* Note: this call doesn't actually require CoInitialize() first 
 
55
     *
 
56
     * XXX: we should scramble the bytes or some such to eliminate the
 
57
     * possible misuse/abuse since uuid is based on the NIC address, and
 
58
     * is therefore not only a uniqifier, but an identity (which might not
 
59
     * be appropriate in all cases.
 
60
     *
 
61
     * Note that Win2000, XP and later no longer suffer from this problem,
 
62
     * a scrambling fix is only needed for (apr_os_level < APR_WIN_2000)
 
63
     */
 
64
    if (FAILED(UuidCreate((UUID *)uuid_data))) {
 
65
        return APR_EGENERAL;
 
66
    }
 
67
    return APR_SUCCESS;
 
68
}