Better error reporting through com_err
[mech_eap.orig] / unwrap_iov.c
index 4061bee..a5a2c2f 100644 (file)
@@ -73,14 +73,13 @@ unwrapToken(OM_uint32 *minor,
     gss_iov_buffer_t header;
     gss_iov_buffer_t padding;
     gss_iov_buffer_t trailer;
-    unsigned char acceptorFlag;
+    unsigned char flags;
     unsigned char *ptr = NULL;
     int keyUsage;
     size_t rrc, ec;
     size_t dataLen, assocDataLen;
     uint64_t seqnum;
     int valid = 0;
-    krb5_cksumtype cksumtype;
     int conf_flag = 0;
     krb5_context krbContext;
 
@@ -100,31 +99,27 @@ unwrapToken(OM_uint32 *minor,
 
     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
 
-    acceptorFlag = CTX_IS_INITIATOR(ctx) ? TOK_FLAG_SENDER_IS_ACCEPTOR : 0;
-    keyUsage = (toktype == TOK_TYPE_WRAP
-                ? (!CTX_IS_INITIATOR(ctx)
+    flags = rfc4121Flags(ctx, TRUE);
+
+    if (toktype == TOK_TYPE_WRAP) {
+        keyUsage = !CTX_IS_INITIATOR(ctx)
                    ? KEY_USAGE_INITIATOR_SEAL
-                   : KEY_USAGE_ACCEPTOR_SEAL)
-                : (!CTX_IS_INITIATOR(ctx)
+                   : KEY_USAGE_ACCEPTOR_SEAL;
+    } else {
+        keyUsage = !CTX_IS_INITIATOR(ctx)
                    ? KEY_USAGE_INITIATOR_SIGN
-                   : KEY_USAGE_ACCEPTOR_SIGN));
+                   : KEY_USAGE_ACCEPTOR_SIGN;
+    }
 
     gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
 
     ptr = (unsigned char *)header->buffer.value;
 
-    if (header->buffer.length < 16) {
-        *minor = 0;
+    if (header->buffer.length < 16)
         return GSS_S_DEFECTIVE_TOKEN;
-    }
 
-    if ((ptr[2] & TOK_FLAG_SENDER_IS_ACCEPTOR) != acceptorFlag) {
+    if ((ptr[2] & flags) != flags)
         return GSS_S_BAD_SIG;
-    }
-
-    if (ptr[2] & TOK_FLAG_ACCEPTOR_SUBKEY) {
-        return GSS_S_BAD_SIG;
-    }
 
     if (toktype == TOK_TYPE_WRAP) {
         unsigned int krbTrailerLen;
@@ -201,7 +196,7 @@ unwrapToken(OM_uint32 *minor,
             store_uint16_be(0, ptr + 4);
             store_uint16_be(0, ptr + 6);
 
-            code = gssEapVerify(krbContext, cksumtype, rrc,
+            code = gssEapVerify(krbContext, ctx->checksumType, rrc,
                                 &ctx->rfc3961Key, keyUsage,
                                 iov, iov_count, &valid);
             if (code != 0 || valid == FALSE) {
@@ -210,9 +205,9 @@ unwrapToken(OM_uint32 *minor,
             }
         }
 
-        code = sequenceCheck(&ctx->seqState, seqnum);
+        code = sequenceCheck(minor, &ctx->seqState, seqnum);
     } else if (toktype == TOK_TYPE_MIC) {
-        if (load_uint16_be(ptr) != TOK_TYPE_MIC)
+        if (load_uint16_be(ptr) != toktype)
             goto defective;
 
     verify_mic_1:
@@ -220,16 +215,16 @@ unwrapToken(OM_uint32 *minor,
             goto defective;
         seqnum = load_uint64_be(ptr + 8);
 
-        code = gssEapVerify(krbContext, cksumtype, 0,
+        code = gssEapVerify(krbContext, ctx->checksumType, 0,
                             &ctx->rfc3961Key, keyUsage,
                             iov, iov_count, &valid);
         if (code != 0 || valid == FALSE) {
             *minor = code;
             return GSS_S_BAD_SIG;
         }
-        code = sequenceCheck(&ctx->seqState, seqnum);
-    } else if (toktype == TOK_TYPE_DELETE) {
-        if (load_uint16_be(ptr) != TOK_TYPE_DELETE)
+        code = sequenceCheck(minor, &ctx->seqState, seqnum);
+    } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
+        if (load_uint16_be(ptr) != TOK_TYPE_DELETE_CONTEXT)
             goto defective;
         goto verify_mic_1;
     } else {
@@ -298,8 +293,8 @@ unwrapStream(OM_uint32 *minor,
 
     assert(toktype == TOK_TYPE_WRAP);
 
-    if (toktype != TOK_TYPE_WRAP || (ctx->gssFlags & GSS_C_DCE_STYLE)) {
-        code = EINVAL;
+    if (toktype != TOK_TYPE_WRAP) {
+        code = GSSEAP_WRONG_TOK_ID;
         goto cleanup;
     }
 
@@ -335,7 +330,7 @@ unwrapStream(OM_uint32 *minor,
         if (type == GSS_IOV_BUFFER_TYPE_DATA) {
             if (data != NULL) {
                 /* only a single DATA buffer can appear */
-                code = EINVAL;
+                code = GSSEAP_BAD_STREAM_IOV;
                 goto cleanup;
             }
 
@@ -349,7 +344,7 @@ unwrapStream(OM_uint32 *minor,
 
     if (data == NULL) {
         /* a single DATA buffer must be present */
-        code = EINVAL;
+        code = GSSEAP_BAD_STREAM_IOV;
         goto cleanup;
     }
 
@@ -403,7 +398,6 @@ unwrapStream(OM_uint32 *minor,
     }
 
     /* IOV: -----------0-------------+---1---+--2--+----------------3--------------*/
-    /* Old: GSS-Header | Conf        | Data  | Pad |                               */
     /* CFX: GSS-Header | Kerb-Header | Data  |     | EC | E(Header) | Kerb-Trailer */
     /* GSS: -------GSS-HEADER--------+-DATA--+-PAD-+----------GSS-TRAILER----------*/
 
@@ -411,7 +405,7 @@ unwrapStream(OM_uint32 *minor,
     if (stream->buffer.length < theader->buffer.length +
         tpadding->buffer.length +
         ttrailer->buffer.length) {
-        code = KRB5_BAD_MSIZE;
+        code = GSSEAP_WRONG_SIZE;
         major = GSS_S_DEFECTIVE_TOKEN;
         goto cleanup;
     }
@@ -468,8 +462,8 @@ gssEapUnwrapOrVerifyMIC(OM_uint32 *minor,
 {
     OM_uint32 major;
 
-    if (!CTX_IS_ESTABLISHED(ctx))
-        return GSS_S_NO_CONTEXT;
+    if (ctx->encryptionType == ENCTYPE_NULL)
+        return GSS_S_UNAVAILABLE;
 
     if (gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM) != NULL) {
         major = unwrapStream(minor, ctx, conf_state, qop_state,
@@ -490,6 +484,30 @@ gss_unwrap_iov(OM_uint32 *minor,
                gss_iov_buffer_desc *iov,
                int iov_count)
 {
-    return gssEapUnwrapOrVerifyMIC(minor, ctx, conf_state, qop_state,
-                                   iov, iov_count, TOK_TYPE_WRAP);
+    OM_uint32 major;
+
+    if (ctx == GSS_C_NO_CONTEXT) {
+        *minor = EINVAL;
+        return GSS_S_NO_CONTEXT;
+    }
+
+    *minor = 0;
+
+    GSSEAP_MUTEX_LOCK(&ctx->mutex);
+
+    if (!CTX_IS_ESTABLISHED(ctx)) {
+        *minor = GSSEAP_CONTEXT_INCOMPLETE;
+        major = GSS_S_NO_CONTEXT;
+        goto cleanup;
+    }
+
+    major = gssEapUnwrapOrVerifyMIC(minor, ctx, conf_state, qop_state,
+                                    iov, iov_count, TOK_TYPE_WRAP);
+    if (GSS_ERROR(major))
+        goto cleanup;
+
+cleanup:
+    GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
+
+    return major;
 }