8f0cb6b63ed3ba5dbea49bb70c289e03e6e2a8fe
[strongswan.git] / src / libstrongswan / plugins / sshkey / sshkey_encoder.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "sshkey_encoder.h"
17
18 #include <bio/bio_writer.h>
19
20 /**
21 * Encode the public key as Base64 encoded SSH key blob
22 */
23 static bool build_public_key(chunk_t *encoding, va_list args)
24 {
25 bio_writer_t *writer;
26 chunk_t n, e;
27
28 if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
29 CRED_PART_RSA_PUB_EXP, &e, CRED_PART_END))
30 {
31 writer = bio_writer_create(0);
32 writer->write_data32(writer, chunk_from_str("ssh-rsa"));
33
34 writer->write_data32(writer, e);
35 writer->write_data32(writer, n);
36 *encoding = chunk_to_base64(writer->get_buf(writer), NULL);
37 writer->destroy(writer);
38 return TRUE;
39 }
40 return FALSE;
41 }
42
43 bool sshkey_encoder_encode(cred_encoding_type_t type, chunk_t *encoding,
44 va_list args)
45 {
46 switch (type)
47 {
48 case PUBKEY_SSHKEY:
49 return build_public_key(encoding, args);
50 default:
51 return FALSE;
52 }
53 }