toolkit/components/downloads/SQLFunctions.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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 #include "mozilla/storage.h"
michael@0 7 #include "mozilla/storage/Variant.h"
michael@0 8 #include "mozilla/mozalloc.h"
michael@0 9 #include "nsString.h"
michael@0 10 #include "SQLFunctions.h"
michael@0 11 #include "nsUTF8Utils.h"
michael@0 12 #include "plbase64.h"
michael@0 13 #include "prio.h"
michael@0 14
michael@0 15 // The length of guids that are used by the download manager
michael@0 16 #define GUID_LENGTH 12
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19 namespace downloads {
michael@0 20
michael@0 21 // Keep this file in sync with the GUID-related code in toolkit/places/SQLFunctions.cpp
michael@0 22 // and toolkit/places/Helpers.cpp!
michael@0 23
michael@0 24 ////////////////////////////////////////////////////////////////////////////////
michael@0 25 //// GUID Creation Function
michael@0 26
michael@0 27 //////////////////////////////////////////////////////////////////////////////
michael@0 28 //// GenerateGUIDFunction
michael@0 29
michael@0 30 /* static */
michael@0 31 nsresult
michael@0 32 GenerateGUIDFunction::create(mozIStorageConnection *aDBConn)
michael@0 33 {
michael@0 34 nsRefPtr<GenerateGUIDFunction> function = new GenerateGUIDFunction();
michael@0 35 nsresult rv = aDBConn->CreateFunction(
michael@0 36 NS_LITERAL_CSTRING("generate_guid"), 0, function
michael@0 37 );
michael@0 38 NS_ENSURE_SUCCESS(rv, rv);
michael@0 39
michael@0 40 return NS_OK;
michael@0 41 }
michael@0 42
michael@0 43 NS_IMPL_ISUPPORTS(
michael@0 44 GenerateGUIDFunction,
michael@0 45 mozIStorageFunction
michael@0 46 )
michael@0 47
michael@0 48 static
michael@0 49 nsresult
michael@0 50 Base64urlEncode(const uint8_t* aBytes,
michael@0 51 uint32_t aNumBytes,
michael@0 52 nsCString& _result)
michael@0 53 {
michael@0 54 // SetLength does not set aside space for null termination. PL_Base64Encode
michael@0 55 // will not null terminate, however, nsCStrings must be null terminated. As a
michael@0 56 // result, we set the capacity to be one greater than what we need, and the
michael@0 57 // length to our desired length.
michael@0 58 uint32_t length = (aNumBytes + 2) / 3 * 4; // +2 due to integer math.
michael@0 59 NS_ENSURE_TRUE(_result.SetCapacity(length + 1, mozilla::fallible_t()),
michael@0 60 NS_ERROR_OUT_OF_MEMORY);
michael@0 61 _result.SetLength(length);
michael@0 62 (void)PL_Base64Encode(reinterpret_cast<const char*>(aBytes), aNumBytes,
michael@0 63 _result.BeginWriting());
michael@0 64
michael@0 65 // base64url encoding is defined in RFC 4648. It replaces the last two
michael@0 66 // alphabet characters of base64 encoding with '-' and '_' respectively.
michael@0 67 _result.ReplaceChar('+', '-');
michael@0 68 _result.ReplaceChar('/', '_');
michael@0 69 return NS_OK;
michael@0 70 }
michael@0 71
michael@0 72 #ifdef XP_WIN
michael@0 73 // Included here because windows.h conflicts with the use of mozIStorageError
michael@0 74 // above.
michael@0 75 #include <windows.h>
michael@0 76 #include <wincrypt.h>
michael@0 77 #endif
michael@0 78
michael@0 79 static
michael@0 80 nsresult
michael@0 81 GenerateRandomBytes(uint32_t aSize,
michael@0 82 uint8_t* _buffer)
michael@0 83 {
michael@0 84 // On Windows, we'll use its built-in cryptographic API.
michael@0 85 #if defined(XP_WIN)
michael@0 86 HCRYPTPROV cryptoProvider;
michael@0 87 BOOL rc = CryptAcquireContext(&cryptoProvider, 0, 0, PROV_RSA_FULL,
michael@0 88 CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
michael@0 89 if (rc) {
michael@0 90 rc = CryptGenRandom(cryptoProvider, aSize, _buffer);
michael@0 91 (void)CryptReleaseContext(cryptoProvider, 0);
michael@0 92 }
michael@0 93 return rc ? NS_OK : NS_ERROR_FAILURE;
michael@0 94
michael@0 95 // On Unix, we'll just read in from /dev/urandom.
michael@0 96 #elif defined(XP_UNIX)
michael@0 97 NS_ENSURE_ARG_MAX(aSize, INT32_MAX);
michael@0 98 PRFileDesc* urandom = PR_Open("/dev/urandom", PR_RDONLY, 0);
michael@0 99 nsresult rv = NS_ERROR_FAILURE;
michael@0 100 if (urandom) {
michael@0 101 int32_t bytesRead = PR_Read(urandom, _buffer, aSize);
michael@0 102 if (bytesRead == static_cast<int32_t>(aSize)) {
michael@0 103 rv = NS_OK;
michael@0 104 }
michael@0 105 (void)PR_Close(urandom);
michael@0 106 }
michael@0 107 return rv;
michael@0 108 #endif
michael@0 109 }
michael@0 110
michael@0 111 nsresult
michael@0 112 GenerateGUID(nsCString& _guid)
michael@0 113 {
michael@0 114 _guid.Truncate();
michael@0 115
michael@0 116 // Request raw random bytes and base64url encode them. For each set of three
michael@0 117 // bytes, we get one character.
michael@0 118 const uint32_t kRequiredBytesLength =
michael@0 119 static_cast<uint32_t>(GUID_LENGTH / 4 * 3);
michael@0 120
michael@0 121 uint8_t buffer[kRequiredBytesLength];
michael@0 122 nsresult rv = GenerateRandomBytes(kRequiredBytesLength, buffer);
michael@0 123 NS_ENSURE_SUCCESS(rv, rv);
michael@0 124
michael@0 125 rv = Base64urlEncode(buffer, kRequiredBytesLength, _guid);
michael@0 126 NS_ENSURE_SUCCESS(rv, rv);
michael@0 127
michael@0 128 NS_ASSERTION(_guid.Length() == GUID_LENGTH, "GUID is not the right size!");
michael@0 129 return NS_OK;
michael@0 130 }
michael@0 131
michael@0 132 //////////////////////////////////////////////////////////////////////////////
michael@0 133 //// mozIStorageFunction
michael@0 134
michael@0 135 NS_IMETHODIMP
michael@0 136 GenerateGUIDFunction::OnFunctionCall(mozIStorageValueArray *aArguments,
michael@0 137 nsIVariant **_result)
michael@0 138 {
michael@0 139 nsAutoCString guid;
michael@0 140 nsresult rv = GenerateGUID(guid);
michael@0 141 NS_ENSURE_SUCCESS(rv, rv);
michael@0 142
michael@0 143 NS_ADDREF(*_result = new mozilla::storage::UTF8TextVariant(guid));
michael@0 144 return NS_OK;
michael@0 145 }
michael@0 146
michael@0 147 } // namespace mozilla
michael@0 148 } // namespace downloads

mercurial