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 224c5e512eSLei He #ifndef QCRYPTO_RSAKEY_H 234c5e512eSLei He #define QCRYPTO_RSAKEY_H 244c5e512eSLei He 254c5e512eSLei He #include "qemu/host-utils.h" 264c5e512eSLei He #include "crypto/akcipher.h" 274c5e512eSLei He 284c5e512eSLei He typedef struct QCryptoAkCipherRSAKey QCryptoAkCipherRSAKey; 294c5e512eSLei He typedef struct QCryptoAkCipherMPI QCryptoAkCipherMPI; 304c5e512eSLei He 314c5e512eSLei He /** 324c5e512eSLei He * Multiple precious integer, encoded as two' complement, 334c5e512eSLei He * copied directly from DER encoded ASN.1 structures. 344c5e512eSLei He */ 354c5e512eSLei He struct QCryptoAkCipherMPI { 364c5e512eSLei He uint8_t *data; 374c5e512eSLei He size_t len; 384c5e512eSLei He }; 394c5e512eSLei He 404c5e512eSLei He /* See rfc2437: https://datatracker.ietf.org/doc/html/rfc2437 */ 414c5e512eSLei He struct QCryptoAkCipherRSAKey { 424c5e512eSLei He /* The modulus */ 434c5e512eSLei He QCryptoAkCipherMPI n; 444c5e512eSLei He /* The public exponent */ 454c5e512eSLei He QCryptoAkCipherMPI e; 464c5e512eSLei He /* The private exponent */ 474c5e512eSLei He QCryptoAkCipherMPI d; 484c5e512eSLei He /* The first factor */ 494c5e512eSLei He QCryptoAkCipherMPI p; 504c5e512eSLei He /* The second factor */ 514c5e512eSLei He QCryptoAkCipherMPI q; 524c5e512eSLei He /* The first factor's exponent */ 534c5e512eSLei He QCryptoAkCipherMPI dp; 544c5e512eSLei He /* The second factor's exponent */ 554c5e512eSLei He QCryptoAkCipherMPI dq; 564c5e512eSLei He /* The CRT coefficient */ 574c5e512eSLei He QCryptoAkCipherMPI u; 584c5e512eSLei He }; 594c5e512eSLei He 604c5e512eSLei He /** 614c5e512eSLei He * Parse DER encoded ASN.1 RSA keys, expected ASN.1 schemas: 624c5e512eSLei He * RsaPrivKey ::= SEQUENCE { 634c5e512eSLei He * version INTEGER 644c5e512eSLei He * n INTEGER 654c5e512eSLei He * e INTEGER 664c5e512eSLei He * d INTEGER 674c5e512eSLei He * p INTEGER 684c5e512eSLei He * q INTEGER 694c5e512eSLei He * dp INTEGER 704c5e512eSLei He * dq INTEGER 714c5e512eSLei He * u INTEGER 724c5e512eSLei He * otherPrimeInfos OtherPrimeInfos OPTIONAL 734c5e512eSLei He * } 744c5e512eSLei He * 754c5e512eSLei He * RsaPubKey ::= SEQUENCE { 764c5e512eSLei He * n INTEGER 774c5e512eSLei He * e INTEGER 784c5e512eSLei He * } 794c5e512eSLei He * 804c5e512eSLei He * Returns: On success QCryptoAkCipherRSAKey is returned, otherwise returns NULL 814c5e512eSLei He */ 824c5e512eSLei He QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse( 834c5e512eSLei He QCryptoAkCipherKeyType type, 844c5e512eSLei He const uint8_t *key, size_t keylen, Error **errp); 854c5e512eSLei He 86*58660863SLei He /** 87*58660863SLei He * qcrypto_akcipher_rsakey_export_as_p8info: 88*58660863SLei He * 89*58660863SLei He * Export RSA private key to PKCS#8 private key info. 90*58660863SLei He */ 91*58660863SLei He void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key, 92*58660863SLei He size_t keylen, 93*58660863SLei He uint8_t **dst, 94*58660863SLei He size_t *dlen); 95*58660863SLei He 964c5e512eSLei He void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *key); 974c5e512eSLei He 984c5e512eSLei He G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipherRSAKey, 994c5e512eSLei He qcrypto_akcipher_rsakey_free); 1004c5e512eSLei He 1014c5e512eSLei He #endif 102