2001-04-17 22:04:44 +00:00
|
|
|
/* (C)2001 by LGB (Gabor Lenart), based on example programs in libcss
|
2001-04-21 15:28:19 +00:00
|
|
|
lgb@lgb.hu */
|
2001-04-17 22:04:44 +00:00
|
|
|
|
|
|
|
/* don't do anything with this source if css support was not requested */
|
|
|
|
#include "config.h"
|
|
|
|
#ifdef HAVE_LIBCSS
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-06-06 21:16:21 +00:00
|
|
|
//#include <string.h> // FIXME: conflicts with fs.h
|
2001-04-17 22:04:44 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <css.h>
|
2001-06-06 21:16:21 +00:00
|
|
|
#if CSS_MAJOR_VERSION > 0 || (CSS_MAJOR_VERSION == 0 && CSS_MINOR_VERSION > 1)
|
|
|
|
# include <dvd.h>
|
|
|
|
# undef OLD_CSS_API
|
|
|
|
#else
|
|
|
|
# if defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
# include <sys/dvdio.h>
|
|
|
|
# elif defined(__linux__)
|
|
|
|
# include <linux/cdrom.h>
|
|
|
|
# elif defined(__sun)
|
|
|
|
# include <sun/dvdio.h>
|
|
|
|
# else
|
|
|
|
# error "Need the DVD ioctls"
|
|
|
|
# endif
|
|
|
|
# define OLD_CSS_API 1
|
|
|
|
#endif
|
2001-04-17 22:04:44 +00:00
|
|
|
|
|
|
|
#include "dvdauth.h"
|
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
|
|
|
|
#if OLD_CSS_API
|
|
|
|
/*
|
|
|
|
* provide some backward compatibiliy macros to compile this
|
|
|
|
* code using the old libcss-0.1
|
|
|
|
*/
|
|
|
|
#define DVDHandle int
|
|
|
|
#define DVDOpenFailed (-1)
|
|
|
|
|
|
|
|
#define DVDAuth(hdl, s) ioctl(hdl, DVD_AUTH, s)
|
|
|
|
#define DVDOpenDevice(path) open(path, O_RDONLY)
|
|
|
|
#define DVDCloseDevice(hdl) close(hdl)
|
|
|
|
#define CSSDVDisEncrypted(hdl) CSSisEncrypted(hdl)
|
|
|
|
#define CSSDVDAuthDisc CSSAuthDisc
|
|
|
|
#define CSSDVDAuthTitlePath(hdl,key_title,path) \
|
|
|
|
CSSAuthTitle(hdl,key_title,path_to_lba(path))
|
|
|
|
|
|
|
|
#else /*OLD_CSS_API*/
|
|
|
|
|
|
|
|
#define DVDHandle struct dvd_device *
|
|
|
|
#define DVDOpenFailed NULL
|
|
|
|
|
|
|
|
#endif /*OLD_CSS_API*/
|
|
|
|
|
|
|
|
|
2001-06-05 02:13:31 +00:00
|
|
|
char *dvd_auth_device=NULL;
|
2001-04-17 22:04:44 +00:00
|
|
|
unsigned char key_disc[2048];
|
|
|
|
unsigned char key_title[5];
|
2001-04-20 14:32:09 +00:00
|
|
|
unsigned char *dvdimportkey=NULL;
|
|
|
|
int descrambling=0;
|
2001-04-17 22:04:44 +00:00
|
|
|
|
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
#if OLD_CSS_API
|
|
|
|
/*
|
|
|
|
* With the old libcss-0.1 api, we have to find out the LBA for
|
|
|
|
* a title for title authentication.
|
|
|
|
*/
|
|
|
|
#ifdef __linux__
|
2001-04-17 22:04:44 +00:00
|
|
|
#include <linux/fs.h>
|
2001-06-06 21:16:21 +00:00
|
|
|
#include <errno.h>
|
2001-04-17 22:04:44 +00:00
|
|
|
|
|
|
|
#ifndef FIBMAP
|
|
|
|
#define FIBMAP 1
|
|
|
|
#endif
|
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
static int path_to_lba (char *path)
|
2001-04-17 22:04:44 +00:00
|
|
|
{
|
2001-06-06 21:16:21 +00:00
|
|
|
int fd, lba = 0;
|
|
|
|
|
|
|
|
if ((fd = open(path, O_RDONLY)) == -1) {
|
|
|
|
fprintf(stderr, "Cannot open file %s: %s",
|
|
|
|
path ? path : "(NULL)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ioctl(fd, FIBMAP, &lba) != 0) {
|
|
|
|
perror ("ioctl FIBMAP");
|
|
|
|
fprintf(stderr,"Hint: run mplayer as root!\n");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return lba;
|
2001-04-17 22:04:44 +00:00
|
|
|
}
|
2001-06-06 21:16:21 +00:00
|
|
|
#else /*linux*/
|
|
|
|
static int path_to_lba (char *path)
|
|
|
|
{
|
|
|
|
#warning translating pathname to iso9660 LBA is not supported on this platform
|
|
|
|
fprintf(stderr, "Translating pathname to iso9660 LBA is not supported on this platform\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /*linux*/
|
|
|
|
#endif /*OLD_CSS_API*/
|
2001-04-17 22:04:44 +00:00
|
|
|
|
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
static void reset_agids ( DVDHandle dvd )
|
2001-04-17 22:04:44 +00:00
|
|
|
{
|
|
|
|
dvd_authinfo ai;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
memset(&ai, 0, sizeof(ai));
|
|
|
|
ai.type = DVD_INVALIDATE_AGID;
|
|
|
|
ai.lsa.agid = i;
|
2001-06-06 21:16:21 +00:00
|
|
|
DVDAuth(dvd, &ai);
|
2001-04-17 22:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-20 14:32:09 +00:00
|
|
|
int dvd_import_key ( unsigned char *hexkey )
|
|
|
|
{
|
|
|
|
unsigned char *t=key_title;
|
|
|
|
int digit=4,len;
|
|
|
|
bzero(key_title,sizeof(key_title));
|
|
|
|
// printf("DVD key: %s\n",hexkey);
|
|
|
|
for (len=0;len<10;len++) {
|
|
|
|
// printf("-> %c\n",*hexkey);
|
|
|
|
if (!*hexkey) return 1;
|
|
|
|
if (*hexkey>='A'&&*hexkey<='F') *t|=(*hexkey-'A'+10)<<digit;
|
|
|
|
else if (*hexkey>='0'&&*hexkey<='9') *t|=(*hexkey-'0')<<digit;
|
|
|
|
else return 1;
|
|
|
|
if (digit) digit=0; else {
|
|
|
|
digit=4;
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
hexkey++;
|
|
|
|
}
|
|
|
|
if (*hexkey) return 1;
|
|
|
|
printf("DVD key (requested): %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
|
|
|
|
descrambling=1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-04-17 22:04:44 +00:00
|
|
|
|
2001-04-21 15:28:19 +00:00
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
int dvd_auth ( char *dev , char *filename )
|
2001-04-17 22:04:44 +00:00
|
|
|
{
|
2001-06-06 21:16:21 +00:00
|
|
|
DVDHandle dvd; /* DVD device handle */
|
2001-04-17 22:04:44 +00:00
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
if ((dvd=DVDOpenDevice(dev)) == DVDOpenFailed) {
|
2001-04-17 22:04:44 +00:00
|
|
|
fprintf(stderr,"DVD: cannot open DVD device \"%s\".\n",dev);
|
|
|
|
return 1;
|
|
|
|
}
|
2001-04-21 15:28:19 +00:00
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
if (!CSSDVDisEncrypted(dvd)) {
|
2001-04-21 15:28:19 +00:00
|
|
|
printf("DVD is unencrypted! Skipping authentication!\n(note: you should not use -dvd switch for unencrypted discs!)\n");
|
2001-06-06 21:16:21 +00:00
|
|
|
DVDCloseDevice(dvd);
|
2001-04-21 15:28:19 +00:00
|
|
|
return 0;
|
|
|
|
} else printf("DVD is encrypted, issuing authentication ...\n");
|
2001-04-17 22:04:44 +00:00
|
|
|
|
|
|
|
/* reset AGIDs */
|
2001-06-06 21:16:21 +00:00
|
|
|
reset_agids(dvd);
|
2001-04-17 22:04:44 +00:00
|
|
|
|
|
|
|
/* authenticate disc */
|
2001-06-06 21:16:21 +00:00
|
|
|
if (CSSDVDAuthDisc(dvd,key_disc)) {
|
|
|
|
fprintf(stderr,"DVD: CSSDVDAuthDisc() failed.\n");
|
|
|
|
DVDCloseDevice(dvd);
|
2001-04-17 22:04:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
if (CSSDVDAuthTitlePath(dvd,key_title,filename)) {
|
|
|
|
fprintf(stderr,"DVD: CSSDVDAuthTitle() failed.\n");
|
|
|
|
DVDCloseDevice(dvd);
|
2001-04-17 22:04:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decrypting title */
|
|
|
|
if (CSSDecryptTitleKey (key_title, key_disc) < 0) {
|
|
|
|
fprintf(stderr,"DVD: CSSDecryptTitleKey() failed.\n");
|
2001-06-06 21:16:21 +00:00
|
|
|
DVDCloseDevice(dvd);
|
2001-04-17 22:04:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
DVDCloseDevice(dvd);
|
2001-04-20 14:32:09 +00:00
|
|
|
printf("DVD title key is: %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
|
|
|
|
descrambling=1;
|
2001-04-17 22:04:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|