1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/*****************************************************************************/
/*--[litdrm.c]-----------------------------------------------------------------
| Copyright (C) 2002 Dan A. Jackson
|
| This file is part of the "openclit" library for processing .LIT files.
|
| "Openclit" is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2 of the License, or
| (at your option) any later version.
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; if not, write to the Free Software
| Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
| The GNU General Public License may also be available at the following
| URL: http://www.gnu.org/licenses/gpl.html
*/
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "litlib.h"
#include "litinternal.h"
#include "d3des.h"
#include "sha.h"
static int calculate_deskey(lit_file *, U8 key[8]);
static void hash_bytes(SHA_CTX * pctx, U8 * pdata, int length, int padding);
static void random_data(U8 * pData, int len);
#define SEALED_SIZE 16
/*
| This file contains routines to deal with the two standard DRM levels.
| Those levels are
| DRM1 - "Sealed"
| "/DRMStorage/DRMSealed" contains a DES key that is encrypted with
| a DES key generated from "/meta" and "/DRMStorage/DRMSource"
| DRM3 - "Inscribed"
| "/DRMStorage/DRMSealed" contains a DES key that is encrypted with
| a DES key generated from "/meta","/DRMStorage/DRMSource", and
| "/DRMStorage/DRMBookplate" (the inscribed text in Unicode).
|
| "/DRMStorage/ValidationStream" should always be "MSReader" if the
| decryption works.
*/
static const char * drmsource_string = "/DRMStorage/DRMSource";
static const char * sealed_string = "/DRMStorage/DRMSealed";
static const char * bookplate_string = "/DRMStorage/DRMBookplate";
static const char * validation_string = "/DRMStorage/ValidationStream";
static const char * license_string = "/DRMStorage/Licenses/EUL";
static const char * license_path_string = "/DRMStorage/Licenses";
static const char * meta_string = "/meta";
static const char * msreader_string = "MSReader";
/*--[lit_i_read_drm]-----------------------------------------------------------
|
| This routine checks for the presence of DRM and initializes the DRM level
| and bookkey appropriately.
|
*/
int lit_i_read_drm(lit_file * litfile)
{
U8 * ptr, tmpkey[8];
int size, ofs;
int status;
status = lit_get_file(litfile, meta_string, NULL, NULL);
if (status) {
lit_error(ERR_R,"Bad LIT directory - no \"%s\" file!",meta_string);
return status;
}
litfile->drmlevel = 0;
status = lit_get_file(litfile, license_string, NULL, NULL);
if (!status) litfile->drmlevel = 5;
if (status) {
status = lit_get_file(litfile,bookplate_string, NULL, NULL);
if (!status) litfile->drmlevel = 3;
}
if (status) {
status = lit_get_file(litfile,sealed_string, NULL, NULL);
if (!status) litfile->drmlevel = 1;
}
if (!litfile->drmlevel) return 0;
if (litfile->drmlevel < 5) {
status = calculate_deskey(litfile, tmpkey);
if (status) return status;
status = lit_get_file(litfile, sealed_string, &ptr, &size);
if (status) return status;
deskey(tmpkey, DE1);
for (ofs = 0; ofs < size; ofs += 8)
{
des( (ptr+ofs), (ptr+ofs));
}
if (*ptr) {
lit_error(ERR_R,
"Unable to decrypt title key! Check value = %02x\n"
"This may be a newer or corrupted .LIT file!", *ptr);
litfile->drmlevel = DRM_LEVEL_INVALID;
free(ptr);
return E_LIT_DRM_ERROR;
}
/* the key is the 8 bytes, skipping the first (should be 0) */
memcpy(litfile->bookkey, ptr+1, 8);
free(ptr);
ptr = NULL;
}
else { /* DRM level is 5 */
if (litfile->drm5_callback) {
litfile->drmlevel = 5;
status = litfile->drm5_callback(litfile->drm5_data,NULL,0);
return status;
}
else {
lit_error(ERR_R, "DRM5 is not supported!");
litfile->drmlevel = 5;
return E_LIT_DRM_ERROR;
}
}
status = lit_get_file(litfile,validation_string,&ptr,&size);
if (status) {
litfile->drmlevel = DRM_LEVEL_INVALID;
return status;
}
if (strncmp(msreader_string,ptr,strlen(msreader_string)) != 0) {
lit_error(ERR_R,
"Decryption is incorrect -- the Validation string is wrong!");
free(ptr);
litfile->drmlevel = DRM_LEVEL_INVALID;
return E_LIT_DRM_ERROR;
}
free(ptr);
return 0;
}
/*--[lit_i_encrypt]------------------------------------------------------------
|
| This routine encrypts the section with the specified key
|
*/
int lit_i_encrypt(U8 * pContent, int sizeContent, U8 * new_key)
{
int ofs;
deskey(new_key, EN0);
for (ofs = 0; ofs < sizeContent; ofs+= 8)
{
des( (pContent+ofs), (pContent+ofs));
}
return 0;
}
/*--[lit_i_decrypt]------------------------------------------------------------
|
| This routine handles decryption of sections.
|
*/
int lit_i_decrypt(lit_file * litfile, U8 * pContent, int sizeContent)
{
int status;
int ofs;
if (litfile->drmlevel < 0) {
return E_LIT_DRM_ERROR;
}
if (litfile->drmlevel == 5) {
status = E_LIT_DRM_ERROR;
if (litfile->drm5_callback) {
status = litfile->drm5_callback(litfile->drm5_data,
pContent, sizeContent);
} else {
lit_error(ERR_R,
"This version is unable to decrypt owner-exclusive content!");
}
return status;
}
deskey(litfile->bookkey, DE1);
for (ofs = 0; ofs < sizeContent; ofs+= 8)
{
des( (pContent+ofs), (pContent+ofs));
}
return 0;
}
/*--[lit_change_drm_level]-----------------------------------------------------
|
| This routine changes the DRM on the file.
| Specifically, this can go from 5 or 3 to 1, and from 1 to 3 (inscribing).
| DRM2 and DRM4 are undefined.
| and DRM0 is unsupported for now
*/
int lit_change_drm_level(lit_file * litfile, int newlevel, U8 * drm_data,
int data_size)
{
int i, status;
U8 * new_sealed, * pBookplate, new_key[8];
if (litfile->drmlevel == 0) {
lit_error(ERR_W,"Changing DRM0 is not yet supported.");
return E_LIT_UNSUPPORTED;
}
if (newlevel == 0) {
lit_error(ERR_W,"Converting TO DRM0 is not yet supported.");
return E_LIT_UNSUPPORTED;
}
if (newlevel == 3) {
if (!drm_data) {
lit_error(ERR_W,"No inscription data supplied!");
return E_LIT_NULL_POINTER;
}
}
lit_remove_files(litfile, license_path_string);
switch (newlevel)
{
case 1:
lit_remove_files(litfile, bookplate_string);
break;
case 3:
pBookplate = malloc(data_size);
if (!pBookplate) {
lit_error(ERR_W,"Can't allocate %d bytes for copy of inscription.",
data_size);
return E_LIT_OUT_OF_MEMORY;
}
memcpy(pBookplate, drm_data, data_size);
status = lit_put_file(litfile,bookplate_string,pBookplate,data_size, 0);
if (status) return status;
break;
default:
lit_error(ERR_W,"Unable to change DRM level to %d.", newlevel);
return E_LIT_UNSUPPORTED;
}
if ((litfile->drmlevel == 5) || (litfile->drmlevel == 0)) {
/* Need a new bookkey */
random_data(new_key, 8);
for (i = 1; i < litfile->num_sections; i++) {
status = lit_i_encrypt_section(litfile,litfile->sections[i].name,
&new_key[0]);
}
memcpy(litfile->bookkey, new_key, 8);
}
new_sealed = malloc(SEALED_SIZE);
if (!new_sealed) {
lit_error(ERR_W,"Not enough space for new sealed.");
return E_LIT_OUT_OF_MEMORY;
}
memset(new_sealed, 0, SEALED_SIZE);
memcpy(new_sealed+1, litfile->bookkey, 8);
litfile->drmlevel = newlevel;
status = calculate_deskey(litfile,new_key);
if (status ) {
lit_error(ERR_W, "Unable to make new \"Sealed\" key.");
free(new_sealed);
return status;
}
deskey(new_key, EN0);
for (i = 0; i < SEALED_SIZE; i+=8) {
des( (new_sealed+i), (new_sealed+i));
}
status = lit_put_file(litfile,sealed_string,new_sealed,
SEALED_SIZE,0);
if (status) {
free(new_sealed);
return status;
}
return 0;
}
/*--[hash_bytes]---------------------------------------------------------------
|
| This is a "special" hash routine which has slightly different properties
| than just calling SHA_Update.
| Note that the SHA routine isn't exactly standard either.
*/
void hash_bytes(SHA_CTX * pctx, U8 * pdata, int length, int padding)
{
/* This buffer size is FIXED as per the algorithm */
U8 buffer[64];
int pos, avail, offset;
if (padding > sizeof(buffer))
{
return;
}
memset(buffer, 0, padding);
pos = padding;
offset = 0;
while (length > 0)
{
avail = sizeof(buffer) - pos;
if (length >= avail)
{
memcpy(&buffer[pos],pdata+offset, avail);
length -= avail;
offset += avail;
} else {
/* Length + 0's */
memcpy(&buffer[pos],pdata+offset, length);
memset(&buffer[pos+length],0, avail - length);
/* offset is meaningless now */
length = 0;
}
SHA1_Update(pctx, buffer, sizeof(buffer));
pos = 0;
}
}
/*--[calculate_deskey]---------------------------------------------------------
|
| This routine is responsible for calculating a DES key from the internal
| files.
| See the comment above for which files affect the calculation process.
| Things are done in a particular way for various reasons and small changes
| will invalidate the key calculation process.
|
*/
int calculate_deskey(lit_file * litfile, U8 key[8])
{
int nbytes;
int status, i, pad;
U8 *p, digest[20];
SHA_CTX ctx;
const char *hashfiles[] = {meta_string,drmsource_string,
bookplate_string,NULL};
const char **s;
SHA1_Init(&ctx);
/* Remove the bookplate if it is not present. */
if (litfile->drmlevel != 3) {
hashfiles[2] = 0;
}
s = hashfiles;
/* First hash has two bytes of padding */
pad = 2;
while (*s)
{
status = lit_get_file(litfile, *s, &p, &nbytes);
if (status) { return status; }
hash_bytes(&ctx, p, (int)nbytes, pad);
free(p);
pad = 0;
s++;
}
SHA1_Final(digest, &ctx);
memset(key, 0, 8);
for (i = 0; i < sizeof(digest); i++)
{
key[i%8] ^= digest[i];
}
return 0;
}
/*--[random_data]--------------------------------------------------------------
|
| Random data for creating book key's.
| FUTUREFIX - This is very non-random.
*/
void random_data(U8 * pData, int length)
{
int i, r;
srand(time(NULL));
for (i = 0; i < length; i++) {
r = rand() ^ (rand() >> 7);
pData[i] = (r & 0xff);
}
}
|