Win32::OLE and CAPICOM to find a certificate in certificate store will raise exception

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default Win32::OLE and CAPICOM to find a certificate in certificate store will raise exception

    Hi,

    I am trying to use win32::OLE to access certificate store via CAPICOM.
    If certificates in the store meet the searching criteria, the
    certificates object
    method "find" works, but if no certificate meets the searching
    criteria, it will raise
    an error message:
    OLE exception from "<Unknown Source>": The Data is invalid.

    How to solve the problem or catch the exception in Perl script?

    Thanks!

    ===== test case =======
    #!c:/Perl/bin/perl.exe -w
    #Test store->certificates->Find

    use strict;
    use Win32::OLE;

    # CAPICOM constant definitions
    use constant {
    # Store Location
    #
    [url]http://msdn.microsoft.com/library/en-us/security/security/capicom_store_location.asp[/url]
    CAPICOM_LOCAL_MACHINE_STORE => 1,
    CAPICOM_CURRENT_USER_STORE => 2,
    # Store Open Mode
    #
    [url]http://msdn.microsoft.com/library/en-us/security/security/capicom_store_open_mode.asp[/url]
    CAPICOM_STORE_OPEN_READ_ONLY => 0,
    # key storage flags
    #
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/seccrypto/security/certificate_load.asp[/url]
    CAPICOM_KEY_STORAGE_DEFAULT => 0,
    # The Find method returns a Certificates object that contains all
    certificates that
    # match the specified search criteria. This method was introduced
    in CAPICOM 2.0. see:
    #
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/seccrypto/security/certificates_find.asp[/url]
    CAPICOM_CERTIFICATE_FIND_SHA1_HASH => 0,
    };

    # Search the certificate with the thumbprint SHA1 in local certificate
    store.
    my $SHA1 = "0000000049d8650d2515111709ee1b4800000000";

    Win32::OLE->Option ('Warn' => 3);

    # Create a new Store object, and use it to open the store. See
    #
    <http://msdn.microsoft.com/library/en-us/security/security/store.asp>.
    my $Store = Win32::OLE->new('CAPICOM.Store', sub {$_[0]->Close();})
    or die "Oops, cannot start CAPICOM";
    $Store->Open (CAPICOM_LOCAL_MACHINE_STORE, 'ROOT',
    CAPICOM_STORE_OPEN_READ_ONLY);

    # TEST the certificates->find function
    # if no certificate is found, this program will hangup after several
    tries!
    # here 10 tries, should print out 10 times "continue searching..." if
    no exception.
    for(1...10)
    {
    $Store->Certificates->Find(CAPICOM_CERTIFICATE_FIND_SHA1_HASH,
    $SHA1);
    print "continue searching...\n";
    }
    print "\nCompleted.\n";


    # perl version
    #
    # This is perl, v5.8.8 built for MSWin32-x86-multi-thread
    # (with 25 registered patches, see perl -V for more detail)
    #
    # Copyright 1987-2006, Larry Wall
    #
    # Binary build 817 [257965] provided by ActiveState
    [url]http://www.ActiveState.com[/url]
    # Built Mar 20 2006 17:54:25

    danielhe99@gmail.com Guest

  2. Similar Questions and Discussions

    1. Digitally Sign PDF with CAPICOM Certificate
      Hi all ! I have a problem regarding the signing of pdf files from another application. I am a developer of a ASP.NET (1.1) document management...
    2. SSL Certificate
      We're trying to get a SSL certificate set up. But we keep getting a "connection terminated unexpectedly" error. And nothing is being recorded in...
    3. Require SSL certificate
      Hi, i am hoping to get some advice on the best way to achieve the following. I have a website and a security certificate, i install the...
    4. CAPICOM, Certificate Stores, and ASP.NET
      Hello, I'm working on an ASP.NET web application that takes in an encrypted data token and decrypts it. On my Windows XP box, I have created a...
    5. Certificate Server and Windows XP - Cannot install certificate
      Hello all, I would like to implement certificate server. I have installed the service on Win2003 server in standalone mode. I have created from...
  3. #2

    Default Re: Win32::OLE and CAPICOM to find a certificate in certificate storewill raise exception

    [email]danielhe99@gmail.com[/email] wrote:
    > Hi,
    >
    > I am trying to use win32::OLE to access certificate store via CAPICOM.
    > If certificates in the store meet the searching criteria, the
    > certificates object
    > method "find" works, but if no certificate meets the searching
    > criteria, it will raise
    > an error message:
    > OLE exception from "<Unknown Source>": The Data is invalid.
    >
    > How to solve the problem or catch the exception in Perl script?
    >
    > Thanks!
    >
    > ===== test case =======
    > #!c:/Perl/bin/perl.exe -w
    > #Test store->certificates->Find
    >
    > use strict;
    > use Win32::OLE;
    >
    > # CAPICOM constant definitions
    > use constant {
    > # Store Location
    > #
    > [url]http://msdn.microsoft.com/library/en-us/security/security/capicom_store_location.asp[/url]
    > CAPICOM_LOCAL_MACHINE_STORE => 1,
    > CAPICOM_CURRENT_USER_STORE => 2,
    > # Store Open Mode
    > #
    > [url]http://msdn.microsoft.com/library/en-us/security/security/capicom_store_open_mode.asp[/url]
    > CAPICOM_STORE_OPEN_READ_ONLY => 0,
    > # key storage flags
    > #
    > [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/seccrypto/security/certificate_load.asp[/url]
    > CAPICOM_KEY_STORAGE_DEFAULT => 0,
    > # The Find method returns a Certificates object that contains all
    > certificates that
    > # match the specified search criteria. This method was introduced
    > in CAPICOM 2.0. see:
    > #
    > [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/seccrypto/security/certificates_find.asp[/url]
    > CAPICOM_CERTIFICATE_FIND_SHA1_HASH => 0,
    > };
    >
    > # Search the certificate with the thumbprint SHA1 in local certificate
    > store.
    > my $SHA1 = "0000000049d8650d2515111709ee1b4800000000";
    >
    > Win32::OLE->Option ('Warn' => 3);
    >
    > # Create a new Store object, and use it to open the store. See
    > #
    > <http://msdn.microsoft.com/library/en-us/security/security/store.asp>.
    > my $Store = Win32::OLE->new('CAPICOM.Store', sub {$_[0]->Close();})
    > or die "Oops, cannot start CAPICOM";
    > $Store->Open (CAPICOM_LOCAL_MACHINE_STORE, 'ROOT',
    > CAPICOM_STORE_OPEN_READ_ONLY);
    >
    > # TEST the certificates->find function
    > # if no certificate is found, this program will hangup after several
    > tries!
    > # here 10 tries, should print out 10 times "continue searching..." if
    > no exception.
    > for(1...10)
    > {
    > $Store->Certificates->Find(CAPICOM_CERTIFICATE_FIND_SHA1_HASH,
    > $SHA1);
    > print "continue searching...\n";
    > }
    > print "\nCompleted.\n";
    >
    >
    > # perl version
    > #
    > # This is perl, v5.8.8 built for MSWin32-x86-multi-thread
    > # (with 25 registered patches, see perl -V for more detail)
    > #
    > # Copyright 1987-2006, Larry Wall
    > #
    > # Binary build 817 [257965] provided by ActiveState
    > [url]http://www.ActiveState.com[/url]
    > # Built Mar 20 2006 17:54:25
    >
    I'm not sure what catching thrown exceptions has to do with Perl
    modules. Have you read the documentation on the 'eval' built-in?

    Nit-pick: do you mean the 'Find' function? There does not appear to be
    an invocation of 'find' in your code.

    Tom Wyant
    harryfmudd [AT] comcast [DOT] net Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139