/* CertContext demonstrates COM access from Java for MS JVM. Searches specified system certificate store for certificate with specified SubjectName (sub)string, and displays first certificate that matches, along with certcontext hex value. jactivex capicom.dll --> generates JCW for Java access. Examine resultant source code to determine required interfaces. M. Gallant 09/13/2002 */ import com.ms.com.*; import java.io.*; import capicom.*; class CertContext { static final int CAPICOM_LOCAL_MACHINE_STORE = 1; static final int CAPICOM_CURRENT_USER_STORE = 2; static final int CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME = 1; static final int CAPICOM_STORE_OPEN_READ_ONLY = 0; public static void main(String args[]) { IStore oStore; ICertificates2 certs; ICertificates fcerts; ICertificate certfound; ICertContext ictxt ; if(args.length != 2) { System.out.println("Usage: jview CertContext \n") ; try { int inp = System.in.read() ; } catch(IOException ioe) {;} System.exit(1); } String storename = args[0] ; String subjectname = args[1] ; oStore = (IStore) new Store(); oStore.Open(CAPICOM_CURRENT_USER_STORE, storename, CAPICOM_STORE_OPEN_READ_ONLY ); certs = (ICertificates2)oStore.getCertificates() ; fcerts = certs.Find(CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME, new Variant(subjectname), false); System.out.println("Certificates in store \"" + storename + "\": " + certs.getCount()) ; System.out.println("FilteredCount: " + fcerts.getCount()) ; if(fcerts.getCount() == 0) { System.out.println("No certs with subject name substring \"" + subjectname + "\" found") ; System.exit(1) ; } certfound = (ICertificate)fcerts.getItem(1).getDispatch() ; //getItem() returns variant certfound.Display() ; ictxt = (ICertContext) certfound ; int hcertcontext = ictxt.getCertContext() ; System.out.println("Certificate context: " + Integer.toHexString(hcertcontext).toUpperCase() + "h") ; ictxt.FreeContext(hcertcontext) ; //release the cert context. try { int inp = System.in.read() ; } catch(IOException ioe) {;} } }