Пример #1
0
/*
 * Curl_sasl_create_cram_md5_message()
 *
 * This is used to generate an already encoded CRAM-MD5 response message ready
 * for sending to the recipient.
 *
 * Parameters:
 *
 * data    [in]     - The session handle.
 * chlg64  [in]     - Pointer to the base64 encoded challenge buffer.
 * userp   [in]     - The user name.
 * passdwp [in]     - The user's password.
 * outptr  [in/out] - The address where a pointer to newly allocated memory
 *                    holding the result will be stored upon completion.
 * outlen  [out]    - The length of the output message.
 *
 * Returns CURLE_OK on success.
 */
CURLcode Curl_sasl_create_cram_md5_message(struct SessionHandle *data,
                                           const char* chlg64,
                                           const char* userp,
                                           const char* passwdp,
                                           char **outptr, size_t *outlen)
{
  CURLcode result = CURLE_OK;
  size_t chlg64len = strlen(chlg64);
  unsigned char *chlg = (unsigned char *) NULL;
  size_t chlglen = 0;
  HMAC_context *ctxt;
  unsigned char digest[MD5_DIGEST_LEN];
  char response[MAX_CURL_USER_LENGTH + 2 * MD5_DIGEST_LEN + 1];

  /* Decode the challenge if necessary */
  if(chlg64len && *chlg64 != '=') {
    result = Curl_base64_decode(chlg64, &chlg, &chlglen);

    if(result)
      return result;
  }

  /* Compute the digest using the password as the key */
  ctxt = Curl_HMAC_init(Curl_HMAC_MD5,
                        (const unsigned char *) passwdp,
                        curlx_uztoui(strlen(passwdp)));

  if(!ctxt) {
    Curl_safefree(chlg);
    return CURLE_OUT_OF_MEMORY;
  }

  /* Update the digest with the given challenge */
  if(chlglen > 0)
    Curl_HMAC_update(ctxt, chlg, curlx_uztoui(chlglen));

  Curl_safefree(chlg);

  /* Finalise the digest */
  Curl_HMAC_final(ctxt, digest);

  /* Prepare the response */
  snprintf(response, sizeof(response),
      "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
           userp, digest[0], digest[1], digest[2], digest[3], digest[4],
           digest[5], digest[6], digest[7], digest[8], digest[9], digest[10],
           digest[11], digest[12], digest[13], digest[14], digest[15]);

  /* Base64 encode the reply */
  return Curl_base64_encode(data, response, 0, outptr, outlen);
}
Пример #2
0
/*
 * sasl_create_cram_md5_message()
 *
 * This is used to generate an already encoded CRAM-MD5 response message ready
 * for sending to the recipient.
 *
 * Parameters:
 *
 * data    [in]     - The session handle.
 * chlg    [in]     - The challenge.
 * userp   [in]     - The user name.
 * passdwp [in]     - The user's password.
 * outptr  [in/out] - The address where a pointer to newly allocated memory
 *                    holding the result will be stored upon completion.
 * outlen  [out]    - The length of the output message.
 *
 * Returns CURLE_OK on success.
 */
CURLcode sasl_create_cram_md5_message(struct SessionHandle *data,
                                      const char *chlg,
                                      const char *userp,
                                      const char *passwdp,
                                      char **outptr, size_t *outlen)
{
  CURLcode result = CURLE_OK;
  size_t chlglen = 0;
  HMAC_context *ctxt;
  unsigned char digest[MD5_DIGEST_LEN];
  char *response;

  if(chlg)
    chlglen = strlen(chlg);

  /* Compute the digest using the password as the key */
  ctxt = Curl_HMAC_init(Curl_HMAC_MD5,
                        (const unsigned char *) passwdp,
                        curlx_uztoui(strlen(passwdp)));
  if(!ctxt)
    return CURLE_OUT_OF_MEMORY;

  /* Update the digest with the given challenge */
  if(chlglen > 0)
    Curl_HMAC_update(ctxt, (const unsigned char *) chlg,
                     curlx_uztoui(chlglen));

  /* Finalise the digest */
  Curl_HMAC_final(ctxt, digest);

  /* Generate the response */
  response = aprintf(
    "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
    userp, digest[0], digest[1], digest[2], digest[3], digest[4],
    digest[5], digest[6], digest[7], digest[8], digest[9], digest[10],
    digest[11], digest[12], digest[13], digest[14], digest[15]);
  if(!response)
    return CURLE_OUT_OF_MEMORY;

  /* Base64 encode the response */
  result = Curl_base64_encode(data, response, 0, outptr, outlen);

  free(response);

  return result;
}
Пример #3
0
/* for AUTH CRAM-MD5 responses. */
static CURLcode smtp_state_authcram_resp(struct connectdata *conn,
        int smtpcode,
        smtpstate instate)
{
    CURLcode result = CURLE_OK;
    struct SessionHandle *data = conn->data;
    char * chlg64 = data->state.buffer;
    unsigned char * chlg;
    size_t chlglen;
    size_t l;
    char * rplyb64;
    HMAC_context * ctxt;
    unsigned char digest[16];
    char reply[MAX_CURL_USER_LENGTH + 32 /* 2 * size of MD5 digest */ + 1];

    (void)instate; /* no use for this yet */

    if(smtpcode != 334) {
        failf(data, "Access denied: %d", smtpcode);
        return CURLE_LOGIN_DENIED;
    }

    /* Get the challenge. */
    for (chlg64 += 4; *chlg64 == ' ' || *chlg64 == '\t'; chlg64++)
        ;

    chlg = (unsigned char *) NULL;
    chlglen = 0;

    if(*chlg64 != '=') {
        for (l = strlen(chlg64); l--;)
            if(chlg64[l] != '\r' && chlg64[l] != '\n' && chlg64[l] != ' ' &&
                    chlg64[l] != '\t')
                break;

        if(++l) {
            chlg64[l] = '\0';

            if(!(chlglen = Curl_base64_decode(chlg64, &chlg)))
                return CURLE_OUT_OF_MEMORY;
        }
    }

    /* Compute digest. */
    ctxt = Curl_HMAC_init(Curl_HMAC_MD5,
                          (const unsigned char *) conn->passwd,
                          (unsigned int)(strlen(conn->passwd)));

    if(!ctxt) {
        if(chlg)
            free(chlg);

        return CURLE_OUT_OF_MEMORY;
    }

    if(chlglen > 0)
        Curl_HMAC_update(ctxt, chlg, (unsigned int)(chlglen));

    if(chlg)
        free(chlg);

    Curl_HMAC_final(ctxt, digest);

    /* Prepare the reply. */
    snprintf(reply, sizeof reply,
             "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
             conn->user, digest[0], digest[1], digest[2], digest[3], digest[4], digest[5],
             digest[6], digest[7], digest[8], digest[9], digest[10], digest[11],
             digest[12], digest[13], digest[14], digest[15]);

    /* Encode it to base64 and send it. */
    l = Curl_base64_encode(data, reply, 0, &rplyb64);

    if(!l)
        result = CURLE_OUT_OF_MEMORY;
    else {
        result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", rplyb64);
        free(rplyb64);

        if(!result)
            state(conn, SMTP_AUTH);
    }

    return result;
}
Пример #4
0
/*
 * Output an HTTP MAC Authorization header.
 */
CURLcode Curl_output_mac(struct connectdata *conn,
                         bool proxy,
                         const unsigned char *request,
                         const unsigned char *uripath,
                         struct curl_oauth2_token *token)
{
  /* Please refer to draft-ietf-oauth-v2-http-mac for all the juicy
     details about HTTP MAC construction. */

  struct timeval now;
  char ts[12];
  char nonce[33];
  long nonceval = 0;
  size_t noncesz = 0;
  char *nreq = NULL;
  char **allocuserpwd;
  struct auth *authp;
  struct SessionHandle *data = conn->data;
  const char *hosthdr = NULL, *hosthdrp1 = NULL, *hosthdrp2 = NULL;
  char *hostname = NULL;
  unsigned long port = 0;
  const char *ext = data->set.str[STRING_HTTP_MAC_EXT];
  bool extprovided = (ext != 0);
  char *extinfo;
  const HMAC_params *params;
  HMAC_context *ctxt;
  char digest[32];            /* The max of result_len is enough. */
  char *mac = NULL;
  size_t macsz = 0;
  CURLcode rc;
/* The CURL_OUTPUT_MAC_CONV macro below is for non-ASCII machines.
   It converts digest text to ASCII so the MAC will be correct for
   what ultimately goes over the network.
*/
#define CURL_OUTPUT_MAC_CONV(a, b) \
  rc = Curl_convert_to_network(a, (char *)b, strlen((const char*)b)); \
  if(rc != CURLE_OK) { \
    free(b); \
    goto cleanup; \
  }

  /* Check that we have the proper kind of token. */

  if(token->token_type != CURL_OAUTH2_TOKEN_TYPE_MAC) {
    return CURLE_OAUTH2_TOKEN_MALFORMAT;
  }

  /* Select the right Authorization field to fill in depending on
     whether we're talking to a proxy or the remote host. */

  if(proxy) {
    allocuserpwd = &conn->allocptr.proxyuserpwd;
    authp = &data->state.authproxy;
  }
  else {
    allocuserpwd = &conn->allocptr.userpwd;
    authp = &data->state.authhost;
  }

  if(*allocuserpwd) {
    Curl_safefree(*allocuserpwd);
    *allocuserpwd = NULL;
  }

  authp->done = TRUE;

  /* Generate a timestamp from a monotically increasing source whose
     origin does not change. */

  now = curlx_tvgettimeofday();
#ifdef DELTA_EPOCH_IN_SECS
  now.tv_sec -= DELTA_EPOCH_IN_SECS;
#endif
  snprintf(ts, sizeof(ts) - 1, "%ld", (long)now.tv_sec);
  ts[sizeof(ts) - 1] = '\0';

  /* Generate a nonce that is unique for that timestamp */

  nonceval = (long)now.tv_sec + now.tv_usec;
  for(noncesz = 0; nonceval && noncesz < sizeof(nonce) - 1; ++noncesz) {
    int base = "\x08\x10\x0a\x1a"[noncesz % 4];
    nonce[noncesz] = "0123456789abcdefghijklmnopqrstuvwxyz"[nonceval % base];
    nonceval /= base;
  }
  nonce[noncesz] = '\0';

  /* Find hostname and port in headers, do not use the connection data. */

  hosthdr = conn->allocptr.host;
  if(!hosthdr) {
    hosthdr = Curl_checkheaders(data, "Host:");
  }
  if(!hosthdr) {
    rc = CURLE_HTTP_MAC_INVALID_HOST;
    goto cleanup;
  }

  /* Copy the hostname port of the Host header. */

  for(hosthdrp1 = hosthdr + 5; *hosthdrp1 && ISSPACE(*hosthdrp1); ++hosthdrp1);
  for(hosthdrp2 = hosthdrp1; *hosthdrp2 && *hosthdrp2 != ':'
        && !ISSPACE(*hosthdrp2); ++hosthdrp2);
  if(hosthdrp2 - hosthdrp1 == 0) {
    rc = CURLE_HTTP_MAC_INVALID_HOST;
    goto cleanup;
  }
  hostname = calloc(1, hosthdrp2 - hosthdrp1 + 1);
  if(!hostname) {
    rc = CURLE_OUT_OF_MEMORY;
    goto cleanup;
  }
  strncpy(hostname, hosthdrp1, hosthdrp2 - hosthdrp1);

  /* Check for a port in the Host header, and if we find it makes sure that
     it is well formed. If not, take the connection's port. */

  for(hosthdrp1 = hosthdrp2 = (hosthdrp2 + (*hosthdrp2 ? 1 : 0));
       *hosthdrp2 && ISDIGIT(*hosthdrp2); ++hosthdrp2);
  if(hosthdrp2 - hosthdrp1) {
    char *rest;
    port = strtoul(hosthdrp1, &rest, 10);  /* Must be decimal */
    if(rest != (hosthdrp1 + 1) && !*rest) {
      if(port > 0xffff) {   /* Single unix standard says port numbers are
                              * 16 bits long */
        rc = CURLE_HTTP_MAC_INVALID_HOST;
        goto cleanup;
      }
    }
  }
  else {
    port = conn->remote_port;
  }

  /* Any non-space character found in the Host field after the hostname
     and port makes that header invalid. */

  for(; *hosthdrp2 && ISSPACE(*hosthdrp2); ++hosthdrp2);
  if(*hosthdrp2) {
    rc = CURLE_HTTP_MAC_INVALID_HOST;
    goto cleanup;
  }

  /* Now generate the normalized request. See the I-D for details of the
     fields that get into there. All fields are separated by a newline
     defined as %0a (ASCII 10). If an "ext" field was not provided,
     include an empty one. */

  if(!ext) {
    ext = "";
  }
  nreq = aprintf("%s\x0a%s\x0a%s\x0a%s\x0a%s\x0a%lu\x0a%s\x0a",
                 ts,
                 nonce,
                 request,
                 uripath,
                 hostname,
                 port,
                 ext);
  if(!nreq) {
    rc = CURLE_OUT_OF_MEMORY;
    goto cleanup;
  }
  CURL_OUTPUT_MAC_CONV(data, nreq);

  /* Pick appropriate parameters to run the HMAC on the normalized
     request. */

  switch (token->mac_token.mac_algo) {
  case CURL_OAUTH2_MAC_ALGO_HMAC_SHA1:
    params = Curl_HMAC_SHA1;
    break;
  case CURL_OAUTH2_MAC_ALGO_HMAC_SHA256:
    params = Curl_HMAC_SHA256;
    break;
  default:
    rc = CURLE_OAUTH2_TOKEN_MALFORMAT;
    goto cleanup;
  }

  /* Compute the MAC using the MAC token key. */

  ctxt = Curl_HMAC_init(params,
                        (const unsigned char *)token->mac_token.mac_key,
                        curlx_uztoui(strlen(token->mac_token.mac_key)));
  if(!ctxt) {
    rc = CURLE_OUT_OF_MEMORY;
    goto cleanup;
  }

  /* Update the MAC with the normalized request. */

  Curl_HMAC_update(ctxt, (const unsigned char *)nreq,
                   curlx_uztoui(strlen(nreq)));

  /* Finalise the MAC */
  Curl_HMAC_final(ctxt, (unsigned char *)digest);

  /* Base64-encode the mac to produce the request MAC. */

  rc = Curl_base64_encode(data, digest, (*params).hmac_resultlen,
                            &mac, &macsz);
  if(rc)
    goto cleanup;

  /* Produce the Authorization header. The header contains the computed
     MAC, of course, but also all the other information needed by a server
     to run HMAC on the same normalized request. The server uses the same
     MAC secret and algorithm since the client and server both have a copy
     of the MAC token (identified here by the "id" field).

     We omit the "ext" field if it was not given to us. We always put it
     there if it was provided, even if it's empty. */

  if(extprovided) {
    extinfo = aprintf("ext=\"%s\", ", ext);
  }
  else {
    extinfo = "";
  }

  *allocuserpwd =
    aprintf( "Authorization: MAC "
             "id=\"%s\", "
             "ts=\"%s\", "
             "nonce=\"%s\", "
             "%s"
             "mac=\"%s\"\n", token->access_token, ts, nonce, extinfo, mac);

  if(!*allocuserpwd) {
    rc = CURLE_OUT_OF_MEMORY;
    goto cleanup;
  }
  CURL_OUTPUT_MAC_CONV(data, allocuserpwd);

  rc = CURLE_OK;

  /* Cleanup allocated memory. */

  cleanup:
  if(extprovided) Curl_safefree(extinfo);
  Curl_safefree(mac);
  Curl_safefree(hostname);
  Curl_safefree(nreq);

  return rc;
}