Skip to content

Commit 2e1bbc8

Browse files
author
Hans Hörberg
committed
Added encryption certificate
1 parent 362b219 commit 2e1bbc8

File tree

12 files changed

+252
-98
lines changed

12 files changed

+252
-98
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,9 @@ example/sp/nocert_sp_conf/sp.xml
155155
example/sp/nocert_sp_conf/sp_conf.py
156156

157157
example/sp/nocert_sp_conf/who.ini
158+
159+
example/sp-repoze/my_sp.xml
160+
161+
example/sp-repoze/pki/localhost.ca.crt
162+
163+
example/sp-repoze/pki/localhost.ca.key

example/idp2/idp.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
import argparse
33
import base64
4-
4+
import xmldsig as ds
55
import re
66
import logging
77
import time
@@ -24,6 +24,7 @@
2424
from saml2.authn_context import PASSWORD
2525
from saml2.authn_context import UNSPECIFIED
2626
from saml2.authn_context import authn_context_class_ref
27+
from saml2.extension import pefim
2728
from saml2.httputil import Response
2829
from saml2.httputil import NotFound
2930
from saml2.httputil import geturl
@@ -38,7 +39,7 @@
3839
from saml2.s_utils import UnknownPrincipal
3940
from saml2.s_utils import UnsupportedBinding
4041
from saml2.s_utils import PolicyError
41-
from saml2.sigver import verify_redirect_signature
42+
from saml2.sigver import verify_redirect_signature, cert_from_instance, encrypt_cert_from_item
4243

4344
logger = logging.getLogger("saml2.idp")
4445

@@ -125,8 +126,9 @@ def operation(self, _dict, binding):
125126
return resp(self.environ, self.start_response)
126127
else:
127128
try:
129+
_encrypt_cert = encrypt_cert_from_item(_dict["req_info"].message)
128130
return self.do(_dict["SAMLRequest"], binding,
129-
_dict["RelayState"])
131+
_dict["RelayState"], encrypt_cert=_encrypt_cert)
130132
except KeyError:
131133
# Can live with no relay state
132134
return self.do(_dict["SAMLRequest"], binding)
@@ -151,7 +153,7 @@ def response(self, binding, http_args):
151153
resp = Response(http_args["data"], headers=http_args["headers"])
152154
return resp(self.environ, self.start_response)
153155

154-
def do(self, query, binding, relay_state=""):
156+
def do(self, query, binding, relay_state="", encrypt_cert=None):
155157
pass
156158

157159
def redirect(self):
@@ -277,7 +279,7 @@ def verify_request(self, query, binding):
277279

278280
return resp_args, _resp
279281

280-
def do(self, query, binding_in, relay_state=""):
282+
def do(self, query, binding_in, relay_state="", encrypt_cert=None):
281283
try:
282284
resp_args, _resp = self.verify_request(query, binding_in)
283285
except UnknownPrincipal, excp:
@@ -297,13 +299,10 @@ def do(self, query, binding_in, relay_state=""):
297299
if REPOZE_ID_EQUIVALENT:
298300
identity[REPOZE_ID_EQUIVALENT] = self.user
299301
try:
300-
sign_assertion = IDP.config.getattr("sign_assertion", "idp")
301-
if sign_assertion is None:
302-
sign_assertion = False
303302
_resp = IDP.create_authn_response(
304303
identity, userid=self.user,
305-
authn=AUTHN_BROKER[self.environ["idp.authn_ref"]], sign_assertion=sign_assertion,
306-
sign_response=False, **resp_args)
304+
authn=AUTHN_BROKER[self.environ["idp.authn_ref"]], sign_response=False, encrypt_cert=encrypt_cert,
305+
**resp_args)
307306
except Exception, excp:
308307
logging.error(exception_trace(excp))
309308
resp = ServiceError("Exception: %s" % (excp,))
@@ -537,7 +536,7 @@ def not_found(environ, start_response):
537536
# return subject, sp_entity_id
538537

539538
class SLO(Service):
540-
def do(self, request, binding, relay_state=""):
539+
def do(self, request, binding, relay_state="", encrypt_cert=None):
541540
logger.info("--- Single Log Out Service ---")
542541
try:
543542
_, body = request.split("\n")
@@ -589,7 +588,7 @@ def do(self, request, binding, relay_state=""):
589588

590589
class NMI(Service):
591590

592-
def do(self, query, binding, relay_state=""):
591+
def do(self, query, binding, relay_state="", encrypt_cert=None):
593592
logger.info("--- Manage Name ID Service ---")
594593
req = IDP.parse_manage_name_id_request(query, binding)
595594
request = req.message
@@ -617,7 +616,7 @@ def do(self, query, binding, relay_state=""):
617616

618617
# Only URI binding
619618
class AIDR(Service):
620-
def do(self, aid, binding, relay_state=""):
619+
def do(self, aid, binding, relay_state="", encrypt_cert=None):
621620
logger.info("--- Assertion ID Service ---")
622621

623622
try:
@@ -646,7 +645,7 @@ def operation(self, _dict, binding, **kwargs):
646645
# ----------------------------------------------------------------------------
647646

648647
class ARS(Service):
649-
def do(self, request, binding, relay_state=""):
648+
def do(self, request, binding, relay_state="", encrypt_cert=None):
650649
_req = IDP.parse_artifact_resolve(request, binding)
651650

652651
msg = IDP.create_artifact_response(_req, _req.artifact.text)
@@ -664,7 +663,7 @@ def do(self, request, binding, relay_state=""):
664663

665664
# Only SOAP binding
666665
class AQS(Service):
667-
def do(self, request, binding, relay_state=""):
666+
def do(self, request, binding, relay_state="", encrypt_cert=None):
668667
logger.info("--- Authn Query Service ---")
669668
_req = IDP.parse_authn_query(request, binding)
670669
_query = _req.message
@@ -688,7 +687,7 @@ def do(self, request, binding, relay_state=""):
688687

689688
# Only SOAP binding
690689
class ATTR(Service):
691-
def do(self, request, binding, relay_state=""):
690+
def do(self, request, binding, relay_state="", encrypt_cert=None):
692691
logger.info("--- Attribute Query Service ---")
693692

694693
_req = IDP.parse_attribute_query(request, binding)
@@ -721,7 +720,7 @@ def do(self, request, binding, relay_state=""):
721720

722721

723722
class NIM(Service):
724-
def do(self, query, binding, relay_state=""):
723+
def do(self, query, binding, relay_state="", encrypt_cert=None):
725724
req = IDP.parse_name_id_mapping_request(query, binding)
726725
request = req.message
727726
# Do the necessary stuff

example/sp-repoze/sp.xml

Lines changed: 0 additions & 34 deletions
This file was deleted.

example/sp-repoze/sp_conf.py

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,130 @@
1+
import uuid
12
from saml2 import BINDING_HTTP_REDIRECT
3+
import saml2
4+
from saml2.cert import OpenSSLWrapper
5+
from saml2.extension.idpdisc import BINDING_DISCO
26
from saml2.saml import NAME_FORMAT_URI
7+
#from saml2.sigver import CertHandlerExtra
8+
from saml2.entity_category.edugain import COC
9+
from saml2.entity_category.swamid import RESEARCH_AND_EDUCATION
10+
from saml2.entity_category.swamid import HEI
11+
from saml2.entity_category.swamid import SFS_1993_1153
12+
from saml2.entity_category.swamid import NREN
13+
from saml2.entity_category.swamid import EU
314

15+
16+
#BASE= "http://130.239.200.146:8087"
417
BASE= "http://localhost:8087"
518
#BASE= "http://lingon.catalogix.se:8087"
619

20+
"""
21+
class SpCertHandlerExtraClass(CertHandlerExtra):
22+
23+
def use_generate_cert_func(self):
24+
return True
25+
26+
def generate_cert(self, generate_cert_info, ca_cert_string, ca_key_string):
27+
print "Hello"
28+
return (ca_cert_string, ca_key_string)
29+
30+
def use_validate_cert_func(self):
31+
return False
32+
33+
def validate_cert(self, cert_str, ca_cert_string, ca_key_string):
34+
pass
35+
"""
36+
37+
def generate_cert():
38+
sn = uuid.uuid4().urn
39+
cert_info = {
40+
"cn": "localhost",
41+
"country_code": "se",
42+
"state": "ac",
43+
"city": "Umea",
44+
"organization": "ITS",
45+
"organization_unit": "DIRG"
46+
}
47+
osw = OpenSSLWrapper()
48+
ca_cert_str = osw.read_str_from_file("/Users/haho0032/Develop/root_cert/localhost.ca.crt")
49+
ca_key_str = osw.read_str_from_file("/Users/haho0032/Develop/root_cert/localhost.ca.key")
50+
#ca_cert_str = osw.read_str_from_file("/Users/haho0032/Develop/githubFork/pysaml2/example/sp-repoze/pki/localhost.ca.crt")
51+
#ca_key_str = osw.read_str_from_file("/Users/haho0032/Develop/githubFork/pysaml2/example/sp-repoze/pki/localhost.ca.key")
52+
req_cert_str, req_key_str = osw.create_certificate(cert_info, request=True, sn=sn, key_length=2048)
53+
cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str, req_cert_str)
54+
return cert_str, req_key_str
55+
756
CONFIG = {
8-
"entityid": "%s/sp.xml" % BASE,
9-
"description": "My SP",
57+
"entityid": "%s/LocalTestSPHans.xml" % BASE,
58+
"description": "Lokal test SP Hans",
59+
"entity_category": [COC, RESEARCH_AND_EDUCATION, HEI, SFS_1993_1153, NREN, EU],
60+
"generate_cert_func": generate_cert,
61+
#Information needed for generated cert (NO CERT) solution.
62+
#"only_use_keys_in_metadata": False,
63+
#"cert_handler_extra_class": None,#MyCertGeneration(),
64+
#"generate_cert_info": {
65+
# "cn": "localhost",
66+
# "country_code": "se",
67+
# "state": "ac",
68+
# "city": "Umea",
69+
# "organization": "ITS Umea University",
70+
# "organization_unit": "DIRG"
71+
#},
72+
#"tmp_key_file": "pki/tmp_mykey.pem",
73+
#"tmp_cert_file": "pki/tmp_mycert.pem",
74+
#"validate_certificate": True,
75+
#############################################################
1076
"service": {
1177
"sp": {
12-
"name": "Rolands SP",
78+
#Information needed for generated cert (NO CERT) solution.
79+
"authn_requests_signed": "true", #Will sign the request!
80+
"want_assertions_signed": "false", #Demands that the assertion is signed.
81+
"allow_unsolicited": "true", #Allows the message not to be ment for this sp.
82+
#############################################################
83+
"name": "LocalTestSPHans",
1384
"endpoints": {
1485
"assertion_consumer_service": [BASE],
1586
"single_logout_service": [(BASE + "/slo",
1687
BINDING_HTTP_REDIRECT)],
88+
"discovery_response": [
89+
("%s/disco" % BASE, BINDING_DISCO)
90+
]
1791
},
1892
"required_attributes": ["surname", "givenname",
1993
"edupersonaffiliation"],
2094
"optional_attributes": ["title"],
2195
}
2296
},
2397
"debug": 1,
98+
#Information needed for generated cert (NO CERT) solution.
2499
"key_file": "pki/mykey.pem",
25100
"cert_file": "pki/mycert.pem",
101+
#############################################################
26102
"attribute_map_dir": "./attributemaps",
27-
"metadata": {"local": ["../idp2/idp.xml"]},
103+
"metadata": {
104+
#"local": ["../idp2/idp_nocert.xml"],
105+
#"local": ["/Users/haho0032/Develop/svn/trunk/pyOpSamlProxy/idp_nocert.xml"],
106+
107+
#Information needed for generated cert (NO CERT) solution.
108+
#"local": ["/Users/haho0032/Develop/github/IdProxy/idp_nocert.xml"],
109+
"local": ["/Users/haho0032/Develop/github/IdProxy/idp.xml"],
110+
#"local": ["../idp2/idp.xml"],
111+
#############################################################
112+
113+
#"local": ["/Users/haho0032/Develop/github/IdProxy/idp.xml"],
114+
# #"remote": [{"url": "http://130.239.201.5/role/idp.xml", "cert": None}],
115+
116+
},
117+
118+
28119
# -- below used by make_metadata --
29120
"organization": {
30-
"name": "Exempel AB",
31-
"display_name": [("Exempel AB", "se"), ("Example Co.", "en")],
32-
"url": "http://www.example.com/roland",
121+
"name": "Lokal test SP Hans",
122+
"display_name": [("Lokal test SP Hans", "se"), ("Lokal test SP Hans", "en")],
123+
"url": "http://130.239.200.146:8087",
33124
},
34-
"contact_person": [{
35-
"given_name":"John",
36-
"sur_name": "Smith",
37-
"email_address": ["[email protected]"],
38-
"contact_type": "technical",
39-
},
125+
"contact_person": [
40126
],
41-
#"xmlsec_binary":"/opt/local/bin/xmlsec1",
127+
"xmlsec_binary": '/usr/local/bin/xmlsec1',
42128
"name_form": NAME_FORMAT_URI,
43129
"logger": {
44130
"rotating": {
@@ -49,3 +135,4 @@
49135
"loglevel": "debug",
50136
}
51137
}
138+

0 commit comments

Comments
 (0)