Added function to build an export library. Fixed up certificate stuff

This commit is contained in:
planetbeing 2008-06-14 13:45:46 -04:00
Родитель ee4d91fe61
Коммит 42c9254b07
13 изменённых файлов: 150 добавлений и 5 удалений

Просмотреть файл

@ -57,6 +57,7 @@ AbstractFile* createAbstractFileFromFile(FILE* file) {
toReturn->tell = ftellWrapper;
toReturn->getLength = fileGetLength;
toReturn->close = fcloseWrapper;
toReturn->type = AbstractFileTypeFile;
return toReturn;
}
@ -92,6 +93,7 @@ AbstractFile* createAbstractFileFromDummy() {
toReturn->tell = dummyTell;
toReturn->getLength = NULL;
toReturn->close = dummyClose;
toReturn->type = AbstractFileTypeDummy;
return toReturn;
}
@ -153,6 +155,7 @@ AbstractFile* createAbstractFileFromMemory(void** buffer, size_t size) {
toReturn->tell = memTell;
toReturn->getLength = memGetLength;
toReturn->close = memClose;
toReturn->type = AbstractFileTypeMem;
return toReturn;
}
@ -276,6 +279,7 @@ AbstractFile* createAbstractFileFromMemoryFile(void** buffer, size_t* size) {
toReturn->tell = memFileTell;
toReturn->getLength = memFileGetLength;
toReturn->close = memFileClose;
toReturn->type = AbstractFileTypeMemFile;
return toReturn;
}
@ -297,6 +301,7 @@ AbstractFile* createAbstractFileFromMemoryFileBuffer(void** buffer, size_t* size
toReturn->tell = memFileTell;
toReturn->getLength = memFileGetLength;
toReturn->close = memFileClose;
toReturn->type = AbstractFileTypeMemFile;
return toReturn;
}

Просмотреть файл

@ -1,6 +1,12 @@
INCLUDE(FindOpenSSL)
INCLUDE(FindZLIB)
FIND_LIBRARY(CRYPTO_LIBRARIES crypto
PATHS
/usr/lib
/usr/local/lib
)
IF(NOT ZLIB_FOUND)
message(FATAL_ERROR "zlib is required for dmg!")
ENDIF(NOT ZLIB_FOUND)
@ -15,8 +21,7 @@ add_library(dmg base64.c checksum.c dmgfile.c dmglib.c filevault.c io.c partitio
IF(OPENSSL_FOUND)
add_definitions(-DHAVE_CRYPT)
include_directories(${OPENSSL_INCLUDE_DIR})
link_directories(${OPENSSL_LIBRARIES})
target_link_libraries(dmg crypto)
target_link_libraries(dmg ${CRYPTO_LIBRARIES})
ENDIF(OPENSSL_FOUND)
target_link_libraries(dmg common hfs z)
@ -25,3 +30,4 @@ add_executable(dmg-bin dmg.c)
target_link_libraries (dmg-bin dmg)
set_target_properties(dmg-bin PROPERTIES OUTPUT_NAME "dmg")

Просмотреть файл

@ -12,6 +12,18 @@ typedef off_t (*TellFunc)(AbstractFile* file);
typedef void (*CloseFunc)(AbstractFile* file);
typedef off_t (*GetLengthFunc)(AbstractFile* file);
typedef enum AbstractFileType {
AbstractFileTypeFile,
AbstractFileType8900,
AbstractFileTypeImg2,
AbstractFileTypeImg3,
AbstractFileTypeLZSS,
AbstractFileTypeIBootIM,
AbstractFileTypeMem,
AbstractFileTypeMemFile,
AbstractFileTypeDummy
} AbstractFileType;
struct AbstractFile {
void* data;
WriteFunc write;
@ -20,6 +32,7 @@ struct AbstractFile {
TellFunc tell;
GetLengthFunc getLength;
CloseFunc close;
AbstractFileType type;
};
typedef struct {

Просмотреть файл

@ -43,5 +43,6 @@ typedef struct Info8900 {
AbstractFile* createAbstractFileFrom8900(AbstractFile* file);
AbstractFile* duplicate8900File(AbstractFile* file, AbstractFile* backing);
void replaceCertificate8900(AbstractFile* file, AbstractFile* certificate);
#endif

Просмотреть файл

@ -51,6 +51,7 @@ typedef struct Img3Info {
AbstractFile* file;
Img3Element* root;
Img3Element* data;
Img3Element* cert;
size_t offset;
char dirty;
} Img3Info;
@ -60,6 +61,7 @@ extern "C" {
#endif
AbstractFile* createAbstractFileFromImg3(AbstractFile* file);
AbstractFile* duplicateImg3File(AbstractFile* file, AbstractFile* backing);
void replaceCertificateImg3(AbstractFile* file, AbstractFile* certificate);
#ifdef __cplusplus
}
#endif

Просмотреть файл

@ -11,6 +11,7 @@ extern "C" {
#endif
AbstractFile* openAbstractFile(AbstractFile* file);
AbstractFile* duplicateAbstractFile(AbstractFile* file, AbstractFile* backing);
AbstractFile* duplicateAbstractFileWithCertificate(AbstractFile* file, AbstractFile* backing, AbstractFile* certificate);
#ifdef __cplusplus
}
#endif

Просмотреть файл

@ -172,9 +172,21 @@ AbstractFile* createAbstractFileFrom8900(AbstractFile* file) {
toReturn->tell = tell8900;
toReturn->getLength = getLength8900;
toReturn->close = close8900;
toReturn->type = AbstractFileType8900;
return toReturn;
}
void replaceCertificate8900(AbstractFile* file, AbstractFile* certificate) {
Info8900* info = (Info8900*) (file->data);
info->header.footerCertLen = certificate->getLength(certificate);
if(info->footerCertificate != NULL) {
free(info->footerCertificate);
}
info->footerCertificate = (unsigned char*) malloc(info->header.footerCertLen);
certificate->read(certificate, info->footerCertificate, info->header.footerCertLen);
info->dirty = TRUE;
}
AbstractFile* duplicate8900File(AbstractFile* file, AbstractFile* backing) {
Info8900* info;
unsigned char* copyCertificate;
@ -204,5 +216,6 @@ AbstractFile* duplicate8900File(AbstractFile* file, AbstractFile* backing) {
toReturn->tell = tell8900;
toReturn->getLength = getLength8900;
toReturn->close = close8900;
toReturn->type = AbstractFileType8900;
return toReturn;
}

Просмотреть файл

@ -3,6 +3,12 @@ INCLUDE(FindZLIB)
INCLUDE(FindBZip2)
INCLUDE(FindPNG)
FIND_LIBRARY(CRYPTO_LIBRARIES crypto
PATHS
/usr/lib
/usr/local/lib
)
IF(NOT ZLIB_FOUND)
message(FATAL_ERROR "zlib is required for ipsw!")
ENDIF(NOT ZLIB_FOUND)
@ -35,7 +41,18 @@ link_directories(${PROJECT_BINARY_DIR}/common ${PROJECT_BINARY_DIR}/hfs ${PROJEC
add_library(xpwn 8900.c bspatch.c ibootim.c img2.c img3.c libxpwn.c lzss.c lzssfile.c nor_files.c outputstate.c plist.c pwnutil.c)
target_link_libraries(xpwn z crypto bz2 png minizip common dmg hfs)
target_link_libraries(xpwn dmg hfs common minizip ${CRYPTO_LIBRARIES} ${BZIP2_LIBRARIES} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES} m)
ADD_CUSTOM_TARGET(libXPwn.a ALL
COMMAND ${CMAKE_C_COMPILER}
-L${PROJECT_BINARY_DIR}/ipsw-patch -L${PROJECT_BINARY_DIR}/dmg -L${PROJECT_BINARY_DIR}/hfs
-L${PROJECT_BINARY_DIR}/hfs -L${PROJECT_BINARY_DIR}/minizip -L${PROJECT_BINARY_DIR}/common
-Xlinker --whole-archive -lxpwn -ldmg -lhfs -lcommon -lminizip
${CRYPTO_LIBRARIES} ${BZIP2_LIBRARIES} ${PNG_LIBRARIES}
-Xlinker --unresolved-symbols=ignore-all -Xlinker -r -nostdlib -o libXPwn.o
COMMAND ${CMAKE_AR} cr libXPwn.a libXPwn.o
COMMAND ${CMAKE_RANLIB} libXPwn.a
DEPENDS xpwn dmg hfs common minizip)
add_executable(ipsw main.c)
target_link_libraries(ipsw xpwn)

Просмотреть файл

@ -136,6 +136,7 @@ AbstractFile* createAbstractFileFromIBootIM(AbstractFile* file) {
toReturn->tell = tellIBootIM;
toReturn->getLength = getLengthIBootIM;
toReturn->close = closeIBootIM;
toReturn->type = AbstractFileTypeIBootIM;
return toReturn;
}
@ -165,6 +166,7 @@ AbstractFile* duplicateIBootIMFile(AbstractFile* file, AbstractFile* backing) {
toReturn->tell = tellIBootIM;
toReturn->getLength = getLengthIBootIM;
toReturn->close = closeIBootIM;
toReturn->type = AbstractFileTypeIBootIM;
return toReturn;
}

Просмотреть файл

@ -119,6 +119,7 @@ AbstractFile* createAbstractFileFromImg2(AbstractFile* file) {
toReturn->tell = tellImg2;
toReturn->getLength = getLengthImg2;
toReturn->close = closeImg2;
toReturn->type = AbstractFileTypeImg2;
return toReturn;
}
@ -147,6 +148,7 @@ AbstractFile* duplicateImg2File(AbstractFile* file, AbstractFile* backing) {
toReturn->tell = tellImg2;
toReturn->getLength = getLengthImg2;
toReturn->close = closeImg2;
toReturn->type = AbstractFileTypeImg2;
return toReturn;
}

Просмотреть файл

@ -249,7 +249,9 @@ AbstractFile* createAbstractFileFromImg3(AbstractFile* file) {
while(current != NULL) {
if(current->header->magic == IMG3_DATA_MAGIC) {
info->data = current;
break;
}
if(current->header->magic == IMG3_CERT_MAGIC) {
info->cert = current;
}
current = current->next;
}
@ -265,8 +267,24 @@ AbstractFile* createAbstractFileFromImg3(AbstractFile* file) {
toReturn->tell = tellImg3;
toReturn->getLength = getLengthImg3;
toReturn->close = closeImg3;
toReturn->type = AbstractFileTypeImg3;
return toReturn;
}
void replaceCertificateImg3(AbstractFile* file, AbstractFile* certificate) {
Img3Info* info = (Img3Info*) file->data;
info->cert->header->dataSize = certificate->getLength(certificate);
info->cert->header->size = info->cert->header->dataSize + sizeof(AppleImg3Header);
if(info->cert->data != NULL) {
free(info->cert->data);
}
info->cert->data = malloc(info->cert->header->dataSize);
certificate->read(certificate, info->cert->data, info->cert->header->dataSize);
info->dirty = TRUE;
}
AbstractFile* duplicateImg3File(AbstractFile* file, AbstractFile* backing) {
Img3Info* info;
AbstractFile* toReturn;

Просмотреть файл

@ -125,6 +125,7 @@ AbstractFile* createAbstractFileFromComp(AbstractFile* file) {
toReturn->tell = tellComp;
toReturn->getLength = getLengthComp;
toReturn->close = closeComp;
toReturn->type = AbstractFileTypeLZSS;
return toReturn;
}
@ -154,7 +155,8 @@ AbstractFile* duplicateCompFile(AbstractFile* file, AbstractFile* backing) {
toReturn->tell = tellComp;
toReturn->getLength = getLengthComp;
toReturn->close = closeComp;
toReturn->type = AbstractFileTypeLZSS;
return toReturn;
}

Просмотреть файл

@ -63,3 +63,66 @@ AbstractFile* duplicateAbstractFile(AbstractFile* file, AbstractFile* backing) {
return backing;
}
}
AbstractFile* duplicateAbstractFileWithCertificate(AbstractFile* file, AbstractFile* backing, AbstractFile* certificate) {
uint32_t signatureBE;
uint32_t signatureLE;
AbstractFile* orig;
AbstractFile* newFile;
file->seek(file, 0);
file->read(file, &signatureBE, sizeof(signatureBE));
signatureLE = signatureBE;
FLIPENDIAN(signatureBE);
FLIPENDIANLE(signatureLE);
file->seek(file, 0);
if(signatureBE == SIGNATURE_8900) {
orig = createAbstractFileFrom8900(file);
newFile = duplicate8900File(orig, backing);
replaceCertificate8900(newFile, certificate);
return duplicateAbstractFile(orig, newFile);
} else if(signatureLE == IMG2_SIGNATURE) {
orig = createAbstractFileFromImg2(file);
return duplicateAbstractFile(orig, duplicateImg2File(orig, backing));
} else if(signatureLE == IMG3_SIGNATURE) {
orig = createAbstractFileFromImg3(file);
newFile = duplicateImg3File(orig, backing);
replaceCertificateImg3(newFile, certificate);
return duplicateAbstractFile(orig, newFile);
} else if(signatureBE == COMP_SIGNATURE) {
orig = createAbstractFileFromComp(file);
return duplicateAbstractFile(orig, duplicateCompFile(orig, backing));
} else if(signatureBE == IBOOTIM_SIG_UINT) {
orig = createAbstractFileFromIBootIM(file);
return duplicateAbstractFile(orig, duplicateIBootIMFile(orig, backing));
} else {
file->close(file);
return backing;
}
}
void replaceCertificateAbstractFile(AbstractFile* file, AbstractFile* certificate) {
uint32_t signatureBE;
uint32_t signatureLE;
AbstractFile* f;
file->seek(file, 0);
file->read(file, &signatureBE, sizeof(signatureBE));
signatureLE = signatureBE;
FLIPENDIAN(signatureBE);
FLIPENDIANLE(signatureLE);
file->seek(file, 0);
if(signatureBE == SIGNATURE_8900) {
f = createAbstractFileFrom8900(file);
replaceCertificate8900(f, certificate);
f->close(f);
} else if(signatureLE == IMG3_SIGNATURE) {
f = createAbstractFileFromImg3(file);
replaceCertificateImg3(f, certificate);
f->close(f);
}
}