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