int GetKeyFilename(int config, char *keyfilename, unsigned int keyfilenamesize, int *keyfile_format) { FILE *fp; char line[256]; int found_key=0; int found_pkcs12=0; char configfile_path[MAX_PATH]; strncpy(configfile_path, o.cnn[config].config_dir, sizeof(configfile_path)); if (!(configfile_path[strlen(configfile_path)-1] == '\\')) strcat(configfile_path, "\\"); strncat(configfile_path, o.cnn[config].config_file, sizeof(configfile_path) - strlen(configfile_path) - 1); if (!(fp=fopen(configfile_path, "r"))) { /* can't open config file */ ShowLocalizedMsg(GUI_NAME, ERR_OPEN_CONFIG, configfile_path); return(0); } while (fgets(line, sizeof (line), fp)) { if (LineBeginsWith(line, "key", 3)) { if (found_key) { /* only one key option */ ShowLocalizedMsg(GUI_NAME, ERR_ONLY_ONE_KEY_OPTION, ""); return(0); } if (found_pkcs12) { /* key XOR pkcs12 */ ShowLocalizedMsg(GUI_NAME, ERR_ONLY_KEY_OR_PKCS12, ""); return(0); } found_key=1; *keyfile_format = KEYFILE_FORMAT_PEM; if (!ParseKeyFilenameLine(config, keyfilename, keyfilenamesize, &line[4])) return(0); } if (LineBeginsWith(line, "pkcs12", 6)) { if (found_pkcs12) { /* only one pkcs12 option */ ShowLocalizedMsg(GUI_NAME, ERR_ONLY_ONE_PKCS12_OPTION, ""); return(0); } if (found_key) { /* only key XOR pkcs12 */ ShowLocalizedMsg(GUI_NAME, ERR_ONLY_KEY_OR_PKCS12, ""); return(0); } found_pkcs12=1; *keyfile_format = KEYFILE_FORMAT_PKCS12; if (!ParseKeyFilenameLine(config, keyfilename, keyfilenamesize, &line[7])) return(0); } } if ((!found_key) && (!found_pkcs12)) { /* must have key or pkcs12 option */ ShowLocalizedMsg(GUI_NAME, ERR_MUST_HAVE_KEY_OR_PKCS12, ""); return(0); } return(1); }
static int GetKeyFilename(connection_t *c, TCHAR *keyfilename, size_t keyfilenamesize, int *keyfile_format) { FILE *fp; char line[256]; int found_key=0; int found_pkcs12=0; TCHAR configfile_path[MAX_PATH]; _tcsncpy(configfile_path, c->config_dir, _countof(configfile_path)); if (!(configfile_path[_tcslen(configfile_path)-1] == '\\')) _tcscat(configfile_path, _T("\\")); _tcsncat(configfile_path, c->config_file, _countof(configfile_path) - _tcslen(configfile_path) - 1); if (!(fp=_tfopen(configfile_path, _T("r")))) { /* can't open config file */ ShowLocalizedMsg(IDS_ERR_OPEN_CONFIG, configfile_path); return(0); } while (fgets(line, sizeof (line), fp)) { if (LineBeginsWith(line, "key", 3)) { if (found_key) { /* only one key option */ ShowLocalizedMsg(IDS_ERR_ONLY_ONE_KEY_OPTION); return(0); } if (found_pkcs12) { /* key XOR pkcs12 */ ShowLocalizedMsg(IDS_ERR_ONLY_KEY_OR_PKCS12); return(0); } found_key=1; *keyfile_format = KEYFILE_FORMAT_PEM; if (!ParseKeyFilenameLine(c, keyfilename, keyfilenamesize, &line[4])) return(0); } if (LineBeginsWith(line, "pkcs12", 6)) { if (found_pkcs12) { /* only one pkcs12 option */ ShowLocalizedMsg(IDS_ERR_ONLY_ONE_PKCS12_OPTION); return(0); } if (found_key) { /* only key XOR pkcs12 */ ShowLocalizedMsg(IDS_ERR_ONLY_KEY_OR_PKCS12); return(0); } found_pkcs12=1; *keyfile_format = KEYFILE_FORMAT_PKCS12; if (!ParseKeyFilenameLine(c, keyfilename, keyfilenamesize, &line[7])) return(0); } } if ((!found_key) && (!found_pkcs12)) { /* must have key or pkcs12 option */ ShowLocalizedMsg(IDS_ERR_HAVE_KEY_OR_PKCS12); return(0); } return(1); }