PowerNets.psm1


function Find-NetsCertificate {
    param (
        [string]$Email
    )
    
    $searchDN="c=dk"
    $subjectAlternateNameUid = "2.5.29.17"

    #import .net assembly
    if ($PSVersionTable.PSEdition -eq "Desktop") {
        $ldapconnection = New-Object -TypeName Novell.Directory.LDAP.LdapConnection
    }
    else {
        $ldapconnection = New-Object -TypeName Novell.Directory.LDAP.VQ.LdapConnection
    }
    
    $ldapconnection.Connect("crtdir.certifikat.dk",389)
    $ldapscope = 2 #scope_sub
    
    
    #sanitize user input
    $certificatestring = $Email.Trim()
    try {
        
        #make connection to crtdir with ldap
        $cn = $ldapconnection.Search($searchDN,$ldapscope,"(mail=$certificatestring)",$null,$false)
        #this method has to be called before using count on object to check if any results are returned because it is blocking
        $cn.hasMore() | Out-Null
        #check if any results
        if ($cn.Count -eq 0) {
            throw "No certificate found"
        }

        $customobject = @()
        do {
            $dn = $cn.next()
            #get attribute from ldap entry in dn
            $ldapcert = $dn.getAttribute("userCertificate;binary")
            #put sbyte array into var
            $certificatebinary = $ldapcert.ByteValue
            #put binary data into .net certficate object.
            $Certobject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 ` -ArgumentList @(,$certificatebinary)
            $decimalserial = [convert]::ToInt64($certobject.SerialNumber,16)

            #put extesions into asn objects
            $extensions = $Certobject.Extensions
            $asnarray = @{}
            foreach ($e in $extensions) {
                $asn = New-Object -TypeName System.Security.Cryptography.AsnEncodedData($e.oid, $e.rawdata)
                $asnformatted= $asn.Format($true)
                $asnarray.add($asn.Oid, $asnformatted)
            }
                
            #get mail from rfc822 name inside certificate
            $mail = ((($asnarray[($asnarray.Keys | Where-Object Value -eq $subjectAlternateNameUid)]) -replace "`r`n","").Split('='))[1]
            
            #check to see if certifcate has expired
            $expired = ""
                
            if($Certobject.NotAfter -lt (Get-Date)) {
                $expired = $true
            } else {
                $expired = $false
            }
            
            #make psobject with information
            $customobject += New-Object psobject -Property @{RawCertificate=$certificatebinary;Mail=$mail;Name=$Certobject.Subject;Created=$Certobject.NotBefore;Expires=$Certobject.NotAfter;SerialNumberDecimal=$decimalserial;SerialNumberHex=$Certobject.SerialNumber;Expired=$expired;Extensions=$extensions;Thumbprint=$Certobject.Thumbprint}
            
        }  while ($cn.hasMore())

        return $customobject    
    } catch {
        if ($_.Exception.Message -like "*Exception calling `"SendRequest`" with `"2`" argument(s): `"Den tilladte*`"") {
            throw "More than five certificates found for this email"
        } else {
            throw $_
        }
    }
}
if ($PSVersionTable.PSEdition -eq "Desktop") {
    Add-Type -Path "$PSScriptRoot\Novell.Directory.LDAP.dll" #mono compatible assembly
}
else {
    Add-Type -Path "$PSScriptRoot\Novell.Directory.LDAP.VQ.dll" #.core assembly
}