// return the real size of target required // return ZERO if failed int RSA_save_public_certificate_to_buffer(pub_key *pubkey, char *target, int targetLen) { char *pTarget; char temp[buffer_size]; int result; // 1. save structure to buffer result = save_public_certificate_structure_to_buffer(pubkey, target, targetLen); if (result == 0) return 0; // 2. encode the buffer to PEM result = PEM_encode(target, temp, result, buffer_size); if (result == 0) return 0; // 3. input RSA public certificate header string pTarget = target; memcpy(pTarget, certificate_top, certificate_top_length); pTarget += certificate_top_length; memcpy(pTarget, crlf, crlf_length); pTarget += crlf_length; memcpy(pTarget, temp, result); pTarget += result; memcpy(pTarget, crlf, crlf_length); pTarget += crlf_length; memcpy(pTarget, certificate_bottom, certificate_bottom_length); pTarget += certificate_bottom_length; return pTarget - target; }
/* * PEM encode a PKCS #8 private key, encrypted */ std::string PEM_encode(const Private_Key& key, RandomNumberGenerator& rng, const std::string& pass, std::chrono::milliseconds msec, const std::string& pbe_algo) { if(pass == "") return PEM_encode(key); return PEM_Code::encode(PKCS8::BER_encode(key, rng, pass, msec, pbe_algo), "ENCRYPTED PRIVATE KEY"); }
int RSA_save_private_key_to_buffer(pri_key *prikey, char *target, int targetLen) { char *pTarget; char temp[buffer_size]; int result; // 1. save structure to buffer result = save_private_key_structure_to_buffer(prikey, target, targetLen); if (result == 0) return 0; // 2. encode the buffer to PEM result = PEM_encode(target, temp, result, buffer_size); if (result == 0) return 0; // 3. input RSA public certificate header string pTarget = target; // copy the top string + crlf memcpy(pTarget, key_top, key_top_length); pTarget += key_top_length; memcpy(pTarget, crlf, crlf_length); pTarget += crlf_length; // copy the real body content memcpy(pTarget, temp, result); pTarget += result; // copy crlf and bottom certificate string memcpy(pTarget, crlf, crlf_length); pTarget += crlf_length; memcpy(pTarget, key_bottom, key_bottom_length); pTarget += key_bottom_length; return pTarget - target; }
/* * Make a copy of this private key */ Private_Key* copy_key(const Private_Key& key, RandomNumberGenerator& rng) { DataSource_Memory source(PEM_encode(key)); return PKCS8::load_key(source, rng); }
/* * Make a copy of this private key */ std::unique_ptr<Private_Key> copy_key(const Private_Key& key) { DataSource_Memory source(PEM_encode(key)); return PKCS8::load_key(source); }
/* * Make a copy of this public key */ Public_Key* copy_key(const Public_Key& key) { DataSource_Memory source(PEM_encode(key)); return X509::load_key(source); }