//Pascal &or the FreePascal use of nintendo 2ds, 3ds regime // // Copyright (c) 2013, 2015, 2017 Kenneth Dwayne Lee Bsc. // all rights reserved // type sslcContext = record servhandle : Handle; sslchandle : u32; sharedmem_handle : Handle; end; PsslcContext = ^sslcContext; SSLC_DefaultRootCert = (SSLC_DefaultRootCert_Nintendo_CA := $1, SSLC_DefaultRootCert_Nintendo_CA_G2 := $2, SSLC_DefaultRootCert_Nintendo_CA_G3 := $3, SSLC_DefaultRootCert_Nintendo_Class2_CA := $4, SSLC_DefaultRootCert_Nintendo_Class2_CA_G2 := $5, SSLC_DefaultRootCert_Nintendo_Class2_CA_G3 := $6, SSLC_DefaultRootCert_CyberTrust := $7, SSLC_DefaultRootCert_AddTrust_External_CA := $8, SSLC_DefaultRootCert_COMODO := $9,SSLC_DefaultRootCert_USERTrust := $A, SSLC_DefaultRootCert_DigiCert_EV := $B ); SSLC_DefaultClientCert = (SSLC_DefaultClientCert_ClCertA := $40 ); {/ sslc options. https://www.3dbrew.org/wiki/SSL_Services#SSLOpt } const SSLCOPT_Default = 0; SSLCOPT_DisableVerify = 1 shl 9; // BIT(9), // "Disables server cert verification when set." SSLCOPT_TLSv10 = 1 shl 11; // "Use TLSv1.0." {/ Initializes SSLC. Normally session_handle should be 0. When non-zero this will use the specified handle for the main-service-session without using the Initialize command, instead of using srvGetServiceHandle. } function sslcInit(session_handle:Handle):s32;cdecl;external; {/ Exits SSLC. } procedure sslcExit;cdecl;external; {* * @brief Creates a RootCertChain. * @param RootCertChain_contexthandle Output contexthandle. } function sslcCreateRootCertChain(RootCertChain_contexthandle:Pu32):s32;cdecl;external; {* * @brief Destroys a RootCertChain. * @param RootCertChain_contexthandle RootCertChain contexthandle. } function sslcDestroyRootCertChain(RootCertChain_contexthandle:u32):s32;cdecl;external; {* * @brief Adds a trusted RootCA cert to a RootCertChain. * @param RootCertChain_contexthandle RootCertChain to use. * @param cert Pointer to the DER cert. * @param certsize Size of the DER cert. } function sslcAddTrustedRootCA(RootCertChain_contexthandle:u32; cert:Pu8; certsize:u32; cert_contexthandle:Pu32):s32;cdecl;external; {* * @brief Adds a default RootCA cert to a RootCertChain. * @param RootCertChain_contexthandle RootCertChain to use. * @param certID ID of the cert to add. * @param cert_contexthandle Optional, the cert contexthandle can be written here. } function sslcRootCertChainAddDefaultCert(RootCertChain_contexthandle:u32; certID:SSLC_DefaultRootCert; cert_contexthandle:Pu32):s32;cdecl;external; {* * @brief Removes the specified cert from the RootCertChain. * @param RootCertChain_contexthandle RootCertChain to use. * @param cert_contexthandle Cert contexthandle to remove from the RootCertChain. } function sslcRootCertChainRemoveCert(RootCertChain_contexthandle:u32; cert_contexthandle:u32):s32;cdecl;external; {* * @brief Creates an unknown CertChain. * @param CertChain_contexthandle Output contexthandle. } function sslcCreate8CertChain(CertChain_contexthandle:Pu32):s32;cdecl;external; {* * @brief Destroys a CertChain from sslcCreate8CertChain(). * @param CertChain_contexthandle CertChain contexthandle. } function sslcDestroy8CertChain(CertChain_contexthandle:u32):s32;cdecl;external; {* * @brief Adds a cert to a CertChain from sslcCreate8CertChain(). * @param CertChain_contexthandle CertChain to use. * @param cert Pointer to the cert. * @param certsize Size of the cert. } function sslc8CertChainAddCert(CertChain_contexthandle:u32; cert:Pu8; certsize:u32; cert_contexthandle:Pu32):s32;cdecl;external; {* * @brief Adds a default cert to a CertChain from sslcCreate8CertChain(). Not actually usable since no certIDs are implemented in SSL-module for this. * @param CertChain_contexthandle CertChain to use. * @param certID ID of the cert to add. * @param cert_contexthandle Optional, the cert contexthandle can be written here. } function sslc8CertChainAddDefaultCert(CertChain_contexthandle:u32; certID:u8; cert_contexthandle:Pu32):s32;cdecl;external; {* * @brief Removes the specified cert from the CertChain from sslcCreate8CertChain(). * @param CertChain_contexthandle CertChain to use. * @param cert_contexthandle Cert contexthandle to remove from the CertChain. } function sslc8CertChainRemoveCert(CertChain_contexthandle:u32; cert_contexthandle:u32):s32;cdecl;external; {* * @brief Opens a new ClientCert-context. * @param cert Pointer to the DER cert. * @param certsize Size of the DER cert. * @param key Pointer to the DER key. * @param keysize Size of the DER key. * @param ClientCert_contexthandle Output contexthandle. } function sslcOpenClientCertContext(cert:Pu8; certsize:u32; key:Pu8; keysize:u32; ClientCert_contexthandle:Pu32):s32;cdecl;external; {* * @brief Opens a ClientCert-context with a default certID. * @param certID ID of the ClientCert to use. * @param ClientCert_contexthandle Output contexthandle. } function sslcOpenDefaultClientCertContext(certID:SSLC_DefaultClientCert; ClientCert_contexthandle:Pu32):s32;cdecl;external; {* * @brief Closes the specified ClientCert-context. * @param ClientCert_contexthandle ClientCert-context to use. } function sslcCloseClientCertContext(ClientCert_contexthandle:u32):s32;cdecl;external; {* * @brief This uses ps:ps SeedRNG internally. } function sslcSeedRNG:s32;cdecl;external; {* * @brief This uses ps:ps GenerateRandomData internally. * @param buf Output buffer. * @param size Output size. } function sslcGenerateRandomData(buf:Pu8; size:u32):s32;cdecl;external; {* * @brief Creates a sslc context. * @param context sslc context. * @param sockfd Socket fd, this code automatically uses the required SOC command before using the actual sslc command. * @param input_opt Input sslc options bitmask. * @param hostname Server hostname. } function sslcCreateContext(context:PsslcContext; sockfd:cint; input_opt:u32; hostname:pcchar):s32;cdecl;external; { * @brief Destroys a sslc context. The associated sockfd must be closed manually. * @param context sslc context. } function sslcDestroyContext(context:PsslcContext):s32;cdecl;external; { * @brief Starts the TLS connection. If successful, this will not return until the connection is ready for data-transfer via sslcRead/sslcWrite. * @param context sslc context. * @param internal_retval Optional ptr where the internal_retval will be written. The value is only copied to here by this function when no error occurred. * @param Optional ptr where an output u32 will be written. The value is only copied to here by this function when no error occurred. } function sslcStartConnection(context:PsslcContext; internal_retval:pcint; ptr:Pu32):s32;cdecl;external; { * @brief Receive data over the network connection. * @param context sslc context. * @param buf Output buffer. * @param len Size to receive. * @param peek When true, this is equivalent to setting the recv() MSG_PEEK flag. * @return When this isn't an error-code, this is the total transferred data size. } function sslcRead(context:PsslcContext; buf:pointer; len:u32; peek:bool):s32;cdecl;external; { * @brief Send data over the network connection. * @param context sslc context. * @param buf Input buffer. * @param len Size to send. * @return When this isn't an error-code, this is the total transferred data size. } function sslcWrite(context:PsslcContext; buf:pointer; len:u32):s32;cdecl;external; { * @brief Set the RootCertChain for the specified sslc context. * @param context sslc context. * @param handle RootCertChain contexthandle. } function sslcContextSetRootCertChain(context:PsslcContext; handle:u32):s32;cdecl;external; { * @brief Set the ClientCert-context for the specified sslc context. * @param context sslc context. * @param handle ClientCert contexthandle. } function sslcContextSetClientCert(context:PsslcContext; handle:u32):s32;cdecl;external; { * @brief Set the context for a CertChain from sslcCreate8CertChain(), for the specified sslc context. This needs updated once it's known what this context is for. * @param context sslc context. * @param handle contexthandle. } function sslcContextSetHandle8(context:PsslcContext; handle:u32):s32;cdecl;external; { * @brief Clears the options field bits for the context using the specified bitmask. * @param context sslc context. * @param bitmask opt bitmask. } function sslcContextClearOpt(context:PsslcContext; bitmask:u32):s32;cdecl;external; { * @brief This copies two strings from context state to the specified output buffers. Each string is only copied if it was successfully loaded. The maxsizes include the nul-terminator. This can only be used if sslcStartConnection() was already used su * @param context sslc context. * @param outprotocols Output buffer for a string containing all protocol versions supported by SSL-module. * @param outprotocols_maxsize Max size of the above output buffer. * @param outcipher Output buffer for a string containing the cipher suite currently being used. * @param outcipher_maxsize Max size of the above output buffer. } function sslcContextGetProtocolCipher(context:PsslcContext; outprotocols:pchar; outprotocols_maxsize:u32; outcipher:pchar; outcipher_maxsize:u32):s32;cdecl;external; { * @brief This loads an u32 from the specified context state. This needs updated once it's known what this field is for. * @param context sslc context. * @param Output ptr to write the value to. } function sslcContextGetState(context:PsslcContext; ptr:Pu32):s32;cdecl;external; { * @brief This initializes sharedmem for the specified context. * @param context sslc context. * @param buf Sharedmem buffer with address aligned to 0x1000-bytes. * @param size Sharedmem size aligned to 0x1000-bytes. } function sslcContextInitSharedmem(context:PsslcContext; buf:Pu8; size:u32):s32;cdecl;external; { * @brief This loads the specified cert. This needs updated once it's known what the cert format is and what the cert is used for later. * @param buf Input cert. * @param size Cert size. } function sslcAddCert(context:PsslcContext; buf:Pu8; size:u32):s32;cdecl;external;