Развернуть все
Свернуть все

Добавление подписанных атрибутов, работа с датами

HTML
<script language="javascript" src="cadesplugin_api.js"></script>

Приведенный пример использует синхронную версию плагина. Также см. пример работы с асинхронными обьектами.

JavaScript
    var CADESCOM_CADES_BES = 1;
    var CAPICOM_CURRENT_USER_STORE = 2;
    var CAPICOM_MY_STORE = "My";
    var CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED = 2;
    var CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME = 1;
    var CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME = 0;
    var CADESCOM_AUTHENTICATED_ATTRIBUTE_DOCUMENT_NAME = 1;


    function ConvertDate(date) {
        switch (navigator.appName) {
            case "Microsoft Internet Explorer":
                return date.getVarDate();
            default:
                return date;
        }
    }

    function SignCreate(certSubjectName, dataToSign) {
        var oStore = cadesplugin.CreateObject("CAdESCOM.Store");
        oStore.Open(CAPICOM_CURRENT_USER_STORE, CAPICOM_MY_STORE,
            CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);

        var oCertificates = oStore.Certificates.Find(
            CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME, certSubjectName);
        if (oCertificates.Count == 0) {
            alert("Certificate not found: " + certSubjectName);
            return;
        }
        var oCertificate = oCertificates.Item(1);

        var oSigningTimeAttr = cadesplugin.CreateObject("CADESCOM.CPAttribute");
        oSigningTimeAttr.Name = CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME;
        var oTimeNow = new Date();
        oSigningTimeAttr.Value = ConvertDate(oTimeNow);

        var oDocumentNameAttr = cadesplugin.CreateObject("CADESCOM.CPAttribute");
        oDocumentNameAttr.Name = CADESCOM_AUTHENTICATED_ATTRIBUTE_DOCUMENT_NAME;
        oDocumentNameAttr.Value = "Document Name";

        var oSigner = cadesplugin.CreateObject("CAdESCOM.CPSigner");
        oSigner.Certificate = oCertificate;
        oSigner.AuthenticatedAttributes2.Add(oSigningTimeAttr);
        oSigner.AuthenticatedAttributes2.Add(oDocumentNameAttr);

        var oSignedData = cadesplugin.CreateObject("CAdESCOM.CadesSignedData");
        oSignedData.Content = dataToSign;

        try {
            var sSignedMessage = oSignedData.SignCades(oSigner, CADESCOM_CADES_BES);
        } catch (err) {
            alert("Failed to create signature. Error: " + cadesplugin.getLastError(err));
            return;
        }

        oStore.Close();

        return sSignedMessage;
    }

    function Verify(sSignedMessage) {
        var oSignedData = cadesplugin.CreateObject("CAdESCOM.CadesSignedData");
        try {
            oSignedData.VerifyCades(sSignedMessage, CADESCOM_CADES_BES);
        } catch (err) {
            alert("Failed to verify signature. Error: " + cadesplugin.getLastError(err));
            return false;
        }

        var oSigner = oSignedData.Signers.Item(1);
        var SigningTime = new Date(oSigner.SigningTime);
        alert(SigningTime);
        return true;
    }


    function run() {
        var oCertName = document.getElementById("CertName");
        var sCertName = oCertName.value; // Здесь следует заполнить SubjectName сертификата
        if ("" == sCertName) {
            alert("Введите имя сертификата (CN).");
            return;
        }
        var signedMessage = SignCreate(sCertName, "Message");

        document.getElementById("signature").innerHTML = signedMessage;

        var verifyResult = Verify(signedMessage);
        if (verifyResult) {
            alert("Signature verified");
        }
    }