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