
#use wml::openssl-macros area=docs page=RSA_set_method

<title>Documents, RSA_set_method(3)</title>

<h1>RSA_set_method(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#THE_RSA_METHOD_STRUCTURE">THE RSA_METHOD STRUCTURE</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#BUGS">BUGS</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
	<LI><A HREF="#HISTORY">HISTORY</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
RSA_set_default_method, RSA_get_default_method, RSA_set_method,
RSA_get_method, RSA_PKCS1_SSLeay, RSA_null_method, RSA_flags,
RSA_new_method - select RSA method

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/rsa.h&gt;
</PRE>
<PRE> void RSA_set_default_method(const RSA_METHOD *meth);
</PRE>
<PRE> RSA_METHOD *RSA_get_default_method(void);
</PRE>
<PRE> int RSA_set_method(RSA *rsa, const RSA_METHOD *meth);
</PRE>
<PRE> RSA_METHOD *RSA_get_method(const RSA *rsa);
</PRE>
<PRE> RSA_METHOD *RSA_PKCS1_SSLeay(void);
</PRE>
<PRE> RSA_METHOD *RSA_null_method(void);
</PRE>
<PRE> int RSA_flags(const RSA *rsa);
</PRE>
<PRE> RSA *RSA_new_method(RSA_METHOD *method);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
An <STRONG>RSA_METHOD</STRONG> specifies the functions that OpenSSL uses for RSA operations. By modifying
the method, alternative implementations such as hardware accelerators may
be used. IMPORTANT: See the NOTES section for important information about
how these RSA API functions are affected by the use of <STRONG>ENGINE</STRONG> API calls.

</P>
<P>
Initially, the default RSA_METHOD is the OpenSSL internal implementation,
as returned by <CODE>RSA_PKCS1_SSLeay().</CODE>

</P>
<P>
<CODE>RSA_set_default_method()</CODE> makes <STRONG>meth</STRONG> the default method for all RSA structures created later. <STRONG>NB</STRONG>: This is true only whilst no ENGINE has been set as a default for RSA, so
this function is no longer recommended.

</P>
<P>
<CODE>RSA_get_default_method()</CODE> returns a pointer to the current
default RSA_METHOD. However, the meaningfulness of this result is dependent
on whether the ENGINE API is being used, so this function is no longer
recommended.

</P>
<P>
<CODE>RSA_set_method()</CODE> selects <STRONG>meth</STRONG> to perform all operations using the key
<STRONG>rsa</STRONG>. This will replace the RSA_METHOD used by the RSA key and if the previous
method was supplied by an ENGINE, the handle to that ENGINE will be
released during the change. It is possible to have RSA keys that only work
with certain RSA_METHOD implementations (eg. from an ENGINE module that
supports embedded hardware-protected keys), and in such cases attempting to
change the RSA_METHOD for the key can have unexpected results.

</P>
<P>
<CODE>RSA_get_method()</CODE> returns a pointer to the RSA_METHOD being
used by <STRONG>rsa</STRONG>. This method may or may not be supplied by an ENGINE implementation, but
if it is, the return value can only be guaranteed to be valid as long as
the RSA key itself is valid and does not have its implementation changed by
<CODE>RSA_set_method().</CODE>

</P>
<P>
<CODE>RSA_flags()</CODE> returns the <STRONG>flags</STRONG> that are set for <STRONG>rsa</STRONG>'s current RSA_METHOD. See the BUGS section.

</P>
<P>
<CODE>RSA_new_method()</CODE> allocates and initializes an RSA structure so
that
<STRONG>engine</STRONG> will be used for the RSA operations. If <STRONG>engine</STRONG> is NULL, the default ENGINE for RSA operations is used, and if no default
ENGINE is set, the RSA_METHOD controlled by
<CODE>RSA_set_default_method()</CODE> is used.

</P>
<P>
<CODE>RSA_flags()</CODE> returns the <STRONG>flags</STRONG> that are set for <STRONG>rsa</STRONG>'s current method.

</P>
<P>
<CODE>RSA_new_method()</CODE> allocates and initializes an <STRONG>RSA</STRONG> structure so that
<STRONG>method</STRONG> will be used for the RSA operations. If <STRONG>method</STRONG> is <STRONG>NULL</STRONG>, the default method is used.

</P>
<P>
<HR>
<H1><A NAME="THE_RSA_METHOD_STRUCTURE">THE RSA_METHOD STRUCTURE</A></H1>
<PRE> typedef struct rsa_meth_st
 {
     /* name of the implementation */
        const char *name;
</PRE>
<PRE>     /* encrypt */
        int (*rsa_pub_enc)(int flen, unsigned char *from,
          unsigned char *to, RSA *rsa, int padding);
</PRE>
<PRE>     /* verify arbitrary data */
        int (*rsa_pub_dec)(int flen, unsigned char *from,
          unsigned char *to, RSA *rsa, int padding);
</PRE>
<PRE>     /* sign arbitrary data */
        int (*rsa_priv_enc)(int flen, unsigned char *from,
          unsigned char *to, RSA *rsa, int padding);
</PRE>
<PRE>     /* decrypt */
        int (*rsa_priv_dec)(int flen, unsigned char *from,
          unsigned char *to, RSA *rsa, int padding);
</PRE>
<PRE>     /* compute r0 = r0 ^ I mod rsa-&gt;n (May be NULL for some
                                        implementations) */
        int (*rsa_mod_exp)(BIGNUM *r0, BIGNUM *I, RSA *rsa);
</PRE>
<PRE>     /* compute r = a ^ p mod m (May be NULL for some implementations) */
        int (*bn_mod_exp)(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
</PRE>
<PRE>     /* called at RSA_new */
        int (*init)(RSA *rsa);
</PRE>
<PRE>     /* called at RSA_free */
        int (*finish)(RSA *rsa);
</PRE>
<PRE>     /* RSA_FLAG_EXT_PKEY        - rsa_mod_exp is called for private key
      *                            operations, even if p,q,dmp1,dmq1,iqmp
      *                            are NULL
      * RSA_FLAG_SIGN_VER        - enable rsa_sign and rsa_verify
      * RSA_METHOD_FLAG_NO_CHECK - don't check pub/private match
      */
        int flags;
</PRE>
<PRE>        char *app_data; /* ?? */
</PRE>
<PRE>     /* sign. For backward compatibility, this is used only
      * if (flags &amp; RSA_FLAG_SIGN_VER)
      */
        int (*rsa_sign)(int type, unsigned char *m, unsigned int m_len,
           unsigned char *sigret, unsigned int *siglen, RSA *rsa);
</PRE>
<PRE>     /* verify. For backward compatibility, this is used only
      * if (flags &amp; RSA_FLAG_SIGN_VER)
      */
        int (*rsa_verify)(int type, unsigned char *m, unsigned int m_len,
           unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
</PRE>
<PRE> } RSA_METHOD;
</PRE>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>RSA_PKCS1_SSLeay(),</CODE> <CODE>RSA_PKCS1_null_method(),</CODE>
<CODE>RSA_get_default_method()</CODE> and <CODE>RSA_get_method()</CODE>
return pointers to the respective RSA_METHODs.

</P>
<P>
<CODE>RSA_set_default_method()</CODE> returns no value.

</P>
<P>
<CODE>RSA_set_method()</CODE> returns a pointer to the old RSA_METHOD
implementation that was replaced. However, this return value should
probably be ignored because if it was supplied by an ENGINE, the pointer
could be invalidated at any time if the ENGINE is unloaded (in fact it
could be unloaded as a result of the <CODE>RSA_set_method()</CODE> function
releasing its handle to the ENGINE). For this reason, the return type may
be replaced with a <STRONG>void</STRONG>
declaration in a future release.

</P>
<P>
<CODE>RSA_new_method()</CODE> returns NULL and sets an error code that can
be obtained by <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A> if the allocation fails. Otherwise it returns a pointer to the newly
allocated structure.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
As of version 0.9.7, RSA_METHOD implementations are grouped together with
other algorithmic APIs (eg. DSA_METHOD, EVP_CIPHER, etc) into <STRONG>ENGINE</STRONG>
modules. If a default ENGINE is specified for RSA functionality using an
ENGINE API function, that will override any RSA defaults set using the RSA
API (ie. <CODE>RSA_set_default_method()).</CODE> For this reason, the
ENGINE API is the recommended way to control default implementations for
use in RSA and other cryptographic algorithms.

</P>
<P>
<HR>
<H1><A NAME="BUGS">BUGS</A></H1>
<P>
The behaviour of <CODE>RSA_flags()</CODE> is a mis-feature that is left
as-is for now to avoid creating compatibility problems. RSA functionality,
such as the encryption functions, are controlled by the <STRONG>flags</STRONG> value in the RSA key itself, not by the <STRONG>flags</STRONG> value in the RSA_METHOD attached to the RSA key (which is what this
function returns). If the flags element of an RSA key is changed, the
changes will be honoured by RSA functionality but will not be reflected in
the return value of the <CODE>RSA_flags()</CODE> function - in effect
<CODE>RSA_flags()</CODE> behaves more like an
<CODE>RSA_default_flags()</CODE> function (which does not currently exist).

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/rsa.html#">rsa(3)</A>, <A HREF="../crypto/RSA_new.html#">RSA_new(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>RSA_new_method()</CODE> and <CODE>RSA_set_default_method()</CODE>
appeared in SSLeay 0.8. <CODE>RSA_get_default_method(),</CODE>
<CODE>RSA_set_method()</CODE> and <CODE>RSA_get_method()</CODE> as well as
the rsa_sign and rsa_verify components of RSA_METHOD were added in OpenSSL
0.9.4.

</P>
<P>
<CODE>RSA_set_default_openssl_method()</CODE> and
<CODE>RSA_get_default_openssl_method()</CODE> replaced
<CODE>RSA_set_default_method()</CODE> and
<CODE>RSA_get_default_method()</CODE> respectively, and
<CODE>RSA_set_method()</CODE> and <CODE>RSA_new_method()</CODE> were
altered to use
<STRONG>ENGINE</STRONG>s rather than <STRONG>RSA_METHOD</STRONG>s during development of the engine version of OpenSSL 0.9.6. For 0.9.7, the
handling of defaults in the ENGINE API was restructured so that this change
was reversed, and behaviour of the other functions resembled more closely
the previous behaviour. The behaviour of defaults in the ENGINE API now
transparently overrides the behaviour of defaults in the RSA API without
requiring changing these function prototypes.

</P>
:}


