【问题标题】:Which PEM file should I provide when uploading to S3 using HTTP PUT使用 HTTP PUT 上传到 S3 时我应该提供哪个 PEM 文件
【发布时间】:2012-02-06 16:02:40
【问题描述】:

我正在尝试使用我的 Java Web 服务器提供的预签名签名将文件放入 S3 http://docs.amazonwebservices.com/AmazonS3/latest/dev/PresignedUrlUploadObjectDotNetSDK.html

我需要我的上传客户端(目前我的 Windows 7 使用 C++)与亚马逊服务器握手,但我不知道该怎么做。

当我尝试使用“默认上下文”(天真地)发送请求时,它会打印“证书链中的自签名证书”错误,并要求我接受或不接受证书。 然后我试图弄清楚如何添加证书并找到了这段代码: POCO C++ - NET SSL - how to POST HTTPS request

问题是我不确定这里需要哪个 pem 文件。 我尝试在 Amazon Web Services 控制台中提供从 x.509 下载的 pem 文件,但它引发了 SSL 异常:SSL3_GET_SERVER_CERTIFICATE

我的代码:

URI uri("https://BUCKET.s3.amazonaws.com/nosigfile?Expires=1959682330&AWSAccessKeyId=ACCESSKEY&Signature=DgOifWPmQi%2BASAIDaIOGXla10%2Fw%3D");
const Poco::Net::Context::Ptr context( new Poco::Net::Context( Poco::Net::Context::CLIENT_USE, "", "", "cert(x509).pem") );
Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context );
HTTPRequest req(HTTPRequest::HTTP_PUT, uri.getPathAndQuery(), HTTPMessage::HTTP_1_1);
req.setContentLength(contentLength);
session.sendRequest(req) << streamToSend;

谢谢

【问题讨论】:

    标签: amazon-s3 openssl x509 poco-libraries


    【解决方案1】:

    Poco 在项目中包含证书。

    您将需要 any.pem、rootcert.pem、yourappname.xml,您可以在 SSL 端的 poco 测试套件中找到它们。

    ./poco-1.4.1p1-all/NetSSL_OpenSSL/testsuite/{any.pem,rootcert.pem,testsuite.xml}
    

    一旦包含两个 pem 文件,即在 initializeSSL 阶段使用的 xml,您将不会收到自签名证书的警告。

    class MySSLApp: public Poco::Util::Application
    {
    public:
        MySSLApp()
        {
            Poco::Net::initializeSSL();
            Poco::Net::HTTPStreamFactory::registerFactory();
            Poco::Net::HTTPSStreamFactory::registerFactory();
        }
    
        ~MySSLApp()
        {
            Poco::Net::uninitializeSSL();
        }
    protected:
        void initialize(Poco::Util::Application& self)
        {
            loadConfiguration(); // load default configuration files, if present
            Poco::Util::Application::initialize(self);
        }
    
        void myUpload(...) {
            ...
            FilePartSource* pFPS = new FilePartSource(szFilename);
            std::string szHost = "BUCKET.s3.amazonaws.com";
            std::string szPath = "/";
            int nRespCode = 201;
            try{
                HTTPClientSession s(szHost);
                HTTPRequest request(HTTPRequest::HTTP_POST, szPath, HTTPMessage::HTTP_1_1);
                HTMLForm pocoForm(HTMLForm::ENCODING_MULTIPART);
                pocoForm.set("AWSAccessKeyId",        ACCESSKEY);
                pocoForm.set("acl",                   "public-read");
                pocoForm.set("success_action_status", toString(nRespCode));
                pocoForm.set("Content-Type",          m_szContentType);
                pocoForm.set("key",                   m_szPath + "/" + m_szDestFileName);
                pocoForm.set("policy",                m_szPolicy);
                pocoForm.set("signature",             m_szSignature);
                pocoForm.addPart("file",              pFPS);
    
                pocoForm.prepareSubmit(request);
    
                std::ostringstream oszMessage;
                pocoForm.write(oszMessage);
                std::string szMessage = oszMessage.str();
    
                //AWS requires a ContentLength set EVEN though it is chunked!
                request.setContentLength((int) szMessage.length());
    
                s.sendRequest(request) << szMessage;
                //or:
                //pocoForm.write(s.sendRequest(request));
    
                HTTPResponse response;
                std::istream& rs = s.receiveResponse(response);
                int code = response.getStatus();
                if (code != nRespCode) {
                    stringstream s;
                    s << "HTTP Error " << code;
                    throw Poco::IOException(s.str());
                }
            } catch (Exception& exc) {
                std::cout << exc.displayText() << endl;
                return;
            }
            return;   
        }
     }
    

    xml 文件将如下所示:

    <AppConfig>
    <openSSL>
        <server>
            <privateKeyFile>${application.configDir}any.pem</privateKeyFile>
            <caConfig>${application.configDir}rootcert.pem</caConfig>
            <verificationMode>none</verificationMode>
            <verificationDepth>9</verificationDepth>
            <loadDefaultCAFile>true</loadDefaultCAFile>
            <cypherList>ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH</cypherList>
            <privateKeyPassphraseHandler>
                <name>KeyFileHandler</name>
                <options>
                    <password>secret</password>
                </options>
            </privateKeyPassphraseHandler>
            <invalidCertificateHandler>
                <name>AcceptCertificateHandler</name>
                <options>
                </options>
            </invalidCertificateHandler>
        </server>
        <client>
            <privateKeyFile>${application.configDir}any.pem</privateKeyFile>
            <caConfig>${application.configDir}rootcert.pem</caConfig>
            <verificationMode>relaxed</verificationMode>
            <verificationDepth>9</verificationDepth>
            <loadDefaultCAFile>true</loadDefaultCAFile>
            <cypherList>ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH</cypherList>
            <privateKeyPassphraseHandler>
                <name>KeyFileHandler</name>
                <options>
                    <password>secret</password>
                </options>
            </privateKeyPassphraseHandler>
            <invalidCertificateHandler>
                <name>AcceptCertificateHandler</name>
                <options>
                </options>
            </invalidCertificateHandler>
        </client>
    </openSSL>
    </AppConfig>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 2019-03-10
      相关资源
      最近更新 更多