Blame doc/examples/ex-verify-ssh.c

Packit 549fdc
/* This example code is placed in the public domain. */
Packit 549fdc
Packit 549fdc
#ifdef HAVE_CONFIG_H
Packit 549fdc
#include <config.h>
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
#include <stdio.h>
Packit 549fdc
#include <stdlib.h>
Packit 549fdc
#include <string.h>
Packit 549fdc
#include <gnutls/gnutls.h>
Packit 549fdc
#include <gnutls/x509.h>
Packit 549fdc
#include <assert.h>
Packit 549fdc
#include "examples.h"
Packit 549fdc
Packit 549fdc
#define CHECK(x) assert((x)>=0)
Packit 549fdc
Packit 549fdc
/* This function will verify the peer's certificate, check
Packit 549fdc
 * if the hostname matches. In addition it will perform an
Packit 549fdc
 * SSH-style authentication, where ultimately trusted keys
Packit 549fdc
 * are only the keys that have been seen before.
Packit 549fdc
 */
Packit 549fdc
int _ssh_verify_certificate_callback(gnutls_session_t session)
Packit 549fdc
{
Packit 549fdc
        unsigned int status;
Packit 549fdc
        const gnutls_datum_t *cert_list;
Packit 549fdc
        unsigned int cert_list_size;
Packit 549fdc
        int ret, type;
Packit 549fdc
        gnutls_datum_t out;
Packit 549fdc
        const char *hostname;
Packit 549fdc
Packit 549fdc
        /* read hostname */
Packit 549fdc
        hostname = gnutls_session_get_ptr(session);
Packit 549fdc
Packit 549fdc
        /* This verification function uses the trusted CAs in the credentials
Packit 549fdc
         * structure. So you must have installed one or more CA certificates.
Packit 549fdc
         */
Packit 549fdc
        CHECK(gnutls_certificate_verify_peers3(session, hostname, &status));
Packit 549fdc
Packit 549fdc
        type = gnutls_certificate_type_get(session);
Packit 549fdc
Packit 549fdc
        CHECK(gnutls_certificate_verification_status_print(status,
Packit 549fdc
                                                           type, &out, 0));
Packit 549fdc
        printf("%s", out.data);
Packit 549fdc
Packit 549fdc
        gnutls_free(out.data);
Packit 549fdc
Packit 549fdc
        if (status != 0)        /* Certificate is not trusted */
Packit 549fdc
                return GNUTLS_E_CERTIFICATE_ERROR;
Packit 549fdc
Packit 549fdc
        /* Do SSH verification */
Packit 549fdc
        cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
Packit 549fdc
        if (cert_list == NULL) {
Packit 549fdc
                printf("No certificate was found!\n");
Packit 549fdc
                return GNUTLS_E_CERTIFICATE_ERROR;
Packit 549fdc
        }
Packit 549fdc
Packit 549fdc
        /* service may be obtained alternatively using getservbyport() */
Packit 549fdc
        ret = gnutls_verify_stored_pubkey(NULL, NULL, hostname, "https",
Packit 549fdc
                                          type, &cert_list[0], 0);
Packit 549fdc
        if (ret == GNUTLS_E_NO_CERTIFICATE_FOUND) {
Packit 549fdc
                printf("Host %s is not known.", hostname);
Packit 549fdc
                if (status == 0)
Packit 549fdc
                        printf("Its certificate is valid for %s.\n",
Packit 549fdc
                               hostname);
Packit 549fdc
Packit 549fdc
                /* the certificate must be printed and user must be asked on
Packit 549fdc
                 * whether it is trustworthy. --see gnutls_x509_crt_print() */
Packit 549fdc
Packit 549fdc
                /* if not trusted */
Packit 549fdc
                return GNUTLS_E_CERTIFICATE_ERROR;
Packit 549fdc
        } else if (ret == GNUTLS_E_CERTIFICATE_KEY_MISMATCH) {
Packit 549fdc
                printf
Packit 549fdc
                    ("Warning: host %s is known but has another key associated.",
Packit 549fdc
                     hostname);
Packit 549fdc
                printf
Packit 549fdc
                    ("It might be that the server has multiple keys, or you are under attack\n");
Packit 549fdc
                if (status == 0)
Packit 549fdc
                        printf("Its certificate is valid for %s.\n",
Packit 549fdc
                               hostname);
Packit 549fdc
Packit 549fdc
                /* the certificate must be printed and user must be asked on
Packit 549fdc
                 * whether it is trustworthy. --see gnutls_x509_crt_print() */
Packit 549fdc
Packit 549fdc
                /* if not trusted */
Packit 549fdc
                return GNUTLS_E_CERTIFICATE_ERROR;
Packit 549fdc
        } else if (ret < 0) {
Packit 549fdc
                printf("gnutls_verify_stored_pubkey: %s\n",
Packit 549fdc
                       gnutls_strerror(ret));
Packit 549fdc
                return ret;
Packit 549fdc
        }
Packit 549fdc
Packit 549fdc
        /* user trusts the key -> store it */
Packit 549fdc
        if (ret != 0) {
Packit 549fdc
                CHECK(gnutls_store_pubkey(NULL, NULL, hostname, "https",
Packit 549fdc
                                          type, &cert_list[0], 0, 0));
Packit 549fdc
        }
Packit 549fdc
Packit 549fdc
        /* notify gnutls to continue handshake normally */
Packit 549fdc
        return 0;
Packit 549fdc
}