コード例 #1
0
 /// <summary>
 /// user cert application
 /// </summary>
 /// <param name="config">basic information</param>
 /// <param name="reqBody">user cert requests</param>
 /// <returns></returns>
 public Tuple <bool, string> EnrollUser(EnrollUserReqBody reqBody)
 {
     try
     {
         NodeApiReqBody <EnrollUserReqBody> req = new NodeApiReqBody <EnrollUserReqBody>()
         {
             body = new EnrollUserReqBody()
             {
                 name   = reqBody.name,
                 secret = reqBody.secret
             },
             header = new ReqHeader()
             {
                 appCode  = config.appInfo.AppCode,
                 userCode = config.userCode
             }
         };
         ////get csr
         var resCsr = config.appInfo.AlgorithmType == EmAlgorithmType.SM2 ?
                      CsrHelper.GetSMCsr(string.Format("{0}@{1}", reqBody.name, config.appInfo.AppCode))
           : CsrHelper.GetCsr(string.Format("{0}@{1}", reqBody.name, config.appInfo.AppCode));
         req.body.csrPem = resCsr.Item1.Replace("\r", "");
         // assemble the original string to sign
         var data = ReqMacExtends.GetEnrollUserReqMac(req);
         req.mac = sign.Sign(data);
         var res = SendHelper.SendPost <NodeApiResBody <EnrollUserResBody> >(config.reqUrl + EnrollUserUrl, JsonConvert.SerializeObject(req), config.httpsCert);
         if (res != null)
         {
             //check the status codes in turn
             if (res.header.code != 0)
             {
                 return(new Tuple <bool, string>(false, res.header.msg));
             }
             //assemble the original string to sign
             var datares = ResMacExtends.GetEnrollUserResMac(res);
             //verify data
             if (sign.Verify(res.mac, datares))
             {
                 //save the private key and cert
                 if (!string.IsNullOrEmpty(res.body.cert))
                 {
                     CertStore.SaveCert(res.body.cert, Path.Combine(config.mspDir, string.Format("{0}@{1}_cert.pem", reqBody.name, config.appInfo.AppCode)));
                     ECDSAStore.SavePriKey(resCsr.Item2, Path.Combine(config.mspDir, string.Format("{0}@{1}_sk.pem", reqBody.name, config.appInfo.AppCode)));
                 }
                 return(new Tuple <bool, string>(true, "cert registration successful"));
             }
             else
             {
                 return(new Tuple <bool, string>(false, "failed to verify the signature"));
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(new Tuple <bool, string>(false, "failed to verify the cert"));
 }
コード例 #2
0
        public static ECKeyPair GetUserKey(string userName, AppSetting a)
        {
            try
            {
                var       prikurl = a.mspDir + "/" + userName + "@" + a.appInfo.AppCode + "_prik";
                var       pubkurl = a.mspDir + "/" + userName + "@" + a.appInfo.AppCode + "_pubk";
                ECKeyPair key     = new ECKeyPair();

                if (!File.Exists(prikurl))
                {
                    if (a.appInfo.AlgorithmType == EmAlgorithmType.SM2)
                    {
                        var sm2key = SM2.SM2Utils.GenerateKeyPair();

                        ECDSAStore.SavePriKey((ECPrivateKeyParameters)sm2key.Private, prikurl);
                        ECDSAStore.SavePubKey((ECPublicKeyParameters)sm2key.Public, pubkurl);
                        key.prik = (ECPrivateKeyParameters)sm2key.Private;
                        key.pubk = (ECPublicKeyParameters)sm2key.Public;
                    }
                    else
                    {
                        var k1key = Ecdsa.ECDSAUtils.GenerateSecP256k1KeyPair();
                        ECDSAStore.SavePriKey(k1key.Private, prikurl);
                        ECDSAStore.SavePubKey(k1key.Public, pubkurl);
                        key.prik = (ECPrivateKeyParameters)k1key.Private;
                        key.pubk = (ECPublicKeyParameters)k1key.Public;
                    }
                }
                else
                {
                    key.prik = LibraryHelper.LoadPrikey(prikurl);
                    key.pubk = LibraryHelper.LoadPubkey(pubkurl);
                }

                return(key);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }