Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <stdexcept> |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 7 | #include <openssl/ssl.h> |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 8 | #include "c/encoding/binary-xml-data.h" |
| 9 | #include "encoding/binary-xml-encoder.hpp" |
| 10 | #include "key-chain.hpp" |
| 11 | |
| 12 | using namespace std; |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | static unsigned char DEFAULT_PUBLIC_KEY[] = { |
| 17 | 0x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, |
| 18 | 0x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xE1, 0x7D, 0x30, 0xA7, 0xD8, 0x28, 0xAB, 0x1B, 0x84, 0x0B, 0x17, |
| 19 | 0x54, 0x2D, 0xCA, 0xF6, 0x20, 0x7A, 0xFD, 0x22, 0x1E, 0x08, 0x6B, 0x2A, 0x60, 0xD1, 0x6C, 0xB7, 0xF5, 0x44, 0x48, 0xBA, |
| 20 | 0x9F, 0x3F, 0x08, 0xBC, 0xD0, 0x99, 0xDB, 0x21, 0xDD, 0x16, 0x2A, 0x77, 0x9E, 0x61, 0xAA, 0x89, 0xEE, 0xE5, 0x54, 0xD3, |
| 21 | 0xA4, 0x7D, 0xE2, 0x30, 0xBC, 0x7A, 0xC5, 0x90, 0xD5, 0x24, 0x06, 0x7C, 0x38, 0x98, 0xBB, 0xA6, 0xF5, 0xDC, 0x43, 0x60, |
| 22 | 0xB8, 0x45, 0xED, 0xA4, 0x8C, 0xBD, 0x9C, 0xF1, 0x26, 0xA7, 0x23, 0x44, 0x5F, 0x0E, 0x19, 0x52, 0xD7, 0x32, 0x5A, 0x75, |
| 23 | 0xFA, 0xF5, 0x56, 0x14, 0x4F, 0x9A, 0x98, 0xAF, 0x71, 0x86, 0xB0, 0x27, 0x86, 0x85, 0xB8, 0xE2, 0xC0, 0x8B, 0xEA, 0x87, |
| 24 | 0x17, 0x1B, 0x4D, 0xEE, 0x58, 0x5C, 0x18, 0x28, 0x29, 0x5B, 0x53, 0x95, 0xEB, 0x4A, 0x17, 0x77, 0x9F, 0x02, 0x03, 0x01, |
| 25 | 0x00, 01 |
| 26 | }; |
| 27 | |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 28 | /** |
| 29 | * Compute the sha-256 digest of data. |
| 30 | * @param data Pointer to the input byte array. |
| 31 | * @param dataLength The length of data. |
| 32 | * @param digest A pointer to a buffer of size SHA256_DIGEST_LENGTH to receive the data. |
| 33 | */ |
| 34 | static void digestSha256(unsigned char *data, unsigned int dataLength, unsigned char *digest) |
Jeff Thompson | b81192e | 2013-08-09 09:34:51 -0700 | [diff] [blame] | 35 | { |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 36 | SHA256_CTX sha256; |
| 37 | SHA256_Init(&sha256); |
| 38 | SHA256_Update(&sha256, data, dataLength); |
| 39 | SHA256_Final(digest, &sha256); |
Jeff Thompson | b81192e | 2013-08-09 09:34:51 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 42 | /** |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 43 | * Call digestSha256 and set the digest vector to the result. |
| 44 | * @param data |
| 45 | * @param dataLength |
| 46 | * @param digest |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 47 | */ |
| 48 | static void setSha256(unsigned char *data, unsigned int dataLength, vector<unsigned char> &digest) |
| 49 | { |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 50 | unsigned char digestBuffer[SHA256_DIGEST_LENGTH]; |
| 51 | digestSha256(data, dataLength, digestBuffer); |
| 52 | setVector(digest, digestBuffer, sizeof(digestBuffer)); |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Encode the fields of the Data object and set digest to the sha-256 digest. |
| 57 | * @param data The Data object with the fields to digest. |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 58 | * @param digest A pointer to a buffer of size SHA256_DIGEST_LENGTH to receive the data. |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 59 | */ |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 60 | static void digestDataFieldsSha256(const Data &data, unsigned char *digest) |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 61 | { |
| 62 | // Imitate BinaryXmlWireFormat::encodeData. |
| 63 | struct ndn_NameComponent nameComponents[100]; |
| 64 | struct ndn_Data dataStruct; |
| 65 | ndn_Data_init(&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0])); |
| 66 | data.get(dataStruct); |
| 67 | |
| 68 | BinaryXmlEncoder encoder; |
| 69 | unsigned int signedFieldsBeginOffset, signedFieldsEndOffset; |
| 70 | ndn_Error error; |
| 71 | if ((error = ndn_encodeBinaryXmlData(&dataStruct, &signedFieldsBeginOffset, &signedFieldsEndOffset, &encoder))) |
| 72 | throw std::runtime_error(ndn_getErrorString(error)); |
| 73 | |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 74 | digestSha256(encoder.output.array + signedFieldsBeginOffset, signedFieldsEndOffset - signedFieldsBeginOffset, digest); |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void KeyChain::defaultSign(Data &data) |
| 78 | { |
| 79 | setSha256(DEFAULT_PUBLIC_KEY, sizeof(DEFAULT_PUBLIC_KEY), data.getSignedInfo().getPublisherPublicKeyDigest().getPublisherPublicKeyDigest()); |
| 80 | // TODO: Get the real current timestamp. |
| 81 | data.getSignedInfo().setTimestampMilliseconds(256); |
| 82 | data.getSignedInfo().getKeyLocator().setType(ndn_KeyLocatorType_KEY); |
| 83 | data.getSignedInfo().getKeyLocator().setKeyOrCertificate(DEFAULT_PUBLIC_KEY, sizeof(DEFAULT_PUBLIC_KEY)); |
| 84 | |
| 85 | data.getSignature().getSignature().clear(); |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 86 | unsigned char dataFieldsDigest[SHA256_DIGEST_LENGTH]; |
| 87 | digestDataFieldsSha256(data, dataFieldsDigest); |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 88 | // TODO: use RSA_size to get the proper size of the signature buffer. |
| 89 | unsigned char signature[1000]; |
| 90 | unsigned int signatureLength; |
Jeff Thompson | b81192e | 2013-08-09 09:34:51 -0700 | [diff] [blame] | 91 | #if 0 |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 92 | RSA *privateKey; |
Jeff Thompson | 8bee610 | 2013-08-09 10:26:02 -0700 | [diff] [blame^] | 93 | if (!RSA_sign(NID_sha256, dataFieldsDigest, sizeof(dataFieldsDigest), signature, &signatureLength, privateKey)) |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 94 | throw std::runtime_error("Errir in RSA_sign"); |
| 95 | |
| 96 | data.getSignature().setSignature(signature, signatureLength); |
Jeff Thompson | ade5bda | 2013-08-09 01:01:22 -0700 | [diff] [blame] | 97 | #endif |
Jeff Thompson | b81192e | 2013-08-09 09:34:51 -0700 | [diff] [blame] | 98 | } |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 99 | |
| 100 | } |