weblogic 10.0:samlsignedobject.verify()は署名値の検証に失敗しました
-
20-09-2019 - |
質問
私はしばらくこの問題を抱えてきましたが、それは私を夢中にさせています。 SAML 1.1を使用してWeblogic 10.0サーバー(ブラウザ/投稿プロファイルを使用して単一のサインオンシナリオ)にサインオンするクライアント(C#.NET 2.0)を作成しようとしています。クライアントはWINXPマシン上にあり、WebLogicサーバーはRhel 5ボックスにあります。
ここの例の例では、クライアントの主にコードに基づいています。 http://www.codeproject.com/kb/aspnet/dotnetsamlpost.aspx (ソースには、SAML 1.1のセクションがあります)。
ここからSAML Destinationサイトの指示に基づいてWebLogicを設定しました。http://www.oracle.com/technology/pub/articles/dev2arch/2006/12/sso-with-saml4.html
VS 2005に付属するMakeCertを使用して証明書を作成しました。
makecert -r -pe -n "CN=whatever" -b 01/01/2010 -e 01/01/2011 -sky exchange whatever.cer -sv whatever.pvk
pvk2pfx.exe -pvk whatever.pvk -spc whatever.cer -pfx whatever.pfx
次に、.pfxを個人証明書ディレクトリにインストールし、.cerをWebLogic SAML ID ASSERTER V2にインストールしました。
署名後に応答(つまり、白人を追加する)をフォーマットすることを別のサイトで読みました。署名後、この問題を引き起こすと、オン/オフのXMLWriterSettingsのオン/オフのさまざまな組み合わせを試してみました。 XMLドキュメントは、どれも違いを生みませんでした。メッセージがエンコード/送信される前と、到着/デコードされた後、署名のバルブを印刷しましたが、それらは同じです。
したがって、明確にするために、応答は形成、エンコード、送信、およびデコードされているように見えます(Weblogicログに完全な応答が表示されます)。 Weblogicは、使用したい証明書を見つけ、キーが提供され、署名情報を取得し、署名の検証に失敗することを確認します。
コード:
public string createResponse(Dictionary<string, string> attributes){
ResponseType response = new ResponseType();
// Create Response
response.ResponseID = "_" + Guid.NewGuid().ToString();
response.MajorVersion = "1";
response.MinorVersion = "1";
response.IssueInstant = System.DateTime.UtcNow;
response.Recipient = "http://theWLServer/samlacs/acs";
StatusType status = new StatusType();
status.StatusCode = new StatusCodeType();
status.StatusCode.Value = new XmlQualifiedName("Success", "urn:oasis:names:tc:SAML:1.0:protocol");
response.Status = status;
// Create Assertion
AssertionType assertionType = CreateSaml11Assertion(attributes);
response.Assertion = new AssertionType[] {assertionType};
//Serialize
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("samlp", "urn:oasis:names:tc:SAML:1.0:protocol");
ns.Add("saml", "urn:oasis:names:tc:SAML:1.0:assertion");
XmlSerializer responseSerializer =
new XmlSerializer(response.GetType());
StringWriter stringWriter = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = false;//I've tried both ways, for the fun of it
settings.Encoding = Encoding.UTF8;
XmlWriter responseWriter = XmlTextWriter.Create(stringWriter, settings);
responseSerializer.Serialize(responseWriter, response, ns);
responseWriter.Close();
string samlString = stringWriter.ToString();
stringWriter.Close();
// Sign the document
XmlDocument doc = new XmlDocument();
doc.PreserveWhiteSpace = true; //also tried this both ways to no avail
doc.LoadXml(samlString);
X509Certificate2 cert = null;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection coll = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "distName", true);
if (coll.Count < 1) {
throw new ArgumentException("Unable to locate certificate");
}
cert = coll[0];
store.Close();
//this special SignDoc just overrides a function in SignedXml so
//it knows to look for ResponseID rather than ID
XmlElement signature = SamlHelper.SignDoc(
doc, cert, "ResponseID", response.ResponseID);
doc.DocumentElement.InsertBefore(signature,
doc.DocumentElement.ChildNodes[0]);
// Base64Encode and URL Encode
byte[] base64EncodedBytes =
Encoding.UTF8.GetBytes(doc.OuterXml);
string returnValue = System.Convert.ToBase64String(
base64EncodedBytes);
return returnValue;
}
private AssertionType CreateSaml11Assertion(Dictionary<string, string> attributes){
AssertionType assertion = new AssertionType();
assertion.AssertionID = "_" + Guid.NewGuid().ToString();
assertion.Issuer = "madeUpValue";
assertion.MajorVersion = "1";
assertion.MinorVersion = "1";
assertion.IssueInstant = System.DateTime.UtcNow;
//Not before, not after conditions
ConditionsType conditions = new ConditionsType();
conditions.NotBefore = DateTime.UtcNow;
conditions.NotBeforeSpecified = true;
conditions.NotOnOrAfter = DateTime.UtcNow.AddMinutes(10);
conditions.NotOnOrAfterSpecified = true;
//Name Identifier to be used in Saml Subject
NameIdentifierType nameIdentifier = new NameIdentifierType();
nameIdentifier.NameQualifier = domain.Trim();
nameIdentifier.Value = subject.Trim();
SubjectConfirmationType subjectConfirmation = new SubjectConfirmationType();
subjectConfirmation.ConfirmationMethod = new string[] { "urn:oasis:names:tc:SAML:1.0:cm:bearer" };
//
// Create some SAML subject.
SubjectType samlSubject = new SubjectType();
AttributeStatementType attrStatement = new AttributeStatementType();
AuthenticationStatementType authStatement = new AuthenticationStatementType();
authStatement.AuthenticationMethod = "urn:oasis:names:tc:SAML:1.0:am:password";
authStatement.AuthenticationInstant = System.DateTime.UtcNow;
samlSubject.Items = new object[] { nameIdentifier, subjectConfirmation};
attrStatement.Subject = samlSubject;
authStatement.Subject = samlSubject;
IPHostEntry ipEntry =
Dns.GetHostEntry(System.Environment.MachineName);
SubjectLocalityType subjectLocality = new SubjectLocalityType();
subjectLocality.IPAddress = ipEntry.AddressList[0].ToString();
authStatement.SubjectLocality = subjectLocality;
attrStatement.Attribute = new AttributeType[attributes.Count];
int i=0;
// Create SAML attributes.
foreach (KeyValuePair<string, string> attribute in attributes) {
AttributeType attr = new AttributeType();
attr.AttributeName = attribute.Key;
attr.AttributeNamespace= domain;
attr.AttributeValue = new object[] {attribute.Value};
attrStatement.Attribute[i] = attr;
i++;
}
assertion.Conditions = conditions;
assertion.Items = new StatementAbstractType[] {authStatement, attrStatement};
return assertion;
}
private static XmlElement SignDoc(XmlDocument doc, X509Certificate2 cert2, string referenceId, string referenceValue) {
// Use our own implementation of SignedXml
SamlSignedXml sig = new SamlSignedXml(doc, referenceId);
// Add the key to the SignedXml xmlDocument.
sig.SigningKey = cert2.PrivateKey;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri= String.Empty;
reference.Uri = "#" + referenceValue;
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object.
sig.AddReference(reference);
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new KeyInfoX509Data(cert2));
sig.KeyInfo = keyInfo;
// Compute the signature.
sig.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = sig.GetXml();
return xmlDigitalSignature;
}
クライアントアプリでページを開くには、
string postData = String.Format("SAMLResponse={0}&APID=ap_00001&TARGET={1}", System.Web.HttpUtility.UrlEncode(builder.buildResponse("http://theWLServer/samlacs/acs",attributes)), "http://desiredURL");
webBrowser.Navigate("http://theWLServer/samlacs/acs", "_self", Encoding.UTF8.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded");
解決
聖なるがらくた私はついにそれを見つけました。とにかく、その一部。 signdoc()関数では、参照に別の変換を追加する必要がありました。
reference.AddTransform(new XmlDsigExcC14NTransform());
私が知る限り、標準化法を排他的に変更します。私はそれがすでに行われていると思っていました(CanonicalizationMethod要素が応答に表示されていたので)が、明らかにそうではありません。
今、私は別のエラーでヒットし、「ベアラー」サブジェクト確認方法が無効であることを伝えてくれます。 Bearer Methodがブラウザ/投稿に使用するものであると読んだと思いましたが、この時点で、私はほとんど気にしない最初のエラーを過ぎてしまうことをとてもうれしく思います。