util/uuid: Define UUID_STR_LEN from UUID_NONE string
[qemu/kevin.git] / crypto / rsakey.c
blob7d6f273aef6b7ffec3da292efaeb4253e95a0e8b
1 /*
2 * QEMU Crypto RSA key parser
4 * Copyright (c) 2022 Bytedance
5 * Author: lei he <helei.sig11@bytedance.com>
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.
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.
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/>.
22 #include "qemu/osdep.h"
23 #include "der.h"
24 #include "rsakey.h"
26 void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *rsa_key)
28 if (!rsa_key) {
29 return;
31 g_free(rsa_key->n.data);
32 g_free(rsa_key->e.data);
33 g_free(rsa_key->d.data);
34 g_free(rsa_key->p.data);
35 g_free(rsa_key->q.data);
36 g_free(rsa_key->dp.data);
37 g_free(rsa_key->dq.data);
38 g_free(rsa_key->u.data);
39 g_free(rsa_key);
42 /**
43 * PKCS#8 private key info for RSA
45 * PrivateKeyInfo ::= SEQUENCE {
46 * version INTEGER,
47 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
48 * privateKey OCTET STRING,
49 * attributes [0] IMPLICIT Attributes OPTIONAL
50 * }
52 void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key,
53 size_t keylen,
54 uint8_t **dst,
55 size_t *dlen)
57 QCryptoEncodeContext *ctx = qcrypto_der_encode_ctx_new();
58 uint8_t version = 0;
60 qcrypto_der_encode_seq_begin(ctx);
62 /* version */
63 qcrypto_der_encode_int(ctx, &version, sizeof(version));
65 /* algorithm identifier */
66 qcrypto_der_encode_seq_begin(ctx);
67 qcrypto_der_encode_oid(ctx, (uint8_t *)QCRYPTO_OID_rsaEncryption,
68 sizeof(QCRYPTO_OID_rsaEncryption) - 1);
69 qcrypto_der_encode_null(ctx);
70 qcrypto_der_encode_seq_end(ctx);
72 /* RSA private key */
73 qcrypto_der_encode_octet_str(ctx, key, keylen);
75 qcrypto_der_encode_seq_end(ctx);
77 *dlen = qcrypto_der_encode_ctx_buffer_len(ctx);
78 *dst = g_malloc(*dlen);
79 qcrypto_der_encode_ctx_flush_and_free(ctx, *dst);
82 #if defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED)
83 #include "rsakey-nettle.c.inc"
84 #else
85 #include "rsakey-builtin.c.inc"
86 #endif