示例#1
0
            public IGeneratePinEncryptionKeyResponse Generate(string exchangeKey, KeyScheme?exchangeKeyScheme, KeyScheme?storageKeyScheme)
            {
                int keyLength = exchangeKey.Length;

                //configure Parameters
                var parameters = new List <string>()
                {
                    exchangeKey,
                };

                if (exchangeKeyScheme.HasValue || storageKeyScheme.HasValue)
                {
                    parameters.Add(";");
                    parameters.Add(exchangeKeyScheme.HasValue ? ((char)exchangeKeyScheme.Value).ToString() : ((char)storageKeyScheme.Value).ToString());
                    parameters.Add(storageKeyScheme.HasValue ? ((char)storageKeyScheme.Value).ToString() : ((char)exchangeKeyScheme.Value).ToString());
                    parameters.Add("1");
                }

                //configure Response Format
                var responseFormat = new ThalesSim.Core.Message.XML.MessageFields();

                responseFormat.Fields.AddRange(new List <MessageField>()
                {
                    new MessageField()
                    {
                        Name = "zmk", Length = exchangeKey.Length
                    },
                    new MessageField()
                    {
                        Name = "lmk", Length = exchangeKey.Length
                    },
                    new MessageField()
                    {
                        Name = "checkValue", Length = 6
                    }
                });
                var hsmResponse = _theHsm.Send("IA", parameters, responseFormat);

                IGeneratePinEncryptionKeyResponse response = new GeneratePinEncryptionKeyResponse();

                response.PinEncryptionKeyUnderExchangeKey = hsmResponse.Item("zmk");
                response.PinEncryptionKeyUnderStorageKey  = hsmResponse.Item("lmk");
                response.CheckValue = hsmResponse.Item("checkValue");

                return(response);
            }
示例#2
0
        private ThalesSim.Core.Message.XML.MessageKeyValuePairs Send(string command, List <string> parameters, ThalesSim.Core.Message.XML.MessageFields responseFormat)
        {
            //Build Function Command
            StringBuilder commandBuilder = new StringBuilder(string.Format("{0}{1}", this.MessageHeader, command));

            foreach (string parameter in parameters)
            {
                commandBuilder.Append(parameter);
            }

            this.Connect();
            string reply = string.Empty;

            try
            {
                reply = SendFunctionCommand(commandBuilder.ToString());
            }
            catch
            {
                throw;
            }
            finally
            {
                this.Close();
            }

            if (string.IsNullOrEmpty(reply))
            {
                throw new ApplicationException("No reply from HSM");
            }

            int    respPosition = _headerLength + 2;
            string responseCode = reply.Substring(respPosition, 2);

            //string[] fbres = { "00", "01" };
            //if (reply.ToLower().Contains("fb") && !fbres.Contains(responseCode))
            //{
            //    throw new ApplicationException(responseCode);
            //}

            if (responseCode != "00")
            {
                if (responseCode == "02")
                {
                    //new PANE.ERRORLOG.Error().LogInfo("HSM response 02: Key inappropraite length for algorithm");
                }
                else if (reply.ToLower().Contains("fb") && reply.Length < respPosition + 2)  //if the reply is greater than the header+responsecode then it is fine
                {
                    throw new ApplicationException(responseCode);
                }
                else if (!reply.ToLower().Contains("fb"))
                {
                    throw new ApplicationException(responseCode);
                }
            }

            var msg = new ThalesSim.Core.Message.Message(reply);

            //Build Response Format
            responseFormat.Fields.InsertRange(0, new List <MessageField>()
            {
                new ThalesSim.Core.Message.XML.MessageField()
                {
                    Name = "messageHeader", Length = _headerLength
                },
                new ThalesSim.Core.Message.XML.MessageField()
                {
                    Name = "command", Length = 2
                },
                new ThalesSim.Core.Message.XML.MessageField()
                {
                    Name = "response", Length = 2
                }
            });

            ThalesSim.Core.Message.XML.MessageKeyValuePairs results = new ThalesSim.Core.Message.XML.MessageKeyValuePairs();
            string result = null;

            ThalesSim.Core.Message.XML.MessageParser.Parse(msg, responseFormat, ref results, ref result);
            return(results);
        }