【问题标题】:How do I set keys on a global platform off-card repository?如何在全球平台卡外存储库上设置密钥?
【发布时间】:2012-09-02 20:05:50
【问题描述】:

我需要帮助来设置全球平台的卡上和卡外部分之间的安全通道。如果我让自己不清楚,希望举个例子来简化我对问题的理解。

例子:

如果我使用 JCOP shell,我需要在 ext-auth 之前输入 set-key 命令,如下所示:

cm> 设置密钥 255/1/DES-ECB/404142434445464748494a4b4c4d4e4f
cm> 设置密钥 255/2/DES-ECB/404142434445464748494a4b4c4d4e4f
cm> 设置密钥 255/3/DES-ECB/404142434445464748494a4b4c4d4e4f
厘米> 初始化更新 255
cm> ext-auth

现在我需要在 c# 中做同样的事情(set-key)。但是,我在 JCOP shell 中找不到 set-key 命令背后发生了什么,所以我不知道如何从 c# 中设置密钥。

【问题讨论】:

    标签: c# channel javacard


    【解决方案1】:

    Bahribayli 发布的 C# sn-p 是对该问题的更接近的答案。但是我要在这里添加解释,因为我的祖母正在向我询问这件事,我希望她在不涉及任何说明的情况下理解。

    offcardcard 需要具有对称密钥值的 a-priori 知识。 offcardcard 需要对这些键的数字索引位置有一个a-priori 协议。现在,这里有两种情况。第一种场景是offcard作为查询客户端,通过initialize updateapdu命令的p1参数主动指定这个key的索引号,card会在里面搜索这个索引对应的key。第二种情况是,initialize update apdu 命令中的p1 值为零,这意味着offcardcard 积极决定使用哪个索引,然后offcard 将查找对应的键这个指数。在这两种情况下,最终结果是offcardcard 现在已经同意一个索引值,希望指向相同的键以便开始安全地对话。如果这些键的值不同,尽管它们在同一个索引上,那么 initialize update 会失败,并且无法继续进行安全通信。

    set-key 命令是您代表offcard 执行的命令,预期存在并准备在两侧指向相同键的键索引。由于仅通过不安全的通道交换索引值,cardoffcard 仍然需要确定该索引号是否确实指向双方的相同键。从这里开始,这就像*** boy meets girl *** 求爱事件,其中男孩将开始与女孩进行奇怪的神秘对话以计算某些东西,而女孩也会这样做。如果索引确实指向相同的键,这个初始的神秘对话将验证,第二阶段从这里开始。双方创建session keys 作为继续通话的键。

    【讨论】:

      【解决方案2】:

      看看globalplatform.net,它是.Net Framework 全球平台规范的开源实现。下面的代码 sn -p 展示了如何使用它:

          SCardContext context = new SCardContext();
          context.Establish(SCardScope.System);
          string[] readers = context.GetReaders();
          IsoReader isoReader = new IsoReader(context, readers[3], SCardShareMode.Shared, SCardProtocol.Any, false);
          CommandApdu apdu = new CommandApdu(IsoCase.Case4Short, isoReader.ActiveProtocol)
          {
              CLA = 0x00,
              Instruction = InstructionCode.SelectFile,
              P1 = 0x04,
              P2 = 0x00,
              Data = new byte[] { 0xa0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 }
      
          };
      
          Response response = isoReader.Transmit(apdu);
      
          GlobalPlatform gp = new GlobalPlatform();
          KeySet scKeys = new KeySet();
          scKeys.MacKey = new Key("404142434445464748494a4b4c4d4e4f");
          scKeys.EncKey = new Key("404142434445464748494a4b4c4d4e4f");
          scKeys.KekKey = new Key("404142434445464748494a4b4c4d4e4f");
          CommandAPDU initUpdate = gp.CreateInitUpdateCommand(scKeys,
              SecurityLevel.C_DECRYPTION | SecurityLevel.C_MAC, GlobalPlatform.SCP_ANY, GlobalPlatform.IMPL_OPTION_ANY);
          apdu = new CommandApdu(IsoCase.Case4Short, isoReader.ActiveProtocol)
          {
              CLA = (byte)initUpdate.CLA,
              INS = (byte)initUpdate.INS,
              P1 = (byte)initUpdate.P1,
              P2 = (byte)initUpdate.P2,
              Data = initUpdate.Data
          };
          response = isoReader.Transmit(apdu);
          if (response.SW1 == 0x6C)
          {
              apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol)
              {
                  CLA = 0x00,
                  Instruction = InstructionCode.GetResponse,
                  P1 = 0x00,
                  P2 = 0x00,
                  Le = response.SW2
      
              };
      
              response = isoReader.Transmit(apdu);
      
          }
      
          byte[] responseData = response.GetData();
      
          gp.ProcessInitUpdateResponse(new ResponseAPDU(response.SW1, response.SW2, responseData));
      
          CommandAPDU extAuth = gp.CreateExternalAuthCommand();
          apdu = new CommandApdu(IsoCase.Case3Short, isoReader.ActiveProtocol)
          {
              CLA = (byte)extAuth.CLA,
              INS = (byte)extAuth.INS,
              P1 = (byte)extAuth.P1,
              P2 = (byte)extAuth.P2,
              Data = extAuth.Data
          };
      
          response = isoReader.Transmit(apdu);
      
          gp.ProcessExternalAuthResponse(new ResponseAPDU(response.SW1, response.SW2, response.GetData()));
      
          Key newMacKey = new Key("505152535455565758595a5b5c5d5e5f", 0, 0);
          Key newEncKey = new Key("505152535455565758595a5b5c5d5e5f", 1, 0);
          Key newKekKey = new Key("505152535455565758595a5b5c5d5e5f", 2, 0);
      
          List<Key> newKeys = new List<Key>();
          newKeys.Add(newMacKey);
          newKeys.Add(newEncKey);
          newKeys.Add(newKekKey);
      
          CommandAPDU putKey = gp.CreatePutKeyCommand(newKeys, true, false, GlobalPlatform.KEY_FORMAT_1);
      
          apdu = new CommandApdu(IsoCase.Case3Short, isoReader.ActiveProtocol)
          {
              CLA = (byte)putKey.CLA,
              INS = (byte)putKey.INS,
              P1 = (byte)putKey.P1,
              P2 = (byte)putKey.P2,
              Data = putKey.Data
          };
      
          response = isoReader.Transmit(apdu);
          if (response.SW1 == 0x6C)
          {
              apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol)
              {
                  CLA = 0x00,
                  Instruction = InstructionCode.GetResponse,
                  P1 = 0x00,
                  P2 = 0x00,
                  Le = response.SW2
      
              };
      
              response = isoReader.Transmit(apdu);
      
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        set-key 命令设置密钥 - 或者在您的示例中,设置客户端 3DES 密钥的 3 个部分。此命令不会向卡发送 APDU。

        随后执行用于建立安全通道的全球平台INITIALIZE UPDATE,该安全通道将以APDU的形式发送到卡。在此步骤中,重要的部分是将主机端生成的质询发送到卡 - 卡生成的质询从卡发送回。

        然后EXTERNAL AUTHENTICATE 命令被发送到卡。

        有关INITIALIZE UPDATEEXTERNAL AUTHENTICATE 命令的详细信息(APDU 的具体值),请参阅GlobalPlatform Card Specification。它还描述了可用于建立安全通道的两种不同协议。

        【讨论】:

        • 嗨,我都知道。但是,我不知道如何从 c# 设置这些键。需要访问 GlobalPlatform 卡外部分,但我不知道该怎么做......你能帮忙吗?顺便说一句,谢谢你的回答:)
        • 您应该阅读例如“安全通道协议'02'”章节的GP-Spec。它包含有关加密操作的详细信息 - 以及在何处以及如何使用 set-key 命令设置的密钥。
        猜你喜欢
        • 1970-01-01
        • 2015-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多