public bool AddContactCertificate(string strContactID, X509Certificate cert) { if (strContactID == null || strContactID.Length == 0) { throw new ArgumentException("Invalid contact ID", "strContactID"); } if (cert == null) { throw new ArgumentNullException("cert", "Invalid Contact X509 Certificate"); } // Prevent any attempt to add multiple certificates for a contact if (IsContactCertificateInStore(strContactID)) { return(true); } bool bRetVal = true; // Use CAPICOM (v2.0) support to add certificate StoreClass store = new StoreClass(); store.Open(CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE, X509CertificateStore.MyStore, CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY | CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED ); // Store certificate // Convert cert to base64 string string strBase64Cert = cert.ToBase64String(); // Save to temp file string strTempFile = Guid.NewGuid().ToString(); StreamWriter sw = new StreamWriter(File.Create(strTempFile)); sw.Write(strBase64Cert); sw.Flush(); sw.Close(); // Load cert from temp file store.Load(strTempFile, Constants.DEFAULT_CERT_PSWD, CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_DEFAULT | CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_EXPORTABLE); // Delete temp file File.Delete(strTempFile); // Close store store.CloseHandle(store.StoreHandle); // Return true return(bRetVal); }
static bool parseCommandLine(String input) { ArrayList alArgs = ParseArgs(input); Certificate oCert = null; switch ((String)alArgs[0]) { case "cd": { //This is the 'change directory' command String storename; if (alArgs.Count > 1) { storename = (String)alArgs[1]; } else { storename = _currStoreName; //reset store name } if (storename.Equals("..") && _currStoreName.Length > 0) { _oCurrStore = new StoreClass(); storename = null; _currFilter = null; _currStoreName = ""; } else if (storename.StartsWith("..") && _currStoreName.Length > 0) { _oCurrStore = new StoreClass(); _currFilter = null; storename = storename.Substring(3, storename.Length - 3); _currStoreName = ""; } else if (storename.Equals("..")) { storename = null; } else if (storename.Equals("\\" + LocalMachine) || storename.Equals("\\lm")) { _oCurrStore = new StoreClass(); _currStoreName = ""; storename = null; _currStoreLocation = LocalMachine; _currFilter = null; } else if (storename.Equals("\\" + CurrentUser) || storename.Equals("\\cu")) { _oCurrStore = new StoreClass(); _currStoreName = ""; storename = null; _currStoreLocation = CurrentUser; _currFilter = null; } if (storename != null && _currStoreName.Equals("")) { try { CAPICOM_STORE_LOCATION OpenMode = CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE; if (_currStoreLocation.Equals(LocalMachine)) { OpenMode = CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE; } //Open the store MAX_ALLOWED in case the user wants to import/rem/export //They may not have permission to modify HKLM stores _oCurrStore.Open(OpenMode, storename, CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY | CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED); _currStoreName = storename; } catch (Exception e) { Console.WriteLine(e.Message); } } return(false); } case "q": case "quit": { return(true); } case "h": case "help": { DisplayHelp(); return(false); } case "v": case "view": try { oCert = GetCertByIndex(Convert.ToInt32(alArgs[1])); if (oCert != null) { DisplayCertificate(oCert, ""); } else { Console.WriteLine("No certificate with that index (" + alArgs[1] + ") could be found."); } } catch (Exception e) { Console.WriteLine("An error was encountered processing the " + alArgs[0] + " command: " + e.Message); } break; case "y": case "display": try { oCert = GetCertByIndex(Convert.ToInt32(alArgs[1])); if (oCert != null) { oCert.Display(); } else { Console.WriteLine("No certificate with that index (" + alArgs[1] + ") could be found."); } } catch (Exception e) { Console.WriteLine("An error was encountered processing the " + alArgs[0] + " command: " + e.Message); } break; case "rem": try { oCert = GetCertByIndex(Convert.ToInt32(alArgs[1])); if (oCert != null) { String answer = "n"; Console.WriteLine(ToColumn(oCert.GetInfo(CAPICOM_CERT_INFO_TYPE.CAPICOM_CERT_INFO_SUBJECT_SIMPLE_NAME), CERTNAME_LENGTH) + " " + ToColumn(oCert.Thumbprint, THUMBPRINT_LENGTH)); Console.WriteLine("Issuer: " + oCert.GetInfo(CAPICOM_CERT_INFO_TYPE.CAPICOM_CERT_INFO_ISSUER_SIMPLE_NAME)); Console.WriteLine("Validity Period: " + oCert.ValidFromDate + " - " + oCert.ValidToDate); DisplayEKU(oCert.ExtendedKeyUsage().EKUs); Console.WriteLine(); Console.Write("Are you sure you want to remove this certificate (y/n)? "); answer = Console.ReadLine(); if (!answer.Equals("y")) { break; } if (oCert.HasPrivateKey() && !oCert.PrivateKey.IsHardwareDevice()) { oCert.PrivateKey.Delete(); Console.WriteLine("The private key was deleted."); } try { _oCurrStore.Remove(oCert); Console.WriteLine("The certificate was removed."); } catch { Console.WriteLine("The certificate could not be removed."); } } else { Console.WriteLine("No certificate with that index (" + alArgs[1] + ") could be found."); } } catch (Exception e) { Console.WriteLine("An error was encountered processing the " + alArgs[0] + " command: " + e.Message); } break; case "e": case "export": try { oCert = GetCertByIndex(Convert.ToInt32(alArgs[1])); if (oCert != null) { String filename = (String)alArgs[2]; if (filename != null) { oCert.Save(filename, "", CAPICOM_CERTIFICATE_SAVE_AS_TYPE.CAPICOM_CERTIFICATE_SAVE_AS_CER, CAPICOM_CERTIFICATE_INCLUDE_OPTION.CAPICOM_CERTIFICATE_INCLUDE_END_ENTITY_ONLY); } else { Console.WriteLine("No filename specified."); } } else { Console.WriteLine("No certificate with that index (" + alArgs[1] + ") could be found."); } } catch (Exception e) { Console.WriteLine("An error was encountered processing the " + alArgs[0] + " command: " + e.Message); } break; case "i": case "import": try { String filename = null; String password = null; CAPICOM_KEY_STORAGE_FLAG iFlag = CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_DEFAULT; if (alArgs.Count >= 2) { filename = (String)alArgs[1]; } if (alArgs.Count >= 3) { password = (String)alArgs[2]; } if (alArgs.Count >= 4) { String flag = null; if (alArgs.Count == 4) { password = null; flag = (String)alArgs[3]; } else if (alArgs.Count == 5) { flag = (String)alArgs[4]; } if (flag.Equals("e")) { iFlag = CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_EXPORTABLE; } else if (flag.Equals("p")) { iFlag = CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_USER_PROTECTED; } else if (flag.Equals("ep") || flag.Equals("pe")) { iFlag = CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_USER_PROTECTED | CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_EXPORTABLE; } } _oCurrStore.Load(filename, password, iFlag); } catch (Exception e) { Console.WriteLine("An error was encountered processing the " + alArgs[0] + " command: " + e.Message); } break; case "p": case "pfxexport": try { oCert = GetCertByIndex(Convert.ToInt32(alArgs[1])); if (oCert != null) { String filename = (String)alArgs[2]; String password = (String)alArgs[3]; if (filename != null) { oCert.Save(filename, password, CAPICOM_CERTIFICATE_SAVE_AS_TYPE.CAPICOM_CERTIFICATE_SAVE_AS_PFX, CAPICOM_CERTIFICATE_INCLUDE_OPTION.CAPICOM_CERTIFICATE_INCLUDE_WHOLE_CHAIN); } else { Console.WriteLine("No filename specified."); } } else { Console.WriteLine("No certificate with that index (" + alArgs[1] + ") could be found."); } } catch (Exception e) { Console.WriteLine("An error was encountered processing the " + alArgs[0] + " command: " + e.Message); } break; case "c": case "create": case "del": try { // Opening it is the same whether we're creating or deleting if (alArgs.Count == 1) { Console.WriteLine("Please enter a name for the store"); break; } String name = (String)alArgs[1]; for (int i = 2; i < alArgs.Count; i++) { name += " " + alArgs[i]; } Store newStore = new Store(); CAPICOM_STORE_LOCATION storeLoc = CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE; if (_currStoreLocation.Equals(LocalMachine)) { storeLoc = CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE; } newStore.Open(storeLoc, name, CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_WRITE); // Now delete if that is what was chosen if (alArgs[0].Equals("del")) { if (_currStoreName.Equals(name)) { Console.WriteLine("You cannot delete the store you are currently viewing"); Console.WriteLine("Please use the <cd> command to change stores first"); break; } newStore.Delete(); } } catch (Exception e) { Console.WriteLine("An error was encountered processing the " + alArgs[0] + " command: " + e.Message); } break; case "d": case "dir": { Certificates oCerts; Listing listing = Listing.Brief; if ((_currStoreLocation.Equals(CurrentUser) || _currStoreLocation.Equals(LocalMachine)) && _currStoreName.Length == 0) { uint retval = 0; uint dwFlags = CERT_SYSTEM_STORE_CURRENT_USER; CertEnumSystemStoreCallback StoreCallback = new CertEnumSystemStoreCallback(Storesh.CertEnumSystemStoreCallback); if (_currStoreLocation.Equals(LocalMachine)) { dwFlags = CERT_SYSTEM_STORE_LOCAL_MACHINE; } retval = Win32.CertEnumSystemStore( dwFlags, 0, _currStoreName, StoreCallback ); } else if (alArgs.Count >= 1) { int i = 0; try { _currFilter = _oCurrStore.Certificates.GetEnumerator(); for (i = 1; i < alArgs.Count; i++) { String param = ((String)alArgs[i]).ToLower(); if (param.Equals("/l") || param.Equals("-l")) { listing = Listing.Long; } else if (param.Equals("/subject") || param.Equals("-subject")) { Object filter = (System.String)alArgs[++i]; oCerts = (Certificates)_oCurrStore.Certificates; oCerts = (Certificates)oCerts.Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME, filter, false); _currFilter = oCerts.GetEnumerator(); } else if (param.Equals("/eku") || param.Equals("-eku")) { Object filter = (System.String)alArgs[++i]; oCerts = (Certificates)_oCurrStore.Certificates; oCerts = (Certificates)oCerts.Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_APPLICATION_POLICY, filter, false); _currFilter = oCerts.GetEnumerator(); } else if (param.Equals("/issuer") || param.Equals("-issuer")) { Object filter = (System.String)alArgs[++i]; oCerts = (Certificates)_oCurrStore.Certificates; oCerts = (Certificates)oCerts.Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_ISSUER_NAME, filter, false); _currFilter = oCerts.GetEnumerator(); } else if (param.Equals("/sha1") || param.Equals("-sha1")) { String filter = (String)alArgs[++i]; oCerts = (Certificates)_oCurrStore.Certificates; oCerts = (Certificates)oCerts.Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_SHA1_HASH, filter, false); _currFilter = oCerts.GetEnumerator(); } } i = 1; while (_currFilter.MoveNext() == true) { oCert = (Certificate)_currFilter.Current; Console.Write((i++ + ". ").PadRight(4, ' ')); switch (listing) { case Listing.Brief: Console.WriteLine(ToColumn(oCert.GetInfo(CAPICOM_CERT_INFO_TYPE.CAPICOM_CERT_INFO_SUBJECT_SIMPLE_NAME), CERTNAME_LENGTH) + " " + ToColumn(oCert.ValidToDate.ToString(), VALIDITY_LENGTH) + ToColumn(oCert.GetInfo(CAPICOM_CERT_INFO_TYPE.CAPICOM_CERT_INFO_ISSUER_SIMPLE_NAME), CERTNAME_LENGTH)); break; case Listing.Long: { Console.WriteLine(ToColumn(oCert.GetInfo(CAPICOM_CERT_INFO_TYPE.CAPICOM_CERT_INFO_SUBJECT_SIMPLE_NAME), CERTNAME_LENGTH) + " " + ToColumn(oCert.Thumbprint, THUMBPRINT_LENGTH)); Console.WriteLine("Issuer: " + oCert.GetInfo(CAPICOM_CERT_INFO_TYPE.CAPICOM_CERT_INFO_ISSUER_SIMPLE_NAME)); Console.WriteLine("Validity Period: " + oCert.ValidFromDate + " - " + oCert.ValidToDate); DisplayEKU(oCert.ExtendedKeyUsage().EKUs); Console.WriteLine(); break; } } } } catch (Exception e) { Console.WriteLine("An error was encountered: " + e.Message); } } break; } default: DisplayHelp(); break; } return(false); }