f



Secure ssl connection with wrap_socket

Hi, I'm a new python user and I'm writing a small web service with ssl.
I want use a self-signed certificate like in wiki:
http://docs.python.org/dev/library/ssl.html#certificates
I've used wrap_socket, but if i try to use
cert_reqs=ssl.CERT_REQUIRED, it doesn't work with error:

urllib2.URLError: <urlopen error _ssl.c:326: No root certificates
specified for verification of other-side certificates.>

It works only with CERT_NONE (the default) but with this option i
could access to the service in insicure mode.

Have you some suggestions for my service?

Thanks. Regards.

-- 
Andrea Di Mario
0
7/5/2011 8:52:57 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

3 Replies
1002 Views

Similar Articles

[PageSpeed] 49

On Jul 5, 4:52=A0am, Andrea Di Mario <anddima...@gmail.com> wrote:
> Hi, I'm a new python user and I'm writing a small web service with ssl.
> I want use a self-signed certificate like in wiki:http://docs.python.org/=
dev/library/ssl.html#certificates
> I've used wrap_socket, but if i try to use
> cert_reqs=3Dssl.CERT_REQUIRED, it doesn't work with error:
>
> urllib2.URLError: <urlopen error _ssl.c:326: No root certificates
> specified for verification of other-side certificates.>
>
> It works only with CERT_NONE (the default) but with this option i
> could access to the service in insicure mode.
>
> Have you some suggestions for my service?
>

Also specify some root certificates to use in verifying the peer's
certificate.  Certificate verification works by proceeding from a
collection of "root" certificates which are explicitly trusted.  These
are used to sign other certificates (which may in turn be used to sign
others, which in turn...).  The process of certificate verification is
the process of following the signatures from the certificate in use by
the server you connect to back up the chain until you reach a root
which you have either decided to trust or not.  If the signatures are
all valid and the root is one you trust, then you have established a
connection to a trusted entity.  If any signature is invalid, or the
root is not one you trust, then you have not.

The root certificates are also called the "ca certificates" or
"certificate authority certificates".  `wrap_socket` accepts a
`ca_certs` argument.  See http://docs.python.org/library/ssl.html#ssl-certi=
ficates
for details about that argument.

Jean-Paul
0
7/5/2011 2:08:59 PM
On Jul 5, 4:08=A0pm, Jean-Paul Calderone <calderone.jeanp...@gmail.com>
wrote:
> On Jul 5, 4:52=A0am, Andrea Di Mario <anddima...@gmail.com> wrote:
>
> > Hi, I'm a new python user and I'm writing a small web service with ssl.
> > I want use a self-signed certificate like in wiki:http://docs.python.or=
g/dev/library/ssl.html#certificates
> > I've used wrap_socket, but if i try to use
> > cert_reqs=3Dssl.CERT_REQUIRED, it doesn't work with error:
>
> > urllib2.URLError: <urlopen error _ssl.c:326: No root certificates
> > specified for verification of other-side certificates.>
>
> > It works only with CERT_NONE (the default) but with this option i
> > could access to the service in insicure mode.
>
> > Have you some suggestions for my service?
>
> Also specify some root certificates to use in verifying the peer's
> certificate. =A0Certificate verification works by proceeding from a
> collection of "root" certificates which are explicitly trusted. =A0These
> are used to sign other certificates (which may in turn be used to sign
> others, which in turn...). =A0The process of certificate verification is
> the process of following the signatures from the certificate in use by
> the server you connect to back up the chain until you reach a root
> which you have either decided to trust or not. =A0If the signatures are
> all valid and the root is one you trust, then you have established a
> connection to a trusted entity. =A0If any signature is invalid, or the
> root is not one you trust, then you have not.
>
> The root certificates are also called the "ca certificates" or
> "certificate authority certificates". =A0`wrap_socket` accepts a
> `ca_certs` argument. =A0Seehttp://docs.python.org/library/ssl.html#ssl-ce=
rtificates
> for details about that argument.
>
> Jean-Paul

Hi Jean-Paul, i thought that with self-signed certificate i shouldn't
use ca_certs option. Now, i've created a ca-authority and i use this
command:

 self.sock =3D ssl.wrap_socket(sock, certfile =3D "ca/certs/
myfriend.cert.pem", keyfile =3D "ca/private/myfriend.key.pem",
ca_certs=3D"/home/andrea/ca/certs/cacert.pem",
cert_reqs=3Dssl.CERT_REQUIRED)

When i use the some machine as client-server it works, but, when i use
another machine as client, i've this:

Traceback (most recent call last):
=A0 File "loginsender.py", line 48, in <module>
=A0 =A0 handle =3D url_opener.open('https://debian.andrea.it:10700/%s+%s' %
(DATA,IPIN))
=A0 File "/usr/lib/python2.6/urllib2.py", line 391, in open
=A0 =A0 response =3D self._open(req, data)
=A0 File "/usr/lib/python2.6/urllib2.py", line 409, in _open
=A0 =A0 '_open', req)
=A0 File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
=A0 =A0 result =3D func(*args)
=A0 File "loginsender.py", line 33, in https_open
=A0 =A0 return self.do_open(self.specialized_conn_class, req)
=A0 File "/usr/lib/python2.6/urllib2.py", line 1145, in do_open
=A0 =A0 raise URLError(err)
urllib2.URLError: <urlopen error [Errno 185090050] _ssl.c:328: error:
0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib>

I see that i should create a certificate with server, client and ca
autority, but i haven't clear the ca_certs option and which path i
should use.
Have you any suggestion?

Thank. Regards.
0
7/6/2011 8:44:16 AM
On Jul 6, 4:44=A0am, AndDM <anddima...@gmail.com> wrote:
> On Jul 5, 4:08=A0pm, Jean-Paul Calderone <calderone.jeanp...@gmail.com>
> wrote:
>
>
>
> > On Jul 5, 4:52=A0am, Andrea Di Mario <anddima...@gmail.com> wrote:
>
> > > Hi, I'm a new python user and I'm writing a small web service with ss=
l.
> > > I want use a self-signed certificate like in wiki:http://docs.python.=
org/dev/library/ssl.html#certificates
> > > I've used wrap_socket, but if i try to use
> > > cert_reqs=3Dssl.CERT_REQUIRED, it doesn't work with error:
>
> > > urllib2.URLError: <urlopen error _ssl.c:326: No root certificates
> > > specified for verification of other-side certificates.>
>
> > > It works only with CERT_NONE (the default) but with this option i
> > > could access to the service in insicure mode.
>
> > > Have you some suggestions for my service?
>
> > Also specify some root certificates to use in verifying the peer's
> > certificate. =A0Certificate verification works by proceeding from a
> > collection of "root" certificates which are explicitly trusted. =A0Thes=
e
> > are used to sign other certificates (which may in turn be used to sign
> > others, which in turn...). =A0The process of certificate verification i=
s
> > the process of following the signatures from the certificate in use by
> > the server you connect to back up the chain until you reach a root
> > which you have either decided to trust or not. =A0If the signatures are
> > all valid and the root is one you trust, then you have established a
> > connection to a trusted entity. =A0If any signature is invalid, or the
> > root is not one you trust, then you have not.
>
> > The root certificates are also called the "ca certificates" or
> > "certificate authority certificates". =A0`wrap_socket` accepts a
> > `ca_certs` argument. =A0Seehttp://docs.python.org/library/ssl.html#ssl-=
certificates
> > for details about that argument.
>
> > Jean-Paul
>
> Hi Jean-Paul, i thought that with self-signed certificate i shouldn't
> use ca_certs option. Now, i've created a ca-authority and i use this
> command:
>
> =A0self.sock =3D ssl.wrap_socket(sock, certfile =3D "ca/certs/
> myfriend.cert.pem", keyfile =3D "ca/private/myfriend.key.pem",
> ca_certs=3D"/home/andrea/ca/certs/cacert.pem",
> cert_reqs=3Dssl.CERT_REQUIRED)
>
> When i use the some machine as client-server it works, but, when i use
> another machine as client, i've this:
>
> Traceback (most recent call last):
> =A0 File "loginsender.py", line 48, in <module>
> =A0 =A0 handle =3D url_opener.open('https://debian.andrea.it:10700/%s+%s'=
%
> (DATA,IPIN))
> =A0 File "/usr/lib/python2.6/urllib2.py", line 391, in open
> =A0 =A0 response =3D self._open(req, data)
> =A0 File "/usr/lib/python2.6/urllib2.py", line 409, in _open
> =A0 =A0 '_open', req)
> =A0 File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
> =A0 =A0 result =3D func(*args)
> =A0 File "loginsender.py", line 33, in https_open
> =A0 =A0 return self.do_open(self.specialized_conn_class, req)
> =A0 File "/usr/lib/python2.6/urllib2.py", line 1145, in do_open
> =A0 =A0 raise URLError(err)
> urllib2.URLError: <urlopen error [Errno 185090050] _ssl.c:328: error:
> 0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib>
>
> I see that i should create a certificate with server, client and ca
> autority, but i haven't clear the ca_certs option and which path i
> should use.
> Have you any suggestion?

You need to have the CA certificate on any machine that is going to
verify the certificate used on the SSL connection.  The path just
needs to be the path to that CA certificate on the client machine.

Jean-Paul
0
7/6/2011 1:53:09 PM
Reply: