2 * QEMU Crypto akcipher algorithms
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 <nettle/asn1.h>
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
28 static bool DumpMPI(struct asn1_der_iterator *i, QCryptoAkCipherMPI *mpi)
30 mpi->data = g_memdup2(i->data, i->length);
35 static bool GetMPI(struct asn1_der_iterator *i, QCryptoAkCipherMPI *mpi)
37 if (asn1_der_iterator_next(i) != ASN1_ITERATOR_PRIMITIVE ||
38 i->type != ASN1_INTEGER) {
41 return DumpMPI(i, mpi);
45 * RsaPrivKey ::= SEQUENCE {
55 * otherPrimeInfos OtherPrimeInfos OPTIONAL
58 static QCryptoAkCipherRSAKey *qcrypto_nettle_rsa_private_key_parse(
59 const uint8_t *key, size_t keylen, Error **errp)
61 QCryptoAkCipherRSAKey *rsa = g_new0(QCryptoAkCipherRSAKey, 1);
62 struct asn1_der_iterator i;
66 /* Parse entire struct */
67 if (asn1_der_iterator_first(&i, keylen, key) != ASN1_ITERATOR_CONSTRUCTED ||
68 i.type != ASN1_SEQUENCE ||
69 asn1_der_decode_constructed_last(&i) != ASN1_ITERATOR_PRIMITIVE ||
70 i.type != ASN1_INTEGER ||
71 !asn1_der_get_uint32(&i, &version) ||
73 !GetMPI(&i, &rsa->n) ||
74 !GetMPI(&i, &rsa->e) ||
75 !GetMPI(&i, &rsa->d) ||
76 !GetMPI(&i, &rsa->p) ||
77 !GetMPI(&i, &rsa->q) ||
78 !GetMPI(&i, &rsa->dp) ||
79 !GetMPI(&i, &rsa->dq) ||
80 !GetMPI(&i, &rsa->u)) {
85 tag = asn1_der_iterator_next(&i);
87 * According to the standard otherPrimeInfos must be present for
88 * version 1. There is no strict verification here, this is to be
89 * compatible with the unit test of the kernel. TODO: remove this
90 * until linux-kernel's unit-test is fixed;
92 if (tag == ASN1_ITERATOR_END) {
95 if (tag != ASN1_ITERATOR_CONSTRUCTED ||
96 i.type != ASN1_SEQUENCE) {
101 if (asn1_der_iterator_next(&i) != ASN1_ITERATOR_END) {
108 error_setg(errp, "Failed to parse RSA private key");
109 qcrypto_akcipher_rsakey_free(rsa);
114 * RsaPubKey ::= SEQUENCE {
119 static QCryptoAkCipherRSAKey *qcrypto_nettle_rsa_public_key_parse(
120 const uint8_t *key, size_t keylen, Error **errp)
123 QCryptoAkCipherRSAKey *rsa = g_new0(QCryptoAkCipherRSAKey, 1);
124 struct asn1_der_iterator i;
126 if (asn1_der_iterator_first(&i, keylen, key) != ASN1_ITERATOR_CONSTRUCTED ||
127 i.type != ASN1_SEQUENCE ||
128 asn1_der_decode_constructed_last(&i) != ASN1_ITERATOR_PRIMITIVE ||
129 !DumpMPI(&i, &rsa->n) ||
130 !GetMPI(&i, &rsa->e) ||
131 asn1_der_iterator_next(&i) != ASN1_ITERATOR_END) {
138 error_setg(errp, "Failed to parse RSA public key");
139 qcrypto_akcipher_rsakey_free(rsa);
143 QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse(
144 QCryptoAkCipherKeyType type, const uint8_t *key,
145 size_t keylen, Error **errp)
148 case QCRYPTO_AK_CIPHER_KEY_TYPE_PRIVATE:
149 return qcrypto_nettle_rsa_private_key_parse(key, keylen, errp);
151 case QCRYPTO_AK_CIPHER_KEY_TYPE_PUBLIC:
152 return qcrypto_nettle_rsa_public_key_parse(key, keylen, errp);
155 error_setg(errp, "Unknown key type: %d", type);