Develop and Download Open Source Software

Browse Subversion Repository

Diff of /branches/ssh_chacha20poly1305/ttssh2/ttxssh/ssh.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2854 by yutakakn, Mon Mar 6 14:43:50 2006 UTC revision 2856 by yutakakn, Sun Mar 26 15:43:58 2006 UTC
# Line 1213  static BOOL handle_server_public_key(PTI Line 1213  static BOOL handle_server_public_key(PTI
1213          int protocol_flags_pos;          int protocol_flags_pos;
1214          int supported_ciphers;          int supported_ciphers;
1215          char FAR *inmsg;          char FAR *inmsg;
1216            Key hostkey;
1217    
1218          if (!grab_payload(pvar, 14))          if (!grab_payload(pvar, 14))
1219                  return FALSE;                  return FALSE;
# Line 1268  static BOOL handle_server_public_key(PTI Line 1269  static BOOL handle_server_public_key(PTI
1269    
1270          /* this must be the LAST THING in this function, since it can cause          /* this must be the LAST THING in this function, since it can cause
1271             host_is_OK to be called. */             host_is_OK to be called. */
1272          HOSTS_check_host_key(pvar, pvar->ssh_state.hostname,          hostkey.type = KEY_RSA1;
1273                                                   get_uint32(inmsg + host_key_bits_pos),          hostkey.bits = get_uint32(inmsg + host_key_bits_pos);
1274                                                   inmsg + host_key_bits_pos + 4,          hostkey.exp = inmsg + host_key_bits_pos + 4;
1275                                                   inmsg + host_key_public_modulus_pos);          hostkey.mod = inmsg + host_key_public_modulus_pos;
1276            HOSTS_check_host_key(pvar, pvar->ssh_state.hostname, &hostkey);
1277    
1278          return FALSE;          return FALSE;
1279  }  }
# Line 1338  static BOOL negotiate_protocol(PTInstVar Line 1340  static BOOL negotiate_protocol(PTInstVar
1340  static void init_protocol(PTInstVar pvar)  static void init_protocol(PTInstVar pvar)
1341  {  {
1342          CRYPT_initialize_random_numbers(pvar);          CRYPT_initialize_random_numbers(pvar);
1343    
1344            // known_hostsファイルからホスト公開鍵を先読みしておく
1345          HOSTS_prefetch_host_key(pvar, pvar->ssh_state.hostname);          HOSTS_prefetch_host_key(pvar, pvar->ssh_state.hostname);
1346    
1347          /* while we wait for a response from the server... */          /* while we wait for a response from the server... */
1348    
1349          if (SSHv1(pvar)) {          if (SSHv1(pvar)) {
# Line 3107  void debug_print(int no, char *msg, int Line 3112  void debug_print(int no, char *msg, int
3112  // クライアントからサーバへの提案事項  // クライアントからサーバへの提案事項
3113  #ifdef SSH2_DEBUG  #ifdef SSH2_DEBUG
3114  static char *myproposal[PROPOSAL_MAX] = {  static char *myproposal[PROPOSAL_MAX] = {
3115  //      "diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1",          "diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1",
3116          "diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1",  //      "diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1",
3117  //      "ssh-rsa,ssh-dss",          "ssh-rsa,ssh-dss",
3118          "ssh-dss,ssh-rsa",  //      "ssh-dss,ssh-rsa",
3119          "3des-cbc,aes128-cbc",          "3des-cbc,aes128-cbc",
3120          "3des-cbc,aes128-cbc",          "3des-cbc,aes128-cbc",
3121          "hmac-md5,hmac-sha1",          "hmac-md5,hmac-sha1",
# Line 4493  static int key_verify( Line 4498  static int key_verify(
4498          return (ret);   // success          return (ret);   // success
4499  }  }
4500    
4501    //
4502    // RSA構造体の複製
4503    //
4504    RSA *duplicate_RSA(RSA *src)
4505    {
4506            RSA *rsa = NULL;
4507    
4508            rsa = RSA_new();
4509            if (rsa == NULL)
4510                    goto error;
4511            rsa->n = BN_new();
4512            rsa->e = BN_new();
4513            if (rsa->n == NULL || rsa->e == NULL) {
4514                    RSA_free(rsa);
4515                    goto error;
4516            }
4517    
4518            // 深いコピー(deep copy)を行う。浅いコピー(shallow copy)はNG。
4519            BN_copy(rsa->n, src->n);
4520            BN_copy(rsa->e, src->e);
4521    
4522    error:
4523            return (rsa);
4524    }
4525    
4526    
4527    //
4528    // DSA構造体の複製
4529    //
4530    DSA *duplicate_DSA(DSA *src)
4531    {
4532            DSA *dsa = NULL;
4533    
4534            dsa = DSA_new();
4535            if (dsa == NULL)
4536                    goto error;
4537            dsa->p = BN_new();
4538            dsa->q = BN_new();
4539            dsa->g = BN_new();
4540            dsa->pub_key = BN_new();
4541            if (dsa->p == NULL ||
4542                    dsa->q == NULL ||
4543                    dsa->g == NULL ||
4544                    dsa->pub_key == NULL) {
4545                    DSA_free(dsa);
4546                    goto error;
4547            }
4548    
4549            // 深いコピー(deep copy)を行う。浅いコピー(shallow copy)はNG。
4550            BN_copy(dsa->p, src->p);
4551            BN_copy(dsa->q, src->q);
4552            BN_copy(dsa->g, src->g);
4553            BN_copy(dsa->pub_key, src->pub_key);
4554    
4555    error:
4556            return (dsa);
4557    }
4558    
4559    
4560    //
4561    // キーのメモリ領域解放
4562    //
4563    void key_free(Key *key)
4564    {
4565            switch (key->type) {
4566                    case KEY_RSA1:
4567                    case KEY_RSA:
4568                            if (key->rsa != NULL)
4569                                    RSA_free(key->rsa);
4570                            key->rsa = NULL;
4571                            break;
4572    
4573                    case KEY_DSA:
4574                            if (key->dsa != NULL)
4575                                    DSA_free(key->dsa);
4576                            key->dsa = NULL;
4577                            break;
4578            }
4579            free(key);
4580    }
4581    
4582    //
4583    // キーから文字列を返却する
4584    //
4585    char *get_sshname_from_key(Key *key)
4586    {
4587            if (key->type == KEY_RSA) {
4588                    return "ssh-rsa";
4589            } else if (key->type == KEY_DSA) {
4590                    return "ssh-dss";
4591            } else {
4592                    return "ssh-unknown";
4593            }
4594    }
4595    
4596    
4597    //
4598    // キー文字列から種別を判定する
4599    //
4600    enum hostkey_type get_keytype_from_name(char *name)
4601    {
4602            if (strcmp(name, "rsa1") == 0) {
4603                    return KEY_RSA1;
4604            } else if (strcmp(name, "rsa") == 0) {
4605                    return KEY_RSA;
4606            } else if (strcmp(name, "dsa") == 0) {
4607                    return KEY_DSA;
4608            } else if (strcmp(name, "ssh-rsa") == 0) {
4609                    return KEY_RSA;
4610            } else if (strcmp(name, "ssh-dss") == 0) {
4611                    return KEY_DSA;
4612            }
4613            return KEY_UNSPEC;
4614    }
4615    
4616    
4617    //
4618    // キー情報からバッファへ変換する (for SSH2)
4619    // NOTE:
4620    //
4621    int key_to_blob(Key *key, char **blobp, int *lenp)
4622    {
4623            buffer_t *b;
4624            char *sshname;
4625            int len;
4626            int ret = 1;  // success
4627    
4628            b = buffer_init();
4629            sshname = get_sshname_from_key(key);
4630    
4631            if (key->type == KEY_RSA) {
4632                    buffer_put_string(b, sshname, strlen(sshname));
4633                    buffer_put_bignum2(b, key->rsa->e);
4634                    buffer_put_bignum2(b, key->rsa->n);
4635    
4636            } else if (key->type == KEY_DSA) {
4637                    buffer_put_string(b, sshname, strlen(sshname));
4638                    buffer_put_bignum2(b, key->dsa->p);
4639                    buffer_put_bignum2(b, key->dsa->q);
4640                    buffer_put_bignum2(b, key->dsa->g);
4641                    buffer_put_bignum2(b, key->dsa->pub_key);
4642    
4643            } else {
4644                    ret = 0;
4645                    goto error;
4646    
4647            }
4648    
4649            len = buffer_len(b);
4650            if (lenp != NULL)
4651                    *lenp = len;
4652            if (blobp != NULL) {
4653                    *blobp = malloc(len);
4654                    if (*blobp == NULL) {
4655                            ret = 0;
4656                            goto error;
4657                    }
4658                    memcpy(*blobp, buffer_ptr(b), len);
4659            }
4660    
4661    error:
4662            buffer_free(b);
4663    
4664            return (ret);
4665    }
4666    
4667    
4668    //
4669    // バッファからキー情報を取り出す(for SSH2)
4670    // NOTE: 返値はアロケート領域になるので、呼び出し側で解放すること。
4671    //
4672    Key *key_from_blob(char *data, int blen)
4673    {
4674            int keynamelen;
4675            char key[128];
4676            RSA *rsa = NULL;
4677            DSA *dsa = NULL;
4678            Key *hostkey;  // hostkey
4679            enum hostkey_type type;
4680    
4681            hostkey = malloc(sizeof(Key));
4682            if (hostkey == NULL)
4683                    goto error;
4684    
4685            memset(hostkey, 0, sizeof(Key));
4686    
4687            keynamelen = get_uint32_MSBfirst(data);
4688            if (keynamelen >= sizeof(key)) {
4689                    goto error;
4690            }
4691            data += 4;
4692            memcpy(key, data, keynamelen);
4693            key[keynamelen] = 0;
4694            data += keynamelen;
4695    
4696            type = get_keytype_from_name(key);
4697    
4698                    // RSA key
4699            if (type == KEY_RSA) {
4700                    rsa = RSA_new();
4701                    if (rsa == NULL) {
4702                            goto error;
4703                    }
4704                    rsa->n = BN_new();
4705                    rsa->e = BN_new();
4706                    if (rsa->n == NULL || rsa->e == NULL) {
4707                            goto error;
4708                    }
4709    
4710                    buffer_get_bignum2(&data, rsa->e);
4711                    buffer_get_bignum2(&data, rsa->n);
4712    
4713                    hostkey->type = type;
4714                    hostkey->rsa = rsa;
4715    
4716            } else if (type == KEY_DSA) { // DSA key
4717                    dsa = DSA_new();
4718                    if (dsa == NULL) {
4719                            goto error;
4720                    }
4721                    dsa->p = BN_new();
4722                    dsa->q = BN_new();
4723                    dsa->g = BN_new();
4724                    dsa->pub_key = BN_new();
4725                    if (dsa->p == NULL ||
4726                            dsa->q == NULL ||
4727                            dsa->g == NULL ||
4728                            dsa->pub_key == NULL) {
4729                            goto error;
4730                    }
4731    
4732                    buffer_get_bignum2(&data, dsa->p);
4733                    buffer_get_bignum2(&data, dsa->q);
4734                    buffer_get_bignum2(&data, dsa->g);
4735                    buffer_get_bignum2(&data, dsa->pub_key);
4736    
4737                    hostkey->type = type;
4738                    hostkey->dsa = dsa;
4739    
4740            } else {
4741                    // unknown key
4742                    goto error;
4743    
4744            }
4745    
4746            return (hostkey);
4747    
4748    error:
4749            if (rsa != NULL)
4750                    RSA_free(rsa);
4751            if (dsa != NULL)
4752                    DSA_free(dsa);
4753    
4754            return NULL;
4755    }
4756    
4757    
4758    //
4759  // Diffie-Hellman Key Exchange Reply(SSH2_MSG_KEXDH_REPLY:31)  // Diffie-Hellman Key Exchange Reply(SSH2_MSG_KEXDH_REPLY:31)
4760  //  //
4761  static BOOL handle_SSH2_dh_kex_reply(PTInstVar pvar)  static BOOL handle_SSH2_dh_kex_reply(PTInstVar pvar)
# Line 4513  static BOOL handle_SSH2_dh_kex_reply(PTI Line 4775  static BOOL handle_SSH2_dh_kex_reply(PTI
4775          BIGNUM *share_key = NULL;          BIGNUM *share_key = NULL;
4776          char *hash;          char *hash;
4777          char *emsg, emsg_tmp[1024];  // error message          char *emsg, emsg_tmp[1024];  // error message
4778            Key hostkey;  // hostkey
4779    
4780            memset(&hostkey, 0, sizeof(hostkey));
4781    
4782          // TODO: buffer overrun check          // TODO: buffer overrun check
4783    
# Line 4529  static BOOL handle_SSH2_dh_kex_reply(PTI Line 4793  static BOOL handle_SSH2_dh_kex_reply(PTI
4793          data += 4;          data += 4;
4794          server_host_key_blob = data; // for hash          server_host_key_blob = data; // for hash
4795    
4796            // key_from_blob()#key.c の処理が以下から始まる。
4797            // known_hosts検証用の server_host_key は rsa or dsa となる。
4798          keynamelen = get_uint32_MSBfirst(data);          keynamelen = get_uint32_MSBfirst(data);
4799          if (keynamelen >= 128) {          if (keynamelen >= 128) {
4800                  emsg = "keyname length too big @ handle_SSH2_dh_kex_reply()";                  emsg = "keyname length too big @ handle_SSH2_dh_kex_reply()";
# Line 4556  static BOOL handle_SSH2_dh_kex_reply(PTI Line 4822  static BOOL handle_SSH2_dh_kex_reply(PTI
4822                  buffer_get_bignum2(&data, rsa->e);                  buffer_get_bignum2(&data, rsa->e);
4823                  buffer_get_bignum2(&data, rsa->n);                  buffer_get_bignum2(&data, rsa->n);
4824    
4825                    hostkey.type = KEY_RSA;
4826                    hostkey.rsa = rsa;
4827    
4828          } else if (strcmp(key, "ssh-dss") == 0) { // DSA key          } else if (strcmp(key, "ssh-dss") == 0) { // DSA key
4829                  dsa = DSA_new();                  dsa = DSA_new();
4830                  if (dsa == NULL) {                  if (dsa == NULL) {
# Line 4579  static BOOL handle_SSH2_dh_kex_reply(PTI Line 4848  static BOOL handle_SSH2_dh_kex_reply(PTI
4848                  buffer_get_bignum2(&data, dsa->g);                  buffer_get_bignum2(&data, dsa->g);
4849                  buffer_get_bignum2(&data, dsa->pub_key);                  buffer_get_bignum2(&data, dsa->pub_key);
4850    
4851                    hostkey.type = KEY_DSA;
4852                    hostkey.dsa = dsa;
4853    
4854          } else {          } else {
4855                  // unknown key                  // unknown key
4856                  _snprintf(emsg_tmp, sizeof(emsg_tmp), "Unknown key type(%s) @ handle_SSH2_dh_kex_reply()", key);                  _snprintf(emsg_tmp, sizeof(emsg_tmp), "Unknown key type(%s) @ handle_SSH2_dh_kex_reply()", key);
# Line 4587  static BOOL handle_SSH2_dh_kex_reply(PTI Line 4859  static BOOL handle_SSH2_dh_kex_reply(PTI
4859    
4860          }          }
4861    
4862            // known_hosts対応 (2006.3.20 yutaka)
4863            if (hostkey.type != pvar->hostkey_type) {  // ホストキーの種別比較
4864                    _snprintf(emsg_tmp, sizeof(emsg_tmp), "type mismatch for decoded server_host_key_blob @ %s", __FUNCTION__);
4865                    emsg = emsg_tmp;
4866                    goto error;
4867            }
4868            HOSTS_check_host_key(pvar, pvar->ssh_state.hostname, &hostkey);
4869    
4870    
4871          dh_server_pub = BN_new();          dh_server_pub = BN_new();
4872          if (dh_server_pub == NULL) {          if (dh_server_pub == NULL) {
4873                  emsg = "Out of memory5 @ handle_SSH2_dh_kex_reply()";                  emsg = "Out of memory5 @ handle_SSH2_dh_kex_reply()";
# Line 4661  static BOOL handle_SSH2_dh_kex_reply(PTI Line 4942  static BOOL handle_SSH2_dh_kex_reply(PTI
4942    
4943          kex_derive_keys(pvar, pvar->we_need, hash, share_key, pvar->session_id, pvar->session_id_len);          kex_derive_keys(pvar, pvar->we_need, hash, share_key, pvar->session_id, pvar->session_id_len);
4944    
4945    
4946          // KEX finish          // KEX finish
4947          begin_send_packet(pvar, SSH2_MSG_NEWKEYS, 0);          begin_send_packet(pvar, SSH2_MSG_NEWKEYS, 0);
4948          finish_send_packet(pvar);          finish_send_packet(pvar);
# Line 4715  error: Line 4997  error:
4997    
4998    
4999    
5000    
5001  // SHA-1(160bit)を求める  // SHA-1(160bit)を求める
5002  static unsigned char *kex_dh_gex_hash(  static unsigned char *kex_dh_gex_hash(
5003      char *client_version_string,      char *client_version_string,
# Line 4800  static BOOL handle_SSH2_dh_gex_reply(PTI Line 5083  static BOOL handle_SSH2_dh_gex_reply(PTI
5083          char *hash;          char *hash;
5084          char *emsg, emsg_tmp[1024];  // error message          char *emsg, emsg_tmp[1024];  // error message
5085          int ret;          int ret;
5086            Key hostkey;  // hostkey
5087    
5088            memset(&hostkey, 0, sizeof(hostkey));
5089    
5090          // TODO: buffer overrun check          // TODO: buffer overrun check
5091    
# Line 4820  static BOOL handle_SSH2_dh_gex_reply(PTI Line 5105  static BOOL handle_SSH2_dh_gex_reply(PTI
5105    
5106          push_memdump("DH_GEX_REPLY", "server_host_key_blob", server_host_key_blob, bloblen);          push_memdump("DH_GEX_REPLY", "server_host_key_blob", server_host_key_blob, bloblen);
5107    
5108            // key_from_blob()#key.c の処理が以下から始まる。
5109            // known_hosts検証用の server_host_key は rsa or dsa となる。
5110          keynamelen = get_uint32_MSBfirst(data);          keynamelen = get_uint32_MSBfirst(data);
5111          if (keynamelen >= 128) {          if (keynamelen >= 128) {
5112                  emsg = "keyname length too big @ handle_SSH2_dh_kex_reply()";                  emsg = "keyname length too big @ handle_SSH2_dh_kex_reply()";
# Line 4849  static BOOL handle_SSH2_dh_gex_reply(PTI Line 5136  static BOOL handle_SSH2_dh_gex_reply(PTI
5136                  buffer_get_bignum2(&data, rsa->e);                  buffer_get_bignum2(&data, rsa->e);
5137                  buffer_get_bignum2(&data, rsa->n);                  buffer_get_bignum2(&data, rsa->n);
5138    
5139                    hostkey.type = KEY_RSA;
5140                    hostkey.rsa = rsa;
5141    
5142          } else if (strcmp(key, "ssh-dss") == 0) { // DSA key          } else if (strcmp(key, "ssh-dss") == 0) { // DSA key
5143                  dsa = DSA_new();                  dsa = DSA_new();
5144                  if (dsa == NULL) {                  if (dsa == NULL) {
# Line 4872  static BOOL handle_SSH2_dh_gex_reply(PTI Line 5162  static BOOL handle_SSH2_dh_gex_reply(PTI
5162                  buffer_get_bignum2(&data, dsa->g);                  buffer_get_bignum2(&data, dsa->g);
5163                  buffer_get_bignum2(&data, dsa->pub_key);                  buffer_get_bignum2(&data, dsa->pub_key);
5164    
5165                    hostkey.type = KEY_DSA;
5166                    hostkey.dsa = dsa;
5167    
5168          } else {          } else {
5169                  // unknown key                  // unknown key
5170                  _snprintf(emsg_tmp, sizeof(emsg_tmp), "Unknown key type(%s) @ handle_SSH2_dh_kex_reply()", key);                  _snprintf(emsg_tmp, sizeof(emsg_tmp), "Unknown key type(%s) @ handle_SSH2_dh_kex_reply()", key);
# Line 4880  static BOOL handle_SSH2_dh_gex_reply(PTI Line 5173  static BOOL handle_SSH2_dh_gex_reply(PTI
5173    
5174          }          }
5175    
5176            // known_hosts対応 (2006.3.20 yutaka)
5177            if (hostkey.type != pvar->hostkey_type) {  // ホストキーの種別比較
5178                    _snprintf(emsg_tmp, sizeof(emsg_tmp), "type mismatch for decoded server_host_key_blob @ %s", __FUNCTION__);
5179                    emsg = emsg_tmp;
5180                    goto error;
5181            }
5182            HOSTS_check_host_key(pvar, pvar->ssh_state.hostname, &hostkey);
5183    
5184    
5185          dh_server_pub = BN_new();          dh_server_pub = BN_new();
5186          if (dh_server_pub == NULL) {          if (dh_server_pub == NULL) {
5187                  emsg = "Out of memory5 @ handle_SSH2_dh_kex_reply()";                  emsg = "Out of memory5 @ handle_SSH2_dh_kex_reply()";
# Line 6419  static BOOL handle_SSH2_window_adjust(PT Line 6721  static BOOL handle_SSH2_window_adjust(PT
6721    
6722  /*  /*
6723   * $Log: not supported by cvs2svn $   * $Log: not supported by cvs2svn $
6724     * Revision 1.40  2006/03/06 14:43:49  yutakakn
6725     * SSH2ウィンドウ制御の見直しにより、スループットを向上させた。
6726     *
6727   * Revision 1.39  2006/02/23 14:13:57  yutakakn   * Revision 1.39  2006/02/23 14:13:57  yutakakn
6728   * authorized_keysファイルの"command="をサポートした   * authorized_keysファイルの"command="をサポートした
6729   *   *

Legend:
Removed from v.2854  
changed lines
  Added in v.2856

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26