bool Oauth::Step2(wchar_t* veri){ SetVerifier(veri); /* PairData pd; GetHeader(&pd); wstring head; head.append(pd.key).append(TEXT(":")).append(pd.data); HINTERNET hSession,hConnect,hRequest; hSession=WinHttpOpen(TEXT("WinHttp /6.1"), WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS,NULL); hConnect=WinHttpConnect(hSession,TEXT("twitter.com"),INTERNET_DEFAULT_HTTPS_PORT,NULL); hRequest=WinHttpOpenRequest( hConnect,TEXT("POST"), TEXT("/oauth/access_token"), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE); WinHttpSendRequest(hRequest,head.c_str(),head.size(),WINHTTP_NO_REQUEST_DATA,NULL,NULL,NULL); WinHttpReceiveResponse(hRequest,NULL); DWORD dwStatusCode = 0; DWORD statusCodeSize = sizeof(DWORD); ::WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &dwStatusCode, &statusCodeSize, WINHTTP_NO_HEADER_INDEX); char buf[50000]; WinHttpReadData(hRequest,buf,50000,NULL);*/ return GetAccessToken(); }
/// <summary> /// Authenticates cartridge by verifying its certificate and its signature of a random token. /// </summary> /// <param name="ucCartridgeNum">The cartridge number.</param> /// <param name="aucPubKS">Stratasys public key.</param> /// <param name="aucIdd">The tag identification data (output parameter).</param> /// <param name="usIddLength">The tag identification data length (output parameter).</param> /// <param name="uiCurrentVolume">The tag current volume (output parameter).</param> /// <returns>0 on success, non-zero for failures.</returns> int MaterialMonitor::AuthenticateCartridge(unsigned char ucCartridgeNum, const unsigned char *aucPubKS, unsigned char *aucIdd, unsigned short *usIddLength, unsigned int *uiCurrentVolume) { UniqueLock uniqueLock(m_mtx); int returnValue = IDTLIB_SUCCESS; if (aucPubKS == NULL && m_pubKS == NULL) { returnValue = INVALID_HOST_KEY; LOG_EXT(LEVEL_ERROR, "Invalid host key (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } if (aucIdd == NULL || usIddLength == NULL || uiCurrentVolume == NULL) { returnValue = AUTHENTICATE_NULL_PARAMS; LOG_EXT(LEVEL_ERROR, "Invalid parameters (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } if (!m_bInitialized) { returnValue = HW_NOT_INITIALIZED; LOG_EXT(LEVEL_ERROR, "Hardware not initialized (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } try { LOG_EXT(LEVEL_INFO, "Cartridge #" << (unsigned short)ucCartridgeNum << " authentication started:"); LOG_EXT(LEVEL_INFO, "Reset cartridge #"<< (unsigned short)ucCartridgeNum << " info..."); int returnValue = ResetCartridgeInfo(ucCartridgeNum); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error resetting cartridge #"<< (unsigned short)ucCartridgeNum << " info (number of cartridges: " << (unsigned short)m_ucCartridgesCount << ", error code 0x" << hex << (short)returnValue << ")."); return returnValue; } LOG_EXT(LEVEL_INFO, "Switching to cartridge #"<< (unsigned short)ucCartridgeNum << "..."); returnValue = m_tagAdapter->SwitchToCartridge(ucCartridgeNum); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error switching to cartridge #"<< (unsigned short)ucCartridgeNum << " (number of cartridges: " << (unsigned short)m_ucCartridgesCount << ", error code 0x" << hex << (short)returnValue << ")."); return returnValue; } LOG_EXT(LEVEL_INFO, "Reading ID certificate..."); unsigned char aucBuffer[CERTIFICATE_SIZE]; memset(aucBuffer, 0, sizeof(aucBuffer)); returnValue = ReadIDCertificate(aucBuffer); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error reading ID certificate (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } bool isEmpty = m_tagAdapter->IsCertificateEmpty(aucBuffer); if (isEmpty) { returnValue = EMPTY_CERTIFICATE_FILE; LOG_EXT(LEVEL_ERROR, "Tag is blank (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } LOG_EXT(LEVEL_DEBUG, "Extracting cartridge #" << (unsigned short)ucCartridgeNum << " public key..."); IDCertificate *certificate = new IDCertificate(); unsigned short certificateSize; try { certificateSize = (unsigned short)certificate->Decode(aucBuffer); } catch (exception& e) { delete certificate; returnValue = INVALID_CERTIFICATE_FILE; LOG_EXT(LEVEL_ERROR, "Invalid certificate (error code 0x" << hex << (short)returnValue << "): " << e.what() << "."); return returnValue; } *usIddLength = certificateSize - SIGNATURE_SIZE - PUBLIC_KEY_SIZE; memcpy(aucIdd, aucBuffer, *usIddLength); ECDSA<EC2N, SHA256>::PublicKey* pubKS; if (aucPubKS != NULL) { pubKS = LoadPublicKey(aucPubKS); if (pubKS == NULL) { delete certificate; return INVALID_HOST_KEY; } } else { pubKS = m_pubKS; } LOG_EXT(LEVEL_INFO, "Verifying ID certificate..."); SetVerifier(pubKS); pthread_t verifierThread; VerifyParameters* verifyParameters = CreateVerifyParameters( m_veriferS, aucBuffer, certificateSize - SIGNATURE_SIZE, aucBuffer + (certificateSize - SIGNATURE_SIZE), SIGNATURE_SIZE); CreateVerifierThread(&verifierThread, verifyParameters); LOG_EXT(LEVEL_INFO, "Sending random token for IDT to sign..."); byte aucHostChallenge[TOKEN_SIZE]; GenerateToken(aucHostChallenge, TOKEN_SIZE); byte aucDeviceChallenge[64]; // expected device challenge length byte aucSignedDeviceChallenge[SIGNATURE_SIZE]; unsigned short usDeviceChallengeLength = sizeof(aucDeviceChallenge); unsigned short usSignedDeviceChallengeLength = sizeof(aucSignedDeviceChallenge); returnValue = ChallengeHostDevice(ucCartridgeNum, aucHostChallenge, TOKEN_SIZE, aucDeviceChallenge, &usDeviceChallengeLength, aucSignedDeviceChallenge, &usSignedDeviceChallengeLength); void* status; pthread_join(verifierThread, &status); delete verifyParameters; if (!m_bVerified) { returnValue = INVALID_CERTIFICATE_SIGNATURE; LOG_EXT(LEVEL_ERROR, "Error verifying ID certificate (error code 0x" << hex << (short)returnValue << ")."); delete certificate; return returnValue; } if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error sending random token to IDT (error code 0x" << hex << (short)returnValue << ")."); delete certificate; return returnValue; } LOG_EXT(LEVEL_INFO, "Verifying IDT signature..."); verifyParameters = CreateVerifyParameters( certificate->IDD.GetVerifier(), aucDeviceChallenge, usDeviceChallengeLength, aucSignedDeviceChallenge, usSignedDeviceChallengeLength); CreateVerifierThread(&verifierThread, verifyParameters); LOG_EXT(LEVEL_INFO, "Getting current volume..."); returnValue = GetCurrentVolume(ucCartridgeNum, uiCurrentVolume); pthread_join(verifierThread, &status); delete verifyParameters; if (!m_bVerified) { returnValue = INVALID_TOKEN_SIGNATURE; LOG_EXT(LEVEL_ERROR, "Invalid signature (error code 0x" << hex << (short)returnValue << ")."); delete certificate; return returnValue; } if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error getting current volume (error code 0x" << hex << (short)returnValue << ")."); delete certificate; return returnValue; } m_certificates[ucCartridgeNum] = certificate; LOG_EXT(LEVEL_INFO, "Cartridge #" << (unsigned short)ucCartridgeNum << " authentication ended successfully. Current weight: " << *uiCurrentVolume << " milligrams."); return returnValue; } catch (exception& e) { LOG_EXT(LEVEL_ERROR, "Exception caught: " << e.what() << "."); return EXCEPTION_CAUGHT; } }
/// <summary> /// Generates certificate from material information and burns it into tag. /// </summary> /// <param name="ucCartridgeNum">The cartridge number.</param> /// <param name="aucMaterialInfo">The material information.</param> /// <param name="usMaterialInfoLength">The material information length.</param> /// <param name="aucPrvKS">SSYS private key.</param> /// <param name="aucPubKS">SSYS public key.</param> /// <returns>0 on success, non-zero for failures.</returns> int MaterialMonitor::BurnIDC(unsigned char ucCartridgeNum, const unsigned char *aucMaterialInfo, unsigned short usMaterialInfoLength, const unsigned char *aucPrvKS, const unsigned char *aucPubKS) { UniqueLock uniqueLock(m_mtx); int returnValue = IDTLIB_SUCCESS; bool areParametersValid = true; if (m_bCorrupt) { areParametersValid = (aucMaterialInfo != NULL) && (aucPrvKS != NULL) && ((aucPubKS != NULL) || (m_pubKS != NULL)); } else { areParametersValid = (aucMaterialInfo == NULL) || ((aucPrvKS != NULL) && ((aucPubKS != NULL) || (m_pubKS != NULL))); } if (!areParametersValid) { returnValue = BURN_NULL_PARAMS; LOG_EXT(LEVEL_ERROR, "Invalid parameters."); return returnValue; } try { LOG_EXT(LEVEL_INFO, "Setting ID certificate for cartridge #" << (unsigned short)ucCartridgeNum << " started:"); LOG_EXT(LEVEL_INFO, "Switching to cartridge #"<< (unsigned short)ucCartridgeNum << "..."); returnValue = m_tagAdapter->SwitchToCartridge(ucCartridgeNum); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error switching to cartridge #"<< (unsigned short)ucCartridgeNum << " (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } unsigned char aucCertificate[CERTIFICATE_SIZE]; unsigned short usCertificateLength = CERTIFICATE_SIZE; if (m_bCorrupt) { ReadIDCertificate(aucCertificate); } LOG_EXT(LEVEL_INFO, "Logging in to cartridge #"<< (unsigned short)ucCartridgeNum << " ..."); returnValue = m_tagAdapter->Login(); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error logging in to cartridge #"<< (unsigned short)ucCartridgeNum << " (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } if (aucMaterialInfo != NULL) { LOG_EXT(LEVEL_INFO, "Configuring cartridge #"<< (unsigned short)ucCartridgeNum << "..."); returnValue = m_tagAdapter->Configure(); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error configuring to cartridge #"<< (unsigned short)ucCartridgeNum << " (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } unsigned char pubKS[PUBLIC_KEY_SIZE]; if (aucPubKS != NULL) { returnValue = SetPubKS(aucPubKS); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error setting host public key (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } } try { m_pubKS->Save(ArraySink(pubKS, PUBLIC_KEY_SIZE).Ref()); } catch (...) { returnValue = INVALID_HOST_KEY; LOG_EXT(LEVEL_ERROR, "Invalid host key."); return returnValue; } LOG_EXT(LEVEL_INFO, "Setting keys of cartridge #"<< (unsigned short)ucCartridgeNum << "..."); returnValue = m_tagAdapter->SetKeys(pubKS); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error setting keys of cartridge #"<< (unsigned short)ucCartridgeNum << " (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } LOG_EXT(LEVEL_INFO, "Setting counter of cartridge #"<< (unsigned short)ucCartridgeNum << "..."); MaterialInformation materialInfo; materialInfo.Decode(aucMaterialInfo); returnValue = m_tagAdapter->SetCounter((unsigned int)(materialInfo.InitialWeight) * 1000); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error setting counter of cartridge #"<< (unsigned short)ucCartridgeNum << " (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } if (m_bCorrupt) { LOG_EXT(LEVEL_INFO, "Modifying cartridge #"<< (unsigned short)ucCartridgeNum << " certificate..."); memcpy(aucCertificate, aucMaterialInfo, usMaterialInfoLength); } else { LOG_EXT(LEVEL_INFO, "Generate cartridge #"<< (unsigned short)ucCartridgeNum << " certificate..."); returnValue = m_tagAdapter->GenerateCertificate(aucMaterialInfo, usMaterialInfoLength, aucPrvKS, aucCertificate, &usCertificateLength); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error generating cartridge #"<< (unsigned short)ucCartridgeNum << " certificate (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } bool ok; LOG_EXT(LEVEL_INFO, "Verifying ID certificate..."); try { SetVerifier(m_pubKS); ok = m_veriferS->VerifyMessage(aucCertificate, usCertificateLength - SIGNATURE_SIZE, aucCertificate + (usCertificateLength - SIGNATURE_SIZE), SIGNATURE_SIZE); } catch (exception& e) { LOG_EXT(LEVEL_ERROR, "Error verifying ID certificate (error code 0x" << hex << (short)returnValue << "): " << e.what() << "."); ok = false; } if (!ok) { returnValue = INVALID_CERTIFICATE_SIGNATURE; LOG_EXT(LEVEL_ERROR, "Error verifying ID certificate (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } } LOG_EXT(LEVEL_INFO, "Writing certificate into cartridge #"<< (unsigned short)ucCartridgeNum << " ..."); returnValue = m_tagAdapter->WriteCertificate(aucCertificate, usCertificateLength); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error writing certificate into cartridge #"<< (unsigned short)ucCartridgeNum << " (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } } LOG_EXT(LEVEL_INFO, "Logging out from cartridge #"<< (unsigned short)ucCartridgeNum << " ..."); returnValue = m_tagAdapter->Logout(); if (returnValue != IDTLIB_SUCCESS) { LOG_EXT(LEVEL_ERROR, "Error logging out from cartridge #"<< (unsigned short)ucCartridgeNum << " (error code 0x" << hex << (short)returnValue << ")."); return returnValue; } LOG_EXT(LEVEL_INFO, "Setting ID certificate for cartridge #" << (unsigned short)ucCartridgeNum << " ended successfully."); return returnValue; } catch (exception& e) { LOG_EXT(LEVEL_ERROR, "Exception caught: " << e.what() << "."); return EXCEPTION_CAUGHT; } }