Make PKCS#12 code handle missing passwords.
[openssl.git] / crypto / evp / p5_crpt2.c
index ef3f941a7e5f5c593ca38860a19ebad09ceca01e..717fad68ca8fa053a56d4102ceb70bb52f442b39 100644 (file)
@@ -55,6 +55,7 @@
  * Hudson (tjh@cryptsoft.com).
  *
  */
+#if !defined(NO_HMAC) && !defined(NO_SHA)
 #include <stdio.h>
 #include <stdlib.h>
 #include <openssl/x509.h>
 #include <openssl/hmac.h>
 #include "cryptlib.h"
 
+/* set this to print out info about the keygen algorithm */
+/* #define DEBUG_PKCS5V2 */
+
+#ifdef DEBUG_PKCS5V2
+       static void h__dump (const unsigned char *p, int len);
+#endif
+
 /* This is an implementation of PKCS#5 v2.0 password based encryption key
  * derivation function PBKDF2 using the only currently defined function HMAC
  * with SHA1. Verified against test vectors posted by Peter Gutmann
@@ -73,21 +81,23 @@ int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
                           int keylen, unsigned char *out)
 {
        unsigned char digtmp[SHA_DIGEST_LENGTH], *p, itmp[4];
-       int cplen, j, k;
+       int cplen, j, k, tkeylen;
        unsigned long i = 1;
        HMAC_CTX hctx;
        p = out;
-       if(passlen == -1) passlen = strlen(pass);
-       while(keylen) {
-               if(keylen > SHA_DIGEST_LENGTH) cplen = SHA_DIGEST_LENGTH;
-               else cplen = keylen;
+       tkeylen = keylen;
+       if(!pass) passlen = 0;
+       else if(passlen == -1) passlen = strlen(pass);
+       while(tkeylen) {
+               if(tkeylen > SHA_DIGEST_LENGTH) cplen = SHA_DIGEST_LENGTH;
+               else cplen = tkeylen;
                /* We are unlikely to ever use more than 256 blocks (5120 bits!)
                 * but just in case...
                 */
-               itmp[0] = (i >> 24) & 0xff;
-               itmp[1] = (i >> 16) & 0xff;
-               itmp[2] = (i >> 8) & 0xff;
-               itmp[3] = i & 0xff;
+               itmp[0] = (unsigned char)((i >> 24) & 0xff);
+               itmp[1] = (unsigned char)((i >> 16) & 0xff);
+               itmp[2] = (unsigned char)((i >> 8) & 0xff);
+               itmp[3] = (unsigned char)(i & 0xff);
                HMAC_Init(&hctx, pass, passlen, EVP_sha1());
                HMAC_Update(&hctx, salt, saltlen);
                HMAC_Update(&hctx, itmp, 4);
@@ -98,11 +108,20 @@ int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
                                 digtmp, SHA_DIGEST_LENGTH, digtmp, NULL);
                        for(k = 0; k < cplen; k++) p[k] ^= digtmp[k];
                }
-               keylen-= cplen;
+               tkeylen-= cplen;
                i++;
                p+= cplen;
        }
        HMAC_cleanup(&hctx);
+#ifdef DEBUG_PKCS5V2
+       fprintf(stderr, "Password:\n");
+       h__dump (pass, passlen);
+       fprintf(stderr, "Salt:\n");
+       h__dump (salt, saltlen);
+       fprintf(stderr, "Iteration count %d\n", iter);
+       fprintf(stderr, "Key:\n");
+       h__dump (out, keylen);
+#endif
        return 1;
 }
 
@@ -219,3 +238,11 @@ int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
        return 0;
 }
 
+#ifdef DEBUG_PKCS5V2
+static void h__dump (const unsigned char *p, int len)
+{
+        for (; len --; p++) fprintf(stderr, "%02X ", *p);
+        fprintf(stderr, "\n");
+}
+#endif
+#endif