+static bool
+silc_verify_public_key_internal(SilcServer server, SilcSocketConnection sock,
+ SilcSocketType conn_type,
+ unsigned char *pk, SilcUInt32 pk_len,
+ SilcSKEPKType pk_type)
+{
+ char file[256], filename[256], *fingerprint;
+ struct stat st;
+
+ if (pk_type != SILC_SKE_PK_TYPE_SILC) {
+ SILC_LOG_WARNING(("We don't support %s (%s) port %d public key type %d",
+ sock->hostname, sock->ip, sock->port, pk_type));
+ return FALSE;
+ }
+
+ /* Accept client keys without verification */
+ if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
+ SILC_LOG_DEBUG(("Accepting client public key without verification"));
+ return TRUE;
+ }
+
+ /* XXX For now, accept server keys without verification too. We are
+ currently always doing mutual authentication so the proof of posession
+ of the private key is verified, and if server is authenticated in
+ conn auth protocol with public key we MUST have the key already. */
+ return TRUE;
+ /* Rest is unreachable code! */
+
+ memset(filename, 0, sizeof(filename));
+ memset(file, 0, sizeof(file));
+ snprintf(file, sizeof(file) - 1, "serverkey_%s_%d.pub", sock->hostname,
+ sock->port);
+ snprintf(filename, sizeof(filename) - 1, SILC_ETCDIR "/serverkeys/%s",
+ file);
+
+ /* Create serverkeys directory if it doesn't exist. */
+ if (stat(SILC_ETCDIR "/serverkeys", &st) < 0) {
+ /* If dir doesn't exist */
+ if (errno == ENOENT) {
+ if (mkdir(SILC_ETCDIR "/serverkeys", 0755) < 0) {
+ SILC_LOG_ERROR(("Couldn't create `%s' directory\n",
+ SILC_ETCDIR "/serverkeys"));
+ return TRUE;
+ }
+ } else {
+ SILC_LOG_ERROR(("%s\n", strerror(errno)));
+ return TRUE;
+ }
+ }
+
+ /* Take fingerprint of the public key */
+ fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
+ SILC_LOG_DEBUG(("Received server %s (%s) port %d public key (%s)",
+ sock->hostname, sock->ip, sock->port, fingerprint));
+ silc_free(fingerprint);
+
+ /* Check whether this key already exists */
+ if (stat(filename, &st) < 0) {
+ /* We don't have it, then cache it. */
+ SILC_LOG_DEBUG(("New public key from server"));
+
+ silc_pkcs_save_public_key_data(filename, pk, pk_len,
+ SILC_PKCS_FILE_PEM);
+ return TRUE;
+ } else {
+ /* The key already exists, verify it. */
+ SilcPublicKey public_key;
+ unsigned char *encpk;
+ SilcUInt32 encpk_len;
+
+ SILC_LOG_DEBUG(("We have the public key saved locally"));
+
+ /* Load the key file */
+ if (!silc_pkcs_load_public_key(filename, &public_key,
+ SILC_PKCS_FILE_PEM))
+ if (!silc_pkcs_load_public_key(filename, &public_key,
+ SILC_PKCS_FILE_BIN)) {
+ SILC_LOG_WARNING(("Could not load local copy of the %s (%s) port %d "
+ "server public key", sock->hostname, sock->ip,
+ sock->port));
+
+ /* Save the key for future checking */
+ unlink(filename);
+ silc_pkcs_save_public_key_data(filename, pk, pk_len,
+ SILC_PKCS_FILE_PEM);
+ return TRUE;
+ }
+
+ /* Encode the key data */
+ encpk = silc_pkcs_public_key_encode(public_key, &encpk_len);
+ if (!encpk) {
+ SILC_LOG_WARNING(("Local copy of the server %s (%s) port %d public key "
+ "is malformed", sock->hostname, sock->ip, sock->port));
+
+ /* Save the key for future checking */
+ unlink(filename);
+ silc_pkcs_save_public_key_data(filename, pk, pk_len, SILC_PKCS_FILE_PEM);
+ return TRUE;
+ }
+
+ if (memcmp(pk, encpk, encpk_len)) {
+ SILC_LOG_WARNING(("%s (%s) port %d server public key does not match "
+ "with local copy", sock->hostname, sock->ip,
+ sock->port));
+ SILC_LOG_WARNING(("It is possible that the key has expired or changed"));
+ SILC_LOG_WARNING(("It is also possible that some one is performing "
+ "man-in-the-middle attack"));
+ SILC_LOG_WARNING(("Will not accept the server %s (%s) port %d public "
+ "key",
+ sock->hostname, sock->ip, sock->port));
+ return FALSE;
+ }
+
+ /* Local copy matched */
+ return TRUE;
+ }
+}
+
+/* Callback that is called when we have received KE2 payload from
+ responder. We try to verify the public key now. */
+
+static void
+silc_server_protocol_ke_verify_key(SilcSKE ske,
+ unsigned char *pk_data,
+ SilcUInt32 pk_len,
+ SilcSKEPKType pk_type,
+ void *context,
+ SilcSKEVerifyCbCompletion completion,
+ void *completion_context)
+{
+ SilcProtocol protocol = (SilcProtocol)context;
+ SilcServerKEInternalContext *ctx =
+ (SilcServerKEInternalContext *)protocol->context;
+ SilcServer server = (SilcServer)ctx->server;
+
+ SILC_LOG_DEBUG(("Verifying received public key"));
+
+ if (silc_verify_public_key_internal(
+ server, ctx->sock,
+ (ctx->responder == FALSE ?
+ SILC_SOCKET_TYPE_ROUTER:
+ ctx->sconfig.ref_ptr ? SILC_SOCKET_TYPE_SERVER :
+ ctx->rconfig.ref_ptr ? SILC_SOCKET_TYPE_ROUTER :
+ SILC_SOCKET_TYPE_CLIENT),
+ pk_data, pk_len, pk_type))
+ completion(ske, SILC_SKE_STATUS_OK, completion_context);
+ else
+ completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
+ completion_context);
+}
+