1 /* Copyright (C) 2001-2008 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied, modified
8    or distributed except as expressly authorized under the terms of that
9    license.  Refer to licensing information at http://www.artifex.com/
10    or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11    San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 
14 /* $Id$ */
15 
16 /* this is the ps interpreter interface to the AES cipher filter
17    used in PDF encryption. We currently provide only decode support. */
18 
19 #include "memory_.h"
20 #include "ghost.h"
21 #include "oper.h"
22 #include "gsstruct.h"
23 #include "ialloc.h"
24 #include "idict.h"
25 #include "stream.h"
26 #include "strimpl.h"
27 #include "ifilter.h"
28 #include "saes.h"
29 
30 /* <source> <dict> aes/filter <file> */
31 
32 static int
z_aes_d(i_ctx_t * i_ctx_p)33 z_aes_d(i_ctx_t * i_ctx_p)
34 {
35     os_ptr op = osp;		/* i_ctx_p->op_stack.stack.p defined in osstack.h */
36     ref *sop = NULL;
37     stream_aes_state state;
38 
39     /* extract the key from the parameter dictionary */
40     check_type(*op, t_dictionary);
41     check_dict_read(*op);
42     if (dict_find_string(op, "Key", &sop) <= 0)
43 	return_error(e_rangecheck);
44 
45     s_aes_set_key(&state, sop->value.const_bytes, r_size(sop));
46 
47     /* we pass npop=0, since we've no arguments left to consume */
48     /* FIXME: passing 0 instead of the usual rspace(sop) will allocate
49        storage for filter state from the same memory pool as the stream
50        it's coding. this caused no trouble when we were the arcfour cipher
51        and maintained no pointers. */
52     return filter_read(i_ctx_p, 0, &s_aes_template,
53 		       (stream_state *) & state, 0);
54 }
55 
56 /* Match the above routine to its postscript filter name.
57    This is how our static routines get called externally. */
58 const op_def zfaes_op_defs[] = {
59     op_def_begin_filter(),
60     {"2AESDecode", z_aes_d},
61     op_def_end(0)
62 };
63