KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > encryption > PDFEncryption


1 /**
2  * Copyright (c) 2003-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.encryption;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.ByteArrayOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38
39 import java.security.MessageDigest JavaDoc;
40 import java.security.NoSuchAlgorithmException JavaDoc;
41
42 import org.pdfbox.exceptions.CryptographyException;
43
44 /**
45  * This class will deal with PDF encryption algorithms.
46  *
47  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
48  * @version $Revision: 1.15 $
49  *
50  * @deprecated use the new security layer instead
51  *
52  * @see org.pdfbox.pdmodel.encryption.StandardSecurityHandler
53  */

54 public final class PDFEncryption
55 {
56     private ARCFour rc4 = new ARCFour();
57     /**
58      * The encryption padding defined in the PDF 1.4 Spec algorithm 3.2.
59      */

60     public static final byte[] ENCRYPT_PADDING =
61     {
62         (byte)0x28, (byte)0xBF, (byte)0x4E, (byte)0x5E, (byte)0x4E,
63         (byte)0x75, (byte)0x8A, (byte)0x41, (byte)0x64, (byte)0x00,
64         (byte)0x4E, (byte)0x56, (byte)0xFF, (byte)0xFA, (byte)0x01,
65         (byte)0x08, (byte)0x2E, (byte)0x2E, (byte)0x00, (byte)0xB6,
66         (byte)0xD0, (byte)0x68, (byte)0x3E, (byte)0x80, (byte)0x2F,
67         (byte)0x0C, (byte)0xA9, (byte)0xFE, (byte)0x64, (byte)0x53,
68         (byte)0x69, (byte)0x7A
69     };
70
71     /**
72      * This will encrypt a piece of data.
73      *
74      * @param objectNumber The id for the object.
75      * @param genNumber The generation id for the object.
76      * @param key The key used to encrypt the data.
77      * @param data The data to encrypt/decrypt.
78      * @param output The stream to write to.
79      *
80      * @throws CryptographyException If there is an error encrypting the data.
81      * @throws IOException If there is an io error.
82      */

83     public final void encryptData(
84         long objectNumber,
85         long genNumber,
86         byte[] key,
87         InputStream JavaDoc data,
88         OutputStream JavaDoc output )
89         throws CryptographyException, IOException JavaDoc
90     {
91         byte[] newKey = new byte[ key.length + 5 ];
92         System.arraycopy( key, 0, newKey, 0, key.length );
93         //PDF 1.4 reference pg 73
94
//step 1
95
//we have the reference
96

97         //step 2
98
newKey[newKey.length -5] = (byte)(objectNumber & 0xff);
99         newKey[newKey.length -4] = (byte)((objectNumber >> 8) & 0xff);
100         newKey[newKey.length -3] = (byte)((objectNumber >> 16) & 0xff);
101         newKey[newKey.length -2] = (byte)(genNumber & 0xff);
102         newKey[newKey.length -1] = (byte)((genNumber >> 8) & 0xff);
103
104
105         //step 3
106
byte[] digestedKey = null;
107         try
108         {
109             MessageDigest JavaDoc md = MessageDigest.getInstance( "MD5" );
110             digestedKey = md.digest( newKey );
111         }
112         catch( NoSuchAlgorithmException JavaDoc e )
113         {
114             throw new CryptographyException( e );
115         }
116
117         //step 4
118
int length = Math.min( newKey.length, 16 );
119         byte[] finalKey = new byte[ length ];
120         System.arraycopy( digestedKey, 0, finalKey, 0, length );
121
122         rc4.setKey( finalKey );
123         rc4.write( data, output );
124         output.flush();
125     }
126
127     /**
128      * This will get the user password from the owner password and the documents o value.
129      *
130      * @param ownerPassword The plaintext owner password.
131      * @param o The document's o entry.
132      * @param revision The document revision number.
133      * @param length The length of the encryption.
134      *
135      * @return The plaintext padded user password.
136      *
137      * @throws CryptographyException If there is an error getting the user password.
138      * @throws IOException If there is an error reading data.
139      */

140     public final byte[] getUserPassword(
141         byte[] ownerPassword,
142         byte[] o,
143         int revision,
144         long length )
145         throws CryptographyException, IOException JavaDoc
146     {
147         try
148         {
149             ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
150
151             //3.3 STEP 1
152
byte[] ownerPadded = truncateOrPad( ownerPassword );
153
154             //3.3 STEP 2
155
MessageDigest JavaDoc md = MessageDigest.getInstance( "MD5" );
156             md.update( ownerPadded );
157             byte[] digest = md.digest();
158
159             //3.3 STEP 3
160
if( revision == 3 || revision == 4 )
161             {
162                 for( int i=0; i<50; i++ )
163                 {
164                     md.reset();
165                     md.update( digest );
166                     digest = md.digest();
167                 }
168             }
169             if( revision == 2 && length != 5 )
170             {
171                 throw new CryptographyException(
172                     "Error: Expected length=5 actual=" + length );
173             }
174
175             //3.3 STEP 4
176
byte[] rc4Key = new byte[ (int)length ];
177             System.arraycopy( digest, 0, rc4Key, 0, (int)length );
178
179             //3.7 step 2
180
if( revision == 2 )
181             {
182                 rc4.setKey( rc4Key );
183                 rc4.write( o, result );
184             }
185             else if( revision == 3 || revision == 4)
186             {
187                 /**
188                 byte[] iterationKey = new byte[ rc4Key.length ];
189                 byte[] dataToEncrypt = o;
190                 for( int i=19; i>=0; i-- )
191                 {
192                     System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
193                     for( int j=0; j< iterationKey.length; j++ )
194                     {
195                         iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
196                     }
197                     rc4.setKey( iterationKey );
198                     rc4.write( dataToEncrypt, result );
199                     dataToEncrypt = result.toByteArray();
200                     result.reset();
201                 }
202                 result.write( dataToEncrypt, 0, dataToEncrypt.length );
203                 */

204                 byte[] iterationKey = new byte[ rc4Key.length ];
205                 
206               
207                 byte[] otemp = new byte[ o.length ]; //sm
208
System.arraycopy( o, 0, otemp, 0, o.length ); //sm
209
rc4.write( o, result);//sm
210

211                 for( int i=19; i>=0; i-- )
212                 {
213                     System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
214                     for( int j=0; j< iterationKey.length; j++ )
215                     {
216                         iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
217                     }
218                     rc4.setKey( iterationKey );
219                     result.reset(); //sm
220
rc4.write( otemp, result ); //sm
221
otemp = result.toByteArray(); //sm
222
}
223             }
224
225
226             return result.toByteArray();
227
228         }
229         catch( NoSuchAlgorithmException JavaDoc e )
230         {
231             throw new CryptographyException( e );
232         }
233     }
234
235     /**
236      * This will tell if this is the owner password or not.
237      *
238      * @param ownerPassword The plaintext owner password.
239      * @param u The U value from the PDF Document.
240      * @param o The owner password hash.
241      * @param permissions The document permissions.
242      * @param id The document id.
243      * @param revision The revision of the encryption.
244      * @param length The length of the encryption key.
245      *
246      * @return true if the owner password matches the one from the document.
247      *
248      * @throws CryptographyException If there is an error while executing crypt functions.
249      * @throws IOException If there is an error while checking owner password.
250      */

251     public final boolean isOwnerPassword(
252         byte[] ownerPassword,
253         byte[] u,
254         byte[] o,
255         int permissions,
256         byte[] id,
257         int revision,
258         int length)
259         throws CryptographyException, IOException JavaDoc
260     {
261         byte[] userPassword = getUserPassword( ownerPassword, o, revision, length );
262         return isUserPassword( userPassword, u, o, permissions, id, revision, length );
263     }
264
265     /**
266      * This will tell if this is a valid user password.
267      *
268      * Algorithm 3.6 pg 80
269      *
270      * @param password The password to test.
271      * @param u The U value from the PDF Document.
272      * @param o The owner password hash.
273      * @param permissions The document permissions.
274      * @param id The document id.
275      * @param revision The revision of the encryption.
276      * @param length The length of the encryption key.
277      *
278      * @return true If this is the correct user password.
279      *
280      * @throws CryptographyException If there is an error computing the value.
281      * @throws IOException If there is an IO error while computing the owners password.
282      */

283     public final boolean isUserPassword(
284         byte[] password,
285         byte[] u,
286         byte[] o,
287         int permissions,
288         byte[] id,
289         int revision,
290         int length)
291         throws CryptographyException, IOException JavaDoc
292     {
293         boolean matches = false;
294         //STEP 1
295
byte[] computedValue = computeUserPassword( password, o, permissions, id, revision, length );
296         if( revision == 2 )
297         {
298             //STEP 2
299
matches = arraysEqual( u, computedValue );
300         }
301         else if( revision == 3 || revision == 4 )
302         {
303             //STEP 2
304
matches = arraysEqual( u, computedValue, 16 );
305         }
306         return matches;
307     }
308
309     /**
310      * This will compare two byte[] for equality for count number of bytes.
311      *
312      * @param first The first byte array.
313      * @param second The second byte array.
314      * @param count The number of bytes to compare.
315      *
316      * @return true If the arrays contain the exact same data.
317      */

318     private final boolean arraysEqual( byte[] first, byte[] second, int count )
319     {
320         boolean equal = first.length >= count && second.length >= count;
321         for( int i=0; i<count && equal; i++ )
322         {
323             equal = first[i] == second[i];
324         }
325         return equal;
326     }
327
328     /**
329      * This will compare two byte[] for equality.
330      *
331      * @param first The first byte array.
332      * @param second The second byte array.
333      *
334      * @return true If the arrays contain the exact same data.
335      */

336     private final boolean arraysEqual( byte[] first, byte[] second )
337     {
338         boolean equal = first.length == second.length;
339         for( int i=0; i<first.length && equal; i++ )
340         {
341             equal = first[i] == second[i];
342         }
343         return equal;
344     }
345
346     /**
347      * This will compute the user password hash.
348      *
349      * @param password The plain text password.
350      * @param o The owner password hash.
351      * @param permissions The document permissions.
352      * @param id The document id.
353      * @param revision The revision of the encryption.
354      * @param length The length of the encryption key.
355      *
356      * @return The user password.
357      *
358      * @throws CryptographyException If there is an error computing the user password.
359      * @throws IOException If there is an IO error.
360      */

361     public final byte[] computeUserPassword(
362         byte[] password,
363         byte[] o,
364         int permissions,
365         byte[] id,
366         int revision,
367         int length )
368         throws CryptographyException, IOException JavaDoc
369     {
370         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
371         //STEP 1
372
byte[] encryptionKey = computeEncryptedKey( password, o, permissions, id, revision, length );
373
374         if( revision == 2 )
375         {
376             //STEP 2
377
rc4.setKey( encryptionKey );
378             rc4.write( ENCRYPT_PADDING, result );
379         }
380         else if( revision == 3 || revision == 4 )
381         {
382             try
383             {
384                 //STEP 2
385
MessageDigest JavaDoc md = MessageDigest.getInstance("MD5");
386                 //md.update( truncateOrPad( password ) );
387
md.update( ENCRYPT_PADDING );
388
389                 //STEP 3
390
md.update( id );
391                 result.write( md.digest() );
392
393                 //STEP 4 and 5
394
byte[] iterationKey = new byte[ encryptionKey.length ];
395                 for( int i=0; i<20; i++ )
396                 {
397                     System.arraycopy( encryptionKey, 0, iterationKey, 0, iterationKey.length );
398                     for( int j=0; j< iterationKey.length; j++ )
399                     {
400                         iterationKey[j] = (byte)(iterationKey[j] ^ i);
401                     }
402                     rc4.setKey( iterationKey );
403                     ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc( result.toByteArray() );
404                     result.reset();
405                     rc4.write( input, result );
406                 }
407
408                 //step 6
409
byte[] finalResult = new byte[32];
410                 System.arraycopy( result.toByteArray(), 0, finalResult, 0, 16 );
411                 System.arraycopy( ENCRYPT_PADDING, 0, finalResult, 16, 16 );
412                 result.reset();
413                 result.write( finalResult );
414             }
415             catch( NoSuchAlgorithmException JavaDoc e )
416             {
417                 throw new CryptographyException( e );
418             }
419         }
420         return result.toByteArray();
421     }
422
423     /**
424      * This will compute the encrypted key.
425      *
426      * @param password The password used to compute the encrypted key.
427      * @param o The owner password hash.
428      * @param permissions The permissions for the document.
429      * @param id The document id.
430      * @param revision The security revision.
431      * @param length The length of the encryption key.
432      *
433      * @return The encryption key.
434      *
435      * @throws CryptographyException If there is an error computing the key.
436      */

437     public final byte[] computeEncryptedKey(
438         byte[] password,
439         byte[] o,
440         int permissions,
441         byte[] id,
442         int revision,
443         int length )
444         throws CryptographyException
445     {
446         byte[] result = new byte[ length ];
447         try
448         {
449             //PDFReference 1.4 pg 78
450
//step1
451
byte[] padded = truncateOrPad( password );
452
453             //step 2
454
MessageDigest JavaDoc md = MessageDigest.getInstance("MD5");
455             md.update( padded );
456
457             //step 3
458
md.update( o );
459
460             //step 4
461
byte zero = (byte)(permissions >>> 0);
462             byte one = (byte)(permissions >>> 8);
463             byte two = (byte)(permissions >>> 16);
464             byte three = (byte)(permissions >>> 24);
465
466             md.update( zero );
467             md.update( one );
468             md.update( two );
469             md.update( three );
470
471             //step 5
472
md.update( id );
473             byte[] digest = md.digest();
474
475             //step 6
476
if( revision == 3 || revision == 4)
477             {
478                 for( int i=0; i<50; i++ )
479                 {
480                     md.reset();
481                     md.update( digest, 0, length );
482                     digest = md.digest();
483                 }
484             }
485
486             //step 7
487
if( revision == 2 && length != 5 )
488             {
489                 throw new CryptographyException(
490                     "Error: length should be 5 when revision is two actual=" + length );
491             }
492             System.arraycopy( digest, 0, result, 0, length );
493         }
494         catch( NoSuchAlgorithmException JavaDoc e )
495         {
496             throw new CryptographyException( e );
497         }
498         return result;
499     }
500
501     /**
502      * This algorithm is taked from PDF Reference 1.4 Algorithm 3.3 Page 79.
503      *
504      * @param ownerPassword The plain owner password.
505      * @param userPassword The plain user password.
506      * @param revision The version of the security.
507      * @param length The length of the document.
508      *
509      * @return The computed owner password.
510      *
511      * @throws CryptographyException If there is an error computing O.
512      * @throws IOException If there is an error computing O.
513      */

514     public final byte[] computeOwnerPassword(
515         byte[] ownerPassword,
516         byte[] userPassword,
517         int revision,
518         int length )
519         throws CryptographyException, IOException JavaDoc
520     {
521         try
522         {
523             //STEP 1
524
byte[] ownerPadded = truncateOrPad( ownerPassword );
525
526             //STEP 2
527
MessageDigest JavaDoc md = MessageDigest.getInstance( "MD5" );
528             md.update( ownerPadded );
529             byte[] digest = md.digest();
530
531             //STEP 3
532
if( revision == 3 || revision == 4)
533             {
534                 for( int i=0; i<50; i++ )
535                 {
536                     md.reset();
537                     md.update( digest, 0, length );
538                     digest = md.digest();
539                 }
540             }
541             if( revision == 2 && length != 5 )
542             {
543                 throw new CryptographyException(
544                     "Error: Expected length=5 actual=" + length );
545             }
546
547             //STEP 4
548
byte[] rc4Key = new byte[ length ];
549             System.arraycopy( digest, 0, rc4Key, 0, length );
550
551             //STEP 5
552
byte[] paddedUser = truncateOrPad( userPassword );
553
554
555             //STEP 6
556
rc4.setKey( rc4Key );
557             ByteArrayOutputStream JavaDoc crypted = new ByteArrayOutputStream JavaDoc();
558             rc4.write( new ByteArrayInputStream JavaDoc( paddedUser ), crypted );
559
560
561             //STEP 7
562
if( revision == 3 || revision == 4 )
563             {
564                 byte[] iterationKey = new byte[ rc4Key.length ];
565                 for( int i=1; i<20; i++ )
566                 {
567                     System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
568                     for( int j=0; j< iterationKey.length; j++ )
569                     {
570                         iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
571                     }
572                     rc4.setKey( iterationKey );
573                     ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc( crypted.toByteArray() );
574                     crypted.reset();
575                     rc4.write( input, crypted );
576                 }
577             }
578
579             //STEP 8
580
return crypted.toByteArray();
581         }
582         catch( NoSuchAlgorithmException JavaDoc e )
583         {
584             throw new CryptographyException( e.getMessage() );
585         }
586     }
587
588     /**
589      * This will take the password and truncate or pad it as necessary.
590      *
591      * @param password The password to pad or truncate.
592      *
593      * @return The padded or truncated password.
594      */

595     private final byte[] truncateOrPad( byte[] password )
596     {
597         byte[] padded = new byte[ ENCRYPT_PADDING.length ];
598         int bytesBeforePad = Math.min( password.length, padded.length );
599         System.arraycopy( password, 0, padded, 0, bytesBeforePad );
600         System.arraycopy( ENCRYPT_PADDING, 0, padded, bytesBeforePad, ENCRYPT_PADDING.length-bytesBeforePad );
601         return padded;
602     }
603 }
Popular Tags