• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 package com.google.crypto.tink.testing;
18 
19 import com.google.crypto.tink.PublicKeySign;
20 import com.google.crypto.tink.PublicKeyVerify;
21 import com.google.crypto.tink.testing.proto.CreationRequest;
22 import com.google.crypto.tink.testing.proto.CreationResponse;
23 import com.google.crypto.tink.testing.proto.SignatureGrpc.SignatureImplBase;
24 import com.google.crypto.tink.testing.proto.SignatureSignRequest;
25 import com.google.crypto.tink.testing.proto.SignatureSignResponse;
26 import com.google.crypto.tink.testing.proto.SignatureVerifyRequest;
27 import com.google.crypto.tink.testing.proto.SignatureVerifyResponse;
28 import com.google.protobuf.ByteString;
29 import io.grpc.stub.StreamObserver;
30 import java.security.GeneralSecurityException;
31 
32 /** Implements a gRPC Signature Testing service. */
33 public final class SignatureServiceImpl extends SignatureImplBase {
34 
SignatureServiceImpl()35   public SignatureServiceImpl() throws GeneralSecurityException {
36   }
37 
38   @Override
createPublicKeySign( CreationRequest request, StreamObserver<CreationResponse> responseObserver)39   public void createPublicKeySign(
40       CreationRequest request, StreamObserver<CreationResponse> responseObserver) {
41     Util.createPrimitiveForRpc(request, responseObserver, PublicKeySign.class);
42   }
43 
44   @Override
createPublicKeyVerify( CreationRequest request, StreamObserver<CreationResponse> responseObserver)45   public void createPublicKeyVerify(
46       CreationRequest request, StreamObserver<CreationResponse> responseObserver) {
47     Util.createPrimitiveForRpc(request, responseObserver, PublicKeyVerify.class);
48   }
49 
sign(SignatureSignRequest request)50   private SignatureSignResponse sign(SignatureSignRequest request) throws GeneralSecurityException {
51     try {
52       PublicKeySign signer =
53           Util.parseBinaryProtoKeyset(request.getPrivateAnnotatedKeyset())
54               .getPrimitive(PublicKeySign.class);
55       byte[] signatureValue = signer.sign(request.getData().toByteArray());
56       return SignatureSignResponse.newBuilder().setSignature(ByteString.copyFrom(signatureValue)).build();
57     } catch (GeneralSecurityException e)  {
58       return SignatureSignResponse.newBuilder().setErr(e.toString()).build();
59     }
60   }
61 
62   @Override
sign( SignatureSignRequest request, StreamObserver<SignatureSignResponse> responseObserver)63   public void sign(
64       SignatureSignRequest request, StreamObserver<SignatureSignResponse> responseObserver) {
65     try {
66       responseObserver.onNext(sign(request));
67       responseObserver.onCompleted();
68     } catch (GeneralSecurityException e) {
69       responseObserver.onError(e);
70     }
71   }
72 
verify(SignatureVerifyRequest request)73   private SignatureVerifyResponse verify(SignatureVerifyRequest request)
74       throws GeneralSecurityException {
75     try {
76       PublicKeyVerify verifier =
77           Util.parseBinaryProtoKeyset(request.getPublicAnnotatedKeyset())
78               .getPrimitive(PublicKeyVerify.class);
79       verifier.verify(request.getSignature().toByteArray(), request.getData().toByteArray());
80       return SignatureVerifyResponse.getDefaultInstance();
81     } catch (GeneralSecurityException e) {
82       return SignatureVerifyResponse.newBuilder().setErr(e.toString()).build();
83     }
84   }
85 
86   @Override
verify( SignatureVerifyRequest request, StreamObserver<SignatureVerifyResponse> responseObserver)87   public void verify(
88       SignatureVerifyRequest request, StreamObserver<SignatureVerifyResponse> responseObserver) {
89     try {
90       responseObserver.onNext(verify(request));
91       responseObserver.onCompleted();
92     } catch (GeneralSecurityException e) {
93       responseObserver.onError(e);
94     }
95   }
96 }
97