~ubuntu-branches/ubuntu/precise/arj/precise-security

« back to all changes in this revision

Viewing changes to arjsec_h.c

  • Committer: Bazaar Package Importer
  • Author(s): Guillem Jover
  • Date: 2004-06-27 08:07:09 UTC
  • Revision ID: james.westby@ubuntu.com-20040627080709-1gkxm72ex66gkwe4
Tags: upstream-3.10.21
ImportĀ upstreamĀ versionĀ 3.10.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: arjsec_h.c,v 1.5 2003/05/07 18:55:51 andrew_belov Exp $
 
3
 * ---------------------------------------------------------------------------
 
4
 * The high-level ARJ-security envelope  verification routine is contained  in
 
5
 * this module.
 
6
 *
 
7
 */
 
8
 
 
9
#include "arj.h"
 
10
 
 
11
DEBUGHDR(__FILE__)                      /* Debug information block */
 
12
 
 
13
/* Returns ARJ-security signature and 0 when successful. */
 
14
 
 
15
#if SFX_LEVEL>=ARJSFX
 
16
int get_arjsec_signature(FILE *stream, long offset, char *signature, int iter)
 
17
{
 
18
#if SFX_LEVEL<=ARJSFXV&&!defined(COMMERCIAL)
 
19
 return(0);
 
20
#else
 
21
 unsigned char tail[ARJSEC_RECORD_SIZE];
 
22
 unsigned long tmp_tail[8];
 
23
 unsigned long block[8];                /* CRC accumulation */
 
24
 unsigned char *dest;
 
25
 int i;
 
26
 /* We need to retain the position when processing ARJSFX archives */
 
27
 #if SFX_LEVEL<=ARJSFXV
 
28
  unsigned long cur_pos;
 
29
  int c;
 
30
 #endif
 
31
 
 
32
 #if SFX_LEVEL<=ARJSFXV
 
33
  msg_cprintf(0, M_VERIFYING_ARJSEC);
 
34
  cur_pos=ftell(stream);
 
35
 #endif
 
36
 fseek(stream, offset, SEEK_SET);
 
37
 if(fread(tail, 1, sizeof(tail), stream)!=ARJSEC_RECORD_SIZE)
 
38
  return(1);
 
39
 #if SFX_LEVEL>=ARJ
 
40
  fseek(stream, 0L, SEEK_SET);
 
41
 #endif
 
42
 crc32term=CRC_MASK;
 
43
 crc32_for_block(tail, ARJSEC_RECORD_SIZE-4);
 
44
 if(crc32term!=mget_dword(&tail[ARJSEC_RECORD_SIZE-4]))
 
45
  return(1);
 
46
 dest=tail+40;
 
47
 for(i=0; i<76; i++)
 
48
  *(dest++)^=0x80|tail[8+i%32];
 
49
 memcpy(signature, tail+40, 76);
 
50
 /* The owner's name is already stored at this point, now just make sure that
 
51
    we have the envelope in its original, unmodified form. */
 
52
 #if SFX_LEVEL>=ARJ
 
53
  dest=tail+40;
 
54
  arjsec_newblock(block+4);
 
55
  while(*dest!='\0')
 
56
   arjsec_crcterm(block+4, *(dest++));
 
57
  arjsec_invert(block+4);
 
58
  arjsec_read(block, stream, offset);
 
59
  #ifdef WORDS_BIGENDIAN  
 
60
  for (i=0;i<sizeof(tmp_tail)>>2;i++)
 
61
   tmp_tail[i]=mget_dword(tail+8+(i<<2));
 
62
  #else
 
63
  memcpy(tmp_tail, tail+8, sizeof(tmp_tail));
 
64
  #endif
 
65
  arjsec_term(block+4, tmp_tail, iter);
 
66
  i=0;
 
67
  if(tmp_tail[0]!=block[0])
 
68
   i++;
 
69
  if(tmp_tail[1]!=block[1])
 
70
   i++;
 
71
  if(tmp_tail[2]!=block[2])
 
72
   i++;
 
73
  if(tmp_tail[3]!=block[3])
 
74
   i++;
 
75
  arjsec_invert(block);
 
76
  arjsec_xor(block, block+4);
 
77
  if(tmp_tail[4]!=block[0])
 
78
   i++;
 
79
  if(tmp_tail[5]!=block[1])
 
80
   i++;
 
81
  if(tmp_tail[6]!=block[2])
 
82
   i++;
 
83
  if(tmp_tail[7]!=block[3])
 
84
   i++;
 
85
 #else
 
86
  #ifdef WORDS_BIGENDIAN  
 
87
  for (i=0;i<sizeof(tmp_tail)>>2;i++)
 
88
   tmp_tail[i]=mget_dword(tail+8+(i<<2));
 
89
  #else
 
90
  memcpy(tmp_tail, tail+8, sizeof(tmp_tail));
 
91
  #endif
 
92
  rewind(stream);
 
93
  dest=signature;
 
94
  arjsec_newblock(block+4);
 
95
  while(*dest!='\0')
 
96
   arjsec_crcterm(block+4, *(dest++));
 
97
  arjsec_invert(block+4);
 
98
  arjsec_newblock(block);
 
99
  while(--offset>=0L&&(c=fgetc(stream))!=EOF)
 
100
   arjsec_crcterm(block, (char)c);
 
101
  arjsec_invert(block);
 
102
  arjsec_term(block+4, tmp_tail, iter);
 
103
  i=0;
 
104
  if(memcmp(tmp_tail, block, 16))
 
105
   i++;
 
106
  arjsec_invert(block);
 
107
  block[0]^=block[4];
 
108
  block[1]^=block[5];
 
109
  block[2]^=block[6];
 
110
  block[3]^=block[7];
 
111
  if(memcmp(tmp_tail+4, block, 16))
 
112
   i++;
 
113
  fseek(stream, cur_pos, SEEK_SET);
 
114
  if(i==0)
 
115
  {
 
116
   msg_cprintf(0, M_VALID_ENVELOPE);
 
117
   valid_envelope=1;
 
118
  }
 
119
 #endif
 
120
 return(i);                            /* Number of errors */
 
121
#endif
 
122
}
 
123
#endif
 
124
 
 
125
/* Verifies registration information */
 
126
 
 
127
#if SFX_LEVEL>=ARJ||defined(REARJ)||defined(ARJUTIL)
 
128
int verify_reg_name(char *key1, char *key2, char *name, char *validation)
 
129
{
 
130
 unsigned long encrypt_pad[8], sec_blk[8];
 
131
 int i;
 
132
 char c, j;
 
133
 
 
134
 #if defined(WORDS_BIGENDIAN)&&!defined(ARJUTIL)
 
135
 for (i=0;i<8;i++)
 
136
  encrypt_pad[i]=mget_dword(validation+(i<<2));
 
137
 #else
 
138
 memcpy(encrypt_pad, validation, 32);
 
139
 #endif
 
140
 arjsec_newblock(sec_blk+4);
 
141
 for(i=0; key1[i]!='\0'; i++)
 
142
  arjsec_crcterm(sec_blk+4, (unsigned char)toupper(key1[i]));
 
143
 arjsec_invert(sec_blk+4);
 
144
 arjsec_newblock(sec_blk);
 
145
 for(i=0; key2[i]!='\0'; i++)
 
146
  arjsec_crcterm(sec_blk, (unsigned char)toupper(key2[i]));
 
147
 j='\0';
 
148
 for(i=0; name[i]!='\0'; i++)
 
149
 {
 
150
  c=toupper(name[i]);
 
151
  if(c!=' '||j!=' ')
 
152
   arjsec_crcterm(sec_blk, c);
 
153
  j=c;
 
154
 }
 
155
 arjsec_invert(sec_blk);
 
156
 arjsec_term(sec_blk+4, encrypt_pad, ARJSEC_ITER);
 
157
 i=0;
 
158
 if(encrypt_pad[0]!=sec_blk[0])
 
159
  i++;
 
160
 if(encrypt_pad[1]!=sec_blk[1])
 
161
  i++;
 
162
 if(encrypt_pad[2]!=sec_blk[2])
 
163
  i++;
 
164
 if(encrypt_pad[3]!=sec_blk[3])
 
165
  i++;
 
166
 arjsec_invert(sec_blk);
 
167
 if((sec_blk[0]^sec_blk[4])!=encrypt_pad[4])
 
168
  i++;
 
169
 if((sec_blk[1]^sec_blk[5])!=encrypt_pad[5])
 
170
  i++;
 
171
 if((sec_blk[2]^sec_blk[6])!=encrypt_pad[6])
 
172
  i++;
 
173
 if((sec_blk[3]^sec_blk[7])!=encrypt_pad[7])
 
174
  i++;
 
175
 return(i);
 
176
}
 
177
#endif