1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
|
/* $Id: */
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/dsa.h>
#include <openssl/ssl.h>
#include <openssl/opensslv.h>
#ifdef __cplusplus
}
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static void DSA_get0_pqg(const DSA *d,
const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
{
if (p != NULL)
*p = d->p;
if (q != NULL)
*q = d->q;
if (g != NULL)
*g = d->g;
}
static int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
{
/* If the fields p, q and g in d are NULL, the corresponding input
* parameters MUST be non-NULL.
*/
if ((d->p == NULL && p == NULL)
|| (d->q == NULL && q == NULL)
|| (d->g == NULL && g == NULL))
return 0;
if (p != NULL) {
BN_free(d->p);
d->p = p;
}
if (q != NULL) {
BN_free(d->q);
d->q = q;
}
if (g != NULL) {
BN_free(d->g);
d->g = g;
}
return 1;
}
static void DSA_get0_key(const DSA *d,
const BIGNUM **pub_key, const BIGNUM **priv_key)
{
if (pub_key != NULL)
*pub_key = d->pub_key;
if (priv_key != NULL)
*priv_key = d->priv_key;
}
static int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
{
/* If the field pub_key in d is NULL, the corresponding input
* parameters MUST be non-NULL. The priv_key field may
* be left NULL.
*/
if (d->pub_key == NULL && pub_key == NULL)
return 0;
if (pub_key != NULL) {
BN_free(d->pub_key);
d->pub_key = pub_key;
}
if (priv_key != NULL) {
BN_free(d->priv_key);
d->priv_key = priv_key;
}
return 1;
}
static void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr,
const BIGNUM **ps)
{
if (pr != NULL)
*pr = sig->r;
if (ps != NULL)
*ps = sig->s;
}
static int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
{
if (r == NULL || s == NULL)
return 0;
BN_clear_free(sig->r);
BN_clear_free(sig->s);
sig->r = r;
sig->s = s;
return 1;
}
#endif
MODULE = Crypt::OpenSSL::DSA PACKAGE = Crypt::OpenSSL::DSA
PROTOTYPES: DISABLE
BOOT:
#if OPENSSL_VERSION_NUMBER < 0x10100000L
ERR_load_crypto_strings();
#endif
DSA *
new(CLASS)
char * CLASS
CODE:
RETVAL = DSA_new();
OUTPUT:
RETVAL
void
DESTROY(dsa)
DSA *dsa
CODE:
DSA_free(dsa);
DSA *
generate_parameters(CLASS, bits, seed = NULL)
char * CLASS
int bits
SV * seed
PREINIT:
DSA * dsa;
STRLEN seed_len = 0;
char * seedpv = NULL;
unsigned long err;
CODE:
if (seed) {
seedpv = SvPV(seed, seed_len);
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L
dsa = DSA_generate_parameters(bits, seedpv, (int)seed_len, NULL, NULL, NULL, NULL);
if (!dsa) {
#else
dsa = DSA_new();
if (!DSA_generate_parameters_ex(dsa, bits, seedpv, (int)seed_len, NULL, NULL, NULL)) {
#endif
err = ERR_get_error();
if (err == 0) {
croak("DSA_generate_parameters() returned NULL");
}
else {
croak("%s", ERR_reason_error_string(err));
}
}
RETVAL = dsa;
OUTPUT:
RETVAL
int
generate_key(dsa)
DSA * dsa
CODE:
RETVAL = DSA_generate_key(dsa);
OUTPUT:
RETVAL
int
get_sig_size(dsa)
DSA * dsa
CODE:
RETVAL = DSA_size(dsa);
OUTPUT:
RETVAL
DSA_SIG *
do_sign(dsa, dgst)
DSA * dsa
SV * dgst
PREINIT:
DSA_SIG * sig;
char * CLASS = "Crypt::OpenSSL::DSA::Signature";
char * dgst_pv = NULL;
STRLEN dgst_len = 0;
CODE:
dgst_pv = SvPV(dgst, dgst_len);
if (!(sig = DSA_do_sign((const unsigned char *) dgst_pv, (int)dgst_len, dsa))) {
croak("Error in dsa_sign: %s",ERR_error_string(ERR_get_error(), NULL));
}
RETVAL = sig;
OUTPUT:
RETVAL
SV *
sign(dsa, dgst)
DSA * dsa
SV * dgst
PREINIT:
unsigned char *sigret;
unsigned int siglen;
char * dgst_pv = NULL;
STRLEN dgst_len = 0;
CODE:
siglen = DSA_size(dsa);
sigret = malloc(siglen);
dgst_pv = SvPV(dgst, dgst_len);
/* warn("Length of sign [%s] is %d\n", dgst_pv, dgst_len); */
if (!(DSA_sign(0, (const unsigned char *) dgst_pv, (int)dgst_len, sigret, &siglen, dsa))) {
croak("Error in DSA_sign: %s",ERR_error_string(ERR_get_error(), NULL));
}
RETVAL = newSVpvn(sigret, siglen);
free(sigret);
OUTPUT:
RETVAL
int
verify(dsa, dgst, sigbuf)
DSA * dsa
SV *dgst
SV *sigbuf
PREINIT:
char * dgst_pv = NULL;
STRLEN dgst_len = 0;
char * sig_pv = NULL;
STRLEN sig_len = 0;
CODE:
dgst_pv = SvPV(dgst, dgst_len);
sig_pv = SvPV(sigbuf, sig_len);
RETVAL = DSA_verify(0, dgst_pv, (int)dgst_len, sig_pv, (int)sig_len, dsa);
if (RETVAL == -1)
croak("Error in DSA_verify: %s",ERR_error_string(ERR_get_error(), NULL));
OUTPUT:
RETVAL
int
do_verify(dsa, dgst, sig)
DSA *dsa
SV *dgst
DSA_SIG *sig
PREINIT:
char * dgst_pv = NULL;
STRLEN dgst_len = 0;
CODE:
dgst_pv = SvPV(dgst, dgst_len);
RETVAL = DSA_do_verify(dgst_pv, (int)dgst_len, sig, dsa);
if (RETVAL == -1)
croak("Error in DSA_do_verify: %s",ERR_error_string(ERR_get_error(), NULL));
OUTPUT:
RETVAL
DSA *
read_params(CLASS, filename)
char *CLASS
char *filename
PREINIT:
FILE *f;
CODE:
if(!(f = fopen(filename, "r")))
croak("Can't open file %s", filename);
RETVAL = PEM_read_DSAparams(f, NULL, NULL, NULL);
fclose(f);
OUTPUT:
RETVAL
int
write_params(dsa, filename)
DSA * dsa
char *filename
PREINIT:
FILE *f;
CODE:
if(!(f = fopen(filename, "w")))
croak("Can't open file %s", filename);
RETVAL = PEM_write_DSAparams(f, dsa);
fclose(f);
OUTPUT:
RETVAL
DSA *
_load_key(CLASS, private_flag_SV, key_string_SV)
char *CLASS;
SV * private_flag_SV;
SV * key_string_SV;
PREINIT:
STRLEN key_string_length; /* Needed to pass to SvPV */
char *key_string;
char private_flag;
BIO *stringBIO;
CODE:
private_flag = SvTRUE( private_flag_SV );
key_string = SvPV( key_string_SV, key_string_length );
if( (stringBIO = BIO_new_mem_buf(key_string, (int)key_string_length)) == NULL )
croak( "Failed to create memory BIO %s", ERR_error_string(ERR_get_error(), NULL));
RETVAL = private_flag
? PEM_read_bio_DSAPrivateKey( stringBIO, NULL, NULL, NULL )
: PEM_read_bio_DSA_PUBKEY( stringBIO, NULL, NULL, NULL );
BIO_set_close(stringBIO, BIO_CLOSE);
BIO_free( stringBIO );
if ( RETVAL == NULL )
croak( "Failed to read key %s", ERR_error_string(ERR_get_error(), NULL));
OUTPUT:
RETVAL
DSA *
read_pub_key(CLASS, filename)
char *CLASS
char *filename
PREINIT:
FILE *f;
CODE:
if(!(f = fopen(filename, "r")))
croak("Can't open file %s", filename);
RETVAL = PEM_read_DSA_PUBKEY(f, NULL, NULL, NULL);
fclose(f);
OUTPUT:
RETVAL
int
write_pub_key(dsa, filename)
DSA * dsa
char *filename
PREINIT:
FILE *f;
CODE:
if(!(f = fopen(filename, "w")))
croak("Can't open file %s", filename);
RETVAL = PEM_write_DSA_PUBKEY(f, dsa);
fclose(f);
OUTPUT:
RETVAL
DSA *
read_priv_key(CLASS, filename)
char *CLASS
char *filename
PREINIT:
FILE *f;
CODE:
if(!(f = fopen(filename, "r")))
croak("Can't open file %s", filename);
RETVAL = PEM_read_DSAPrivateKey(f, NULL, NULL, NULL);
fclose(f);
OUTPUT:
RETVAL
int
write_priv_key(dsa, filename)
DSA * dsa
char *filename
PREINIT:
FILE *f;
CODE:
if(!(f = fopen(filename, "w")))
croak("Can't open file %s", filename);
RETVAL = PEM_write_DSAPrivateKey(f, dsa, NULL, NULL, 0, NULL, NULL);
fclose(f);
OUTPUT:
RETVAL
SV *
get_p(dsa)
DSA *dsa
PREINIT:
const BIGNUM *p;
char *to;
int len;
int bnlen;
CODE:
DSA_get0_pqg(dsa, &p, NULL, NULL);
bnlen = BN_num_bytes(p);
to = malloc(sizeof(char) * bnlen);
len = BN_bn2bin(p, to);
RETVAL = newSVpvn(to, len);
free(to);
OUTPUT:
RETVAL
SV *
get_q(dsa)
DSA *dsa
PREINIT:
const BIGNUM *q;
char *to;
int len;
int bnlen;
CODE:
DSA_get0_pqg(dsa, NULL, &q, NULL);
bnlen = BN_num_bytes(q);
to = malloc(sizeof(char) * bnlen);
len = BN_bn2bin(q, to);
RETVAL = newSVpvn(to, len);
free(to);
OUTPUT:
RETVAL
SV *
get_g(dsa)
DSA *dsa
PREINIT:
const BIGNUM *g;
char *to;
int len;
int bnlen;
CODE:
DSA_get0_pqg(dsa, NULL, NULL, &g);
bnlen = BN_num_bytes(g);
to = malloc(sizeof(char) * bnlen);
len = BN_bn2bin(g, to);
RETVAL = newSVpvn(to, len);
free(to);
OUTPUT:
RETVAL
SV *
get_pub_key(dsa)
DSA *dsa
PREINIT:
const BIGNUM *pub_key;
char *to;
int len;
int bnlen;
CODE:
DSA_get0_key(dsa, &pub_key, NULL);
bnlen = BN_num_bytes(pub_key);
to = malloc(sizeof(char) * bnlen);
len = BN_bn2bin(pub_key, to);
RETVAL = newSVpvn(to, len);
free(to);
OUTPUT:
RETVAL
SV *
get_priv_key(dsa)
DSA *dsa
PREINIT:
const BIGNUM *priv_key;
char *to;
int len;
int bnlen;
CODE:
DSA_get0_key(dsa, NULL, &priv_key);
bnlen = BN_num_bytes(priv_key);
to = malloc(sizeof(char) * bnlen);
len = BN_bn2bin(priv_key, to);
RETVAL = newSVpvn(to, len);
free(to);
OUTPUT:
RETVAL
void
set_p(dsa, p_SV)
DSA *dsa
SV * p_SV
PREINIT:
STRLEN len;
BIGNUM *p;
BIGNUM *q;
BIGNUM *g;
const BIGNUM *old_q;
const BIGNUM *old_g;
CODE:
len = SvCUR(p_SV);
p = BN_bin2bn(SvPV(p_SV, len), (int)len, NULL);
DSA_get0_pqg(dsa, NULL, &old_q, &old_g);
if (NULL == old_q) {
q = BN_new();
} else {
q = BN_dup(old_q);
}
if (NULL == q) {
BN_free(p);
croak("Could not duplicate another prime");
}
if (NULL == old_g) {
g = BN_new();
} else {
g = BN_dup(old_g);
}
if (NULL == g) {
BN_free(p);
BN_free(q);
croak("Could not duplicate another prime");
}
if (!DSA_set0_pqg(dsa, p, q, g)) {
BN_free(p);
BN_free(q);
BN_free(g);
croak("Could not set a prime");
}
void
set_q(dsa, q_SV)
DSA *dsa
SV * q_SV
PREINIT:
STRLEN len;
BIGNUM *p;
BIGNUM *q;
BIGNUM *g;
const BIGNUM *old_p;
const BIGNUM *old_g;
CODE:
len = SvCUR(q_SV);
q = BN_bin2bn(SvPV(q_SV, len), (int)len, NULL);
DSA_get0_pqg(dsa, &old_p, NULL, &old_g);
if (NULL == old_p) {
p = BN_new();
} else {
p = BN_dup(old_p);
}
if (NULL == p) {
BN_free(q);
croak("Could not duplicate another prime");
}
if (NULL == old_g) {
g = BN_new();
} else {
g = BN_dup(old_g);
}
if (NULL == g) {
BN_free(p);
BN_free(q);
croak("Could not duplicate another prime");
}
if (!DSA_set0_pqg(dsa, p, q, g)) {
BN_free(p);
BN_free(q);
BN_free(g);
croak("Could not set a prime");
}
void
set_g(dsa, g_SV)
DSA *dsa
SV * g_SV
PREINIT:
STRLEN len;
BIGNUM *p;
BIGNUM *q;
BIGNUM *g;
const BIGNUM *old_p;
const BIGNUM *old_q;
CODE:
len = SvCUR(g_SV);
g = BN_bin2bn(SvPV(g_SV, len), (int)len, NULL);
DSA_get0_pqg(dsa, &old_p, &old_q, NULL);
if (NULL == old_p) {
p = BN_new();
} else {
p = BN_dup(old_p);
}
if (NULL == p) {
BN_free(g);
croak("Could not duplicate another prime");
}
if (NULL == old_q) {
q = BN_new();
} else {
q = BN_dup(old_q);
}
if (NULL == q) {
BN_free(p);
BN_free(g);
croak("Could not duplicate another prime");
}
if (!DSA_set0_pqg(dsa, p, q, g)) {
BN_free(p);
BN_free(q);
BN_free(g);
croak("Could not set a prime");
}
void
set_pub_key(dsa, pub_key_SV)
DSA *dsa
SV * pub_key_SV
PREINIT:
STRLEN len;
BIGNUM *pub_key;
CODE:
len = SvCUR(pub_key_SV);
pub_key = BN_bin2bn(SvPV(pub_key_SV, len), (int)len, NULL);
if (!DSA_set0_key(dsa, pub_key, NULL)) {
BN_free(pub_key);
croak("Could not set a key");
}
void
set_priv_key(dsa, priv_key_SV)
DSA *dsa
SV * priv_key_SV
PREINIT:
STRLEN len;
const BIGNUM *old_pub_key;
BIGNUM *pub_key;
BIGNUM *priv_key;
CODE:
DSA_get0_key(dsa, &old_pub_key, NULL);
if (NULL == old_pub_key) {
pub_key = BN_new();
if (NULL == pub_key) {
croak("Could not create a dummy public key");
}
if (!DSA_set0_key(dsa, pub_key, NULL)) {
BN_free(pub_key);
croak("Could not set a dummy public key");
}
}
len = SvCUR(priv_key_SV);
priv_key = BN_bin2bn(SvPV(priv_key_SV, len), (int)len, NULL);
if (!DSA_set0_key(dsa, NULL, priv_key)) {
BN_free(priv_key);
croak("Could not set a key");
}
MODULE = Crypt::OpenSSL::DSA PACKAGE = Crypt::OpenSSL::DSA::Signature
DSA_SIG *
new(CLASS)
char * CLASS
CODE:
RETVAL = DSA_SIG_new();
OUTPUT:
RETVAL
void
DESTROY(dsa_sig)
DSA_SIG *dsa_sig
CODE:
DSA_SIG_free(dsa_sig);
SV *
get_r(dsa_sig)
DSA_SIG *dsa_sig
PREINIT:
const BIGNUM *r;
char *to;
int len;
int bnlen;
CODE:
DSA_SIG_get0(dsa_sig, &r, NULL);
bnlen = BN_num_bytes(r);
to = malloc(sizeof(char) * bnlen);
len = BN_bn2bin(r, to);
RETVAL = newSVpvn(to, len);
free(to);
OUTPUT:
RETVAL
SV *
get_s(dsa_sig)
DSA_SIG *dsa_sig
PREINIT:
const BIGNUM *s;
char *to;
int len;
int bnlen;
CODE:
DSA_SIG_get0(dsa_sig, NULL, &s);
bnlen = BN_num_bytes(s);
to = malloc(sizeof(char) * bnlen);
len = BN_bn2bin(s, to);
RETVAL = newSVpvn(to, len);
free(to);
OUTPUT:
RETVAL
void
set_r(dsa_sig, r_SV)
DSA_SIG *dsa_sig
SV * r_SV
PREINIT:
STRLEN len;
BIGNUM *r;
BIGNUM *s;
const BIGNUM *old_s;
CODE:
len = SvCUR(r_SV);
r = BN_bin2bn(SvPV(r_SV, len), (int)len, NULL);
DSA_SIG_get0(dsa_sig, NULL, &old_s);
if (NULL == old_s) {
s = BN_new();
} else {
s = BN_dup(old_s);
}
if (NULL == s) {
BN_free(r);
croak("Could not duplicate another signature value");
}
if (!DSA_SIG_set0(dsa_sig, r, s)) {
BN_free(r);
BN_free(s);
croak("Could not set a signature");
}
void
set_s(dsa_sig, s_SV)
DSA_SIG *dsa_sig
SV * s_SV
PREINIT:
STRLEN len;
BIGNUM *s;
BIGNUM *r;
const BIGNUM *old_r;
CODE:
len = SvCUR(s_SV);
s = BN_bin2bn(SvPV(s_SV, len), (int)len, NULL);
DSA_SIG_get0(dsa_sig, &old_r, NULL);
if (NULL == old_r) {
r = BN_new();
} else {
r = BN_dup(old_r);
}
if (NULL == r) {
BN_free(s);
croak("Could not duplicate another signature value");
}
if (!DSA_SIG_set0(dsa_sig, r, s)) {
BN_free(s);
croak("Could not set a signature");
}
|