/src/qemu/crypto/rsakey.c
Line | Count | Source |
1 | | /* |
2 | | * QEMU Crypto RSA key parser |
3 | | * |
4 | | * Copyright (c) 2022 Bytedance |
5 | | * Author: lei he <helei.sig11@bytedance.com> |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | * |
20 | | */ |
21 | | |
22 | | #include "qemu/osdep.h" |
23 | | #include "der.h" |
24 | | #include "rsakey.h" |
25 | | |
26 | | void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *rsa_key) |
27 | 0 | { |
28 | 0 | if (!rsa_key) { |
29 | 0 | return; |
30 | 0 | } |
31 | 0 | g_free(rsa_key->n.data); |
32 | 0 | g_free(rsa_key->e.data); |
33 | 0 | g_free(rsa_key->d.data); |
34 | 0 | g_free(rsa_key->p.data); |
35 | 0 | g_free(rsa_key->q.data); |
36 | 0 | g_free(rsa_key->dp.data); |
37 | 0 | g_free(rsa_key->dq.data); |
38 | 0 | g_free(rsa_key->u.data); |
39 | 0 | g_free(rsa_key); |
40 | 0 | } |
41 | | |
42 | | /** |
43 | | * PKCS#8 private key info for RSA |
44 | | * |
45 | | * PrivateKeyInfo ::= SEQUENCE { |
46 | | * version INTEGER, |
47 | | * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, |
48 | | * privateKey OCTET STRING, |
49 | | * attributes [0] IMPLICIT Attributes OPTIONAL |
50 | | * } |
51 | | */ |
52 | | void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key, |
53 | | size_t keylen, |
54 | | uint8_t **dst, |
55 | | size_t *dlen) |
56 | 0 | { |
57 | 0 | QCryptoEncodeContext *ctx = qcrypto_der_encode_ctx_new(); |
58 | 0 | uint8_t version = 0; |
59 | |
|
60 | 0 | qcrypto_der_encode_seq_begin(ctx); |
61 | | |
62 | | /* version */ |
63 | 0 | qcrypto_der_encode_int(ctx, &version, sizeof(version)); |
64 | | |
65 | | /* algorithm identifier */ |
66 | 0 | qcrypto_der_encode_seq_begin(ctx); |
67 | 0 | qcrypto_der_encode_oid(ctx, (uint8_t *)QCRYPTO_OID_rsaEncryption, |
68 | 0 | sizeof(QCRYPTO_OID_rsaEncryption) - 1); |
69 | 0 | qcrypto_der_encode_null(ctx); |
70 | 0 | qcrypto_der_encode_seq_end(ctx); |
71 | | |
72 | | /* RSA private key */ |
73 | 0 | qcrypto_der_encode_octet_str(ctx, key, keylen); |
74 | |
|
75 | 0 | qcrypto_der_encode_seq_end(ctx); |
76 | |
|
77 | 0 | *dlen = qcrypto_der_encode_ctx_buffer_len(ctx); |
78 | 0 | *dst = g_malloc(*dlen); |
79 | 0 | qcrypto_der_encode_ctx_flush_and_free(ctx, *dst); |
80 | 0 | } |
81 | | |
82 | | #if defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED) |
83 | | #include "rsakey-nettle.c.inc" |
84 | | #else |
85 | | #include "rsakey-builtin.c.inc" |
86 | | #endif |