Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Sources / CXKit / mobileprovision.c

mobileprovision.c68 lines · 1.7 KiBRaw
1//
2// mobileprovision_utils.c
3// XKit
4//
5// Created by Kabir Oberai on 05/07/19.
6// Copyright © 2019 Kabir Oberai. All rights reserved.
7//
8
9#include <stdlib.h>
10#include <string.h>
11#include <openssl/pkcs7.h>
12#include <openssl/bio.h>
13#include "mobileprovision.h"
14
15struct mobileprovision {
16 PKCS7 *raw;
17};
18
19static mobileprovision_t mobileprovision_create(PKCS7 *raw) {
20 mobileprovision_t profile = malloc(sizeof(struct mobileprovision));
21 if (!profile) return NULL;
22
23 profile->raw = raw;
24 return profile;
25}
26
27mobileprovision_t mobileprovision_create_from_data(const void *data, size_t len) {
28 PKCS7 *raw = NULL;
29 d2i_PKCS7(&raw, (const unsigned char **)&data, len);
30 if (!raw) return NULL;
31
32 return mobileprovision_create(raw);
33}
34
35void mobileprovision_free(mobileprovision_t profile) {
36 PKCS7_free(profile->raw);
37 free(profile);
38}
39
40void *mobileprovision_copy_data(mobileprovision_t profile, size_t *len) {
41 unsigned char *data = NULL;
42 size_t data_len = i2d_PKCS7(profile->raw, &data);
43 if (data_len < 0) return NULL;
44 *len = data_len;
45 void *ret = malloc(data_len);
46
47 if (!ret) {
48 OPENSSL_free(data);
49 return NULL;
50 }
51
52 memcpy(ret, data, data_len);
53 OPENSSL_free(data);
54 return ret;
55}
56
57const void *mobileprovision_get_digest(mobileprovision_t profile, size_t *len) {
58 // we could dig into the struct ourselves instead but using the
59 // documented API is better.
60 BIO *digest_bio = PKCS7_dataDecode(profile->raw, NULL, NULL, NULL);
61 if (!digest_bio) return NULL;
62 char *data = NULL;
63 long digest_len = BIO_get_mem_data(digest_bio, &data);
64 // the mem pointer is owned by `profile`, not the bio
65 BIO_free_all(digest_bio);
66 if (len) *len = digest_len;
67 return data;
68}