.\" $NetBSD: d2i_RSAPrivateKey.3,v 1.2.2.3 2023/11/02 19:32:31 sborrill Exp $ .\" .\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "d2i_RSAPrivateKey 3" .TH d2i_RSAPrivateKey 3 "2023-10-25" "3.0.12" "OpenSSL" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" d2i_DSAPrivateKey, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey_fp, d2i_DSAPublicKey, d2i_DSA_PUBKEY, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY_fp, d2i_DSAparams, d2i_RSAPrivateKey, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey_fp, d2i_RSAPublicKey, d2i_RSAPublicKey_bio, d2i_RSAPublicKey_fp, d2i_RSA_PUBKEY, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY_fp, d2i_DHparams, d2i_DHparams_bio, d2i_DHparams_fp, d2i_ECParameters, d2i_ECPrivateKey, d2i_ECPrivateKey_bio, d2i_ECPrivateKey_fp, d2i_EC_PUBKEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY_fp, i2d_RSAPrivateKey, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey_fp, i2d_RSAPublicKey, i2d_RSAPublicKey_bio, i2d_RSAPublicKey_fp, i2d_RSA_PUBKEY, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY_fp, i2d_DHparams, i2d_DHparams_bio, i2d_DHparams_fp, i2d_DSAPrivateKey, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey_fp, i2d_DSAPublicKey, i2d_DSA_PUBKEY, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY_fp, i2d_DSAparams, i2d_ECParameters, i2d_ECPrivateKey, i2d_ECPrivateKey_bio, i2d_ECPrivateKey_fp, i2d_EC_PUBKEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY_fp \&\- DEPRECATED .SH "LIBRARY" libcrypto, -lcrypto .SH "SYNOPSIS" .IX Header "SYNOPSIS" The following functions have been deprecated since OpenSSL 3.0, and can be hidden entirely by defining \fB\s-1OPENSSL_API_COMPAT\s0\fR with a suitable version value, see \fBopenssl_user_macros\fR\|(7): .PP .Vb 12 \& TYPE *d2i_TYPEPrivateKey(TYPE **a, const unsigned char **ppin, long length); \& TYPE *d2i_TYPEPrivateKey_bio(BIO *bp, TYPE **a); \& TYPE *d2i_TYPEPrivateKey_fp(FILE *fp, TYPE **a); \& TYPE *d2i_TYPEPublicKey(TYPE **a, const unsigned char **ppin, long length); \& TYPE *d2i_TYPEPublicKey_bio(BIO *bp, TYPE **a); \& TYPE *d2i_TYPEPublicKey_fp(FILE *fp, TYPE **a); \& TYPE *d2i_TYPEparams(TYPE **a, const unsigned char **ppin, long length); \& TYPE *d2i_TYPEparams_bio(BIO *bp, TYPE **a); \& TYPE *d2i_TYPEparams_fp(FILE *fp, TYPE **a); \& TYPE *d2i_TYPE_PUBKEY(TYPE **a, const unsigned char **ppin, long length); \& TYPE *d2i_TYPE_PUBKEY_bio(BIO *bp, TYPE **a); \& TYPE *d2i_TYPE_PUBKEY_fp(FILE *fp, TYPE **a); \& \& int i2d_TYPEPrivateKey(const TYPE *a, unsigned char **ppout); \& int i2d_TYPEPrivateKey(TYPE *a, unsigned char **ppout); \& int i2d_TYPEPrivateKey_fp(FILE *fp, const TYPE *a); \& int i2d_TYPEPrivateKey_fp(FILE *fp, TYPE *a); \& int i2d_TYPEPrivateKey_bio(BIO *bp, const TYPE *a); \& int i2d_TYPEPrivateKey_bio(BIO *bp, TYPE *a); \& int i2d_TYPEPublicKey(const TYPE *a, unsigned char **ppout); \& int i2d_TYPEPublicKey(TYPE *a, unsigned char **ppout); \& int i2d_TYPEPublicKey_fp(FILE *fp, const TYPE *a); \& int i2d_TYPEPublicKey_fp(FILE *fp, TYPE *a); \& int i2d_TYPEPublicKey_bio(BIO *bp, const TYPE *a); \& int i2d_TYPEPublicKey_bio(BIO *bp, TYPE *a); \& int i2d_TYPEparams(const TYPE *a, unsigned char **ppout); \& int i2d_TYPEparams(TYPE *a, unsigned char **ppout); \& int i2d_TYPEparams_fp(FILE *fp, const TYPE *a); \& int i2d_TYPEparams_fp(FILE *fp, TYPE *a); \& int i2d_TYPEparams_bio(BIO *bp, const TYPE *a); \& int i2d_TYPEparams_bio(BIO *bp, TYPE *a); \& int i2d_TYPE_PUBKEY(const TYPE *a, unsigned char **ppout); \& int i2d_TYPE_PUBKEY(TYPE *a, unsigned char **ppout); \& int i2d_TYPE_PUBKEY_fp(FILE *fp, const TYPE *a); \& int i2d_TYPE_PUBKEY_fp(FILE *fp, TYPE *a); \& int i2d_TYPE_PUBKEY_bio(BIO *bp, const TYPE *a); \& int i2d_TYPE_PUBKEY_bio(BIO *bp, TYPE *a); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" All functions described here are deprecated. Please use \s-1\fBOSSL_DECODER\s0\fR\|(3) instead of the \fBd2i\fR functions and \s-1\fBOSSL_ENCODER\s0\fR\|(3) instead of the \fBi2d\fR functions. See \*(L"Migration\*(R" below. .PP In the description here, \fB\f(BI\s-1TYPE\s0\fB\fR is used a placeholder for any of the OpenSSL datatypes, such as \fB\s-1RSA\s0\fR. The function parameters \fIppin\fR and \fIppout\fR are generally either both named \&\fIpp\fR in the headers, or \fIin\fR and \fIout\fR. .PP All the functions here behave the way that's described in \fBd2i_X509\fR\|(3). .PP Please note that not all functions in the synopsis are available for all key types. For example, there are no \fBd2i_RSAparams()\fR or \fBi2d_RSAparams()\fR, because the PKCS#1 \fB\s-1RSA\s0\fR structure doesn't include any key parameters. .PP \&\fBd2i_\f(BI\s-1TYPE\s0\fBPrivateKey\fR() and derivates thereof decode \s-1DER\s0 encoded \&\fB\f(BI\s-1TYPE\s0\fB\fR private key data organized in a type specific structure. .PP \&\fBd2i_\f(BI\s-1TYPE\s0\fBPublicKey\fR() and derivates thereof decode \s-1DER\s0 encoded \&\fB\f(BI\s-1TYPE\s0\fB\fR public key data organized in a type specific structure. .PP \&\fBd2i_\f(BI\s-1TYPE\s0\fBparams\fR() and derivates thereof decode \s-1DER\s0 encoded \fB\f(BI\s-1TYPE\s0\fB\fR key parameters organized in a type specific structure. .PP \&\fBd2i_\f(BI\s-1TYPE\s0\fB_PUBKEY\fR() and derivates thereof decode \s-1DER\s0 encoded \fB\f(BI\s-1TYPE\s0\fB\fR public key data organized in a \fBSubjectPublicKeyInfo\fR structure. .PP \&\fBi2d_\f(BI\s-1TYPE\s0\fBPrivateKey\fR() and derivates thereof encode the private key \&\fB\f(BI\s-1TYPE\s0\fB\fR data into a type specific \s-1DER\s0 encoded structure. .PP \&\fBi2d_\f(BI\s-1TYPE\s0\fBPublicKey\fR() and derivates thereof encode the public key \&\fB\f(BI\s-1TYPE\s0\fB\fR data into a type specific \s-1DER\s0 encoded structure. .PP \&\fBi2d_\f(BI\s-1TYPE\s0\fBparams\fR() and derivates thereof encode the \fB\f(BI\s-1TYPE\s0\fB\fR key parameters data into a type specific \s-1DER\s0 encoded structure. .PP \&\fBi2d_\f(BI\s-1TYPE\s0\fB_PUBKEY\fR() and derivates thereof encode the public key \&\fB\f(BI\s-1TYPE\s0\fB\fR data into a \s-1DER\s0 encoded \fBSubjectPublicKeyInfo\fR structure. .PP For example, \fBd2i_RSAPrivateKey()\fR and \fBd2i_RSAPublicKey()\fR expects the structure defined by PKCS#1. Similarly, \fBi2d_RSAPrivateKey()\fR and \fBi2d_RSAPublicKey()\fR produce \s-1DER\s0 encoded string organized according to PKCS#1. .SS "Migration" .IX Subsection "Migration" Migration from the diverse \fB\f(BI\s-1TYPE\s0\fB\fRs requires using corresponding new OpenSSL types. For all \fB\f(BI\s-1TYPE\s0\fB\fRs described here, the corresponding new type is \fB\s-1EVP_PKEY\s0\fR. The rest of this section assumes that this has been done, exactly how to do that is described elsewhere. .PP There are two migration paths: .IP "\(bu" 4 Replace b with \fBd2i_PrivateKey\fR\|(3), b with \fBd2i_PublicKey\fR\|(3), b with \fBd2i_KeyParams\fR\|(3), b with \fBd2i_PUBKEY\fR\|(3), b with \fBi2d_PrivateKey\fR\|(3), b with \fBi2d_PublicKey\fR\|(3), b with \fBi2d_KeyParams\fR\|(3), b with \fBi2d_PUBKEY\fR\|(3). A caveat is that \fBi2d_PrivateKey\fR\|(3) may output a \s-1DER\s0 encoded PKCS#8 outermost structure instead of the type specific structure, and that \&\fBd2i_PrivateKey\fR\|(3) recognises and unpacks a PKCS#8 structures. .IP "\(bu" 4 Use \s-1\fBOSSL_DECODER\s0\fR\|(3) and \s-1\fBOSSL_ENCODER\s0\fR\|(3). How to migrate is described below. All those descriptions assume that the key to be encoded is in the variable \fIpkey\fR. .PP \fIMigrating \f(BIi2d\fI functions to \f(BI\s-1OSSL_ENCODER\s0\fI\fR .IX Subsection "Migrating i2d functions to OSSL_ENCODER" .PP The exact \s-1\fBOSSL_ENCODER\s0\fR\|(3) output is driven by arguments rather than by function names. The sample code to get \s-1DER\s0 encoded output in a type specific structure is uniform, the only things that vary are the selection of what part of the \fB\s-1EVP_PKEY\s0\fR should be output, and the structure. The \&\fBi2d\fR functions names can therefore be translated into two variables, \&\fIselection\fR and \fIstructure\fR as follows: .IP "\fBi2d_\f(BI\s-1TYPE\s0\fBPrivateKey\fR() translates into:" 4 .IX Item "i2d_TYPEPrivateKey() translates into:" .Vb 2 \& int selection = EVP_PKEY_KEYPAIR; \& const char *structure = "type\-specific"; .Ve .IP "\fBi2d_\f(BI\s-1TYPE\s0\fBPublicKey\fR() translates into:" 4 .IX Item "i2d_TYPEPublicKey() translates into:" .Vb 2 \& int selection = EVP_PKEY_PUBLIC_KEY; \& const char *structure = "type\-specific"; .Ve .IP "\fBi2d_\f(BI\s-1TYPE\s0\fBparams\fR() translates into:" 4 .IX Item "i2d_TYPEparams() translates into:" .Vb 2 \& int selection = EVP_PKEY_PARAMETERS; \& const char *structure = "type\-specific"; .Ve .IP "\fBi2d_\f(BI\s-1TYPE\s0\fB_PUBKEY\fR() translates into:" 4 .IX Item "i2d_TYPE_PUBKEY() translates into:" .Vb 2 \& int selection = EVP_PKEY_PUBLIC_KEY; \& const char *structure = "SubjectPublicKeyInfo"; .Ve .PP The following sample code does the rest of the work: .PP .Vb 10 \& unsigned char *p = buffer; /* |buffer| is supplied by the caller */ \& size_t len = buffer_size; /* assumed be the size of |buffer| */ \& OSSL_ENCODER_CTX *ctx = \& OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER", structure, \& NULL, NULL); \& if (ctx == NULL) { \& /* fatal error handling */ \& } \& if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) { \& OSSL_ENCODER_CTX_free(ctx); \& /* non\-fatal error handling */ \& } \& if (!OSSL_ENCODER_to_data(ctx, &p, &len)) { \& OSSL_ENCODER_CTX_free(ctx); \& /* error handling */ \& } \& OSSL_ENCODER_CTX_free(ctx); .Ve .SH "NOTES" .IX Header "NOTES" The letters \fBi\fR and \fBd\fR in \fBi2d_\f(BI\s-1TYPE\s0\fB\fR() stand for \&\*(L"internal\*(R" (that is, an internal C structure) and \*(L"\s-1DER\*(R"\s0 respectively. So \fBi2d_\f(BI\s-1TYPE\s0\fB\fR() converts from internal to \s-1DER.\s0 .PP The functions can also understand \fB\s-1BER\s0\fR forms. .PP The actual \s-1TYPE\s0 structure passed to \fBi2d_\f(BI\s-1TYPE\s0\fB\fR() must be a valid populated \fB\f(BI\s-1TYPE\s0\fB\fR structure \*(-- it \fBcannot\fR simply be fed with an empty structure such as that returned by \fBTYPE_new()\fR. .PP The encoded data is in binary form and may contain embedded zeros. Therefore, any \s-1FILE\s0 pointers or BIOs should be opened in binary mode. Functions such as \fBstrlen()\fR will \fBnot\fR return the correct length of the encoded structure. .PP The ways that \fI*ppin\fR and \fI*ppout\fR are incremented after the operation can trap the unwary. See the \fB\s-1WARNINGS\s0\fR section in \fBd2i_X509\fR\|(3) for some common errors. The reason for this-auto increment behaviour is to reflect a typical usage of \s-1ASN1\s0 functions: after one structure is encoded or decoded another will be processed after it. .PP The following points about the data types might be useful: .IP "\fB\s-1DSA_PUBKEY\s0\fR" 4 .IX Item "DSA_PUBKEY" Represents a \s-1DSA\s0 public key using a \fBSubjectPublicKeyInfo\fR structure. .IP "\fBDSAPublicKey\fR, \fBDSAPrivateKey\fR" 4 .IX Item "DSAPublicKey, DSAPrivateKey" Use a non-standard OpenSSL format and should be avoided; use \fB\s-1DSA_PUBKEY\s0\fR, \&\fBPEM_write_PrivateKey\fR\|(3), or similar instead. .SH "RETURN VALUES" .IX Header "RETURN VALUES" \&\fBd2i_\f(BI\s-1TYPE\s0\fB\fR(), \fBd2i_\f(BI\s-1TYPE\s0\fB_bio\fR() and \fBd2i_\f(BI\s-1TYPE\s0\fB_fp\fR() return a valid \&\fB\f(BI\s-1TYPE\s0\fB\fR structure or \s-1NULL\s0 if an error occurs. If the \*(L"reuse\*(R" capability has been used with a valid structure being passed in via \fIa\fR, then the object is freed in the event of error and \fI*a\fR is set to \s-1NULL.\s0 .PP \&\fBi2d_\f(BI\s-1TYPE\s0\fB\fR() returns the number of bytes successfully encoded or a negative value if an error occurs. .PP \&\fBi2d_\f(BI\s-1TYPE\s0\fB_bio\fR() and \fBi2d_\f(BI\s-1TYPE\s0\fB_fp\fR() return 1 for success and 0 if an error occurs. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\s-1\fBOSSL_ENCODER\s0\fR\|(3), \s-1\fBOSSL_DECODER\s0\fR\|(3), \&\fBd2i_PrivateKey\fR\|(3), \fBd2i_PublicKey\fR\|(3), \fBd2i_KeyParams\fR\|(3), \&\fBd2i_PUBKEY\fR\|(3), \&\fBi2d_PrivateKey\fR\|(3), \fBi2d_PublicKey\fR\|(3), \fBi2d_KeyParams\fR\|(3), \&\fBi2d_PUBKEY\fR\|(3) .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2020\-2023 The OpenSSL Project Authors. All Rights Reserved. .PP Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use this file except in compliance with the License. You can obtain a copy in the file \s-1LICENSE\s0 in the source distribution or at .