int main(int argc, char * argv[]) { if (argc != 2) usage(); FILE * f = fopen(argv[1], "rb"); if (f == NULL) usage(); fseek(f, 0, SEEK_END); size_t length = (size_t)ftell(f); fseek(f, 0, SEEK_SET); unsigned char * buffer = malloc(length); fread(buffer, length, 1, f); /* Assuming `buffer` contains `length` bytes of input data */ struct cbor_load_result result; cbor_item_t * item = cbor_load(buffer, length, &result); if (result.error.code != CBOR_ERR_NONE) { printf("There was an error while reading the input near byte %zu (read %zu bytes in total): ", result.error.position, result.read); switch (result.error.code) { case CBOR_ERR_MALFORMATED: { printf("Malformed data\n"); break; } case CBOR_ERR_MEMERROR: { printf("Memory error -- perhaps the input is too large?\n"); break; } case CBOR_ERR_NODATA: { printf("The input is empty\n"); break; } case CBOR_ERR_NOTENOUGHDATA: { printf("Data seem to be missing -- is the input complete?\n"); break; } case CBOR_ERR_SYNTAXERROR: { printf("Syntactically malformed data -- see http://tools.ietf.org/html/rfc7049\n"); break; } case CBOR_ERR_NONE: { // GCC's cheap dataflow analysis gag break; } } exit(1); } /* Pretty-print the result */ cbor_describe(item, stdout); fflush(stdout); /* Deallocate the result */ cbor_decref(&item); fclose(f); }
void DynamicConfigurationServer::handleRequestReceived(const boost::system::error_code& error, size_t bytes_transferred) { LOG(INFO) << "Request received from " << remote_endpoint_; LOG(INFO) << "Begin CBOR decoding"; struct cbor_load_result result; cbor_item_t* item = cbor_load((uint8_t*)recv_buffer_.data(), bytes_transferred, &result); size_t items = cbor_array_size(item); assert(items == 2); cbor_item_t* nonceItem = cbor_array_get(item, 0); cbor_item_t* ciphertextItem = cbor_array_get(item, 1); //cbor_describe(item, stdout); //fflush(stdout); std::array<uint8_t, 16> nonce; memcpy(nonce.data(), cbor_bytestring_handle(nonceItem), 16); std::vector<uint8_t> ciphertext; ciphertext.resize(cbor_bytestring_length(ciphertextItem)); memcpy(ciphertext.data(), cbor_bytestring_handle(ciphertextItem), ciphertext.size()); std::array<uint8_t, 16> requestKey; memcpy(requestKey.data(), confRequestKey, 16); LOG(INFO) << "End CBOR decoding"; LOG(INFO) << "Begin NORX decryption"; std::tuple<bool, std::vector<uint8_t> > plaintext = NORX::decrypt( std::vector<uint8_t>(), ciphertext, std::vector<uint8_t>(), nonce, requestKey); LOG(INFO) << "END NORX decryption"; cbor_decref(&item); if (std::get<0>(plaintext)) { LOG(INFO) << "decryption successful"; item = cbor_load((uint8_t*)std::get<1>(plaintext).data(), std::get<1>(plaintext).size(), &result); //LOG(INFO) << "plaintext: " << byteVecToStr(std::get<1>(plaintext)); if (strncmp("REQ", (const char*)cbor_string_handle(item), 3) == 0) { LOG(INFO) << "received correct request"; generateCredentialsAndSendResponse(nonce); } else { cbor_describe(item, stdout); } fflush(stdout); /* Deallocate the result */ cbor_decref(&item); } else { LOG(ERROR) << "failed decrypting dynamic initialisation request."; LOG(ERROR) << "ciphertext: " << byteVecToStr(ciphertext); LOG(ERROR) << "nonce: " << byteVecToStr(std::vector<uint8_t>(nonce.begin(), nonce.end())); } startReceive(); }