9
9
#include <openssl/pem.h>
10
10
#include <openssl/x509v3.h>
11
11
#include <openssl/err.h>
12
+ #include <openssl/rand.h>
12
13
13
14
#include "ppport.h"
14
15
@@ -36,7 +37,7 @@ typedef struct
36
37
} Crypt__OpenSSL__RSA ;
37
38
38
39
#define PACKAGE_NAME "Crypt::OpenSSL::PKCS10"
39
- #define PACKAGE_CROAK (p_message ) croak("%s:%d: %s ", (p_message))
40
+ #define PACKAGE_CROAK (p_message ) croak("%s", (p_message))
40
41
#define CHECK_NEW (p_var , p_size , p_type ) \
41
42
if (New(0, p_var, p_size, p_type) == NULL) \
42
43
{ PACKAGE_CROAK("unable to alloc buffer"); }
@@ -63,7 +64,7 @@ X509_NAME *parse_name(char *subject, long chtype, int multirdn)
63
64
64
65
X509_NAME * n = NULL ;
65
66
66
- if (!buf || !ne_types || !ne_values )
67
+ if (!buf || !ne_types || !ne_values || ! mval )
67
68
{
68
69
croak ("malloc error\n" );
69
70
goto error ;
@@ -172,6 +173,8 @@ X509_NAME *parse_name(char *subject, long chtype, int multirdn)
172
173
OPENSSL_free (ne_types );
173
174
if (buf )
174
175
OPENSSL_free (buf );
176
+ if (mval )
177
+ OPENSSL_free (mval );
175
178
return NULL ;
176
179
}
177
180
@@ -227,20 +230,23 @@ SV* make_pkcs10_obj(SV* p_proto, X509_REQ* p_req, EVP_PKEY* p_pk, STACK_OF(X509_
227
230
}
228
231
229
232
/* stolen from OpenSSL.xs */
230
- long bio_write_cb (struct bio_st * bm , int m , const char * ptr , int l , long x , long y ) {
231
-
233
+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
234
+ long bio_write_cb (struct bio_st * bm , int m , const char * ptr , size_t len , int l , long x , int y , size_t * processed ) {
235
+ #else
236
+ long bio_write_cb (struct bio_st * bm , int m , const char * ptr , int len , long x , long y ) {
237
+ #endif
232
238
if (m == BIO_CB_WRITE ) {
233
239
SV * sv = (SV * ) BIO_get_callback_arg (bm );
234
- sv_catpvn (sv , ptr , l );
240
+ sv_catpvn (sv , ptr , len );
235
241
}
236
242
237
243
if (m == BIO_CB_PUTS ) {
238
244
SV * sv = (SV * ) BIO_get_callback_arg (bm );
239
- l = strlen (ptr );
240
- sv_catpvn (sv , ptr , l );
245
+ len = strlen (ptr );
246
+ sv_catpvn (sv , ptr , len );
241
247
}
242
248
243
- return l ;
249
+ return len ;
244
250
}
245
251
246
252
static BIO * sv_bio_create (void ) {
@@ -250,7 +256,11 @@ static BIO* sv_bio_create(void) {
250
256
/* create an in-memory BIO abstraction and callbacks */
251
257
BIO * bio = BIO_new (BIO_s_mem ());
252
258
259
+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
260
+ BIO_set_callback_ex (bio , bio_write_cb );
261
+ #else
253
262
BIO_set_callback (bio , bio_write_cb );
263
+ #endif
254
264
BIO_set_callback_arg (bio , (void * )sv );
255
265
256
266
return bio ;
@@ -339,25 +349,48 @@ new(class, keylen = 1024)
339
349
PREINIT :
340
350
X509_REQ * x ;
341
351
EVP_PKEY * pk ;
342
- RSA * rsa = NULL ;
343
-
352
+ char * classname = SvPVutf8_nolen ( class ) ;
353
+
344
354
CODE :
345
355
//CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
346
-
347
- if (( pk = EVP_PKEY_new ()) == NULL )
348
- croak ( "%s - can't create PKEY" , class );
356
+ if (! RAND_status ())
357
+ printf ( "Warning: generating random key material may take a long time\n"
358
+ "if the system has a poor entropy source\n" );
349
359
350
360
if ((x = X509_REQ_new ()) == NULL )
351
- croak ("%s - can't create req" , class );
361
+ croak ("%s - can't create req" , classname );
362
+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
363
+ pk = EVP_RSA_gen (keylen );
364
+ #elif OPENSSL_VERSION_NUMBER <= 0x10000000L
365
+ RSA * rsa ;
366
+ if ((pk = EVP_PKEY_new ()) == NULL )
367
+ croak ("%s - can't create PKEY" , classname );
352
368
353
369
rsa = RSA_generate_key (keylen , RSA_F4 , NULL , NULL );
354
370
if (!EVP_PKEY_assign_RSA (pk ,rsa ))
355
- croak ("%s - EVP_PKEY_assign_RSA" , class );
356
-
371
+ croak ("%s - EVP_PKEY_assign_RSA" , classname );
372
+ #else
373
+ RSA * rsa = RSA_new ();
374
+ BIGNUM * bne = BN_new ();
375
+ if (bne == NULL )
376
+ croak ("%s - BN_new failed" , classname );
377
+
378
+ if (BN_set_word (bne , RSA_F4 ) != 1 )
379
+ croak ("%s - BN_set_word failed" , classname );
380
+
381
+ if ((pk = EVP_PKEY_new ()) == NULL )
382
+ croak ("%s - can't create PKEY" , classname );
383
+
384
+ if (!RSA_generate_key_ex (rsa , keylen , bne , NULL ))
385
+ croak ("%s - RSA_generate_key_ex failed" , classname );
386
+
387
+ if (!EVP_PKEY_assign_RSA (pk ,rsa ))
388
+ croak ("%s - EVP_PKEY_assign_RSA" , classname );
389
+ #endif
357
390
X509_REQ_set_pubkey (x ,pk );
358
391
X509_REQ_set_version (x ,0L );
359
392
if (!X509_REQ_sign (x ,pk ,EVP_sha256 ()))
360
- croak ("%s - X509_REQ_sign" , class );
393
+ croak ("%s - X509_REQ_sign failed " , classname );
361
394
362
395
RETVAL = make_pkcs10_obj (class , x , pk , NULL , NULL );
363
396
@@ -382,32 +415,43 @@ DESTROY(pkcs10)
382
415
BIO_free(bio_err);*/
383
416
384
417
SV *
385
- new_from_rsa (class , p_rsa )
418
+ _new_from_rsa (class , p_rsa , priv )
386
419
SV * class
387
420
SV * p_rsa
421
+ SV * priv
388
422
389
423
PREINIT :
390
424
Crypt__OpenSSL__RSA * rsa ;
425
+ char * keyString ;
426
+ STRLEN keylen ;
427
+ BIO * bio ;
391
428
X509_REQ * x ;
392
429
EVP_PKEY * pk ;
430
+ char * classname = SvPVutf8_nolen (class );
393
431
394
432
CODE :
395
- //CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
396
-
397
- if ((pk = EVP_PKEY_new ()) == NULL )
398
- croak ("%s - can't create PKEY" , class );
433
+
434
+ // Get the private key and save it in memory
435
+ keyString = SvPV (priv , keylen );
436
+ bio = BIO_new_mem_buf (keyString , keylen );
437
+ if (bio == NULL ) {
438
+ croak ("Bio is null **** \n" );
439
+ }
440
+
441
+ // Create the PrivateKey as EVP_PKEY
442
+ pk = PEM_read_bio_PrivateKey (bio , NULL , 0 , NULL );
443
+ if (pk == NULL ) {
444
+ croak ("Failed operation error code %d\n" , errno );
445
+ }
399
446
400
447
if ((x = X509_REQ_new ()) == NULL )
401
- croak ("%s - can't create req" , class );
448
+ croak ("%s - can't create req" , classname );
402
449
403
450
rsa = (Crypt__OpenSSL__RSA * ) SvIV (SvRV (p_rsa ));
404
- if (!EVP_PKEY_assign_RSA (pk ,rsa -> rsa ))
405
- croak ("%s - EVP_PKEY_assign_RSA" , class );
406
-
407
451
X509_REQ_set_pubkey (x ,pk );
408
452
X509_REQ_set_version (x ,0L );
409
453
if (!X509_REQ_sign (x ,pk ,EVP_sha256 ()))
410
- croak ("%s - X509_REQ_sign" , class );
454
+ croak ("%s - X509_REQ_sign" , classname );
411
455
412
456
RETVAL = make_pkcs10_obj (class , x , pk , NULL , & rsa -> rsa );
413
457
@@ -452,16 +496,12 @@ get_pem_pubkey(pkcs10)
452
496
453
497
type = EVP_PKEY_base_id (pkey );
454
498
if (type == EVP_PKEY_RSA ) {
455
-
456
- # PEM_write_bio_RSAPublicKey (bio, EVP_PKEY_get0_RSA(pkey));
457
- PEM_write_bio_RSA_PUBKEY (bio , EVP_PKEY_get0_RSA (pkey ));
458
-
499
+ PEM_write_bio_PUBKEY (bio , pkey );
459
500
} else if (type == EVP_PKEY_DSA ) {
460
-
461
- PEM_write_bio_DSA_PUBKEY (bio , EVP_PKEY_get0_DSA (pkey ));
501
+ PEM_write_bio_PUBKEY (bio , pkey );
462
502
#ifndef OPENSSL_NO_EC
463
503
} else if ( type == EVP_PKEY_EC ) {
464
- PEM_write_bio_EC_PUBKEY (bio , EVP_PKEY_get0_EC_KEY ( pkey ) );
504
+ PEM_write_bio_PUBKEY (bio , pkey );
465
505
#endif
466
506
} else {
467
507
@@ -562,7 +602,7 @@ get_pem_pk(pkcs10,...)
562
602
/* get the certificate back out in a specified format. */
563
603
564
604
if (!PEM_write_bio_PrivateKey (bio ,pkcs10 -> pk ,NULL ,NULL ,0 ,NULL ,NULL ))
565
- croak ("%s - PEM_write_bio_PrivateKey" , pkcs10 -> pk );
605
+ croak ("%s - PEM_write_bio_PrivateKey" , ( char * ) pkcs10 -> pk );
566
606
567
607
RETVAL = sv_bio_final (bio );
568
608
@@ -686,7 +726,7 @@ add_ext_final(pkcs10)
686
726
if (pkcs10 -> exts )
687
727
sk_X509_EXTENSION_pop_free (pkcs10 -> exts , X509_EXTENSION_free );
688
728
} else {
689
- RETVAL = NULL ;
729
+ RETVAL = 0 ;
690
730
}
691
731
692
732
OUTPUT :
@@ -741,8 +781,12 @@ accessor(pkcs10)
741
781
name = X509_REQ_get_subject_name (pkcs10 -> req );
742
782
X509_NAME_print_ex (bio , name , 0 , XN_FLAG_SEP_CPLUS_SPC );
743
783
} else if (ix == 2 ) {
744
- key = X509_REQ_extract_key (pkcs10 -> req );
784
+ key = X509_REQ_get_pubkey (pkcs10 -> req );
785
+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
786
+ EVP_PKEY_print_public (bio , key , 0 , NULL );
787
+ #else
745
788
RSA_print (bio , EVP_PKEY_get1_RSA (key ), 0 );
789
+ #endif
746
790
}
747
791
}
748
792
0 commit comments