1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-26 12:12:02 +00:00
nxdumptool/source/cert.h

152 lines
5.5 KiB
C
Raw Normal View History

2020-04-16 01:06:41 +01:00
/*
* Copyright (c) 2020 DarkMatterCore
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-04-11 06:28:26 +01:00
#pragma once
#ifndef __CERT_H__
#define __CERT_H__
2020-04-15 21:50:07 +01:00
#include <switch/types.h>
2020-04-11 06:28:26 +01:00
#include "signature.h"
#define CERT_MAX_SIZE 0x500 /* Equivalent to sizeof(CertSigRsa4096PubKeyRsa4096) */
#define CERT_MIN_SIZE 0x180 /* Equivalent to sizeof(CertSigEcsda240PubKeyEcsda240) */
typedef enum {
2020-04-19 23:44:22 +01:00
CertType_None = 0,
CertType_SigRsa4096_PubKeyRsa4096 = 1,
CertType_SigRsa4096_PubKeyRsa2048 = 2,
CertType_SigRsa4096_PubKeyEcsda240 = 3,
CertType_SigRsa2048_PubKeyRsa4096 = 4,
CertType_SigRsa2048_PubKeyRsa2048 = 5,
CertType_SigRsa2048_PubKeyEcsda240 = 6,
CertType_SigEcsda240_PubKeyRsa4096 = 7,
CertType_SigEcsda240_PubKeyRsa2048 = 8,
CertType_SigEcsda240_PubKeyEcsda240 = 9
2020-04-11 06:28:26 +01:00
} CertType;
/// Always stored using big endian byte order.
typedef enum {
CertPubKeyType_Rsa4096 = 0,
CertPubKeyType_Rsa2048 = 1,
CertPubKeyType_Ecsda240 = 2
} CertPubKeyType;
typedef struct {
u8 public_key[0x200];
u32 public_exponent;
u8 padding[0x34];
} CertPublicKeyBlockRsa4096;
typedef struct {
u8 public_key[0x100];
u32 public_exponent;
u8 padding[0x34];
} CertPublicKeyBlockRsa2048;
typedef struct {
u8 public_key[0x3C];
u8 padding[0x3C];
} CertPublicKeyBlockEcsda240;
2020-04-19 23:44:22 +01:00
/// Placed after the certificate signature block.
2020-04-11 06:28:26 +01:00
typedef struct {
char issuer[0x40];
2020-04-19 23:44:22 +01:00
u32 pub_key_type;
2020-04-11 06:28:26 +01:00
char name[0x40];
u32 cert_id;
2020-04-19 23:44:22 +01:00
} CertCommonBlock;
typedef struct {
SignatureBlockRsa4096 sig_block; ///< sig_type field is stored using big endian byte order.
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Rsa4096.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockRsa4096 pub_key_block;
} CertSigRsa4096PubKeyRsa4096;
typedef struct {
SignatureBlockRsa4096 sig_block; ///< sig_type field is stored using big endian byte order.
2020-04-19 23:44:22 +01:00
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Rsa2048.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockRsa2048 pub_key_block;
} CertSigRsa4096PubKeyRsa2048;
typedef struct {
SignatureBlockRsa4096 sig_block; ///< sig_type field is stored using big endian byte order.
2020-04-19 23:44:22 +01:00
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Ecsda240.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockEcsda240 pub_key_block;
} CertSigRsa4096PubKeyEcsda240;
typedef struct {
SignatureBlockRsa2048 sig_block; ///< sig_type field is stored using big endian byte order.
2020-04-19 23:44:22 +01:00
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Rsa4096.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockRsa4096 pub_key_block;
} CertSigRsa2048PubKeyRsa4096;
typedef struct {
SignatureBlockRsa2048 sig_block; ///< sig_type field is stored using big endian byte order.
2020-04-19 23:44:22 +01:00
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Rsa2048.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockRsa2048 pub_key_block;
} CertSigRsa2048PubKeyRsa2048;
typedef struct {
SignatureBlockRsa2048 sig_block; ///< sig_type field is stored using big endian byte order.
2020-04-19 23:44:22 +01:00
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Ecsda240.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockEcsda240 pub_key_block;
} CertSigRsa2048PubKeyEcsda240;
typedef struct {
SignatureBlockEcsda240 sig_block; ///< sig_type field is stored using big endian byte order.
2020-04-19 23:44:22 +01:00
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Rsa4096.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockRsa4096 pub_key_block;
} CertSigEcsda240PubKeyRsa4096;
typedef struct {
SignatureBlockEcsda240 sig_block; ///< sig_type field is stored using big endian byte order.
2020-04-19 23:44:22 +01:00
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Rsa2048.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockRsa2048 pub_key_block;
} CertSigEcsda240PubKeyRsa2048;
typedef struct {
SignatureBlockEcsda240 sig_block; ///< sig_type field is stored using big endian byte order.
2020-04-19 23:44:22 +01:00
CertCommonBlock cert_common_blk; ///< pub_key_type field must be CertPubKeyType_Ecsda240.
2020-04-11 06:28:26 +01:00
CertPublicKeyBlockEcsda240 pub_key_block;
} CertSigEcsda240PubKeyEcsda240;
/// Used to store certificate type, size and raw data.
typedef struct {
u8 type; ///< CertType.
2020-04-19 23:44:22 +01:00
u64 size; ///< Raw certificate size.
u8 data[CERT_MAX_SIZE]; ///< Raw certificate data.
2020-04-11 06:28:26 +01:00
} Certificate;
/// Used to store two or more certificates.
typedef struct {
u32 count;
Certificate *certs;
} CertificateChain;
bool certRetrieveCertificateByName(Certificate *dst, const char *name);
bool certRetrieveCertificateChainBySignatureIssuer(CertificateChain *dst, const char *issuer);
void certFreeCertificateChain(CertificateChain *chain);
2020-04-11 06:28:26 +01:00
2020-04-19 23:44:22 +01:00
/// Retrieves the common block from an input Certificate.
CertCommonBlock *certGetCommonBlockFromCertificate(Certificate *cert);
2020-04-11 06:28:26 +01:00
/// Returns a pointer to a heap allocated buffer that must be freed by the user.
u8 *certGenerateRawCertificateChainBySignatureIssuer(const char *issuer, u64 *out_size);
#endif /* __CERT_H__ */