if ((newkey == NULL) || (!Wallet_Confirm(mismatch))) {
if (NS_FAILED(res) || (!Wallet_Confirm(mismatch))) {
Wallet_SetKey(PRBool isNewkey) {  if (Wallet_KeySet() && !isNewkey) {    return PR_TRUE;  }  Wallet_RestartKey();  char * newkey;  PRBool useDefaultKey = PR_FALSE;  if (Wallet_KeySize() < 0) { /* no key has yet been established */    PRUnichar * message = Wallet_Localize("firstPassword");    PRUnichar * message1 = Wallet_Localize("enterPassword");    PRUnichar * message2 = Wallet_Localize("confirmPassword");    PRUnichar * mismatch = Wallet_Localize("confirmFailed_TryAgain?");    PRBool matched;    for (;;) {      newkey = wallet_GetDoubleString(message, message1, message2, matched);      if ((newkey != NULL) && matched) {        break; /* break out of loop if both passwords matched */      }      /* password confirmation failed, ask user if he wants to try again */      if ((newkey == NULL) || (!Wallet_Confirm(mismatch))) {        Recycle(mismatch);        Recycle(message);        Recycle(message1);        Recycle(message2);        keyCancel = PR_TRUE;        return FALSE; /* user does not want to try again */      }        }    PR_FREEIF(mismatch);    PR_FREEIF(message);    Recycle(message1);    PR_FREEIF(message2);  } else { /* key has previously been established */    PRUnichar * message;    PRUnichar * message1 = Wallet_Localize("enterPassword");    PRUnichar * message2 = Wallet_Localize("confirmPassword");    PRUnichar * mismatch = Wallet_Localize("confirmFailed_TryAgain?");    PRBool matched;    if (isNewkey) { /* user is changing his key */      message = Wallet_Localize("newPassword");    } else {      message = Wallet_Localize("password");    }    if ((Wallet_KeySize() == 0) && !isNewkey) { /* prev-established key is default key */      useDefaultKey = PR_TRUE;      newkey = PL_strdup("~");    } else { /* ask the user for his key */      if (isNewkey) { /* user is changing his password */        for (;;) {          newkey = wallet_GetDoubleString(message, message1, message2, matched);          if ((newkey != NULL) && matched) {            break; /* break out of loop if both passwords matched */          }          /* password confirmation failed, ask user if he wants to try again */          if ((newkey == NULL) || (!Wallet_Confirm(mismatch))) {            Recycle(mismatch);            Recycle(message);            Recycle(message1);            Recycle(message2);            keyCancel = PR_TRUE;            return FALSE; /* user does not want to try again */          }            }      } else {        newkey = wallet_GetString(message, message1);      }      if (newkey == NULL) {        Recycle(message);        Recycle(message1);        Recycle(message2);        Recycle(mismatch);        keyCancel = PR_TRUE;        return PR_FALSE; /* user pressed cancel -- does not want to enter a new key */      }    }    Recycle(message);    Recycle(message1);    Recycle(message2);    Recycle(mismatch);  }  if (PL_strlen(newkey) == 0) { /* user entered a zero-length key */    if ((Wallet_KeySize() < 0) || isNewkey ){      /* no key file existed before or using is changing the key */      useDefaultKey = PR_TRUE;      PR_FREEIF(newkey);      newkey  = PL_strdup("~"); /* use zero-length key */    }  }  for (; (keyPosition < PL_strlen(newkey) && keyPosition < maxKeySize); keyPosition++) {    key[keyPosition] = newkey[keyPosition];  }  key[keyPosition] = '\0';  PR_FREEIF(newkey);  Wallet_RestartKey();  /* verify this with the saved key */  if (isNewkey || (Wallet_KeySize() < 0)) {    /*     * Either key is to be changed or the file containing the saved key doesn' exist.     * In either case we need to (re)create and re(write) the file.     */    nsFileSpec dirSpec;    nsresult rval = Wallet_ProfileDirectory(dirSpec);    if (NS_FAILED(rval)) {      keyCancel = PR_TRUE;      return PR_FALSE;    }    nsOutputFileStream strm2(dirSpec + keyFileName);    if (!strm2.is_open()) {      *key = '\0';      keyCancel = PR_TRUE;      return PR_FALSE;    }    /* If we store the key obscured by the key itself, then the result will be zero     * for all keys (since we are using XOR to obscure).  So instead we store     * key[1..n],key[0] obscured by the actual key.     */    if (!useDefaultKey && (PL_strlen(key) != 0)) {      char* p = key+1;      while (*p) {        strm2.put(*(p++)^Wallet_GetKey());      }      strm2.put((*key)^Wallet_GetKey());    }    strm2.flush();    strm2.close();    Wallet_RestartKey();    keySet = PR_TRUE;    return PR_TRUE;  } else {    /* file of saved key existed so see if it matches the key the user typed in */    /*     * Note that eof() is not set until after we read past the end of the file.  That     * is why the following code reads a character and immediately after the read     * checks for eof()     */    /* test for a null key */    if (useDefaultKey && (Wallet_KeySize() == 0) ) {      Wallet_RestartKey();      keySet = PR_TRUE;      return PR_TRUE;    }    nsFileSpec dirSpec;    nsresult rval = Wallet_ProfileDirectory(dirSpec);    if (NS_FAILED(rval)) {      keyCancel = PR_TRUE;      return PR_FALSE;    }    nsInputFileStream strm(dirSpec + keyFileName);    Wallet_RestartKey();    char* p = key+1;    while (*p) {      if (strm.get() != (*(p++)^Wallet_GetKey()) || strm.eof()) {        strm.close();        *key = '\0';        keyCancel = PR_FALSE;        return PR_FALSE;      }    }    if (strm.get() != ((*key)^Wallet_GetKey()) || strm.eof()) {      strm.close();      *key = '\0';      keyCancel = PR_FALSE;      return PR_FALSE;    }    strm.get(); /* to get past the end of the file so eof() will get set */    PRBool rv = strm.eof();    strm.close();    if (rv) {      Wallet_RestartKey();      keySet = PR_TRUE;      keyExpiresTime = time(NULL) + keyDuration;      return PR_TRUE;    } else {      *key = '\0';      keyCancel = PR_TRUE;      return PR_FALSE;    }  }}