xref: /openbmc/qemu/crypto/rsakey.c (revision 047f2ca1cec9cdb226f4eac7e672f753089a42ee)
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  {
28      if (!rsa_key) {
29          return;
30      }
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);
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   */
52  void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key,
53                                             size_t keylen,
54                                             uint8_t **dst,
55                                             size_t *dlen)
56  {
57      QCryptoEncodeContext *ctx = qcrypto_der_encode_ctx_new();
58      uint8_t version = 0;
59  
60      qcrypto_der_encode_seq_begin(ctx);
61  
62      /* version */
63      qcrypto_der_encode_int(ctx, &version, sizeof(version));
64  
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);
71  
72      /* RSA private key */
73      qcrypto_der_encode_octet_str(ctx, key, keylen);
74  
75      qcrypto_der_encode_seq_end(ctx);
76  
77      *dlen = qcrypto_der_encode_ctx_buffer_len(ctx);
78      *dst = g_malloc(*dlen);
79      qcrypto_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  #else
85  #include "rsakey-builtin.c.inc"
86  #endif
87