📄 storesh.cs
字号:
//******************************************************************************
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//
// Copyright (C) 1999 - 2002. Microsoft Corporation. All rights reserved.
//
//******************************************************************************
//
// storesh.cs
//
// This is a sample script to illustrate how to use CAPICOM and C#. It creates a
// pseudo shell to navigate certificate stores.
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
// To run this sample you need to have Microsoft Visual C# .NET
// To start: open the storesh.sln file in Visual Studio
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
//
// Try these commands:
//
// help
// dir
// cd ..
// cd ..\addressbook
// dir -l
// dir -subject John
// cd \LocalMachine
// cd \lm
// display 3
// view 2
// import c:\home\temp\pfx1.pfx
// export 7 c:\home\temp\itm.cer
// pfxexport 11 c:\home\temp\foo.pfx secretpassword
// rem 2
// dir -issuer "Thawte"
// dir -eku "Code Signing"
// dir -eku 1.3.6.1.5.5.7.3.4
//
// Note: For simplicity, this script does not handle exceptions.
//
//******************************************************************************
using System;
using System.Collections;
using System.Runtime.InteropServices;
using CAPICOM;
using System.Text;
namespace storesh
{
public class Win32
{
[DllImport("crypt32.dll",CharSet=CharSet.Unicode )]
public static extern uint CertEnumSystemStore(
uint dwFlags,
uint pvSystemStoreLocationPara,
String pvArg,
CertEnumSystemStoreCallback pfnEnum
);
}
public delegate bool CertEnumSystemStoreCallback(
[In, MarshalAs( UnmanagedType.LPWStr) ] String pvSystemStore,
uint dwFlags,
ref CERT_SYSTEM_STORE_INFO pStoreInfo,
uint pvReserved,
[In, MarshalAs( UnmanagedType.LPWStr) ] String pvArg
);
[StructLayout(LayoutKind.Sequential)]
public struct CERT_SYSTEM_STORE_INFO
{
uint cbSize;
}
class Storesh
{
const uint CERT_SYSTEM_STORE_CURRENT_USER = 0x10000;
const uint CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x20000;
const int VALIDITY_LENGTH = 23;
const int CERTNAME_LENGTH = 20;
const int THUMBPRINT_LENGTH = 42;
const int FRIENDLYNAME_LENGTH = 20;
const int CAPICOM_ENCODED_DATA_FORMAT_MULTI_LINES =1 ;
const int HELP_PAD = 30;
const String CurrentUser = "CurrentUser";
const String LocalMachine = "LocalMachine";
static private String _currStoreName = "My";
static private String _currStoreLocation = CurrentUser;
static IEnumerator _currFilter;
static StoreClass _oCurrStore;
enum Listing {Brief, Long};
/// The main entry point for the application.
[STAThread]
static void Main(string[] args)
{
//We take one argument, a starting store name
if (args.Length > 0)
{
_currStoreName = args[0];
}
_oCurrStore = new StoreClass();
_oCurrStore.Open(
CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE,
_currStoreName,
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY |
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);
waitForInput();
}
static void waitForInput()
{
bool done = false;
String input;
while (!done)
{
Console.Write(_currStoreLocation + "\\" + _currStoreName + ">");
input = Console.ReadLine();
done = parseCommandLine(input);
}
}
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.");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -