3 #include <openssl/bn.h>
4 #include <openssl/rsa.h>
5 #include <openssl/dsa.h>
12 void write_rsa(FILE *fout, char *estr, int elen, char *nstr, int nlen)
21 r1 = BN_bin2bn(estr, elen, rsa->e);
22 r2 = BN_bin2bn(nstr, nlen, rsa->n);
24 PEM_write_RSA_PUBKEY(fout, rsa);
29 void write_dsa(FILE *fout, char *pstr, int plen, char *qstr, int qlen, char *gstr, int glen, char *pkstr, int pklen)
37 dsa->pub_key = BN_new();
39 BN_bin2bn(pstr, plen, dsa->p);
40 BN_bin2bn(qstr, qlen, dsa->q);
41 BN_bin2bn(gstr, glen, dsa->g);
42 BN_bin2bn(pkstr, pklen, dsa->pub_key);
44 PEM_write_DSA_PUBKEY(fout, dsa);
49 int get_str(char **src, int *len, char *dest)
51 int *iptr = (int*) (*src);
52 int thislen = ntohl(*iptr);
58 // fprintf(stdout, "thislen = %d\n", thislen);
61 fprintf(stdout, "thislen(%d) > *len(%d)\n", thislen, *len);
65 memcpy(dest, *src, thislen);
67 (*len) = (*len) - thislen;
68 (*src) = (*src) + thislen;
71 *(dest + thislen) = '\0';
76 int openssh_binary_to_openssl(char *s, int len, FILE *fout)
78 char keytype[1024], estr[1024], nstr[1024], pstr[1024], qstr[1024], gstr[1024], pkstr[1024];
79 int elen, nlen, plen, qlen, glen, pklen;
82 result = get_str(&s, &len, keytype);
87 fprintf(stdout, "keytype = %s\n", keytype);
89 if (strcmp(keytype, "ssh-rsa") == 0) {
90 elen = get_str(&s, &len, estr);
91 // fprintf(stdout, "elen = %d\n", elen);
95 nlen = get_str(&s, &len, nstr);
96 // fprintf(stdout, "nlen = %d\n", nlen);
100 write_rsa(fout, estr, elen, nstr, nlen);
101 } else if (strcmp(keytype, "ssh-dss") == 0) {
102 plen = get_str(&s, &len, pstr);
103 // fprintf(stdout, "plen = %d\n", plen);
107 qlen = get_str(&s, &len, qstr);
108 // fprintf(stdout, "qlen = %d\n", qlen);
112 glen = get_str(&s, &len, gstr);
113 // fprintf(stdout, "glen = %d\n", glen);
117 pklen = get_str(&s, &len, pkstr);
118 // fprintf(stdout, "pklen = %d\n", pklen);
122 write_dsa(fout, pstr, plen, qstr, qlen, gstr, glen, pkstr, pklen);