示例#1
0
        /// <summary>
        /// Constructor expecting resources needed to perform transmission using AS2. All task required to be done once for
        /// all requests using this instance is done here.
        /// </summary>
        public As2MessageSender(
            X509Certificate certificate,
            SMimeMessageFactory sMimeMessageFactory,
            ITimestampProvider timestampProvider,
            Func <HyperwaySecureMimeContext> secureMimeContext)
        {
            this.sMimeMessageFactory = sMimeMessageFactory;
            this.timestampProvider   = timestampProvider;
            this.secureMimeContext   = secureMimeContext;

            // Establishes our AS2 System Identifier based upon the contents of the CN= field of the certificate
            this.fromIdentifier = CertificateUtils.ExtractCommonName(certificate);
        }
示例#2
0
        public void Persist(
            ITransmissionRequest transmissionRequest,
            ITransmissionResponse transmissionResponse,
            Trace root)
        {
            Trace span = root.Child();

            span.Record(Annotations.ServiceName("persist statistics"));
            span.Record(Annotations.ClientSend());
            try
            {
                RawStatisticsBuilder builder = new RawStatisticsBuilder()
                                               .AccessPointIdentifier(this.ourAccessPointIdentifier).Direction(Direction.OUT)
                                               .DocumentType(transmissionResponse.GetHeader().DocumentType)
                                               .Sender(transmissionResponse.GetHeader().Sender)
                                               .Receiver(transmissionResponse.GetHeader().Receiver)
                                               .Profile(transmissionResponse.GetHeader().Process)
                                               .Date(transmissionResponse.GetTimestamp()); // Time stamp of reception of the receipt

                // If we know the CN name of the destination AP, supply that
                // as the channel id otherwise use the protocol name
                if (transmissionRequest.GetEndpoint().Certificate != null)
                {
                    String accessPointIdentifierValue =
                        CertificateUtils.ExtractCommonName(transmissionRequest.GetEndpoint().Certificate);
                    builder.Channel(new ChannelId(accessPointIdentifierValue));
                }
                else
                {
                    String protocolName = transmissionRequest.GetEndpoint().TransportProfile.Identifier;
                    builder.Channel(new ChannelId(protocolName));
                }

                DefaultRawStatistics rawStatistics = builder.Build();
                this.rawStatisticsRepository.Persist(rawStatistics);
            }
            catch (Exception ex)
            {
                span.Record(Annotations.Tag("exception", ex.Message));
                Logger.Error($"Persisting DefaultRawStatistics about oubound transmission failed : {ex.Message}", ex);
            }
            finally
            {
                span.Record(Annotations.ClientRecv());
            }
        }
示例#3
0
        protected HttpPost PrepareHttpRequest()
        {
            Trace span = this.root.Child();

            span.Record(Annotations.ServiceName("request"));
            span.Record(Annotations.ClientSend());
            try
            {
                HttpPost httpPost;

                // Create the body part of the MIME message containing our content to be transmitted.
                MimeEntity mimeBodyPart = MimeMessageHelper.CreateMimeBodyPart(
                    this.transmissionRequest.GetPayload(),
                    "application/xml");

                // Digest method to use.
                SMimeDigestMethod digestMethod =
                    SMimeDigestMethod.FindByTransportProfile(
                        this.transmissionRequest.GetEndpoint().TransportProfile);



                // Create a complete S/MIME message using the body part containing our content as the
                // signed part of the S/MIME message.
                MimeMessage signedMimeMessage =
                    this.sMimeMessageFactory.CreateSignedMimeMessage(mimeBodyPart, digestMethod);

                var signedMultipart = (signedMimeMessage.Body as MultipartSigned);
                Debug.Assert(signedMultipart != null, nameof(signedMultipart) + " != null");
                this.outboundMic = MimeMessageHelper.CalculateMic(signedMultipart[0], digestMethod);
                span.Record(Annotations.Tag("mic", this.outboundMic.ToString()));
                span.Record(Annotations.Tag("endpoint url", this.transmissionRequest.GetEndpoint().Address.ToString()));

                // Initiate POST request
                httpPost = new HttpPost(this.transmissionRequest.GetEndpoint().Address);

                foreach (var header in signedMimeMessage.Headers)
                {
                    span.Record(Annotations.Tag(header.Field, header.Value));
                    httpPost.AddHeader(header.Field, header.Value.Replace("\r\n\t", string.Empty));
                }
                signedMimeMessage.Headers.Clear();

                this.transmissionIdentifier = TransmissionIdentifier.FromHeader(httpPost.Headers[As2Header.MessageId]);

                // Write content to OutputStream without headers.
                using (var m = new MemoryStream())
                {
                    signedMultipart.WriteTo(m);
                    httpPost.Entity = m.ToBuffer();
                }

                var contentType = signedMultipart.Headers[HeaderId.ContentType];

                // Set all headers specific to AS2 (not MIME).
                httpPost.Host = "skynet.sediva.it";
                httpPost.Headers.Add("Content-Type", this.NormalizeHeaderValue(contentType));
                httpPost.Headers.Add(As2Header.As2From, this.fromIdentifier);
                httpPost.Headers.Add(
                    As2Header.As2To,
                    CertificateUtils.ExtractCommonName(this.transmissionRequest.GetEndpoint().Certificate));
                httpPost.Headers.Add(As2Header.DispositionNotificationTo, "*****@*****.**");
                httpPost.Headers.Add(
                    As2Header.DispositionNotificationOptions,
                    As2DispositionNotificationOptions.GetDefault(digestMethod).ToString());
                httpPost.Headers.Add(As2Header.As2Version, As2Header.Version);
                httpPost.Headers.Add(As2Header.Subject, "AS2 message from HYPERWAY");
                httpPost.Headers.Add(As2Header.Date, As2DateUtil.Rfc822.GetFormat(DateTime.Now));
                return(httpPost);
            }
            catch (Exception)
            {
                throw new HyperwayTransmissionException(
                          "Unable to stream S/MIME message into byte array output stream");
            }
            finally
            {
                span.Record(Annotations.ClientRecv());
            }
        }
示例#4
0
 public DefaultStatisticsService(IRawStatisticsRepository rawStatisticsRepository, X509Certificate certificate)
 {
     this.rawStatisticsRepository  = rawStatisticsRepository;
     this.ourAccessPointIdentifier = new AccessPointIdentifier(CertificateUtils.ExtractCommonName(certificate));
 }