Verify and validate

Peter Volz 1,295 Reputation points
2023-09-10T10:20:34.1333333+00:00

Hello

In my app's UI I'll need to get a pfx certificate from user and pass it to a 3rd party sdk later.

Since all user input must be checked (I have to) to verify and validate the selected pfx, I wrote this:

There's no cert.Dispose in .net 4.0, no EphemeralKeySet so I thought X509Certificate2Collection is safer way.

Don't wanna save files and just make/verify in memory, is my method safe?

Dim VerifyCert As Boolean
Dim Temp509Collection As New X509Certificate2Collection
Try
    Temp509Collection.Import(FileTextBox.Text, PassTextBox.Text, X509KeyStorageFlags.UserKeySet)
    VerifyCert = True
Catch Exception As Exception
    VerifyCert = False
End Try
If Temp509Collection.Count > 0 AndAlso Temp509Collection.Item(0).HasPrivateKey = False Then
    'Some other check
End If
Temp509Collection.Clear()
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,649 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,669 questions
0 comments No comments
{count} vote

Accepted answer
  1. Jiachen Li-MSFT 29,261 Reputation points Microsoft Vendor
    2023-09-11T03:22:15.48+00:00

    Hi @Peter Volz ,

    You could catch CryptographicException for certificate-related issues.

    Using the following code to dispose the resources.

        For Each cert As X509Certificate2 In Temp509Collection
            cert.Reset()
        Next
        Temp509Collection.Clear()
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful