////////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: Service/CredentialsManager/Credentials.sg
//
// Note: Classes for representing credentials and evidence.
//
using System;
using System.Collections;
using System.Diagnostics;
using Microsoft.Contracts;
using Microsoft.Singularity;
using Microsoft.Singularity.Channels;
using Microsoft.Singularity.Directory;
using Microsoft.Singularity.Io;
using Microsoft.SingSharp;
using Ex = Microsoft.Singularity.Security;
namespace Microsoft.Singularity.Security.CredentialsManager
{
/**
This class contains a set of credentials, including sensitive "private" info,
such as passwords, private keys, etc. The Credentials Manager allows clients
to create these entries, enumerate them, etc., but never allows the private
info to flow out of the credentials manager. The CM also allows clients to
create "supplicants", which are instances of security protocols that are
allowed to make use of the private info, but not to expose it.
*/
class Credentials
{
public Credentials(CredentialsId! id, CredentialsEvidence! evidence)
{
this.Id = id;
this.Evidence = evidence;
}
public CredentialsId! Id;
public CredentialsEvidence! Evidence;
}
/**
This class is used as a key in Hashtable, so it implements identity comparison
methods, including Object.GetHashCode(), operator==, operator!=, and Object.Equals().
Instances of this class are immutable.
*/
class CredentialsId
{
public CredentialsId(string! name, string! tag)
{
this.CredentialsName = name;
this.Tag = tag;
}
public CredentialsId(Ex.CredentialsId excredentials)
{
this(
Bitter.ToString2(excredentials.CredentialsName),
Bitter.ToString2(excredentials.Tag));
}
public readonly string! CredentialsName;
public readonly string! Tag;
override public string! ToString()
{
return String.Format("[CredentialsId: name:{0} tag:{1}]", this.CredentialsName, this.Tag);
}
public Ex.CredentialsId ToExchange()
{
Ex.CredentialsId ex = new Ex.CredentialsId();
ex.CredentialsName = Bitter.FromString2(this.CredentialsName);
ex.Tag = Bitter.FromString2(this.Tag);
return ex;
}
override public int GetHashCode()
{
return unchecked((int)(CredentialsName.GetHashCode() + 3 * Tag.GetHashCode()));
}
override public bool Equals(object obj)
{
CredentialsId comparand = obj as CredentialsId;
if (comparand == null)
return false;
return this == comparand;
}
public static bool operator== (CredentialsId! a, CredentialsId! b)
{
return a.CredentialsName == b.CredentialsName && a.Tag == b.Tag;
}
public static bool operator!= (CredentialsId! a, CredentialsId! b)
{
return a.CredentialsName != b.CredentialsName || a.Tag != b.Tag;
}
}
// Base class for classes that provide evidence of ownership of a key.
abstract class CredentialsEvidence
{
}
class PasswordEvidence : CredentialsEvidence
{
public PasswordEvidence(string! password)
{
this.Password = password;
}
public readonly string! Password;
}
/*
class PrivateKeyEvidence : CredentialsEvidence
{
???
}
*/
}