tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

lgfips.c (3435B)


      1 /*
      2 * PKCS #11 FIPS Power-Up Self Test.
      3 *
      4 * This Source Code Form is subject to the terms of the Mozilla Public
      5 * License, v. 2.0. If a copy of the MPL was not distributed with this
      6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      7 /* $Id: fipstest.c,v 1.31 2012/06/28 17:55:06 rrelyea%redhat.com Exp $ */
      8 
      9 #ifndef NSS_FIPS_DISABLED
     10 
     11 #include "seccomon.h"
     12 #include "lgdb.h"
     13 #include "blapi.h"
     14 
     15 /*
     16 * different platforms have different ways of calling and initial entry point
     17 * when the dll/.so is loaded. Most platforms support either a posix pragma
     18 * or the GCC attribute. Some platforms suppor a pre-defined name, and some
     19 * platforms have a link line way of invoking this function.
     20 */
     21 
     22 /* The pragma */
     23 #if defined(USE_INIT_PRAGMA)
     24 #pragma init(lg_startup_tests)
     25 #endif
     26 
     27 /* GCC Attribute */
     28 #if defined(__GNUC__) && !defined(NSS_NO_INIT_SUPPORT)
     29 #define INIT_FUNCTION __attribute__((constructor))
     30 #else
     31 #define INIT_FUNCTION
     32 #endif
     33 
     34 static void INIT_FUNCTION lg_startup_tests(void);
     35 
     36 /* Windows pre-defined entry */
     37 #if defined(XP_WIN) && !defined(NSS_NO_INIT_SUPPORT)
     38 #include <windows.h>
     39 
     40 BOOL WINAPI
     41 DllMain(
     42    HINSTANCE hinstDLL, // handle to DLL module
     43    DWORD fdwReason,    // reason for calling function
     44    LPVOID lpReserved)  // reserved
     45 {
     46    // Perform actions based on the reason for calling.
     47    switch (fdwReason) {
     48        case DLL_PROCESS_ATTACH:
     49            // Initialize once for each new process.
     50            // Return FALSE to fail DLL load.
     51            lg_startup_tests();
     52            break;
     53 
     54        case DLL_THREAD_ATTACH:
     55            // Do thread-specific initialization.
     56            break;
     57 
     58        case DLL_THREAD_DETACH:
     59            // Do thread-specific cleanup.
     60            break;
     61 
     62        case DLL_PROCESS_DETACH:
     63            // Perform any necessary cleanup.
     64            break;
     65    }
     66    return TRUE; // Successful DLL_PROCESS_ATTACH.
     67 }
     68 #endif
     69 
     70 static PRBool lg_self_tests_ran = PR_FALSE;
     71 static PRBool lg_self_tests_success = PR_FALSE;
     72 
     73 static void
     74 lg_local_function(void)
     75 {
     76 }
     77 
     78 /*
     79 * This function is called at dll load time, the code tha makes this
     80 * happen is platform specific on defined above.
     81 */
     82 static void
     83 lg_startup_tests(void)
     84 {
     85    const char *libraryName = LG_LIB_NAME;
     86 
     87    PORT_Assert(!lg_self_tests_ran);
     88    PORT_Assert(!lg_self_tests_success);
     89    lg_self_tests_ran = PR_TRUE;
     90    lg_self_tests_success = PR_FALSE; /* just in case */
     91 
     92    /* no self tests required for the legacy db, only the integrity check */
     93    /* check the integrity of our shared library */
     94    if (!BLAPI_SHVerify(libraryName, (PRFuncPtr)&lg_local_function)) {
     95        /* something is wrong with the library, fail without enabling
     96         * the fips token */
     97        return;
     98    }
     99    /* FIPS product has been installed and is functioning, allow
    100     * the module to operate in fips mode */
    101    lg_self_tests_success = PR_TRUE;
    102 }
    103 
    104 PRBool
    105 lg_FIPSEntryOK()
    106 {
    107 #ifdef NSS_NO_INIT_SUPPORT
    108    /* this should only be set on platforms that can't handle one of the INIT
    109     * schemes.  This code allows those platforms to continue to function,
    110     * though they don't meet the strict NIST requirements. If NO_INIT_SUPPORT
    111     * is not set, and init support has not been properly enabled, softken
    112     * will always fail because of the test below */
    113    if (!lg_self_tests_ran) {
    114        lg_startup_tests();
    115    }
    116 #endif
    117    return lg_self_tests_success;
    118 }
    119 
    120 #endif /* NSS_FIPS_DISABLED */