ssh_auth.c
Go to the documentation of this file.
1 /**
2  * @file ssh_auth.c
3  * @brief SSH user authentication protocol
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSH Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SSH_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "ssh/ssh_transport.h"
37 #include "ssh/ssh_auth.h"
38 #include "ssh/ssh_auth_password.h"
40 #include "ssh/ssh_packet.h"
41 #include "ssh/ssh_misc.h"
42 #include "debug.h"
43 
44 //Check SSH stack configuration
45 #if (SSH_SUPPORT == ENABLED)
46 
47 //Supported authentication methods
48 static const char_t *const sshSupportedAuthMethods[] =
49 {
50 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
51  "publickey",
52 #endif
53 #if (SSH_PASSWORD_AUTH_SUPPORT == ENABLED)
54  "password",
55 #endif
56  "none"
57 };
58 
59 
60 /**
61  * @brief Send SSH_MSG_USERAUTH_BANNER message
62  * @param[in] connection Pointer to the SSH connection
63  * @param[in] banner NULL-terminated string containing the banner message
64  * @return Error code
65  **/
66 
68  const char_t *banner)
69 {
70  error_t error;
71  size_t length;
72  uint8_t *message;
73 
74  //Point to the buffer where to format the message
75  message = connection->buffer + SSH_PACKET_HEADER_SIZE;
76 
77  //Format SSH_MSG_USERAUTH_BANNER message
78  error = sshFormatUserAuthBanner(connection, banner, message, &length);
79 
80  //Check status code
81  if(!error)
82  {
83  //Debug message
84  TRACE_INFO("Sending SSH_MSG_USERAUTH_BANNER message (%" PRIuSIZE " bytes)...\r\n", length);
86 
87  //Send message
88  error = sshSendPacket(connection, message, length);
89  }
90 
91  //Check status code
92  if(!error)
93  {
94  //Wait for an SSH_MSG_USERAUTH_REQUEST message
95  connection->state = SSH_CONN_STATE_USER_AUTH_REQUEST;
96  }
97 
98  //Return status code
99  return error;
100 }
101 
102 
103 /**
104  * @brief Send SSH_MSG_USERAUTH_REQUEST message
105  * @param[in] connection Pointer to the SSH connection
106  * @return Error code
107  **/
108 
110 {
111  error_t error;
112  size_t length;
113  uint8_t *message;
114 
115 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
116  //First authentication request?
117  if(connection->hostKeyIndex < 0)
118  {
119  //Select the first host key to use
120  sshSelectNextHostKey(connection);
121  //The client first verifies whether the key is acceptable
122  connection->publicKeyOk = FALSE;
123  }
124 #endif
125 
126  //Point to the buffer where to format the message
127  message = connection->buffer + SSH_PACKET_HEADER_SIZE;
128 
129  //Format SSH_MSG_USERAUTH_REQUEST message
130  error = sshFormatUserAuthRequest(connection, message, &length);
131 
132  //Check status code
133  if(!error)
134  {
135  //Debug message
136  TRACE_INFO("Sending SSH_MSG_USERAUTH_REQUEST message (%" PRIuSIZE " bytes)...\r\n", length);
138 
139  //Send message
140  error = sshSendPacket(connection, message, length);
141  }
142 
143  //Check status code
144  if(!error)
145  {
146  //Increment the number of authentication attempts
147  connection->authAttempts++;
148 
149 #if (SSH_EXT_INFO_SUPPORT == ENABLED)
150  //The server may send the SSH_MSG_EXT_INFO message immediately preceding
151  //the server's SSH_MSG_USERAUTH_SUCCESS message
152  connection->state = SSH_CONN_STATE_SERVER_EXT_INFO_2;
153 #else
154  //Wait for an SSH_MSG_USERAUTH_SUCCESS or SSH_MSG_USERAUTH_FAILURE
155  //message
156  connection->state = SSH_CONN_STATE_USER_AUTH_REPLY;
157 #endif
158  }
159 
160  //Return status code
161  return error;
162 }
163 
164 
165 /**
166  * @brief Send SSH_MSG_USERAUTH_SUCCESS message
167  * @param[in] connection Pointer to the SSH connection
168  * @return Error code
169  **/
170 
172 {
173  error_t error;
174  size_t length;
175  uint8_t *message;
176 
177  //Point to the buffer where to format the message
178  message = connection->buffer + SSH_PACKET_HEADER_SIZE;
179 
180  //Format SSH_MSG_USERAUTH_SUCCESS message
181  error = sshFormatUserAuthSuccess(connection, message, &length);
182 
183  //Check status code
184  if(!error)
185  {
186  //Debug message
187  TRACE_INFO("Sending SSH_MSG_USERAUTH_SUCCESS message (%" PRIuSIZE " bytes)...\r\n", length);
189 
190  //Send message
191  error = sshSendPacket(connection, message, length);
192  }
193 
194  //Check status code
195  if(!error)
196  {
197  //Either party may later initiate a key re-exchange by sending a
198  //SSH_MSG_KEXINIT message
199  connection->kexInitSent = FALSE;
200  connection->kexInitReceived = FALSE;
201 
202  //Any non-authentication messages sent by the client after the request
203  //that resulted in SSH_MSG_USERAUTH_SUCCESS being sent must be passed
204  //to the service being run on top of this protocol
205  connection->state = SSH_CONN_STATE_OPEN;
206  }
207 
208  //Return status code
209  return error;
210 }
211 
212 
213 /**
214  * @brief Send SSH_MSG_USERAUTH_FAILURE message
215  * @param[in] connection Pointer to the SSH connection
216  * @return Error code
217  **/
218 
220 {
221  error_t error;
222  size_t length;
223  uint8_t *message;
224 
225  //Point to the buffer where to format the message
226  message = connection->buffer + SSH_PACKET_HEADER_SIZE;
227 
228  //Format SSH_MSG_USERAUTH_FAILURE message
229  error = sshFormatUserAuthFailure(connection, message, &length);
230 
231  //Check status code
232  if(!error)
233  {
234  //Debug message
235  TRACE_INFO("Sending SSH_MSG_USERAUTH_FAILURE message (%" PRIuSIZE " bytes)...\r\n", length);
237 
238  //Send message
239  error = sshSendPacket(connection, message, length);
240  }
241 
242  //Check status code
243  if(!error)
244  {
245  //The client may send several authentication requests without waiting
246  //for responses from previous requests. The server must process each
247  //request completely and acknowledge any failed requests with a
248  //SSH_MSG_USERAUTH_FAILURE message before processing the next request
249  connection->state = SSH_CONN_STATE_USER_AUTH_REQUEST;
250  }
251 
252  //Return status code
253  return error;
254 }
255 
256 
257 /**
258  * @brief Accept client's authentication request
259  * @param[in] connection Pointer to the SSH connection
260  * @return Error code
261  **/
262 
264 {
265 #if (SSH_EXT_INFO_SUPPORT == ENABLED)
266  //If the server receives an "ext-info-c", it may send an SSH_MSG_EXT_INFO
267  //message (refer to RFC 8308, section 2.1)
268  if(connection->extInfoReceived)
269  {
270  //The server may send the SSH_MSG_EXT_INFO message immediately preceding
271  //the server's SSH_MSG_USERAUTH_SUCCESS message
272  connection->state = SSH_CONN_STATE_SERVER_EXT_INFO_2;
273  }
274  else
275 #endif
276  {
277  //When the server accepts authentication, it must respond with an
278  //SSH_MSG_USERAUTH_SUCCESS message
279  connection->state = SSH_CONN_STATE_USER_AUTH_SUCCESS;
280  }
281 
282  //Successful processing
283  return NO_ERROR;
284 }
285 
286 
287 /**
288  * @brief Reject client's authentication request
289  * @param[in] connection Pointer to the SSH connection
290  * @return Error code
291  **/
292 
294 {
295  error_t error;
296 
297  //The implementation should limit the number of failed authentication
298  //attempts a client may perform in a single session
299  if(connection->authAttempts <= SSH_MAX_AUTH_ATTEMPTS)
300  {
301  //If the server rejects the authentication request, it must respond with
302  //an SSH_MSG_USERAUTH_FAILURE message
303  error = sshSendUserAuthFailure(connection);
304  }
305  else
306  {
307  //If the threshold is exceeded, the server should disconnect (refer to
308  //RFC 4252, section 4)
310  "Too many authentication attempts");
311  }
312 
313  //Return status code
314  return error;
315 }
316 
317 
318 /**
319  * @brief Format SSH_MSG_USERAUTH_BANNER message
320  * @param[in] connection Pointer to the SSH connection
321  * @param[in] banner NULL-terminated string containing the banner message
322  * @param[out] p Buffer where to format the message
323  * @param[out] length Length of the resulting message, in bytes
324  * @return Error code
325  **/
326 
328  const char_t *banner, uint8_t *p, size_t *length)
329 {
330  error_t error;
331  size_t n;
332 
333  //Total length of the message
334  *length = 0;
335 
336  //Set message type
338 
339  //Point to the first field of the message
340  p += sizeof(uint8_t);
341  *length += sizeof(uint8_t);
342 
343  //The message field contains the banner to be displayed before issuing
344  //a login prompt
345  error = sshFormatString(banner, p, &n);
346  //Any error to report?
347  if(error)
348  return error;
349 
350  //Point to the next field
351  p += n;
352  *length += n;
353 
354  //Format the language tag
355  error = sshFormatString("en", p, &n);
356  //Any error to report?
357  if(error)
358  return error;
359 
360  //Total length of the message
361  *length += n;
362 
363  //Successful processing
364  return NO_ERROR;
365 }
366 
367 
368 /**
369  * @brief Format SSH_MSG_USERAUTH_REQUEST message
370  * @param[in] connection Pointer to the SSH connection
371  * @param[out] message Buffer where to format the message
372  * @param[out] length Length of the resulting message, in bytes
373  * @return Error code
374  **/
375 
377  size_t *length)
378 {
379 #if (SSH_CLIENT_SUPPORT == ENABLED)
380  error_t error;
381  size_t n;
382  uint8_t *p;
383 
384  //Point to the buffer where to format the SSH message
385  p = message;
386  //Total length of the message
387  *length = 0;
388 
389  //Set message type
391 
392  //Point to the first field of the message
393  p += sizeof(uint8_t);
394  *length += sizeof(uint8_t);
395 
396  //Format user name
397  error = sshFormatString(connection->context->username, p, &n);
398  //Any error to report?
399  if(error)
400  return error;
401 
402  //Point to the next field
403  p += n;
404  *length += n;
405 
406  //Format service name
407  error = sshFormatString("ssh-connection", p, &n);
408  //Any error to report?
409  if(error)
410  return error;
411 
412  //Point to the next field
413  p += n;
414  *length += n;
415 
416 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
417  //Public key authentication?
419  {
420  //Retrieve the length of the message to be signed
421  n = *length;
422 
423  //Format "publickey" method specific fields
424  error = sshFormatPublicKeyAuthParams(connection, message, n, p, &n);
425  }
426  else
427 #endif
428 #if (SSH_PASSWORD_AUTH_SUPPORT == ENABLED)
429  //Password authentication?
430  if(sshGetAuthMethod(connection) == SSH_AUTH_METHOD_PASSWORD)
431  {
432  //Format "password" method specific fields
433  error = sshFormatPasswordAuthParams(connection, p, &n);
434  }
435  else
436 #endif
437  //No authentication?
438  {
439  //A client may request a list of authentication methods that may continue
440  //by using the "none" authentication. If no authentication is needed for
441  //the user, the server must return SSH_MSG_USERAUTH_SUCCESS. Otherwise,
442  //the server must return SSH_MSG_USERAUTH_FAILURE
443  error = sshFormatNoneAuthParams(connection, p, &n);
444  }
445 
446  //Check status code
447  if(!error)
448  {
449  //Total length of the message
450  *length += n;
451  }
452 
453  //Return status code
454  return error;
455 #else
456  //Client operation mode is not implemented
457  return ERROR_NOT_IMPLEMENTED;
458 #endif
459 }
460 
461 
462 /**
463  * @brief Format "none" method specific fields
464  * @param[in] connection Pointer to the SSH connection
465  * @param[out] p Output stream where to write the method specific fields
466  * @param[out] written Total number of bytes that have been written
467  * @return Error code
468  **/
469 
471  size_t *written)
472 {
473  error_t error;
474 
475  //Format method name
476  error = sshFormatString("none", p, written);
477  //Any error to report?
478  if(error)
479  return error;
480 
481  //A "none" authentication request does not include any method specific fields
482  return NO_ERROR;
483 }
484 
485 
486 /**
487  * @brief Format SSH_MSG_USERAUTH_SUCCESS message
488  * @param[in] connection Pointer to the SSH connection
489  * @param[out] p Buffer where to format the message
490  * @param[out] length Length of the resulting message, in bytes
491  * @return Error code
492  **/
493 
495  size_t *length)
496 {
497  //The SSH_MSG_USERAUTH_SUCCESS message consists of a single byte
499 
500  //Total length of the message
501  *length = sizeof(uint8_t);
502 
503  //Successful processing
504  return NO_ERROR;
505 }
506 
507 
508 /**
509  * @brief Format SSH_MSG_USERAUTH_FAILURE message
510  * @param[in] connection Pointer to the SSH connection
511  * @param[out] p Buffer where to format the message
512  * @param[out] length Length of the resulting message, in bytes
513  * @return Error code
514  **/
515 
517  size_t *length)
518 {
519  error_t error;
520  size_t n;
521 
522  //Total length of the message
523  *length = 0;
524 
525  //Set message type
527 
528  //Point to the first field of the message
529  p += sizeof(uint8_t);
530  *length += sizeof(uint8_t);
531 
532  //Format the list of authentication methods that can continue
533  error = sshFormatUserAuthMethods(connection, p, &n);
534  //Any error to report?
535  if(error)
536  return error;
537 
538  //Point to the next field
539  p += n;
540  *length += n;
541 
542  //The value of partial success must be TRUE if the authentication request to
543  //which this is a response was successful. It must be FALSE if the request
544  //was not successfully processed
545  p[0] = FALSE;
546 
547  //Total length of the message
548  *length += sizeof(uint8_t);
549 
550  //Successful processing
551  return NO_ERROR;
552 }
553 
554 
555 /**
556  * @brief Format the list of allowed authentication methods
557  * @param[in] connection Pointer to the SSH connection
558  * @param[out] p Output stream where to write the name-list
559  * @param[out] written Total number of bytes that have been written
560  * @return Error code
561  **/
562 
564  size_t *written)
565 {
566  uint_t i;
567  size_t n;
568  bool_t acceptable;
569 
570  //A name-list is represented as a uint32 containing its length followed by
571  //a comma-separated list of zero or more names
572  n = sizeof(uint32_t);
573 
574  //Loop through the list of authentication methods
575  for(i = 0; i < arraysize(sshSupportedAuthMethods); i++)
576  {
577 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
578  //Public key authentication method?
579  if(sshCompareAlgo(sshSupportedAuthMethods[i], "publickey") &&
580  connection->context->publicKeyAuthCallback != NULL)
581  {
582  //The "publickey" authentication method is supported by the server
583  acceptable = TRUE;
584  }
585  else
586 #endif
587 #if (SSH_PASSWORD_AUTH_SUPPORT == ENABLED)
588  //Password authentication method?
589  if(sshCompareAlgo(sshSupportedAuthMethods[i], "password") &&
590  connection->context->passwordAuthCallback != NULL)
591  {
592  //The "password" authentication method is supported by the server
593  acceptable = TRUE;
594  }
595  else
596 #endif
597  //Unknown authentication method?
598  {
599  //The "none" method name must not be listed as supported by the server
600  acceptable = FALSE;
601  }
602 
603  //It is recommended that servers only include the method name values in
604  //the name-list that are actually useful (refer to RFC 4252, section 5.1)
605  if(acceptable)
606  {
607  //Method names are separated by commas
608  if(n != sizeof(uint32_t))
609  {
610  p[n++] = ',';
611  }
612 
613  //A name must have a non-zero length and it must not contain a comma
614  osStrcpy((char_t *) p + n, sshSupportedAuthMethods[i]);
615 
616  //Update the length of the name list
617  n += osStrlen(sshSupportedAuthMethods[i]);
618  }
619  }
620 
621  //The name list is preceded by a uint32 containing its length
622  STORE32BE(n - sizeof(uint32_t), p);
623 
624  //Total number of bytes that have been written
625  *written = n;
626 
627  //Successfull processing
628  return NO_ERROR;
629 }
630 
631 
632 /**
633  * @brief Parse SSH_MSG_USERAUTH_BANNER message
634  * @param[in] connection Pointer to the SSH connection
635  * @param[in] message Pointer to message
636  * @param[in] length Length of the message, in bytes
637  * @return Error code
638  **/
639 
641  const uint8_t *message, size_t length)
642 {
643 #if (SSH_CLIENT_SUPPORT == ENABLED)
644  error_t error;
645  const uint8_t *p;
646  SshString banner;
647  SshString languageTag;
648 
649  //Debug message
650  TRACE_INFO("SSH_MSG_USERAUTH_BANNER message received (%" PRIuSIZE " bytes)...\r\n", length);
652 
653  //Check operation mode
654  if(connection->context->mode != SSH_OPERATION_MODE_CLIENT)
656 
657  //The SSH server may send an SSH_MSG_USERAUTH_BANNER message at any time
658  //after this authentication protocol starts and before authentication is
659  //successful (refer to RFC 4252, section 5.4)
660  if(connection->state != SSH_CONN_STATE_SERVER_EXT_INFO_2 &&
661  connection->state != SSH_CONN_STATE_USER_AUTH_REPLY)
662  {
664  }
665 
666  //Sanity check
667  if(length < sizeof(uint8_t))
668  return ERROR_INVALID_MESSAGE;
669 
670  //Point to the first field of the message
671  p = message + sizeof(uint8_t);
672  //Remaining bytes to process
673  length -= sizeof(uint8_t);
674 
675  //The message field contains the text to be displayed to the client user
676  //before authentication is attempted
677  error = sshParseString(p, length, &banner);
678  //Any error to report?
679  if(error)
680  return error;
681 
682  //Point to the next field
683  p += sizeof(uint32_t) + banner.length;
684  length -= sizeof(uint32_t) + banner.length;
685 
686  //Decode language tag
687  error = sshParseString(p, length, &languageTag);
688  //Any error to report?
689  if(error)
690  return error;
691 
692  //Point to the next field
693  p += sizeof(uint32_t) + languageTag.length;
694  length -= sizeof(uint32_t) + languageTag.length;
695 
696  //Malformed message?
697  if(length != 0)
698  return ERROR_INVALID_MESSAGE;
699 
700  //Successful processing
701  return NO_ERROR;
702 #else
703  //Client operation mode is not implemented
705 #endif
706 }
707 
708 
709 /**
710  * @brief Parse SSH_MSG_USERAUTH_REQUEST message
711  * @param[in] connection Pointer to the SSH connection
712  * @param[in] message Pointer to message
713  * @param[in] length Length of the message, in bytes
714  * @return Error code
715  **/
716 
718  const uint8_t *message, size_t length)
719 {
720 #if (SSH_SERVER_SUPPORT == ENABLED)
721  error_t error;
722  const uint8_t *p;
723  SshString userName;
724  SshString serviceName;
725  SshString methodName;
726 
727  //Debug message
728  TRACE_INFO("SSH_MSG_USERAUTH_REQUEST message received (%" PRIuSIZE " bytes)...\r\n", length);
730 
731  //Check operation mode
732  if(connection->context->mode != SSH_OPERATION_MODE_SERVER)
734 
735  //Check connection state
736  if(connection->state == SSH_CONN_STATE_USER_AUTH_REQUEST)
737  {
738  //The client may send several authentication requests
739  }
740  else if(connection->state == SSH_CONN_STATE_OPEN)
741  {
742  //The SSH_MSG_USERAUTH_SUCCESS message must be sent only once. When
743  //SSH_MSG_USERAUTH_SUCCESS has been sent, any further authentication
744  //requests received after that should be silently ignored
745  return NO_ERROR;
746  }
747  else
748  {
749  //Report an error
751  }
752 
753  //Sanity check
754  if(length < sizeof(uint8_t))
755  return ERROR_INVALID_MESSAGE;
756 
757  //Point to the first field of the message
758  p = message + sizeof(uint8_t);
759  //Remaining bytes to process
760  length -= sizeof(uint8_t);
761 
762  //Decode user name
763  error = sshParseString(p, length, &userName);
764  //Any error to report?
765  if(error)
766  return error;
767 
768  //Point to the next field
769  p += sizeof(uint32_t) + userName.length;
770  length -= sizeof(uint32_t) + userName.length;
771 
772  //Decode service name
773  error = sshParseString(p, length, &serviceName);
774  //Any error to report?
775  if(error)
776  return error;
777 
778  //Point to the next field
779  p += sizeof(uint32_t) + serviceName.length;
780  length -= sizeof(uint32_t) + serviceName.length;
781 
782  //Decode method name
783  error = sshParseString(p, length, &methodName);
784  //Any error to report?
785  if(error)
786  return error;
787 
788  //Point to the method specific fields
789  p += sizeof(uint32_t) + methodName.length;
790  length -= sizeof(uint32_t) + methodName.length;
791 
792  //Increment the number of authentication attempts
793  connection->authAttempts++;
794 
795  //The service name specifies the service to start after authentication
796  if(sshCompareString(&serviceName, "ssh-connection"))
797  {
798  //Empty authentication request?
799  if(sshCompareString(&methodName, "none"))
800  {
801  //A client may request a list of authentication method name values
802  //that may continue by using the "none" authentication method name
803  error = sshParseNoneAuthParams(connection, &userName, p, length);
804  }
805 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
806  //Public key authentication request?
807  else if(sshCompareString(&methodName, "publickey"))
808  {
809  //Parse "publickey" method specific fields
810  error = sshParsePublicKeyAuthParams(connection, &userName, message,
811  p, length);
812  }
813 #endif
814 #if (SSH_PASSWORD_AUTH_SUPPORT == ENABLED)
815  //Password authentication request?
816  else if(sshCompareString(&methodName, "password"))
817  {
818  //Parse "password" method specific fields
819  error = sshParsePasswordAuthParams(connection, &userName, p, length);
820  }
821 #endif
822  //Unknown authentication request?
823  else
824  {
825  //The server must return an SSH_MSG_USERAUTH_FAILURE message and may
826  //return with it a list of authentication methods that may continue
827  error = sshRejectAuthRequest(connection);
828  }
829  }
830  else
831  {
832  //If the requested service is not available, the server may disconnect
833  //immediately. Sending a proper SSH_MSG_DISCONNECT message is recommended.
834  //In any case, if the service does not exist, authentication must not be
835  //accepted (refer to RFC 4252, section 5)
837  "Service not available");
838  }
839 
840  //Return status code
841  return error;
842 #else
843  //Server operation mode is not implemented
845 #endif
846 }
847 
848 
849 /**
850  * @brief Parse "none" method specific fields
851  * @param[in] connection Pointer to the SSH connection
852  * @param[in] userName Pointer to the user name
853  * @param[in] p Pointer to method specific fields
854  * @param[in] length Length of the method specific fields, in bytes
855  * @return Error code
856  **/
857 
859  const SshString *userName, const uint8_t *p, size_t length)
860 {
861  error_t error;
862  SshAuthStatus status;
863  SshContext *context;
864 
865  //Point to the SSH context
866  context = connection->context;
867 
868  //Malformed message?
869  if(length != 0)
870  return ERROR_INVALID_MESSAGE;
871 
872  //Check the length of the user name
873  if(userName->length <= SSH_MAX_USERNAME_LEN)
874  {
875  //Save user name
876  osMemcpy(connection->user, userName->value, userName->length);
877  //Properly terminate the command line with a NULL character
878  connection->user[userName->length] = '\0';
879 
880 #if (SSH_PASSWORD_AUTH_SUPPORT == ENABLED)
881  //The server must always reject this request, unless the client is to be
882  //granted access without any authentication (refer to RFC 4552, section 4)
883  if(context->passwordAuthCallback != NULL)
884  {
885  //Manage authentication policy
886  status = context->passwordAuthCallback(connection, connection->user,
887  "", 0);
888  }
889  else
890 #endif
891  {
892  //Access is denied
893  status = SSH_AUTH_STATUS_FAILURE;
894  }
895  }
896  else
897  {
898  //Access is denied
899  status = SSH_AUTH_STATUS_FAILURE;
900  }
901 
902  //Check whether access is granted to the user
903  if(status == SSH_AUTH_STATUS_SUCCESS)
904  {
905  //If no authentication is needed for the user, the server must return
906  //an SSH_MSG_USERAUTH_SUCCESS message
907  error = sshAcceptAuthRequest(connection);
908  }
909  else
910  {
911  //Otherwise, the server must return an SSH_MSG_USERAUTH_FAILURE message
912  //and may return with it a list of methods that may continue
913  error = sshRejectAuthRequest(connection);
914  }
915 
916  //Return status code
917  return error;
918 }
919 
920 
921 /**
922  * @brief Parse SSH_MSG_USERAUTH_SUCCESS message
923  * @param[in] connection Pointer to the SSH connection
924  * @param[in] message Pointer to message
925  * @param[in] length Length of the message, in bytes
926  * @return Error code
927  **/
928 
930  const uint8_t *message, size_t length)
931 {
932 #if (SSH_CLIENT_SUPPORT == ENABLED)
933  //Debug message
934  TRACE_INFO("SSH_MSG_USERAUTH_SUCCESS message received (%" PRIuSIZE " bytes)...\r\n", length);
936 
937  //Check operation mode
938  if(connection->context->mode != SSH_OPERATION_MODE_CLIENT)
940 
941  //Check connection state
942  if(connection->state != SSH_CONN_STATE_SERVER_EXT_INFO_2 &&
943  connection->state != SSH_CONN_STATE_USER_AUTH_REPLY &&
944  connection->state != SSH_CONN_STATE_USER_AUTH_SUCCESS)
945  {
947  }
948 
949  //Malformed message?
950  if(length != sizeof(uint8_t))
951  return ERROR_INVALID_MESSAGE;
952 
953  //Either party may later initiate a key re-exchange by sending a
954  //SSH_MSG_KEXINIT message
955  connection->kexInitSent = FALSE;
956  connection->kexInitReceived = FALSE;
957 
958  //Any non-authentication messages sent by the client after the request
959  //that resulted in SSH_MSG_USERAUTH_SUCCESS being sent must be passed
960  //to the service being run on top of this protocol
961  connection->state = SSH_CONN_STATE_OPEN;
962 
963  //Successful processing
964  return NO_ERROR;
965 #else
966  //Client operation mode is not implemented
968 #endif
969 }
970 
971 
972 /**
973  * @brief Parse SSH_MSG_USERAUTH_FAILURE message
974  * @param[in] connection Pointer to the SSH connection
975  * @param[in] message Pointer to message
976  * @param[in] length Length of the message, in bytes
977  * @return Error code
978  **/
979 
981  const uint8_t *message, size_t length)
982 {
983 #if (SSH_CLIENT_SUPPORT == ENABLED)
984  error_t error;
985  const uint8_t *p;
986  SshNameList authList;
987  SshBoolean partialSuccess;
988 
989  //Debug message
990  TRACE_INFO("SSH_MSG_USERAUTH_FAILURE message received (%" PRIuSIZE " bytes)...\r\n", length);
992 
993  //Check operation mode
994  if(connection->context->mode != SSH_OPERATION_MODE_CLIENT)
996 
997  //Check connection state
998  if(connection->state != SSH_CONN_STATE_SERVER_EXT_INFO_2 &&
999  connection->state != SSH_CONN_STATE_USER_AUTH_REPLY)
1000  {
1001  return ERROR_UNEXPECTED_MESSAGE;
1002  }
1003 
1004  //Sanity check
1005  if(length < sizeof(uint8_t))
1006  return ERROR_INVALID_MESSAGE;
1007 
1008  //Point to the first field of the message
1009  p = message + sizeof(uint8_t);
1010  //Remaining bytes to process
1011  length -= sizeof(uint8_t);
1012 
1013  //Decode the list of authentications
1014  error = sshParseNameList(p, length, &authList);
1015  //Any error to report?
1016  if(error)
1017  return error;
1018 
1019  //Point to the next field
1020  p += sizeof(uint32_t) + authList.length;
1021  length -= sizeof(uint32_t) + authList.length;
1022 
1023  //Malformed message?
1024  if(length != sizeof(uint8_t))
1025  return ERROR_INVALID_MESSAGE;
1026 
1027  //The value of partial success must be TRUE if the authentication request to
1028  //which this is a response was successful. It must be FALSE if the request
1029  //was not successfully processed
1030  partialSuccess = p[0];
1031 
1032  //The value of this field is not used
1033  (void) partialSuccess;
1034 
1035 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
1036  //Public key authentication method?
1037  if(sshGetAuthMethod(connection) == SSH_AUTH_METHOD_PUBLIC_KEY)
1038  {
1039  //Select the next host key to use
1040  sshSelectNextHostKey(connection);
1041  //The client first verifies whether the key is acceptable
1042  connection->publicKeyOk = FALSE;
1043 
1044  //The client may send several authentication requests
1045  connection->state = SSH_CONN_STATE_USER_AUTH_REQUEST;
1046 
1047  //Continue processing
1048  error = NO_ERROR;
1049  }
1050  else
1051 #endif
1052  //Password authentication method?
1053  {
1054  //The server has rejected the authentication request
1056  }
1057 
1058  //Return status code
1059  return error;
1060 #else
1061  //Client operation mode is not implemented
1062  return ERROR_UNEXPECTED_MESSAGE;
1063 #endif
1064 }
1065 
1066 
1067 /**
1068  * @brief Parse authentication method specific method messages
1069  * @param[in] connection Pointer to the SSH connection
1070  * @param[in] type SSH message type
1071  * @param[in] message Pointer to message
1072  * @param[in] length Length of the message, in bytes
1073  * @return Error code
1074  **/
1075 
1077  const uint8_t *message, size_t length)
1078 {
1079  error_t error;
1080 
1081  //Client operation mode?
1082  if(connection->context->mode == SSH_OPERATION_MODE_CLIENT)
1083  {
1084 #if (SSH_PUBLIC_KEY_AUTH_SUPPORT == ENABLED)
1085  //Different authentication methods reuse the same message numbers
1086  if(sshGetAuthMethod(connection) == SSH_AUTH_METHOD_PUBLIC_KEY)
1087  {
1088  //Check message type
1090  {
1091  //The server must respond to the SSH_MSG_USERAUTH_REQUEST message
1092  //with either SSH_MSG_USERAUTH_FAILURE or SSH_MSG_USERAUTH_PK_OK
1093  error = sshParseUserAuthPkOk(connection, message, length);
1094  }
1095  else
1096  {
1097  //Unknown message type
1098  error = ERROR_INVALID_TYPE;
1099  }
1100  }
1101  else
1102 #endif
1103 #if (SSH_PASSWORD_AUTH_SUPPORT == ENABLED)
1104  //Password authentication?
1105  if(sshGetAuthMethod(connection) == SSH_AUTH_METHOD_PASSWORD)
1106  {
1107  //Check message type
1109  {
1110  //Normally, the server responds to the SSH_MSG_USERAUTH_REQUEST
1111  //message with success or failure. However, if the password has
1112  //expired, the server should indicate this by responding with
1113  //SSH_MSG_USERAUTH_PASSWD_CHANGEREQ
1114  error = sshParseUserAuthPasswdChangeReq(connection, message,
1115  length);
1116  }
1117  else
1118  {
1119  //Unknown message type
1120  error = ERROR_INVALID_TYPE;
1121  }
1122  }
1123  else
1124 #endif
1125  //No authentication?
1126  {
1127  //Unknown message type
1128  error = ERROR_INVALID_TYPE;
1129  }
1130  }
1131  else
1132  {
1133  //Method-specific messages are only sent by the server
1134  error = ERROR_UNEXPECTED_MESSAGE;
1135  }
1136 
1137  //Return status code
1138  return error;
1139 }
1140 
1141 
1142 /**
1143  * @brief Get current authentication method
1144  * @return Authentication method (password or public key authentication)
1145  **/
1146 
1148 {
1149 #if (SSH_CLIENT_SUPPORT == ENABLED)
1151  SshContext *context;
1152 
1153  //Point to the SSH context
1154  context = connection->context;
1155 
1156  //Public key or password authentication method?
1157  if(connection->hostKeyIndex >= 0 &&
1158  connection->hostKeyIndex < SSH_MAX_HOST_KEYS)
1159  {
1161  }
1162  else if(context->password[0] != '\0')
1163  {
1165  }
1166  else
1167  {
1169  }
1170 
1171  //Return the current authentication method
1172  return authMethod;
1173 #else
1174  //Client operation mode is not implemented
1175  return SSH_AUTH_METHOD_NONE;
1176 #endif
1177 }
1178 
1179 #endif
uint8_t message[]
Definition: chap.h:154
uint8_t type
Definition: coap_common.h:176
unsigned int uint_t
Definition: compiler_port.h:50
#define PRIuSIZE
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
Debugging facilities.
#define TRACE_INFO(...)
Definition: debug.h:95
#define TRACE_VERBOSE_ARRAY(p, a, n)
Definition: debug.h:125
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_TYPE
Definition: error.h:115
@ ERROR_INVALID_MESSAGE
Definition: error.h:105
@ ERROR_AUTHENTICATION_FAILED
Definition: error.h:69
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:194
uint8_t authMethod
Definition: ike.h:1400
uint8_t p
Definition: ndp.h:300
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define osStrlen(s)
Definition: os_port.h:165
#define arraysize(a)
Definition: os_port.h:71
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
#define osStrcpy(s1, s2)
Definition: os_port.h:207
Secure Shell (SSH)
@ SSH_CONN_STATE_OPEN
Definition: ssh.h:1071
@ SSH_CONN_STATE_USER_AUTH_REQUEST
Definition: ssh.h:1068
@ SSH_CONN_STATE_SERVER_EXT_INFO_2
Definition: ssh.h:1064
@ SSH_CONN_STATE_USER_AUTH_REPLY
Definition: ssh.h:1069
@ SSH_CONN_STATE_USER_AUTH_SUCCESS
Definition: ssh.h:1070
#define SSH_MAX_HOST_KEYS
Definition: ssh.h:178
#define SSH_MAX_USERNAME_LEN
Definition: ssh.h:255
#define SSH_MAX_AUTH_ATTEMPTS
Definition: ssh.h:227
@ SSH_OPERATION_MODE_SERVER
Definition: ssh.h:902
@ SSH_OPERATION_MODE_CLIENT
Definition: ssh.h:901
#define SshConnection
Definition: ssh.h:883
#define SshContext
Definition: ssh.h:879
@ SSH_DISCONNECT_SERVICE_NOT_AVAILABLE
Definition: ssh.h:1011
@ SSH_DISCONNECT_BY_APPLICATION
Definition: ssh.h:1015
SshAuthStatus
Authentication status.
Definition: ssh.h:911
@ SSH_AUTH_STATUS_FAILURE
Definition: ssh.h:912
@ SSH_AUTH_STATUS_SUCCESS
Definition: ssh.h:913
@ SSH_MSG_USERAUTH_REQUEST
Definition: ssh.h:970
@ SSH_MSG_USERAUTH_BANNER
Definition: ssh.h:973
@ SSH_MSG_USERAUTH_FAILURE
Definition: ssh.h:971
@ SSH_MSG_USERAUTH_PK_OK
Definition: ssh.h:976
@ SSH_MSG_USERAUTH_PASSWD_CHANGEREQ
Definition: ssh.h:977
@ SSH_MSG_USERAUTH_SUCCESS
Definition: ssh.h:972
error_t sshParseUserAuthSuccess(SshConnection *connection, const uint8_t *message, size_t length)
Parse SSH_MSG_USERAUTH_SUCCESS message.
Definition: ssh_auth.c:929
error_t sshFormatUserAuthSuccess(SshConnection *connection, uint8_t *p, size_t *length)
Format SSH_MSG_USERAUTH_SUCCESS message.
Definition: ssh_auth.c:494
SshAuthMethod sshGetAuthMethod(SshConnection *connection)
Get current authentication method.
Definition: ssh_auth.c:1147
error_t sshSendUserAuthSuccess(SshConnection *connection)
Send SSH_MSG_USERAUTH_SUCCESS message.
Definition: ssh_auth.c:171
error_t sshFormatUserAuthRequest(SshConnection *connection, uint8_t *message, size_t *length)
Format SSH_MSG_USERAUTH_REQUEST message.
Definition: ssh_auth.c:376
error_t sshSendUserAuthRequest(SshConnection *connection)
Send SSH_MSG_USERAUTH_REQUEST message.
Definition: ssh_auth.c:109
error_t sshParseUserAuthFailure(SshConnection *connection, const uint8_t *message, size_t length)
Parse SSH_MSG_USERAUTH_FAILURE message.
Definition: ssh_auth.c:980
error_t sshParseUserAuthBanner(SshConnection *connection, const uint8_t *message, size_t length)
Parse SSH_MSG_USERAUTH_BANNER message.
Definition: ssh_auth.c:640
error_t sshFormatNoneAuthParams(SshConnection *connection, uint8_t *p, size_t *written)
Format "none" method specific fields.
Definition: ssh_auth.c:470
error_t sshSendUserAuthFailure(SshConnection *connection)
Send SSH_MSG_USERAUTH_FAILURE message.
Definition: ssh_auth.c:219
error_t sshFormatUserAuthFailure(SshConnection *connection, uint8_t *p, size_t *length)
Format SSH_MSG_USERAUTH_FAILURE message.
Definition: ssh_auth.c:516
error_t sshFormatUserAuthMethods(SshConnection *connection, uint8_t *p, size_t *written)
Format the list of allowed authentication methods.
Definition: ssh_auth.c:563
error_t sshAcceptAuthRequest(SshConnection *connection)
Accept client's authentication request.
Definition: ssh_auth.c:263
error_t sshFormatUserAuthBanner(SshConnection *connection, const char_t *banner, uint8_t *p, size_t *length)
Format SSH_MSG_USERAUTH_BANNER message.
Definition: ssh_auth.c:327
error_t sshParseUserAuthMessage(SshConnection *connection, uint8_t type, const uint8_t *message, size_t length)
Parse authentication method specific method messages.
Definition: ssh_auth.c:1076
error_t sshRejectAuthRequest(SshConnection *connection)
Reject client's authentication request.
Definition: ssh_auth.c:293
error_t sshParseNoneAuthParams(SshConnection *connection, const SshString *userName, const uint8_t *p, size_t length)
Parse "none" method specific fields.
Definition: ssh_auth.c:858
error_t sshSendUserAuthBanner(SshConnection *connection, const char_t *banner)
Send SSH_MSG_USERAUTH_BANNER message.
Definition: ssh_auth.c:67
error_t sshParseUserAuthRequest(SshConnection *connection, const uint8_t *message, size_t length)
Parse SSH_MSG_USERAUTH_REQUEST message.
Definition: ssh_auth.c:717
SSH user authentication protocol.
SshAuthMethod
Authentication methods.
Definition: ssh_auth.h:48
@ SSH_AUTH_METHOD_PUBLIC_KEY
Definition: ssh_auth.h:51
@ SSH_AUTH_METHOD_PASSWORD
Definition: ssh_auth.h:50
@ SSH_AUTH_METHOD_NONE
Definition: ssh_auth.h:49
error_t sshParseUserAuthPasswdChangeReq(SshConnection *connection, const uint8_t *message, size_t length)
Parse SSH_MSG_USERAUTH_PASSWD_CHANGEREQ message.
error_t sshFormatPasswordAuthParams(SshConnection *connection, uint8_t *p, size_t *written)
Format "password" method specific fields.
error_t sshParsePasswordAuthParams(SshConnection *connection, const SshString *userName, const uint8_t *p, size_t length)
Parse "password" method specific fields.
Password authentication method.
error_t sshParseUserAuthPkOk(SshConnection *connection, const uint8_t *message, size_t length)
Parse SSH_MSG_USERAUTH_PK_OK message.
error_t sshFormatPublicKeyAuthParams(SshConnection *connection, const uint8_t *message, size_t messageLen, uint8_t *p, size_t *written)
Format "publickey" method specific fields.
error_t sshParsePublicKeyAuthParams(SshConnection *connection, const SshString *userName, const uint8_t *message, const uint8_t *p, size_t length)
Parse "publickey" method specific fields.
Public key authentication method.
error_t sshFormatString(const char_t *value, uint8_t *p, size_t *written)
Format a string.
Definition: ssh_misc.c:1384
bool_t sshCompareString(const SshString *string, const char_t *value)
Compare a binary string against the supplied value.
Definition: ssh_misc.c:1586
error_t sshParseNameList(const uint8_t *p, size_t length, SshNameList *nameList)
Parse a comma-separated list of names.
Definition: ssh_misc.c:1227
bool_t sshCompareAlgo(const char_t *name1, const char_t *name2)
Compare algorithm names.
Definition: ssh_misc.c:1653
error_t sshParseString(const uint8_t *p, size_t length, SshString *string)
Parse a string.
Definition: ssh_misc.c:1152
int_t sshSelectNextHostKey(SshConnection *connection)
Select the next acceptable host key.
Definition: ssh_misc.c:808
SSH helper functions.
error_t sshSendPacket(SshConnection *connection, uint8_t *payload, size_t payloadLen)
Send SSH packet.
Definition: ssh_packet.c:57
SSH packet encryption/decryption.
#define SSH_PACKET_HEADER_SIZE
Definition: ssh_packet.h:38
error_t sshSendDisconnect(SshConnection *connection, uint32_t reasonCode, const char_t *description)
Send SSH_MSG_DISCONNECT message.
SSH transport layer protocol.
bool_t SshBoolean
Boolean.
Definition: ssh_types.h:48
String containing a comma-separated list of names.
Definition: ssh_types.h:78
size_t length
Definition: ssh_types.h:80
String.
Definition: ssh_types.h:56
const char_t * value
Definition: ssh_types.h:57
size_t length
Definition: ssh_types.h:58
uint8_t length
Definition: tcp.h:368