~ubuntu-branches/ubuntu/saucy/gnutls28/saucy

« back to all changes in this revision

Viewing changes to lib/gnutls_cert.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-07-30 21:40:07 UTC
  • mfrom: (14.1.9 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130730214007-9mrd08xo61kla008
Tags: 3.2.3-1ubuntu1
* Sync with Debian (LP: #1068029). Remaining change:
  - Drop gnutls-bin and -doc since we want to use the versions
    in gnutls26 as the defaults instead

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 *
8
8
 * The GnuTLS is free software; you can redistribute it and/or
9
9
 * modify it under the terms of the GNU Lesser General Public License
10
 
 * as published by the Free Software Foundation; either version 3 of
 
10
 * as published by the Free Software Foundation; either version 2.1 of
11
11
 * the License, or (at your option) any later version.
12
12
 *
13
13
 * This library is distributed in the hope that it will be useful, but
41
41
#ifdef ENABLE_OPENPGP
42
42
#include "openpgp/gnutls_openpgp.h"
43
43
#endif
 
44
#include "gettext.h"
 
45
#define _(String) dgettext (PACKAGE, String)
44
46
 
45
47
/**
46
48
 * gnutls_certificate_free_keys:
137
139
  _gnutls_free_datum (&sc->x509_rdn_sequence);
138
140
}
139
141
 
140
 
/*-
141
 
 * _gnutls_certificate_get_rsa_params - Returns the RSA parameters pointer
142
 
 * @rsa_params: holds the RSA parameters or NULL.
143
 
 * @func: function to retrieve the parameters or NULL.
144
 
 * @session: The session.
145
 
 *
146
 
 * This function will return the rsa parameters pointer.
147
 
 -*/
148
 
gnutls_rsa_params_t
149
 
_gnutls_certificate_get_rsa_params (gnutls_rsa_params_t rsa_params,
150
 
                                    gnutls_params_function * func,
151
 
                                    gnutls_session_t session)
152
 
{
153
 
  gnutls_params_st params;
154
 
  int ret;
155
 
 
156
 
  if (session->internals.params.rsa_params)
157
 
    {
158
 
      return session->internals.params.rsa_params;
159
 
    }
160
 
 
161
 
  if (rsa_params)
162
 
    {
163
 
      session->internals.params.rsa_params = rsa_params;
164
 
    }
165
 
  else if (func)
166
 
    {
167
 
      ret = func (session, GNUTLS_PARAMS_RSA_EXPORT, &params);
168
 
      if (ret == 0 && params.type == GNUTLS_PARAMS_RSA_EXPORT)
169
 
        {
170
 
          session->internals.params.rsa_params = params.params.rsa_export;
171
 
          session->internals.params.free_rsa_params = params.deinit;
172
 
        }
173
 
    }
174
 
 
175
 
  return session->internals.params.rsa_params;
176
 
}
177
 
 
178
 
 
179
142
/**
180
143
 * gnutls_certificate_free_credentials:
181
144
 * @sc: is a #gnutls_certificate_credentials_t structure.
193
156
  gnutls_x509_trust_list_deinit(sc->tlist, 1);
194
157
  gnutls_certificate_free_keys (sc);
195
158
  gnutls_certificate_free_ca_names (sc);
196
 
 
 
159
  gnutls_free(sc->ocsp_response_file);
 
160
  memset(sc->pin_tmp, 0, sizeof(sc->pin_tmp));
197
161
#ifdef ENABLE_OPENPGP
198
162
  gnutls_openpgp_keyring_deinit (sc->keyring);
199
163
#endif
229
193
      gnutls_free(*res);
230
194
      return GNUTLS_E_MEMORY_ERROR;
231
195
    }
232
 
  (*res)->verify_bits = DEFAULT_VERIFY_BITS;
233
 
  (*res)->verify_depth = DEFAULT_VERIFY_DEPTH;
 
196
  (*res)->verify_bits = DEFAULT_MAX_VERIFY_BITS;
 
197
  (*res)->verify_depth = DEFAULT_MAX_VERIFY_DEPTH;
234
198
 
235
199
  return 0;
236
200
}
579
543
 -*/
580
544
static int
581
545
_gnutls_openpgp_crt_verify_peers (gnutls_session_t session,
 
546
                                  const char* hostname,
582
547
                                  unsigned int *status)
583
548
{
584
549
  cert_auth_info_t info;
592
557
    return GNUTLS_E_INVALID_REQUEST;
593
558
 
594
559
  cred = (gnutls_certificate_credentials_t)
595
 
    _gnutls_get_cred (session->key, GNUTLS_CRD_CERTIFICATE, NULL);
 
560
    _gnutls_get_cred (session, GNUTLS_CRD_CERTIFICATE, NULL);
596
561
  if (cred == NULL)
597
562
    {
598
563
      gnutls_assert ();
619
584
  /* Verify certificate 
620
585
   */
621
586
  ret =
622
 
    _gnutls_openpgp_verify_key (cred, &info->raw_certificate_list[0],
 
587
    _gnutls_openpgp_verify_key (cred, hostname, &info->raw_certificate_list[0],
623
588
                                peer_certificate_list_size, status);
624
589
 
625
590
  if (ret < 0)
627
592
      gnutls_assert ();
628
593
      return ret;
629
594
    }
630
 
 
 
595
    
631
596
  return 0;
632
597
}
633
598
#endif
637
602
 * @session: is a gnutls session
638
603
 * @status: is the output of the verification
639
604
 *
640
 
 * This function will try to verify the peer's certificate and return
641
 
 * its status (trusted, invalid etc.).  The value of @status should
642
 
 * be one or more of the gnutls_certificate_status_t enumerated
643
 
 * elements bitwise or'd. To avoid denial of service attacks some
 
605
 * This function will verify the peer's certificate and store
 
606
 * the status in the @status variable as a bitwise or'd gnutls_certificate_status_t
 
607
 * values or zero if the certificate is trusted. Note that value in @status
 
608
 * is set only when the return value of this function is success (i.e, failure 
 
609
 * to trust a certificate does not imply a negative return value).
 
610
 *
 
611
 * If available the OCSP Certificate Status extension will be
 
612
 * utilized by this function.
 
613
 * 
 
614
 * To avoid denial of service attacks some
644
615
 * default upper limits regarding the certificate key size and chain
645
 
 * size are set. To override them use
646
 
 * gnutls_certificate_set_verify_limits().
 
616
 * size are set. To override them use gnutls_certificate_set_verify_limits().
647
617
 *
648
618
 * Note that you must also check the peer's name in order to check if
649
 
 * the verified certificate belongs to the actual peer.
650
 
 *
651
 
 * This function uses gnutls_x509_crt_list_verify() with the CAs in
652
 
 * the credentials as trusted CAs.
 
619
 * the verified certificate belongs to the actual peer, see gnutls_x509_crt_check_hostname(),
 
620
 * or use gnutls_certificate_verify_peers3().
653
621
 *
654
622
 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
655
623
 **/
673
641
  switch (gnutls_certificate_type_get (session))
674
642
    {
675
643
    case GNUTLS_CRT_X509:
676
 
      return _gnutls_x509_cert_verify_peers (session, status);
677
 
#ifdef ENABLE_OPENPGP
678
 
    case GNUTLS_CRT_OPENPGP:
679
 
      return _gnutls_openpgp_crt_verify_peers (session, status);
 
644
      return _gnutls_x509_cert_verify_peers (session, NULL, status);
 
645
#ifdef ENABLE_OPENPGP
 
646
    case GNUTLS_CRT_OPENPGP:
 
647
      return _gnutls_openpgp_crt_verify_peers (session, NULL, status);
 
648
#endif
 
649
    default:
 
650
      return GNUTLS_E_INVALID_REQUEST;
 
651
    }
 
652
}
 
653
 
 
654
/**
 
655
 * gnutls_certificate_verify_peers3:
 
656
 * @session: is a gnutls session
 
657
 * @hostname: is the expected name of the peer; may be %NULL
 
658
 * @status: is the output of the verification
 
659
 *
 
660
 * This function will verify the peer's certificate and store the
 
661
 * status in the @status variable as a bitwise or'd gnutls_certificate_status_t
 
662
 * values or zero if the certificate is trusted. Note that value in @status
 
663
 * is set only when the return value of this function is success (i.e, failure 
 
664
 * to trust a certificate does not imply a negative return value).
 
665
 *
 
666
 * If the @hostname provided is non-NULL then this function will compare
 
667
 * the hostname in the certificate against the given. If they do not match 
 
668
 * the %GNUTLS_CERT_UNEXPECTED_OWNER status flag will be set.
 
669
 *
 
670
 * If available the OCSP Certificate Status extension will be
 
671
 * utilized by this function.
 
672
 * 
 
673
 * To avoid denial of service attacks some
 
674
 * default upper limits regarding the certificate key size and chain
 
675
 * size are set. To override them use gnutls_certificate_set_verify_limits().
 
676
 *
 
677
 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
 
678
 *
 
679
 * Since: 3.1.4
 
680
 **/
 
681
int
 
682
gnutls_certificate_verify_peers3 (gnutls_session_t session,
 
683
                                  const char* hostname,
 
684
                                  unsigned int *status)
 
685
{
 
686
  cert_auth_info_t info;
 
687
 
 
688
  CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
 
689
 
 
690
  info = _gnutls_get_auth_info (session);
 
691
  if (info == NULL)
 
692
    {
 
693
      return GNUTLS_E_NO_CERTIFICATE_FOUND;
 
694
    }
 
695
 
 
696
  if (info->raw_certificate_list == NULL || info->ncerts == 0)
 
697
    return GNUTLS_E_NO_CERTIFICATE_FOUND;
 
698
 
 
699
  switch (gnutls_certificate_type_get (session))
 
700
    {
 
701
    case GNUTLS_CRT_X509:
 
702
      return _gnutls_x509_cert_verify_peers (session, hostname, status);
 
703
#ifdef ENABLE_OPENPGP
 
704
    case GNUTLS_CRT_OPENPGP:
 
705
      return _gnutls_openpgp_crt_verify_peers (session, hostname, status);
680
706
#endif
681
707
    default:
682
708
      return GNUTLS_E_INVALID_REQUEST;
796
822
 * callback function.  See also gnutls_sign_callback_get().
797
823
 *
798
824
 * Deprecated: Use the PKCS 11 or #gnutls_privkey_t interfacess like gnutls_privkey_import_ext() instead.
799
 
 */
 
825
 **/
800
826
void
801
827
gnutls_sign_callback_set (gnutls_session_t session,
802
828
                          gnutls_sign_func sign_func, void *userdata)
816
842
 *   if not set, %NULL.
817
843
 *
818
844
 * Deprecated: Use the PKCS 11 interfaces instead.
819
 
 */
 
845
 **/
820
846
gnutls_sign_func
821
847
gnutls_sign_callback_get (gnutls_session_t session, void **userdata)
822
848
{
825
851
  return session->internals.sign_func;
826
852
}
827
853
 
828
 
 
 
854
/* returns error if the certificate has different algorithm than
 
855
 * the given key parameters.
 
856
 */
 
857
int
 
858
_gnutls_check_key_cert_match (gnutls_certificate_credentials_t res)
 
859
{
 
860
  int pk = gnutls_pubkey_get_pk_algorithm(res->certs[res->ncerts-1].cert_list[0].pubkey, NULL);
 
861
  int pk2 = gnutls_privkey_get_pk_algorithm (res->pkey[res->ncerts - 1], NULL);
 
862
 
 
863
  if (pk2 != pk)
 
864
    {
 
865
      gnutls_assert ();
 
866
      return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
 
867
    }
 
868
 
 
869
  return 0;
 
870
}
 
871
 
 
872
/**
 
873
 * gnutls_certificate_verification_status_print:
 
874
 * @status: The status flags to be printed
 
875
 * @type: The certificate type
 
876
 * @out: Newly allocated datum with (0) terminated string.
 
877
 * @flags: should be zero
 
878
 *
 
879
 * This function will pretty print the status of a verification
 
880
 * process -- eg. the one obtained by gnutls_certificate_verify_peers3().
 
881
 *
 
882
 * The output @out needs to be deallocated using gnutls_free().
 
883
 *
 
884
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
 
885
 *   negative error value.
 
886
 *
 
887
 * Since: 3.1.4
 
888
 **/
 
889
int
 
890
gnutls_certificate_verification_status_print (unsigned int status,
 
891
                       gnutls_certificate_type_t type,
 
892
                       gnutls_datum_t * out, unsigned int flags)
 
893
{
 
894
  gnutls_buffer_st str;
 
895
  int ret;
 
896
 
 
897
  _gnutls_buffer_init (&str);
 
898
 
 
899
  if (status == 0)
 
900
    _gnutls_buffer_append_str (&str, _("The certificate is trusted. "));
 
901
  else
 
902
    _gnutls_buffer_append_str (&str, _("The certificate is NOT trusted. "));
 
903
 
 
904
  if (type == GNUTLS_CRT_X509)
 
905
    {
 
906
      if (status & GNUTLS_CERT_REVOKED)
 
907
        _gnutls_buffer_append_str (&str, _("The certificate chain is revoked. "));
 
908
 
 
909
      if (status & GNUTLS_CERT_MISMATCH)
 
910
        _gnutls_buffer_append_str (&str, _("The certificate doesn't match the local copy (TOFU). "));
 
911
 
 
912
      if (status & GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED)
 
913
         _gnutls_buffer_append_str (&str, _("The revocation data are old and have been superseded. "));
 
914
 
 
915
      if (status & GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)
 
916
         _gnutls_buffer_append_str (&str, _("The revocation data are issued with a future date. "));
 
917
 
 
918
      if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
 
919
         _gnutls_buffer_append_str (&str, _("The certificate issuer is unknown. "));
 
920
 
 
921
      if (status & GNUTLS_CERT_SIGNER_NOT_CA)
 
922
         _gnutls_buffer_append_str (&str, _("The certificate issuer is not a CA. "));
 
923
      }
 
924
    else if (type == GNUTLS_CRT_OPENPGP)
 
925
      {
 
926
        _gnutls_buffer_append_str (&str, _("The certificate is not trusted. "));
 
927
 
 
928
        if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
 
929
          _gnutls_buffer_append_str (&str, _("Could not find a signer of the certificate. "));
 
930
 
 
931
        if (status & GNUTLS_CERT_REVOKED)
 
932
          _gnutls_buffer_append_str (&str, _("The certificate is revoked. "));
 
933
      }
 
934
 
 
935
  if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
 
936
    _gnutls_buffer_append_str (&str, _("The certificate chain uses insecure algorithm. "));
 
937
 
 
938
  if (status & GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE)
 
939
    _gnutls_buffer_append_str (&str, _("The certificate chain violates the signer's constraints. "));
 
940
 
 
941
  if (status & GNUTLS_CERT_NOT_ACTIVATED)
 
942
    _gnutls_buffer_append_str (&str, _("The certificate chain uses not yet valid certificate. "));
 
943
 
 
944
  if (status & GNUTLS_CERT_EXPIRED)
 
945
    _gnutls_buffer_append_str (&str, _("The certificate chain uses expired certificate. "));
 
946
 
 
947
  if (status & GNUTLS_CERT_SIGNATURE_FAILURE)
 
948
    _gnutls_buffer_append_str (&str, _("The signature in the certificate is invalid. "));
 
949
 
 
950
  if (status & GNUTLS_CERT_UNEXPECTED_OWNER)
 
951
    _gnutls_buffer_append_str (&str, _("The name in the certificate does not match the expected. "));
 
952
 
 
953
  ret = _gnutls_buffer_to_datum( &str, out);
 
954
  if (out->size > 0) out->size--;
 
955
      
 
956
  return ret;
 
957
}