A certificate for Zyre
curve authentication.
Refs: - api.zeromq.org/czmq4-0:zcert
- EMPTY_KEY
The placeholder key that is set as the secret key for a public certificate.
Zyre::Cert.from( public_key, secret_key ) → cert
Create a certificate from the public_key
and secret_key
.
static VALUE
rzyre_cert_s_from( VALUE class, VALUE public_key, VALUE secret_key )
{
VALUE self = rzyre_cert_alloc( class );
zcert_t *ptr = NULL;
const char *pub_str = StringValuePtr( public_key ),
*sec_str = StringValuePtr( secret_key );
if ( RSTRING_LEN(public_key) == 32 && RSTRING_LEN(secret_key) == 32 ) {
ptr = zcert_new_from( (const byte *)pub_str, (const byte *)sec_str );
} else if ( RSTRING_LEN(public_key) == 40 && RSTRING_LEN(secret_key) == 40 ) {
#ifdef CZMQ_BUILD_DRAFT_API
ptr = zcert_new_from_txt( pub_str, sec_str );
#else
rb_raise( rb_eNotImpError,
"can't create a key from encoded keys: Czmq was not built with Draft APIs!" );
#endif
}
if ( !ptr ) {
rb_raise( rb_eArgError, "invalid key pair" );
}
RTYPEDDATA_DATA( self ) = ptr;
return self;
}
Zyre::Cert.load( filename ) → cert
Create a certificate from a saved certificate in the specified filename
.
static VALUE
rzyre_cert_s_load( VALUE class, VALUE filename )
{
VALUE self = rzyre_cert_alloc( class );
zcert_t *ptr = zcert_load( StringValueCStr(filename) );
if ( !ptr ) {
rb_raise( rb_eArgError, "failed to load cert from %s", RSTRING_PTR(filename) );
}
RTYPEDDATA_DATA( self ) = ptr;
return self;
}
Fetch the value for the cert metadata with the given name
.
def []( name )
return self.meta( name.to_s )
end
Set the value for the cert metadata with the given name
to value
.
def []=( name, value )
return self.set_meta( name.to_s, value.to_s )
end
Apply the certificate to the specified zyre_node
, i.e. use the cert for CURVE security. If the receiving certificate doesn’t have a private key, an exception will be raised.
def apply( zyre_node )
return zyre_node.zcert = self
end
Delete the value for the cert metadata with the given name
. Requires CZMQ to have been built with Draft APIs.
def delete( name )
name = name.to_s
deleted_val = self[ name ]
self.unset_meta( name )
return deleted_val
end
Return a copy of the certificate.
static VALUE
rzyre_cert_dup( VALUE self )
{
zcert_t *ptr = rzyre_get_cert( self );
zcert_t *other_ptr;
VALUE other = rb_call_super( 0, NULL );
RTYPEDDATA_DATA( other ) = other_ptr = zcert_dup( ptr );
if ( !other_ptr )
rb_raise( rb_eRuntimeError, "couldn't duplicate the cert" );
return other;
}
eql?( other_cert ) → true or false
Return true if the other_cert
has the same keys.
static VALUE
rzyre_cert_eql_p( VALUE self, VALUE other )
{
zcert_t *ptr = rzyre_get_cert( self ),
*other_ptr = rzyre_get_cert( other );
bool equal = zcert_eq( ptr, other_ptr );
return equal ? Qtrue : Qfalse;
}
Returns true
if the certificate has a secret key.
def have_secret_key?
return self.secret_key != EMPTY_KEY
end
Print certificate contents to stdout.
static VALUE
rzyre_cert_print( VALUE self )
{
zcert_t *ptr = rzyre_get_cert( self );
zcert_print( ptr );
return Qtrue;
}
Set up some more Rubyish aliases
Return public part of key pair as 32-byte binary string
static VALUE
rzyre_cert_public_key( VALUE self )
{
zcert_t *ptr = rzyre_get_cert( self );
const byte *key = zcert_public_key( ptr );
VALUE rval = rb_enc_str_new( (const char *)key, 32, rb_ascii8bit_encoding() );
return rval;
}
Return public part of key pair as Z85 armored string
static VALUE
rzyre_cert_public_txt( VALUE self )
{
zcert_t *ptr = rzyre_get_cert( self );
const char *key = zcert_public_txt( ptr );
VALUE rval = rb_usascii_str_new( key, 40 );
return rval;
}
Save the full certificate (public + secret) to the specified filename
. This creates one public file and one secret file (filename + “_secret”).
static VALUE
rzyre_cert_save( VALUE self, VALUE filename )
{
zcert_t *ptr = rzyre_get_cert( self );
const char *filename_str = StringValueCStr( filename );
int result;
result = zcert_save( ptr, filename_str );
if ( result != 0 )
rb_raise( rb_eRuntimeError, "failed to save cert to %s", filename_str );
return Qtrue;
}
Save the public certificate only to the specified filename
.
static VALUE
rzyre_cert_save_public( VALUE self, VALUE filename )
{
zcert_t *ptr = rzyre_get_cert( self );
const char *filename_str = StringValueCStr( filename );
int result;
result = zcert_save_public( ptr, filename_str );
if ( result != 0 )
rb_raise( rb_eRuntimeError, "failed to save public cert to %s", filename_str );
return Qtrue;
}
Save the secret certificate only to the specified filename
.
static VALUE
rzyre_cert_save_secret( VALUE self, VALUE filename )
{
zcert_t *ptr = rzyre_get_cert( self );
const char *filename_str = StringValueCStr( filename );
int result;
result = zcert_save_secret( ptr, filename_str );
if ( result != 0 )
rb_raise( rb_eRuntimeError, "failed to save secret cert to %s", filename_str );
return Qtrue;
}
Return secret part of key pair as 32-byte binary string
static VALUE
rzyre_cert_secret_key( VALUE self )
{
zcert_t *ptr = rzyre_get_cert( self );
const byte *key = zcert_secret_key( ptr );
VALUE rval = rb_enc_str_new( (const char *)key, 32, rb_ascii8bit_encoding() );
return rval;
}
Return secret part of key pair as Z85 armored string
static VALUE
rzyre_cert_secret_txt( VALUE self )
{
zcert_t *ptr = rzyre_get_cert( self );
const char *key = zcert_secret_txt( ptr );
VALUE rval = rb_usascii_str_new( key, 40 );
return rval;
}