交叉编译openssl
2019-07-13 00:14发布
生成海报
本次任务是要完成嵌入式Linux下对openssl程序的支持。
我的开发环境:ARM9开发板 和 嵌入式Linux操作系统。装有Linux系统(我的是ubuntu9.04)的PC机一台。串口和串口连接线。串口调试软件:minicom。交叉编译工具:arm-unknown-linux-gnu-gcc
下载最新版本的openssl-1.0.0:
http://www.openssl.org/source/
解压:$tar -xzvf openssl-1.0.0.tart.gz
$cd openssl-1.0.0
openssl-1.0.0$ ./config no-asm shared prefix=/home/lanpeng/arm-openssl
解释一下这里的参数: config就是配置脚本,这里不多说了。no-asm是在交叉编译过程中不使用汇编代码代码加速编译过程.原因是它的汇编代码是对arm格式不支持的。我试过了,如果没有这个参数,你的交叉编译一定出错。shared生成动态连接库。prefix=路径 就是你的安装目录。安装完成后在这个目录下会有四个文件夹:bin lib include ssl。这四个目录非常重要,openssl编程就需要这些目录的配合。
配置完成后我们还要修改Makefile文件(或许有更好的办法不用修改Makefile,这里请高手指点)。修改如下:
62 #CC= gcc
63 CC = arm-unknown-linux-gnu-gcc
64 #CFLAG= -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAV E_DLFCN_H -DL_ENDIAN -DTERMIO -O3 -fomit-frame-pointer -Wall
65 CFLAG= -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DOPENSSL_N O_KRB5 -DL_ENDIAN -DTERMIO -O3 -fomit-frame-pointer -Wall
66 DEPFLAG= -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MD2 -DOPENSSL_NO_R C5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_STORE
67 PEX_LIBS=
68 EX_LIBS= -ldl
69 EXE_EXT=
70 ARFLAGS=
71 #AR= ar $(ARFLAGS) r
72 AR = arm-unknown-linux-gnu-ar $(ARFLAGS) r
73 RANLIB = arm-unknown-linux-gnu-ranlib
74 #RANLIB= /usr/bin/ranlib
75 #NM= nm
76 NM = arm-unknown-linux-gnu-nm
77 PERL= /usr/bin/perl
78 TAR= tar
79 TARFLAGS= --no-recursion
80 MAKEDEPPROG= gcc
81 LIBDIR=lib
即使用交叉编译工具去编译openssl。
修改好了以后执行命令: opensl-1.0.0$ make
要编译5分钟左右,慢慢等吧。
然后在输入命令: opensl-1.0.0$ make install
没有错误!恭喜了,安装成功。
接下来就使用openssl来编程把。
我在网上找了一段程序,openssl栈的编程:myssl.c:
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<openssl/safestack.h>
#define sk_Student_new(st) SKM_sk_new(Student,
(st))
#define sk_Student_new_null() SKM_sk_new_null(Student)
#define sk_Student_free(st) SKM_sk_free(Student,
(st))
#define sk_Student_num(st) SKM_sk_num(Student,
(st))
#define sk_Student_value(st, i) SKM_sk_value(Student,
(st),
(i))
#define sk_Student_set(st, i, val) SKM_sk_set(Student,
(st),
(i),
(val))
#define sk_Student_zero(st) SKM_sk_zero(Student,
(st))
#define sk_Student_push(st, val) SKM_sk_push(Student,
(st),
(val))
#define sk_Student_unshift(st, val) SKM_sk_unshift(Student,
(st),
(val))
#define sk_Student_find(st, val) SKM_sk_find(Student,
(st),
(val))
#define sk_Student_delete(st, i) SKM_sk_delete(Student,
(st),
(i))
#define sk_Student_delete_ptr(st, ptr) SKM_sk_delete_ptr(Student,
(st),
(ptr))
#define sk_Student_insert(st, val, i) SKM_sk_insert(Student,
(st),
(val),
(i))
#define sk_Student_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(Student,
(st),
(cmp))
#define sk_Student_dup(st) SKM_sk_dup(Student, st)
#define sk_Student_pop_free(st, free_func) SKM_sk_pop_free(Student,
(st),
(free_func))
#define sk_Student_shift(st) SKM_sk_shift(Student,
(st))
#define sk_Student_pop(st) SKM_sk_pop(Student,
(st))
#define sk_Student_sort(st) SKM_sk_sort(Student,
(st))
typedef struct Student_st
{
char *name;
int age;
char *otherInfo;
}Student;
typedef STACK_OF(Student) Students;
Student *Student_Malloc()
{
Student *a=malloc(sizeof(Student));
a->name=malloc(20);
strcpy(a->name,"zcp");
a->otherInfo=malloc(20);
strcpy(a->otherInfo,"no info");
return a;
}
void Student_Free(Student
*a)
{
free(a->name);
free(a->otherInfo);
free(a);
}
static int Student_cmp(Student
*a,Student
*b)
{
int ret;
ret=strcmp(a->name,b->name);
return ret;
}
int main()
{
Students *s,*snew;
Student *s1,*one,*s2;
int i,num;
s=sk_Student_new_null();
snew=sk_Student_new(Student_cmp);
s2=Student_Malloc();
sk_Student_push(snew,s2);
i=sk_Student_find(snew,s2);
s1=Student_Malloc();
sk_Student_push(s,s1);
num=sk_Student_num(s);
for(i=0;i<num;i++)
{
one=sk_Student_value(s,i);
printf("student name : %s
",one->name);
printf("sutdent age : %d
",one->age);
printf("student otherinfo : %s
",one->otherInfo);
}
sk_Student_pop_free(s,Student_Free);
sk_Student_pop_free(snew,Student_Free);
return 0;
}
这里要用到安装目录下的include 和 lib库.所以我写了一个Makefile文件来执行它:
COMPILE := arm-unknown-linux-gnu-
CC := $(COMPILE)gcc
CFLAGS := -Wall -O2
CFLAGS += -I/home/lanpeng/arm-openssl/include/
LIBS := /home/lanpeng/arm-openssl/lib/libssl.a /home/lanpeng/arm-openssl/lib/libcrypto.a
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
hello: zhan.o $(LIBS)
$(CC) $(CFLAGS) -o $@ zhan.o -L.$(LIBS)
clean:
rm -fr *.o hello
在程序目录下执行make命令,编译通过!
注意:这里有一个的问题:就是在我的Makefile文件里LIBS的定义。它定义了两个静态库文件:libssl,libcrypto.这两个库文件的顺序不可以颠倒。必须先加入libssl库,再加入libcrypto库.
把hello文件下载到开发板里,执行 ./hello,结果如下:
student name : zcp
sutdent age : 0
student otherinfo : no info
恭喜了,开始你的嵌入式openssl开发之旅把!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮