📄 cert.cpp
字号:
} break; case WM_NOTIFY: { NMHDR *hdr = (NMHDR*) lparam; switch (hdr->code) { case PSN_APPLY: if (ctx->path) SaveAll(ctx->store); break; case PSN_SETACTIVE: refresh = 1; break; default: return FALSE; } } break; default: return FALSE; } if (refresh) { const char *path = ctx->path; if (!path) { path = ctx->ident->GetName(); HWND bwnd = GetDlgItem(wnd, IDC_IMPORT); Button_Enable(bwnd, FALSE); bwnd = GetDlgItem(wnd, IDC_REMOVE); Button_Enable(bwnd, FALSE); } #ifdef WIN32 const char *name = strrchr(path, '\\');#else const char *name = strrchr(path, '/');#endif name = name ? (name + 1):path; SetDlgItemText(wnd, IDC_NAME, name); SetDlgItemText(wnd, IDC_SUBJECT, ctx->ident->GetName()); SetDlgItemText(wnd, IDC_ISSUER, ctx->ident->GetIssuerName()); char *expires = ctx->ident->GetExpiration(); SetDlgItemText(wnd, IDC_EXPIRES, expires); delete [] expires; SetDlgItemText(wnd, IDC_STATUS, Verify(ctx->store, ctx->ident) ? "Invalid":"Valid"); } return TRUE;}#endif // WIN32/* * List: Display list of certificates. * @store: Certificate store. */static voidList(PTP::Store *store){#ifdef WIN32 DisplayContext ctx; memset(&ctx, 0, sizeof(ctx)); ctx.store = store; PROPSHEETPAGE page; memset(&page, 0, sizeof(page)); page.dwSize = sizeof(page); page.dwFlags = PSP_DEFAULT; page.hInstance = g_inst; page.pszTemplate = MAKEINTRESOURCE(IDD_CERTS); page.pfnDlgProc = ListCallback; page.lParam = (LPARAM) &ctx; PROPSHEETHEADER sheet; memset(&sheet, 0, sizeof(sheet)); sheet.dwSize = sizeof(sheet); sheet.dwFlags = (PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW); sheet.hInstance = g_inst; sheet.pszCaption = __TEXT("Certificates"); sheet.nPages = 1; sheet.nStartPage = 0; sheet.ppsp = &page; PropertySheet(&sheet);#else // WIN32 printf("Certificates:\n"); PTP::Identity *ident = NULL; for (;;) { ident = store->Find(NULL, 0, NULL, ident); if (!ident) break; printf(" %s\n", ident->GetName()); }#endif WIN32}/* * Show: Show certificate information. * @store: Certificate store. * @ident: Certificate. * @path: Certificate file pathname or NULL if none. */static voidShow(PTP::Store *store, PTP::Identity *ident, const char *path){ if (!ident) return;#ifdef WIN32 DisplayContext ctx; memset(&ctx, 0, sizeof(ctx)); ctx.store = store; ctx.path = path; ctx.ident = ident; PROPSHEETPAGE page[2]; memset(&page, 0, sizeof(page)); page[0].dwSize = sizeof(page[0]); page[0].dwFlags = PSP_DEFAULT; page[0].hInstance = g_inst; page[0].pszTemplate = MAKEINTRESOURCE(IDD_GENERAL); page[0].pfnDlgProc = ShowCallback; page[0].lParam = (LPARAM) &ctx; page[1].dwSize = sizeof(page[1]); page[1].dwFlags = PSP_DEFAULT; page[1].hInstance = g_inst; page[1].pszTemplate = MAKEINTRESOURCE(IDD_CERTS); page[1].pfnDlgProc = ListCallback; page[1].lParam = (LPARAM) &ctx; PROPSHEETHEADER sheet; memset(&sheet, 0, sizeof(sheet)); sheet.dwSize = sizeof(sheet); sheet.dwFlags = (PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW); sheet.hInstance = g_inst; sheet.pszCaption = __TEXT("Certificate"); sheet.nPages = path ? 2:1; sheet.nStartPage = 0; sheet.ppsp = page; PropertySheet(&sheet);#else const char *name = strrchr(path, '/'); name = name ? (name + 1):path; char *expire = ident->GetExpiration(); printf("File: %s\n" "Subject: %s\n" "Issuer: %s\n" "Expires: %s\n" "Status: %s\n", name, ident->GetName(), ident->GetIssuerName(), expire ? expire:"", Verify(store, ident) ? "Invalid":"Valid"); delete [] expire; delete ident;#endif}intmain(int argc, char **argv){ const char *prog = strrchr(argv[0], '/'); prog = prog ? (prog + 1):argv[0];#ifdef WIN32 PTP::Store store(HKEY_CURRENT_USER, "Software\\PTL\\Cert", NULL, NULL);#else char path[2048]; sprintf(path, "%s/.ptl", getenv("HOME")); mkdir(path, 0700); chmod(path, 0700); strcat(path, "/cert"); PTP::Store store(path, NULL, NULL);#endif store.Load(); if (argc < 2) List(&store); else { const char *passwd = NULL; for (char **arg = argv + 1; arg < (argv + argc); arg++) { int remain = argc - (arg - argv) - 1; if (strcmp(*arg, "--import") == 0 && remain >= 1) { arg++; const char *path = *arg; PTP::Identity *ident = Load(path, passwd); if (ident && Insert(&store, ident, 0) == 0) SaveAll(&store); delete ident; } else if (strcmp(*arg, "--export") == 0 && remain >= 2) { arg++; const char *name = *arg; arg++; const char *path = *arg; if (name && !*name) { const PTP::Identity *local = store.Find(NULL, 1); if (local) name = local->GetName(); } PTP::Identity *ident = Find(&store, name); if (ident) Save(ident, path, passwd); } else if (strcmp(*arg, "--sign") == 0 && remain >= 2) { arg++; int expire = strtoul(*arg, NULL, 10); expire *= 24 * 60 * 60; if (!**arg) expire = EXPIRE_DEFAULT; arg++; const char *path = *arg; PTP::Identity *ident = Load(path, NULL); if (ident && Sign(&store, ident, expire) == 0) Save(ident, path, passwd); delete ident; } else if (strcmp(*arg, "--remove") == 0 && remain >= 1) { arg++; if (Remove(&store, *arg) == 0) SaveAll(&store); } else if (strcmp(*arg, "--setup") == 0 && remain >= 1) { arg++; const char *name = **arg ? *arg:NULL; PTP::Identity *ident = new PTP::Identity(name); if (!ident) Error("Cannot create `%s'.\n"); else { store.Reset(); if (Insert(&store, ident, 1) == 0) SaveAll(&store); } delete ident; } else if (strcmp(*arg, "--load") == 0 && remain >= 1) { arg++; const char *path = *arg; PTP::Store store2(path, passwd, passwd); if (store2.Load()) Error("Cannot load `%s'.\n", path); else { store.Reset(); PTP::Identity *id = NULL; for (;;) { id = store2.Find(NULL, 0, NULL, id); if (!id) break; store.Insert(id, 1); } SaveAll(&store); } } else if (strcmp(*arg, "--save") == 0 && remain >= 1) { arg++; const char *path = *arg; PTP::Store store2(path, passwd, passwd); PTP::Identity *id = NULL; for (;;) { id = store.Find(NULL, 0, NULL, id); if (!id) break; store2.Insert(id, 1); } if (store2.Save()) Error("Cannot save `%s'.\n", path); } else if (strcmp(*arg, "--passwd") == 0 && remain >= 1) { arg++; passwd = (*arg && **arg) ? *arg:NULL; } else if (**arg != '-') { const char *path = *arg; PTP::Identity *ident = Load(path, passwd); if (ident) Show(&store, ident, path); } else { Error("Invalid option or missing arguments.\n" "Usage: %s [OPTION] [FILE]\n" " NONE " "List all certificates.\n" " FILE " "Display a certificate.\n" " --import FILE " "Import a certificate.\n" " --export NAME FILE " "Export a certificate. \n" " --sign DAYS FILE " "Sign a certificate.\n" " --remove NAME " "Remove a certificate.\n" " --setup NAME " "Setup the local certificate.\n" " --load FILE " "Load the entire store.\n" " --save FILE " "Save the entire store.\n" " --passwd PASSWD " "Set import/export password.\n", prog); break; } } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -