57int save_DHspki_with_private(
Buffer pki, FILE* fp, DH* dhkey)
62 if (fp==NULL || dhkey==NULL)
return FALSE;
67 pv = get_DHprivatekey(dhkey);
87Buffer read_DHspki_with_private(FILE* fp, DH** p_dhkey)
94 if (fp==NULL)
return pp;
98 if (pp.
buf==NULL)
return pp;
100 if (*p_dhkey!=NULL) DH_free(*p_dhkey);
102 if (*p_dhkey==NULL) {
111#if OPENSSL_VERSION_NUMBER < 0x10101000L
113 (*p_dhkey)->p = BN_bin2bn((
const unsigned char*)(pk.
buf), pk.
vldsz, NULL);
114 (*p_dhkey)->g = BN_bin2bn((
const unsigned char*)(gk.
buf), gk.
vldsz, NULL);
115 (*p_dhkey)->pub_key = BN_bin2bn((
const unsigned char*)(yk.
buf), yk.
vldsz, NULL);
118 BIGNUM* dhp_bn = BN_bin2bn((
const unsigned char*)(pk.
buf), pk.
vldsz, NULL);
119 BIGNUM* dhg_bn = BN_bin2bn((
const unsigned char*)(gk.
buf), gk.
vldsz, NULL);
120 BIGNUM* dhy_bn = BN_bin2bn((
const unsigned char*)(gk.
buf), gk.
vldsz, NULL);
122 if (dhp_bn!=NULL && dhg_bn!=NULL && dhy_bn!=NULL) {
123 DH_set0_pqg(*p_dhkey, dhp_bn, NULL, dhg_bn);
124 DH_set0_key(*p_dhkey, dhy_bn, NULL);
137 if (*p_dhkey!=NULL) n = DH_check(*p_dhkey, &code);
138 if (n!=1 || code!=0) {
139 if (*p_dhkey!=NULL) DH_free(*p_dhkey);
148 if (*p_dhkey!=NULL) DH_free(*p_dhkey);
153#if OPENSSL_VERSION_NUMBER < 0x10101000L
154 (*p_dhkey)->priv_key = BN_bin2bn((
const unsigned char*)(pv.
buf), pv.
vldsz, NULL);
156 BIGNUM* priv_key = BN_bin2bn((
const unsigned char*)(pv.
buf), pv.
vldsz, NULL);
157 DH_set0_key(*p_dhkey, NULL, priv_key);
186Buffer get_DHspki_ff(
char* filename,
int ks, DH** p_dhkey)
192 if (filename==NULL || p_dhkey==NULL)
return pki;
196 fp = fopen(filename,
"rb");
197 pki = read_DHspki_with_private(fp, p_dhkey);
201#if OPENSSL_VERSION_NUMBER < 0x10101000L
202 if (DH_size(*p_dhkey)<(ks+7)/8 || (*p_dhkey)->priv_key==NULL)
free_Buffer(&pki);
204 const BIGNUM* priv_key = DH_get0_priv_key(*p_dhkey);
205 if (DH_size(*p_dhkey)<(ks+7)/8 || priv_key==NULL)
free_Buffer(&pki);
212 pki = gen_DHspki(ks, p_dhkey);
217 save_DHspki_with_private(pki, fp, *p_dhkey);
242Buffer gen_DHspki(
int ks, DH** p_dhkey)
248 if (p_dhkey==NULL)
return pk;
252 if (!RAND_load_file(
"/dev/urandom", 1024))
return pk;
256 if (*p_dhkey!=NULL) DH_free(*p_dhkey);
259#if OPENSSL_VERSION_NUMBER < 0x10101000L
260 *p_dhkey = DH_generate_parameters(ks, DH_GENERATOR_2, NULL, NULL);
262 n = DH_generate_parameters_ex(*p_dhkey, ks, DH_GENERATOR_2, NULL);
264 n = DH_check(*p_dhkey, &code);
265 }
while (n!=1 || code!=0);
268 sz = DH_generate_key(*p_dhkey);
276 sz = i2d_DHparams(*p_dhkey, NULL);
283 pp.
vldsz = i2d_DHparams(*p_dhkey, &(px.
buf));
286#if OPENSSL_VERSION_NUMBER < 0x10101000L
287 sz = BN_num_bytes((*p_dhkey)->pub_key);
289 const BIGNUM* pub_key = DH_get0_pub_key(*p_dhkey);
290 sz = BN_num_bytes(pub_key);
301#if OPENSSL_VERSION_NUMBER < 0x10101000L
302 px.
vldsz = BN_bn2bin((*p_dhkey)->pub_key, px.
buf);
304 px.
vldsz = BN_bn2bin(pub_key, px.
buf);
307 pk = join_DHpubkey(pp, px);
336 if (p_dhkey==NULL)
return pk;
338 if (*p_dhkey!=NULL) DH_free(*p_dhkey);
340 if (*p_dhkey==NULL)
return pk;
342 pkey = get_DHPkey(pki);
343 gkey = get_DHGkey(pki);
344 if (pkey.
buf==NULL || gkey.
buf==NULL) {
352#if OPENSSL_VERSION_NUMBER < 0x10101000L
353 (*p_dhkey)->p = BN_bin2bn((
const unsigned char*)(pkey.
buf), pkey.
vldsz, NULL);
354 (*p_dhkey)->g = BN_bin2bn((
const unsigned char*)(gkey.
buf), gkey.
vldsz, NULL);
356 BIGNUM* dhp_bn = BN_bin2bn((
const unsigned char*)(pkey.
buf), pkey.
vldsz, NULL);
357 BIGNUM* dhg_bn = BN_bin2bn((
const unsigned char*)(gkey.
buf), gkey.
vldsz, NULL);
359 if (dhp_bn!=NULL && dhg_bn!=NULL) {
360 DH_set0_pqg(*p_dhkey, dhp_bn, NULL, dhg_bn);
374 if (*p_dhkey!=NULL) n = DH_check(*p_dhkey, &code);
375 if (n!=1 || code!=0) {
376 if (*p_dhkey!=NULL) DH_free(*p_dhkey);
381 sz = DH_generate_key(*p_dhkey);
389 sz = i2d_DHparams(*p_dhkey, NULL);
396 pp.
vldsz = i2d_DHparams(*p_dhkey, &(px.
buf));
398#if OPENSSL_VERSION_NUMBER < 0x10101000L
399 sz = BN_num_bytes((*p_dhkey)->pub_key);
401 const BIGNUM* pub_key = DH_get0_pub_key(*p_dhkey);
402 sz = BN_num_bytes(pub_key);
413#if OPENSSL_VERSION_NUMBER < 0x10101000L
414 px.
vldsz = BN_bn2bin((*p_dhkey)->pub_key, px.
buf);
416 px.
vldsz = BN_bn2bin(pub_key, px.
buf);
419 pk = join_DHpubkey(pp, px);
447 ykey = get_DHYkey(pki);
448 if (ykey.
buf==NULL)
return skey;
450 skey = get_DHsharedkey_fY(ykey, dhkey);
477 if (dhkey==NULL)
return buf;
479 BIGNUM* yk = BN_bin2bn((
const unsigned char*)(ykey.
buf), ykey.
vldsz, NULL);
531 if (pp.
buf==NULL)
return pp;
532 for (i=0; i<lp; i++) pp.
buf[i] = param.
buf[sz+i];
581 if (pp.
buf==NULL)
return pp;
582 for (i=0; i<lp; i++) pp.
buf[i] = param.
buf[sz+i];
636 if (pp.
buf==NULL)
return pp;
637 for (i=0; i<lp; i++) pp.
buf[i] = param.
buf[sz+i];
679 if (pp.
buf==NULL)
return pp;
680 for (i=0; i<lp; i++) pp.
buf[i] = param.
buf[sz+i];
694Buffer get_DHprivatekey(DH* dhkey)
699#if OPENSSL_VERSION_NUMBER < 0x10101000L
700 sz = BN_num_bytes(dhkey->priv_key);
703 const BIGNUM* priv_key = DH_get0_pub_key(dhkey);
704 sz = BN_num_bytes(priv_key);
708 if (pv.
buf==NULL)
return pv;
710#if OPENSSL_VERSION_NUMBER < 0x10101000L
711 pv.
vldsz = BN_bn2bin(dhkey->priv_key, pv.
buf);
714 pv.
vldsz = BN_bn2bin(priv_key, pv.
buf);
756 unsigned char dh_algor[]={0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x03,0x01};
758 int len_dh_algor = 11;
780 memcpy(px.
buf, pp.
buf, lp);
781 memcpy(px.
buf+lp, pm.
buf, ls);
791 memcpy(pp.
buf, dh_algor, la);
792 memcpy(pp.
buf+la, pm.
buf, ls);
805 memcpy(pm.
buf, px.
buf, ls);
806 memcpy(pm.
buf+ls, pp.
buf, lp);
#define JBXL_ASN1_BIT
BIT_STRING.
#define JBXL_ASN1_INT
INTEGER.
#define JBXL_ASN1_OBJ
OBJECT_IDENTIFIER.
#define JBXL_ASN1_SEQ_CNSTRCTD
SEQUENCE + CONSTRUCTED (構造化フラグ)
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Buffer init_Buffer()
初期化したBuffer型変数を返す.
int bufsz
確保してあるバッファの大きさ - 1.
int vldsz
データの長さ.バイナリデータの場合も使用可能.文字列の場合は 0x00 を含まない.
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.