0001-Make-code-work-with-OpenSSL-1.1.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. From 86931f9d7c3d73b97010e598a5ad41ea4fab2b63 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Reimar=20D=C3=B6ffinger?= <Reimar.Doeffinger@gmx.de>
  3. Date: Sun, 12 Mar 2017 23:00:12 +0100
  4. Subject: [PATCH] Make code work with OpenSSL 1.1.
  5. Changes in consist of:
  6. - Use EVP_MD_CTX_new/free API instead of on-stack allocation
  7. - Remove some M_ prefixes like for ASN1_IA5STRING_new
  8. - Remove pagehash functionality because it is useless to me and
  9. fixing it would be a pain. Would require declaring a few
  10. ASN_SEQUENCES and use that to get the required i2d functions
  11. from what I could find out.
  12. - Remove OBJ_create calls that seem to serve no purpose,
  13. now crash because NULL pointers are no longer handled
  14. (who changes API that way?!) and even if that was fixed
  15. lead to errors when these objects are later created
  16. again/"for real" by OBJ_txt2nid or OBJ_txt2obj (I think,
  17. did not investigate further).
  18. diff --git a/osslsigncode.c b/osslsigncode.c
  19. index 2978c02..3797458 100644
  20. --- a/osslsigncode.c
  21. +++ b/osslsigncode.c
  22. @@ -450,16 +450,16 @@ static SpcSpOpusInfo* createOpus(const char *desc, const char *url)
  23. if (desc) {
  24. info->programName = SpcString_new();
  25. info->programName->type = 1;
  26. - info->programName->value.ascii = M_ASN1_IA5STRING_new();
  27. - ASN1_STRING_set((ASN1_STRING *)info->programName->value.ascii,
  28. + info->programName->value.ascii = ASN1_IA5STRING_new();
  29. + ASN1_STRING_set(info->programName->value.ascii,
  30. (const unsigned char*)desc, strlen(desc));
  31. }
  32. if (url) {
  33. info->moreInfo = SpcLink_new();
  34. info->moreInfo->type = 0;
  35. - info->moreInfo->value.url = M_ASN1_IA5STRING_new();
  36. - ASN1_STRING_set((ASN1_STRING *)info->moreInfo->value.url,
  37. + info->moreInfo->value.url = ASN1_IA5STRING_new();
  38. + ASN1_STRING_set(info->moreInfo->value.url,
  39. (const unsigned char*)url, strlen(url));
  40. }
  41. @@ -609,19 +609,20 @@ static int add_timestamp(PKCS7 *sig, char *url, char *proxy, int rfc3161, const
  42. if (rfc3161) {
  43. unsigned char mdbuf[EVP_MAX_MD_SIZE];
  44. - EVP_MD_CTX mdctx;
  45. + EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  46. - EVP_MD_CTX_init(&mdctx);
  47. - EVP_DigestInit(&mdctx, md);
  48. - EVP_DigestUpdate(&mdctx, si->enc_digest->data, si->enc_digest->length);
  49. - EVP_DigestFinal(&mdctx, mdbuf, NULL);
  50. + EVP_DigestInit(mdctx, md);
  51. + EVP_DigestUpdate(mdctx, si->enc_digest->data, si->enc_digest->length);
  52. + EVP_DigestFinal(mdctx, mdbuf, NULL);
  53. + EVP_MD_CTX_free(mdctx);
  54. + mdctx = NULL;
  55. TimeStampReq *req = TimeStampReq_new();
  56. ASN1_INTEGER_set(req->version, 1);
  57. req->messageImprint->digestAlgorithm->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
  58. req->messageImprint->digestAlgorithm->parameters = ASN1_TYPE_new();
  59. req->messageImprint->digestAlgorithm->parameters->type = V_ASN1_NULL;
  60. - M_ASN1_OCTET_STRING_set(req->messageImprint->digest, mdbuf, EVP_MD_size(md));
  61. + ASN1_OCTET_STRING_set(req->messageImprint->digest, mdbuf, EVP_MD_size(md));
  62. req->certReq = (void*)0x1;
  63. len = i2d_TimeStampReq(req, NULL);
  64. @@ -921,83 +922,8 @@ static const unsigned char classid_page_hash[] = {
  65. 0xAE, 0x05, 0xA2, 0x17, 0xDA, 0x8E, 0x60, 0xD6
  66. };
  67. -static unsigned char *calc_page_hash(char *indata, unsigned int peheader, int pe32plus,
  68. - unsigned int sigpos, int phtype, unsigned int *phlen);
  69. -
  70. -DECLARE_STACK_OF(ASN1_OCTET_STRING)
  71. -#ifndef sk_ASN1_OCTET_STRING_new_null
  72. -#define sk_ASN1_OCTET_STRING_new_null() SKM_sk_new_null(ASN1_OCTET_STRING)
  73. -#define sk_ASN1_OCTET_STRING_free(st) SKM_sk_free(ASN1_OCTET_STRING, (st))
  74. -#define sk_ASN1_OCTET_STRING_push(st, val) SKM_sk_push(ASN1_OCTET_STRING, (st), (val))
  75. -#define i2d_ASN1_SET_OF_ASN1_OCTET_STRING(st, pp, i2d_func, ex_tag, ex_class, is_set) \
  76. - SKM_ASN1_SET_OF_i2d(ASN1_OCTET_STRING, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
  77. -#endif
  78. -
  79. -DECLARE_STACK_OF(SpcAttributeTypeAndOptionalValue)
  80. -#ifndef sk_SpcAttributeTypeAndOptionalValue_new_null
  81. -#define sk_SpcAttributeTypeAndOptionalValue_new_null() SKM_sk_new_null(SpcAttributeTypeAndOptionalValue)
  82. -#define sk_SpcAttributeTypeAndOptionalValue_free(st) SKM_sk_free(SpcAttributeTypeAndOptionalValue, (st))
  83. -#define sk_SpcAttributeTypeAndOptionalValue_push(st, val) SKM_sk_push(SpcAttributeTypeAndOptionalValue, (st), (val))
  84. -#define i2d_SpcAttributeTypeAndOptionalValue(st, pp, i2d_func, ex_tag, ex_class, is_set) \
  85. - SKM_ASN1_SET_OF_i2d(SpcAttributeTypeAndOptionalValue, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
  86. -#endif
  87. -
  88. -static SpcLink *get_page_hash_link(int phtype, char *indata, unsigned int peheader, int pe32plus, unsigned int sigpos)
  89. -{
  90. - unsigned int phlen;
  91. - unsigned char *ph = calc_page_hash(indata, peheader, pe32plus, sigpos, phtype, &phlen);
  92. - if (!ph) {
  93. - fprintf(stderr, "Failed to calculate page hash\n");
  94. - exit(-1);
  95. - }
  96. -
  97. - ASN1_OCTET_STRING *ostr = M_ASN1_OCTET_STRING_new();
  98. - M_ASN1_OCTET_STRING_set(ostr, ph, phlen);
  99. - free(ph);
  100. -
  101. - STACK_OF(ASN1_OCTET_STRING) *oset = sk_ASN1_OCTET_STRING_new_null();
  102. - sk_ASN1_OCTET_STRING_push(oset, ostr);
  103. - unsigned char *p, *tmp;
  104. - unsigned int l;
  105. - l = i2d_ASN1_SET_OF_ASN1_OCTET_STRING(oset, NULL, i2d_ASN1_OCTET_STRING,
  106. - V_ASN1_SET, V_ASN1_UNIVERSAL, IS_SET);
  107. - tmp = p = OPENSSL_malloc(l);
  108. - i2d_ASN1_SET_OF_ASN1_OCTET_STRING(oset, &tmp, i2d_ASN1_OCTET_STRING,
  109. - V_ASN1_SET, V_ASN1_UNIVERSAL, IS_SET);
  110. - ASN1_OCTET_STRING_free(ostr);
  111. - sk_ASN1_OCTET_STRING_free(oset);
  112. -
  113. - SpcAttributeTypeAndOptionalValue *aval = SpcAttributeTypeAndOptionalValue_new();
  114. - aval->type = OBJ_txt2obj((phtype == NID_sha1) ? SPC_PE_IMAGE_PAGE_HASHES_V1 : SPC_PE_IMAGE_PAGE_HASHES_V2, 1);
  115. - aval->value = ASN1_TYPE_new();
  116. - aval->value->type = V_ASN1_SET;
  117. - aval->value->value.set = ASN1_STRING_new();
  118. - ASN1_STRING_set(aval->value->value.set, p, l);
  119. - OPENSSL_free(p);
  120. -
  121. - STACK_OF(SpcAttributeTypeAndOptionalValue) *aset = sk_SpcAttributeTypeAndOptionalValue_new_null();
  122. - sk_SpcAttributeTypeAndOptionalValue_push(aset, aval);
  123. - l = i2d_SpcAttributeTypeAndOptionalValue(aset, NULL, i2d_SpcAttributeTypeAndOptionalValue,
  124. - V_ASN1_SET, V_ASN1_UNIVERSAL, IS_SET);
  125. - tmp = p = OPENSSL_malloc(l);
  126. - l = i2d_SpcAttributeTypeAndOptionalValue(aset, &tmp, i2d_SpcAttributeTypeAndOptionalValue,
  127. - V_ASN1_SET, V_ASN1_UNIVERSAL, IS_SET);
  128. - sk_SpcAttributeTypeAndOptionalValue_free(aset);
  129. - SpcAttributeTypeAndOptionalValue_free(aval);
  130. -
  131. - SpcSerializedObject *so = SpcSerializedObject_new();
  132. - M_ASN1_OCTET_STRING_set(so->classId, classid_page_hash, sizeof(classid_page_hash));
  133. - M_ASN1_OCTET_STRING_set(so->serializedData, p, l);
  134. - OPENSSL_free(p);
  135. -
  136. - SpcLink *link = SpcLink_new();
  137. - link->type = 1;
  138. - link->value.moniker = so;
  139. - return link;
  140. -}
  141. -
  142. static void get_indirect_data_blob(u_char **blob, int *len, const EVP_MD *md, file_type_t type,
  143. - int pagehash, char *indata, unsigned int peheader, int pe32plus,
  144. + char *indata, unsigned int peheader, int pe32plus,
  145. unsigned int sigpos)
  146. {
  147. static const unsigned char msistr[] = {
  148. @@ -1024,14 +950,7 @@ static void get_indirect_data_blob(u_char **blob, int *len, const EVP_MD *md, fi
  149. } else if (type == FILE_TYPE_PE) {
  150. SpcPeImageData *pid = SpcPeImageData_new();
  151. ASN1_BIT_STRING_set(pid->flags, (unsigned char*)"0", 0);
  152. - if (pagehash) {
  153. - int phtype = NID_sha1;
  154. - if (EVP_MD_size(md) > EVP_MD_size(EVP_sha1()))
  155. - phtype = NID_sha256;
  156. - pid->file = get_page_hash_link(phtype, indata, peheader, pe32plus, sigpos);
  157. - } else {
  158. - pid->file = get_obsolete_link();
  159. - }
  160. + pid->file = get_obsolete_link();
  161. l = i2d_SpcPeImageData(pid, NULL);
  162. p = OPENSSL_malloc(l);
  163. i2d_SpcPeImageData(pid, &p);
  164. @@ -1046,7 +965,7 @@ static void get_indirect_data_blob(u_char **blob, int *len, const EVP_MD *md, fi
  165. ASN1_INTEGER_set(si->d, 0);
  166. ASN1_INTEGER_set(si->e, 0);
  167. ASN1_INTEGER_set(si->f, 0);
  168. - M_ASN1_OCTET_STRING_set(si->string, msistr, sizeof(msistr));
  169. + ASN1_OCTET_STRING_set(si->string, msistr, sizeof(msistr));
  170. l = i2d_SpcSipInfo(si, NULL);
  171. p = OPENSSL_malloc(l);
  172. i2d_SpcSipInfo(si, &p);
  173. @@ -1068,7 +987,7 @@ static void get_indirect_data_blob(u_char **blob, int *len, const EVP_MD *md, fi
  174. hashlen = EVP_MD_size(md);
  175. hash = OPENSSL_malloc(hashlen);
  176. memset(hash, 0, hashlen);
  177. - M_ASN1_OCTET_STRING_set(idc->messageDigest->digest, hash, hashlen);
  178. + ASN1_OCTET_STRING_set(idc->messageDigest->digest, hash, hashlen);
  179. OPENSSL_free(hash);
  180. *len = i2d_SpcIndirectDataContent(idc, NULL);
  181. @@ -1923,19 +1842,18 @@ static void calc_pe_digest(BIO *bio, const EVP_MD *md, unsigned char *mdbuf,
  182. unsigned int peheader, int pe32plus, unsigned int fileend)
  183. {
  184. static unsigned char bfb[16*1024*1024];
  185. - EVP_MD_CTX mdctx;
  186. + EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  187. - EVP_MD_CTX_init(&mdctx);
  188. - EVP_DigestInit(&mdctx, md);
  189. + EVP_DigestInit(mdctx, md);
  190. memset(mdbuf, 0, EVP_MAX_MD_SIZE);
  191. (void)BIO_seek(bio, 0);
  192. BIO_read(bio, bfb, peheader + 88);
  193. - EVP_DigestUpdate(&mdctx, bfb, peheader + 88);
  194. + EVP_DigestUpdate(mdctx, bfb, peheader + 88);
  195. BIO_read(bio, bfb, 4);
  196. BIO_read(bio, bfb, 60+pe32plus*16);
  197. - EVP_DigestUpdate(&mdctx, bfb, 60+pe32plus*16);
  198. + EVP_DigestUpdate(mdctx, bfb, 60+pe32plus*16);
  199. BIO_read(bio, bfb, 8);
  200. unsigned int n = peheader + 88 + 4 + 60+pe32plus*16 + 8;
  201. @@ -1946,11 +1864,12 @@ static void calc_pe_digest(BIO *bio, const EVP_MD *md, unsigned char *mdbuf,
  202. int l = BIO_read(bio, bfb, want);
  203. if (l <= 0)
  204. break;
  205. - EVP_DigestUpdate(&mdctx, bfb, l);
  206. + EVP_DigestUpdate(mdctx, bfb, l);
  207. n += l;
  208. }
  209. - EVP_DigestFinal(&mdctx, mdbuf, NULL);
  210. + EVP_DigestFinal(mdctx, mdbuf, NULL);
  211. + EVP_MD_CTX_free(mdctx);
  212. }
  213. @@ -2019,16 +1938,15 @@ static unsigned char *calc_page_hash(char *indata, unsigned int peheader, int pe
  214. int phlen = pphlen * (3 + nsections + sigpos / pagesize);
  215. unsigned char *res = malloc(phlen);
  216. unsigned char *zeroes = calloc(pagesize, 1);
  217. - EVP_MD_CTX mdctx;
  218. -
  219. - EVP_MD_CTX_init(&mdctx);
  220. - EVP_DigestInit(&mdctx, md);
  221. - EVP_DigestUpdate(&mdctx, indata, peheader + 88);
  222. - EVP_DigestUpdate(&mdctx, indata + peheader + 92, 60 + pe32plus*16);
  223. - EVP_DigestUpdate(&mdctx, indata + peheader + 160 + pe32plus*16, hdrsize - (peheader + 160 + pe32plus*16));
  224. - EVP_DigestUpdate(&mdctx, zeroes, pagesize - hdrsize);
  225. + EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  226. +
  227. + EVP_DigestInit(mdctx, md);
  228. + EVP_DigestUpdate(mdctx, indata, peheader + 88);
  229. + EVP_DigestUpdate(mdctx, indata + peheader + 92, 60 + pe32plus*16);
  230. + EVP_DigestUpdate(mdctx, indata + peheader + 160 + pe32plus*16, hdrsize - (peheader + 160 + pe32plus*16));
  231. + EVP_DigestUpdate(mdctx, zeroes, pagesize - hdrsize);
  232. memset(res, 0, 4);
  233. - EVP_DigestFinal(&mdctx, res + 4, NULL);
  234. + EVP_DigestFinal(mdctx, res + 4, NULL);
  235. unsigned short sizeofopthdr = GET_UINT16_LE(indata + peheader + 20);
  236. char *sections = indata + peheader + 24 + sizeofopthdr;
  237. @@ -2040,18 +1958,20 @@ static unsigned char *calc_page_hash(char *indata, unsigned int peheader, int pe
  238. unsigned int l;
  239. for (l=0; l < rs; l+=pagesize, pi++) {
  240. PUT_UINT32_LE(ro + l, res + pi*pphlen);
  241. - EVP_DigestInit(&mdctx, md);
  242. + EVP_DigestInit(mdctx, md);
  243. if (rs - l < pagesize) {
  244. - EVP_DigestUpdate(&mdctx, indata + ro + l, rs - l);
  245. - EVP_DigestUpdate(&mdctx, zeroes, pagesize - (rs - l));
  246. + EVP_DigestUpdate(mdctx, indata + ro + l, rs - l);
  247. + EVP_DigestUpdate(mdctx, zeroes, pagesize - (rs - l));
  248. } else {
  249. - EVP_DigestUpdate(&mdctx, indata + ro + l, pagesize);
  250. + EVP_DigestUpdate(mdctx, indata + ro + l, pagesize);
  251. }
  252. - EVP_DigestFinal(&mdctx, res + pi*pphlen + 4, NULL);
  253. + EVP_DigestFinal(mdctx, res + pi*pphlen + 4, NULL);
  254. }
  255. lastpos = ro + rs;
  256. sections += 40;
  257. }
  258. + EVP_MD_CTX_free(mdctx);
  259. + mdctx = NULL;
  260. PUT_UINT32_LE(lastpos, res + pi*pphlen);
  261. memset(res + pi*pphlen + 4, 0, EVP_MD_size(md));
  262. pi++;
  263. @@ -2413,7 +2333,7 @@ int main(int argc, char **argv)
  264. int nturl = 0, ntsurl = 0;
  265. int addBlob = 0;
  266. u_char *p = NULL;
  267. - int ret = 0, i, len = 0, jp = -1, pe32plus = 0, comm = 0, pagehash = 0;
  268. + int ret = 0, i, len = 0, jp = -1, pe32plus = 0, comm = 0;
  269. unsigned int tmp, peheader = 0, padlen = 0;
  270. off_t filesize, fileend, sigfilesize, sigfileend, outdatasize;
  271. file_type_t type;
  272. @@ -2448,13 +2368,6 @@ int main(int argc, char **argv)
  273. ERR_load_crypto_strings();
  274. OPENSSL_add_all_algorithms_conf();
  275. - /* create some MS Authenticode OIDS we need later on */
  276. - if (!OBJ_create(SPC_STATEMENT_TYPE_OBJID, NULL, NULL) ||
  277. - !OBJ_create(SPC_MS_JAVA_SOMETHING, NULL, NULL) ||
  278. - !OBJ_create(SPC_SP_OPUS_INFO_OBJID, NULL, NULL) ||
  279. - !OBJ_create(SPC_NESTED_SIGNATURE_OBJID, NULL, NULL))
  280. - DO_EXIT_0("Failed to add objects\n");
  281. -
  282. md = EVP_sha1();
  283. if (argc > 1) {
  284. @@ -2531,8 +2444,6 @@ int main(int argc, char **argv)
  285. readpass = *(++argv);
  286. } else if ((cmd == CMD_SIGN) && !strcmp(*argv, "-comm")) {
  287. comm = 1;
  288. - } else if ((cmd == CMD_SIGN) && !strcmp(*argv, "-ph")) {
  289. - pagehash = 1;
  290. } else if ((cmd == CMD_SIGN) && !strcmp(*argv, "-n")) {
  291. if (--argc < 1) usage(argv0);
  292. desc = *(++argv);
  293. @@ -3243,7 +3154,7 @@ int main(int argc, char **argv)
  294. p7x = NULL;
  295. }
  296. - get_indirect_data_blob(&p, &len, md, type, pagehash, indata, peheader, pe32plus, fileend);
  297. + get_indirect_data_blob(&p, &len, md, type, indata, peheader, pe32plus, fileend);
  298. len -= EVP_MD_size(md);
  299. memcpy(buf, p, len);
  300. OPENSSL_free(p);
  301. --
  302. 2.34.1