mirror of
https://github.com/DarkStore-3DS/Project_CTR.git
synced 2026-07-03 00:39:14 +00:00
makerom: fixes
Moved away from ctrtool's polarssl api completely. Brought certs.c/h inline with code style, fixed bugs relating to tmd savedata field generation and not recording savedata size from rsf (ncsd.c)
This commit is contained in:
+55
-87
@@ -2,57 +2,64 @@
|
||||
#include "certs.h"
|
||||
|
||||
// Cert Sizes
|
||||
|
||||
u32 GetCertSize(u8 *cert)
|
||||
void GetCertSigSectionSizes(u32 *sign_size, u32 *sign_padlen, u8 *cert)
|
||||
{
|
||||
u32 SigSize = 0;
|
||||
u32 SigPadding = 0;
|
||||
GetCertSigSectionSizes(&SigSize,&SigPadding,cert);
|
||||
if(!SigSize || !SigPadding) return 0;
|
||||
|
||||
Cert_Struct *certcore = (Cert_Struct*)(cert+4+SigSize+SigPadding);
|
||||
|
||||
u32 PubKSectionSize = GetCertPubkSectionSize((pubk_types)u8_to_u32(certcore->KeyType,BE));
|
||||
|
||||
return (4+SigSize+SigPadding+sizeof(Cert_Struct)+PubKSectionSize);
|
||||
}
|
||||
|
||||
void GetCertSigSectionSizes(u32 *SigSize, u32 *SigPadding, u8 *cert)
|
||||
{
|
||||
sig_types sig = (sig_types)u8_to_u32(cert,BE);
|
||||
u32 sig = u8_to_u32(cert,BE);
|
||||
switch(sig){
|
||||
case RSA_4096_SHA1 :
|
||||
*SigSize = 0x200;
|
||||
*SigPadding = 0x3C;
|
||||
*sign_size = 0x200;
|
||||
*sign_padlen = 0x3C;
|
||||
break;
|
||||
case RSA_2048_SHA1 :
|
||||
*SigSize = 0x100;
|
||||
*SigPadding = 0x3C;
|
||||
*sign_size = 0x100;
|
||||
*sign_padlen = 0x3C;
|
||||
break;
|
||||
case ECC_SHA1 :
|
||||
*SigSize = 0x3C;
|
||||
*SigPadding = 0x40;
|
||||
*sign_size = 0x3C;
|
||||
*sign_padlen = 0x40;
|
||||
break;
|
||||
case RSA_4096_SHA256 :
|
||||
*SigSize = 0x200;
|
||||
*SigPadding = 0x3C;
|
||||
*sign_size = 0x200;
|
||||
*sign_padlen = 0x3C;
|
||||
break;
|
||||
case RSA_2048_SHA256 :
|
||||
*SigSize = 0x100;
|
||||
*SigPadding = 0x3C;
|
||||
*sign_size = 0x100;
|
||||
*sign_padlen = 0x3C;
|
||||
break;
|
||||
case ECC_SHA256 :
|
||||
*SigSize = 0x3C;
|
||||
*SigPadding = 0x40;
|
||||
*sign_size = 0x3C;
|
||||
*sign_padlen = 0x40;
|
||||
break;
|
||||
default :
|
||||
*SigSize = 0;
|
||||
*SigPadding = 0;
|
||||
*sign_size = 0;
|
||||
*sign_padlen = 0;
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
u32 GetCertSize(u8 *cert)
|
||||
{
|
||||
u32 sign_size = 0;
|
||||
u32 sign_padlen = 0;
|
||||
GetCertSigSectionSizes(&sign_size,&sign_padlen,cert);
|
||||
if(!sign_size || !sign_padlen)
|
||||
return 0;
|
||||
|
||||
return sizeof(u32) + sign_size + sign_padlen + sizeof(cert_hdr) + GetCertPubkSectionSize(GetCertPubkType(cert));
|
||||
}
|
||||
|
||||
|
||||
cert_hdr* GetCertHdr(u8 *cert)
|
||||
{
|
||||
u32 sign_size = 0;
|
||||
u32 sign_padlen = 0;
|
||||
GetCertSigSectionSizes(&sign_size,&sign_padlen,cert);
|
||||
if(!sign_size || !sign_padlen) return NULL;
|
||||
|
||||
return (cert_hdr*)(cert+4+sign_size+sign_padlen);
|
||||
}
|
||||
|
||||
u32 GetCertPubkSectionSize(pubk_types type)
|
||||
{
|
||||
switch(type){
|
||||
@@ -66,80 +73,41 @@ u32 GetCertPubkSectionSize(pubk_types type)
|
||||
// Issuer/Name Functions
|
||||
u8 *GetCertIssuer(u8 *cert)
|
||||
{
|
||||
u32 SigSize = 0;
|
||||
u32 SigPadding = 0;
|
||||
GetCertSigSectionSizes(&SigSize,&SigPadding,cert);
|
||||
if(!SigSize || !SigPadding) return 0;
|
||||
|
||||
Cert_Struct *certcore = (Cert_Struct*)(cert+4+SigSize+SigPadding);
|
||||
return certcore->Issuer;
|
||||
cert_hdr *hdr = GetCertHdr(cert);
|
||||
return hdr->issuer;
|
||||
}
|
||||
u8 *GetCertName(u8 *cert)
|
||||
{
|
||||
u32 SigSize = 0;
|
||||
u32 SigPadding = 0;
|
||||
GetCertSigSectionSizes(&SigSize,&SigPadding,cert);
|
||||
if(!SigSize || !SigPadding) return 0;
|
||||
|
||||
Cert_Struct *certcore = (Cert_Struct*)(cert+4+SigSize+SigPadding);
|
||||
return certcore->Name;
|
||||
cert_hdr *hdr = GetCertHdr(cert);
|
||||
return hdr->name;
|
||||
}
|
||||
|
||||
int GenCertChildIssuer(u8 *dest, u8 *cert)
|
||||
void GenCertChildIssuer(u8 *dest, u8 *cert)
|
||||
{
|
||||
u8 *issuer = GetCertIssuer(cert);
|
||||
u8 *name = GetCertName(cert);
|
||||
|
||||
/*
|
||||
u32 out_size = strlen((char*)issuer) + strlen((char*)name) + 1;
|
||||
if(out_size > 0x40) return MEM_ERROR;
|
||||
*/
|
||||
|
||||
snprintf((char*)dest,0x40,"%s-%s",issuer,name);
|
||||
|
||||
/*
|
||||
strcat((char*)dest,(char*)issuer);
|
||||
strcat((char*)dest,"-");
|
||||
strcat((char*)dest,(char*)name);
|
||||
*/
|
||||
return 0;
|
||||
snprintf((char*)dest,0x40,"%s-%s",GetCertIssuer(cert),GetCertName(cert));
|
||||
}
|
||||
|
||||
// Pubk
|
||||
pubk_types GetCertPubkType(u8 *cert)
|
||||
{
|
||||
u32 SigSize = 0;
|
||||
u32 SigPadding = 0;
|
||||
GetCertSigSectionSizes(&SigSize,&SigPadding,cert);
|
||||
if(!SigSize || !SigPadding) return 0;
|
||||
cert_hdr *hdr = GetCertHdr(cert);
|
||||
|
||||
Cert_Struct *certcore = (Cert_Struct*)(cert+4+SigSize+SigPadding);
|
||||
|
||||
return (pubk_types)u8_to_u32(certcore->KeyType,BE);
|
||||
return (pubk_types)u8_to_u32(hdr->keyType,BE);
|
||||
}
|
||||
u8 *GetCertPubk(u8 *cert)
|
||||
{
|
||||
u32 SigSize = 0;
|
||||
u32 SigPadding = 0;
|
||||
GetCertSigSectionSizes(&SigSize,&SigPadding,cert);
|
||||
if(!SigSize || !SigPadding) return 0;
|
||||
return (cert+4+SigSize+SigPadding+sizeof(Cert_Struct));
|
||||
if(!GetCertHdr(cert))
|
||||
return NULL;
|
||||
return ((u8*)GetCertHdr(cert)) + sizeof(cert_hdr);
|
||||
}
|
||||
|
||||
bool VerifyCert(u8 *cert, u8 *pubk)
|
||||
{
|
||||
u32 SigSize = 0;
|
||||
u32 SigPadding = 0;
|
||||
GetCertSigSectionSizes(&SigSize,&SigPadding,cert);
|
||||
if(!SigSize || !SigPadding) return 0;
|
||||
if(!GetCertHdr(cert))
|
||||
return false;
|
||||
u8 *signature = (cert+sizeof(u32));
|
||||
u8 *data = (u8*)GetCertHdr(cert);
|
||||
u32 datasize = sizeof(cert_hdr) + GetCertPubkSectionSize(GetCertPubkType(cert));
|
||||
|
||||
|
||||
u8 *signature = (cert+4);
|
||||
u8 *data = (cert+4+SigSize+SigPadding);
|
||||
u32 datasize = sizeof(Cert_Struct) + GetCertPubkSectionSize(GetCertPubkType(cert));
|
||||
|
||||
int result = ctr_sig(data,datasize,signature,pubk,NULL,u8_to_u32(cert,BE),CTR_RSA_VERIFY);
|
||||
|
||||
if(result == 0) return true;
|
||||
else return false;
|
||||
return RsaSignVerify(data,datasize,signature,pubk,NULL,u8_to_u32(cert,BE),CTR_RSA_VERIFY);
|
||||
}
|
||||
Reference in New Issue
Block a user