const CAPICOM_STORE_OPEN_READ_ONLY = 0
const CAPICOM_CURRENT_USER_STORE = 2
const CAPICOM_CERTIFICATE_FIND_SHA1_HASH = 0
const CAPICOM_CERTIFICATE_FIND_EXTENDED_PROPERTY = 6
const CAPICOM_CERTIFICATE_FIND_TIME_VALID = 9
const CAPICOM_CERTIFICATE_FIND_KEY_USAGE = 12
const CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME = 0
const CAPICOM_INFO_SUBJECT_SIMPLE_NAME = 0
const CAPICOM_CERT_INFO_SUBJECT_EMAIL_NAME = 2
const CAPICOM_ENCODE_BASE64 = 0
const CAPICOM_ENCODE_BINARY = 1
const CAPICOM_E_CANCELLED = -2138568446
const CERT_KEY_SPEC_PROP_ID = 6
const CAPICOM_CERT_INFO_ISSUER_DNS_NAME = 7

'*****************************************************************************************
'*********** Imzalama Fonksiyonu (Conceal the wound) *************************************
'*****************************************************************************************

'Imzalı Mail Atma Fonksiyonu 
Public Function SSLImzala(ByVal bodystr)	
	Set oSignedData   = CreateObject("CAPICOM.SignedData") 
	Set oUtilities   = CreateObject("CAPICOM.Utilities") 
	Set oAttribute   = CreateObject("CAPICOM.Attribute") 
	set oStore = CreateObject("CAPICOM.Store") 
	set oSigner = CreateObject("CAPICOM.Signer") 

          
	' sisteme gireceğin sertifika seçimini yap
	oStore.Open CAPICOM_CURRENT_USER_STORE, "My", CAPICOM_STORE_OPEN_READ_ONLY
	Set cSignerCertificates = oStore.Certificates
	
	Select Case cSignerCertificates.Count
		Case 0 
			MsgBox "Sertifika Bulunamadi, sisteme girme yetkiniz yoktur."
			Exit Function		
		Case Else
		    'Enable error handling
            On Error Resume Next
            
			Set cSignerCertificates = cSignerCertificates.Select("Sertifikalar", "Sisteme Girmek Icin Sertifika Seciniz.")	
            If Err.number <> 0 Then	                
		        Exit Function				
		    End If 		    
   
			If (cSignerCertificates.Count = 0) Then
				msgbox "Hata : Sertifika Secimi Iptal edilmistir."
				Exit Function
			End If
			oSigner.Certificate = cSignerCertificates(1)
	End Select
	'sertifika sahibini gonder (bu aynı zamanda from clause olacak)
	SelectCertificate = oSigner.Certificate.GetInfo(CAPICOM_CERT_INFO_SUBJECT_EMAIL_NAME)
	
	'imzalama zamanını ayarla (UTC time)
	oAttribute.Name = CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME
	oAttribute.Value = oUtilities.LocalTimeToUTCTime(Now)
	
	oSigner.AuthenticatedAttributes.Add oAttribute	
	
	'imzalanacak veriyi content'e at (emaillerde strconv kullanıyorduk burda gerek yok)
	oSignedData.Content = bodystr
	
	' veriyi imzala (base 64 formatinda output alıyoruz)
	dim szSignature
	szSignature = oSignedData.Sign(oSigner, True, CAPICOM_ENCODE_BASE64)
    
	SSLImzala = szSignature
	
End Function

'Imzalı Mail Atma Fonksiyonu 
Public Function SertifikaBul(ByRef bodystr, Byref txtSahibi, Byref txtVeren)	
	Set oSignedData   = CreateObject("CAPICOM.SignedData") 
	Set oUtilities   = CreateObject("CAPICOM.Utilities") 
	Set oAttribute   = CreateObject("CAPICOM.Attribute") 
	set oStore = CreateObject("CAPICOM.Store") 
	set oSigner = CreateObject("CAPICOM.Signer") 

	' sisteme gireceğin sertifika seçimini yap
	oStore.Open CAPICOM_CURRENT_USER_STORE, "My", CAPICOM_STORE_OPEN_READ_ONLY
	Set cSignerCertificates = oStore.Certificates
	
	Select Case cSignerCertificates.Count
		Case 0 
			MsgBox "Sertifika Bulunamadi, sisteme girme yetkiniz yoktur."
			Exit Function		
		Case Else
			On Error Resume Next            
			Set cSignerCertificates = cSignerCertificates.Select("Sertifikalar", "Sisteme Girmek Icin Sertifika Seciniz.")	
            If Err.number <> 0 Then	                
		        Exit Function				
		    End If 		    
			If (cSignerCertificates.Count = 0) Then
				msgbox "Hata : Sertifika Secimi Iptal edilmistir."
				Exit Function
			End If
			oSigner.Certificate = cSignerCertificates(1)
	End Select
	
	'sertifika sahibini ve vereni gonder (bu aynı zamanda from clause olacak)
	txtSahibi.value = oSigner.Certificate.GetInfo(CAPICOM_CERT_INFO_SUBJECT_EMAIL_NAME)
	txtVeren.value = oSigner.Certificate.GetInfo(CAPICOM_CERT_INFO_ISSUER_DNS_NAME)
	
	'imzalama zamanını ayarla (UTC time)
	oAttribute.Name = CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME
	oAttribute.Value = oUtilities.LocalTimeToUTCTime(Now)
	
	oSigner.AuthenticatedAttributes.Add oAttribute	
	
	'imzalanacak veriyi content'e at (emaillerde strconv kullanıyorduk burda gerek yok)
	oSignedData.Content = bodystr
	
	' veriyi imzala (base 64 formatinda output alıyoruz)
	dim szSignature
	szSignature = oSignedData.Sign(oSigner, True, CAPICOM_ENCODE_BASE64)	
	SertifikaBul = szSignature
	
End Function
