📄 openssl.txt
字号:
This writes out a PKCS12 structure to a BIO.i2d_PKCS12_fp(fp, p12)This is the same but for a FILE pointer.d2i_PKCS12_bio(bp, p12)This reads in a PKCS12 structure from a BIO.d2i_PKCS12_fp(fp, p12)This is the same but for a FILE pointer.3. High level functions.3.1 Parsing with PKCS12_parse().int PKCS12_parse(PKCS12 *p12, char *pass, EVP_PKEY **pkey, X509 **cert, STACK **ca);This function takes a PKCS12 structure and a password (ASCII, null terminated)and returns the private key, the corresponding certificate and any CAcertificates. If any of these is not required it can be passed as a NULL.The 'ca' parameter should be either NULL, a pointer to NULL or a valid STACKstructure. Typically to read in a PKCS#12 file you might do:p12 = d2i_PKCS12_fp(fp, NULL);PKCS12_parse(p12, password, &pkey, &cert, NULL); /* CAs not wanted */PKCS12_free(p12);3.2 PKCS#12 creation with PKCS12_create().PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert, STACK *ca, int nid_key, int nid_cert, int iter, int mac_iter, int keytype);This function will create a PKCS12 structure from a given password, name,private key, certificate and optional STACK of CA certificates. The remaining5 parameters can be set to 0 and sensible defaults will be used.The parameters nid_key and nid_cert are the key and certificate encryptionalgorithms, iter is the encryption iteration count, mac_iter is the MACiteration count and keytype is the type of private key. If you really wantto know what these last 5 parameters do then read the low level section.Typically to create a PKCS#12 file the following could be used:p12 = PKCS12_create(pass, "My Certificate", pkey, cert, NULL, 0,0,0,0,0);i2d_PKCS12_fp(fp, p12);PKCS12_free(p12);3.3 Changing a PKCS#12 structure password.int PKCS12_newpass(PKCS12 *p12, char *oldpass, char *newpass);This changes the password of an already existing PKCS#12 structure. oldpassis the old password and newpass is the new one. An error occurs if the oldpassword is incorrect.LOW LEVEL FUNCTIONS.In some cases the high level functions do not provide the necessaryfunctionality. For example if you want to generate or parse more complexPKCS#12 files. The sample pkcs12 application uses the low level functionsto display details about the internal structure of a PKCS#12 file.Introduction.This is a brief description of how a PKCS#12 file is represented internally:some knowledge of PKCS#12 is assumed.A PKCS#12 object contains several levels.At the lowest level is a PKCS12_SAFEBAG. This can contain a certificate, aCRL, a private key, encrypted or unencrypted, a set of safebags (so thestructure can be nested) or other secrets (not documented at present). A safebag can optionally have attributes, currently these are: a unicodefriendlyName (a Unicode string) or a localKeyID (a string of bytes).At the next level is an authSafe which is a set of safebags collected intoa PKCS#7 ContentInfo. This can be just plain data, or encrypted itself.At the top level is the PKCS12 structure itself which contains a set ofauthSafes in an embedded PKCS#7 Contentinfo of type data. In addition itcontains a MAC which is a kind of password protected digest to preserveintegrity (so any unencrypted stuff below can't be tampered with).The reason for these levels is so various objects can be encrypted in variousways. For example you might want to encrypt a set of private keys withtriple-DES and then include the related certificates either unencrypted orwith lower encryption. Yes it's the dreaded crypto laws at work again whichallow strong encryption on private keys and only weak encryption on otherstuff.To build one of these things you turn all certificates and keys into safebags(with optional attributes). You collect the safebags into (one or more) STACKSand convert these into authsafes (encrypted or unencrypted). The authsafesare collected into a STACK and added to a PKCS12 structure. Finally a MACinserted.Pulling one apart is basically the reverse process. The MAC is verified againstthe given password. The authsafes are extracted and each authsafe split intoa set of safebags (possibly involving decryption). Finally the safebags aredecomposed into the original keys and certificates and the attributes used tomatch up private key and certificate pairs.Anyway here are the functions that do the dirty work.1. Construction functions.1.1 Safebag functions.M_PKCS12_x5092certbag(x509)This macro takes an X509 structure and returns a certificate bag. TheX509 structure can be freed up after calling this function.M_PKCS12_x509crl2certbag(crl)As above but for a CRL.PKCS8_PRIV_KEY_INFO *PKEY2PKCS8(EVP_PKEY *pkey)Take a private key and convert it into a PKCS#8 PrivateKeyInfo structure.Works for both RSA and DSA private keys. NB since the PKCS#8 PrivateKeyInfostructure contains a private key data in plain text form it should be free'dup as soon as it has been encrypted for security reasons (freeing up thestructure zeros out the sensitive data). This can be done withPKCS8_PRIV_KEY_INFO_free().PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage)This sets the key type when a key is imported into MSIE or Outlook 98. Twovalues are currently supported: KEY_EX and KEY_SIG. KEY_EX is an exchange typekey that can also be used for signing but its size is limited in the exportversions of MS software to 512 bits, it is also the default. KEY_SIG is asigning only key but the keysize is unlimited (well 16K is supposed to work).If you are using the domestic version of MSIE then you can ignore this becauseKEY_EX is not limited and can be used for both.PKCS12_SAFEBAG *PKCS12_MAKE_KEYBAG(PKCS8_PRIV_KEY_INFO *p8)Convert a PKCS8 private key structure into a keybag. This routine embeds thep8 structure in the keybag so p8 should not be freed up or used after it iscalled. The p8 structure will be freed up when the safebag is freed.PKCS12_SAFEBAG *PKCS12_MAKE_SHKEYBAG(int pbe_nid, unsigned char *pass, int passlen, unsigned char *salt, int saltlen, int iter, PKCS8_PRIV_KEY_INFO *p8)Convert a PKCS#8 structure into a shrouded key bag (encrypted). p8 is notembedded and can be freed up after use.int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name, int namelen)int PKCS12_add_friendlyname(PKCS12_SAFEBAG *bag, unsigned char *name, int namelen)Add a local key id or a friendlyname to a safebag.1.2 Authsafe functions.PKCS7 *PKCS12_pack_p7data(STACK *sk)Take a stack of safebags and convert them into an unencrypted authsafe. Thestack of safebags can be freed up after calling this function.PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, unsigned char *pass, int passlen, unsigned char *salt, int saltlen, int iter, STACK *bags);As above but encrypted.1.3 PKCS12 functions.PKCS12 *PKCS12_init(int mode)Initialise a PKCS12 structure (currently mode should be NID_pkcs7_data).M_PKCS12_pack_authsafes(p12, safes)This macro takes a STACK of authsafes and adds them to a PKCS#12 structure.int PKCS12_set_mac(PKCS12 *p12, unsigned char *pass, int passlen, unsigned char *salt, int saltlen, int iter, EVP_MD *md_type);Add a MAC to a PKCS12 structure. If EVP_MD is NULL use SHA-1, the spec suggeststhat SHA-1 should be used.2. Extraction Functions.2.1 Safebags.M_PKCS12_bag_type(bag)Return the type of "bag". Returns one of the followingNID_keyBagNID_pkcs8ShroudedKeyBag 7NID_certBag 8NID_crlBag 9NID_secretBag 10NID_safeContentsBag 11M_PKCS12_cert_bag_type(bag)Returns type of certificate bag, following are understood.NID_x509Certificate 14NID_sdsiCertificate 15M_PKCS12_crl_bag_type(bag)Returns crl bag type, currently only NID_crlBag is recognised.M_PKCS12_certbag2x509(bag)This macro extracts an X509 certificate from a certificate bag.M_PKCS12_certbag2x509crl(bag)As above but for a CRL.EVP_PKEY * PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8)Extract a private key from a PKCS8 private key info structure.M_PKCS12_decrypt_skey(bag, pass, passlen) Decrypt a shrouded key bag and return a PKCS8 private key info structure.Works with both RSA and DSA keyschar *PKCS12_get_friendlyname(bag)Returns the friendlyName of a bag if present or NULL if none. The returnedstring is a null terminated ASCII string allocated with Malloc(). It should thus be freed up with Free() after use.2.2 AuthSafe functions.M_PKCS12_unpack_p7data(p7)Extract a STACK of safe bags from a PKCS#7 data ContentInfo.#define M_PKCS12_unpack_p7encdata(p7, pass, passlen)As above but for an encrypted content info.2.3 PKCS12 functions.M_PKCS12_unpack_authsafes(p12)Extract a STACK of authsafes from a PKCS12 structure.M_PKCS12_mac_present(p12)Check to see if a MAC is present.int PKCS12_verify_mac(PKCS12 *p12, unsigned char *pass, int passlen)Verify a MAC on a PKCS12 structure. Returns an error if MAC not present.Notes.1. All the function return 0 or NULL on error.2. Encryption based functions take a common set of parameters. These aredescribed below.pass, passlenASCII password and length. The password on the MAC is called the "integritypassword" the encryption password is called the "privacy password" in thePKCS#12 documentation. The passwords do not have to be the same. If -1 ispassed for the length it is worked out by the function itself (currentlythis is sometimes done whatever is passed as the length but that may change).salt, saltlenA 'salt' if salt is NULL a random salt is used. If saltlen is also zero adefault length is used.iterIteration count. This is a measure of how many times an internal function iscalled to encrypt the data. The larger this value is the longer it takes, itmakes dictionary attacks on passwords harder. NOTE: Some implementations donot support an iteration count on the MAC. If the password for the MAC andencryption is the same then there is no point in having a high iterationcount for encryption if the MAC has no count. The MAC could be attackedand the password used for the main decryption.pbe_nidThis is the NID of the password based encryption method used. The following aresupported.NID_pbe_WithSHA1And128BitRC4NID_pbe_WithSHA1And40BitRC4NID_pbe_WithSHA1And3_Key_TripleDES_CBCNID_pbe_WithSHA1And2_Key_TripleDES_CBCNID_pbe_WithSHA1And128BitRC2_CBCNID_pbe_WithSHA1And40BitRC2_CBCWhich you use depends on the implementation you are exporting to. "Exportgrade" (i.e. cryptographically challenged) products cannot support allalgorithms. Typically you may be able to use any encryption on shrouded keybags but they must then be placed in an unencrypted authsafe. Other authsafesmay only support 40bit encryption. Of course if you are using SSLeaythroughout you can strongly encrypt everything and have high iteration countson everything.3. For decryption routines only the password and length are needed.4. Unlike the external version the nid's of objects are the values of theconstants: that is NID_certBag is the real nid, therefore there is no PKCS12_obj_offset() function. Note the object constants are not the same asthose of the external version. If you use these constants then you will needto recompile your code.5. With the exception of PKCS12_MAKE_KEYBAG(), after calling any function or macro of the form PKCS12_MAKE_SOMETHING(other) the "other" structure can bereused or freed up safely.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -