private IASSod(byte[] sod) { //Create asn1 structure of IAS sod object var asn1 = ASN1Tag.Parse(sod, true); //Take root element that represents all data signed var root = asn1.Child(0); root.Child(0, 06).Verify(OID.ToByteArray(OID.OIDsignedData)); //06 Verify that result is a OBJECT IDENTIFIER //Struttura che negli altri elementi verrà firmata SignedDataObject = root.DeepChild(1, 0, 2); SignedDataObject.Child(0, 06).Verify(OID.ToByteArray(OID.OIDldsSecurityObject)); //06 Verify that result is a OBJECT IDENTIFIER /*GET SIGNED DATA THAT CONTAINS HASHED INFORMATION OF: * - ID servizi * - Seriale carta * - Certificato utente * - Chiave pubblica di internal authentication * - Chiave pubblica di internal authentication per i servizi*/ SignedData = SignedDataObject.DeepChild(1, 0).Child(0, 0x30); //0x30 Verify that result is a SEQUENCE //GET DOCUMENT SIGNER CERTIFICATE //This object can be parsed to X509Certificate2 class costrusctor DSCertificate = root.DeepChild(1, 0, 3).Child(0, 0x30); //0x30 Verify that result is a SEQUENCE //GET SIGNER INFO var signerInfo = root.DeepChild(1, 0, 4).Child(0, 0x30); //0x30 Verify that result is a SEQUENCE /*GET INFORMATION OF ISSUER FROM SIGNER INFO * La struttura dati contenuta si avvicina alla seguente: * 0 SET (1 elem) * 0.0 SEQUENCE (2 elem) * 0.0.0 OBJECT IDENTIFIER (2.5.4.3 commonName (X.520 DN component)) * 0.0.1 PrintableString (Italian Country Signer CA - TEST) * 1 SET (1 elem) * 1.0 SEQUENCE (2 elem) * 1.0.0 OBJECT IDENTIFIER (2.5.4.11 organizationalUnitName (X.520 DN component)) * 1.0.1 PrintableString (National Electronic Center of State Police) * 2 SET (1 elem) * 2.0 SEQUENCE (2 elem) * 2.0.0 OBJECT IDENTIFIER (2.5.4.10 organizationName (X.520 DN component)) * 2.0.1 PrintableString (Ministry of Interior) * 3 SET (1 elem) * 3.0 SEQUENCE (2 elem) * 3.0.0 OBJECT IDENTIFIER (2.5.4.6 countryName (X.520 DN component)) * 3.0.1 PrintableString (IT)*/ IssuerName = signerInfo.DeepChild(1).Child(0, 0x30); //0x30 Verify that result is a SEQUENCE /*GET INFORMATION OF SIGNER CERTIFICATE SERIAL NUMBER FROM DS CERT * La struttura dati contenuta si avvicina alla seguente: * 0 INTEGER (value of serial number)*/ SerialNumber = signerInfo.DeepChild(1).Child(1, 0x02); //02 Verify that result is a INTEGER /*GET INFORMATION ABOUT SIGNER FROM DS CERT * La struttura dati contenuta si avvicina alla seguente: * 0 SEQUENCE (2 elem) * 0.0 OBJECT IDENTIFIER (1.2.840.113549.1.9.3 OIDcontentType) * 0.1 SET (1 elem) * 0.1.0 OBJECT IDENTIFIER (2.23.136.1.1.1 Security) * 1 SEQUENCE (2 elem) * 1.0 OBJECT IDENTIFIER (1.2.840.113549.1.9.4 OIDmessageDigest) * 1.1 SET (1 elem) * 1.1.0 OCTET STRING (F92C82AE7A944DCFFE4CCD0B6CAFA9D0134ED16EBEEFE6D9A44B641920F99BA9... (hash)) */ SignatureAlgoritmIdentifier = signerInfo.Child(3, 0xA0); //0xA0 Verify that result is a CONTEXT_SPECIFIC //Verify data structure of SignerInfo object SignatureAlgoritmIdentifier.DeepChild(0, 0).Verify(OID.ToByteArray(OID.OIDcontentType)); //Verify that node 0.0 is a OIDcontentType Object identifier SignatureAlgoritmIdentifier.DeepChild(0, 1, 0).Verify(OID.ToByteArray(OID.OIDldsSecurityObject)); //Verify that node 0.1.0 is a OIDldsSecurityObject Object identifier SignatureAlgoritmIdentifier.DeepChild(1, 0).Verify(OID.ToByteArray(OID.OIDmessageDigest)); //Verify that node 1.0 is a OIDmessageDigest Object identifier //Get message digest of Signer that is in the octect string object SignerMessageDigest = SignatureAlgoritmIdentifier.DeepChild(1, 1).Child(0, 04); //04 Verify that result is a OCTECT STRING //GET MESSAGE DIGEST SIGN ALGORYTM AND HASH ALGORYTM OIDDSMessageDigestSignAlgo = OID.ToString(signerInfo.DeepChild(4).Child(0, 06).Data); //06 Verify that result is a OBJECT IDENTIFIER OIDDSMessageDigestHashAlgo = OID.ToString(signerInfo.DeepChild(2).Child(0, 06).Data); //06 Verify that result is a OBJECT IDENTIFIER //Verify Signed data hash algoritm //SignedData.Child(0, 02).Verify(new byte[] { 0 }); SignedData.DeepChild(1).Child(0, 06).Verify(OID.Encode(OIDDSMessageDigestHashAlgo)); //Verify hash algoritm /*GET SIGNATURE * La struttura dati contenuta si avvicina alla seguente: * 0 OCTET STRING (42C600B32BD5EFF0A684F65BD4526872AD3D4EADA3017A6E836736340BCCDA7DB9622...)*/ Signature = signerInfo.Child(5, 04); //04 Verify that result is a OCTECT STRING }
public byte[] ReadNIS(string reader) { // Connection to reader var sc = _peripheral.Connect(reader, SCardShareMode.Shared, SCardProtocol.Any); if (sc != SCardError.Success) { throw new Exception(string.Format("Could not connect to reader {0}:\n{1}", reader, SCardHelper.StringifyError(sc))); } try { CommandApdu apdu; ResponseApdu rapdu; byte[] receiveBuffer; /* 00 A4 - 04 0C 0D - A0 00 00 00 30 80 00 00 00 09 81 60 01 Selezione Applet CIE */ receiveBuffer = new byte[2]; apdu = new CommandApdu(IsoCase.Case4Short, _peripheral.ActiveProtocol) { CLA = 0x00, INS = 0XA4, P1 = 0x04, P2 = 0x0C, Le = 0x0D, Data = new byte[] { (byte)0xA0, 0x00, 0x00, 0x00, 0x30, (byte)0x80, 0x00, 0x00, 0x00, 0x09, (byte)0x81, 0x60, 0x01 } }; sc = _peripheral.Transmit( SCardPCI.GetPci(_peripheral.ActiveProtocol), apdu.ToArray(), new SCardPCI(), ref receiveBuffer); /* 00 A4 - 04 0C 06 - A0 00 00 00 00 39 Selezione DF_CIE */ receiveBuffer = new byte[2]; apdu = new CommandApdu(IsoCase.Case4Short, _peripheral.ActiveProtocol) { CLA = 0x00, INS = 0XA4, P1 = 0x04, P2 = 0x0C, Le = 0x00, Data = new byte[] { (byte)0xA0, 0x00, 0x00, 0x00, 0x00, 0x39 } }; sc = _peripheral.Transmit( SCardPCI.GetPci(_peripheral.ActiveProtocol), apdu.ToArray(), new SCardPCI(), ref receiveBuffer); /* 00 B0 - 81 00 00 Lettura NIS */ receiveBuffer = new byte[14]; apdu = new CommandApdu(IsoCase.Case2Short, _peripheral.ActiveProtocol) { CLA = 0x00, INS = 0XB0, P1 = 0x81, P2 = 0x00, Le = 0x00 }; sc = _peripheral.Transmit( SCardPCI.GetPci(_peripheral.ActiveProtocol), apdu.ToArray(), new SCardPCI(), ref receiveBuffer); rapdu = new ResponseApdu(receiveBuffer, IsoCase.Case2Short, _peripheral.ActiveProtocol); var NIS_ID = rapdu.GetData(); /* 00 B0 - 85 00 00 Lettura chiave pubblica - 1*/ receiveBuffer = new byte[233]; apdu = new CommandApdu(IsoCase.Case2Short, _peripheral.ActiveProtocol) { CLA = 0x00, INS = 0XB0, P1 = 0x85, P2 = 0x00, Le = 0x00 }; sc = _peripheral.Transmit( SCardPCI.GetPci(_peripheral.ActiveProtocol), apdu.ToArray(), new SCardPCI(), ref receiveBuffer); rapdu = new ResponseApdu(receiveBuffer, IsoCase.Case2Short, _peripheral.ActiveProtocol); var pubkey1 = rapdu.GetData(); /* 00 B0 - 85 E7 00 - Lettura chiave pubblica - 2 */ receiveBuffer = new byte[233]; apdu = new CommandApdu(IsoCase.Case2Short, _peripheral.ActiveProtocol) { CLA = 0x00, INS = 0XB0, P1 = 0x85, P2 = 0xE7, Le = 0x00 }; sc = _peripheral.Transmit( SCardPCI.GetPci(_peripheral.ActiveProtocol), apdu.ToArray(), new SCardPCI(), ref receiveBuffer); rapdu = new ResponseApdu(receiveBuffer, IsoCase.Case2Short, _peripheral.ActiveProtocol); var pubkey2 = rapdu.GetData(); //Combine pubkey1 and pubkey2 to obtain ASN1 structure that contain 2 children, modulus and exponent //that is used to create RSA crypto service provider var pubKeyAsn1 = ASN1Tag.Parse(pubkey1.Combine(pubkey2)); /* 00 22 - 41 A4 06 - 80 01 02 84 01 83 - Selezione chiave int-auth */ receiveBuffer = new byte[2]; apdu = new CommandApdu(IsoCase.Case4Short, _peripheral.ActiveProtocol) { CLA = 0x00, INS = 0x22, P1 = 0x41, P2 = 0xA4, Le = 0x02, Data = new byte[] { 0x80, 0x01, 0x02, 0x84, 0x01, 0x83 } }; sc = _peripheral.Transmit( SCardPCI.GetPci(_peripheral.ActiveProtocol), apdu.ToArray(), new SCardPCI(), ref receiveBuffer); /* Generate random to perform sign and verify */ var challenge = ByteArrayOperations.GenerateRandomByteArray(8); /* 00 88 - 00 00 08 - hashChallenge 00 int-auth*/ receiveBuffer = new byte[258]; apdu = new CommandApdu(IsoCase.Case4Short, _peripheral.ActiveProtocol) { CLA = 0x00, INS = 0x88, P1 = 0x00, P2 = 0x00, Le = 0x00, Data = challenge }; sc = _peripheral.Transmit( SCardPCI.GetPci(_peripheral.ActiveProtocol), apdu.ToArray(), new SCardPCI(), ref receiveBuffer); rapdu = new ResponseApdu(receiveBuffer, IsoCase.Case4Short, _peripheral.ActiveProtocol); var signedData = rapdu.GetData(); //Verify challenge with public key using (var rsa = RSA.Create()) { var modulus = pubKeyAsn1.Child(0, 0x02).Data; // modulus. 02 Verify that result is a INTEGER var exp = pubKeyAsn1.Child(1, 0x02).Data; // exp. 02 Verify that result is a INTEGER if (!rsa.PureVerify(challenge, signedData, modulus, exp)) { throw new Exception("Unable to verify challenge"); } } //Read SOD data record var idx = 0; var size = 0xe4; byte[] data; byte[] sodIASData = new byte[0]; bool sodLoaded = false; while (!sodLoaded) { var hexS = idx.ToString("X4"); receiveBuffer = new byte[233]; apdu = new CommandApdu(IsoCase.Case4Short, _peripheral.ActiveProtocol) { CLA = 0x00, INS = 0xB1, P1 = 0x00, P2 = 0x06, Le = 0x00, Data = new byte[] { 0x54, 0x02, byte.Parse(hexS.Substring(0, 2), System.Globalization.NumberStyles.HexNumber), byte.Parse(hexS.Substring(2, 2), System.Globalization.NumberStyles.HexNumber) } }; sc = _peripheral.Transmit( SCardPCI.GetPci(_peripheral.ActiveProtocol), apdu.ToArray(), new SCardPCI(), ref receiveBuffer); rapdu = new ResponseApdu(receiveBuffer, IsoCase.Case4Short, _peripheral.ActiveProtocol); data = rapdu.GetData(); var offset = 2; if (data[1] > 0x80) { offset = 2 + (data[1] - 0x80); } var buf = data.SubArray(offset, data.Length - offset); sodIASData = sodIASData.Combine(buf); idx += size; if (data[2] != 0xe4) { sodLoaded = true; } } //Create IAS ASN1 object var ias = IASSod.Create(sodIASData); var isValid = ias.Verify(NIS_ID); //Verify integrity of data if (isValid) { return(NIS_ID); } return(null); } catch (Exception ex) { throw ex; } finally { _peripheral.Disconnect(SCardReaderDisposition.Reset); } }