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

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