/* Licensed to Stichting The Commons Conservancy (TCC) under one or more * contributor license agreements. See the AUTHORS file distributed with * this work for additional information regarding copyright ownership. * TCC licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "signtext.h" #include #include SignTextData* signtext_data_new() { SignTextData* signtext = calloc(sizeof(SignTextData), 1); // signtext->incoming = g_list_store_new(GCR_TYPE_CERTIFICATE); // signtext->certificates = g_list_store_new(GCR_TYPE_CERTIFICATE); // signtext->hStdin = GetStdHandle(STD_INPUT_HANDLE); // signtext->hStdout = GetStdHandle(STD_OUTPUT_HANDLE); return signtext; } void signtext_data_free(SignTextData* data) { if (data) { // if (data->incoming) g_object_unref(data->incoming); // if (data->certificates) g_object_unref(data->certificates); // if (data->modules) g_list_free_full(g_steal_pointer(&data->modules), g_object_unref); // if (data->slots) g_list_free_full(g_steal_pointer(&data->slots), g_object_unref); } free(data); } SignTextInstance* signtext_instance_new(SignTextData* signtext, int id, const char* uuid, const char* uri) { SignTextInstance* instance = calloc(sizeof(SignTextInstance), 1); instance->signtext = signtext; instance->id = id; instance->uuid = _strdup(uuid); instance->uri = _strdup(uri); return instance; } void signtext_instance_free(SignTextInstance* data) { if (data) { if (data->uuid) free(data->uuid); if (data->uri) free(data->uri); if (data->wUuid) free(data->wUuid); if (data->pinBuffer) { memset(data->pinBuffer, 0, data->pinLen); free(data->pinBuffer); } if (data->digestAlgorithm) { free(data->digestAlgorithm); } } free(data); } SignTextCertificate* signtext_certificate_new(SignTextData* signtext, LPWSTR name, PCCERT_CONTEXT cert, DWORD spec, DWORD type, SECRET_TYPE secretType, SignTextCertificate *prev) { SignTextCertificate* certificate = calloc(sizeof(SignTextCertificate), 1); certificate->signtext = signtext; certificate->name = name; certificate->pbCertEncoded = malloc(cert->cbCertEncoded); certificate->cbCertEncoded = cert->cbCertEncoded; certificate->spec = spec; certificate->type = type; certificate->secretType = secretType; certificate->ref = 1; memcpy(certificate->pbCertEncoded, cert->pbCertEncoded, cert->cbCertEncoded); if (prev) { prev->next = certificate; } return certificate; } SignTextCertificate* signtext_certificate_ref(SignTextCertificate* certificate) { assert(certificate->ref > 0); certificate->ref++; return certificate; } SignTextCertificate* signtext_certificate_unref(SignTextCertificate* certificate) { assert(certificate->ref > 0); certificate->ref--; if (!certificate->ref) { if (certificate->name) { free(certificate->name); } if (certificate->pbCertEncoded) { free(certificate->pbCertEncoded); } free(certificate); return NULL; } return certificate; } LPWSTR signtext_utf8ToUtf16(const char* buffer, size_t length) { int num = MultiByteToWideChar(CP_UTF8, 0, buffer, (int)length, NULL, 0); LPWSTR wbuffer = (LPWSTR)calloc((num + 1), sizeof(wchar_t)); MultiByteToWideChar(CP_UTF8, 0, buffer, (int)length + 1, wbuffer, num); return wbuffer; } void signtext_lasterror() { LPVOID lpMsgBuf; LPVOID lpDisplayBuf; DWORD dw = GetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); // Display the error message and exit the process lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + 40) * sizeof(TCHAR)); StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("Failed with error %d: %s"), dw, lpMsgBuf); MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); LocalFree(lpMsgBuf); LocalFree(lpDisplayBuf); }