// Write when the output file is specified void ConsoleWriteOutFile(CONSOLE *c, wchar_t *str, bool add_last_crlf) { LOCAL_CONSOLE_PARAM *p; // Validate arguments if (c == NULL || str == NULL) { return; } p = (LOCAL_CONSOLE_PARAM *)c->Param; if (p != NULL && p->OutFile != NULL) { wchar_t *tmp = UniNormalizeCrlf(str); UINT utf8_size; UCHAR *utf8; utf8_size = CalcUniToUtf8(tmp); utf8 = ZeroMalloc(utf8_size + 1); UniToUtf8(utf8, utf8_size + 1, tmp); FileWrite(p->OutFile, utf8, utf8_size); if (UniEndWith(str, L"\n") == false && add_last_crlf) { char *crlf = "\r\n"; FileWrite(p->OutFile, "\r\n", StrLen(crlf)); } Free(utf8); Free(tmp); } }
// VALUE を書き出す void WriteValue(BUF *b, VALUE *v, UINT type) { UINT len; BYTE *u; UINT u_size; // 引数チェック if (b == NULL || v == NULL) { return; } // データ項目 switch (type) { case VALUE_INT: // 整数 WriteBufInt(b, v->IntValue); break; case VALUE_INT64: // 64 bit 整数 WriteBufInt64(b, v->Int64Value); break; case VALUE_DATA: // データ // サイズ WriteBufInt(b, v->Size); // 本体 WriteBuf(b, v->Data, v->Size); break; case VALUE_STR: // ANSI 文字列 len = StrLen(v->Str); // 長さ WriteBufInt(b, len); // 文字列本体 WriteBuf(b, v->Str, len); break; case VALUE_UNISTR: // Unicode 文字列 // UTF-8 に変換する u_size = CalcUniToUtf8(v->UniStr) + 1; u = ZeroMalloc(u_size); UniToUtf8(u, u_size, v->UniStr); // サイズ WriteBufInt(b, u_size); // UTF-8 文字列本体 WriteBuf(b, u, u_size); Free(u); break; } }
//! コード変換 Unicode→UTF-8 EConvertResult CUtf8::_UnicodeToUTF8( const CNativeW& cSrc, CMemory* pDstMem, bool bCesu8Mode ) { // エラー状態 bool bError = false; // ソースを取得 const wchar_t* pSrc = cSrc.GetStringPtr(); int nSrcLen = cSrc.GetStringLength(); // 必要なバッファサイズを調べてメモリを確保 char* pDst; try{ pDst = new char[nSrcLen * 3]; }catch( ... ){ pDst = NULL; } if( pDst == NULL ){ return RESULT_FAILURE; } // 変換 int nDstLen = UniToUtf8( pSrc, nSrcLen, pDst, &bError, bCesu8Mode ); // pDstMem を更新 pDstMem->SetRawDataHoldBuffer( pDst, nDstLen ); // 後始末 delete [] pDst; if( bError == false ){ return RESULT_COMPLETE; }else{ return RESULT_LOSESOME; } }
// Output contents of the item void CfgAddItemText(BUF *b, ITEM *t, UINT depth) { char *data; char *sub = NULL; UINT len; UINT size; char *utf8; UINT utf8_size; wchar_t *string; // Validate arguments if (b == NULL || t == NULL) { return; } // Process the data by its type data = NULL; switch (t->Type) { case ITEM_TYPE_INT: data = Malloc(32); ToStr(data, *((UINT *)t->Buf)); break; case ITEM_TYPE_INT64: data = Malloc(64); ToStr64(data, *((UINT64 *)t->Buf)); break; case ITEM_TYPE_BYTE: data = ZeroMalloc(t->size * 4 + 32); len = B64_Encode(data, t->Buf, t->size); data[len] = 0; break; case ITEM_TYPE_STRING: string = t->Buf; utf8_size = CalcUniToUtf8(string); utf8_size++; utf8 = ZeroMalloc(utf8_size); utf8[0] = 0; UniToUtf8(utf8, utf8_size, string); size = utf8_size; data = utf8; break; case ITEM_TYPE_BOOL: size = 32; data = Malloc(size); if (*((bool *)t->Buf) == false) { StrCpy(data, size, TAG_FALSE); } else { StrCpy(data, size, TAG_TRUE); } break; } if (data == NULL) { return; } // Output the data line CfgAddData(b, t->Type, t->Name, data, sub, depth); // Memory release Free(data); if (sub != NULL) { Free(sub); } }
// Output the folder contents (Recursive, binary) void CfgOutputFolderBin(BUF *b, FOLDER *f) { UINT i; // Validate arguments if (b == NULL || f == NULL) { return; } // Folder name WriteBufStr(b, f->Name); // The number of the subfolder WriteBufInt(b, LIST_NUM(f->Folders)); // Subfolder for (i = 0;i < LIST_NUM(f->Folders);i++) { FOLDER *sub = LIST_DATA(f->Folders, i); CfgOutputFolderBin(b, sub); if ((i % 100) == 99) { YieldCpu(); } } // The number of Items WriteBufInt(b, LIST_NUM(f->Items)); // Item for (i = 0;i < LIST_NUM(f->Items);i++) { char *utf8; UINT utf8_size; ITEM *t = LIST_DATA(f->Items, i); // Item Name WriteBufStr(b, t->Name); // Type WriteBufInt(b, t->Type); switch (t->Type) { case ITEM_TYPE_INT: // Integer WriteBufInt(b, *((UINT *)t->Buf)); break; case ITEM_TYPE_INT64: // 64-bit integer WriteBufInt64(b, *((UINT64 *)t->Buf)); break; case ITEM_TYPE_BYTE: // Data size WriteBufInt(b, t->size); // Data WriteBuf(b, t->Buf, t->size); break; case ITEM_TYPE_STRING: // String utf8_size = CalcUniToUtf8((wchar_t *)t->Buf) + 1; utf8 = ZeroMalloc(utf8_size); UniToUtf8(utf8, utf8_size, (wchar_t *)t->Buf); WriteBufInt(b, StrLen(utf8)); WriteBuf(b, utf8, StrLen(utf8)); Free(utf8); break; case ITEM_TYPE_BOOL: // Boolean type if (*((bool *)t->Buf) == false) { WriteBufInt(b, 0); } else { WriteBufInt(b, 1); } break; } } }
// Write the certificate object bool WriteSecCert(SECURE *sec, bool private_obj, char *name, X *x) { UINT obj_class = CKO_CERTIFICATE; CK_BBOOL b_true = true, b_false = false, b_private_obj = private_obj; UINT cert_type = CKC_X_509; CK_DATE start_date, end_date; UCHAR subject[MAX_SIZE]; UCHAR issuer[MAX_SIZE]; wchar_t w_subject[MAX_SIZE]; wchar_t w_issuer[MAX_SIZE]; UCHAR serial_number[MAX_SIZE]; UCHAR value[4096]; UINT ret; BUF *b; UINT object; CK_ATTRIBUTE a[] = { {CKA_SUBJECT, subject, 0}, // 0 {CKA_ISSUER, issuer, 0}, // 1 {CKA_SERIAL_NUMBER, serial_number, 0}, // 2 {CKA_VALUE, value, 0}, // 3 {CKA_CLASS, &obj_class, sizeof(obj_class)}, {CKA_TOKEN, &b_true, sizeof(b_true)}, {CKA_PRIVATE, &b_private_obj, sizeof(b_private_obj)}, {CKA_LABEL, name, StrLen(name)}, {CKA_CERTIFICATE_TYPE, &cert_type, sizeof(cert_type)}, #if 0 // Don't use these because some tokens fail {CKA_START_DATE, &start_date, sizeof(start_date)}, {CKA_END_DATE, &end_date, sizeof(end_date)}, #endif }; // Validate arguments if (sec == NULL) { return false; } if (name == NULL) { sec->Error = SEC_ERROR_BAD_PARAMETER; return false; } if (sec->SessionCreated == false) { sec->Error = SEC_ERROR_NO_SESSION; return false; } if (sec->LoginFlag == false && private_obj) { sec->Error = SEC_ERROR_NOT_LOGIN; return false; } // Copy the certificate to the buffer b = XToBuf(x, false); if (b == NULL) { sec->Error = SEC_ERROR_INVALID_CERT; return false; } if (b->Size > sizeof(value)) { // Size is too large FreeBuf(b); sec->Error = SEC_ERROR_DATA_TOO_BIG; return false; } Copy(value, b->Buf, b->Size); A_SIZE(a, 3) = b->Size; FreeBuf(b); // Store the Subject and the Issuer by encoding into UTF-8 GetPrintNameFromName(w_subject, sizeof(w_subject), x->subject_name); UniToUtf8(subject, sizeof(subject), w_subject); A_SIZE(a, 0) = StrLen(subject); if (x->root_cert == false) { GetPrintNameFromName(w_issuer, sizeof(w_issuer), x->issuer_name); UniToUtf8(issuer, sizeof(issuer), w_issuer); A_SIZE(a, 1) = StrLen(issuer); } // Copy the serial number Copy(serial_number, x->serial->data, MIN(x->serial->size, sizeof(serial_number))); A_SIZE(a, 2) = MIN(x->serial->size, sizeof(serial_number)); // Expiration date information UINT64ToCkDate(&start_date, SystemToLocal64(x->notBefore)); UINT64ToCkDate(&end_date, SystemToLocal64(x->notAfter)); // Workaround for Gemalto PKCS#11 API. It rejects a private certificate. if(sec->Dev->Id == 18 || sec->Dev->Id == 19) { b_private_obj = false; } // Remove objects which have the same name if (CheckSecObject(sec, name, SEC_X)) { DeleteSecCert(sec, name); } // Creating if ((ret = sec->Api->C_CreateObject(sec->SessionId, a, sizeof(a) / sizeof(a[0]), &object)) != CKR_OK) { // Failure sec->Error = SEC_ERROR_HARDWARE_ERROR; Debug("Error: 0x%02x\n", ret); return false; } // Clear Cache EraseEnumSecObjectCache(sec); return true; }