/[imapfilter]/imapfilter/passwd.c
ViewVC logotype

Diff of /imapfilter/passwd.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by lefcha, Thu Jan 31 17:06:38 2002 UTC revision 1.6 by lefcha, Tue Jun 18 21:21:09 2002 UTC
# Line 27  void get_password(char *passwd, size_t p Line 27  void get_password(char *passwd, size_t p
27      char *c;      char *c;
28    
29      tty_disable_echo();      tty_disable_echo();
30        
31      if (fgets(passwd, pwlen, stdin))      if (fgets(passwd, pwlen, stdin))
32          if ((c = strchr(passwd, '\n')))          if ((c = strchr(passwd, '\n')))
33              *c = 0;              *c = 0;
# Line 84  int encrypt_passwords(FILE *fd, account_ Line 84  int encrypt_passwords(FILE *fd, account_
84          EVP_DigestUpdate(&mdctx, buf, strlen(buf));          EVP_DigestUpdate(&mdctx, buf, strlen(buf));
85          EVP_EncryptUpdate(&ctx, ebuf, &ebufl, buf, strlen(buf));          EVP_EncryptUpdate(&ctx, ebuf, &ebufl, buf, strlen(buf));
86          EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);          EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);
87            
88          fwrite(bbuf, sizeof(char), bbufl, fd);          fwrite(bbuf, sizeof(char), bbufl, fd);
89      }      }
90    
# Line 98  int encrypt_passwords(FILE *fd, account_ Line 98  int encrypt_passwords(FILE *fd, account_
98      EVP_EncryptUpdate(&ctx, ebuf, &ebufl, buf, strlen(buf));      EVP_EncryptUpdate(&ctx, ebuf, &ebufl, buf, strlen(buf));
99      EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);      EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);
100      fwrite(bbuf, sizeof(char), bbufl, fd);      fwrite(bbuf, sizeof(char), bbufl, fd);
101        
102      EVP_EncryptFinal(&ctx, ebuf, &ebufl);      EVP_EncryptFinal(&ctx, ebuf, &ebufl);
103      EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);      EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);
104      EVP_EncodeFinal(&bctx, bbuf, &bbufl);      EVP_EncodeFinal(&bctx, bbuf, &bbufl);
# Line 128  int decrypt_passwords(unsigned char **bu Line 128  int decrypt_passwords(unsigned char **bu
128      EVP_CIPHER_CTX *ctx;      EVP_CIPHER_CTX *ctx;
129      EVP_MD_CTX mdctx;      EVP_MD_CTX mdctx;
130      EVP_ENCODE_CTX bctx;      EVP_ENCODE_CTX bctx;
131        
132      c = *buf = (unsigned char *) smalloc(DECRYPTION_BUF * sizeof(char));          c = *buf = (unsigned char *) smalloc(DECRYPTION_BUF * sizeof(char));    
133      key = (unsigned char *) smalloc(EVP_MAX_KEY_LENGTH);      key = (unsigned char *) smalloc(EVP_MAX_KEY_LENGTH);
134      ctx = (EVP_CIPHER_CTX *) smalloc(sizeof(EVP_CIPHER_CTX));      ctx = (EVP_CIPHER_CTX *) smalloc(sizeof(EVP_CIPHER_CTX));
# Line 136  int decrypt_passwords(unsigned char **bu Line 136  int decrypt_passwords(unsigned char **bu
136      fgets(bbuf, LINE_MAX, fd);      fgets(bbuf, LINE_MAX, fd);
137    
138      memcpy(iv, bbuf, EVP_MAX_IV_LENGTH);      memcpy(iv, bbuf, EVP_MAX_IV_LENGTH);
139        
140      EVP_CIPHER_CTX_init(ctx);      EVP_CIPHER_CTX_init(ctx);
141    
142      EVP_BytesToKey(EVP_bf_cbc(), EVP_md5(), NULL, passphr, strlen(passphr),      EVP_BytesToKey(EVP_bf_cbc(), EVP_md5(), NULL, passphr, strlen(passphr),
143                     1, key, NULL);                     1, key, NULL);
144        
145      EVP_DecryptInit(ctx, EVP_bf_cbc(), key, iv);      EVP_DecryptInit(ctx, EVP_bf_cbc(), key, iv);
146      EVP_DecodeInit(&bctx);      EVP_DecodeInit(&bctx);
147    
148      while (fgets(bbuf, LINE_MAX, fd)) {      while (fgets(bbuf, LINE_MAX, fd)) {
149          EVP_DecodeUpdate(&bctx, ebuf, &ebufl, bbuf, strlen(bbuf));          EVP_DecodeUpdate(&bctx, ebuf, &ebufl, bbuf, strlen(bbuf));
150          if (!EVP_DecryptUpdate(ctx, c, &bufl, ebuf, ebufl)) {          if (!EVP_DecryptUpdate(ctx, c, &bufl, ebuf, ebufl))
151              error("Wrong master passphrase.\n");              goto fail;
152              sfree(*buf);  
             sfree(key);  
             sfree(ctx);  
             return 1;  
         }  
           
153          c += bufl;          c += bufl;
154          *c = 0;          *c = 0;
           
         if (c - *buf < DECRYPTION_BUF * j - 64)  
             *buf = (char *) srealloc(*buf, DECRYPTION_BUF * ++j);  
     }  
155    
156      EVP_DecodeFinal(&bctx, ebuf, &ebufl);          if (c - *buf > DECRYPTION_BUF * j - 64) {
157      if (!EVP_DecryptFinal(ctx, c, &bufl)) {              i = c - *buf;
158          fprintf(stderr, "Wrong master passphrase.\n");              *buf = (char *) srealloc(*buf, DECRYPTION_BUF * ++j);
159          sfree(*buf);              c = *buf + i;
160          sfree(key);              *c = 0;
161          sfree(ctx);          }
         return 1;  
162      }      }
163            
164        EVP_DecodeFinal(&bctx, ebuf, &ebufl);
165        if (!EVP_DecryptFinal(ctx, c, &bufl))
166            goto fail;
167    
168      c += bufl;      c += bufl;
169      *c = 0;      *c = 0;
170    
171        if (!(c = strstr(*buf, "\n.\n")))
172            goto fail;
173    
174        EVP_DigestInit(&mdctx, EVP_md5());
175        EVP_DigestUpdate(&mdctx, *buf, c - *buf + 1);
176        EVP_DigestFinal(&mdctx, mdv, &mdl);
177            
178      if ((c = strstr(*buf, "\n.\n"))) {      for (i = 0; i < mdl; i++)
179          EVP_DigestInit(&mdctx, EVP_md5());          snprintf(mdc + i * 2, EVP_MAX_MD_SIZE * 2 + 1 - i * 2, "%02x", mdv[i]);
180          EVP_DigestUpdate(&mdctx, *buf, c - *buf + 1);      
181          EVP_DigestFinal(&mdctx, mdv, &mdl);      c += 3;
182            
183          for (i = 0; i < mdl; i++)      if (strncmp(c, mdc, 32))
184              snprintf(mdc + i * 2, EVP_MAX_MD_SIZE * 2 + 1 - i * 2, "%02x", mdv[i]);          goto fail;
           
         *c = 0;  
     } else {  
         sfree(*buf);  
         sfree(key);  
         sfree(ctx);  
         return 1;  
     }  
185    
186      EVP_CIPHER_CTX_cleanup(ctx);      EVP_CIPHER_CTX_cleanup(ctx);
187            
# Line 196  int decrypt_passwords(unsigned char **bu Line 189  int decrypt_passwords(unsigned char **bu
189      sfree(ctx);      sfree(ctx);
190            
191      return 0;      return 0;
192    
193    fail:
194        error("Wrong master passphrase.\n");
195        sfree(*buf);
196        sfree(key);
197        sfree(ctx);
198        
199        return ERROR_DECRYPT;
200  }  }
201    
202    
# Line 239  void password_editor(void) Line 240  void password_editor(void)
240                         "l       list entries\n"                         "l       list entries\n"
241                         "p       change master password\n"                         "p       change master password\n"
242                         "q       quit without saving\n"                         "q       quit without saving\n"
243                           "s       save changes\n"
244                         "x       save and exit\n");                         "x       save and exit\n");
245              else if (*c == 'q')              else if (*c == 'q')
246                  q = 1;                  q = 1;
# Line 273  void password_editor(void) Line 275  void password_editor(void)
275                  xstrncpy(passphr, p[0], PASSPHRASE_LEN - 1);                  xstrncpy(passphr, p[0], PASSPHRASE_LEN - 1);
276                  sfree(p[0]);                  sfree(p[0]);
277                  sfree(p[1]);                  sfree(p[1]);
278                } else if (*c == 's') {
279                    store_passwords(accts);
280              } else if (*c == 'x') {              } else if (*c == 'x') {
281                  store_passwords(accts);                  store_passwords(accts);
282                  q = 1;                  q = 1;

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26