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

Diff of /imapfilter/passwd.c

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

revision 1.7 by lefcha, Thu Jun 20 08:49:23 2002 UTC revision 1.8 by lefcha, Sat Jul 13 22:31:37 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 43  void get_password(char *passwd, size_t p Line 43  void get_password(char *passwd, size_t p
43   * Encrypt and Base64 encode passwords.  Append the MD5 checksum of the passwords   * Encrypt and Base64 encode passwords.  Append the MD5 checksum of the passwords
44   * before encrypting them.   * before encrypting them.
45   */   */
46  int encrypt_passwords(FILE *fd, account_t *accts[])  int encrypt_passwords(FILE * fd, account_t * accts[])
47  {  {
48      int i;          int i;
49      char *c;      char *c;
50      unsigned char iv[EVP_MAX_IV_LENGTH];      unsigned char iv[EVP_MAX_IV_LENGTH];
51      unsigned char *key;      unsigned char *key;
# Line 57  int encrypt_passwords(FILE *fd, account_ Line 57  int encrypt_passwords(FILE *fd, account_
57      EVP_CIPHER_CTX ctx;      EVP_CIPHER_CTX ctx;
58      EVP_MD_CTX mdctx;      EVP_MD_CTX mdctx;
59      EVP_ENCODE_CTX bctx;      EVP_ENCODE_CTX bctx;
60        
61      key = (unsigned char *) smalloc(EVP_MAX_KEY_LENGTH);      key = (unsigned char *)smalloc(EVP_MAX_KEY_LENGTH);
62    
63      srandom(time(NULL));      srandom(time(NULL));
64    
65        /* Initialization vector. */
66      c = ultostr(1 + random() % 100000000, 10);      c = ultostr(1 + random() % 100000000, 10);
67      memset(iv, '0', EVP_MAX_IV_LENGTH);      memset(iv, '0', EVP_MAX_IV_LENGTH);
68      memcpy(iv + 8 - strlen(c), c, min(8, strlen(c)));      memcpy(iv + 8 - strlen(c), c, min(8, strlen(c)));
   
69      fprintf(fd, "%.8s\n", iv);      fprintf(fd, "%.8s\n", iv);
70    
71      EVP_CIPHER_CTX_init(&ctx);      EVP_CIPHER_CTX_init(&ctx);
72    
73      EVP_BytesToKey(EVP_bf_cbc(), EVP_md5(), NULL, passphr, strlen(passphr), 1,      EVP_BytesToKey(EVP_bf_cbc(), EVP_md5(), NULL, passphr, strlen(passphr), 1,
74                     key, NULL);                     key, NULL);
75            
76      EVP_DigestInit(&mdctx, EVP_md5());      EVP_DigestInit(&mdctx, EVP_md5());
77      EVP_EncryptInit(&ctx, EVP_bf_cbc(), key, iv);      EVP_EncryptInit(&ctx, EVP_bf_cbc(), key, iv);
78      EVP_EncodeInit(&bctx);      EVP_EncodeInit(&bctx);
# Line 89  int encrypt_passwords(FILE *fd, account_ Line 89  int encrypt_passwords(FILE *fd, account_
89      }      }
90    
91      EVP_DigestFinal(&mdctx, mdv, &mdl);      EVP_DigestFinal(&mdctx, mdv, &mdl);
92        
93      xstrncpy(buf, ".\n", ENCRYPTION_BUF - 1);      xstrncpy(buf, ".\n", ENCRYPTION_BUF - 1);
94        
95        /* MD5 checksum of data. */
96      for (i = 0; i < mdl; i++)      for (i = 0; i < mdl; i++)
97          snprintf(2 + buf + i * 2, ENCRYPTION_BUF - i * 2, "%02x", mdv[i]);          snprintf(2 + buf + i * 2, ENCRYPTION_BUF - i * 2, "%02x", mdv[i]);
98        
99      EVP_EncryptUpdate(&ctx, ebuf, &ebufl, buf, strlen(buf));      EVP_EncryptUpdate(&ctx, ebuf, &ebufl, buf, strlen(buf));
100      EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);      EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);
101      fwrite(bbuf, sizeof(char), bbufl, fd);      fwrite(bbuf, sizeof(char), bbufl, fd);
# Line 102  int encrypt_passwords(FILE *fd, account_ Line 103  int encrypt_passwords(FILE *fd, account_
103      EVP_EncryptFinal(&ctx, ebuf, &ebufl);      EVP_EncryptFinal(&ctx, ebuf, &ebufl);
104      EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);      EVP_EncodeUpdate(&bctx, bbuf, &bbufl, ebuf, ebufl);
105      EVP_EncodeFinal(&bctx, bbuf, &bbufl);      EVP_EncodeFinal(&bctx, bbuf, &bbufl);
106        
107      fwrite(bbuf, sizeof(char), bbufl, fd);      fwrite(bbuf, sizeof(char), bbufl, fd);
108    
109      EVP_CIPHER_CTX_cleanup(&ctx);      EVP_CIPHER_CTX_cleanup(&ctx);
110        
111      return 0;      return 0;
112  }  }
113    
# Line 114  int encrypt_passwords(FILE *fd, account_ Line 115  int encrypt_passwords(FILE *fd, account_
115  /*  /*
116   * Decode (Base64) passwords, decrypt them and verify the MD5 checksum.   * Decode (Base64) passwords, decrypt them and verify the MD5 checksum.
117   */   */
118  int decrypt_passwords(unsigned char **buf, FILE *fd)  int decrypt_passwords(unsigned char **buf, FILE * fd)
119  {  {
120      int i, j = 1;      int i, j = 1;
121      unsigned char iv[EVP_MAX_IV_LENGTH];      unsigned char iv[EVP_MAX_IV_LENGTH];
# Line 128  int decrypt_passwords(unsigned char **bu Line 129  int decrypt_passwords(unsigned char **bu
129      EVP_CIPHER_CTX *ctx;      EVP_CIPHER_CTX *ctx;
130      EVP_MD_CTX mdctx;      EVP_MD_CTX mdctx;
131      EVP_ENCODE_CTX bctx;      EVP_ENCODE_CTX bctx;
132        
133      c = *buf = (unsigned char *) smalloc(DECRYPTION_BUF * sizeof(char));          c = *buf = (unsigned char *)smalloc(DECRYPTION_BUF * sizeof(char));
134      key = (unsigned char *) smalloc(EVP_MAX_KEY_LENGTH);      key = (unsigned char *)smalloc(EVP_MAX_KEY_LENGTH);
135      ctx = (EVP_CIPHER_CTX *) smalloc(sizeof(EVP_CIPHER_CTX));      ctx = (EVP_CIPHER_CTX *) smalloc(sizeof(EVP_CIPHER_CTX));
136        
137      fgets(bbuf, LINE_MAX, fd);      fgets(bbuf, LINE_MAX, fd);
138    
139      memcpy(iv, bbuf, EVP_MAX_IV_LENGTH);      memcpy(iv, bbuf, EVP_MAX_IV_LENGTH);
# Line 141  int decrypt_passwords(unsigned char **bu Line 142  int decrypt_passwords(unsigned char **bu
142    
143      EVP_BytesToKey(EVP_bf_cbc(), EVP_md5(), NULL, passphr, strlen(passphr),      EVP_BytesToKey(EVP_bf_cbc(), EVP_md5(), NULL, passphr, strlen(passphr),
144                     1, key, NULL);                     1, key, NULL);
145        
146      EVP_DecryptInit(ctx, EVP_bf_cbc(), key, iv);      EVP_DecryptInit(ctx, EVP_bf_cbc(), key, iv);
147      EVP_DecodeInit(&bctx);      EVP_DecodeInit(&bctx);
148    
# Line 155  int decrypt_passwords(unsigned char **bu Line 156  int decrypt_passwords(unsigned char **bu
156    
157          if (c - *buf > DECRYPTION_BUF * j - 64) {          if (c - *buf > DECRYPTION_BUF * j - 64) {
158              i = c - *buf;              i = c - *buf;
159              *buf = (char *) srealloc(*buf, DECRYPTION_BUF * ++j);              *buf = (char *)srealloc(*buf, DECRYPTION_BUF * ++j);
160              c = *buf + i;              c = *buf + i;
161              *c = 0;              *c = 0;
162          }          }
163      }      }
164        
165      EVP_DecodeFinal(&bctx, ebuf, &ebufl);      EVP_DecodeFinal(&bctx, ebuf, &ebufl);
166      if (!EVP_DecryptFinal(ctx, c, &bufl))      if (!EVP_DecryptFinal(ctx, c, &bufl))
167          goto fail;          goto fail;
# Line 168  int decrypt_passwords(unsigned char **bu Line 169  int decrypt_passwords(unsigned char **bu
169      c += bufl;      c += bufl;
170      *c = 0;      *c = 0;
171    
172        /* Calculate the MD5 checksum and check if it is correct. */
173      if (!(c = strstr(*buf, "\n.\n")))      if (!(c = strstr(*buf, "\n.\n")))
174          goto fail;          goto fail;
175    
176      EVP_DigestInit(&mdctx, EVP_md5());      EVP_DigestInit(&mdctx, EVP_md5());
177      EVP_DigestUpdate(&mdctx, *buf, c - *buf + 1);      EVP_DigestUpdate(&mdctx, *buf, c - *buf + 1);
178      EVP_DigestFinal(&mdctx, mdv, &mdl);      EVP_DigestFinal(&mdctx, mdv, &mdl);
179        
180      for (i = 0; i < mdl; i++)      for (i = 0; i < mdl; i++)
181          snprintf(mdc + i * 2, EVP_MAX_MD_SIZE * 2 + 1 - i * 2, "%02x", mdv[i]);          snprintf(mdc + i * 2, EVP_MAX_MD_SIZE * 2 + 1 - i * 2, "%02x", mdv[i]);
182        
183      c += 3;      c += 3;
184    
185      if (strncmp(c, mdc, 32))      if (strncmp(c, mdc, 32))
186          goto fail;          goto fail;
187    
188      EVP_CIPHER_CTX_cleanup(ctx);      EVP_CIPHER_CTX_cleanup(ctx);
189        
190      sfree(key);      sfree(key);
191      sfree(ctx);      sfree(ctx);
192        
193      return 0;      return 0;
194    
195  fail:  fail:
196      error("Wrong master passphrase.\n");      error("Wrong master passphrase.\n");
197      EVP_CIPHER_CTX_cleanup(ctx);          EVP_CIPHER_CTX_cleanup(ctx);
198      sfree(*buf);      sfree(*buf);
199      sfree(key);      sfree(key);
200      sfree(ctx);      sfree(ctx);
201        
202      return ERROR_DECRYPT;      return ERROR_DECRYPT;
203  }  }
204    
# Line 211  void password_editor(void) Line 213  void password_editor(void)
213      char *c;      char *c;
214      char *p[2];      char *p[2];
215      account_t *a, *accts[EDITOR_PASSWORDS_MAX];      account_t *a, *accts[EDITOR_PASSWORDS_MAX];
216        
217      if (!(flags & FLAG_BLANK_PASSWORD)) {      if (!(flags & FLAG_BLANK_PASSWORD)) {
218          error("imapfilter: no candidate passwords for encryption found\n");          error("imapfilter: no candidate passwords for encryption found\n");
219          return;          return;
220      }      }
       
221      q = 0;      q = 0;
222        
223      memset(accts, 0, EDITOR_PASSWORDS_MAX);      memset(accts, 0, EDITOR_PASSWORDS_MAX);
224    
225      for (i = 0, a = accounts; i < EDITOR_PASSWORDS_MAX - 1 && a; a = a->next) {      for (i = 0, a = accounts; i < EDITOR_PASSWORDS_MAX - 1 && a; a = a->next) {
# Line 264  void password_editor(void) Line 265  void password_editor(void)
265                      break;                      break;
266                  accts[n - 1]->password[0] = 0;                  accts[n - 1]->password[0] = 0;
267              } else if (*c == 'p') {              } else if (*c == 'p') {
268                  p[0] = (char *) smalloc(PASSPHRASE_LEN);                  p[0] = (char *)smalloc(PASSPHRASE_LEN);
269                  p[1] = (char *) smalloc(PASSPHRASE_LEN);                  p[1] = (char *)smalloc(PASSPHRASE_LEN);
270                  do {                  do {
271                      for (i = 0; i < 2; i++) {                      for (i = 0; i < 2; i++) {
272                          printf("Enter %snew master password: ",                          printf("Enter %snew master password: ",
# Line 286  void password_editor(void) Line 287  void password_editor(void)
287          }          }
288      } while (!q);      } while (!q);
289  }  }
290    
291  #endif  #endif

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26