492
|
1 /* (C)2001 by LGB (Gabor Lenart), based on example programs in libcss
|
559
|
2 lgb@lgb.hu */
|
492
|
3
|
|
4 /* don't do anything with this source if css support was not requested */
|
|
5 #include "config.h"
|
|
6 #ifdef HAVE_LIBCSS
|
|
7
|
|
8 #include <stdio.h>
|
|
9 #include <stdlib.h>
|
|
10 #include <linux/cdrom.h>
|
|
11 // FIXME #include <string.h> conflicts with #include <linux/fs.h> (below)
|
|
12 //#include <string.h> // FIXME this conflicts with #include <linux/fs.h>
|
|
13 #include <unistd.h>
|
|
14 #include <fcntl.h>
|
|
15 #include <sys/types.h>
|
|
16 #include <sys/ioctl.h>
|
|
17 #include <sys/stat.h>
|
|
18
|
|
19 #include <css.h>
|
|
20
|
|
21 #include "dvdauth.h"
|
|
22
|
|
23 char *dvd_device=NULL;
|
|
24 unsigned char key_disc[2048];
|
|
25 unsigned char key_title[5];
|
546
|
26 unsigned char *dvdimportkey=NULL;
|
|
27 int descrambling=0;
|
492
|
28
|
|
29
|
|
30 #include <linux/fs.h>
|
|
31
|
|
32 #ifndef FIBMAP
|
|
33 #define FIBMAP 1
|
|
34 #endif
|
|
35
|
|
36
|
|
37 static int path_to_lba ( int fd )
|
|
38 {
|
|
39 int lba = 0;
|
|
40 if (ioctl(fd, FIBMAP, &lba) < 0) {
|
|
41 perror ("ioctl FIBMAP");
|
|
42 fprintf(stderr,"Hint: run mplayer as root!\n");
|
|
43 // close(fd);
|
|
44 return -1;
|
|
45 }
|
|
46 return lba;
|
|
47 }
|
|
48
|
|
49
|
|
50
|
|
51 static void reset_agids ( int fd )
|
|
52 {
|
|
53 dvd_authinfo ai;
|
|
54 int i;
|
|
55 for (i = 0; i < 4; i++) {
|
|
56 memset(&ai, 0, sizeof(ai));
|
|
57 ai.type = DVD_INVALIDATE_AGID;
|
|
58 ai.lsa.agid = i;
|
|
59 ioctl(fd, DVD_AUTH, &ai);
|
|
60 }
|
|
61 }
|
|
62
|
|
63
|
546
|
64 int dvd_import_key ( unsigned char *hexkey )
|
|
65 {
|
|
66 unsigned char *t=key_title;
|
|
67 int digit=4,len;
|
|
68 bzero(key_title,sizeof(key_title));
|
|
69 // printf("DVD key: %s\n",hexkey);
|
|
70 for (len=0;len<10;len++) {
|
|
71 // printf("-> %c\n",*hexkey);
|
|
72 if (!*hexkey) return 1;
|
|
73 if (*hexkey>='A'&&*hexkey<='F') *t|=(*hexkey-'A'+10)<<digit;
|
|
74 else if (*hexkey>='0'&&*hexkey<='9') *t|=(*hexkey-'0')<<digit;
|
|
75 else return 1;
|
|
76 if (digit) digit=0; else {
|
|
77 digit=4;
|
|
78 t++;
|
|
79 }
|
|
80 hexkey++;
|
|
81 }
|
|
82 if (*hexkey) return 1;
|
|
83 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]);
|
|
84 descrambling=1;
|
|
85 return 0;
|
|
86 }
|
|
87
|
492
|
88
|
559
|
89
|
492
|
90 int dvd_auth ( char *dev , int fd )
|
|
91 {
|
|
92 int devfd; /* FD of DVD device */
|
|
93 int lba;
|
|
94
|
|
95
|
|
96 if ((devfd=open(dev,O_RDONLY))<0) {
|
|
97 fprintf(stderr,"DVD: cannot open DVD device \"%s\".\n",dev);
|
|
98 return 1;
|
|
99 }
|
559
|
100
|
|
101 if (!CSSisEncrypted(devfd)) {
|
|
102 printf("DVD is unencrypted! Skipping authentication!\n(note: you should not use -dvd switch for unencrypted discs!)\n");
|
|
103 return 0;
|
|
104 } else printf("DVD is encrypted, issuing authentication ...\n");
|
492
|
105
|
|
106 /* reset AGIDs */
|
|
107 reset_agids(devfd);
|
|
108
|
|
109 /* authenticate disc */
|
|
110 if (CSSAuthDisc(devfd,key_disc)) {
|
|
111 fprintf(stderr,"DVD: CSSAuthDisc() failed.\n");
|
|
112 return 1;
|
|
113 }
|
|
114
|
|
115 /* authenticate title */
|
|
116 lba=path_to_lba(fd);
|
|
117 if (lba==-1) {
|
|
118 fprintf(stderr,"DVD: path_to_lba() failed.\n");
|
|
119 return 1;
|
|
120 }
|
|
121 if (CSSAuthTitle(devfd,key_title,lba)) {
|
|
122 fprintf(stderr,"DVD: CSSAuthTitle() failed.\n");
|
|
123 return 1;
|
|
124 }
|
|
125
|
|
126 /* decrypting title */
|
|
127 if (CSSDecryptTitleKey (key_title, key_disc) < 0) {
|
|
128 fprintf(stderr,"DVD: CSSDecryptTitleKey() failed.\n");
|
|
129 return 1;
|
|
130 }
|
|
131
|
|
132 close(devfd);
|
546
|
133 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]);
|
|
134 descrambling=1;
|
492
|
135 return 0;
|
|
136 }
|
|
137
|
|
138
|
|
139 #endif
|