Pages

Wednesday, June 8, 2011

InfoPath SSL datasource problem

Recently I had completed a solution which required an InfoPath Forms Services form to submit directly to a Tibco ESB generated web service. As the data being submitted was sensitive it was a requirement that the data connection setup in InfoPath be a SSL connection. 


I began to encounter problems while the form was being used through the browser. Whenever the form tried to access the SSL datasource I was prompted with the annoying "Cannot access datasource" screen. So I started to investigate...


Errors being generated
SharePoint Logs
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.     at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)     at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)     at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)     at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)     at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)     at System.Threading.ExecutionContext.runTryCode(Object userData)     at Syste...

Windows error log

Event Type:   Warning
Event Source: Office SharePoint Server
Event Category:       Forms Services Runtime - Data Connections
Event ID:       5566
Date:            26/05/2011
Time:            2:36:00 PM
User:            N/A
Computer:     SERVER01
Description:
The following query failed: PGSconnection (User: Domain\user, Form Name: InfoPath_Test_Form, IP: , Request: http://testintranet/_layouts/FormServer.aspx?XsnLocation=http://testintranet/FormServerTemplates/InfoPath_Test_Form.xsn&SaveLocation=http://testintranet/applications/Forms/FormLib&Source=http://testintranet/applications/Forms/Form/Forms/AllItems.aspx&DefaultItemOpen=1, Form ID: urn:schemas-microsoft-com:office:infopath:InfoPath-Test-Form:-myXSD-2011-05-12T01-55-04, Type: DataAdapterException, Exception Message: The underlying connection was closed: An unexpected error occurred on a send.
The underlying connection was closed: An unexpected error occurred on a send.)

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.


From the SharePoint log I could see that the process was bombing out just as Forms Services was trying to access the SSL certificate. 


I found that with the ServicePoint .Net class you could force Forms Services to use SSLv3 and select any certificate that was available on the server. There is also an option to select a local copy of your .pfx file.


Steps to resolve the issue.

  1. Install VSTA for office:- Control Panel > Add/Remove programs > Change office 2007 > Click on add or remove Features
  2. Once installed open InfoPath and go to Tools menu > Programming > Microsoft Visual Studio Tools Application.
  3. Add the below code to your form and resolve any namespaces
  4. Compile and Save your code
  5. Ensure that you have set the form to full trust under Form Options > Security
  6. Publish your form as an administrator approved form.


using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace InfoPath_Test_Form
{
    public partial class FormCode
    {
        // Member variables are not supported in browser-enabled forms.
        // Instead, write and read these values from the FormState
        // dictionary using code such as the following:
        //
        // private object _memberVariable
        // {
        //     get
        //     {
        //         return FormState["_memberVariable"];
        //     }
        //     set
        //     {
        //         FormState["_memberVariable"] = value;
        //     }
        // }

        // NOTE: The following procedure is required by Microsoft Office InfoPath.
        // It can be modified using Microsoft Office InfoPath.
        public void InternalStartup()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate,X509Chain chain,SslPolicyErrors sslPolicyErrors)
{
                  return true;
};
        }
    }
}

Applies to: InfoPath 2007, SharePoint 2007, Tibco ESB