Fix incorrect free() usage
[mod_auth_gssapi.git] / src / crypto.c
index 584bf16..1983a55 100644 (file)
@@ -57,14 +57,14 @@ apr_status_t SEAL_KEY_CREATE(apr_pool_t *p, struct seal_key **skey,
         memcpy(n->ekey, keys->value, keylen);
         memcpy(n->hkey, keys->value + keylen, keylen);
     } else {
-        ret = RAND_bytes(n->ekey, keylen);
-        if (ret == 0) {
+        ret = apr_generate_random_bytes(n->ekey, keylen);
+        if (ret != 0) {
             ret = EFAULT;
             goto done;
         }
 
-        ret = RAND_bytes(n->hkey, keylen);
-        if (ret == 0) {
+        ret = apr_generate_random_bytes(n->hkey, keylen);
+        if (ret != 0) {
             ret = EFAULT;
             goto done;
         }
@@ -72,24 +72,47 @@ apr_status_t SEAL_KEY_CREATE(apr_pool_t *p, struct seal_key **skey,
 
     ret = 0;
 done:
-    if (ret) {
-        free(n->ekey);
-        free(n->hkey);
-        free(n);
-    } else {
+    if (ret == 0) {
         *skey = n;
     }
     return ret;
 }
 
+apr_status_t HMAC_BUFFER(struct seal_key *skey, struct databuf *buffer,
+                         struct databuf *result)
+{
+    HMAC_CTX hmac_ctx = { 0 };
+    unsigned int len;
+    int ret;
+
+    /* now MAC the buffer */
+    HMAC_CTX_init(&hmac_ctx);
+
+    ret = HMAC_Init_ex(&hmac_ctx, skey->hkey,
+                       skey->cipher->key_len, skey->md, NULL);
+    if (ret == 0) goto done;
+
+    ret = HMAC_Update(&hmac_ctx, buffer->value, buffer->length);
+    if (ret == 0) goto done;
+
+    ret = HMAC_Final(&hmac_ctx, result->value, &len);
+
+done:
+    HMAC_CTX_cleanup(&hmac_ctx);
+    if (ret == 0) return EFAULT;
+
+    result->length = len;
+    return 0;
+}
+
 apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
                          struct databuf *plain, struct databuf *cipher)
 {
+    int blksz = skey->cipher->block_size;
     apr_status_t err = EFAULT;
     EVP_CIPHER_CTX ctx = { 0 };
-    HMAC_CTX hmac_ctx = { 0 };
-    uint8_t rbuf[16];
-    unsigned int len;
+    uint8_t rbuf[blksz];
+    struct databuf hmacbuf;
     int outlen, totlen;
     int ret;
 
@@ -97,12 +120,12 @@ apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
 
     /* confounder to avoid exposing random numbers directly to clients
      * as IVs */
-    ret = RAND_bytes(rbuf, 16);
-    if (ret == 0) goto done;
+    ret = apr_generate_random_bytes(rbuf, sizeof(rbuf));
+    if (ret != 0) goto done;
 
     if (cipher->length == 0) {
         /* add space for confounder and padding and MAC */
-        cipher->length = (plain->length / 16 + 2) * 16;
+        cipher->length = (plain->length / blksz + 2) * blksz;
         cipher->value = apr_palloc(p, cipher->length + skey->md->md_size);
         if (!cipher->value) {
             err = ENOMEM;
@@ -115,7 +138,7 @@ apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
     totlen = 0;
 
     outlen = cipher->length;
-    ret = EVP_EncryptUpdate(&ctx, cipher->value, &outlen, rbuf, 16);
+    ret = EVP_EncryptUpdate(&ctx, cipher->value, &outlen, rbuf, sizeof(rbuf));
     if (ret == 0) goto done;
     totlen += outlen;
 
@@ -131,24 +154,16 @@ apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
     totlen += outlen;
 
     /* now MAC the buffer */
-    HMAC_CTX_init(&hmac_ctx);
-
-    ret = HMAC_Init_ex(&hmac_ctx, skey->hkey,
-                       skey->cipher->key_len, skey->md, NULL);
-    if (ret == 0) goto done;
-
-    ret = HMAC_Update(&hmac_ctx, cipher->value, totlen);
-    if (ret == 0) goto done;
-
-    ret = HMAC_Final(&hmac_ctx, &cipher->value[totlen], &len);
-    if (ret == 0) goto done;
+    cipher->length = totlen;
+    hmacbuf.value = &cipher->value[totlen];
+    ret = HMAC_BUFFER(skey, cipher, &hmacbuf);
+    if (ret != 0) goto done;
 
-    cipher->length = totlen + len;
+    cipher->length += hmacbuf.length;
     err = 0;
 
 done:
     EVP_CIPHER_CTX_cleanup(&ctx);
-    HMAC_CTX_cleanup(&hmac_ctx);
     return err;
 }
 
@@ -157,29 +172,19 @@ apr_status_t UNSEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
 {
     apr_status_t err = EFAULT;
     EVP_CIPHER_CTX ctx = { 0 };
-    HMAC_CTX hmac_ctx = { 0 };
     unsigned char mac[skey->md->md_size];
-    unsigned int len;
+    struct databuf hmacbuf;
     int outlen, totlen;
     volatile bool equal = true;
     int ret, i;
 
     /* check MAC first */
-    HMAC_CTX_init(&hmac_ctx);
-
-    ret = HMAC_Init_ex(&hmac_ctx, skey->hkey,
-                       skey->cipher->key_len, skey->md, NULL);
-    if (ret == 0) goto done;
-
     cipher->length -= skey->md->md_size;
+    hmacbuf.value = mac;
+    ret = HMAC_BUFFER(skey, cipher, &hmacbuf);
+    if (ret != 0) goto done;
 
-    ret = HMAC_Update(&hmac_ctx, cipher->value, cipher->length);
-    if (ret == 0) goto done;
-
-    ret = HMAC_Final(&hmac_ctx, mac, &len);
-    if (ret == 0) goto done;
-
-    if (len != skey->md->md_size) goto done;
+    if (hmacbuf.length != skey->md->md_size) goto done;
     for (i = 0; i < skey->md->md_size; i++) {
         if (cipher->value[cipher->length + i] != mac[i]) equal = false;
         /* not breaking intentionally,
@@ -214,14 +219,22 @@ apr_status_t UNSEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
 
     totlen += outlen;
     /* now remove the confounder */
-    totlen -= 16;
-    memmove(plain->value, plain->value + 16, totlen);
+    totlen -= skey->cipher->block_size;
+    memmove(plain->value, plain->value + skey->cipher->block_size, totlen);
 
     plain->length = totlen;
     err = 0;
 
 done:
     EVP_CIPHER_CTX_cleanup(&ctx);
-    HMAC_CTX_cleanup(&hmac_ctx);
     return err;
 }
+
+int get_mac_size(struct seal_key *skey)
+{
+    if (skey) {
+        return skey->md->md_size;
+    } else {
+        return 0;
+    }
+}