Ejemplo n.º 1
0
        private byte[] ComputeHash(EstEIDReader estEidReader, byte[] data)
        {
            uint       rc, digest_length = 0;
            const uint hash_length = Digest.SHA1_LENGTH;

            Digest hash = new Digest(estEidReader);

            rc = hash.InitDigest(Digest.HashAlgorithm.SHA1);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_INIT);
            }

            rc = hash.UpdateDigest(data, (uint)data.Length);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_UPDATE);
            }

            HGlobalSafeHandle raw       = new HGlobalSafeHandle((int)hash_length);
            IntPtr            hashBytes = raw;

            if (hashBytes == IntPtr.Zero)
            {
                throw new OutOfMemoryException(Resources.OUT_OF_MEMORY);
            }

            rc = hash.FinalizeDigest(ref hashBytes, ref digest_length);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_FINALIZE);
            }

            return(raw.ToByteArray());
        }
Ejemplo n.º 2
0
        private byte[] ComputeHash(EstEIDReader estEidReader, PdfSignatureAppearance sap)
        {
            Digest     hash          = new Digest(estEidReader);
            const uint hash_length   = Digest.SHA1_LENGTH;
            uint       digest_length = 0;
            uint       rc;

            rc = hash.InitDigest(Digest.HashAlgorithm.SHA1);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_INIT);
            }

            Stream       s    = sap.RangeStream;
            MemoryStream ss   = new MemoryStream();
            int          read = 0;

            byte[] buff = new byte[8192];
            while ((read = s.Read(buff, 0, 8192)) > 0)
            {
                ss.Write(buff, 0, read);
                rc = hash.UpdateDigest(buff, (uint)read);
                if (rc != EstEIDReader.ESTEID_OK)
                {
                    throw new Exception(Resources.CARD_HASH_UPDATE);
                }
            }

            HGlobalSafeHandle raw       = new HGlobalSafeHandle((int)hash_length);
            IntPtr            hashBytes = raw;

            if (hashBytes == IntPtr.Zero)
            {
                throw new OutOfMemoryException(Resources.OUT_OF_MEMORY);
            }

            rc = hash.FinalizeDigest(ref hashBytes, ref digest_length);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_FINALIZE);
            }

            return(raw.ToByteArray());
        }
Ejemplo n.º 3
0
        private byte[] EstEIDCardSign(EstEIDReader estEidReader, PKCS11Signer signer, Byte[] digest)
        {
            uint       rc, slot;
            const uint rsa_length    = SIGNATURE_LENGTH;
            uint       digest_length = rsa_length;
            string     pin           = string.Empty;
            TokenInfo  token         = null;

            HGlobalSafeHandle raw      = new HGlobalSafeHandle((int)rsa_length);
            IntPtr            rsaBytes = raw;

            if (rsaBytes == IntPtr.Zero)
            {
                throw new OutOfMemoryException(Resources.OUT_OF_MEMORY);
            }

            token = signer.Token;
            slot  = signer.Slot;

            if (token.LoginRequired && token.PinIsSet)
            {
                if (!token.PinPadPresent)
                {
                    pin = ReadPin(token);

                    // redraw window: single-threaded UI
                    Application.DoEvents();

                    // user requested Cancel ?
                    if (pin == null)
                    {
                        throw new CancelException(Resources.ACTION_CANCELED);
                    }
                    // no pin supplied ?
                    else if (pin == string.Empty)
                    {
                        throw new Exception(Resources.NO_PIN_SUPPLIED);
                    }
                }
                else
                {
                    // we have a PINPAD present
                    pin = null;
                    statusHandler(string.Format(Resources.UI_ENTER_PIN_ONTHE_PINPAD, token.Label), false);
                }
            }

            rc = estEidReader.Sign(slot, pin, digest, (uint)digest.Length, ref rsaBytes, ref digest_length);
            // failure ?
            if (rc != EstEIDReader.ESTEID_OK)
            {
                // signing cancelled or timed out
                if (rc == PKCS11Error.CKR_FUNCTION_CANCELED)
                {
                    throw new CancelException(Resources.ACTION_CANCELED);
                }

                throw new PKCS11Exception(rc);
            }

            return(raw.ToByteArray());
        }