andre@0: andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifdef FREEBL_NO_DEPEND andre@0: #include "stubs.h" andre@0: #endif andre@0: andre@0: #include "shsign.h" andre@0: #include "prlink.h" andre@0: #include "prio.h" andre@0: #include "blapi.h" andre@0: #include "seccomon.h" andre@0: #include "stdio.h" andre@0: #include "prmem.h" andre@0: #include "hasht.h" andre@0: #include "pqg.h" andre@0: andre@0: /* andre@0: * Most modern version of Linux support a speed optimization scheme where an andre@0: * application called prelink modifies programs and shared libraries to quickly andre@0: * load if they fit into an already designed address space. In short, prelink andre@0: * scans the list of programs and libraries on your system, assigns them a andre@0: * predefined space in the the address space, then provides the fixups to the andre@0: * library. andre@0: andre@0: * The modification of the shared library is correctly detected by the freebl andre@0: * FIPS checksum scheme where we check a signed hash of the library against the andre@0: * library itself. andre@0: * andre@0: * The prelink command itself can reverse the process of modification and andre@0: * output the prestine shared library as it was before prelink made it's andre@0: * changes. If FREEBL_USE_PRELINK is set Freebl uses prelink to output the andre@0: * original copy of the shared library before prelink modified it. andre@0: */ andre@0: #ifdef FREEBL_USE_PRELINK andre@0: #ifndef FREELB_PRELINK_COMMAND andre@0: #define FREEBL_PRELINK_COMMAND "/usr/sbin/prelink -u -o -" andre@0: #endif andre@0: #include "private/pprio.h" andre@0: andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: andre@0: /* andre@0: * This function returns an NSPR PRFileDesc * which the caller can read to andre@0: * obtain the prestine value of the shared library, before any OS related andre@0: * changes to it (usually address fixups). andre@0: * andre@0: * If prelink is installed, this andre@0: * file descriptor is a pipe connecting the output of andre@0: * /usr/sbin/prelink -u -o - {Library} andre@0: * and *pid returns the process id of the prelink child. andre@0: * andre@0: * If prelink is not installed, it returns a normal readonly handle to the andre@0: * library itself and *pid is set to '0'. andre@0: */ andre@0: PRFileDesc * andre@0: bl_OpenUnPrelink(const char *shName, int *pid) andre@0: { andre@0: char *command= strdup(FREEBL_PRELINK_COMMAND); andre@0: char *argString = NULL; andre@0: char **argv = NULL; andre@0: char *shNameArg = NULL; andre@0: char *cp; andre@0: pid_t child; andre@0: int argc = 0, argNext = 0; andre@0: struct stat statBuf; andre@0: int pipefd[2] = {-1,-1}; andre@0: int ret; andre@0: andre@0: *pid = 0; andre@0: andre@0: /* make sure the prelink command exists first. If not, fall back to andre@0: * just reading the file */ andre@0: for (cp = command; *cp ; cp++) { andre@0: if (*cp == ' ') { andre@0: *cp++ = 0; andre@0: argString = cp; andre@0: break; andre@0: } andre@0: } andre@0: memset (&statBuf, 0, sizeof(statBuf)); andre@0: /* stat the file, follow the link */ andre@0: ret = stat(command, &statBuf); andre@0: if (ret < 0) { andre@0: free(command); andre@0: return PR_Open(shName, PR_RDONLY, 0); andre@0: } andre@0: /* file exits, make sure it's an executable */ andre@0: if (!S_ISREG(statBuf.st_mode) || andre@0: ((statBuf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)) { andre@0: free(command); andre@0: return PR_Open(shName, PR_RDONLY, 0); andre@0: } andre@0: andre@0: /* OK, the prelink command exists and looks correct, use it */ andre@0: /* build the arglist while we can still malloc */ andre@0: /* count the args if any */ andre@0: if (argString && *argString) { andre@0: /* argString may have leading spaces, strip them off*/ andre@0: for (cp = argString; *cp && *cp == ' '; cp++); andre@0: argString = cp; andre@0: if (*cp) { andre@0: /* there is at least one arg.. */ andre@0: argc = 1; andre@0: } andre@0: andre@0: /* count the rest: Note there is no provision for escaped andre@0: * spaces here */ andre@0: for (cp = argString; *cp ; cp++) { andre@0: if (*cp == ' ') { andre@0: while (*cp && *cp == ' ') cp++; andre@0: if (*cp) argc++; andre@0: } andre@0: } andre@0: } andre@0: andre@0: /* add the additional args: argv[0] (command), shName, NULL*/ andre@0: argc += 3; andre@0: argv = PORT_NewArray(char *, argc); andre@0: if (argv == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* fill in the arglist */ andre@0: argv[argNext++] = command; andre@0: if (argString && *argString) { andre@0: argv[argNext++] = argString; andre@0: for (cp = argString; *cp; cp++) { andre@0: if (*cp == ' ') { andre@0: *cp++ = 0; andre@0: while (*cp && *cp == ' ') cp++; andre@0: if (*cp) argv[argNext++] = cp; andre@0: } andre@0: } andre@0: } andre@0: /* exec doesn't advertise taking const char **argv, do the paranoid andre@0: * copy */ andre@0: shNameArg = strdup(shName); andre@0: if (shNameArg == NULL) { andre@0: goto loser; andre@0: } andre@0: argv[argNext++] = shNameArg; andre@0: argv[argNext++] = 0; andre@0: andre@0: ret = pipe(pipefd); andre@0: if (ret < 0) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* use vfork() so we don't trigger the pthread_at_fork() handlers */ andre@0: child = vfork(); andre@0: if (child < 0) goto loser; andre@0: if (child == 0) { andre@0: /* set up the file descriptors */ andre@0: /* if we need to support BSD, this will need to be an open of andre@0: * /dev/null and dup2(nullFD, 0)*/ andre@0: close(0); andre@0: /* associate pipefd[1] with stdout */ andre@0: if (pipefd[1] != 1) dup2(pipefd[1], 1); andre@0: close(2); andre@0: close(pipefd[0]); andre@0: /* should probably close the other file descriptors? */ andre@0: andre@0: andre@0: execv(command, argv); andre@0: /* avoid at_exit() handlers */ andre@0: _exit(1); /* shouldn't reach here except on an error */ andre@0: } andre@0: close(pipefd[1]); andre@0: pipefd[1] = -1; andre@0: andre@0: /* this is safe because either vfork() as full fork() semantics, and thus andre@0: * already has it's own address space, or because vfork() has paused andre@0: * the parent util the exec or exit */ andre@0: free(command); andre@0: free(shNameArg); andre@0: PORT_Free(argv); andre@0: andre@0: *pid = child; andre@0: andre@0: return PR_ImportPipe(pipefd[0]); andre@0: andre@0: loser: andre@0: if (pipefd[0] != -1) { andre@0: close(pipefd[0]); andre@0: } andre@0: if (pipefd[1] != -1) { andre@0: close(pipefd[1]); andre@0: } andre@0: free(command); andre@0: free(shNameArg); andre@0: PORT_Free(argv); andre@0: andre@0: return NULL; andre@0: } andre@0: andre@0: /* andre@0: * bl_CloseUnPrelink - andre@0: * andre@0: * This closes the file descripter and reaps and children openned and crated by andre@0: * b;_OpenUnprelink. It's primary difference between it and just close is andre@0: * that it calls wait on the pid if one is supplied, preventing zombie children andre@0: * from hanging around. andre@0: */ andre@0: void andre@0: bl_CloseUnPrelink( PRFileDesc *file, int pid) andre@0: { andre@0: /* close the file descriptor */ andre@0: PR_Close(file); andre@0: /* reap the child */ andre@0: if (pid) { andre@0: waitpid(pid, NULL, 0); andre@0: } andre@0: } andre@0: #endif andre@0: andre@0: /* #define DEBUG_SHVERIFY 1 */ andre@0: andre@0: static char * andre@0: mkCheckFileName(const char *libName) andre@0: { andre@0: int ln_len = PORT_Strlen(libName); andre@0: char *output = PORT_Alloc(ln_len+sizeof(SGN_SUFFIX)); andre@0: int index = ln_len + 1 - sizeof("."SHLIB_SUFFIX); andre@0: andre@0: if ((index > 0) && andre@0: (PORT_Strncmp(&libName[index], andre@0: "."SHLIB_SUFFIX,sizeof("."SHLIB_SUFFIX)) == 0)) { andre@0: ln_len = index; andre@0: } andre@0: PORT_Memcpy(output,libName,ln_len); andre@0: PORT_Memcpy(&output[ln_len],SGN_SUFFIX,sizeof(SGN_SUFFIX)); andre@0: return output; andre@0: } andre@0: andre@0: static int andre@0: decodeInt(unsigned char *buf) andre@0: { andre@0: return (buf[3]) | (buf[2] << 8) | (buf[1] << 16) | (buf[0] << 24); andre@0: } andre@0: andre@0: static SECStatus andre@0: readItem(PRFileDesc *fd, SECItem *item) andre@0: { andre@0: unsigned char buf[4]; andre@0: int bytesRead; andre@0: andre@0: andre@0: bytesRead = PR_Read(fd, buf, 4); andre@0: if (bytesRead != 4) { andre@0: return SECFailure; andre@0: } andre@0: item->len = decodeInt(buf); andre@0: andre@0: item->data = PORT_Alloc(item->len); andre@0: if (item->data == NULL) { andre@0: item->len = 0; andre@0: return SECFailure; andre@0: } andre@0: bytesRead = PR_Read(fd, item->data, item->len); andre@0: if (bytesRead != item->len) { andre@0: PORT_Free(item->data); andre@0: item->data = NULL; andre@0: item->len = 0; andre@0: return SECFailure; andre@0: } andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* andre@0: * Define PSEUDO_FIPS if you can't do FIPS software integrity test (e.g., andre@0: * if you're using NSS as static libraries), but want to conform to the andre@0: * rest of the FIPS requirements. andre@0: */ andre@0: #ifdef NSS_STATIC andre@0: #define PSEUDO_FIPS andre@0: #endif andre@0: andre@0: PRBool andre@0: BLAPI_SHVerify(const char *name, PRFuncPtr addr) andre@0: { andre@0: #ifdef PSEUDO_FIPS andre@0: return PR_TRUE; /* a lie, hence *pseudo* FIPS */ andre@0: #else andre@0: PRBool result = PR_FALSE; /* if anything goes wrong, andre@0: * the signature does not verify */ andre@0: /* find our shared library name */ andre@0: char *shName = PR_GetLibraryFilePathname(name, addr); andre@0: if (!shName) { andre@0: goto loser; andre@0: } andre@0: result = BLAPI_SHVerifyFile(shName); andre@0: andre@0: loser: andre@0: if (shName != NULL) { andre@0: PR_Free(shName); andre@0: } andre@0: andre@0: return result; andre@0: #endif /* PSEUDO_FIPS */ andre@0: } andre@0: andre@0: PRBool andre@0: BLAPI_SHVerifyFile(const char *shName) andre@0: { andre@0: #ifdef PSEUDO_FIPS andre@0: return PR_TRUE; /* a lie, hence *pseudo* FIPS */ andre@0: #else andre@0: char *checkName = NULL; andre@0: PRFileDesc *checkFD = NULL; andre@0: PRFileDesc *shFD = NULL; andre@0: void *hashcx = NULL; andre@0: const SECHashObject *hashObj = NULL; andre@0: SECItem signature = { 0, NULL, 0 }; andre@0: SECItem hash; andre@0: int bytesRead, offset; andre@0: SECStatus rv; andre@0: DSAPublicKey key; andre@0: int count; andre@0: #ifdef FREEBL_USE_PRELINK andre@0: int pid = 0; andre@0: #endif andre@0: andre@0: PRBool result = PR_FALSE; /* if anything goes wrong, andre@0: * the signature does not verify */ andre@0: unsigned char buf[4096]; andre@0: unsigned char hashBuf[HASH_LENGTH_MAX]; andre@0: andre@0: PORT_Memset(&key,0,sizeof(key)); andre@0: hash.data = hashBuf; andre@0: hash.len = sizeof(hashBuf); andre@0: andre@0: if (!shName) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* figure out the name of our check file */ andre@0: checkName = mkCheckFileName(shName); andre@0: if (!checkName) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* open the check File */ andre@0: checkFD = PR_Open(checkName, PR_RDONLY, 0); andre@0: if (checkFD == NULL) { andre@0: #ifdef DEBUG_SHVERIFY andre@0: fprintf(stderr, "Failed to open the check file %s: (%d, %d)\n", andre@0: checkName, (int)PR_GetError(), (int)PR_GetOSError()); andre@0: #endif /* DEBUG_SHVERIFY */ andre@0: goto loser; andre@0: } andre@0: andre@0: /* read and Verify the headerthe header */ andre@0: bytesRead = PR_Read(checkFD, buf, 12); andre@0: if (bytesRead != 12) { andre@0: goto loser; andre@0: } andre@0: if ((buf[0] != NSS_SIGN_CHK_MAGIC1) || (buf[1] != NSS_SIGN_CHK_MAGIC2)) { andre@0: goto loser; andre@0: } andre@0: if ((buf[2] != NSS_SIGN_CHK_MAJOR_VERSION) || andre@0: (buf[3] < NSS_SIGN_CHK_MINOR_VERSION)) { andre@0: goto loser; andre@0: } andre@0: #ifdef notdef andre@0: if (decodeInt(&buf[8]) != CKK_DSA) { andre@0: goto loser; andre@0: } andre@0: #endif andre@0: andre@0: /* seek past any future header extensions */ andre@0: offset = decodeInt(&buf[4]); andre@0: PR_Seek(checkFD, offset, PR_SEEK_SET); andre@0: andre@0: /* read the key */ andre@0: rv = readItem(checkFD,&key.params.prime); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: rv = readItem(checkFD,&key.params.subPrime); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: rv = readItem(checkFD,&key.params.base); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: rv = readItem(checkFD,&key.publicValue); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: /* read the siganture */ andre@0: rv = readItem(checkFD,&signature); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* done with the check file */ andre@0: PR_Close(checkFD); andre@0: checkFD = NULL; andre@0: andre@0: hashObj = HASH_GetRawHashObject(PQG_GetHashType(&key.params)); andre@0: if (hashObj == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* open our library file */ andre@0: #ifdef FREEBL_USE_PRELINK andre@0: shFD = bl_OpenUnPrelink(shName,&pid); andre@0: #else andre@0: shFD = PR_Open(shName, PR_RDONLY, 0); andre@0: #endif andre@0: if (shFD == NULL) { andre@0: #ifdef DEBUG_SHVERIFY andre@0: fprintf(stderr, "Failed to open the library file %s: (%d, %d)\n", andre@0: shName, (int)PR_GetError(), (int)PR_GetOSError()); andre@0: #endif /* DEBUG_SHVERIFY */ andre@0: goto loser; andre@0: } andre@0: andre@0: /* hash our library file with SHA1 */ andre@0: hashcx = hashObj->create(); andre@0: if (hashcx == NULL) { andre@0: goto loser; andre@0: } andre@0: hashObj->begin(hashcx); andre@0: andre@0: count = 0; andre@0: while ((bytesRead = PR_Read(shFD, buf, sizeof(buf))) > 0) { andre@0: hashObj->update(hashcx, buf, bytesRead); andre@0: count += bytesRead; andre@0: } andre@0: #ifdef FREEBL_USE_PRELINK andre@0: bl_CloseUnPrelink(shFD, pid); andre@0: #else andre@0: PR_Close(shFD); andre@0: #endif andre@0: shFD = NULL; andre@0: andre@0: hashObj->end(hashcx, hash.data, &hash.len, hash.len); andre@0: andre@0: andre@0: /* verify the hash against the check file */ andre@0: if (DSA_VerifyDigest(&key, &signature, &hash) == SECSuccess) { andre@0: result = PR_TRUE; andre@0: } andre@0: #ifdef DEBUG_SHVERIFY andre@0: { andre@0: int i,j; andre@0: fprintf(stderr,"File %s: %d bytes\n",shName, count); andre@0: fprintf(stderr," hash: %d bytes\n", hash.len); andre@0: #define STEP 10 andre@0: for (i=0; i < hash.len; i += STEP) { andre@0: fprintf(stderr," "); andre@0: for (j=0; j < STEP && (i+j) < hash.len; j++) { andre@0: fprintf(stderr," %02x", hash.data[i+j]); andre@0: } andre@0: fprintf(stderr,"\n"); andre@0: } andre@0: fprintf(stderr," signature: %d bytes\n", signature.len); andre@0: for (i=0; i < signature.len; i += STEP) { andre@0: fprintf(stderr," "); andre@0: for (j=0; j < STEP && (i+j) < signature.len; j++) { andre@0: fprintf(stderr," %02x", signature.data[i+j]); andre@0: } andre@0: fprintf(stderr,"\n"); andre@0: } andre@0: fprintf(stderr,"Verified : %s\n",result?"TRUE": "FALSE"); andre@0: } andre@0: #endif /* DEBUG_SHVERIFY */ andre@0: andre@0: andre@0: loser: andre@0: if (checkName != NULL) { andre@0: PORT_Free(checkName); andre@0: } andre@0: if (checkFD != NULL) { andre@0: PR_Close(checkFD); andre@0: } andre@0: if (shFD != NULL) { andre@0: PR_Close(shFD); andre@0: } andre@0: if (hashcx != NULL) { andre@0: if (hashObj) { andre@0: hashObj->destroy(hashcx,PR_TRUE); andre@0: } andre@0: } andre@0: if (signature.data != NULL) { andre@0: PORT_Free(signature.data); andre@0: } andre@0: if (key.params.prime.data != NULL) { andre@0: PORT_Free(key.params.prime.data); andre@0: } andre@0: if (key.params.subPrime.data != NULL) { andre@0: PORT_Free(key.params.subPrime.data); andre@0: } andre@0: if (key.params.base.data != NULL) { andre@0: PORT_Free(key.params.base.data); andre@0: } andre@0: if (key.publicValue.data != NULL) { andre@0: PORT_Free(key.publicValue.data); andre@0: } andre@0: andre@0: return result; andre@0: #endif /* PSEUDO_FIPS */ andre@0: } andre@0: andre@0: PRBool andre@0: BLAPI_VerifySelf(const char *name) andre@0: { andre@0: if (name == NULL) { andre@0: /* andre@0: * If name is NULL, freebl is statically linked into softoken. andre@0: * softoken will call BLAPI_SHVerify next to verify itself. andre@0: */ andre@0: return PR_TRUE; andre@0: } andre@0: return BLAPI_SHVerify(name, (PRFuncPtr) decodeInt); andre@0: }