public virtual byte[] processCommand(Apdu apdu) { CardContext context = handler.Context; if (context.CurDF == null) { return(Error.ClaNotValid); } TLV fciExt = new TLV(apdu.Data); if (fciExt[0x62] == null) { return(Error.DataFieldNotValid); } TLV fci = new TLV(fciExt[0x62]); var Size1 = fci[0x80]; var Size2 = fci[0x81]; var Options = fci[0x82]; var Id = fci[0x83]; var Fixed = fci[0x85]; var AC = fci[0x86]; var SM = card.GetSMTLV(fci); if (Size1 != null && Size2 != null) { return(Error.DataFieldNotValid); } if (Options == null) { return(Error.DataFieldNotValid); } if (Id == null) { return(Error.DataFieldNotValid); } if (Fixed == null) { return(Error.DataFieldNotValid); } if (AC == null) { return(Error.DataFieldNotValid); } if (Options.Length != 3) { return(Error.DataFieldNotValid); } if (Id.Length != 2) { return(Error.DataFieldNotValid); } if (Fixed.Length != 1) { return(Error.DataFieldNotValid); } if (Fixed[0] != 1) { return(Error.DataFieldNotValid); } if (Options[0] == 0x38 && Size2 == null) { return(Error.DataFieldNotValid); } if (!handler.IsVerifiedAC(context.CurDF, DF_AC.AC_CREATE)) { return(Error.SecurityStatusNotSatisfied); } ushort newId = Util.ToUShort(Id); if (newId == 0x3F00 || newId == 0x3FFF || newId == 0xFFFF) { return(Error.DataFieldNotValid); } if (context.CurDF.GetChildEForDF(newId) != null) { return(Error.FileAlreadyExists); } CardSelectable obj = null; if (Options[0] == 0x38) { obj = card.CreateDF(newId, context.CurDF); } else if (Options[0] == 0x01) { obj = card.CreateEF(newId, context.CurDF, Util.ToUInt(Size1)); } else if (Options[0] == 0x02) { obj = new EFLinearFixed(newId, card, context.CurDF, Util.ToUInt(Size1), Options[2]); } else if (Options[0] == 0x05) { obj = card.CreateEFLinearTLV(newId, context.CurDF, Util.ToUInt(Size1)); } else if (Options[0] == 0x06) { obj = new EFCyclic(newId, card, context.CurDF, Util.ToUInt(Size1), Options[2]); } else { return(Error.DataFieldNotValid); } if (AC != null) { obj.AC.Set(AC); } if (SM != null) { obj.SM.Set(SM); } if (obj != null) { context.CurFile = obj; } return(Error.Ok); }
public virtual byte[] processCommand(Apdu apdu) { CardContext context = handler.Context; if (apdu.P2 != 0) { return(Error.P1OrP2NotValid); } CardSelectable obj = null; switch (apdu.P1) { case 0: if (apdu.Data == null || apdu.Data.Length == 0) { obj = card.MasterFile; } else { if (apdu.Data.Length != 2) { return(Error.DataFieldNotValid); } ushort id = Util.ToUShort(apdu.Data); if (id == 0x3f00) { obj = card.MasterFile; } else { obj = context.CurDF.GetChildEForDF(id); if (obj == null && context.CurDF.Parent != null) { obj = context.CurDF.Parent.GetChildEForDF(id); } if (obj == null && context.CurDF.Parent != null && id == context.CurDF.ID) { obj = context.CurDF.Parent; } } } if (obj == null) { return(Error.FileNotFound); } context.CurFile = obj; break; case 1: if (apdu.Data == null || apdu.Data.Length != 2) { return(Error.DataFieldNotValid); } ushort id2 = Util.ToUShort(apdu.Data); obj = context.CurDF.GetChildDF(id2); if (obj == null) { return(Error.FileNotFound); } context.CurFile = obj; break; case 2: if (apdu.Data == null || apdu.Data.Length != 2) { return(Error.DataFieldNotValid); } ushort id3 = Util.ToUShort(apdu.Data); obj = context.CurDF.GetChildDF(id3); if (obj == null) { return(Error.FileNotFound); } context.CurFile = obj; break; case 3: if (apdu.Data != null && apdu.Data.Length != 0) { return(Error.DataFieldNotValid); } if (context.CurDF.Parent == null) { return(Error.FileNotFound); } context.CurFile = context.CurDF.Parent; break; case 4: if (apdu.Data == null || apdu.Data.Length == 0) { return(Error.DataFieldNotValid); } DF namedDF = handler.GetNamedDF(card.MasterFile, apdu.Data); if (namedDF == null) { return(Error.FileNotFound); } context.CurFile = namedDF; break; case 8: if (apdu.Data == null || apdu.Data.Length == 0) { obj = card.MasterFile; } else if (apdu.Data != null && apdu.Data.Length == 2 && Util.ToUShort(apdu.Data) == 0x3f00) { obj = card.MasterFile; } else { if ((apdu.Data.Length % 2) != 0) { return(Error.DataFieldNotValid); } obj = handler.GetSelectablePath(card.MasterFile, apdu.Data, 0); if (obj == null) { return(Error.FileNotFound); } } context.CurFile = obj; break; case 9: if (apdu.Data == null || apdu.Data.Length == 0) { return(Error.DataFieldNotValid); } else { if ((apdu.Data.Length % 2) != 0) { return(Error.DataFieldNotValid); } if (context.CurDF == null) { return(Error.ClaNotValid); } obj = handler.GetSelectablePath(context.CurDF, apdu.Data, 0); if (obj == null) { return(Error.FileNotFound); } } context.CurFile = obj; break; default: return(Error.P1OrP2NotValid); } if (context.CurFile == null) { return(Error.FileNotFound); } var outData = (context.CurFile as IObjectWithFCI).FCI; TLV tlv = new TLV(); TLV tlv2 = new TLV(); tlv2.elems.AddRange(outData); tlv.addTag(0x6f, tlv2.GetBytes()); return(Util.Response(tlv.GetBytes(), Error.Ok)); }