@@ -3581,6 +3581,190 @@ void GetHashes(const FunctionCallbackInfo<Value>& args) {
35813581}
35823582
35833583
3584+ void Certificate::Initialize (Handle<Object> target) {
3585+ HandleScope scope (node_isolate);
3586+
3587+ Local<FunctionTemplate> t = FunctionTemplate::New (New);
3588+
3589+ t->InstanceTemplate ()->SetInternalFieldCount (1 );
3590+
3591+ NODE_SET_PROTOTYPE_METHOD (t, " verifySpkac" , VerifySpkac);
3592+ NODE_SET_PROTOTYPE_METHOD (t, " exportPublicKey" , ExportPublicKey);
3593+ NODE_SET_PROTOTYPE_METHOD (t, " exportChallenge" , ExportChallenge);
3594+
3595+ target->Set (FIXED_ONE_BYTE_STRING (node_isolate, " Certificate" ),
3596+ t->GetFunction ());
3597+ }
3598+
3599+
3600+ void Certificate::New (const FunctionCallbackInfo<Value>& args) {
3601+ new Certificate (args.GetIsolate (), args.This ());
3602+ }
3603+
3604+
3605+ bool Certificate::VerifySpkac (const char * data, unsigned int len) {
3606+ bool i = 0 ;
3607+ EVP_PKEY * pkey = NULL ;
3608+ NETSCAPE_SPKI * spki = NULL ;
3609+
3610+ spki = NETSCAPE_SPKI_b64_decode (data, len);
3611+ if (spki == NULL )
3612+ goto exit;
3613+
3614+ pkey = X509_PUBKEY_get (spki->spkac ->pubkey );
3615+ if (pkey == NULL )
3616+ goto exit;
3617+
3618+ i = NETSCAPE_SPKI_verify (spki, pkey) > 0 ;
3619+
3620+ exit:
3621+ if (pkey != NULL )
3622+ EVP_PKEY_free (pkey);
3623+
3624+ if (spki != NULL )
3625+ NETSCAPE_SPKI_free (spki);
3626+
3627+ return i;
3628+ }
3629+
3630+
3631+ void Certificate::VerifySpkac (const FunctionCallbackInfo<Value>& args) {
3632+ HandleScope scope (node_isolate);
3633+
3634+ Certificate* certificate = WeakObject::Unwrap<Certificate>(args.This ());
3635+ bool i = false ;
3636+
3637+ if (args.Length () < 1 )
3638+ return ThrowTypeError (" Missing argument" );
3639+
3640+ ASSERT_IS_BUFFER (args[0 ]);
3641+
3642+ size_t length = Buffer::Length (args[0 ]);
3643+ if (length == 0 )
3644+ return args.GetReturnValue ().Set (i);
3645+
3646+ char * data = Buffer::Data (args[0 ]);
3647+ assert (data != NULL );
3648+
3649+ i = certificate->VerifySpkac (data, length) > 0 ;
3650+
3651+ args.GetReturnValue ().Set (i);
3652+ }
3653+
3654+
3655+ const char * Certificate::ExportPublicKey (const char * data, int len) {
3656+ char * buf = NULL ;
3657+ EVP_PKEY * pkey = NULL ;
3658+ NETSCAPE_SPKI * spki = NULL ;
3659+
3660+ BIO * bio = BIO_new (BIO_s_mem ());
3661+ if (bio == NULL )
3662+ goto exit;
3663+
3664+ spki = NETSCAPE_SPKI_b64_decode (data, len);
3665+ if (spki == NULL )
3666+ goto exit;
3667+
3668+ pkey = NETSCAPE_SPKI_get_pubkey (spki);
3669+ if (pkey == NULL )
3670+ goto exit;
3671+
3672+ if (PEM_write_bio_PUBKEY (bio, pkey) <= 0 )
3673+ goto exit;
3674+
3675+ BIO_write (bio, " \0 " , 1 );
3676+ BUF_MEM * ptr;
3677+ BIO_get_mem_ptr (bio, &ptr);
3678+
3679+ buf = new char [ptr->length ];
3680+ memcpy (buf, ptr->data , ptr->length );
3681+
3682+ exit:
3683+ if (pkey != NULL )
3684+ EVP_PKEY_free (pkey);
3685+
3686+ if (spki != NULL )
3687+ NETSCAPE_SPKI_free (spki);
3688+
3689+ if (bio != NULL )
3690+ BIO_free_all (bio);
3691+
3692+ return buf;
3693+ }
3694+
3695+
3696+ void Certificate::ExportPublicKey (const FunctionCallbackInfo<Value>& args) {
3697+ HandleScope scope (node_isolate);
3698+
3699+ Certificate* certificate = WeakObject::Unwrap<Certificate>(args.This ());
3700+
3701+ if (args.Length () < 1 )
3702+ return ThrowTypeError (" Missing argument" );
3703+
3704+ ASSERT_IS_BUFFER (args[0 ]);
3705+
3706+ size_t length = Buffer::Length (args[0 ]);
3707+ if (length == 0 )
3708+ return args.GetReturnValue ().SetEmptyString ();
3709+
3710+ char * data = Buffer::Data (args[0 ]);
3711+ assert (data != NULL );
3712+
3713+ const char * pkey = certificate->ExportPublicKey (data, length);
3714+ if (pkey == NULL )
3715+ return args.GetReturnValue ().SetEmptyString ();
3716+
3717+ Local<Value> out = Encode (pkey, strlen (pkey), BUFFER );
3718+
3719+ delete[] pkey;
3720+
3721+ args.GetReturnValue ().Set (out);
3722+ }
3723+
3724+
3725+ const char * Certificate::ExportChallenge (const char * data, int len) {
3726+ NETSCAPE_SPKI * sp = NULL ;
3727+
3728+ sp = NETSCAPE_SPKI_b64_decode (data, len);
3729+ if (sp == NULL )
3730+ return NULL ;
3731+
3732+ const char * buf = NULL ;
3733+ buf = reinterpret_cast <const char *>(ASN1_STRING_data (sp->spkac ->challenge ));
3734+
3735+ return buf;
3736+ }
3737+
3738+
3739+ void Certificate::ExportChallenge (const FunctionCallbackInfo<Value>& args) {
3740+ HandleScope scope (node_isolate);
3741+
3742+ Certificate* crt = WeakObject::Unwrap<Certificate>(args.This ());
3743+
3744+ if (args.Length () < 1 )
3745+ return ThrowTypeError (" Missing argument" );
3746+
3747+ ASSERT_IS_BUFFER (args[0 ]);
3748+
3749+ size_t len = Buffer::Length (args[0 ]);
3750+ if (len == 0 )
3751+ return args.GetReturnValue ().SetEmptyString ();
3752+
3753+ char * data = Buffer::Data (args[0 ]);
3754+ assert (data != NULL );
3755+
3756+ const char * cert = crt->ExportChallenge (data, len);
3757+ if (cert == NULL )
3758+ return args.GetReturnValue ().SetEmptyString ();
3759+
3760+ Local<Value> outString = Encode (cert, strlen (cert), BUFFER );
3761+
3762+ delete[] cert;
3763+
3764+ args.GetReturnValue ().Set (outString);
3765+ }
3766+
3767+
35843768void InitCryptoOnce () {
35853769 SSL_library_init ();
35863770 OpenSSL_add_all_algorithms ();
@@ -3621,6 +3805,7 @@ void InitCrypto(Handle<Object> target,
36213805 Hash::Initialize (env, target);
36223806 Sign::Initialize (env, target);
36233807 Verify::Initialize (env, target);
3808+ Certificate::Initialize (target);
36243809
36253810 NODE_SET_METHOD (target, " PBKDF2" , PBKDF2 );
36263811 NODE_SET_METHOD (target, " randomBytes" , RandomBytes<false >);
0 commit comments