qemu
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
26void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *rsa_key)27{
28if (!rsa_key) {29return;30}31g_free(rsa_key->n.data);32g_free(rsa_key->e.data);33g_free(rsa_key->d.data);34g_free(rsa_key->p.data);35g_free(rsa_key->q.data);36g_free(rsa_key->dp.data);37g_free(rsa_key->dq.data);38g_free(rsa_key->u.data);39g_free(rsa_key);40}
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*/
52void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key,53size_t keylen,54uint8_t **dst,55size_t *dlen)56{
57QCryptoEncodeContext *ctx = qcrypto_der_encode_ctx_new();58uint8_t version = 0;59
60qcrypto_der_encode_seq_begin(ctx);61
62/* version */63qcrypto_der_encode_int(ctx, &version, sizeof(version));64
65/* algorithm identifier */66qcrypto_der_encode_seq_begin(ctx);67qcrypto_der_encode_oid(ctx, (uint8_t *)QCRYPTO_OID_rsaEncryption,68sizeof(QCRYPTO_OID_rsaEncryption) - 1);69qcrypto_der_encode_null(ctx);70qcrypto_der_encode_seq_end(ctx);71
72/* RSA private key */73qcrypto_der_encode_octet_str(ctx, key, keylen);74
75qcrypto_der_encode_seq_end(ctx);76
77*dlen = qcrypto_der_encode_ctx_buffer_len(ctx);78*dst = g_malloc(*dlen);79qcrypto_der_encode_ctx_flush_and_free(ctx, *dst);80}
81
82#if defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED)83#include "rsakey-nettle.c.inc"84#else85#include "rsakey-builtin.c.inc"86#endif87