ssh_cert_export.c
Go to the documentation of this file.
1 /**
2  * @file ssh_cert_export.c
3  * @brief SSH certificate export functions
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.4
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_cert_export.h"
37 #include "ssh/ssh_cert_parse.h"
38 #include "ssh/ssh_misc.h"
39 #include "encoding/base64.h"
40 #include "debug.h"
41 
42 //Check SSH stack configuration
43 #if (SSH_SUPPORT == ENABLED && SSH_CERT_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Export SSH certificate (OpenSSH format)
48  * @param[in] input Certificate structure to encode
49  * @param[in] inputLen Length of the certificate structure to encode
50  * @param[out] output Resulting certificate file (optional parameter)
51  * @param[out] outputLen Length of the resulting certificate file
52  **/
53 
54 error_t sshExportCertificate(const void *input, size_t inputLen,
55  char_t *output, size_t *outputLen)
56 {
57  error_t error;
58  size_t n;
59  SshCertificate cert;
60  uint8_t identifier[40];
61 
62  //Check parameters
63  if(input == NULL || outputLen == NULL)
65 
66  //Parse certificate structure
67  error = sshParseCertificate(input, inputLen, &cert);
68  //Any error to report?
69  if(error)
70  return error;
71 
72  //Sanity check
73  if(cert.keyFormatId.length > sizeof(identifier))
75 
76  //Save key format identifier
78 
79  //Encode the certificate structure using Base64
80  base64Encode(input, inputLen, output, &n);
81 
82  //If the output parameter is NULL, then the function calculates the length
83  //of the resulting certificate file without copying any data
84  if(output != NULL)
85  {
86  //Make room for the identifier string
87  osMemmove(output + cert.keyFormatId.length + 1, output, n + 1);
88  //Copy identifier string
89  osMemcpy(output, identifier, cert.keyFormatId.length);
90  //The identifier must be followed by a whitespace character
91  output[cert.keyFormatId.length] = ' ';
92  }
93 
94  //Consider the length of the identifier string
95  n += cert.keyFormatId.length + 1;
96 
97  //Total number of bytes that have been written
98  *outputLen = n;
99 
100  //Successful processing
101  return NO_ERROR;
102 }
103 
104 #endif
SSH certificate export functions.
void base64Encode(const void *input, size_t inputLen, char_t *output, size_t *outputLen)
Base64 encoding algorithm.
Definition: base64.c:142
size_t length
Definition: ssh_types.h:58
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
const char_t * value
Definition: ssh_types.h:57
error_t
Error codes.
Definition: error.h:43
error_t sshExportCertificate(const void *input, size_t inputLen, char_t *output, size_t *outputLen)
Export SSH certificate (OpenSSH format)
SSH certificate parsing.
Base64 encoding scheme.
char char_t
Definition: compiler_port.h:48
SshString keyFormatId
uint8_t n
error_t sshParseCertificate(const uint8_t *data, size_t length, SshCertificate *cert)
Parse SSH certificate.
SSH helper functions.
@ ERROR_WRONG_IDENTIFIER
Definition: error.h:89
uint8_t identifier[]
Secure Shell (SSH)
SSH certificate (OpenSSH format)
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define osMemmove(dest, src, length)
Definition: os_port.h:147