2002-01-04 13:08:14 +00:00
|
|
|
/* (C)2001,2002 by LGB (Gabor Lenart), based on example programs in libcss
|
|
|
|
lgb@lgb.hu
|
|
|
|
|
|
|
|
This source is part of MPlayer project. This source is copyrighted by
|
|
|
|
the author according to rules declared in GNU/GPL license.
|
|
|
|
|
|
|
|
2001 Inital version (LGB)
|
|
|
|
2001 fibmap_mplayer to avoid uid=0 mplayer need (LGB)
|
|
|
|
2001 Support for libcss with the new API (by ???)
|
|
|
|
2002/Jan/04 Use dlopen to access libcss.so to avoid conflict with
|
|
|
|
libdvdread [now with only libcss with old API (LGB)
|
|
|
|
|
|
|
|
TODO:
|
|
|
|
support for libcss libraries with new API */
|
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
|
|
|
|
|
2002-01-04 13:08:14 +00:00
|
|
|
#warning FIXME: Dynamic loading of libcss.so with newer (ver>0.1) libcss API is not supported in this version!
|
|
|
|
|
2001-04-17 22:04:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-06-06 21:16:21 +00:00
|
|
|
//#include <string.h> // FIXME: conflicts with fs.h
|
2001-08-14 12:17:38 +00:00
|
|
|
#include <errno.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>
|
2001-06-18 23:56:19 +00:00
|
|
|
#include <sys/wait.h>
|
2002-01-04 13:08:14 +00:00
|
|
|
// #include <css.h>
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
2001-06-06 21:16:21 +00:00
|
|
|
# include <sys/dvdio.h>
|
2002-01-04 13:08:14 +00:00
|
|
|
#elif defined(__linux__)
|
2001-06-06 21:16:21 +00:00
|
|
|
# include <linux/cdrom.h>
|
2002-01-04 13:08:14 +00:00
|
|
|
#elif defined(__sun)
|
2001-06-06 21:16:21 +00:00
|
|
|
# include <sun/dvdio.h>
|
2002-01-04 13:08:14 +00:00
|
|
|
#else
|
2001-06-06 21:16:21 +00:00
|
|
|
# error "Need the DVD ioctls"
|
|
|
|
#endif
|
2001-04-17 22:04:44 +00:00
|
|
|
|
2002-01-04 13:08:14 +00:00
|
|
|
#include <dlfcn.h>
|
2001-04-17 22:04:44 +00:00
|
|
|
#include "dvdauth.h"
|
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
|
2002-01-04 13:08:14 +00:00
|
|
|
// #if OLD_CSS_API
|
2001-06-06 21:16:21 +00:00
|
|
|
/*
|
|
|
|
* 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)
|
2002-01-04 13:08:14 +00:00
|
|
|
// #define CSSDVDisEncrypted(hdl) CSSisEncrypted(hdl)
|
|
|
|
// #define CSSDVDAuthDisc CSSAuthDisc
|
2001-06-06 21:16:21 +00:00
|
|
|
|
|
|
|
|
2001-06-05 02:13:31 +00:00
|
|
|
char *dvd_auth_device=NULL;
|
2001-06-18 23:56:19 +00:00
|
|
|
char *dvd_device=NULL;
|
|
|
|
char *dvd_raw_device=NULL;
|
2002-01-04 13:08:14 +00:00
|
|
|
char *css_so=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
|
|
|
|
2002-01-04 13:08:14 +00:00
|
|
|
static void *dlid;
|
|
|
|
static int (*dl_CSSisEncrypted)(int);
|
|
|
|
static int (*dl_CSSAuthDisc)(int,char *);
|
|
|
|
static int (*dl_CSSAuthTitle)(int, char *,int);
|
|
|
|
static int (*dl_CSSGetASF)(int);
|
|
|
|
static int (*dl_CSSDecryptTitleKey)(char *, char *);
|
|
|
|
static void (*dl_CSSDescramble)(u_char *, u_char *);
|
|
|
|
|
|
|
|
dvd_css_descramble ( u_char *sec , u_char *key )
|
|
|
|
{
|
|
|
|
(*dl_CSSDescramble)(sec,key);
|
|
|
|
}
|
|
|
|
|
2001-04-17 22:04:44 +00:00
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
#ifdef __linux__
|
2001-04-17 22:04:44 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
|
|
|
|
#ifndef FIBMAP
|
|
|
|
#define FIBMAP 1
|
|
|
|
#endif
|
|
|
|
|
2002-01-04 13:08:14 +00:00
|
|
|
|
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-18 23:56:19 +00:00
|
|
|
int lba = 0;
|
|
|
|
char cmd[100];
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
snprintf(cmd,sizeof(cmd),"fibmap_mplayer %s",path);
|
|
|
|
fp=popen(cmd,"r");
|
|
|
|
if (fp) {
|
|
|
|
int ret;
|
2001-12-24 10:35:43 +00:00
|
|
|
memset(cmd,0,sizeof(cmd));
|
2001-06-18 23:56:19 +00:00
|
|
|
fgets(cmd,99,fp);
|
2002-01-04 13:08:14 +00:00
|
|
|
// printf("DVD: cmd: %s\n",cmd);
|
2001-06-18 23:56:19 +00:00
|
|
|
if ((ret=pclose(fp)))
|
2002-01-04 13:08:14 +00:00
|
|
|
fprintf(stderr,"DVD: fibmap_mplayer: %s\n",*cmd?cmd:"no error info");
|
|
|
|
if (cmd[0]<'0'||cmd[0]>'9') fp=NULL; else {
|
|
|
|
if(WIFEXITED(ret) && !WEXITSTATUS(ret)) {
|
|
|
|
lba=atoi(cmd);
|
|
|
|
printf("DVD: fibmap_mplayer is being used\n");
|
|
|
|
} else
|
|
|
|
fp=NULL;
|
|
|
|
}
|
2001-06-06 21:16:21 +00:00
|
|
|
}
|
2001-06-18 23:56:19 +00:00
|
|
|
if (!fp) {
|
|
|
|
int fd;
|
2002-01-04 13:08:14 +00:00
|
|
|
printf("DVD: fibmap_mplayer could not run, trying with ioctl() ...\n");
|
2001-06-18 23:56:19 +00:00
|
|
|
if ((fd = open(path, O_RDONLY)) == -1) {
|
2002-01-04 13:08:14 +00:00
|
|
|
fprintf(stderr, "DVD: Cannot open file %s: %s",
|
2001-06-18 23:56:19 +00:00
|
|
|
path ? path : "(NULL)", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ioctl(fd, FIBMAP, &lba) != 0) {
|
2002-01-04 13:08:14 +00:00
|
|
|
perror("DVD: ioctl FIBMAP");
|
|
|
|
fprintf(stderr," Hint: run mplayer as root (or better to install fibmap_mplayer as suid root)!\n");
|
2001-06-18 23:56:19 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
2001-06-06 21:16:21 +00:00
|
|
|
}
|
2002-01-04 13:08:14 +00:00
|
|
|
printf("DVD: LBA: %d\n",lba);
|
2001-06-06 21:16:21 +00:00
|
|
|
return lba;
|
2001-04-17 22:04:44 +00:00
|
|
|
}
|
2001-06-18 23:56:19 +00:00
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
#else /*linux*/
|
2002-01-04 13:08:14 +00:00
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
static int path_to_lba (char *path)
|
|
|
|
{
|
|
|
|
#warning translating pathname to iso9660 LBA is not supported on this platform
|
2002-01-04 13:08:14 +00:00
|
|
|
fprintf(stderr, "DVD: Translating pathname to iso9660 LBA is not supported on this platform\n");
|
2001-06-06 21:16:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-01-04 13:08:14 +00:00
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
#endif /*linux*/
|
2002-01-04 13:08:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int CSSDVDAuthTitlePath(DVDHandle hdl,unsigned char *key_title,char *path)
|
|
|
|
{
|
|
|
|
int lba=path_to_lba(path);
|
|
|
|
if (lba==-1) return -1;
|
|
|
|
return (*dl_CSSAuthTitle)(hdl,key_title,lba);
|
|
|
|
}
|
|
|
|
|
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;
|
2001-12-24 10:35:43 +00:00
|
|
|
memset(key_title,0,sizeof(key_title));
|
2001-04-20 14:32:09 +00:00
|
|
|
// 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;
|
2002-01-04 13:08:14 +00:00
|
|
|
printf("DVD: DVD key (requested): %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
|
2001-04-20 14:32:09 +00:00
|
|
|
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
|
|
|
|
2002-01-11 22:15:55 +00:00
|
|
|
if (!css_so) css_so=strdup("libcss.so");
|
2002-01-04 13:08:14 +00:00
|
|
|
printf("DVD: opening libcss.so as %s ...\n",css_so);
|
|
|
|
dlid=dlopen(css_so,RTLD_NOW);
|
|
|
|
if (!dlid) {
|
|
|
|
printf("DVD: dlopen: %s\n",dlerror());
|
|
|
|
return 1;
|
|
|
|
} printf("DVD: dlopen OK!\n");
|
|
|
|
|
|
|
|
#define CSS_DLSYM(v,s) if (!(v=dlsym(dlid,s))) {\
|
|
|
|
fprintf(stderr,"DVD: %s\n Hint: use libcss version 0.1!\n",dlerror());\
|
|
|
|
return 1; }
|
|
|
|
|
|
|
|
CSS_DLSYM(dl_CSSisEncrypted,"CSSisEncrypted");
|
|
|
|
CSS_DLSYM(dl_CSSAuthDisc,"CSSAuthDisc");
|
|
|
|
CSS_DLSYM(dl_CSSAuthTitle,"CSSAuthTitle");
|
|
|
|
CSS_DLSYM(dl_CSSGetASF,"CSSGetASF");
|
|
|
|
CSS_DLSYM(dl_CSSDecryptTitleKey,"CSSDecryptTitleKey");
|
|
|
|
CSS_DLSYM(dl_CSSDescramble,"CSSDescramble");
|
|
|
|
|
|
|
|
#undef CSS_DLSYM
|
|
|
|
|
2001-06-06 21:16:21 +00:00
|
|
|
if ((dvd=DVDOpenDevice(dev)) == DVDOpenFailed) {
|
2001-08-14 12:17:38 +00:00
|
|
|
fprintf(stderr,"DVD: cannot open DVD device \"%s\": %s.\n",
|
|
|
|
dev, strerror(errno));
|
2001-04-17 22:04:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-01-04 13:08:14 +00:00
|
|
|
|
|
|
|
if (!(*dl_CSSisEncrypted)(dvd)) {
|
|
|
|
printf("DVD: 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;
|
2002-01-04 13:08:14 +00:00
|
|
|
} else printf("DVD: 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 */
|
2002-01-04 13:08:14 +00:00
|
|
|
if ((*dl_CSSAuthDisc)(dvd,key_disc)) {
|
2001-06-06 21:16:21 +00:00
|
|
|
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 */
|
2002-01-04 13:08:14 +00:00
|
|
|
if ((*dl_CSSDecryptTitleKey)(key_title, key_disc) < 0) {
|
2001-04-17 22:04:44 +00:00
|
|
|
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);
|
2002-01-04 13:08:14 +00:00
|
|
|
printf("DVD: 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]);
|
2001-04-20 14:32:09 +00:00
|
|
|
descrambling=1;
|
2001-04-17 22:04:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|