~ubuntu-branches/ubuntu/saucy/curl/saucy-201307251546

« back to all changes in this revision

Viewing changes to lib/ssluse.c

  • Committer: Bazaar Package Importer
  • Author(s): Ramakrishnan Muthukrishnan
  • Date: 2011-02-28 19:35:36 UTC
  • mto: (3.6.1 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: james.westby@ubuntu.com-20110228193536-p3a9jawxxofcsz7o
Tags: upstream-7.21.4
ImportĀ upstreamĀ versionĀ 7.21.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
225
225
  /* If we get here, it means we need to seed the PRNG using a "silly"
226
226
     approach! */
227
227
#ifdef HAVE_RAND_SCREEN
228
 
  /* if RAND_screen() is present, it was called during global init */
 
228
  /* if RAND_screen() is present, this is windows and thus we assume that the
 
229
     randomness is already taken care of */
229
230
  nread = 100; /* just a value */
230
231
#else
231
232
  {
324
325
       * If password has been given, we store that in the global
325
326
       * area (*shudder*) for a while:
326
327
       */
327
 
      size_t len = strlen(data->set.key_passwd);
 
328
      size_t len = strlen(data->set.str[STRING_KEY_PASSWD]);
328
329
      if(len < sizeof(global_passwd))
329
 
        memcpy(global_passwd, data->set.key_passwd, len+1);
 
330
        memcpy(global_passwd, data->set.str[STRING_KEY_PASSWD], len+1);
330
331
#else
331
332
      /*
332
333
       * We set the password in the callback userdata
685
686
 
686
687
  OpenSSL_add_all_algorithms();
687
688
 
688
 
#ifdef HAVE_RAND_SCREEN
689
 
  /* This one gets a random value by reading the currently shown screen.
690
 
     RAND_screen() is not thread-safe according to OpenSSL devs - although not
691
 
     mentioned in documentation. */
692
 
  RAND_screen();
693
 
#endif
694
 
 
695
689
  return 1;
696
690
}
697
691
 
1469
1463
  connssl->ctx = SSL_CTX_new(req_method);
1470
1464
 
1471
1465
  if(!connssl->ctx) {
1472
 
    failf(data, "SSL: couldn't create a context!");
 
1466
    failf(data, "SSL: couldn't create a context: %s",
 
1467
          ERR_error_string(ERR_peek_error(), NULL));
1473
1468
    return CURLE_OUT_OF_MEMORY;
1474
1469
  }
1475
1470
 
1846
1841
                        unsigned char *raw,
1847
1842
                        int len)
1848
1843
{
1849
 
  char buffer[1024];
1850
 
  size_t left = sizeof(buffer);
 
1844
  size_t left;
1851
1845
  int i;
1852
 
  char *ptr=buffer;
1853
1846
  char namebuf[32];
1854
 
 
1855
 
  snprintf(namebuf, sizeof(namebuf), "%s(%s)", type, name);
1856
 
 
1857
 
  for(i=0; i< len; i++) {
1858
 
    snprintf(ptr, left, "%02x:", raw[i]);
1859
 
    ptr += 3;
1860
 
    left -= 3;
 
1847
  char *buffer;
 
1848
 
 
1849
  left = sizeof(len*3 + 1);
 
1850
  buffer = malloc(left);
 
1851
  if(buffer) {
 
1852
    char *ptr=buffer;
 
1853
    snprintf(namebuf, sizeof(namebuf), "%s(%s)", type, name);
 
1854
    for(i=0; i< len; i++) {
 
1855
      snprintf(ptr, left, "%02x:", raw[i]);
 
1856
      ptr += 3;
 
1857
      left -= 3;
 
1858
    }
 
1859
    infof(data, "   %s: %s\n", namebuf, buffer);
 
1860
    push_certinfo(data, num, namebuf, buffer);
 
1861
    free(buffer);
1861
1862
  }
1862
 
  infof(data, "   %s: %s\n", namebuf, buffer);
1863
 
  push_certinfo(data, num, namebuf, buffer);
1864
1863
}
1865
1864
 
1866
1865
#define print_pubkey_BN(_type, _name, _num)    \
1867
1866
do {                              \
1868
1867
  if (pubkey->pkey._type->_name != NULL) { \
1869
1868
    int len = BN_num_bytes(pubkey->pkey._type->_name); \
1870
 
    if(len < (int)sizeof(buf)) {                       \
1871
 
      BN_bn2bin(pubkey->pkey._type->_name, (unsigned char*)buf); \
1872
 
      buf[len] = 0; \
1873
 
      pubkey_show(data, _num, #_type, #_name, (unsigned char*)buf, len); \
 
1869
    if(len < CERTBUFFERSIZE) {                       \
 
1870
      BN_bn2bin(pubkey->pkey._type->_name, (unsigned char*)bufp); \
 
1871
      bufp[len] = 0; \
 
1872
      pubkey_show(data, _num, #_type, #_name, (unsigned char*)bufp, len); \
1874
1873
    } \
1875
1874
  } \
1876
1875
} while (0)
1986
1985
  return 0;
1987
1986
}
1988
1987
 
 
1988
/*
 
1989
 * This size was previously 512 which has been reported "too small" without
 
1990
 * any specifics, so it was enlarged to allow more data to get shown uncut.
 
1991
 * The "perfect" size is yet to figure out.
 
1992
 */
 
1993
#define CERTBUFFERSIZE 8192
 
1994
 
1989
1995
static CURLcode get_cert_chain(struct connectdata *conn,
1990
1996
                               struct ssl_connect_data *connssl)
1991
1997
 
1992
1998
{
1993
1999
  STACK_OF(X509) *sk;
1994
2000
  int i;
1995
 
  char buf[512];
 
2001
  char *bufp;
1996
2002
  struct SessionHandle *data = conn->data;
1997
2003
  int numcerts;
1998
2004
 
 
2005
  bufp = malloc(CERTBUFFERSIZE);
 
2006
  if(!bufp)
 
2007
    return CURLE_OUT_OF_MEMORY;
 
2008
 
1999
2009
  sk = SSL_get_peer_cert_chain(connssl->handle);
2000
 
 
2001
 
  if(!sk)
 
2010
  if(!sk) {
 
2011
    free(bufp);
2002
2012
    return CURLE_OUT_OF_MEMORY;
 
2013
  }
2003
2014
 
2004
2015
  numcerts = sk_X509_num(sk);
2005
 
 
2006
 
  if(init_certinfo(data, numcerts))
 
2016
  if(init_certinfo(data, numcerts)) {
 
2017
    free(bufp);
2007
2018
    return CURLE_OUT_OF_MEMORY;
 
2019
  }
2008
2020
 
2009
2021
  infof(data, "--- Certificate chain\n");
2010
2022
  for (i=0; i<numcerts; i++) {
2024
2036
    int j;
2025
2037
    char *ptr;
2026
2038
 
2027
 
    (void)x509_name_oneline(X509_get_subject_name(x), buf, sizeof(buf));
2028
 
    infof(data, "%2d Subject: %s\n",i,buf);
2029
 
    push_certinfo(data, i, "Subject", buf);
 
2039
    (void)x509_name_oneline(X509_get_subject_name(x), bufp, CERTBUFFERSIZE);
 
2040
    infof(data, "%2d Subject: %s\n", i, bufp);
 
2041
    push_certinfo(data, i, "Subject", bufp);
2030
2042
 
2031
 
    (void)x509_name_oneline(X509_get_issuer_name(x), buf, sizeof(buf));
2032
 
    infof(data, "   Issuer: %s\n",buf);
2033
 
    push_certinfo(data, i, "Issuer", buf);
 
2043
    (void)x509_name_oneline(X509_get_issuer_name(x), bufp, CERTBUFFERSIZE);
 
2044
    infof(data, "   Issuer: %s\n", bufp);
 
2045
    push_certinfo(data, i, "Issuer", bufp);
2034
2046
 
2035
2047
    value = X509_get_version(x);
2036
2048
    infof(data, "   Version: %lu (0x%lx)\n", value+1, value);
2037
 
    snprintf(buf, sizeof(buf), "%lx", value);
2038
 
    push_certinfo(data, i, "Version", buf); /* hex */
 
2049
    snprintf(bufp, CERTBUFFERSIZE, "%lx", value);
 
2050
    push_certinfo(data, i, "Version", bufp); /* hex */
2039
2051
 
2040
2052
    num=X509_get_serialNumber(x);
2041
2053
    if (num->length <= 4) {
2042
2054
      value = ASN1_INTEGER_get(num);
2043
2055
      infof(data,"   Serial Number: %ld (0x%lx)\n", value, value);
2044
 
      snprintf(buf, sizeof(buf), "%lx", value);
 
2056
      snprintf(bufp, CERTBUFFERSIZE, "%lx", value);
2045
2057
    }
2046
2058
    else {
 
2059
      int left = CERTBUFFERSIZE;
2047
2060
 
2048
 
      ptr = buf;
 
2061
      ptr = bufp;
2049
2062
      *ptr++ = 0;
2050
2063
      if(num->type == V_ASN1_NEG_INTEGER)
2051
2064
        *ptr++='-';
2052
2065
 
2053
 
      for (j=0; j<num->length; j++) {
 
2066
      for (j=0; (j<num->length) && (left>=4); j++) {
2054
2067
        /* TODO: length restrictions */
2055
2068
        snprintf(ptr, 3, "%02x%c",num->data[j],
2056
2069
                 ((j+1 == num->length)?'\n':':'));
2057
2070
        ptr += 3;
 
2071
        left-=4;
2058
2072
      }
2059
2073
      if(num->length)
2060
 
        infof(data,"   Serial Number: %s\n", buf);
 
2074
        infof(data,"   Serial Number: %s\n", bufp);
2061
2075
      else
2062
 
        buf[0]=0;
 
2076
        bufp[0]=0;
2063
2077
    }
2064
 
    if(buf[0])
2065
 
      push_certinfo(data, i, "Serial Number", buf); /* hex */
 
2078
    if(bufp[0])
 
2079
      push_certinfo(data, i, "Serial Number", bufp); /* hex */
2066
2080
 
2067
2081
    cinf = x->cert_info;
2068
2082
 
2069
 
    j = asn1_object_dump(cinf->signature->algorithm, buf, sizeof(buf));
 
2083
    j = asn1_object_dump(cinf->signature->algorithm, bufp, CERTBUFFERSIZE);
2070
2084
    if(!j) {
2071
 
      infof(data, "   Signature Algorithm: %s\n", buf);
2072
 
      push_certinfo(data, i, "Signature Algorithm", buf);
 
2085
      infof(data, "   Signature Algorithm: %s\n", bufp);
 
2086
      push_certinfo(data, i, "Signature Algorithm", bufp);
2073
2087
    }
2074
2088
 
2075
2089
    certdate = X509_get_notBefore(x);
2076
 
    asn1_output(certdate, buf, sizeof(buf));
2077
 
    infof(data, "   Start date: %s\n", buf);
2078
 
    push_certinfo(data, i, "Start date", buf);
 
2090
    asn1_output(certdate, bufp, CERTBUFFERSIZE);
 
2091
    infof(data, "   Start date: %s\n", bufp);
 
2092
    push_certinfo(data, i, "Start date", bufp);
2079
2093
 
2080
2094
    certdate = X509_get_notAfter(x);
2081
 
    asn1_output(certdate, buf, sizeof(buf));
2082
 
    infof(data, "   Expire date: %s\n", buf);
2083
 
    push_certinfo(data, i, "Expire date", buf);
 
2095
    asn1_output(certdate, bufp, CERTBUFFERSIZE);
 
2096
    infof(data, "   Expire date: %s\n", bufp);
 
2097
    push_certinfo(data, i, "Expire date", bufp);
2084
2098
 
2085
 
    j = asn1_object_dump(cinf->key->algor->algorithm, buf, sizeof(buf));
 
2099
    j = asn1_object_dump(cinf->key->algor->algorithm, bufp, CERTBUFFERSIZE);
2086
2100
    if(!j) {
2087
 
      infof(data, "   Public Key Algorithm: %s\n", buf);
2088
 
      push_certinfo(data, i, "Public Key Algorithm", buf);
 
2101
      infof(data, "   Public Key Algorithm: %s\n", bufp);
 
2102
      push_certinfo(data, i, "Public Key Algorithm", bufp);
2089
2103
    }
2090
2104
 
2091
2105
    pubkey = X509_get_pubkey(x);
2096
2110
      case EVP_PKEY_RSA:
2097
2111
        infof(data,  "   RSA Public Key (%d bits)\n",
2098
2112
              BN_num_bits(pubkey->pkey.rsa->n));
2099
 
        snprintf(buf, sizeof(buf), "%d", BN_num_bits(pubkey->pkey.rsa->n));
2100
 
        push_certinfo(data, i, "RSA Public Key", buf);
 
2113
        snprintf(bufp, CERTBUFFERSIZE, "%d", BN_num_bits(pubkey->pkey.rsa->n));
 
2114
        push_certinfo(data, i, "RSA Public Key", bufp);
2101
2115
 
2102
2116
        print_pubkey_BN(rsa, n, i);
2103
2117
        print_pubkey_BN(rsa, e, i);
2137
2151
    dumpcert(data, x, i);
2138
2152
  }
2139
2153
 
 
2154
  free(bufp);
 
2155
 
2140
2156
  return CURLE_OK;
2141
2157
}
2142
2158
 
2379
2395
 
2380
2396
  if(ssl_connect_1==connssl->connecting_state) {
2381
2397
    /* Find out how much more time we're allowed */
2382
 
    timeout_ms = Curl_timeleft(conn, NULL, TRUE);
 
2398
    timeout_ms = Curl_timeleft(data, NULL, TRUE);
2383
2399
 
2384
2400
    if(timeout_ms < 0) {
2385
2401
      /* no need to continue if time already is up */
2396
2412
        ssl_connect_2_writing == connssl->connecting_state) {
2397
2413
 
2398
2414
    /* check allowed time left */
2399
 
    timeout_ms = Curl_timeleft(conn, NULL, TRUE);
 
2415
    timeout_ms = Curl_timeleft(data, NULL, TRUE);
2400
2416
 
2401
2417
    if(timeout_ms < 0) {
2402
2418
      /* no need to continue if time already is up */