Exemplo n.º 1
0
static ssh_buffer privatekey_string_to_buffer(const char *pkey, int type,
        ssh_auth_callback cb, void *userdata, const char *desc) {
    ssh_buffer buffer = NULL;
    ssh_buffer out = NULL;
    const char *p;
    unsigned char *iv = NULL;
    const char *header_begin;
    const char *header_end;
    unsigned int header_begin_size;
    unsigned int header_end_size;
    unsigned int key_len = 0;
    unsigned int iv_len = 0;
    int algo = 0;
    int mode = 0;
    int len;

    buffer = ssh_buffer_new();
    if (buffer == NULL) {
        return NULL;
    }

    switch(type) {
    case SSH_KEYTYPE_DSS:
        header_begin = DSA_HEADER_BEGIN;
        header_end = DSA_HEADER_END;
        break;
    case SSH_KEYTYPE_RSA:
        header_begin = RSA_HEADER_BEGIN;
        header_end = RSA_HEADER_END;
        break;
    default:
        ssh_buffer_free(buffer);
        return NULL;
    }

    header_begin_size = strlen(header_begin);
    header_end_size = strlen(header_end);

    p = pkey;
    len = 0;
    get_next_line(p, len);

    while(len > 0 && strncmp(p, header_begin, header_begin_size)) {
        /* skip line */
        get_next_line(p, len);
    }
    if(len < 0) {
        /* no header found */
        return NULL;
    }
    /* skip header line */
    get_next_line(p, len);

    if (len > 11 && strncmp("Proc-Type: 4,ENCRYPTED", p, 11) == 0) {
        /* skip line */
        get_next_line(p, len);

        if (len > 10 && strncmp("DEK-Info: ", p, 10) == 0) {
            p += 10;
            len = 0;
            get_next_line(p, len);
            if (privatekey_dek_header(p, len, &algo, &mode, &key_len,
                                      &iv, &iv_len) < 0) {
                ssh_buffer_free(buffer);
                SAFE_FREE(iv);
                return NULL;
            }
        } else {
            ssh_buffer_free(buffer);
            SAFE_FREE(iv);
            return NULL;
        }
    } else {
        if(len > 0) {
            if (ssh_buffer_add_data(buffer, p, len) < 0) {
                ssh_buffer_free(buffer);
                SAFE_FREE(iv);
                return NULL;
            }
        }
    }

    get_next_line(p, len);
    while(len > 0 && strncmp(p, header_end, header_end_size) != 0) {
        if (ssh_buffer_add_data(buffer, p, len) < 0) {
            ssh_buffer_free(buffer);
            SAFE_FREE(iv);
            return NULL;
        }
        get_next_line(p, len);
    }

    if (len == -1 || strncmp(p, header_end, header_end_size) != 0) {
        ssh_buffer_free(buffer);
        SAFE_FREE(iv);
        return NULL;
    }

    if (ssh_buffer_add_data(buffer, "\0", 1) < 0) {
        ssh_buffer_free(buffer);
        SAFE_FREE(iv);
        return NULL;
    }

    out = base64_to_bin(ssh_buffer_get(buffer));
    ssh_buffer_free(buffer);
    if (out == NULL) {
        SAFE_FREE(iv);
        return NULL;
    }

    if (algo) {
        if (privatekey_decrypt(algo, mode, key_len, iv, iv_len, out,
                               cb, userdata, desc) < 0) {
            ssh_buffer_free(out);
            SAFE_FREE(iv);
            return NULL;
        }
    }
    SAFE_FREE(iv);

    return out;
}
Exemplo n.º 2
0
static ssh_buffer privatekey_file_to_buffer(FILE *fp, int type,
    ssh_auth_callback cb, void *userdata, const char *desc) {
  ssh_buffer buffer = NULL;
  ssh_buffer out = NULL;
  char buf[MAXLINESIZE] = {0};
  unsigned char *iv = NULL;
  const char *header_begin;
  const char *header_end;
  unsigned int header_begin_size;
  unsigned int header_end_size;
  unsigned int key_len = 0;
  unsigned int iv_len = 0;
  int algo = 0;
  int mode = 0;
  int len;

  buffer = buffer_new();
  if (buffer == NULL) {
    return NULL;
  }

  switch(type) {
    case TYPE_DSS:
      header_begin = DSA_HEADER_BEGIN;
      header_end = DSA_HEADER_END;
      break;
    case TYPE_RSA:
      header_begin = RSA_HEADER_BEGIN;
      header_end = RSA_HEADER_END;
      break;
    default:
      buffer_free(buffer);
      return NULL;
  }

  header_begin_size = strlen(header_begin);
  header_end_size = strlen(header_end);

  while (read_line(buf, MAXLINESIZE, fp) &&
      strncmp(buf, header_begin, header_begin_size))
    ;

  len = read_line(buf, MAXLINESIZE, fp);
  if (len > 11 && strncmp("Proc-Type: 4,ENCRYPTED", buf, 11) == 0) {
    len = read_line(buf, MAXLINESIZE, fp);
    if (len > 10 && strncmp("DEK-Info: ", buf, 10) == 0) {
      if ((privatekey_dek_header(buf + 10, len - 10, &algo, &mode, &key_len,
                                 &iv, &iv_len) < 0)
          || read_line(buf, MAXLINESIZE, fp)) {
        buffer_free(buffer);
        SAFE_FREE(iv);
        return NULL;
      }
    } else {
      buffer_free(buffer);
      SAFE_FREE(iv);
      return NULL;
    }
  } else {
    if (buffer_add_data(buffer, buf, len) < 0) {
      buffer_free(buffer);
      SAFE_FREE(iv);
      return NULL;
    }
  }

  while ((len = read_line(buf,MAXLINESIZE,fp)) &&
      strncmp(buf, header_end, header_end_size) != 0) {
    if (len == -1) {
      buffer_free(buffer);
      SAFE_FREE(iv);
      return NULL;
    }
    if (buffer_add_data(buffer, buf, len) < 0) {
      buffer_free(buffer);
      SAFE_FREE(iv);
      return NULL;
    }
  }

  if (strncmp(buf,header_end,header_end_size) != 0) {
    buffer_free(buffer);
    SAFE_FREE(iv);
    return NULL;
  }

  if (buffer_add_data(buffer, "\0", 1) < 0) {
    buffer_free(buffer);
    SAFE_FREE(iv);
    return NULL;
  }

  out = base64_to_bin(buffer_get(buffer));
  buffer_free(buffer);
  if (out == NULL) {
    SAFE_FREE(iv);
    return NULL;
  }

  if (algo) {
    if (privatekey_decrypt(algo, mode, key_len, iv, iv_len, out,
          cb, userdata, desc) < 0) {
      buffer_free(out);
      SAFE_FREE(iv);
      return NULL;
    }
  }
  SAFE_FREE(iv);

  return out;
}