//Function import the certificate to the machine store and sets the friendly name static bool importCert() { try { //Create Certificate Object _certificate = new CertificateClass(); //Load the certificate into the obejct from file _certificate.Load(_CertPath, "", CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_EXPORTABLE, CAPICOM_KEY_LOCATION.CAPICOM_LOCAL_MACHINE_KEY); //Create extended property Class for friendly name _friendlyProp = new ExtendedPropertyClass(); _friendlyProp.PropID = CAPICOM_PROPID.CAPICOM_PROPID_FRIENDLY_NAME; _friendlyProp.set_Value(CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BINARY, _FriendlyName); //Add extendedProp on cert object _extendedProp = _certificate.ExtendedProperties(); //Set extendded prop to friendly name object _extendedProp.Add(_friendlyProp); _oCurrStore.Add(_certificate); return(true); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(_CertPath); return(true); } }
private byte[] CoSignBuffer(byte[] data, byte[] signdata) { X509Certificate2 m_cert = cbCerts.SelectedItem as X509Certificate2; if (m_cert == null) { MessageBox.Show("не найден сертификат!"); return(null); } SignedData signedData = new SignedDataClass(); Utilities utilities = new UtilitiesClass(); byte[] array = data; Signer signer = new SignerClass(); IStore store = new StoreClass(); bool flag2 = false; store.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "My", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY); foreach (ICertificate certificate in store.Certificates) { if (certificate.Thumbprint == m_cert.Thumbprint) { signer.Certificate = certificate; flag2 = true; break; } } if (!flag2) { throw new Exception("Не удалось найти сертификат подписи!"); } CapiComRCW.Attribute attribute = new AttributeClass(); attribute.Name = CAPICOM_ATTRIBUTE.CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME; attribute.Value = DateTime.Now.ToUniversalTime(); signer.AuthenticatedAttributes.Add(attribute); byte[] array3; byte[] array2 = signdata; ((CapiComRCW.ISignedData)signedData).set_Content(utilities.ByteArrayToBinaryString(array)); signedData.Verify(Convert.ToBase64String(array2), true, CAPICOM_SIGNED_DATA_VERIFY_FLAG.CAPICOM_VERIFY_SIGNATURE_ONLY); Store store2 = new StoreClass(); store2.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "AddressBook", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_WRITE); for (int i = 1; i <= signedData.Signers.Count; i++) { Signer signer2 = (Signer)signedData.Signers[i]; Certificate pVal = (Certificate)signer2.Certificate; store2.Add(pVal); } store2.Close(); string s = signedData.CoSign(signer, CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64); array3 = Convert.FromBase64String(s); return(array3); }
// Used for actually generating objects of this class - // these interface with the private constructor above. #region Public/Static accessible constructors public static CharacterResponse Create(Account account, string name, Class cls, Race race, Faction faction) { Character character = null; string failedMessage = Validation.Instance.TestName(name); if (failedMessage == string.Empty) { cls.Validate(race); string raceTest = race.Validate(faction); if (raceTest != string.Empty) { if (failedMessage != string.Empty) { failedMessage = failedMessage + Environment.NewLine; } failedMessage += raceTest; } if (failedMessage == string.Empty) { byte level = 1; if (cls == Class.DeathKnight) { level = 55; } // Final test - pulls each character for a given user. If user has a non-deleted character of the opposing faction, Create returns with failedMessage and null Character. foreach (string characterName in account.CharacterNames) { Character existingCharacter = Store.Get(characterName); if (existingCharacter.Faction != faction && !existingCharacter.Deleted) { failedMessage = "Error: You already have at least one active character of the opposing faction. You cannot create a " + faction.ToString() + " character without deleting that character.)"; } } if (failedMessage == string.Empty) { character = new Character(account.Name, name, level, cls, race, faction); failedMessage = Store.Add(character); if (failedMessage == string.Empty) { account.AddCharacterName(character.Name); } } } } if (failedMessage != string.Empty) { character = null; } return(new CharacterResponse(failedMessage, character)); }
/// <summary> /// Used for generating new accounts. Sends the stripped down /// PublicAccount version back to the client, as the hash and salts /// are datamembers so they can be written to the store, so PublicAccount /// In order to avoid kicking back the salt and the hash to the client. /// </summary> /// <param name="name">Username for new account</param> /// <param name="pass">Password for new account</param> /// <returns></returns> public static AccountResponse Create(string name, string pass) { PublicAccount acct = null; string message = Validation.Instance.TestName(name, true); if (message == string.Empty) { message = Validation.Instance.TestPassword(pass); if (message == string.Empty) { message = Store.Add(new Account(name, pass)); if (message == string.Empty) { acct = new PublicAccount(name, null, null); } } } return(new AccountResponse(message, acct)); }
private void SignFile(string sFileIn) { if (Path.GetExtension(sFileIn).ToUpper().Equals(".ZIP")) { SignZip(sFileIn); if (!cbExtSignZIP.Checked) { return; } } X509Certificate2 m_cert = cbCerts.SelectedItem as X509Certificate2; if (m_cert == null) { throw new ApplicationException("Сформировать ЭЦП невозможно: не найден действительный сертификат отправителя!"); } SignedData signedData = new SignedDataClass(); Utilities utilities = new UtilitiesClass(); byte[] array; using (FileStream fileStream = new FileStream(sFileIn, FileMode.Open, FileAccess.Read)) { array = new byte[fileStream.Length]; fileStream.Position = 0L; fileStream.Read(array, 0, (int)fileStream.Length); fileStream.Close(); } byte[] array2 = null; bool flag = false; if (File.Exists(sFileIn + ".sig")) { flag = true; using (FileStream fileStream = new FileStream(sFileIn + ".sig", FileMode.Open, FileAccess.Read)) { array2 = new byte[fileStream.Length]; fileStream.Position = 0L; fileStream.Read(array2, 0, (int)fileStream.Length); fileStream.Close(); } } Signer signer = new SignerClass(); IStore store = new StoreClass(); bool flag2 = false; store.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "My", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY); foreach (ICertificate certificate in store.Certificates) { if (certificate.Thumbprint == m_cert.Thumbprint) { signer.Certificate = certificate; flag2 = true; break; } } if (!flag2) { throw new Exception("Не удалось найти сертификат подписи!"); } CapiComRCW.Attribute attribute = new AttributeClass(); attribute.Name = CAPICOM_ATTRIBUTE.CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME; attribute.Value = DateTime.Now.ToUniversalTime(); signer.AuthenticatedAttributes.Add(attribute); byte[] array3; if (flag) { // signedData.Content = ""; //signedData.Content = Marshal.PtrToStringBSTR( utilities.ByteArrayToBinaryString(array)); ((CapiComRCW.ISignedData)signedData).set_Content(utilities.ByteArrayToBinaryString(array)); try { signedData.Verify(Convert.ToBase64String(array2), true, CAPICOM_SIGNED_DATA_VERIFY_FLAG.CAPICOM_VERIFY_SIGNATURE_ONLY); } catch (Exception e) { errlist.Add("Ошибка проверки подписи!" + sFileIn + ":" + e.Message); } Store store2 = new StoreClass(); store2.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "AddressBook", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_WRITE); for (int i = 1; i <= signedData.Signers.Count; i++) { Signer signer2 = (Signer)signedData.Signers[i]; Certificate pVal = (Certificate)signer2.Certificate; store2.Add(pVal); } store2.Close(); string s = signedData.CoSign(signer, CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64); array3 = Convert.FromBase64String(s); } else { //signedData.Content = utilities.ByteArrayToBinaryString(array); ((CapiComRCW.ISignedData)signedData).set_Content(utilities.ByteArrayToBinaryString(array)); string s = signedData.Sign(signer, true, CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64); array3 = Convert.FromBase64String(s); } using (FileStream fileStream = new FileStream(sFileIn + ".sig", FileMode.Create, FileAccess.Write)) { fileStream.Write(array3, 0, array3.Length); fileStream.Close(); } }