Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #if defined(XP_WIN) |
michael@0 | 7 | #include <windows.h> |
michael@0 | 8 | #include <objbase.h> |
michael@0 | 9 | #elif defined(XP_MACOSX) |
michael@0 | 10 | #include <CoreFoundation/CoreFoundation.h> |
michael@0 | 11 | #else |
michael@0 | 12 | #include <stdlib.h> |
michael@0 | 13 | #include "prrng.h" |
michael@0 | 14 | #endif |
michael@0 | 15 | |
michael@0 | 16 | #include "nsUUIDGenerator.h" |
michael@0 | 17 | |
michael@0 | 18 | using namespace mozilla; |
michael@0 | 19 | |
michael@0 | 20 | NS_IMPL_ISUPPORTS(nsUUIDGenerator, nsIUUIDGenerator) |
michael@0 | 21 | |
michael@0 | 22 | nsUUIDGenerator::nsUUIDGenerator() |
michael@0 | 23 | : mLock("nsUUIDGenerator.mLock") |
michael@0 | 24 | { |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | nsUUIDGenerator::~nsUUIDGenerator() |
michael@0 | 28 | { |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | nsresult |
michael@0 | 32 | nsUUIDGenerator::Init() |
michael@0 | 33 | { |
michael@0 | 34 | // We're a service, so we're guaranteed that Init() is not going |
michael@0 | 35 | // to be reentered while we're inside Init(). |
michael@0 | 36 | |
michael@0 | 37 | #if !defined(XP_WIN) && !defined(XP_MACOSX) && !defined(ANDROID) |
michael@0 | 38 | /* initialize random number generator using NSPR random noise */ |
michael@0 | 39 | unsigned int seed; |
michael@0 | 40 | |
michael@0 | 41 | size_t bytes = 0; |
michael@0 | 42 | while (bytes < sizeof(seed)) { |
michael@0 | 43 | size_t nbytes = PR_GetRandomNoise(((unsigned char *)&seed)+bytes, |
michael@0 | 44 | sizeof(seed)-bytes); |
michael@0 | 45 | if (nbytes == 0) { |
michael@0 | 46 | return NS_ERROR_FAILURE; |
michael@0 | 47 | } |
michael@0 | 48 | bytes += nbytes; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | /* Initialize a new RNG state, and immediately switch |
michael@0 | 52 | * back to the previous one -- we want to use mState |
michael@0 | 53 | * only for our own calls to random(). |
michael@0 | 54 | */ |
michael@0 | 55 | mSavedState = initstate(seed, mState, sizeof(mState)); |
michael@0 | 56 | setstate(mSavedState); |
michael@0 | 57 | |
michael@0 | 58 | mRBytes = 4; |
michael@0 | 59 | #ifdef RAND_MAX |
michael@0 | 60 | if ((unsigned long) RAND_MAX < (unsigned long)0xffffffff) |
michael@0 | 61 | mRBytes = 3; |
michael@0 | 62 | if ((unsigned long) RAND_MAX < (unsigned long)0x00ffffff) |
michael@0 | 63 | mRBytes = 2; |
michael@0 | 64 | if ((unsigned long) RAND_MAX < (unsigned long)0x0000ffff) |
michael@0 | 65 | mRBytes = 1; |
michael@0 | 66 | if ((unsigned long) RAND_MAX < (unsigned long)0x000000ff) |
michael@0 | 67 | return NS_ERROR_FAILURE; |
michael@0 | 68 | #endif |
michael@0 | 69 | |
michael@0 | 70 | #endif /* non XP_WIN and non XP_MACOSX */ |
michael@0 | 71 | |
michael@0 | 72 | return NS_OK; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | NS_IMETHODIMP |
michael@0 | 76 | nsUUIDGenerator::GenerateUUID(nsID** ret) |
michael@0 | 77 | { |
michael@0 | 78 | nsID *id = static_cast<nsID*>(NS_Alloc(sizeof(nsID))); |
michael@0 | 79 | if (id == nullptr) |
michael@0 | 80 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 81 | |
michael@0 | 82 | nsresult rv = GenerateUUIDInPlace(id); |
michael@0 | 83 | if (NS_FAILED(rv)) { |
michael@0 | 84 | NS_Free(id); |
michael@0 | 85 | return rv; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | *ret = id; |
michael@0 | 89 | return rv; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_IMETHODIMP |
michael@0 | 93 | nsUUIDGenerator::GenerateUUIDInPlace(nsID* id) |
michael@0 | 94 | { |
michael@0 | 95 | // The various code in this method is probably not threadsafe, so lock |
michael@0 | 96 | // across the whole method. |
michael@0 | 97 | MutexAutoLock lock(mLock); |
michael@0 | 98 | |
michael@0 | 99 | #if defined(XP_WIN) |
michael@0 | 100 | HRESULT hr = CoCreateGuid((GUID*)id); |
michael@0 | 101 | if (FAILED(hr)) |
michael@0 | 102 | return NS_ERROR_FAILURE; |
michael@0 | 103 | #elif defined(XP_MACOSX) |
michael@0 | 104 | CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); |
michael@0 | 105 | if (!uuid) |
michael@0 | 106 | return NS_ERROR_FAILURE; |
michael@0 | 107 | |
michael@0 | 108 | CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid); |
michael@0 | 109 | memcpy(id, &bytes, sizeof(nsID)); |
michael@0 | 110 | |
michael@0 | 111 | CFRelease(uuid); |
michael@0 | 112 | #else /* not windows or OS X; generate randomness using random(). */ |
michael@0 | 113 | /* XXX we should be saving the return of setstate here and switching |
michael@0 | 114 | * back to it; instead, we use the value returned when we called |
michael@0 | 115 | * initstate, since older glibc's have broken setstate() return values |
michael@0 | 116 | */ |
michael@0 | 117 | #ifndef ANDROID |
michael@0 | 118 | setstate(mState); |
michael@0 | 119 | #endif |
michael@0 | 120 | |
michael@0 | 121 | size_t bytesLeft = sizeof(nsID); |
michael@0 | 122 | while (bytesLeft > 0) { |
michael@0 | 123 | #ifdef ANDROID |
michael@0 | 124 | long rval = arc4random(); |
michael@0 | 125 | const size_t mRBytes = 4; |
michael@0 | 126 | #else |
michael@0 | 127 | long rval = random(); |
michael@0 | 128 | #endif |
michael@0 | 129 | |
michael@0 | 130 | |
michael@0 | 131 | uint8_t *src = (uint8_t*)&rval; |
michael@0 | 132 | // We want to grab the mRBytes least significant bytes of rval, since |
michael@0 | 133 | // mRBytes less than sizeof(rval) means the high bytes are 0. |
michael@0 | 134 | #ifdef IS_BIG_ENDIAN |
michael@0 | 135 | src += sizeof(rval) - mRBytes; |
michael@0 | 136 | #endif |
michael@0 | 137 | uint8_t *dst = ((uint8_t*) id) + (sizeof(nsID) - bytesLeft); |
michael@0 | 138 | size_t toWrite = (bytesLeft < mRBytes ? bytesLeft : mRBytes); |
michael@0 | 139 | for (size_t i = 0; i < toWrite; i++) |
michael@0 | 140 | dst[i] = src[i]; |
michael@0 | 141 | |
michael@0 | 142 | bytesLeft -= toWrite; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | /* Put in the version */ |
michael@0 | 146 | id->m2 &= 0x0fff; |
michael@0 | 147 | id->m2 |= 0x4000; |
michael@0 | 148 | |
michael@0 | 149 | /* Put in the variant */ |
michael@0 | 150 | id->m3[0] &= 0x3f; |
michael@0 | 151 | id->m3[0] |= 0x80; |
michael@0 | 152 | |
michael@0 | 153 | #ifndef ANDROID |
michael@0 | 154 | /* Restore the previous RNG state */ |
michael@0 | 155 | setstate(mSavedState); |
michael@0 | 156 | #endif |
michael@0 | 157 | #endif |
michael@0 | 158 | |
michael@0 | 159 | return NS_OK; |
michael@0 | 160 | } |