14c5e512eSLei He /* 24c5e512eSLei He * QEMU Crypto RSA key parser 34c5e512eSLei He * 44c5e512eSLei He * Copyright (c) 2022 Bytedance 54c5e512eSLei He * Author: lei he <helei.sig11@bytedance.com> 64c5e512eSLei He * 74c5e512eSLei He * This library is free software; you can redistribute it and/or 84c5e512eSLei He * modify it under the terms of the GNU Lesser General Public 94c5e512eSLei He * License as published by the Free Software Foundation; either 104c5e512eSLei He * version 2.1 of the License, or (at your option) any later version. 114c5e512eSLei He * 124c5e512eSLei He * This library is distributed in the hope that it will be useful, 134c5e512eSLei He * but WITHOUT ANY WARRANTY; without even the implied warranty of 144c5e512eSLei He * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 154c5e512eSLei He * Lesser General Public License for more details. 164c5e512eSLei He * 174c5e512eSLei He * You should have received a copy of the GNU Lesser General Public 184c5e512eSLei He * License along with this library; if not, see <http://www.gnu.org/licenses/>. 194c5e512eSLei He * 204c5e512eSLei He */ 214c5e512eSLei He 22*58660863SLei He #include "qemu/osdep.h" 23*58660863SLei He #include "der.h" 244c5e512eSLei He #include "rsakey.h" 254c5e512eSLei He 264c5e512eSLei He void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *rsa_key) 274c5e512eSLei He { 284c5e512eSLei He if (!rsa_key) { 294c5e512eSLei He return; 304c5e512eSLei He } 314c5e512eSLei He g_free(rsa_key->n.data); 324c5e512eSLei He g_free(rsa_key->e.data); 334c5e512eSLei He g_free(rsa_key->d.data); 344c5e512eSLei He g_free(rsa_key->p.data); 354c5e512eSLei He g_free(rsa_key->q.data); 364c5e512eSLei He g_free(rsa_key->dp.data); 374c5e512eSLei He g_free(rsa_key->dq.data); 384c5e512eSLei He g_free(rsa_key->u.data); 394c5e512eSLei He g_free(rsa_key); 404c5e512eSLei He } 414c5e512eSLei He 42*58660863SLei He /** 43*58660863SLei He * PKCS#8 private key info for RSA 44*58660863SLei He * 45*58660863SLei He * PrivateKeyInfo ::= SEQUENCE { 46*58660863SLei He * version INTEGER, 47*58660863SLei He * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, 48*58660863SLei He * privateKey OCTET STRING, 49*58660863SLei He * attributes [0] IMPLICIT Attributes OPTIONAL 50*58660863SLei He * } 51*58660863SLei He */ 52*58660863SLei He void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key, 53*58660863SLei He size_t keylen, 54*58660863SLei He uint8_t **dst, 55*58660863SLei He size_t *dlen) 56*58660863SLei He { 57*58660863SLei He QCryptoEncodeContext *ctx = qcrypto_der_encode_ctx_new(); 58*58660863SLei He uint8_t version = 0; 59*58660863SLei He 60*58660863SLei He qcrypto_der_encode_seq_begin(ctx); 61*58660863SLei He 62*58660863SLei He /* version */ 63*58660863SLei He qcrypto_der_encode_int(ctx, &version, sizeof(version)); 64*58660863SLei He 65*58660863SLei He /* algorithm identifier */ 66*58660863SLei He qcrypto_der_encode_seq_begin(ctx); 67*58660863SLei He qcrypto_der_encode_oid(ctx, (uint8_t *)QCRYPTO_OID_rsaEncryption, 68*58660863SLei He sizeof(QCRYPTO_OID_rsaEncryption) - 1); 69*58660863SLei He qcrypto_der_encode_null(ctx); 70*58660863SLei He qcrypto_der_encode_seq_end(ctx); 71*58660863SLei He 72*58660863SLei He /* RSA private key */ 73*58660863SLei He qcrypto_der_encode_octet_str(ctx, key, keylen); 74*58660863SLei He 75*58660863SLei He qcrypto_der_encode_seq_end(ctx); 76*58660863SLei He 77*58660863SLei He *dlen = qcrypto_der_encode_ctx_buffer_len(ctx); 78*58660863SLei He *dst = g_malloc(*dlen); 79*58660863SLei He qcrypto_der_encode_ctx_flush_and_free(ctx, *dst); 80*58660863SLei He } 81*58660863SLei He 824c5e512eSLei He #if defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED) 834c5e512eSLei He #include "rsakey-nettle.c.inc" 844c5e512eSLei He #else 854c5e512eSLei He #include "rsakey-builtin.c.inc" 864c5e512eSLei He #endif 87