Example #1
0
        public void ValidationTestFailure()
        {
            var order = new OstcAntrag()
            {
                Antragsteller = new OstcAntragAntragsteller()
                {
                    IK_BN = "12345678",
                },
            };
            var encoding = Encoding.GetEncoding("iso-8859-1");
            var data = OstcUtils.Serialize(order, encoding);

            var validator = new OstcExtraValidator(OstcMessageType.ApplicationData, ExtraTransportDirection.Request);
            var ex = Assert.Throws<XmlSchemaValidationException>(() => validator.Validate(data));
            Assert.Equal("The 'IK_BN' element is invalid - The value '12345678' is invalid according to its datatype 'sType_an10_11' - The actual length is less than the MinLength value.", ex.Message);
        }
Example #2
0
 /// <summary>
 /// Erstellung des OSTC-Client-Nutzers (Absender) anhand des Antrags
 /// </summary>
 /// <param name="application">Antrag</param>
 public OstcSender(OstcAntrag application)
     : this(SenderId.FromBnr(application.Antragsteller.IK_BN), application.Antragsteller.Firma)
 {
 }
Example #3
0
        /// <summary>
        /// Antragstellung
        /// </summary>
        /// <param name="application">Antrag</param>
        /// <param name="certStore">Zertifikat-Speicher für die Ermittlung der Zertifikatskette des Absender-Zertifikats</param>
        /// <param name="pfx">Absender-Zertifikat für die Signierung des Antrags</param>
        /// <returns>Ergebnis der Antragstellung</returns>
        public async Task <OstcApplicationResult> SendApplicationAsync([NotNull] OstcAntrag application, [CanBeNull] IOstcCertificateStore certStore, [CanBeNull] Pkcs12Store pfx)
        {
            var now = DateTime.Now;

            application.Antragsteller.IK_BN = Sender.SenderId.ToString();

            RsaPrivateCrtKeyParameters rsaPrivateKey;
            AsymmetricCipherKeyPair    rsaKeyPair;
            X509Certificate            certificate;

            if (pfx != null)
            {
                var alias = pfx.Aliases.Cast <string>().First(pfx.IsKeyEntry);
                rsaPrivateKey = (RsaPrivateCrtKeyParameters)pfx.GetKey(alias).Key;
                var rsaPublicKey = new RsaKeyParameters(false, rsaPrivateKey.Modulus, rsaPrivateKey.PublicExponent);
                rsaKeyPair  = new AsymmetricCipherKeyPair(rsaPublicKey, rsaPrivateKey);
                certificate = pfx.GetCertificate(alias).Certificate;
            }
            else
            {
                var keyPairGen = new RsaKeyPairGenerator();
                keyPairGen.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
                rsaKeyPair    = keyPairGen.GenerateKeyPair();
                rsaPrivateKey = (RsaPrivateCrtKeyParameters)rsaKeyPair.Private;
                certificate   = null;
            }

            var requester  = new Requester(application.Antragsteller.IK_BN, application.Antragsteller.Firma, application.Antragsteller.Nachname);
            var p10Creator = new Pkcs10Creator(requester, rsaKeyPair);
            var p10Data    = p10Creator.CreateRequest();

            application.Antragsinfo.Requestschlüssel = p10Data.CertRequestDer;

            var applicationData = OstcUtils.Serialize(application, Iso88591);

            ValidateData(applicationData, OstcMessageType.ApplicationData);

            var receiver = Sender.SenderId.CommunicationServerReceiver;

            if (certificate != null)
            {
                Debug.Assert(certStore != null, "certStore != null");
                var certChain = certStore.GetChain(certificate).ToList();
                Debug.Assert(certChain[0].SubjectDN.Equivalent(certificate.SubjectDN));
                certChain.RemoveAt(0);
                applicationData = OstcUtils.SignData(applicationData, rsaPrivateKey, certificate, certChain);
                var receiverCert = certStore.GetCertificate(receiver);
                applicationData = OstcUtils.EncryptData(applicationData, receiverCert);
            }

            var fileName = $"{application.Antragsteller.IK_BN}_{now.Date:ddMMyyyy}.xml";
            var message  = new TransportRequestType()
            {
                version         = SupportedVersionsType.Item11,
                profile         = ExtraProfileOstc,
                TransportHeader = CreateRequestHeader(now, OstcDataType.Application, ExtraScenario.RequestWithAcknowledgement),
                TransportBody   = new TransportRequestBodyType
                {
                    Items = new object[]
                    {
                        new DataType
                        {
                            Item = new Base64CharSequenceType()
                            {
                                Value = applicationData,
                            },
                        },
                    },
                },
            };

            if (certificate != null)
            {
                message.TransportPlugIns = new AnyPlugInContainerType
                {
                    Items = new object[]
                    {
                        new DataTransformsType
                        {
                            version    = "1.1",
                            Encryption = new[]
                            {
                                new EncryptionType
                                {
                                    order     = "1",
                                    Algorithm = new EncryptionAlgorithmType
                                    {
                                        id            = ExtraEncryption.Pkcs7,
                                        Specification = new SpecificationType
                                        {
                                            url  = "http://www.gkv-datenaustausch.de",
                                            name = "Security-Schnittstelle fuer den Datenaustausch im Gesundheitswesen",
                                        },
                                    },
                                },
                            },
                        },
                        new DataSourceType
                        {
                            version       = "1.1",
                            DataContainer = new DataContainerType
                            {
                                type             = ExtraContainerType.File,
                                name             = fileName,
                                created          = now,
                                createdSpecified = true,
                                encoding         = ExtraContainerEncoding.Utf8,
                            }
                        },
                    },
                };
            }

            ValidateRequest(message, OstcMessageType.Application);

            var messageData = OstcExtraSerializer.Iso88591.Serialize(message);
            var request     = CreateRequest(Network.Requests.Application);

            using (var requestStream = await Task.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null))
            {
                requestStream.Write(messageData, 0, messageData.Length);
            }

            using (var response = await Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null))
            {
                var serializer   = new XmlSerializer(typeof(TransportResponseType));
                var responseData = (TransportResponseType)serializer.Deserialize(response.GetResponseStream());

                var flags = responseData.TransportHeader.GetFlags().ToList();
                if (flags.Any(x => x.weight == ExtraFlagWeight.Error))
                {
                    throw new Ostc2Exception(flags);
                }

                return(new OstcApplicationResult
                {
                    OrderId = responseData.TransportHeader.ResponseDetails.ResponseID.Value,
                    Pkcs10 = p10Data.CertRequestDer,
                    RSA = rsaPrivateKey,
                    Hash = p10Data.PublicKeyHashRaw,
                });
            }
        }
Example #4
0
        /// <summary>
        /// Erstellung des OSTC-Client-Nutzers (Absender) anhand des Antrags
        /// </summary>
        /// <param name="application">Antrag</param>
        public OstcSender(OstcAntrag application)
            : this(SenderId.FromBnr(application.Antragsteller.IK_BN), application.Antragsteller.Firma)
        {

        }
Example #5
0
 /// <summary>
 /// Antragstellung
 /// </summary>
 /// <param name="application">Antrag</param>
 /// <returns>Ergebnis der Antragstellung</returns>
 /// <remarks>
 /// Es wird für die PKCS#10-Erstellung ein neuer RSA-Schlüssel erstellt. Es ist zwingend notwendig, dass der private
 /// Teil des RSA-Schlüssels vom Aufrufer gespeichert wird, weil ohne diesen Bestandteil keine Entschlüsselung, bzw. Signatur
 /// mit dem von der OSTC zurückgelieferten Zertifikat durchgeführt werden kann.
 /// </remarks>
 public async Task <OstcApplicationResult> SendApplication([NotNull] OstcAntrag application)
 {
     return(await SendApplicationAsync(application, null, null));
 }