今天有一个项目需要使用到 https, 以前一直用的都是http请求, 用 socket() 实现 https 请求我还真是头一回遇到。

先网上搜索了一下相关资料,明白了 https 相比较 http 就是多了一个认证,发送与接收的数据都是经过加密的,不能是明文。

然后说下载个openssl,用这个能实现https请求。

我平时很少用c写代码,再加上学习c的时候选了一条现在看来不是很理想的路(c & gcc & mingw & windows & codeblocks)

我到现在连基本的gcc 用法都不会。

花了一整天,终于完成 openssl在本地的编译,下面是我的操作步骤,收藏一下,不然下次又需要好久:

1. 下载 MinGW 和 MSYS,并安装 (可从http://www.mingw.org/下载, MSYS 也可以单独下载)

2. 下载 per 5.x, (百度搜索:ActivePerl , 就能下载)

3. 下载 openssl 源代码 (http://www.openssl.org/source/), 下载完成后可以解压缩到 C:\openssl-1.0.1g

4. 运行 MSYS 控制台:  msys.bat

5. cd /c/openssl-1.0.1g    (这是linux下的路径写法)

6.  编译方法见INSTALL  文件,以及INSTALL.WIN32文件

按说明书上,依次执行命令:(执行命令前,建议把360临时退出,编译速度会提高很多)

默认编译得到的是静态库:

$ ./config

$ make            (在我电脑上耗时: 10分钟左右)

$ make test       (在我电脑上耗时: 20分钟左右)

$ make install    (在我电脑上耗时: 30分钟左右)  注意,执行这一命令前,需要修改一下 Makefile 文件。

 编译动态库,只需添加参数 shared 即可:

$ ./config  shared

$ make

$ make test

$ make install

----------------------------------------------------------------------------------------------------

注:2018-8-25 从官网下载了 openssl-1.0.2p.tar.gz,然后解压缩,编译动态库。

编译完动态库后,接着使用 ./config 来编译静态库的时候,生成的静态库中 bin\openssl.exe 有问题。

只好重新解压缩一下 openssl-1.0.2p.tar.gz,使用 ./config 来重新编译静态库,成功编译。

本来想下载最新版: openssl-1.1.1-pre9.tar.gz, 但看到官网介绍说  1.1.1 版将是LTS版本,目前正在开发中,主要关注的是实施TLSv1.3。

由于稳定版还没有发布,同时看到一篇文章(https://blog.csdn.net/lostspeed/article/details/69492531)说,  如果由原来的 1.0.2 升级到 1.1.1 原来的项目代码中涉及到 openssl 的部分代码需要改动,我就暂时放弃升级到 1.1.1

----------------------------------------------------------------------------------------------------

2018-8-27

试着对 openssl 进行瘦身(裁剪), 因为项目中使用静态库生成的exe文件因openssl增加了 1.5M。

参考网上的一些例子:

openssl 剪裁: http://www.169it.com/blog_article/841427108.html

Build openssl with just RSA and AES: https://stackoverflow.com/questions/7827836/build-openssl-with-just-rsa-and-aes

重新编译:

./config no-idea no-camellia no-seed no-bf no-cast no-des no-rc2 no-rc4 no-rc5 no-md2 no-md4 no-ripemd no-mdc2 no-ssl2 no-ssl3

运行结果提示:

Configured for mingw.

*** Because of configuration changes, you MUST do the following before
*** building:

make depend

然后根据提示,执行命令: make depend

making depend in crypto...
make[1]: Entering directory `/c/openssl-1.0.2p/crypto'
../util/domd: line 35: cc: command not found
make[1]: *** [local_depend] Error 1
make[1]: Leaving directory `/c/openssl-1.0.2p/crypto'
make: *** [depend] Error 1

关于这个问题,继续从网上找解决方案: http://openssl.6102.n7.nabble.com/OpenSSL-1-0-1c-Mac-OS-X-no-XXX-and-missing-make-depend-td42920.html

部分内容:

Open the Makefile generated by ./config. Its located in the root of
openssl-1.0.1x. Scroll down to line 75 or so. Change
MAKEDEPPROG=makedepend to:

MAKEDEPPROG=$(CC) -M

And some things that don't work:

MAKEDEPPROG=/usr/bin/gcc -M
MAKEDEPPROG=gcc -M
MAKEDEPPROG=llv-gcc -M

根据上面的英文提示:

修改 Makefile 文件, 将 MAKEDEPPROG=... 修改为 MAKEDEPPROG=$(CC) -M

然后重试: make depend

不会再显示错误信息了。

接下来: make

然后:make test

...

signed content test streaming BER format, DSA key: OK
signed content test streaming BER format, 2 DSA and 2 RSA keys: OK
signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes: OK
signed content test streaming S/MIME format, 2 DSA and 2 RSA keys: OK
signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys: OK
enveloped content test streaming S/MIME format, 3 recipients: generation error
make[1]: *** [test_cms] Error 1
make[1]: Leaving directory `/c/openssl-1.0.2p/test'
make: *** [tests] Error 2

具体错误是什么,我没看明白。

最后:make install

瘦身的操作完工,这个时候我看了一下 lib 文件夹中的 libcrypto.a 和 libssl.a 文件大小显示:

libcrypto.a    (2941KB)

libssl.a          (539KB)

合计: 3.39M

我又去查看了一下不做任何瘦身操作生成的这两个文件的大小:

libcrypto.a    (3180KB)

libssl.a          (548KB)

合计:3.63M

也就是说通过 ./config no-idea no-camellia no-seed no-bf no-cast no-des no-rc2 no-rc4 no-rc5 no-md2 no-md4 no-ripemd no-mdc2 no-ssl2 no-ssl3

libcrypto.a 和 libssl.a 文件大小一共减少了 0.24M

我原来的项目 exe 文件大小: 2083KB,然后我用瘦身后的 openssl 重新编译我的项目,生成的 exe 文件大小:2010KB, 项目 exe 文件大小减少了 73KB。

总结:瘦身 openssl 的结果不太令我满意,只减少了 73KB。

----------------------------------------------------------------------------------------------------

下面摘自其 INSTALL 说明文件

Configuration Options

---------------------

There are several options to ./config (or ./Configure) to customize

the build:         .....

shared        In addition to the usual static libraries, create shared

libraries on platforms where it's supported.  See "Note on

shared libraries" below.

make install完毕之后,会在D:\MinGW\msys\1.0\local\ssl的目录下找到openssl库,包括了头文件,dll文件等

---------------------------------------------------------------------------------

我在第一次执行 make install 时, 花了差不多30分钟执行完成,但最后抛出一个错误提示:

installing libcrypto.a

/bin/sh: line 5: CodeBlocksMinGWbin/ranlib.exe: No such file or directory

installing libssl.a

/bin/sh: line 5: CodeBlocksMinGWbin/ranlib.exe: No such file or directory

make: *** [install_sw] Error 1

看这个意思,是与 ranlib.exe 有关,没找到文件。

由于在 MYSY控制台,路径名都像 linux一样, 我打开 Makefile 文件,搜索: ranlib.exe

找到一行: RANLIB= \CodeBlocks\MinGW\bin/ranlib.exe

看这个文件名路径怪怪的,又有 '\', 又有 '/', 我搜索了一下本地电脑上的 ranlib.exe, 找到文件位置后, 我修改为:

RANLIB= /c/CodeBlocks/MinGW/bin/ranlib.exe (也就是 c:\CodeBlocks\MinGW\bin\ranlib.exe)

保存,然后重新执行 make install, 不会再抛出错误。

---------------------------------------------------------------------------------

相关资料:

mingw下编译openssl

http://blog.csdn.net/cibiren2011/article/details/10095021

用MinGW编译openssl

http://blog.csdn.net/feiyunw/article/details/5597546

MinGW编译支持openssl-1.0.0a的libcurl-7.21.3

http://blog.csdn.net/yui/article/details/6170889

How to build OpenSSL with MinGW in WIndows?

http://stackoverflow.com/questions/9379363/how-to-build-openssl-with-mingw-in-windows

在Windows下编译OpenSSL(VS2005)

http://lwglucky.blog.51cto.com/1228348/325483

2014-05-06

最新文章

  1. 安装ArcGIS Desktop 9.3
  2. 编译原理-词法分析05-正则表达式到DFA-01
  3. jQuery之DOM属性
  4. destoon去掉会员注册email验证
  5. LeetCode-Search a 2D Matrix
  6. Mysql 5.7.10以上版本安装大坑
  7. iptables相关
  8. "npm ERR! Error: EPERM: operation not permitted"问题解决
  9. 分布式数据库hbase详解
  10. Java String.compareTo()方法
  11. [CC150] Get all permutations of a string
  12. 动态加载JS文件,并根据JS文件的加载状态来执行自己的回调函数
  13. Mongodb数据更新命令
  14. chrome:// 的秘密!!一些有用的命令!
  15. Composer配置中国镜像
  16. break在switch中的使用例子
  17. asp.net使用unescape读取js escape编码过的字符串
  18. PS2键盘 + LCD12864 实验
  19. 循环语句——do…while语句
  20. 分布式架构实战--ActiveMQ的安装与使用(单节点)

热门文章

  1. 从源码的角度再看 React JS 中的 setState
  2. springboot整合curator实现分布式锁
  3. 关于git的一些体会:
  4. NumsCount (java)
  5. EnglishGame
  6. junit4实验报告
  7. 第三个Sprint冲刺第6天
  8. Beta 冲刺报告模板
  9. [2017BUAA软工]第一次个人项目 数独的生成与求解
  10. 传输层中的协议 TCP & UDP